Skip to content

Commit

Permalink
Merge pull request #19 from TechnologyBrewery/15-expose-root-project-…
Browse files Browse the repository at this point in the history
…directory

#15: update Fermenter to expose root project directory
  • Loading branch information
chang-annie authored Jun 26, 2023
2 parents 32c1ec7 + 2932041 commit e7a9312
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.logging.LogFactory;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -135,6 +136,9 @@ public class GenerateSourcesMojo extends AbstractMojo {

private VelocityEngine engine;

@Parameter(property = "session", required = true, readonly = true)
protected MavenSession session;

/**
* Creates a {@link GenerateSourcesHelper.LoggerDelegate} implementation for use with
* {@link GenerateSourcesHelper} that delegates to the {@link Log} that has
Expand Down Expand Up @@ -468,7 +472,7 @@ private Exception handleInvalidProfile(String targetProfile, Collection<Expanded
* @return {@link GenerationContext} that can be provided to the given
* {@link Target}'s {@link Generator} to execute code generation.
*/
private GenerationContext createGenerationContext(Target target) {
protected GenerationContext createGenerationContext(Target target) {
GenerationContext context = new GenerationContext(target);
context.setStatisticsService(statisticsService);
context.setBasePackage(basePackage);
Expand All @@ -486,6 +490,7 @@ private GenerationContext createGenerationContext(Target target) {
context.setScmUrl(project.getScm().getUrl());
}
context.setPropertyVariables(propertyVariables);
context.setExecutionRootDirectory(session.getExecutionRootDirectory());

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class GenerationContext {
private String scmUrl;
private Map<String, String> propertyVariables;
private StatisticsService statisticsService;
private String executionRootDirectory;

public VelocityEngine getEngine() {
return engine;
Expand Down Expand Up @@ -203,5 +204,13 @@ public StatisticsService getStatisticsService() {
public void setStatisticsService(StatisticsService statisticsService) {
this.statisticsService = statisticsService;
}

public String getExecutionRootDirectory() {
return executionRootDirectory;
}

public void setExecutionRootDirectory(String executionRootDirectory){
this.executionRootDirectory = executionRootDirectory;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.technologybrewery.fermenter.mda;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
import org.technologybrewery.fermenter.mda.element.Target;
import org.technologybrewery.fermenter.mda.generator.GenerationContext;

import java.io.File;

public class GenerationContextSteps {
private MojoTestCaseWrapper mojoTestCase = new MojoTestCaseWrapper();
protected File mavenProjectBaseDir;
protected GenerateSourcesMojo generateSourcesMojo;
protected GenerationContext context;

@Before("@generationContext")
public void configureMavenPluginTestHarness() throws Exception {
mojoTestCase.configurePluginTestHarness();
}

@After("@generationContext")
public void tearDownMavenPluginTestHarness() throws Exception {
mojoTestCase.tearDownPluginTestHarness();
}

/*
This is an approximation of using a submodule whose base directory is different from the base directory set in test maven session. While not an exact replica, it highlights that we aren't giving a local base directory from the loaded pom.
*/
@Given("^generation in a Maven submodule$")
public void generation_in_a_Maven_submodule() throws Throwable {
mavenProjectBaseDir = new File("src/test/resources/plugin-testing-harness-pom-files/java-default-config");
generateSourcesMojo = (GenerateSourcesMojo) mojoTestCase.lookupConfiguredMojo(new File(mavenProjectBaseDir, "pom.xml"), "generate-sources");
generateSourcesMojo.updateMojoConfigsBasedOnLanguage();
generateSourcesMojo.validateMojoConfigs();
}

@When("^the generation context is created$")
public void the_generation_context_is_created() throws Throwable {
Target target = new Target();
context = generateSourcesMojo.createGenerationContext(target);
}

@Then("^access to the root module's base directory is available$")
public void access_to_the_root_module_s_base_directory_is_available() throws Throwable {
Assert.assertNotNull(context.getExecutionRootDirectory());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void tearDownPluginTestHarness() throws Exception {
*/
public MavenSession newMavenSession() throws Exception {
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(new File( "target"));
MavenExecutionResult result = new DefaultMavenExecutionResult();

// Populates sensible defaults, including repository basedir and remote repos
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@generationContext
Feature: Expose project data to generation context

Scenario: The execution root is available to generators
Given generation in a Maven submodule
When the generation context is created
Then access to the root module's base directory is available

0 comments on commit e7a9312

Please sign in to comment.