From c706f584e4d8417ec7474c5c95fd62cdd37f27b1 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 29 Apr 2024 17:24:58 +0200 Subject: [PATCH] [MINVOKER-284] Switch to JUnit5, drop maven-compat --- pom.xml | 25 +--- src/it/exec-timeout-invoker-level/pom.xml | 11 ++ src/it/exec-timeout-mojo-level/pom.xml | 11 ++ .../plugins/invoker/AbstractInvokerMojo.java | 17 +-- .../maven/plugins/invoker/InvokerReport.java | 4 +- .../maven/plugins/invoker/InvokerSession.java | 4 +- .../maven/plugins/invoker/PomUtils.java | 4 +- .../maven/plugins/invoker/VerifyMojo.java | 4 +- ...ProjectStub.java => AbstractTestUtil.java} | 31 ++--- .../plugins/invoker/InterpolationTest.java | 86 +++++++------- .../plugins/invoker/InvokerMojoTest.java | 39 ++++--- .../invoker/InvokerPropertiesTest.java | 62 +++++----- .../plugins/invoker/InvokerSessionTest.java | 6 +- .../plugins/invoker/JobExecutorTest.java | 10 +- .../maven/plugins/invoker/SelectorTest.java | 26 ++--- .../plugins/invoker/SelectorUtilsTest.java | 109 ++++++++++-------- 16 files changed, 231 insertions(+), 218 deletions(-) rename src/test/java/org/apache/maven/plugins/invoker/{ExtendedMavenProjectStub.java => AbstractTestUtil.java} (59%) diff --git a/pom.xml b/pom.xml index 234ad90c..6b6ab537 100644 --- a/pom.xml +++ b/pom.xml @@ -252,33 +252,20 @@ under the License. - junit - junit - 4.13.2 + org.junit.jupiter + junit-jupiter-api test org.mockito mockito-core - 4.10.0 + 4.11.0 test - org.apache.maven.plugin-testing - maven-plugin-testing-harness - 3.3.0 - test - - - org.codehaus.plexus - plexus-container-default - - - - - org.apache.maven - maven-compat - ${mavenVersion} + org.mockito + mockito-junit-jupiter + 4.11.0 test diff --git a/src/it/exec-timeout-invoker-level/pom.xml b/src/it/exec-timeout-invoker-level/pom.xml index 441d9aac..352ba165 100644 --- a/src/it/exec-timeout-invoker-level/pom.xml +++ b/src/it/exec-timeout-invoker-level/pom.xml @@ -36,6 +36,17 @@ under the License. + + + + org.apache.maven.plugins + maven-plugin-plugin + + test + + + + org.apache.maven.plugins diff --git a/src/it/exec-timeout-mojo-level/pom.xml b/src/it/exec-timeout-mojo-level/pom.xml index e9a1f5e5..1ff964ec 100644 --- a/src/it/exec-timeout-mojo-level/pom.xml +++ b/src/it/exec-timeout-mojo-level/pom.xml @@ -36,6 +36,17 @@ under the License. + + + + org.apache.maven.plugins + maven-plugin-plugin + + test + + + + org.apache.maven.plugins diff --git a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java index 14ddb735..580b2a3f 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java @@ -28,6 +28,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -91,13 +92,13 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.InterpolationFilterReader; -import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.ReflectionUtils; -import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; import org.codehaus.plexus.util.cli.StreamConsumer; +import org.codehaus.plexus.util.xml.XmlStreamReader; +import org.codehaus.plexus.util.xml.XmlStreamWriter; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomWriter; @@ -750,9 +751,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } + if (encoding == null || encoding.isEmpty()) { - getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING - + ", i.e. build is platform dependent!"); + getLog().warn("File encoding has not been set, using platform encoding " + + Charset.defaultCharset().displayName() + ", i.e. build is platform dependent!"); } // done it here to prevent issues with concurrent access in case of parallel run @@ -1522,7 +1524,7 @@ private void runBuild( File interpolatedPomFile = interpolatePomFile(pomFile, basedir); // FIXME: Think about the following code part -- ^^^^^^^ END - getLog().info(buffer().a("Building: ").strong(buildJob.getProject()).toString()); + getLog().info(buffer().a("Building: ").strong(buildJob.getProject()).build()); InvokerProperties invokerProperties = getInvokerProperties(basedir, globalInvokerProperties); @@ -2274,13 +2276,12 @@ void buildInterpolatedFile(File originalFile, File interpolatedFile) throws Mojo // interpolation with token @...@ try (Reader reader = - new InterpolationFilterReader(ReaderFactory.newXmlReader(originalFile), composite, "@", "@")) { + new InterpolationFilterReader(new XmlStreamReader(originalFile), composite, "@", "@")) { xml = IOUtil.toString(reader); } - try (Writer writer = WriterFactory.newXmlWriter(interpolatedFile)) { + try (Writer writer = new XmlStreamWriter(interpolatedFile)) { interpolatedFile.getParentFile().mkdirs(); - writer.write(xml); } } catch (IOException e) { diff --git a/src/main/java/org/apache/maven/plugins/invoker/InvokerReport.java b/src/main/java/org/apache/maven/plugins/invoker/InvokerReport.java index 339705a4..b3adabfb 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InvokerReport.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InvokerReport.java @@ -32,7 +32,6 @@ import org.apache.maven.reporting.AbstractMavenReport; import org.apache.maven.reporting.MavenReportException; import org.codehaus.plexus.i18n.I18N; -import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -64,7 +63,7 @@ protected void executeReport(Locale locale) throws MavenReportException { BuildJobXpp3Reader buildJobReader = new BuildJobXpp3Reader(); List buildJobs = new ArrayList<>(reportFiles.length); for (File reportFile : reportFiles) { - try (XmlStreamReader xmlReader = ReaderFactory.newXmlReader(reportFile)) { + try (XmlStreamReader xmlReader = new XmlStreamReader(reportFile)) { buildJobs.add(buildJobReader.read(xmlReader)); } catch (XmlPullParserException e) { throw new MavenReportException("Failed to parse report file: " + reportFile, e); @@ -103,6 +102,7 @@ private File[] getReportFiles() { return ReportUtils.getReportFiles(reportsDirectory); } + @Override public boolean canGenerateReport() { return getReportFiles().length > 0; } diff --git a/src/main/java/org/apache/maven/plugins/invoker/InvokerSession.java b/src/main/java/org/apache/maven/plugins/invoker/InvokerSession.java index 9f47c1eb..a6667449 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/InvokerSession.java +++ b/src/main/java/org/apache/maven/plugins/invoker/InvokerSession.java @@ -27,7 +27,7 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.invoker.model.BuildJob; -import org.apache.maven.shared.utils.io.IOUtil; +import org.codehaus.plexus.util.IOUtil; import static org.apache.maven.shared.utils.logging.MessageUtils.buffer; @@ -38,7 +38,7 @@ */ class InvokerSession { private static final String SEPARATOR = - buffer().strong("-------------------------------------------------").toString(); + buffer().strong("-------------------------------------------------").build(); private List buildJobs; diff --git a/src/main/java/org/apache/maven/plugins/invoker/PomUtils.java b/src/main/java/org/apache/maven/plugins/invoker/PomUtils.java index 17965c96..526ed7dd 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/PomUtils.java +++ b/src/main/java/org/apache/maven/plugins/invoker/PomUtils.java @@ -25,7 +25,7 @@ import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.MojoExecutionException; -import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.xml.XmlStreamReader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** @@ -43,7 +43,7 @@ class PomUtils { * @throws MojoExecutionException If the POM file could not be loaded. */ public static Model loadPom(File pomFile) throws MojoExecutionException { - try (Reader reader = ReaderFactory.newXmlReader(pomFile)) { + try (Reader reader = new XmlStreamReader(pomFile)) { return new MavenXpp3Reader().read(reader, false); } catch (XmlPullParserException e) { throw new MojoExecutionException("Failed to parse POM: " + pomFile, e); diff --git a/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java b/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java index 9bd6d823..b3a21cb0 100644 --- a/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java +++ b/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java @@ -29,7 +29,7 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader; -import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.xml.XmlStreamReader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** @@ -117,7 +117,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { InvokerSession invokerSession = new InvokerSession(); for (File reportFile : reportFiles) { - try (Reader xmlReader = ReaderFactory.newXmlReader(reportFile)) { + try (Reader xmlReader = new XmlStreamReader(reportFile)) { invokerSession.addJob(reader.read(xmlReader)); } catch (XmlPullParserException e) { throw new MojoExecutionException("Failed to parse report file: " + reportFile, e); diff --git a/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/invoker/AbstractTestUtil.java similarity index 59% rename from src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java rename to src/test/java/org/apache/maven/plugins/invoker/AbstractTestUtil.java index b8e5f9d7..2a2f791e 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/invoker/AbstractTestUtil.java @@ -18,33 +18,18 @@ */ package org.apache.maven.plugins.invoker; -import java.util.Properties; +import java.io.File; -import org.apache.maven.model.Scm; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.codehaus.plexus.util.ReflectionUtils; -/** - * @author Olivier Lamy - * @since 22 nov. 07 - */ -public class ExtendedMavenProjectStub extends MavenProjectStub { - private Scm scm; - - private Properties properties; - - public Scm getScm() { - return scm; - } - - public void setScm(Scm scm) { - this.scm = scm; - } +abstract class AbstractTestUtil { - public Properties getProperties() { - return properties; + protected String getBasedir() { + String path = System.getProperty("basedir"); + return path != null ? path : new File("").getAbsolutePath(); } - public void setProperties(Properties properties) { - this.properties = properties; + protected void setVariableValueToObject(Object object, String filed, Object value) throws IllegalAccessException { + ReflectionUtils.setVariableValueInObject(object, filed, value); } } diff --git a/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java b/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java index 637845d2..83850119 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java @@ -20,80 +20,80 @@ import java.io.File; import java.io.Reader; +import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.maven.model.Scm; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.xml.XmlStreamReader; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author Olivier Lamy * @since 22 nov. 07 */ -public class InterpolationTest extends AbstractMojoTestCase { +class InterpolationTest extends AbstractTestUtil { - protected MavenProjectStub buildMavenProjectStub() { - ExtendedMavenProjectStub project = new ExtendedMavenProjectStub(); + private MavenProject buildMavenProjectStub() { + MavenProject project = new MavenProject(); project.setVersion("1.0-SNAPSHOT"); project.setArtifactId("foo"); project.setGroupId("bar"); + project.setFile(new File(getBasedir(), "pom.xml")); Properties properties = new Properties(); properties.put("fooOnProject", "barOnProject"); - project.setProperties(properties); + project.getModel().setProperties(properties); Scm scm = new Scm(); scm.setConnection("http://blabla"); project.setScm(scm); return project; } - public void testCompositeMap() { + @Test + void testCompositeMap() { - Properties properties = new Properties(); + Map properties = new HashMap<>(); properties.put("foo", "bar"); properties.put("version", "2.0-SNAPSHOT"); - CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), (Map) properties, false); - assertEquals("1.0-SNAPSHOT", compositeMap.get("pom.version")); - assertEquals("bar", compositeMap.get("foo")); - assertEquals("bar", compositeMap.get("pom.groupId")); - assertEquals("http://blabla", compositeMap.get("pom.scm.connection")); - assertEquals("barOnProject", compositeMap.get("fooOnProject")); + CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), properties, false); + assertThat(compositeMap).containsEntry("pom.version", "1.0-SNAPSHOT"); + assertThat(compositeMap).containsEntry("foo", "bar"); + assertThat(compositeMap).containsEntry("pom.groupId", "bar"); + assertThat(compositeMap).containsEntry("pom.scm.connection", "http://blabla"); + assertThat(compositeMap).containsEntry("fooOnProject", "barOnProject"); } - public void testPomInterpolation() throws Exception { - Reader reader = null; + @Test + void testPomInterpolation() throws Exception { File interpolatedPomFile; - try { - InvokerMojo invokerMojo = new InvokerMojo(); - setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub()); - setVariableValueToObject(invokerMojo, "settings", new Settings()); - Properties properties = new Properties(); - properties.put("foo", "bar"); - properties.put("version", "2.0-SNAPSHOT"); - setVariableValueToObject(invokerMojo, "filterProperties", properties); - String dirPath = - getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar - + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation"; + InvokerMojo invokerMojo = new InvokerMojo(); + setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub()); + setVariableValueToObject(invokerMojo, "settings", new Settings()); + Properties properties = new Properties(); + properties.put("foo", "bar"); + properties.put("version", "2.0-SNAPSHOT"); + setVariableValueToObject(invokerMojo, "filterProperties", properties); + String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar + + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation"; - interpolatedPomFile = new File(getBasedir(), "target/interpolated-pom.xml"); - invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile); - reader = ReaderFactory.newXmlReader(interpolatedPomFile); + interpolatedPomFile = new File(getBasedir(), "target/interpolated-pom.xml"); + invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile); + try (Reader reader = new XmlStreamReader(interpolatedPomFile)) { + String content = IOUtil.toString(reader); + assertThat(content.indexOf("bar")) + .isPositive(); + } + // recreate it to test delete if exists before creation + invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile); + try (Reader reader = new XmlStreamReader(interpolatedPomFile)) { String content = IOUtil.toString(reader); - assertTrue(content.indexOf("bar") > 0); - reader.close(); - reader = null; - // recreate it to test delete if exists before creation - invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile); - reader = ReaderFactory.newXmlReader(interpolatedPomFile); - content = IOUtil.toString(reader); - assertTrue(content.indexOf("bar") > 0); - reader.close(); - reader = null; - } finally { - IOUtil.close(reader); + assertThat(content.indexOf("bar")) + .isPositive(); } } } diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java index 49abbeba..d57a5876 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java @@ -22,10 +22,10 @@ import java.util.Collections; import java.util.List; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugins.invoker.model.BuildJob; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -33,7 +33,7 @@ * @author Olivier Lamy * @since 18 nov. 07 */ -public class InvokerMojoTest extends AbstractMojoTestCase { +class InvokerMojoTest extends AbstractTestUtil { private static final String DUMMY_PROJECT = "dummy" + File.separator + "pom.xml"; private static final String WITH_POM_DIR_PROJECT = "with-pom-project-dir" + File.separator + "pom.xml"; @@ -46,7 +46,8 @@ private MavenProject getMavenProject() { return mavenProject; } - public void testSingleInvokerTest() throws Exception { + @Test + void testSingleInvokerTest() throws Exception { // given InvokerMojo invokerMojo = new InvokerMojo(); String dirPath = getBasedir() + "/src/test/resources/unit"; @@ -63,7 +64,8 @@ public void testSingleInvokerTest() throws Exception { assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT); } - public void testMultiInvokerTest() throws Exception { + @Test + void testMultiInvokerTest() throws Exception { // given InvokerMojo invokerMojo = new InvokerMojo(); String dirPath = getBasedir() + "/src/test/resources/unit"; @@ -80,7 +82,8 @@ public void testMultiInvokerTest() throws Exception { assertThat(jobs).map(BuildJob::getProject).containsExactlyInAnyOrder(DUMMY_PROJECT, INTERPOLATION_PROJECT); } - public void testFullPatternInvokerTest() throws Exception { + @Test + void testFullPatternInvokerTest() throws Exception { // given InvokerMojo invokerMojo = new InvokerMojo(); String dirPath = getBasedir() + "/src/test/resources/unit"; @@ -100,7 +103,8 @@ public void testFullPatternInvokerTest() throws Exception { DUMMY_PROJECT, WITH_POM_DIR_PROJECT, WITHOUT_POM_PROJECT, INTERPOLATION_PROJECT); } - public void testSetupInProjectList() throws Exception { + @Test + void testSetupInProjectList() throws Exception { // given InvokerMojo invokerMojo = new InvokerMojo(); String dirPath = getBasedir() + "/src/test/resources/unit"; @@ -127,7 +131,8 @@ public void testSetupInProjectList() throws Exception { .containsExactlyInAnyOrder(DUMMY_PROJECT); } - public void testSetupProjectIsFiltered() throws Exception { + @Test + void testSetupProjectIsFiltered() throws Exception { // given InvokerMojo invokerMojo = new InvokerMojo(); String dirPath = getBasedir() + "/src/test/resources/unit"; @@ -152,14 +157,20 @@ public void testSetupProjectIsFiltered() throws Exception { .isEmpty(); } - public void testAlreadyCloned() { - assertFalse(AbstractInvokerMojo.alreadyCloned("dir", Collections.emptyList())); - assertTrue(AbstractInvokerMojo.alreadyCloned("dir", Collections.singletonList("dir"))); - assertTrue(AbstractInvokerMojo.alreadyCloned("dir" + File.separator + "sub", Collections.singletonList("dir"))); - assertFalse(AbstractInvokerMojo.alreadyCloned("dirs", Collections.singletonList("dir"))); + @Test + void testAlreadyCloned() { + assertThat(AbstractInvokerMojo.alreadyCloned("dir", Collections.emptyList())) + .isFalse(); + assertThat(AbstractInvokerMojo.alreadyCloned("dir", Collections.singletonList("dir"))) + .isTrue(); + assertThat(AbstractInvokerMojo.alreadyCloned("dir" + File.separator + "sub", Collections.singletonList("dir"))) + .isTrue(); + assertThat(AbstractInvokerMojo.alreadyCloned("dirs", Collections.singletonList("dir"))) + .isFalse(); } - public void testParallelThreadsSettings() throws IllegalAccessException { + @Test + void testParallelThreadsSettings() throws Exception { Object[][] testValues = { {"4", 4}, {"1C", Runtime.getRuntime().availableProcessors()}, @@ -174,7 +185,7 @@ public void testParallelThreadsSettings() throws IllegalAccessException { setVariableValueToObject(invokerMojo, "parallelThreads", parallelThreads); - assertEquals(expectedParallelThreads, invokerMojo.getParallelThreadsCount()); + assertThat(expectedParallelThreads).isEqualTo(invokerMojo.getParallelThreadsCount()); } } } diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java index 8db6c396..26a706ec 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java @@ -27,10 +27,10 @@ import org.apache.maven.shared.invoker.InvocationRequest; import org.apache.maven.shared.invoker.InvocationRequest.ReactorFailureBehavior; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; @@ -48,20 +48,20 @@ * * @author Benjamin Bentmann */ -@RunWith(MockitoJUnitRunner.class) -public class InvokerPropertiesTest { +@ExtendWith(MockitoExtension.class) +class InvokerPropertiesTest { @Mock private InvocationRequest request; @Test - public void testConstructorNullSafe() { + void testConstructorNullSafe() { InvokerProperties facade = new InvokerProperties(null); assertThat(facade.getProperties()).isNotNull(); } @Test - public void testGetInvokerProperty() { + void testGetInvokerProperty() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -76,7 +76,7 @@ public void testGetInvokerProperty() { } @Test - public void testGetJobName() { + void testGetJobName() { Properties props = new Properties(); final String jobName = "Build Job name"; props.put("invoker.name", jobName); @@ -86,7 +86,7 @@ public void testGetJobName() { } @Test - public void testIsExpectedResult() { + void testIsExpectedResult() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -103,7 +103,7 @@ public void testIsExpectedResult() { } @Test - public void testConfigureRequestEmptyProperties() { + void testConfigureRequestEmptyProperties() { InvokerProperties facade = new InvokerProperties(null); @@ -112,7 +112,7 @@ public void testConfigureRequestEmptyProperties() { } @Test - public void testConfigureRequestGoals() { + void testConfigureRequestGoals() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -149,7 +149,7 @@ public void testConfigureRequestGoals() { } @Test - public void testConfigureRequestProfiles() { + void testConfigureRequestProfiles() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -185,7 +185,7 @@ public void testConfigureRequestProfiles() { } @Test - public void testConfigureRequestProject() throws Exception { + void testConfigureRequestProject() throws Exception { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -211,7 +211,7 @@ public void testConfigureRequestProject() throws Exception { } @Test - public void testConfigureRequestMavenExecutable() { + void testConfigureRequestMavenExecutable() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -232,7 +232,7 @@ public void testConfigureRequestMavenExecutable() { } @Test - public void testConfigureRequestMavenOpts() { + void testConfigureRequestMavenOpts() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -252,7 +252,7 @@ public void testConfigureRequestMavenOpts() { } @Test - public void testConfigureRequestFailureBehavior() { + void testConfigureRequestFailureBehavior() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -264,7 +264,7 @@ public void testConfigureRequestFailureBehavior() { } @Test - public void testConfigureRequestFailureBehaviorUnKnownName() { + void testConfigureRequestFailureBehaviorUnKnownName() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -278,7 +278,7 @@ public void testConfigureRequestFailureBehaviorUnKnownName() { } @Test - public void testConfigureRequestRecursion() { + void testConfigureRequestRecursion() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -295,7 +295,7 @@ public void testConfigureRequestRecursion() { } @Test - public void testConfigureRequestOffline() { + void testConfigureRequestOffline() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -312,7 +312,7 @@ public void testConfigureRequestOffline() { } @Test - public void testConfigureRequestDebug() { + void testConfigureRequestDebug() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -343,7 +343,7 @@ public void testConfigureRequestDebug() { } @Test - public void testConfigureRequestQuiet() { + void testConfigureRequestQuiet() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -374,7 +374,7 @@ public void testConfigureRequestQuiet() { } @Test - public void testConfigureRequestTimeoutInSeconds() { + void testConfigureRequestTimeoutInSeconds() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -393,7 +393,7 @@ public void testConfigureRequestTimeoutInSeconds() { } @Test - public void testConfigureEnvironmentVariables() { + void testConfigureEnvironmentVariables() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -411,7 +411,7 @@ public void testConfigureEnvironmentVariables() { } @Test - public void testConfigureEnvironmentVariablesWithIndex() { + void testConfigureEnvironmentVariablesWithIndex() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -429,7 +429,7 @@ public void testConfigureEnvironmentVariablesWithIndex() { } @Test - public void testConfigureUpdateSnapshots() { + void testConfigureUpdateSnapshots() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -447,7 +447,7 @@ public void testConfigureUpdateSnapshots() { } @Test - public void testConfigureUpdateSnapshotsDefault() { + void testConfigureUpdateSnapshotsDefault() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -464,7 +464,7 @@ public void testConfigureUpdateSnapshotsDefault() { } @Test - public void testIsInvocationDefined() { + void testIsInvocationDefined() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -486,7 +486,7 @@ public void testIsInvocationDefined() { } @Test - public void testIsSelectedDefined() { + void testIsSelectedDefined() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -508,7 +508,7 @@ public void testIsSelectedDefined() { } @Test - public void testGetToolchainsForEmptyProperties() { + void testGetToolchainsForEmptyProperties() { Properties props = new Properties(); InvokerProperties facade = new InvokerProperties(props); @@ -521,7 +521,7 @@ public void testGetToolchainsForEmptyProperties() { } @Test - public void testGetToolchains() { + void testGetToolchains() { Properties props = new Properties(); props.put("invoker.toolchain.jdk.version", "11"); InvokerProperties facade = new InvokerProperties(props); @@ -534,7 +534,7 @@ public void testGetToolchains() { } @Test - public void testGetToolchainsWithIndex() { + void testGetToolchainsWithIndex() { Properties props = new Properties(); props.put("selector.1.invoker.toolchain.jdk.version", "11"); InvokerProperties facade = new InvokerProperties(props); diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerSessionTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerSessionTest.java index 6226e394..9220735b 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/InvokerSessionTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerSessionTest.java @@ -22,7 +22,7 @@ import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.invoker.model.BuildJob; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.mock; @@ -32,10 +32,10 @@ /** * Unittest for {@link InvokerSession} */ -public class InvokerSessionTest { +class InvokerSessionTest { @Test - public void skipSummary() { + void skipSummary() { Log logger = mock(Log.class); BuildJob skippedBuildJob = new BuildJob("minvoker-279"); skippedBuildJob.setResult(BuildJob.Result.SKIPPED); diff --git a/src/test/java/org/apache/maven/plugins/invoker/JobExecutorTest.java b/src/test/java/org/apache/maven/plugins/invoker/JobExecutorTest.java index de374329..9f564086 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/JobExecutorTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/JobExecutorTest.java @@ -26,7 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.apache.maven.plugins.invoker.model.BuildJob; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -36,17 +36,17 @@ * * @author Slawomir Jaranowski */ -public class JobExecutorTest { +class JobExecutorTest { @Test - public void emptyJobList() { + void emptyJobList() { JobExecutor jobExecutor = new JobExecutor(Collections.emptyList(), 1); jobExecutor.forEach(job -> fail("fail")); } @Test - public void failedJob() { + void failedJob() { BuildJob job = aJob("job1", 100); JobExecutor jobExecutor = new JobExecutor(Collections.singletonList(job), 1); @@ -58,7 +58,7 @@ public void failedJob() { } @Test - public void jobsShouldBeGroupedAndExecutedInProperOrder() { + void jobsShouldBeGroupedAndExecutedInProperOrder() { Map jobsCounter = new HashMap<>(); jobsCounter.put(100, new AtomicInteger(3)); jobsCounter.put(10, new AtomicInteger(2)); diff --git a/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java b/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java index f4a8f974..d55875c9 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java @@ -20,39 +20,39 @@ import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * Test for {@link Selector}. */ -public class SelectorTest { +class SelectorTest { @Test - public void testGlobalMatch() { + void testGlobalMatch() { Selector selector = new Selector("3.2.5", "1.7", null); Properties props = new Properties(); props.setProperty("invoker.maven.version", "3.0+"); InvokerProperties invokerProperties = new InvokerProperties(props); - assertEquals(0, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isZero(); } @Test - public void testSelectorMatch() { + void testSelectorMatch() { Selector selector = new Selector("3.2.5", "1.7", null); Properties props = new Properties(); props.setProperty("selector.1.maven.version", "3.0+"); InvokerProperties invokerProperties = new InvokerProperties(props); - assertEquals(0, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isZero(); props.setProperty("selector.1.maven.version", "3.3.1+"); - assertEquals(Selector.SELECTOR_MULTI, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isEqualTo(Selector.SELECTOR_MULTI); } @Test - public void testSelectorWithGlobalMatch() { + void testSelectorWithGlobalMatch() { Selector selector = new Selector("3.2.5", "1.7", null); Properties props = new Properties(); @@ -61,16 +61,16 @@ public void testSelectorWithGlobalMatch() { props.setProperty("selector.1.java.version", "1.4+"); props.setProperty("selector.2.os.family", "myos"); InvokerProperties invokerProperties = new InvokerProperties(props); - assertEquals(0, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isZero(); props.setProperty("invoker.maven.version", "3.3.1+"); - assertEquals(Selector.SELECTOR_MULTI, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isEqualTo(Selector.SELECTOR_MULTI); props.setProperty("invoker.maven.version", "3.0+"); props.setProperty("selector.1.maven.version", "3.3.1+"); - assertEquals(Selector.SELECTOR_MULTI, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isEqualTo(Selector.SELECTOR_MULTI); props.setProperty("selector.2.os.family", "!myos"); - assertEquals(0, selector.getSelection(invokerProperties)); + assertThat(selector.getSelection(invokerProperties)).isZero(); } } diff --git a/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java b/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java index 77f26bee..8f72bfac 100644 --- a/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java +++ b/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java @@ -24,18 +24,14 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; import org.apache.maven.plugins.invoker.AbstractInvokerMojo.ToolchainPrivateManager; import org.apache.maven.toolchain.ToolchainPrivate; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.isA; +import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -44,63 +40,69 @@ * * @author Benjamin Bentmann */ -public class SelectorUtilsTest { +class SelectorUtilsTest { @Test - public void testParseList() { + void testParseList() { List includes = new ArrayList<>(); List excludes = new ArrayList<>(); SelectorUtils.parseList(null, includes, excludes); SelectorUtils.parseList(" 1.5, !1.4, 1.6+ ", includes, excludes); - assertEquals(Arrays.asList("1.5", "1.6+"), includes); - assertEquals(Collections.singletonList("1.4"), excludes); + assertThat(includes).containsExactly("1.5", "1.6+"); + assertThat(excludes).containsExactly("1.4"); } @Test - public void testParseVersion() { - assertEquals(Arrays.asList(1, 6, 0, 12), SelectorUtils.parseVersion("1.6.0_12")); - - assertEquals(Arrays.asList(1, 6, 0, 12), SelectorUtils.parseVersion("1.6.0_12+")); - assertEquals(Arrays.asList(1, 6, 0, 12), SelectorUtils.parseVersion("1.6.0_12-")); + void testParseVersion() { + assertThat(SelectorUtils.parseVersion("1.6.0_12")).containsExactly(1, 6, 0, 12); + assertThat(SelectorUtils.parseVersion("1.6.0_12+")).containsExactly(1, 6, 0, 12); + assertThat(SelectorUtils.parseVersion("1.6.0_12-")).containsExactly(1, 6, 0, 12); } @Test - public void testCompareVersions() { - assertTrue(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 6)) == 0); - - assertTrue(SelectorUtils.compareVersions(Arrays.asList(1, 5), Arrays.asList(1, 6)) < 0); - assertTrue(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 5)) > 0); - - assertTrue(SelectorUtils.compareVersions(Collections.singletonList(1), Arrays.asList(1, 6)) < 0); - assertTrue(SelectorUtils.compareVersions(Arrays.asList(1, 6), Collections.singletonList(1)) > 0); + void testCompareVersions() { + assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 6))) + .isZero(); + + assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 5), Arrays.asList(1, 6))) + .isNegative(); + assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Arrays.asList(1, 5))) + .isPositive(); + + assertThat(SelectorUtils.compareVersions(Collections.singletonList(1), Arrays.asList(1, 6))) + .isNegative(); + assertThat(SelectorUtils.compareVersions(Arrays.asList(1, 6), Collections.singletonList(1))) + .isPositive(); } @Test - public void testIsMatchingJre() { - - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5")); - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5")); - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5")); - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5")); - - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5+")); - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5+")); - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5+")); - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5+")); - - assertTrue(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5-")); - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5-")); - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5-")); - assertFalse(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5-")); - - assertTrue(SelectorUtils.isJreVersion((String) null, "1.5")); - assertTrue(SelectorUtils.isJreVersion("", "1.5")); + void testIsMatchingJre() { + + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5")).isFalse(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5")).isTrue(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5")).isTrue(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5")).isFalse(); + + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5+")) + .isFalse(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5+")).isTrue(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5+")).isTrue(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5+")).isTrue(); + + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 4, 2, 8), "1.5-")) + .isTrue(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5), "1.5-")).isFalse(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 5, 9), "1.5-")).isFalse(); + assertThat(SelectorUtils.isJreVersion(Arrays.asList(1, 6), "1.5-")).isFalse(); + + assertThat(SelectorUtils.isJreVersion((String) null, "1.5")).isTrue(); + assertThat(SelectorUtils.isJreVersion("", "1.5")).isTrue(); } @Test - public void testIsMatchingToolchain() throws Exception { + void testIsMatchingToolchain() throws Exception { InvokerToolchain openJdk9 = new InvokerToolchain("jdk"); openJdk9.addProvides("version", "9"); openJdk9.addProvides("vendor", "openJDK"); @@ -110,32 +112,37 @@ public void testIsMatchingToolchain() throws Exception { ToolchainPrivateManager toolchainPrivateManager = mock(ToolchainPrivateManager.class); ToolchainPrivate jdkMatching = mock(ToolchainPrivate.class); - when(jdkMatching.matchesRequirements(isA(Map.class))).thenReturn(true); + when(jdkMatching.matchesRequirements(anyMap())).thenReturn(true); when(jdkMatching.getType()).thenReturn("jdk"); ToolchainPrivate jdkMismatch = mock(ToolchainPrivate.class); when(jdkMismatch.getType()).thenReturn("jdk"); when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMatching}); - assertTrue(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))); + assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))) + .isTrue(); when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMismatch}); - assertFalse(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))); + assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))) + .isFalse(); when(toolchainPrivateManager.getToolchainPrivates("jdk")) .thenReturn(new ToolchainPrivate[] {jdkMatching, jdkMismatch, jdkMatching}); - assertTrue(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))); + assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))) + .isTrue(); when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[0]); - assertFalse(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))); + assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Collections.singleton(openJdk9))) + .isFalse(); when(toolchainPrivateManager.getToolchainPrivates("jdk")).thenReturn(new ToolchainPrivate[] {jdkMatching}); when(toolchainPrivateManager.getToolchainPrivates("maven")).thenReturn(new ToolchainPrivate[0]); - assertFalse(SelectorUtils.isToolchain(toolchainPrivateManager, Arrays.asList(openJdk9, maven360))); + assertThat(SelectorUtils.isToolchain(toolchainPrivateManager, Arrays.asList(openJdk9, maven360))) + .isFalse(); } @Test - public void mavenVersionForNotExistingMavenHomeThrowException() { + void mavenVersionForNotExistingMavenHomeThrowException() { File mavenHome = new File("not-existing-path"); assertThatCode(() -> SelectorUtils.getMavenVersion(mavenHome)) @@ -144,7 +151,7 @@ public void mavenVersionForNotExistingMavenHomeThrowException() { } @Test - public void mavenVersionFromMavenHome() throws IOException { + void mavenVersionFromMavenHome() throws IOException { File mavenHome = new File(System.getProperty("maven.home")); String mavenVersion = SelectorUtils.getMavenVersion(mavenHome);