From 7d2369e4d45676d5b131e3ad2be306dca15a91bb Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 15 Jun 2023 07:26:28 +0200 Subject: [PATCH 1/2] Switch to junit 5 --- pom.xml | 52 +++-- .../maven/plugins/clean/CleanMojoTest.java | 203 +++++++----------- 2 files changed, 115 insertions(+), 140 deletions(-) diff --git a/pom.xml b/pom.xml index 0436869..0764a49 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ under the License. - 3.2.5 + 3.5.4 2023-06-14T18:50:52Z @@ -73,15 +73,21 @@ under the License. provided - org.codehaus.plexus - plexus-utils - 4.0.0 + org.apache.maven + maven-core + ${mavenVersion} + provided + + + org.apache.maven.resolver + maven-resolver-api + 1.1.1 + provided org.codehaus.plexus - plexus-xml + plexus-utils 4.0.0 - provided @@ -95,25 +101,37 @@ under the License. org.apache.maven.plugin-testing maven-plugin-testing-harness - 3.3.0 + 4.0.0-alpha-2-SNAPSHOT test - org.apache.maven - maven-compat - ${mavenVersion} + org.junit.jupiter + junit-jupiter-api + 5.9.3 test - org.apache.maven - maven-core - ${mavenVersion} - provided + org.codehaus.plexus + plexus-testing + 1.1.0 + test + + + org.eclipse.sisu + org.eclipse.sisu.plexus + test - junit - junit - 4.13.2 + com.google.inject + guice + 4.2.0 + no_aop + test + + + org.apache.maven + maven-resolver-provider + ${mavenVersion} test diff --git a/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java b/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java index f82e615..8525d42 100644 --- a/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java @@ -29,45 +29,59 @@ import java.nio.file.Paths; import java.util.Collections; +import com.google.inject.Provides; +import com.google.inject.Singleton; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; - -import static org.apache.commons.io.FileUtils.copyDirectory; +import org.apache.maven.plugin.testing.junit5.InjectMojo; +import org.apache.maven.plugin.testing.junit5.MojoTest; +import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.codehaus.plexus.PlexusContainer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.apache.maven.plugin.testing.junit5.MojoExtension.setVariableValueToObject; +import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; import static org.codehaus.plexus.util.IOUtil.copy; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Test the clean mojo. * * @author Vincent Siveton */ -public class CleanMojoTest extends AbstractMojoTestCase { +@MojoTest +public class CleanMojoTest { /** * Tests the simple removal of directories * * @throws Exception in case of an error. */ - public void testBasicClean() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/basic-clean-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/basic-clean-test"), - new File(getBasedir(), "target/test-classes/unit/basic-clean-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/basic-clean-test/plugin-pom.xml") + public void testBasicClean(CleanMojo mojo) throws Exception { mojo.execute(); assertFalse( - "Directory exists", - checkExists(getBasedir() + "/target/test-classes/unit/" + "basic-clean-test/buildDirectory")); + checkExists(getBasedir() + "/target/test-classes/unit/" + "basic-clean-test/buildDirectory"), + "Directory exists"); assertFalse( - "Directory exists", - checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildOutputDirectory")); + checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildOutputDirectory"), + "Directory exists"); assertFalse( - "Directory exists", - checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildTestDirectory")); + checkExists(getBasedir() + "/target/test-classes/unit/basic-clean-test/" + "buildTestDirectory"), + "Directory exists"); } /** @@ -75,17 +89,9 @@ public void testBasicClean() throws Exception { * * @throws Exception in case of an error. */ - public void testCleanNestedStructure() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/nested-clean-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/nested-clean-test"), - new File(getBasedir(), "target/test-classes/unit/nested-clean-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/nested-clean-test/plugin-pom.xml") + public void testCleanNestedStructure(CleanMojo mojo) throws Exception { mojo.execute(); assertFalse(checkExists(getBasedir() + "/target/test-classes/unit/nested-clean-test/target")); @@ -99,17 +105,9 @@ public void testCleanNestedStructure() throws Exception { * * @throws Exception in case of an error. */ - public void testCleanEmptyDirectories() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/empty-clean-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/empty-clean-test"), - new File(getBasedir(), "target/test-classes/unit/empty-clean-test")); - - CleanMojo mojo = (CleanMojo) lookupEmptyMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/empty-clean-test/plugin-pom.xml") + public void testCleanEmptyDirectories(CleanMojo mojo) throws Exception { mojo.execute(); assertTrue(checkExists(getBasedir() + "/target/test-classes/unit/empty-clean-test/testDirectoryStructure")); @@ -126,17 +124,9 @@ public void testCleanEmptyDirectories() throws Exception { * * @throws Exception in case of an error. */ - public void testFilesetsClean() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/fileset-clean-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/fileset-clean-test"), - new File(getBasedir(), "target/test-classes/unit/fileset-clean-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/fileset-clean-test/plugin-pom.xml") + public void testFilesetsClean(CleanMojo mojo) throws Exception { mojo.execute(); // fileset 1 @@ -161,24 +151,10 @@ public void testFilesetsClean() throws Exception { * * @throws Exception in case of an error. */ - public void testCleanInvalidDirectory() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/invalid-directory-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/invalid-directory-test"), - new File(getBasedir(), "target/test-classes/unit/invalid-directory-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - - try { - mojo.execute(); - - fail("Should fail to delete a file treated as a directory"); - } catch (MojoExecutionException expected) { - assertTrue(true); - } + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/invalid-directory-test/plugin-pom.xml") + public void testCleanInvalidDirectory(CleanMojo mojo) throws Exception { + assertThrows(MojoExecutionException.class, mojo::execute); } /** @@ -186,17 +162,9 @@ public void testCleanInvalidDirectory() throws Exception { * * @throws Exception in case of an error. */ - public void testMissingDirectory() throws Exception { - String pluginPom = getBasedir() + "/src/test/resources/unit/missing-directory-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/missing-directory-test"), - new File(getBasedir(), "target/test-classes/unit/missing-directory-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @InjectMojo(goal = "clean", pom = "classpath:/unit/missing-directory-test/plugin-pom.xml") + public void testMissingDirectory(CleanMojo mojo) throws Exception { mojo.execute(); assertFalse(checkExists(getBasedir() + "/target/test-classes/unit/missing-directory-test/does-not-exist")); @@ -210,22 +178,10 @@ public void testMissingDirectory() throws Exception { * * @throws Exception in case of an error. */ - public void testCleanLockedFile() throws Exception { - if (!System.getProperty("os.name").toLowerCase().contains("windows")) { - assertTrue("Ignored this test on non Windows based systems", true); - return; - } - - String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/locked-file-test"), - new File(getBasedir(), "target/test-classes/unit/locked-file-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); - assertNotNull(mojo); - + @Test + @EnabledOnOs(OS.WINDOWS) + @InjectMojo(goal = "clean", pom = "classpath:/unit/locked-file-test/plugin-pom.xml") + public void testCleanLockedFile(CleanMojo mojo) throws Exception { File f = new File(getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt"); try (FileChannel channel = new RandomAccessFile(f, "rw").getChannel(); FileLock ignored = channel.lock()) { @@ -244,20 +200,10 @@ public void testCleanLockedFile() throws Exception { * * @throws Exception in case of an error. */ - public void testCleanLockedFileWithNoError() throws Exception { - if (!System.getProperty("os.name").toLowerCase().contains("windows")) { - assertTrue("Ignore this test on non Windows based systems", true); - return; - } - - String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml"; - - // safety - copyDirectory( - new File(getBasedir(), "src/test/resources/unit/locked-file-test"), - new File(getBasedir(), "target/test-classes/unit/locked-file-test")); - - CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom); + @Test + @EnabledOnOs(OS.WINDOWS) + @InjectMojo(goal = "clean", pom = "classpath:/unit/locked-file-test/plugin-pom.xml") + public void testCleanLockedFileWithNoError(CleanMojo mojo) throws Exception { setVariableValueToObject(mojo, "failOnError", Boolean.FALSE); assertNotNull(mojo); @@ -275,12 +221,9 @@ public void testCleanLockedFileWithNoError() throws Exception { * Test the followLink option with windows junctions * @throws Exception */ + @Test + @EnabledOnOs(OS.WINDOWS) public void testFollowLinksWithWindowsJunction() throws Exception { - if (!System.getProperty("os.name").toLowerCase().contains("windows")) { - assertTrue("Ignore this test on non Windows based systems", true); - return; - } - testSymlink((link, target) -> { Process process = new ProcessBuilder() .directory(link.getParent().toFile()) @@ -300,12 +243,9 @@ public void testFollowLinksWithWindowsJunction() throws Exception { * Test the followLink option with sym link * @throws Exception */ + @Test + @DisabledOnOs(OS.WINDOWS) public void testFollowLinksWithSymLinkOnPosix() throws Exception { - if (System.getProperty("os.name").toLowerCase().contains("windows")) { - assertTrue("Ignore this test on Windows based systems", true); - return; - } - testSymlink((link, target) -> { try { Files.createSymbolicLink(link, target); @@ -355,6 +295,23 @@ private void testSymlink(LinkCreator linkCreator) throws Exception { assertFalse(Files.exists(dirWithLnk)); } + @Provides + @Singleton + @SuppressWarnings("unused") + private MavenSession createSession(PlexusContainer container) { + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + MavenExecutionResult result = new DefaultMavenExecutionResult(); + MavenSession session = new MavenSession(container, MavenRepositorySystemUtils.newSession(), request, result); + return session; + } + + @Provides + @Singleton + @SuppressWarnings("unused") + private MojoExecution createMojoExecution() { + return new MojoExecution(null); + } + /** * @param dir a dir or a file * @return true if a file/dir exists, false otherwise From 9f2dd25a1bdb308a8175bdaadfc289db8f78c36f Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 28 Dec 2023 14:00:14 +0100 Subject: [PATCH 2/2] Require maven 3.6.3 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 53e29db..2cae719 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,7 @@ under the License. - 3.5.4 + 3.6.3 2023-06-14T18:50:52Z