From 1cfdb5402a6eee97854edc610433a38524a2a240 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Sat, 29 Jul 2023 18:41:58 +0200 Subject: [PATCH] Resolves #990: Check prerequisites if required enforcer Maven version is empty --- .../versions/DisplayPluginUpdatesMojo.java | 3 +- .../MinimalMavenBuildVersionFinder.java | 32 +++- .../DisplayPluginUpdatesMojoTest.java | 160 ++++++++++++++++++ .../MinimalMavenBuildVersionFinderTest.java | 29 ++++ .../issue-990/no-enforcer.xml | 39 +++++ .../issue-990/no-prerequisites.xml | 55 ++++++ .../issue-990/prerequisites-greater.xml | 59 +++++++ .../issue-990/prerequisites-lesser.xml | 59 +++++++ 8 files changed, 428 insertions(+), 8 deletions(-) create mode 100644 versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-enforcer.xml create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-prerequisites.xml create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-greater.xml create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-lesser.xml diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java index 4a8df84227..d263633cce 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java @@ -51,7 +51,6 @@ import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.model.BuildBase; import org.apache.maven.model.Model; @@ -816,7 +815,7 @@ private Set findPluginsWithVersionsSpecified(StringBuilder pomContents, private ArtifactVersion getPrerequisitesMavenVersion(MavenProject pluginProject) { return ofNullable(pluginProject.getPrerequisites()) .map(Prerequisites::getMaven) - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .orElse(DefaultArtifactVersionCache.of(DEFAULT_MVN_VERSION)); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java index de90d72fe1..8e7e841afe 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java @@ -1,16 +1,22 @@ package org.codehaus.mojo.versions; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.Optional; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; +import org.apache.maven.model.Prerequisites; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.plexus.util.xml.Xpp3Dom; +import static java.util.Optional.ofNullable; + /** * Finds the minimal Maven version required to build a Maven project. * Evaluates the {@code maven-enforcer-plugin:enforce} goal and @@ -24,12 +30,26 @@ private MinimalMavenBuildVersionFinder() { // not supposed to be created, static methods only } - static ArtifactVersion find(MavenProject mavenProject, String defaultVersion, Log log) { - ArtifactVersion version = getEnforcerMavenVersion(mavenProject, log); - if (version == null && defaultVersion != null) { - version = DefaultArtifactVersionCache.of(defaultVersion); - } - return version; + static Optional getGreatestVersion(ArtifactVersion... v) { + return Arrays.stream(v).filter(Objects::nonNull).reduce((v1, v2) -> { + if (v1.compareTo(v2) >= 0) { + return v1; + } + return v2; + }); + } + + static ArtifactVersion find(MavenProject mavenProject, String defaultMavenVersion, Log log) { + return getGreatestVersion( + getEnforcerMavenVersion(mavenProject, log), + ofNullable(mavenProject.getPrerequisites()) + .map(Prerequisites::getMaven) + .map(DefaultArtifactVersionCache::of) + .orElse(null), + ofNullable(defaultMavenVersion) + .map(DefaultArtifactVersionCache::of) + .orElse(null)) + .orElse(null); } private static ArtifactVersion getEnforcerMavenVersion(MavenProject mavenProject, Log log) { diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java new file mode 100644 index 0000000000..71f03d68d2 --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java @@ -0,0 +1,160 @@ +package org.codehaus.mojo.versions; + +/* + * Copyright MojoHaus and Contributors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Prerequisites; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.project.*; +import org.codehaus.mojo.versions.utils.TestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.apache.commons.codec.CharEncoding.UTF_8; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Contains unit tests for {@link DisplayPluginUpdatesMojo}. + * + * @author Andrzej Jarmoniuk + */ +public class DisplayPluginUpdatesMojoTest extends AbstractMojoTestCase { + + @Rule + public MojoRule mojoRule = new MojoRule(this); + + private Path tempDir; + + private Path outputPath; + + @Before + public void setUp() throws Exception { + super.setUp(); + tempDir = TestUtils.createTempDir("display-plugin-updates"); + outputPath = Files.createTempFile(tempDir, "output", ""); + } + + @After + public void tearDown() throws Exception { + try { + TestUtils.tearDownTempDir(tempDir); + } finally { + super.tearDown(); + } + } + + private static ProjectBuilder mockupProjectBuilder() { + Prerequisites prerequisites = new Prerequisites(); + prerequisites.setMaven("3.6.3"); + MavenProject mavenProject = mock(MavenProject.class); + when(mavenProject.getPrerequisites()).thenReturn(prerequisites); + ProjectBuildingResult projectBuildingResult = mock(ProjectBuildingResult.class); + when(projectBuildingResult.getProject()).thenReturn(mavenProject); + ProjectBuilder projectBuilder = mock(ProjectBuilder.class); + try { + when(projectBuilder.build(any(Artifact.class), anyBoolean(), any(ProjectBuildingRequest.class))) + .thenReturn(projectBuildingResult); + } catch (ProjectBuildingException e) { + throw new RuntimeException(e); + } + return projectBuilder; + } + + private DisplayPluginUpdatesMojo createMojo() throws Exception { + DisplayPluginUpdatesMojo mojo = + (DisplayPluginUpdatesMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "display-plugin-updates"); + mojo.outputEncoding = UTF_8; + mojo.outputFile = outputPath.toFile(); + mojo.setPluginContext(new HashMap<>()); + mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap() { + { + put("default-plugin", new String[] {"1.0.0"}); + put("maven-enforcer-plugin", new String[] {"3.0.0"}); + } + }); + setVariableValueToObject(mojo, "projectBuilder", mockupProjectBuilder()); + return mojo; + } + + @Test + public void testNoEnforcer() throws Exception { + Files.copy( + Paths.get("src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-enforcer.xml"), + tempDir.resolve("pom.xml")); + + DisplayPluginUpdatesMojo mojo = createMojo(); + mojo.execute(); + + List output = Files.readAllLines(outputPath); + assertThat(output, hasItem(containsString("Using the minimum version of Maven: 3.3.9"))); + } + + @Test + public void testNoPrerequisites() throws Exception { + Files.copy( + Paths.get("src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-prerequisites.xml"), + tempDir.resolve("pom.xml")); + + DisplayPluginUpdatesMojo mojo = createMojo(); + mojo.execute(); + + List output = Files.readAllLines(outputPath); + assertThat(output, hasItem(containsString("Using the minimum version of Maven: 3.3.9"))); + } + + @Test + public void testPrerequisitesGreaterThanEnforcer() throws Exception { + Files.copy( + Paths.get( + "src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-greater.xml"), + tempDir.resolve("pom.xml")); + + DisplayPluginUpdatesMojo mojo = createMojo(); + mojo.execute(); + + List output = Files.readAllLines(outputPath); + assertThat(output, hasItem(containsString("Using the minimum version of Maven: 3.3.9"))); + } + + @Test + public void testPrerequisitesLesserThanEnforcer() throws Exception { + Files.copy( + Paths.get( + "src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-lesser.xml"), + tempDir.resolve("pom.xml")); + + DisplayPluginUpdatesMojo mojo = createMojo(); + mojo.execute(); + + List output = Files.readAllLines(outputPath); + assertThat(output, hasItem(containsString("Using the minimum version of Maven: 3.3.9"))); + } +} diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java index c558405a71..036e58680d 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java @@ -1,5 +1,7 @@ package org.codehaus.mojo.versions; +import java.util.Optional; + import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; @@ -38,4 +40,31 @@ public void testInvalidVersionRanges() { assertNull(MinimalMavenBuildVersionFinder.getMinimumVersionFromRange("()1.0")); assertNull(MinimalMavenBuildVersionFinder.getMinimumVersionFromRange("(1.0]")); } + + @Test + public void testGetGreatestVersion() { + assertEquals( + MinimalMavenBuildVersionFinder.getGreatestVersion( + new DefaultArtifactVersion("1"), new DefaultArtifactVersion("2")), + Optional.of(new DefaultArtifactVersion("2"))); + assertEquals( + MinimalMavenBuildVersionFinder.getGreatestVersion( + new DefaultArtifactVersion("1"), + new DefaultArtifactVersion("2"), + new DefaultArtifactVersion("3")), + Optional.of(new DefaultArtifactVersion("3"))); + assertEquals( + MinimalMavenBuildVersionFinder.getGreatestVersion( + new DefaultArtifactVersion("3"), + new DefaultArtifactVersion("2"), + new DefaultArtifactVersion("1")), + Optional.of(new DefaultArtifactVersion("3"))); + assertEquals( + MinimalMavenBuildVersionFinder.getGreatestVersion( + new DefaultArtifactVersion("1"), null, new DefaultArtifactVersion("3")), + Optional.of(new DefaultArtifactVersion("3"))); + assertEquals( + MinimalMavenBuildVersionFinder.getGreatestVersion(new DefaultArtifactVersion("1"), null), + Optional.of(new DefaultArtifactVersion("1"))); + } } diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-enforcer.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-enforcer.xml new file mode 100644 index 0000000000..6558339b2f --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-enforcer.xml @@ -0,0 +1,39 @@ + + + + + 4.0.0 + + default-group + default-artifact + 1.0.0 + + + 3.3.9 + + + + + + default-group + default-plugin + 1.0.0 + + + + diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-prerequisites.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-prerequisites.xml new file mode 100644 index 0000000000..bac65abd2d --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/no-prerequisites.xml @@ -0,0 +1,55 @@ + + + + + 4.0.0 + + default-group + default-artifact + 1.0.0 + + + + + default-group + default-plugin + 1.0.0 + + + + maven-enforcer-plugin + 3.0.0 + + + enforce-maven + + enforce + + + + + 3.3.9 + + + + + + + + + diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-greater.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-greater.xml new file mode 100644 index 0000000000..d067ab245c --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-greater.xml @@ -0,0 +1,59 @@ + + + + + 4.0.0 + + default-group + default-artifact + 1.0.0 + + + 3.3.9 + + + + + + default-group + default-plugin + 1.0.0 + + + + maven-enforcer-plugin + 3.0.0 + + + enforce-maven + + enforce + + + + + 3.3.8 + + + + + + + + + diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-lesser.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-lesser.xml new file mode 100644 index 0000000000..cad19c35f3 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/prerequisites-lesser.xml @@ -0,0 +1,59 @@ + + + + + 4.0.0 + + default-group + default-artifact + 1.0.0 + + + 3.3.8 + + + + + + default-group + default-plugin + 1.0.0 + + + + maven-enforcer-plugin + 3.0.0 + + + enforce-maven + + enforce + + + + + 3.3.9 + + + + + + + + +