diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextReleasesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextReleasesMojo.java index ca5bb9b027..a0fdfddcc9 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextReleasesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextReleasesMojo.java @@ -23,9 +23,9 @@ import javax.xml.stream.XMLStreamException; import java.io.IOException; +import java.util.Arrays; import java.util.Collection; import java.util.Map; -import java.util.regex.Pattern; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -41,7 +41,6 @@ import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import static java.util.Collections.singletonList; -import static java.util.Optional.of; /** * Replaces any release versions with the next release version (if it has been released). @@ -52,13 +51,6 @@ @Mojo(name = "use-next-releases", threadSafe = true) public class UseNextReleasesMojo extends UseLatestVersionsMojoBase { - // ------------------------------ FIELDS ------------------------------ - - /** - * Pattern to match a snapshot version. - */ - private static final Pattern MATCH_SNAPSHOT_REGEX = Pattern.compile("^(.+)-((SNAPSHOT)|(\\d{8}\\.\\d{6}-\\d+))$"); - // ------------------------------ METHODS -------------------------- @Inject @@ -107,7 +99,8 @@ private void useNextReleases( useLatestVersions( pom, dependencies, - (dep, versions) -> of(versions.getNewerVersions(dep.getVersion(), false)[0]), + (dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), false)) + .findFirst(), changeKind, dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches()); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextVersionsMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextVersionsMojo.java index 7e3654f79b..b263c0887c 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextVersionsMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextVersionsMojo.java @@ -23,6 +23,7 @@ import javax.xml.stream.XMLStreamException; import java.io.IOException; +import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -40,7 +41,6 @@ import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import static java.util.Collections.singletonList; -import static java.util.Optional.of; /** * Replaces any version with the latest version. @@ -97,7 +97,8 @@ private void useNextVersions( useLatestVersions( pom, dependencies, - (dep, versions) -> of(versions.getNewerVersions(dep.getVersion(), allowSnapshots)[0]), + (dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), allowSnapshots)) + .findFirst(), changeKind); } } diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java index f103df7359..2760d8807b 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java @@ -4,12 +4,9 @@ import java.util.HashMap; -import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PomHelper; @@ -27,11 +24,10 @@ import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; +import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.when; public class UseLatestReleasesMojoTest { private UseLatestReleasesMojo mojo; @@ -39,20 +35,7 @@ public class UseLatestReleasesMojoTest { @Before public void setUp() throws Exception { - RepositorySystem repositorySystemMock = mock(RepositorySystem.class); - when(repositorySystemMock.createDependencyArtifact(any(Dependency.class))) - .thenAnswer(invocation -> { - Dependency dependency = invocation.getArgument(0); - return new DefaultArtifact( - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion(), - dependency.getScope(), - dependency.getType(), - dependency.getClassifier() != null ? dependency.getClassifier() : "default", - new DefaultArtifactHandlerStub("default")); - }); - + RepositorySystem repositorySystemMock = mockRepositorySystem(); org.eclipse.aether.RepositorySystem aetherRepositorySystem = mockAetherRepositorySystem(new HashMap() { { diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java index 0c3095af84..ea7a12b135 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java @@ -7,12 +7,10 @@ import java.util.HashMap; import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PomHelper; @@ -31,37 +29,22 @@ import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; +import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.not; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.when; -@SuppressWarnings("deprecation") public class UseLatestVersionsMojoTest { private UseLatestVersionsMojo mojo; private TestChangeRecorder changeRecorder; @Before public void setUp() throws Exception { - RepositorySystem repositorySystemMock = mock(RepositorySystem.class); - when(repositorySystemMock.createDependencyArtifact(any(Dependency.class))) - .thenAnswer(invocation -> { - Dependency dependency = invocation.getArgument(0); - return new DefaultArtifact( - dependency.getGroupId(), - dependency.getArtifactId(), - dependency.getVersion(), - dependency.getScope(), - dependency.getType(), - dependency.getClassifier() != null ? dependency.getClassifier() : "default", - new DefaultArtifactHandlerStub("default")); - }); - + RepositorySystem repositorySystemMock = mockRepositorySystem(); org.eclipse.aether.RepositorySystem aetherRepositorySystem = mockAetherRepositorySystem(new HashMap() { { diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextReleasesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextReleasesMojoTest.java new file mode 100644 index 0000000000..3a8329f343 --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextReleasesMojoTest.java @@ -0,0 +1,126 @@ +package org.codehaus.mojo.versions; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.util.Collections; +import java.util.HashMap; + +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.change.DefaultVersionChange; +import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.TestChangeRecorder; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; + +import static java.util.Collections.emptyList; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; +import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; +import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; + +/** + * Unit tests for {@link UseNextReleasesMojo} + */ +public class UseNextReleasesMojoTest { + + private UseNextReleasesMojo mojo; + private TestChangeRecorder changeRecorder; + + @Before + public void setUp() throws Exception { + RepositorySystem repositorySystemMock = mockRepositorySystem(); + org.eclipse.aether.RepositorySystem aetherRepositorySystem = + mockAetherRepositorySystem(new HashMap() { + { + put("dependency-artifact", new String[] {"1.0.0", "1.1.0", "1.1.1-SNAPSHOT"}); + } + }); + changeRecorder = new TestChangeRecorder(); + mojo = new UseNextReleasesMojo(repositorySystemMock, aetherRepositorySystem, null, changeRecorder.asTestMap()) { + { + reactorProjects = emptyList(); + session = mockMavenSession(); + project = new MavenProject() { + { + setModel(new Model() { + { + setGroupId("default-group"); + setArtifactId("project-artifact"); + setVersion("1.0.0-SNAPSHOT"); + } + }); + setDependencies(Collections.singletonList(DependencyBuilder.dependencyWith( + "default-group", "dependency-artifact", "1.1.0", "default", "pom", SCOPE_COMPILE))); + } + }; + } + }; + setVariableValueToObject(mojo, "processDependencyManagement", false); + } + + @Test + public void testNoNewerReleases() { + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) + .thenReturn(true); + pomHelper + .when(() -> PomHelper.getRawModel(any(MavenProject.class))) + .thenReturn(mojo.getProject().getModel()); + mojo.update(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + assertThat(changeRecorder.getChanges(), Matchers.empty()); + } + + @Test + public void testFindANewerRelease() throws IllegalAccessException { + setVariableValueToObject( + mojo, "aetherRepositorySystem", mockAetherRepositorySystem(new HashMap() { + { + put("dependency-artifact", new String[] {"1.0.0", "1.1.0", "1.1.1", "2.0.0"}); + } + })); + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) + .thenReturn(true); + pomHelper + .when(() -> PomHelper.getRawModel(any(MavenProject.class))) + .thenReturn(mojo.getProject().getModel()); + mojo.update(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1"))); + } +} diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextVersionsMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextVersionsMojoTest.java new file mode 100644 index 0000000000..0dddd3f4b5 --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/UseNextVersionsMojoTest.java @@ -0,0 +1,132 @@ +package org.codehaus.mojo.versions; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.util.Collections; +import java.util.HashMap; + +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.change.DefaultVersionChange; +import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.TestChangeRecorder; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; + +import static java.util.Collections.emptyList; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; +import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; +import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; + +/** + * Unit tests for {@link UseNextVersionsMojo} + */ +public class UseNextVersionsMojoTest { + + private UseNextVersionsMojo mojo; + private TestChangeRecorder changeRecorder; + + @Before + public void setUp() throws Exception { + RepositorySystem repositorySystemMock = mockRepositorySystem(); + org.eclipse.aether.RepositorySystem aetherRepositorySystem = + mockAetherRepositorySystem(new HashMap() { + { + put("dependency-artifact", new String[] {"1.0.0", "1.1.0-SNAPSHOT"}); + } + }); + changeRecorder = new TestChangeRecorder(); + mojo = new UseNextVersionsMojo(repositorySystemMock, aetherRepositorySystem, null, changeRecorder.asTestMap()) { + { + reactorProjects = emptyList(); + session = mockMavenSession(); + project = new MavenProject() { + { + setModel(new Model() { + { + setGroupId("default-group"); + setArtifactId("project-artifact"); + setVersion("1.0.0-SNAPSHOT"); + } + }); + setDependencies(Collections.singletonList(DependencyBuilder.dependencyWith( + "default-group", + "dependency-artifact", + "1.1.0-SNAPSHOT", + "default", + "pom", + SCOPE_COMPILE))); + } + }; + } + }; + setVariableValueToObject(mojo, "processDependencyManagement", false); + } + + @Test + public void testNoNewerVersions() { + + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) + .thenReturn(true); + pomHelper + .when(() -> PomHelper.getRawModel(any(MavenProject.class))) + .thenReturn(mojo.getProject().getModel()); + mojo.update(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + assertThat(changeRecorder.getChanges(), Matchers.empty()); + } + + @Test + public void testFindANewerVersion() throws IllegalAccessException { + setVariableValueToObject( + mojo, "aetherRepositorySystem", mockAetherRepositorySystem(new HashMap() { + { + put("dependency-artifact", new String[] {"1.0.0", "1.1.0-SNAPSHOT", "1.1.1", "2.0.0"}); + } + })); + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setDependencyVersion(any(), any(), any(), any(), any(), any())) + .thenReturn(true); + pomHelper + .when(() -> PomHelper.getRawModel(any(MavenProject.class))) + .thenReturn(mojo.getProject().getModel()); + mojo.update(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0-SNAPSHOT", "1.1.1"))); + } +}