From ee2e6b9022e67fe3aba9978a11ba617f8c07f326 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Mon, 10 Oct 2022 18:29:46 +0200 Subject: [PATCH] #454: Applying bound artifact versions to UpdatePropertiesMojo --- .../mojo/versions/UpdatePropertiesMojo.java | 6 +- .../mojo/versions/api/PropertyVersions.java | 41 +++++++----- .../versions/UpdatePropertiesMojoTest.java | 62 ++++++++++++++++--- 3 files changed, 84 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java index 9cb0e812e0..4716c72cd2 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java @@ -104,7 +104,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo */ @Parameter( property = "allowMajorUpdates", defaultValue = "true" ) - protected boolean allowMajorUpdates; + protected boolean allowMajorUpdates = true; /** * Whether to allow the minor version number to be changed. @@ -113,7 +113,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo */ @Parameter( property = "allowMinorUpdates", defaultValue = "true" ) - protected boolean allowMinorUpdates; + protected boolean allowMinorUpdates = true; /** * Whether to allow the incremental version number to be changed. @@ -122,7 +122,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo */ @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" ) - protected boolean allowIncrementalUpdates; + protected boolean allowIncrementalUpdates = true; // -------------------------- STATIC METHODS -------------------------- diff --git a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java index 7d4621e630..9b25e60462 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -34,16 +34,19 @@ import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.Property; +import org.codehaus.mojo.versions.ordering.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import static java.util.Optional.empty; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; /** * Manages a property that is associated with one or more artifacts. @@ -320,22 +323,21 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert * Retrieves the newest artifact version for the given property-denoted artifact or {@code null} if no newer * version could be found. * - * @param currentVersion current version of the artifact + * @param versionString current version of the artifact * @param property property name indicating the artifact * @param allowSnapshots whether snapshots should be considered * @param reactorProjects collection of reactor projects * @param helper VersionHelper object * @param allowDowngrade whether downgrades should be allowed - * @param unchangedSegment indicates the (0-based) most major segment which needs to stay unchanged; - * -1 means that the whole version can be changed + * @param upperBoundSegment the upper bound segment; empty() means no upper bound * @return newest artifact version fulfilling the criteria or null if no newer version could be found * @throws InvalidSegmentException thrown if the {@code unchangedSegment} is not valid (e.g. greater than the number * of segments in the version string) * @throws InvalidVersionSpecificationException thrown if the version string in the property is not valid */ - public ArtifactVersion getNewestVersion( String currentVersion, Property property, boolean allowSnapshots, + public ArtifactVersion getNewestVersion( String versionString, Property property, boolean allowSnapshots, Collection reactorProjects, VersionsHelper helper, - boolean allowDowngrade, Optional unchangedSegment ) + boolean allowDowngrade, Optional upperBoundSegment ) throws InvalidSegmentException, InvalidVersionSpecificationException { final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots; @@ -346,24 +348,33 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert ? VersionRange.createFromVersionSpec( property.getVersion() ) : null; helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range ); - ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion ); - if ( allowDowngrade ) - { - Optional updatedVersion = getLowerBound( lowerBound, unchangedSegment ); - lowerBound = updatedVersion.map( helper::createArtifactVersion ).orElse( null ); - } + + ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); + ArtifactVersion lowerBound = allowDowngrade + ? getLowerBound( currentVersion, upperBoundSegment ) + .map( DefaultArtifactVersion::new ) + .orElse( null ) + : currentVersion; if ( helper.getLog().isDebugEnabled() ) { helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBound ); } - ArtifactVersion upperBound = null; - if ( unchangedSegment.isPresent() ) + ArtifactVersion upperBound = + !upperBoundSegment.isPresent() + ? null + : upperBoundSegment + .map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion, + s.isMajorTo( SUBINCREMENTAL ) + ? Segment.of( s.value() + 1 ) + : s ) ) + .orElse( null ); + if ( helper.getLog().isDebugEnabled() ) { - upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment.get() ); helper.getLog().debug( "Property ${" + property.getName() + "}: upperBound is: " + upperBound ); } - Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + + Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade ); ArtifactVersion result = getNewestVersion( range, restriction, includeSnapshots ); helper.getLog().debug( "Property ${" + property.getName() + "}: Current winner is: " + result ); diff --git a/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java index 90aaed8d57..d886dd6643 100644 --- a/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java @@ -26,16 +26,22 @@ import java.util.HashMap; import java.util.Objects; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; import org.apache.maven.plugin.testing.stubs.StubArtifactRepository; +import org.codehaus.mojo.versions.change.VersionChange; +import org.codehaus.mojo.versions.utils.TestChangeRecorder; +import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.apache.commons.text.CaseUtils.toCamelCase; import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; +import static org.hamcrest.MatcherAssert.assertThat; /** * Unit tests for {@link UpdatePropertiesMojo} @@ -45,12 +51,19 @@ public class UpdatePropertiesMojoTest extends AbstractMojoTestCase @Rule public MojoRule mojoRule = new MojoRule( this ); private Path pomDir; + private ArtifactMetadataSource artifactMetadataSource; + private TestChangeRecorder changeRecorder; @Before public void setUp() throws Exception { super.setUp(); - pomDir = Files.createTempDirectory( "update-properties-" ); + pomDir = Files.createTempDirectory( toCamelCase( getClass().getSimpleName(), false ) ); + changeRecorder = new TestChangeRecorder(); + artifactMetadataSource = mockArtifactMetadataSource( new HashMap() + {{ + put( "default-artifact", new String[] { "1.0.0", "1.0.1-rc1", "1.1.0-alpha", "2.0.0-M1" } ); + }} ); } @After @@ -70,18 +83,53 @@ public void tearDown() throws Exception } } + private void setUpMojo( UpdatePropertiesMojo mojo ) throws IllegalAccessException + { + mojo.localRepository = new StubArtifactRepository( pomDir.toString() ); + mojo.artifactMetadataSource = artifactMetadataSource; + setVariableValueToObject( mojo, "changeRecorder", changeRecorder ); + setVariableValueToObject( mojo, "generateBackupPoms", false ); + } + @Test - public void testUpdatePropertiesDisallowMajorUpdates() throws Exception + public void testUpdatePropertiesAllowMajorUpdates() throws Exception { Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ), Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING ); UpdatePropertiesMojo mojo = (UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" ); - mojo.localRepository = new StubArtifactRepository( pomDir.toString() ); - mojo.artifactMetadataSource = mockArtifactMetadataSource( new HashMap() - {{ - put( "default-artifact", new String[] { "1.0.0", "2.0.0-M1" } ); - }} ); + setUpMojo( mojo ); + mojo.execute(); + assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group", + "default-artifact", "1.0.0", "2.0.0-M1" ) ) ); + } + + @Test + public void testUpdatePropertiesAllowMinorUpdates() throws Exception + { + Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ), + Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING ); + UpdatePropertiesMojo mojo = + (UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" ); + setUpMojo( mojo ); + mojo.allowMajorUpdates = false; + mojo.execute(); + assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group", + "default-artifact", "1.0.0", "1.1.0-alpha" ) ) ); + } + + @Test + public void testUpdatePropertiesAllowIncrementalUpdates() throws Exception + { + Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ), + Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING ); + UpdatePropertiesMojo mojo = + (UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" ); + setUpMojo( mojo ); + mojo.allowMajorUpdates = false; + mojo.allowMinorUpdates = false; mojo.execute(); + assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group", + "default-artifact", "1.0.0", "1.0.1-rc1" ) ) ); } }