@Test
public void testAttachArtifact() throws Exception {

    final File testOutDir = new File(testOutBaseDir, "testAttachArtifact");
    FileUtils.deleteDirectory(testOutDir);
    testOutDir.mkdirs();
    final OpearPackageMojo mojo = newMojo();
    final MavenProject project = mock(MavenProject.class);
    mojo.project = project;
    final String expectGroupId = "com.example.artifacts";
    when(project.getGroupId()).thenReturn(expectGroupId);
    final String expectArtifactId = "sample-project";
    when(project.getArtifactId()).thenReturn(expectArtifactId);
    final String expectVersion = "0.314159";
    when(project.getVersion()).thenReturn(expectVersion);
    when(project.getPackaging()).thenReturn("jar");
    final ArtifactHandlerManager artifactHandlerManager = mock(ArtifactHandlerManager.class);
    mojo.artifactHandlerManager = artifactHandlerManager;
    final ArtifactHandler opearHandler = mock(ArtifactHandler.class);
    when(artifactHandlerManager.getArtifactHandler(OPEAR)).thenReturn(opearHandler);
    final CompletableFuture<Artifact> attachSlot = new CompletableFuture<>();
    doAnswer(call -> attachSlot.complete(call.getArgument(0, Artifact.class)))
            .when(project).addAttachedArtifact(any(Artifact.class));
    final File finalFile = new File(testOutDir, "finalFile.opear");
    final Artifact jarAttached = mojo.attachArtifact(finalFile);
    assertSame("same attachment", jarAttached, attachSlot.getNow(null));
    assertSame("same file", finalFile, jarAttached.getFile());
    assertSame("same handler", opearHandler, jarAttached.getArtifactHandler());
    assertEquals("same groupId", project.getGroupId(), jarAttached.getGroupId());
    assertEquals("same artifactId", project.getArtifactId(), jarAttached.getArtifactId());
    assertEquals("same version", project.getVersion(), jarAttached.getVersion());
    assertEquals("same type", OPEAR, jarAttached.getType());
    final Artifact projectArtifact = mock(Artifact.class);
    when(project.getArtifact()).thenReturn(projectArtifact);
    final CompletableFuture<File> fileSlot = new CompletableFuture<>();
    doAnswer(call -> fileSlot.complete(call.getArgument(0, File.class)))
            .when(projectArtifact).setFile(any(File.class));
    final CompletableFuture<ArtifactHandler> handlerSlot = new CompletableFuture<>();
    doAnswer(call -> handlerSlot.complete(call.getArgument(0, ArtifactHandler.class)))
            .when(projectArtifact).setArtifactHandler(any(ArtifactHandler.class));
    when(project.getPackaging()).thenReturn(OPEAR);
    final Artifact opearAttached = mojo.attachArtifact(finalFile);
    assertSame("same project artifact", projectArtifact, opearAttached);
    assertSame("same artifact handler", opearHandler, handlerSlot.getNow(null));
}