Adds unit tests for the dependency management

This commit is contained in:
Marco Hunsicker 2016-11-14 00:37:41 +01:00
parent 7944fafcb2
commit 5d8dcf019d
15 changed files with 543 additions and 11 deletions

View File

@ -48,6 +48,11 @@
<!-- Ancilliary information for completeness -->
<inceptionYear>2009</inceptionYear>
<properties>
<mavenVersion>3.3.9</mavenVersion>
<takariLifecycleVersion>1.12.2</takariLifecycleVersion>
</properties>
<!-- ============================================================================= -->
<!-- What are we depedent on for the Mojos to execute? We need the plugin
@ -63,11 +68,6 @@
<version>3.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
@ -93,21 +93,50 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-plugin-testing</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.15</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/test</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -132,6 +161,21 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-lifecycle-plugin</artifactId>
<version>${takariLifecycleVersion}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>testProperties</id>
<phase>process-test-resources</phase>
<goals>
<goal>testProperties</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -0,0 +1,319 @@
package org.antlr.mojo.antlr4;
import io.takari.maven.testing.TestMavenRuntime;
import io.takari.maven.testing.TestResources;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Antlr4MojoTest {
@Rule
public final TestResources resources = new TestResources();
@Rule
public final TestMavenRuntime maven = new TestMavenRuntime();
@Test
public void importTokens() throws Exception {
Map.Entry<ByteArrayOutputStream, PrintStream> stdout = redirect();
ByteArrayOutputStream output = stdout.getKey();
try {
File baseDir = resources.getBasedir("importTokens");
File antlrDir = new File(baseDir, "src/main/antlr4");
File tokens = new File(antlrDir, "imports/SimpleLexer.tokens");
MavenProject project = maven.readMavenProject(baseDir);
MavenSession session = maven.newMavenSession(project);
MojoExecution exec = maven.newMojoExecution("antlr4");
////////////////////////////////////////////////////////////////////////
// 1st - all grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/SimpleParser.g4"), processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 2nd - nothing has been modified, no grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Collections.emptyList(), processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 3rd - the imported grammar changed, every dependency has to be processed
////////////////////////////////////////////////////////////////////////
String original = modify(tokens);
try {
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/SimpleParser.g4"),
processAndReset(output));
} finally {
write(tokens, original);
}
} finally {
System.setOut(stdout.getValue());
}
}
@Test
public void importsCustomLayout() throws Exception {
Map.Entry<ByteArrayOutputStream, PrintStream> stdout = redirect();
ByteArrayOutputStream output = stdout.getKey();
try {
File baseDir = resources.getBasedir("importsCustom");
File antlrDir = new File(baseDir, "src/main/antlr4");
File generatedSources = new File(baseDir, "target/generated-sources/antlr4");
File baseGrammar = new File(antlrDir, "imports/TestBaseLexer.g4");
File lexerGrammar = new File(antlrDir, "TestLexer.g4");
File parserGrammar = new File(antlrDir, "TestParser.g4");
File lexerTokens = new File(generatedSources, "TestLexer.tokens");
Xpp3Dom outputDirectory = TestMavenRuntime.newParameter("outputDirectory",
"src/main/java/com/foo");
Xpp3Dom arguments = new Xpp3Dom("arguments");
arguments.addChild(TestMavenRuntime.newParameter("argument", "-package"));
arguments.addChild(TestMavenRuntime.newParameter("argument", "foo"));
MavenProject project = maven.readMavenProject(baseDir);
MavenSession session = maven.newMavenSession(project);
MojoExecution exec = maven.newMojoExecution("antlr4", outputDirectory,
arguments);
////////////////////////////////////////////////////////////////////////
// 1st - all grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("Hello.g4", "TestLexer.g4", "TestParser.g4"),
processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 2nd - nothing has been modified, no grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Collections.emptyList(), processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 3rd - the imported grammar changed, every dependency has to be processed
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
String original = modify(baseGrammar);
try {
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("TestLexer.g4", "TestParser.g4"),
processAndReset(output));
} finally {
write(baseGrammar, original);
}
////////////////////////////////////////////////////////////////////////
// 4th - the lexer grammar changed, the parser grammar has to be processed as well
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
original = modify(lexerGrammar);
try {
lexerTokens.delete();
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("TestLexer.g4", "TestParser.g4"),
processAndReset(output));
} finally {
write(lexerGrammar, original);
}
////////////////////////////////////////////////////////////////////////
// 5th - the parser grammar changed, no other grammars have to be processed
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
original = modify(parserGrammar);
try {
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("TestParser.g4"), processAndReset(output));
} finally {
write(parserGrammar, original);
}
} finally {
System.out.flush();
System.setOut(stdout.getValue());
}
}
@Test
public void importsStandardLayout() throws Exception {
Map.Entry<ByteArrayOutputStream, PrintStream> stdout = redirect();
ByteArrayOutputStream output = stdout.getKey();
try {
File baseDir = resources.getBasedir("importsStandard");
File antlrDir = new File(baseDir, "src/main/antlr4");
File generatedSources = new File(baseDir, "target/generated-sources/antlr4");
File baseGrammar = new File(antlrDir, "imports/TestBaseLexer.g4");
File lexerGrammar = new File(antlrDir, "test/TestLexer.g4");
File parserGrammar = new File(antlrDir, "test/TestParser.g4");
File lexerTokens = new File(generatedSources, "TestLexer.tokens");
MavenProject project = maven.readMavenProject(baseDir);
MavenSession session = maven.newMavenSession(project);
MojoExecution exec = maven.newMojoExecution("antlr4");
////////////////////////////////////////////////////////////////////////
// 1st - all grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/Hello.g4", "test/TestLexer.g4",
"test/TestParser.g4"), processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 2nd - nothing has been modified, no grammars have to be processed
////////////////////////////////////////////////////////////////////////
maven.executeMojo(session, project, exec);
assertEquals(Collections.emptyList(), processAndReset(output));
////////////////////////////////////////////////////////////////////////
// 3rd - the imported grammar changed, every dependency has to be processed
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
String original = modify(baseGrammar);
try {
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/TestLexer.g4", "test/TestParser.g4"),
processAndReset(output));
} finally {
write(baseGrammar, original);
}
////////////////////////////////////////////////////////////////////////
// 4th - the lexer grammar changed, the parser grammar has to be processed as well
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
original = modify(lexerGrammar);
try {
lexerTokens.delete();
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/TestLexer.g4", "test/TestParser.g4"),
processAndReset(output));
} finally {
write(lexerGrammar, original);
}
////////////////////////////////////////////////////////////////////////
// 5th - the parser grammar changed, no other grammars have to be processed
////////////////////////////////////////////////////////////////////////
// modify the grammar to make checksum comparison detect a change
original = modify(parserGrammar);
try {
maven.executeMojo(session, project, exec);
assertEquals(Arrays.asList("test/TestParser.g4"),
processAndReset(output));
} finally {
write(parserGrammar, original);
}
} finally {
System.out.flush();
System.setOut(stdout.getValue());
}
}
private List<String> processAndReset(ByteArrayOutputStream output) {
Pattern pattern = Pattern.compile("Processing grammar: (.+)");
Matcher matcher = pattern.matcher(output.toString());
// we reset here simply for convenience
output.reset();
List<String> result = new ArrayList<String>();
while (matcher.find()) {
result.add(matcher.group(1));
}
Collections.sort(result);
return result;
}
private String modify(File file) throws IOException {
String content = text(file);
write(file, content + "\n");
return content;
}
private void write(File file, String text) throws IOException {
Files.write(file.toPath(), text.getBytes(StandardCharsets.UTF_8));
}
private String text(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
}
private Map.Entry<ByteArrayOutputStream, PrintStream> redirect() {
// there does not seem to be a clean way to intercept logging output
ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream old = System.out;
System.setOut(new PrintStream(buf));
return new AbstractMap.SimpleEntry<ByteArrayOutputStream, PrintStream>(buf, old);
}
}

View File

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>import.tokens</groupId>
<artifactId>importTokens</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test importing tokens file</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>antlr4-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,8 @@
parser grammar SimpleParser;
options {
// get token types from SimpleLexer.tokens; don't name it
// SimpleParser.tokens as ANTLR will overwrite!
tokenVocab=SimpleLexer;
}
s : ( ID | INT )* SEMI ;

View File

@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>imports.custom</groupId>
<artifactId>importsCustom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test importing, custom layout</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>antlr4-maven-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/src/main/java/com/foo</outputDirectory>
<arguments>
<argument>-visitor</argument>
<argument>-no-listener</argument>
<argument>-Xlog</argument>
<argument>-package</argument>
<argument>com.foo</argument>
</arguments>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,4 @@
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
WS : [ \r\t\n]+ -> skip ;

View File

@ -0,0 +1,6 @@
lexer grammar TestLexer;
import TestBaseLexer;
WS : Whitespace+ -> skip;
TEXT : ~[<&]+ ; // match any 16 bit char other than < and &

View File

@ -0,0 +1,5 @@
parser grammar TestParser;
options { tokenVocab=TestLexer; }
document : (Comment | Name) EOF ;

View File

@ -0,0 +1,16 @@
lexer grammar TestBaseLexer;
tokens { Name }
// Default "mode": Everything OUTSIDE of a tag
Comment : '<!--' .*? '-->' ;
CDSect : '<![CDATA[' .*? ']]>' ;
fragment
Whitespace : ' ' | '\n' | '\t' | '\r' ;
fragment
Hexdigit : [a-fA-F0-9] ;
fragment
Digit : [0-9] ;

View File

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>imports.standard</groupId>
<artifactId>importsStandard</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test importing, standard layout</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>antlr4-maven-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,16 @@
lexer grammar TestBaseLexer;
tokens { Name }
// Default "mode": Everything OUTSIDE of a tag
Comment : '<!--' .*? '-->' ;
CDSect : '<![CDATA[' .*? ']]>' ;
fragment
Whitespace : ' ' | '\n' | '\t' | '\r' ;
fragment
Hexdigit : [a-fA-F0-9] ;
fragment
Digit : [0-9] ;

View File

@ -0,0 +1,4 @@
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
WS : [ \r\t\n]+ -> skip ;

View File

@ -0,0 +1,6 @@
lexer grammar TestLexer;
import TestBaseLexer;
WS : Whitespace+ -> skip;
TEXT : ~[<&]+ ; // match any 16 bit char other than < and &

View File

@ -0,0 +1,5 @@
parser grammar TestParser;
options { tokenVocab=TestLexer; }
document : (Comment | Name) EOF ;