OpearPackageMojo extends AbstractCommonMojo { @Override public void execute() throws MojoExecutionException, MojoFailureException { resolveDependencies(project.getDependencies(), true); try { final File finalFile = assembleOpear(); attachArtifact(finalFile); } catch (Exception e) { throw new MojoExecutionException("Failed to assemble the opear file.", e); } }  @Override void execute(); static final String OPEAR; }
@Test(expected = MojoExecutionException.class) public void testExecute_fails() throws Exception { final OpearPackageMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); when(project.clone()).thenReturn(project); mojo.project = project; final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); mojo.execute(); }
@Test public void testExecute_asJar() throws Exception { final File preInstallPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); final File testOutDir = new File(testOutBaseDir, "testExecute_asJar"); FileUtils.deleteDirectory(testOutDir); testOutDir.mkdirs(); final OpearPackageMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); mojo.project = project; when(project.getPackaging()).thenReturn("jar"); final String expectGroupId = "com.example.testAssembleOpear"; final String expectArtifactId = "assembly1"; final String expectVersion = "1.0-SNAPSHOT"; when(project.getGroupId()).thenReturn(expectGroupId); when(project.getArtifactId()).thenReturn(expectArtifactId); when(project.getVersion()).thenReturn(expectVersion); final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); final RepositorySystem repositorySystem = mock(RepositorySystem.class); mojo.repositorySystem = repositorySystem; final ArtifactHandlerManager artifactHandlerManager = mock(ArtifactHandlerManager.class); mojo.artifactHandlerManager = artifactHandlerManager; final ArtifactHandler opearHandler = mock(ArtifactHandler.class); when(artifactHandlerManager.getArtifactHandler(OPEAR)).thenReturn(opearHandler); final File basedir = new File(testOutDir, "basedir"); final File projectFile = new File(basedir, "pom.xml"); FileUtils.touch(projectFile); when(project.getFile()).thenReturn(projectFile); final File target = new File(basedir, "target"); target.mkdirs(); mojo.outputDirectory = target; final File targetClasses = new File(target, "classes"); targetClasses.mkdirs(); final Build build = mock(Build.class); when(project.getBuild()).thenReturn(build); when(build.getDirectory()).thenReturn(target.getPath()); when(build.getOutputDirectory()).thenReturn(targetClasses.getPath()); FileUtils.copyFile(new File(srcDir, "echo.js"), new File(targetClasses, "echo.js")); final String expectFinalName = expectArtifactId + "-" + expectVersion; mojo.finalName = expectFinalName; final File fakeJar = new File(target, expectFinalName + ".jar"); FileUtils.touch(fakeJar); final Artifact projectArtifact = mock(Artifact.class); when(projectArtifact.getType()).thenReturn("jar"); when(project.getArtifact()).thenReturn(projectArtifact); when(projectArtifact.getFile()).thenReturn(fakeJar); final String expectOakpalVersion = "0.31415926535898"; final Dependency oakpalCoreDep = new DependencyFilter() .withGroupId(OAKPAL_GROUP_ID) .withArtifactId(OAKPAL_CORE_ARTIFACT_ID) .withVersion(expectOakpalVersion) .toDependency(); final File oakpalDepFile = new File(testOutDir, "oakpal-core.jar"); FileUtils.touch(oakpalDepFile); final Artifact oakpalArt = mock(Artifact.class); when(oakpalArt.getGroupId()).thenReturn(OAKPAL_GROUP_ID); when(oakpalArt.getArtifactId()).thenReturn(OAKPAL_CORE_ARTIFACT_ID); when(oakpalArt.getVersion()).thenReturn(expectOakpalVersion); when(oakpalArt.getScope()).thenReturn("compile"); when(oakpalArt.getFile()).thenReturn(oakpalDepFile); final Dependency compileDep = new DependencyFilter().toDependency("compile"); final File compileDepFile = new File(testOutDir, "compile-scope.jar"); FileUtils.touch(compileDepFile); final Artifact compileArt = mock(Artifact.class); when(compileArt.getGroupId()).thenReturn(OAKPAL_GROUP_ID); when(compileArt.getArtifactId()).thenReturn("compile-scope"); when(compileArt.getScope()).thenReturn("compile"); when(compileArt.getFile()).thenReturn(compileDepFile); when(project.getDependencies()).thenReturn(Arrays.asList(oakpalCoreDep, compileDep)); doAnswer(call -> { final Dependency input = call.getArgument(0, Dependency.class); if (input == oakpalCoreDep) { return oakpalArt; } else if (input == compileDep) { return compileArt; } else { return null; } }).when(repositorySystem).createDependencyArtifact(any(Dependency.class)); doAnswer(call -> { final ArtifactResolutionRequest request = call.getArgument(0, ArtifactResolutionRequest.class); final ArtifactResolutionResult result = mock(ArtifactResolutionResult.class); when(result.getArtifacts()).thenReturn(Collections.singleton(request.getArtifact())); return result; }).when(repositorySystem).resolve(any(ArtifactResolutionRequest.class)); final String expectFilename = "sample-plan.json"; final File samplePlanFile = new File(srcDir, expectFilename); mojo.planFile = samplePlanFile; when(project.clone()).thenReturn(project); final File expectFinalFile = new File(target, expectFinalName + "." + OPEAR); final CompletableFuture<Artifact> attachedSlot = new CompletableFuture<>(); doAnswer(call -> attachedSlot.complete(call.getArgument(0, Artifact.class))) .when(project).addAttachedArtifact(any(Artifact.class)); mojo.execute(); final Artifact attached = attachedSlot.getNow(null); assertNotNull("attached is present", attached); assertEquals("expect opear file", expectFinalFile, attached.getFile()); try (JarFile jarFile = new JarFile(expectFinalFile)) { final Set<String> allEntries = jarFile.stream().map(JarEntry::getName).collect(Collectors.toSet()); final List<String> expectEntries = Arrays.asList( "lib/" + fakeJar.getName(), "lib/" + compileDepFile.getName(), "tmp_foo_bar.zip", expectFilename ); for (String expectEntry : expectEntries) { assertTrue(String.format("should have entry %s in %s", expectEntry, allEntries), jarFile.stream().anyMatch(entry -> expectEntry.equals(entry.getName()))); } final List<String> expectNoEntries = Arrays.asList( "lib/classes/", "lib/classes/echo.js", "lib/" + oakpalDepFile.getName() ); for (String noExpectEntry : expectNoEntries) { assertTrue(String.format("should not have entry %s in %s", noExpectEntry, allEntries), jarFile.stream().noneMatch(entry -> noExpectEntry.equals(entry.getName()))); } final Manifest manifest = jarFile.getManifest(); assertEquals("expect bsn", Collections.singletonList(expectArtifactId + "-" + OPEAR), Util.getManifestHeaderValues(manifest, "Bundle-SymbolicName")); assertEquals("expect bcp", Arrays.asList("lib/" + fakeJar.getName(), "lib/" + compileDepFile.getName()), Util.getManifestHeaderValues(manifest, "Bundle-ClassPath")); assertEquals("expect oakpal version", Collections.singletonList(expectOakpalVersion), Util.getManifestHeaderValues(manifest, "Oakpal-Version")); assertEquals("expect oakpal plan", Collections.singletonList(expectFilename), Util.getManifestHeaderValues(manifest, "Oakpal-Plan")); } }
@Test(expected = MojoExecutionException.class) public void testExecute_failToAssemble() throws Exception { final OpearPackageMojo mojo = newMojo(); final MavenProject project = mock(MavenProject.class); when(project.clone()).thenReturn(project); mojo.project = project; final MavenSession session = mock(MavenSession.class); mojo.session = session; final MavenExecutionRequest executionRequest = mock(MavenExecutionRequest.class); when(session.getRequest()).thenReturn(executionRequest); mojo.execute(); }