diff --git a/pom.xml b/pom.xml index 6f585248..0b8df866 100644 --- a/pom.xml +++ b/pom.xml @@ -156,9 +156,8 @@ - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api test @@ -180,6 +179,12 @@ 4.11.0 test + + org.mockito + mockito-junit-jupiter + 4.11.0 + test + org.slf4j slf4j-nop diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index 3ec2fe3b..826c063f 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -24,7 +24,6 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; import org.codehaus.plexus.util.FileUtils; @@ -37,13 +36,19 @@ import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; +import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +import static org.apache.maven.api.plugin.testing.MojoExtension.getVariableValueFromObject; +import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author Allan Ramirez */ -public class InstallFileMojoTest extends AbstractMojoTestCase { +public class InstallFileMojoTest { private String groupId; private String artifactId; @@ -61,19 +66,19 @@ public class InstallFileMojoTest extends AbstractMojoTestCase { private static final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/"; public void setUp() throws Exception { - super.setUp(); + // super.setUp(); FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO)); FileUtils.deleteDirectory(new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO)); } - public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() throws Exception { + public void testInstallFileFromLocalRepositoryToLocalRepositoryPath(InstallFileMojo mojo) throws Exception { File localRepository = new File(getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target"); - File testPom = new File(localRepository.getParentFile(), "plugin-config.xml"); + // File testPom = new File(localRepository.getParentFile(), "plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -95,16 +100,17 @@ public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() throws Exc assertTrue(installedArtifact.exists()); assertEquals( - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(), 5, - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size()); + FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size(), + FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString()); } - public void testInstallFileWithLocalRepositoryPath() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml"); + public void testInstallFileWithLocalRepositoryPath(InstallFileMojo mojo) throws Exception { + // File testPom = + // new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + + // "plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -126,25 +132,27 @@ public void testInstallFileWithLocalRepositoryPath() throws Exception { assertTrue(installedArtifact.exists()); assertEquals( - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(), 5, - FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size()); + FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size(), + FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString()); } - public void testInstallFileTestEnvironment() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); + public void testInstallFileTestEnvironment(InstallFileMojo mojo) throws Exception { + // File testPom = new File(getBasedir(), + // "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO)); assertNotNull(mojo); } - public void testBasicInstallFile() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); + public void testBasicInstallFile(InstallFileMojo mojo) throws Exception { + // File testPom = new File(getBasedir(), + // "target/test-classes/unit/install-file-basic-test/plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -164,11 +172,12 @@ public void testBasicInstallFile() throws Exception { assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFileWithClassifier() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml"); + public void testInstallFileWithClassifier(InstallFileMojo mojo) throws Exception { + // File testPom = + // new File(getBasedir(), + // "target/test-classes/unit/install-file-with-classifier/plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -190,11 +199,12 @@ public void testInstallFileWithClassifier() throws Exception { assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFileWithGeneratePom() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml"); + public void testInstallFileWithGeneratePom(InstallFileMojo mojo) throws Exception { + // File testPom = + // new File(getBasedir(), + // "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -233,11 +243,12 @@ public void testInstallFileWithGeneratePom() throws Exception { assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFileWithPomFile() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml"); + public void testInstallFileWithPomFile(InstallFileMojo mojo) throws Exception { + // File testPom = + // new File(getBasedir(), + // "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -268,11 +279,12 @@ public void testInstallFileWithPomFile() throws Exception { assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFileWithPomAsPackaging() throws Exception { - File testPom = new File( - getBasedir(), "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml"); + public void testInstallFileWithPomAsPackaging(InstallFileMojo mojo) throws Exception { + // File testPom = new File( + // getBasedir(), "target/test-classes/unit/install-file-with-pom-as-packaging/" + + // "plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -296,11 +308,12 @@ public void testInstallFileWithPomAsPackaging() throws Exception { assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); } - public void testInstallFile() throws Exception { - File testPom = - new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml"); + public void testInstallFile(InstallFileMojo mojo) throws Exception { + // File testPom = + // new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + + // "plugin-config.xml"); - InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); + // InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom); assertNotNull(mojo); @@ -318,9 +331,9 @@ public void testInstallFile() throws Exception { assertTrue(installedArtifact.exists()); assertEquals( - FileUtils.getFiles(new File(LOCAL_REPO), null, null).toString(), 5, - FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + FileUtils.getFiles(new File(LOCAL_REPO), null, null).size(), + FileUtils.getFiles(new File(LOCAL_REPO), null, null).toString()); } private void assignValuesForParameter(Object obj) throws Exception { diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index d5809672..557b1f36 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -18,162 +18,155 @@ */ package org.apache.maven.plugins.install; -import java.io.File; +import javax.inject.Inject; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; import java.util.Collections; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; +import java.util.HashMap; -import org.apache.maven.artifact.Artifact; +import org.apache.maven.api.di.Provides; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Build; import org.apache.maven.model.Plugin; -import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0; -import org.apache.maven.plugins.install.stubs.InstallArtifactStub; -import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; -import org.codehaus.plexus.util.FileUtils; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.internal.impl.DefaultLocalPathComposer; -import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory; -import org.eclipse.aether.internal.impl.DefaultTrackingFileManager; -import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.NoLocalRepositoryManagerException; - +import org.apache.maven.project.MavenProjectHelper; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.installation.InstallRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; /** * @author Allan Ramirez */ -public class InstallMojoTest extends AbstractMojoTestCase { +@ExtendWith(MockitoExtension.class) +@MojoTest +public class InstallMojoTest { - InstallArtifactStub artifact; + @TempDir + private Path tempDir; - private static final String LOCAL_REPO = "target/local-repo/"; + @Inject + private MavenProject mavenProject; - public void setUp() throws Exception { - super.setUp(); + @Inject + private MavenSession mavenSession; - FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO)); - } + @Inject + private MavenProjectHelper mavenProjectHelper; - public void testInstallTestEnvironment() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); + @Mock + private RepositorySystem repositorySystem; - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); + @Captor + private ArgumentCaptor installRequestCaptor; - assertNotNull(mojo); + @Provides + @SuppressWarnings("unused") + private RepositorySystem repositorySystemProvides() { + return repositorySystem; } - public void testBasicInstall() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); + @BeforeEach + public void setUp() throws Exception { + mavenProject.setGroupId("org.apache.maven.test"); + mavenProject.setArtifactId("maven-install-test"); + mavenProject.setVersion("1.0-SNAPSHOT"); + mavenProject.setPackaging("jar"); - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); + Plugin plugin = new Plugin(); + plugin.setArtifactId("maven-install-plugin"); + PluginExecution execution = new PluginExecution(); + execution.setGoals(Collections.singletonList("install")); + plugin.setExecutions(Collections.singletonList(execution)); + mavenProject.getBuild().addPlugin(plugin); - artifact = (InstallArtifactStub) project.getArtifact(); + lenient().when(mavenSession.getProjects()).thenReturn(Collections.singletonList(mavenProject)); + } - artifact.setFile(file); + @Test + @InjectMojo(goal = "install") + public void testBasicInstall(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + setProjectArtifact(mavenProject); mojo.execute(); - String groupId = dotToSlashReplacer(artifact.getGroupId()); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." - + artifact.getArtifactHandler().getExtension()); + verify(repositorySystem).install(any(), installRequestCaptor.capture()); - assertTrue(installedArtifact.exists()); + InstallRequest installRequest = installRequestCaptor.getValue(); + ArrayList artifacts = new ArrayList<>(installRequest.getArtifacts()); + assertEquals(2, artifacts.size()); - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertArtifactInstalled(mavenProject, artifacts.get(0), "pom"); + assertArtifactInstalled(mavenProject, artifacts.get(1), "jar"); } - public void testBasicInstallWithAttachedArtifacts() throws Exception { - File testPom = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - List attachedArtifacts = project.getAttachedArtifacts(); + @Test + @InjectMojo(goal = "install") + public void testBasicInstallWithAttachedArtifacts(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + setProjectArtifact(mavenProject); + + mavenProjectHelper.attachArtifact( + mavenProject, + "jar", + "next1", + Files.createTempFile(tempDir, "test-artifact1", "jar").toFile()); + mavenProjectHelper.attachArtifact( + mavenProject, + "jar", + "next2", + Files.createTempFile(tempDir, "test-artifact2", "jar").toFile()); mojo.execute(); - String packaging = project.getPackaging(); - - String groupId; - - for (Object attachedArtifact1 : attachedArtifacts) { - AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) attachedArtifact1; + verify(repositorySystem).install(any(), installRequestCaptor.capture()); - groupId = dotToSlashReplacer(attachedArtifact.getGroupId()); + InstallRequest installRequest = installRequestCaptor.getValue(); + ArrayList artifacts = new ArrayList<>(installRequest.getArtifacts()); - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + attachedArtifact.getArtifactId() - + "/" + attachedArtifact.getVersion() + "/" + attachedArtifact.getArtifactId() - + "-" + attachedArtifact.getVersion() + "." + packaging); - - assertTrue(installedArtifact.getPath() + " does not exist", installedArtifact.exists()); - } - - assertEquals(13, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertEquals(4, artifacts.size()); + assertArtifactInstalled(mavenProject, artifacts.get(0), "pom"); + assertArtifactInstalled(mavenProject, artifacts.get(1), "jar"); + assertArtifactInstalled(mavenProject, artifacts.get(2), "next1", "jar"); + assertArtifactInstalled(mavenProject, artifacts.get(3), "next2", "jar"); } - public void testNonPomInstallWithAttachedArtifactsOnly() throws Exception { - File testPom = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); + @Test + @InjectMojo(goal = "install") + public void testNonPomInstallWithAttachedArtifactsOnly(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + setProjectArtifact(mavenProject); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "session", createMavenSession()); + mavenProjectHelper.attachArtifact( + mavenProject, + "jar", + "next1", + Files.createTempFile(tempDir, "test-artifact1", "jar").toFile()); - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(null); + mavenProject.getArtifact().setFile(null); try { mojo.execute(); @@ -185,60 +178,15 @@ public void testNonPomInstallWithAttachedArtifactsOnly() throws Exception { + "but it has attachments. Change packaging to 'pom'.", e.getMessage()); } + verifyNoInteractions(repositorySystem); } - public void testUpdateReleaseParamSetToTrue() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/configured-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); - - mojo.execute(); - - // assertTrue( artifact.isRelease() ); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); - } - - public void testInstallIfArtifactFileIsNull() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(null); - - assertNull(artifact.getFile()); + @Test + @InjectMojo(goal = "install") + public void testInstallIfArtifactFileIsNull(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + setProjectArtifact(mavenProject); + mavenProject.getArtifact().setFile(null); try { mojo.execute(); @@ -249,27 +197,15 @@ public void testInstallIfArtifactFileIsNull() throws Exception { "The packaging plugin for project maven-install-test did not assign a file to the build artifact", e.getMessage()); } - - assertFalse(new File(LOCAL_REPO).exists()); + verifyNoInteractions(repositorySystem); } - public void testInstallIfProjectFileIsNull() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "session", createMavenSession()); - - project.setFile(null); - - assertNull(project.getFile()); + @Test + @InjectMojo(goal = "install") + public void testInstallIfProjectFileIsNull(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + setProjectArtifact(mavenProject); + mavenProject.setFile(null); try { mojo.execute(); @@ -278,199 +214,83 @@ public void testInstallIfProjectFileIsNull() throws Exception { // expected, message should include artifactId assertEquals("The POM for project maven-install-test could not be attached", e.getMessage()); } + verifyNoInteractions(repositorySystem); } - public void testInstallIfPackagingIsPom() throws Exception { - File testPom = new File( - getBasedir(), "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - String packaging = project.getPackaging(); - - assertEquals("pom", packaging); - - artifact = (InstallArtifactStub) project.getArtifact(); - - mojo.execute(); - - String groupId = dotToSlashReplacer(artifact.getGroupId()); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom"); - - assertTrue(installedArtifact.exists()); - - assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); - } - - public void testInstallIfPackagingIsBom() throws Exception { - File testPom = new File( - getBasedir(), "target/test-classes/unit/basic-install-test-packaging-bom/" + "plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - String packaging = project.getPackaging(); - - assertEquals("bom", packaging); - - artifact = (InstallArtifactStub) project.getArtifact(); + @Test + @InjectMojo(goal = "install") + public void testInstallIfPackagingIsPom(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + mavenProject.setPomFile( + Files.createTempFile(tempDir, "test-artifact", "pom").toFile()); + mavenProject.setPackaging("pom"); mojo.execute(); - String groupId = dotToSlashReplacer(artifact.getGroupId()); + verify(repositorySystem).install(any(), installRequestCaptor.capture()); - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom"); + InstallRequest installRequest = installRequestCaptor.getValue(); + ArrayList artifacts = new ArrayList<>(installRequest.getArtifacts()); - assertTrue(installedArtifact.exists()); - - assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertEquals(1, artifacts.size()); + assertArtifactInstalled(mavenProject, artifacts.get(0), "pom"); } - public void testBasicInstallAndCreate() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml"); - - AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - MavenSession mavenSession = createMavenSession(); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); + @Test + @InjectMojo(goal = "install") + public void testInstallIfPackagingIsBom(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); + mavenProject.setPomFile( + Files.createTempFile(tempDir, "test-artifact", "pom").toFile()); + mavenProject.setPackaging("bom"); mojo.execute(); - File pom = new File( - new File(LOCAL_REPO), - mavenSession - .getRepositorySession() - .getLocalRepositoryManager() - .getPathForLocalArtifact(new DefaultArtifact( - artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion()))); - - assertTrue(pom.exists()); - - String groupId = dotToSlashReplacer(artifact.getGroupId()); - String packaging = project.getPackaging(); - String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" - + artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion(); + verify(repositorySystem).install(any(), installRequestCaptor.capture()); - File installedArtifact = new File(localPath + "." + packaging); + InstallRequest installRequest = installRequestCaptor.getValue(); + ArrayList artifacts = new ArrayList<>(installRequest.getArtifacts()); - assertTrue(installedArtifact.exists()); - - assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size()); + assertEquals(1, artifacts.size()); + assertArtifactInstalled(mavenProject, artifacts.get(0), "pom"); } - public void testSkip() throws Exception { - File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml"); - - InstallMojo mojo = (InstallMojo) lookupMojo("install", testPom); - - assertNotNull(mojo); - - File file = new File( - getBasedir(), - "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar"); - - MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project"); - updateMavenProject(project); - - MavenSession session = createMavenSession(); - session.setProjects(Collections.singletonList(project)); - setVariableValueToObject(mojo, "session", session); - setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>()); - setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor()); - setVariableValueToObject(mojo, "skip", Boolean.TRUE); - - artifact = (InstallArtifactStub) project.getArtifact(); - - artifact.setFile(file); + @Test + @InjectMojo(goal = "install") + @MojoParameter(name = "skip", value = "true") + public void testSkip(InstallMojo mojo) throws Exception { + mojo.setPluginContext(new HashMap<>()); mojo.execute(); - String groupId = dotToSlashReplacer(artifact.getGroupId()); - - String packaging = project.getPackaging(); - - File installedArtifact = new File( - getBasedir(), - LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" - + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging); - - assertFalse(installedArtifact.exists()); - - assertFalse(new File(LOCAL_REPO).exists()); + verifyNoInteractions(repositorySystem); } - private String dotToSlashReplacer(String parameter) { - return parameter.replace('.', '/'); + private void assertArtifactInstalled( + MavenProject mavenProject, org.eclipse.aether.artifact.Artifact artifact, String type) { + assertEquals(mavenProject.getArtifactId(), artifact.getArtifactId()); + assertEquals(mavenProject.getGroupId(), artifact.getGroupId()); + assertEquals(mavenProject.getVersion(), artifact.getVersion()); + assertEquals(type, artifact.getExtension()); } - private MavenSession createMavenSession() throws NoLocalRepositoryManagerException { - MavenSession session = mock(MavenSession.class); - DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession(); - repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory( - new DefaultLocalPathComposer(), - new DefaultTrackingFileManager(), - new DefaultLocalPathPrefixComposerFactory()) - .newInstance(repositorySession, new LocalRepository(LOCAL_REPO))); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(repositorySession); - when(session.getProjectBuildingRequest()).thenReturn(buildingRequest); - when(session.getRepositorySession()).thenReturn(repositorySession); - when(session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class))) - .thenReturn(new ConcurrentHashMap()); - return session; + private void assertArtifactInstalled(MavenProject mavenProject, Artifact artifact, String classifier, String type) { + assertArtifactInstalled(mavenProject, artifact, type); + assertEquals(classifier, artifact.getClassifier()); } - private void updateMavenProject(MavenProject project) { - project.setGroupId(project.getArtifact().getGroupId()); - project.setArtifactId(project.getArtifact().getArtifactId()); - project.setVersion(project.getArtifact().getVersion()); - - Plugin plugin = new Plugin(); - plugin.setArtifactId("maven-install-plugin"); - project.setBuild(new Build()); - project.getBuild().addPlugin(plugin); + private void setProjectArtifact(MavenProject mavenProject) throws IOException { + org.apache.maven.artifact.DefaultArtifact artifact = new org.apache.maven.artifact.DefaultArtifact( + mavenProject.getGroupId(), + mavenProject.getArtifactId(), + mavenProject.getVersion(), + null, + "jar", + null, + new DefaultArtifactHandler("jar")); + artifact.setFile(Files.createTempFile(tempDir, "test-artifact", "jar").toFile()); + mavenProject.setArtifact(artifact); + mavenProject.setPomFile( + Files.createTempFile(tempDir, "test-artifact", "pom").toFile()); } } diff --git a/src/test/resources/unit/basic-install-test/plugin-config.xml b/src/test/resources/unit/basic-install-test/plugin-config.xml index c95dccc0..b72dc783 100644 --- a/src/test/resources/unit/basic-install-test/plugin-config.xml +++ b/src/test/resources/unit/basic-install-test/plugin-config.xml @@ -21,14 +21,6 @@ under the License. maven-install-plugin - - - ${basedir}/src/test/resources/unit/basic-install-test/plugin-config.xml - jar - - - -