MainInstrumentMojo extends AbstractInstrumentMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { Log log = getLog(); File mainOutputFolder = new File(getProject().getBuild().getOutputDirectory()); if (!mainOutputFolder.isDirectory()) { log.warn("Main folder doesn't exist -- nothing to instrument"); return; } List<String> classpath; try { classpath = getProject().getCompileClasspathElements(); } catch (DependencyResolutionRequiredException ex) { throw new MojoExecutionException("Dependency resolution problem", ex); } log.debug("Processing main output folder ... "); instrumentPath(log, classpath, mainOutputFolder); }  @Override void execute();  }
@Test public void mustInstrumentClasses() throws Exception { byte[] classContent = readZipFromResource("NormalInvokeTest.zip").get("NormalInvokeTest.class"); File mainDir = null; try { mainDir = Files.createTempDirectory(getClass().getSimpleName()).toFile(); File mainClass = new File(mainDir, "NormalInvokeTest.class"); FileUtils.writeByteArrayToFile(mainClass, classContent); Mockito.when(mavenProject.getCompileClasspathElements()).thenReturn(Collections.emptyList()); Build build = Mockito.mock(Build.class); Mockito.when(mavenProject.getBuild()).thenReturn(build); Mockito.when(build.getOutputDirectory()).thenReturn(mainDir.getAbsolutePath()); fixture.execute(); byte[] modifiedMainClassContent = FileUtils.readFileToByteArray(mainClass); assertTrue(modifiedMainClassContent.length > classContent.length); } finally { if (mainDir != null) { FileUtils.deleteDirectory(mainDir); } } }
@Test public void mustNotThrowExceptionWhenDirectoryDoesntExist() throws Exception { File mainDir = null; try { mainDir = Files.createTempDirectory(getClass().getSimpleName()).toFile(); File fakeFolder = new File(mainDir, "DOESNOTEXIST"); Mockito.when(mavenProject.getCompileClasspathElements()).thenReturn(Collections.emptyList()); Build build = Mockito.mock(Build.class); Mockito.when(mavenProject.getBuild()).thenReturn(build); Mockito.when(build.getOutputDirectory()).thenReturn(fakeFolder.getAbsolutePath()); fixture.execute(); } finally { if (mainDir != null) { FileUtils.deleteDirectory(mainDir); } } }