Skip to content

Commit

Permalink
Fix issues #20 and #21
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcesarf committed Apr 22, 2020
1 parent 49b48a3 commit 7f5c529
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
34 changes: 33 additions & 1 deletion src/main/java/com/khubla/antlr/antlr4test/GrammarTestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public class GrammarTestMojo extends AbstractMojo {
@Parameter
private List<Scenario> scenarios = null;

/* read outputDirectory from pom project.build.outputDirectory */
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true)
private String outputDirectory = "/target/classes";

/* read testOutputDirectory from pom project.build.testOutputDirectory */
@Parameter(defaultValue = "${project.build.testOutputDirectory}", readonly = true)
private String testOutputDirectory = "/target/test-classes";

/**
* ctor
*
Expand Down Expand Up @@ -268,6 +276,30 @@ public void setScenarios(List<Scenario> scenarios) {
this.scenarios = scenarios;
}

public String getGrammarInitializer() {
return grammarInitializer;
}

public void setGrammarInitializer(String grammarInitializer) {
this.grammarInitializer = grammarInitializer;
}

public String getOutputDirectory() {
return outputDirectory;
}

public void setOutputDirectory(String outputDirectory) {
this.outputDirectory = outputDirectory;
}

public String getTestOutputDirectory() {
return testOutputDirectory;
}

public void setTestOutputDirectory(String testOutputDirectory) {
this.testOutputDirectory = testOutputDirectory;
}

private void testScenarios() throws Exception {
Log mojoLogger = getLog();
// check if baseDir has been set with some value.
Expand Down Expand Up @@ -310,7 +342,7 @@ private void testScenarios() throws Exception {
}
}
if (scenario.isEnabled()) {
ScenarioExecutor executor = new ScenarioExecutor(scenario, mojoLogger);
ScenarioExecutor executor = new ScenarioExecutor(this, scenario, mojoLogger);
executor.testGrammars();
executor = null;
} else {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/khubla/antlr/antlr4test/ScenarioExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ public class ScenarioExecutor {

private Scenario scenario = null;
private Log log = null;
private GrammarTestMojo mojo = null;
private HashMap<URL, ClassLoader> classLoaderMap = new HashMap<>();

public ScenarioExecutor(Scenario scenario, Log log) {
public ScenarioExecutor(GrammarTestMojo mojo, Scenario scenario, Log log) {
this.mojo = mojo;
this.scenario = scenario;
this.log = log;
}
Expand Down Expand Up @@ -109,10 +111,10 @@ private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
* classloader
*/
// create grammarClassLoader as child of current thread's context classloader
final ClassLoader grammarClassLoader = getClassLoader(scenario, "/target/classes");
final ClassLoader grammarClassLoader = getClassLoader(mojo.getOutputDirectory());
// testClassLoader should be grammarClassLoader's child so test classes can find
// grammar classes
final ClassLoader testClassLoader = getClassLoader(scenario, "/target/test-classes", grammarClassLoader);
final ClassLoader testClassLoader = getClassLoader(mojo.getTestOutputDirectory(), grammarClassLoader);
/*
* get the classes we need
*/
Expand Down Expand Up @@ -220,9 +222,9 @@ private void testGrammar(Scenario scenario, File grammarFile) throws Exception {
/**
* build a classloader that can find the files we need
*/
private ClassLoader getClassLoader(Scenario scenario, String relativePath, ClassLoader parent)
private ClassLoader getClassLoader(String path, ClassLoader parent)
throws MalformedURLException, ClassNotFoundException {
final URL antlrGeneratedURL = new File(scenario.getBaseDir(), relativePath).toURI().toURL();
final URL antlrGeneratedURL = new File(path).toURI().toURL();
// check if classloader for this URL was already created.
ClassLoader ret = classLoaderMap.get(antlrGeneratedURL);
if (ret == null) {
Expand All @@ -234,9 +236,9 @@ private ClassLoader getClassLoader(Scenario scenario, String relativePath, Class
return ret;
}

private ClassLoader getClassLoader(Scenario scenario, String relativePath)
private ClassLoader getClassLoader(String path)
throws MalformedURLException, ClassNotFoundException {
// create a classloader child of Thread.currentThread().getContextClassLoader().
return getClassLoader(scenario, relativePath, Thread.currentThread().getContextClassLoader());
return getClassLoader(path, Thread.currentThread().getContextClassLoader());
}
}

0 comments on commit 7f5c529

Please sign in to comment.