Skip to content

Commit

Permalink
Add javax.annotation to the jflex-testsuite-maven-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
regisd committed May 12, 2020
1 parent d586375 commit ac227fd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
10 changes: 10 additions & 0 deletions testsuite/jflex-testsuite-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,15 @@
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jflex.maven.plugin.testsuite;

import static java.util.stream.Collectors.joining;

import com.google.common.base.Joiner;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -52,9 +54,10 @@ private static String[] toArray(List<String> a, List<String> b) {
*
* @param javaSourceFiles A list of files to compile, or {@code null}
* @param dir Source directory.
* @param additionalJars Files to add to the classpath for the compilation.
*/
public static TestResult execJavac(
List<String> javaSourceFiles, File dir, String additionalJars, String encoding)
List<String> javaSourceFiles, File dir, List<File> additionalJars, String encoding)
throws FileNotFoundException {
// javac fails if an input file doesn't exist
checkFilesExist(javaSourceFiles, dir);
Expand All @@ -73,7 +76,10 @@ public static TestResult execJavac(
javac.setEncoding(encoding);
Path classPath = javac.createClasspath();
// Locate the jflex jar in the user's Maven local repository
classPath.setPath(additionalJars);
classPath.setPath(
additionalJars.stream()
.map(File::getPath)
.collect(joining(String.valueOf(File.pathSeparatorChar))));

ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream outSafe = System.err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.RepositorySystemSession;

/** Runs test cases in the JFlex test suite */
@Mojo(name = "run-test-suite", defaultPhase = LifecyclePhase.TEST)
Expand All @@ -35,16 +37,24 @@ public class JFlexTestsuiteMojo extends AbstractMojo {
defaultValue = "${project.parent.basedir}/jflex/target/jflex-full-${project.version}.jar")
private String jflexUberJarFilename;

/** */
/** The current repository/network configuration of Maven. */
@Parameter(defaultValue = "${repositorySystemSession}")
private RepositorySystemSession repoSession;

public void execute() throws MojoExecutionException, MojoFailureException {
boolean success = true;
File jflexUberJar = new File(jflexUberJarFilename);
File javaxAnnotationJar =
new File(
repoSession.getLocalRepository().getBasedir(),
"javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar");
try {
System.setOut(new PrintStream(System.out, true));
List<File> files = new ArrayList<>();
getLog().info("JFlex: " + jflexUberJar.getAbsolutePath());
getLog()
.info("Testing version: " + PomUtils.getPomVersion("de.jflex", "jflex", jflexUberJar));
getLog().debug("javax.annotation jar: " + javaxAnnotationJar);
getLog().info("Test directory: " + testDirectory);
getLog().info("Test case(s): " + (null == testcases ? "All" : testcases));

Expand All @@ -65,7 +75,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
TestsuiteUtils.verbose = verbose;
getLog().info("verbose: " + verbose);

success = TestsuiteUtils.runTests(files, jflexUberJar);
success = TestsuiteUtils.runTests(files, Arrays.asList(jflexUberJar, javaxAnnotationJar));

} catch (Exception e) {
throw new MojoExecutionException("Failed to execute test suite: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void init(File testDir) {
testPath = testDir;
}

void createScanner(File jflexUberJar, boolean verbose)
void createScanner(List<File> classpath, boolean verbose)
throws TestFailException, MojoFailureException {
File lexFile = new File(testPath, testName + ".flex");
if (verbose) {
Expand Down Expand Up @@ -208,8 +208,7 @@ void createScanner(File jflexUberJar, boolean verbose)
System.out.println("File(s) to compile: " + toCompile);
}
try {
TestResult javacResult =
ExecUtils.execJavac(toCompile, testPath, jflexUberJar.getAbsolutePath(), javacEncoding);
TestResult javacResult = ExecUtils.execJavac(toCompile, testPath, classpath, javacEncoding);

// System.out.println(javacResult);
if (TestsuiteUtils.verbose) {
Expand Down Expand Up @@ -277,7 +276,7 @@ boolean hasMoreToDo() {
return !(inputOutput.isEmpty());
}

void runNext(File jflexUberJar) throws TestFailException, UnsupportedEncodingException {
void runNext(List<File> classpath) throws TestFailException, UnsupportedEncodingException {
// Get first file and remove it from list
InputOutput current = inputOutput.remove(0);
// Create List with only first input in
Expand All @@ -287,15 +286,9 @@ void runNext(File jflexUberJar) throws TestFailException, UnsupportedEncodingExc
List<String> cmdLine = new ArrayList<>();
cmdLine.add("--encoding");
cmdLine.add(inputFileEncoding);
List<File> additionalJars = ImmutableList.of(jflexUberJar);
TestResult classExecResult =
ExecUtils.execClass(
className,
testPath.toString(),
inputFiles,
additionalJars,
outputFileEncoding,
cmdLine);
className, testPath.toString(), inputFiles, classpath, outputFileEncoding, cmdLine);
if (TestsuiteUtils.verbose) {
System.out.println("Running scanner on [" + current.getName() + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TestsuiteUtils {

private TestsuiteUtils() {}

public static boolean verbose;
static boolean verbose;
public static String jflexTestVersion;

public static final String version = "1.0alpha";
Expand All @@ -28,7 +28,7 @@ public static void showUsage(String error) {
*
* @return a list of files
*/
public static List<File> scan(File dir, final String extension, boolean recursive) {
static List<File> scan(File dir, final String extension, boolean recursive) {
List<File> result = new ArrayList<>();

FilenameFilter extFilter =
Expand Down Expand Up @@ -65,11 +65,11 @@ public boolean accept(File f, String name) {

/**
* @param tests a list of File
* @param jflexUberJar The JFlex shaded jar
* @param classPath the files to add in the classpath
* @return true if all tests succeeded, false otherwise
*/
public static boolean runTests(List<File> tests, File jflexUberJar)
throws TestFailException, MojoExecutionException, MojoFailureException {
static boolean runTests(List<File> tests, List<File> classPath)
throws MojoExecutionException, MojoFailureException {
int successCount = 0;
int totalCount = 0;

Expand All @@ -91,8 +91,10 @@ public static boolean runTests(List<File> tests, File jflexUberJar)
if (verbose) System.out.println("Loaded successfully"); // - Details:\n"+currentTest);

if (currentTest.checkJavaVersion()) {
currentTest.createScanner(jflexUberJar, verbose);
while (currentTest.hasMoreToDo()) currentTest.runNext(jflexUberJar);
currentTest.createScanner(classPath, verbose);
while (currentTest.hasMoreToDo()) {
currentTest.runNext(classPath);
}

successCount++;
System.out.println("Test [" + test + "] finished successfully.");
Expand Down

0 comments on commit ac227fd

Please sign in to comment.