GenerateMojo extends AbstractMojo { @VisibleForTesting void generateParser(File cupFile) throws IOException, MojoExecutionException { String javaPackage = findJavaPackage(cupFile); mavenProject.addCompileSourceRoot(generatedSourcesDirectory.getPath()); boolean skipGeneration = !force && !isGeneratedCodeOutdated(cupFile, javaPackage); if (skipGeneration) { log.i("Do nothing. Generated code for is up to date for: %s", cupFile.getName()); return; } else if (force) { log.i("Generation requested by force for: %s", cupFile.getName()); } try { log.d("Generate CUP parser for %s", cupFile.getAbsolutePath()); File outputDirectory = JavaUtils.directory(generatedSourcesDirectory, javaPackage); log.d("CUP output directory: %s", outputDirectory); if (!outputDirectory.exists()) { if (!outputDirectory.mkdirs()) { throw new IOException("Could not create " + outputDirectory); } } cupInvoker.invoke( javaPackage, outputDirectory, parserName, symbolsName, symbolInterface, cupFile.getAbsolutePath()); log.i("CUP generated %s and %s for %s", parserName, symbolsName, cupFile.getPath()); } catch (Exception e) { throw new MojoExecutionException( "CUP failed to generate parser for " + cupFile.getAbsolutePath(), e); } } @SuppressWarnings("WeakerAccess")  GenerateMojo(); @VisibleForTesting  GenerateMojo(CliCupInvoker cupInvoker, Log logger); @Override void execute();  }
@Test public void generateParser_cliInvocation() throws Exception { ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("test.cup").getFile()); mojo.generateParser(file); File expectedDestDir = new File(tempTargetGeneratedCupDirectory, "/foo/bar"); verify(mockCupInvoker) .invoke( eq("foo.bar"), eq(expectedDestDir), eq("parser"), eq("sym"), eq(false), endsWith("test.cup")); verify(mockLogger) .debug("Parser file for test.cup is not actual: " + expectedDestDir + "/parser.java"); }
@Test public void generateParser_cliInvocation_interface() throws Exception { mojo.symbolInterface = true; ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("test.cup").getFile()); mojo.generateParser(file); File expectedDestDir = new File(tempTargetGeneratedCupDirectory, "/foo/bar"); verify(mockCupInvoker) .invoke( eq("foo.bar"), eq(expectedDestDir), eq("parser"), eq("sym"), eq(true), endsWith("test.cup")); verify(mockLogger) .debug("Parser file for test.cup is not actual: " + expectedDestDir + "/parser.java"); }