diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 14fc8aa333..9b9b4dca77 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -28,8 +28,10 @@ import java.io.IOException; import java.io.Writer; import java.util.List; +import java.util.Optional; import java.util.Set; +import org.apache.commons.text.CaseUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; @@ -55,6 +57,7 @@ import org.codehaus.mojo.versions.api.DefaultVersionsHelper; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.model.RuleSet; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; @@ -67,6 +70,12 @@ import org.codehaus.plexus.util.WriterFactory; import org.codehaus.stax2.XMLInputFactory2; +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; + /** * Abstract base class for Versions Mojos. * @@ -568,43 +577,33 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A * @param allowIncrementalUpdates Allow incremental updates * @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1. */ - protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, - boolean allowIncrementalUpdates ) + protected Optional determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, + boolean allowIncrementalUpdates ) { - int segment; - if ( allowMajorUpdates ) - { - segment = -1; - getLog().info( "Major version changes allowed" ); - } - else if ( allowMinorUpdates ) - { - segment = 0; - getLog().info( "Minor version changes allowed" ); - } - else if ( allowIncrementalUpdates ) - { - segment = 1; - getLog().info( "Incremental version changes allowed" ); - } - else + Optional unchangedSegment = allowMajorUpdates ? empty() + : allowMinorUpdates ? of( MAJOR ) + : allowIncrementalUpdates ? of( MINOR ) + : of( INCREMENTAL ); + if ( getLog().isInfoEnabled() ) { - segment = 2; - getLog().info( "Subincremental version changes allowed" ); + getLog().info( + unchangedSegment.map( s -> + CaseUtils.toCamelCase( Segment.of( s.value() + 1 ).toString(), true ) ) + .orElse( "All" ) + " version changes allowed" ); } - - return segment; + return unchangedSegment; } protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property, PropertyVersions version, String currentVersion, - boolean allowDowngrade, int segment ) + boolean allowDowngrade, + Optional unchangedSegment ) throws XMLStreamException, InvalidVersionSpecificationException, InvalidSegmentException, MojoExecutionException { ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects, - this.getHelper(), allowDowngrade, segment ); + this.getHelper(), allowDowngrade, unchangedSegment ); if ( winner == null || currentVersion.equals( winner.toString() ) ) { diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java index 238077de9b..97c15dc585 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.TreeSet; import org.apache.maven.artifact.manager.WagonManager; @@ -40,6 +41,7 @@ import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -155,11 +157,12 @@ public void execute() continue; } - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = + determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); try { ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, - this.reactorProjects, this.getHelper(), false, segment ); + this.reactorProjects, this.getHelper(), false, unchangedSegment ); if ( winner != null && !currentVersion.equals( winner.toString() ) ) { StringBuilder buf = new StringBuilder(); diff --git a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java index 14bf5c73b5..0ed8fe5b68 100644 --- a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Map; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -44,6 +45,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -310,11 +312,12 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom ) property.setVersion( currentVersion ); - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates ); // TODO: Check if we could add allowDowngrade ? try { - updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment ); + updatePropertyToNewestVersion( pom, property, version, currentVersion, false, unchangedSegment ); } catch ( InvalidSegmentException | InvalidVersionSpecificationException e ) { diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java index 910eff7439..9cb0e812e0 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java @@ -23,6 +23,7 @@ import javax.xml.stream.XMLStreamException; import java.util.Map; +import java.util.Optional; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; @@ -37,6 +38,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -179,13 +181,13 @@ protected void update( ModifiedPomXMLEventReader pom ) if ( canUpdateProperty ) { - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); try { ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, - allowDowngrade, segment ); + allowDowngrade, unchangedSegment ); if ( targetVersion != null ) { diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java index 0101e5ebdc..ea7e4c002c 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java @@ -23,6 +23,7 @@ import javax.xml.stream.XMLStreamException; import java.util.Map; +import java.util.Optional; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; @@ -37,6 +38,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -163,11 +165,12 @@ protected void update( ModifiedPomXMLEventReader pom ) continue; } - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = + determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); try { ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, - allowDowngrade, segment ); + allowDowngrade, unchangedSegment ); if ( targetVersion != null ) { diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java index c48197b2c6..ba1ad991e4 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -47,6 +48,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -147,7 +149,8 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates ); MajorMinorIncrementalFilter majorMinorIncfilter = new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); @@ -182,7 +185,7 @@ private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates ); MajorMinorIncrementalFilter majorMinorIncfilter = new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); @@ -180,15 +184,18 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection versionComparator.getSegmentCount( lowerBound ) ) + if ( unchangedSegment.isPresent() + && unchangedSegment.get().value() >= versionComparator.getSegmentCount( lowerBound ) ) { getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" ); continue; } try { - ArtifactVersion upperBound = - segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; + ArtifactVersion upperBound = unchangedSegment.isPresent() + && unchangedSegment.get().value() >= MAJOR.value() + ? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() ) + : null; getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); ArtifactVersion[] newer = versions.getVersions( restriction, true ); diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java index e93b7d5a3c..172da4e174 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.util.Collection; +import java.util.Optional; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; @@ -42,6 +43,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -163,7 +165,7 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - int unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); MajorMinorIncrementalFilter majorMinorIncfilter = new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); diff --git a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index be11af6fa4..3050b2ce9a 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Collection; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -44,10 +45,13 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; + /** * Replaces any release versions with the next snapshot version (if it has been deployed). * @@ -132,7 +136,9 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional + unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates ); for ( Dependency dep : dependencies ) { @@ -162,15 +168,18 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection versionComparator.getSegmentCount( lowerBound ) ) + if ( unchangedSegment.isPresent() + && unchangedSegment.get().value() >= versionComparator.getSegmentCount( lowerBound ) ) { getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" ); continue; } try { - ArtifactVersion upperBound = - segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; + ArtifactVersion upperBound = unchangedSegment.isPresent() + && unchangedSegment.get().value() >= MAJOR.value() + ? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() ) + : null; getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); ArtifactVersion[] newer = versions.getVersions( restriction, true ); diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 2bab33c80a..e846a20914 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -225,16 +225,18 @@ public final ArtifactVersion[] getNewerVersions( String version, boolean include * Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots * should be included. * - * @param version current version - * @param upperBoundSegment the upper bound segment - * @param includeSnapshots whether snapshot versions should be included - * @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, int, boolean, boolean)} instead + * @param version current version + * @param upperBoundSegment the upper bound segment; empty() means no upper bound + * @param includeSnapshots whether snapshot versions should be included * @return array of newer versions fulfilling the criteria * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than - * the segment count) + * the segment count) + * @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, Optional, boolean, boolean)}, + * boolean, boolean)} instead */ @Deprecated - public final ArtifactVersion[] getNewerVersions( String version, int upperBoundSegment, boolean includeSnapshots ) + public final ArtifactVersion[] getNewerVersions( String version, Optional upperBoundSegment, + boolean includeSnapshots ) throws InvalidSegmentException { return getNewerVersions( version, upperBoundSegment, includeSnapshots, false ); @@ -245,7 +247,7 @@ public final ArtifactVersion[] getNewerVersions( String version, int upperBoundS * should be included. * * @param versionString current version - * @param upperBoundSegment the upper bound segment + * @param upperBoundSegment the upper bound segment; empty() means no upper bound * @param includeSnapshots whether snapshot versions should be included * @param allowDowngrade whether to allow downgrading if the current version is a snapshots and snapshots * are disallowed @@ -253,15 +255,15 @@ public final ArtifactVersion[] getNewerVersions( String version, int upperBoundS * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than * the segment count) */ - public final ArtifactVersion[] getNewerVersions( String versionString, int upperBoundSegment, + public final ArtifactVersion[] getNewerVersions( String versionString, Optional upperBoundSegment, boolean includeSnapshots, boolean allowDowngrade ) throws InvalidSegmentException { ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); ArtifactVersion lowerBound = allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion; - ArtifactVersion upperBound = upperBoundSegment == -1 ? null - : getVersionComparator().incrementSegment( lowerBound, upperBoundSegment ); + ArtifactVersion upperBound = !upperBoundSegment.isPresent() ? null + : getVersionComparator().incrementSegment( lowerBound, upperBoundSegment.get() ); Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade ); return getVersions( restriction, includeSnapshots ); @@ -436,13 +438,14 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu * implying that there is also no string designation of the lower bound version. * * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed - * @param unchangedSegment 0-based index of the first segment not to be changed; -1 means everything can change + * @param unchangedSegment first segment not to be changed; empty() means any segment can change * @return {@link ArtifactVersion} the lowest artifact version with the given segment held or null if no such * version can be found * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than * the segment count) */ - protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int unchangedSegment ) + protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, + Optional unchangedSegment ) throws InvalidSegmentException { Optional lowerBound = getLowerBound( version, unchangedSegment ); @@ -455,48 +458,46 @@ protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, * implying that there is also no string designation of the lower bound version. * * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed - * @param unchangedSegment 0-based index of the first segment not to be changed; -1 means everything can change + * @param unchangedSegment first segment not to be changed; empty() means anything can change * @return {@link Optional} string containing the lowest artifact version with the given segment held * @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than * the segment count) */ - protected Optional getLowerBound( ArtifactVersion version, int unchangedSegment ) + protected Optional getLowerBound( ArtifactVersion version, Optional unchangedSegment ) throws InvalidSegmentException { - if ( unchangedSegment < 0 ) + if ( !unchangedSegment.isPresent() ) { return empty(); } int segmentCount = getVersionComparator().getSegmentCount( version ); - if ( unchangedSegment > segmentCount ) + if ( unchangedSegment.get().value() > segmentCount ) { - throw new InvalidSegmentException( unchangedSegment, segmentCount, version ); + throw new InvalidSegmentException( unchangedSegment.get(), segmentCount, version ); } StringBuilder newVersion = new StringBuilder(); newVersion.append( version.getMajorVersion() ); + if ( segmentCount > 0 ) { - newVersion.append( "." ) - .append( unchangedSegment >= 1 ? version.getMinorVersion() : 0 ); + newVersion.append( "." ).append( unchangedSegment.get().value() >= 1 ? version.getMinorVersion() : 0 ); } if ( segmentCount > 1 ) { newVersion.append( "." ) - .append( unchangedSegment >= 2 ? version.getIncrementalVersion() : 0 ); + .append( unchangedSegment.get().value() >= 2 ? version.getIncrementalVersion() : 0 ); } if ( segmentCount > 2 ) { if ( version.getQualifier() != null ) { - newVersion.append( "-" ) - .append( unchangedSegment >= 3 ? version.getQualifier() : "0" ); + newVersion.append( "-" ).append( unchangedSegment.get().value() >= 3 ? version.getQualifier() : "0" ); } else { - newVersion.append( "-" ) - .append( unchangedSegment >= 3 ? version.getBuildNumber() : "0" ); + newVersion.append( "-" ).append( unchangedSegment.get().value() >= 3 ? version.getBuildNumber() : "0" ); } } return of( newVersion.toString() ); @@ -539,10 +540,10 @@ protected Restriction restrictionFor( ArtifactVersion currentVersion, Optional Segment.MAJOR.value() - ? versionComparator.incrementSegment( currentVersion, scope.get().value() - 1 ) + ? versionComparator.incrementSegment( currentVersion, Segment.of( scope.get().value() - 1 ) ) : null; return new Restriction( lowerBound, lowerBound != currentVersion, upperBound, false ); 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 6292349644..6cfc260f21 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -43,6 +43,8 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; +import static java.util.Optional.empty; + /** * Manages a property that is associated with one or more artifacts. * @@ -310,7 +312,8 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert List reactorProjects, VersionsHelper helper ) throws InvalidVersionSpecificationException, InvalidSegmentException { - return getNewestVersion( currentVersion, property, allowSnapshots, reactorProjects, helper, false, -1 ); + return getNewestVersion( currentVersion, property, allowSnapshots, reactorProjects, helper, + false, empty() ); } /** @@ -332,7 +335,7 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert */ public ArtifactVersion getNewestVersion( String currentVersion, Property property, boolean allowSnapshots, Collection reactorProjects, VersionsHelper helper, - boolean allowDowngrade, int unchangedSegment ) + boolean allowDowngrade, Optional unchangedSegment ) throws InvalidSegmentException, InvalidVersionSpecificationException { final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots; @@ -355,9 +358,9 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert } ArtifactVersion upperBound = null; - if ( unchangedSegment != -1 ) + if ( unchangedSegment.isPresent() ) { - upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment ); + upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment.get() ); helper.getLog().debug( "Property ${" + property.getName() + "}: upperBound is: " + upperBound ); } Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); @@ -418,22 +421,22 @@ else if ( getVersionComparator().compare( result, fromReactor ) < 0 ) return result; } - private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper helper, int segment, + private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper helper, + Optional unchangedSegment, boolean includeSnapshots, VersionRange range ) throws InvalidSegmentException { ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion ); ArtifactVersion upperBound = null; - if ( segment != -1 ) + if ( unchangedSegment.isPresent() ) { - upperBound = getVersionComparator().incrementSegment( lowerBound, segment ); + upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment.get() ); } Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); return getNewestVersion( range, restriction, includeSnapshots ); } - private final class PropertyVersionComparator - implements VersionComparator + private final class PropertyVersionComparator implements VersionComparator { public int compare( ArtifactVersion v1, ArtifactVersion v2 ) { @@ -494,7 +497,7 @@ public int getSegmentCount( ArtifactVersion v ) return result; } - public ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException + public ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { if ( !isAssociated() ) { diff --git a/src/main/java/org/codehaus/mojo/versions/api/Segment.java b/src/main/java/org/codehaus/mojo/versions/api/Segment.java index b64e8abc20..c329898914 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/Segment.java +++ b/src/main/java/org/codehaus/mojo/versions/api/Segment.java @@ -45,6 +45,19 @@ public int value() return index; } + public static Segment of( int index ) + { + switch ( index ) + { + case 0: return MAJOR; + case 1: return MINOR; + case 2: return INCREMENTAL; + case 3: return SUBINCREMENTAL; + default: + throw new IllegalArgumentException( "Wrong segment index: " + index ); + } + } + @Override public String toString() { diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java index afed874df2..1108ac3494 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java @@ -20,6 +20,7 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; /** * Base class for version comparators. @@ -56,17 +57,22 @@ public final int getSegmentCount( ArtifactVersion v ) /** * {@inheritDoc} */ - public final ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException + public final ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { if ( VersionComparators.isSnapshot( v ) ) { return VersionComparators.copySnapshot( v, innerIncrementSegment( VersionComparators.stripSnapshot( v ), segment ) ); } + int segmentCount = getSegmentCount( v ); + if ( segment.value() >= segmentCount ) + { + throw new InvalidSegmentException( segment, segmentCount, v ); + } return innerIncrementSegment( v, segment ); } - protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) + protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException; /** diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java b/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java index ce704ff423..ad656cfe63 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java @@ -20,13 +20,14 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; /** * Represents an invalid segment being identified within a version. */ public class InvalidSegmentException extends Exception { - private final int segment; + private final Segment segment; private final int segmentCount; @@ -39,9 +40,9 @@ public class InvalidSegmentException extends Exception * @param segmentCount the number of segments. * @param version the version object. */ - public InvalidSegmentException( int segment, int segmentCount, ArtifactVersion version ) + public InvalidSegmentException( Segment segment, int segmentCount, ArtifactVersion version ) { - super( String.format( "Invalid segment, %d, for the %d segment version: '%s'", segment, segmentCount, + super( String.format( "Invalid segment %s for the %d segment version: '%s'", segment.toString(), segmentCount, version.toString() ) ); this.segment = segment; this.segmentCount = segmentCount; @@ -51,7 +52,7 @@ public InvalidSegmentException( int segment, int segmentCount, ArtifactVersion v /** * @return segment */ - public int getSegment() + public Segment getSegment() { return segment; } diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java b/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java index a88311698a..39b7cba69d 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java @@ -21,6 +21,7 @@ import java.util.LinkedList; import java.util.List; +import java.util.Optional; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.codehaus.mojo.versions.api.AbstractVersionDetails; @@ -39,7 +40,7 @@ *
  • 3.1.0
  • *
  • 3.3.0
  • * - * The {@link AbstractVersionDetails#getNewerVersions(String, int, boolean)} will use an upper version of + * The {@link AbstractVersionDetails#getNewerVersions(String, Optional, boolean, boolean)} will use an upper version of * 2.1.0 to limit the versions to use. The result of this would be using 2.1.0-M1 which * contradicts the wish of the user of not updating the minor version. The root cause of this is the comparison of Maven * versions which will defined 2.1.0-M1 as less than 2.1.0. The method diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java index b0ee00c9cc..db6444ff49 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java @@ -21,6 +21,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.plexus.util.StringUtils; /** @@ -29,8 +30,7 @@ * @author Stephen Connolly * @since 1.0-alpha-3 */ -public class MavenVersionComparator - extends AbstractVersionComparator +public class MavenVersionComparator extends AbstractVersionComparator { /** @@ -92,15 +92,10 @@ protected int innerGetSegmentCount( ArtifactVersion v ) /** * {@inheritDoc} */ - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { - int segmentCount = innerGetSegmentCount( v ); - if ( segment < 0 || segment >= segmentCount ) - { - throw new InvalidSegmentException( segment, segmentCount, v ); - } String version = v.toString(); - if ( segmentCount == 1 ) + if ( innerGetSegmentCount( v ) == 1 ) { // only the qualifier version = VersionComparators.alphaNumIncrement( version ); @@ -124,14 +119,14 @@ protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment switch ( segment ) { - case 0: + case MAJOR: major++; minor = 0; incremental = 0; build = 0; qualifier = null; break; - case 1: + case MINOR: minor++; incremental = 0; build = 0; @@ -140,12 +135,12 @@ protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment qualifier = "SNAPSHOT"; } break; - case 2: + case INCREMENTAL: incremental++; build = 0; qualifier = null; break; - case 3: + case SUBINCREMENTAL: if ( haveQualifier ) { qualifier = qualifierIncrement( qualifier ); diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java index 335538a0c7..d633e39b24 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java @@ -24,6 +24,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; /** * A comparator which uses Mercury's version rules. @@ -52,18 +53,13 @@ protected int innerGetSegmentCount( ArtifactVersion v ) return tok.countTokens(); } - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { - final int segmentCount = getSegmentCount( v ); - if ( segment < 0 || segment > segmentCount ) - { - throw new InvalidSegmentException( segment, segmentCount, v ); - } final String version = v.toString(); StringBuilder result = new StringBuilder( version.length() + 10 ); StringTokenizer tok = new StringTokenizer( version, ".-" ); int index = 0; - while ( tok.hasMoreTokens() && segment > 0 ) + while ( tok.hasMoreTokens() && segment.value() > 0 ) { String token = tok.nextToken(); result.append( token ); @@ -74,9 +70,9 @@ protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment result.append( version.substring( index, index + 1 ) ); index++; } - segment--; + segment = Segment.of( segment.value() - 1 ); } - if ( segment == 0 ) + if ( segment.value() == 0 ) { if ( tok.hasMoreTokens() ) { diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java index 85443c2767..70a38f3011 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java @@ -24,6 +24,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; /** * A comparator which will compare all segments of a dot separated version string as numbers if possible, i.e. 1.3.34 @@ -163,18 +164,13 @@ protected int innerGetSegmentCount( ArtifactVersion v ) * {@inheritDoc} */ @SuppressWarnings( "checkstyle:MethodLength" ) - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { - final int segmentCount = innerGetSegmentCount( v ); - if ( segment < 0 || segment > segmentCount ) - { - throw new InvalidSegmentException( segment, segmentCount, v ); - } final String version = v.toString(); StringBuilder buf = new StringBuilder(); StringTokenizer tok = new StringTokenizer( version, "." ); boolean first = true; - while ( segment >= 0 && tok.hasMoreTokens() ) + for ( int segmentIdx = segment.value(); segmentIdx >= 0 && tok.hasMoreTokens(); --segmentIdx ) { if ( first ) { @@ -193,7 +189,7 @@ protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment p = p.substring( 0, index ); } - if ( segment == 0 ) + if ( segmentIdx == 0 ) { try { @@ -317,7 +313,6 @@ else if ( "ga".equalsIgnoreCase( p ) || "final".equalsIgnoreCase( p ) ) buf.append( '-' ); buf.append( q ); } - segment--; } while ( tok.hasMoreTokens() ) { diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java index 11f2af07f3..08d3973a78 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java @@ -22,6 +22,7 @@ import java.util.Comparator; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; /** * A rule for comparing and manipulating versions. @@ -47,5 +48,5 @@ public interface VersionComparator * @since 1.0-beta-1 * @throws InvalidSegmentException if {@code segment} ∉ [0, segmentCount) */ - ArtifactVersion incrementSegment( ArtifactVersion artifactVersion, int segment ) throws InvalidSegmentException; + ArtifactVersion incrementSegment( ArtifactVersion artifactVersion, Segment segment ) throws InvalidSegmentException; } diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java index d691557152..4aed2cf78b 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java @@ -20,8 +20,12 @@ */ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; import org.junit.Test; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; import static org.junit.Assert.assertEquals; public class MavenVersionComparatorTest @@ -44,21 +48,21 @@ public void testSegmentCounting() public void testSegmentIncrementing() throws InvalidSegmentException { - assertIncrement( "6", "5", 0 ); - assertIncrement( "6.0", "5.0", 0 ); - assertIncrement( "5.1", "5.0", 1 ); - assertIncrement( "5.1.0", "5.0.1", 1 ); - assertIncrement( "5.alpha.2", "5.alpha.1", 0 ); - assertIncrement( "5.alpha-1.2", "5.alpha-1.1", 0 ); - assertIncrement( "5.alpha-1.ba", "5.alpha-1.az", 0 ); - assertIncrement( "5.alpha-wins.2", "5.alpha-wins.1", 0 ); - assertIncrement( "1.0-alpha-3-SNAPSHOT", "1.0-alpha-2-SNAPSHOT", 3 ); - assertIncrement( "1.0-alpha-90-SNAPSHOT", "1.0-alpha-9-SNAPSHOT", 3 ); - assertIncrement( "1.0-za-SNAPSHOT", "1.0-z-SNAPSHOT", 3 ); - assertIncrement( "1.0-z90-SNAPSHOT", "1.0-z9-SNAPSHOT", 3 ); + assertIncrement( "6", "5", MAJOR ); + assertIncrement( "6.0", "5.0", MAJOR ); + assertIncrement( "5.1", "5.0", MINOR ); + assertIncrement( "5.1.0", "5.0.1", MINOR ); + assertIncrement( "5.alpha.2", "5.alpha.1", MAJOR ); + assertIncrement( "5.alpha-1.2", "5.alpha-1.1", MAJOR ); + assertIncrement( "5.alpha-1.ba", "5.alpha-1.az", MAJOR ); + assertIncrement( "5.alpha-wins.2", "5.alpha-wins.1", MAJOR ); + assertIncrement( "1.0-alpha-3-SNAPSHOT", "1.0-alpha-2-SNAPSHOT", SUBINCREMENTAL ); + assertIncrement( "1.0-alpha-90-SNAPSHOT", "1.0-alpha-9-SNAPSHOT", SUBINCREMENTAL ); + assertIncrement( "1.0-za-SNAPSHOT", "1.0-z-SNAPSHOT", SUBINCREMENTAL ); + assertIncrement( "1.0-z90-SNAPSHOT", "1.0-z9-SNAPSHOT", SUBINCREMENTAL ); } - private void assertIncrement( String expected, String initial, int segment ) throws InvalidSegmentException + private void assertIncrement( String expected, String initial, Segment segment ) throws InvalidSegmentException { assertEquals( expected, instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString() ); diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java index deeb70c8ea..a9dcfe8680 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java @@ -22,6 +22,9 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.junit.Test; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; import static org.junit.Assert.assertEquals; public class MercuryVersionComparatorTest @@ -30,7 +33,7 @@ public class MercuryVersionComparatorTest @Test public void testSegmentCounting() - throws Exception + throws Exception { assertEquals( 1, instance.getSegmentCount( new DefaultArtifactVersion( "5" ) ) ); assertEquals( 2, instance.getSegmentCount( new DefaultArtifactVersion( "5.0" ) ) ); @@ -42,23 +45,23 @@ public void testSegmentCounting() @Test public void testSegmentIncrementing() - throws Exception + throws Exception { assertEquals( new DefaultArtifactVersion( "6" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5" ), 0 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); assertEquals( new DefaultArtifactVersion( "6.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), 0 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.1" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.1.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.beta.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.beta-0.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.alpha-2.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), 2 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), INCREMENTAL ).toString() ); assertEquals( new DefaultArtifactVersion( "5.beta-0.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-wins.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-wins.1" ), MINOR ).toString() ); } } \ No newline at end of file diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java index fc0ac1498d..e7a58949be 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java @@ -22,6 +22,9 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.junit.Test; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -104,20 +107,21 @@ public void testSegmentIncrementing() throws Exception { assertEquals( new DefaultArtifactVersion( "6" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5" ), 0 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); assertEquals( new DefaultArtifactVersion( "6.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), 0 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.1" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.1.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.beta.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.alpha-2.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), MINOR ).toString() ); assertEquals( new DefaultArtifactVersion( "5.alpha-1.2" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), 2 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), INCREMENTAL ) + .toString() ); assertEquals( new DefaultArtifactVersion( "5.beta.0" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-wins.1" ), 1 ).toString() ); + instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-wins.1" ), MINOR ).toString() ); } } diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java index 06bdbf7f3d..d433d7fc0e 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java @@ -21,6 +21,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; import org.junit.Test; import static org.junit.Assert.assertTrue; @@ -70,7 +71,7 @@ public void assertLater( String version, VersionComparator instance ) throws Inv int count = instance.getSegmentCount( v1 ); for ( int i = 0; i < count; i++ ) { - ArtifactVersion v2 = instance.incrementSegment( v1, i ); + ArtifactVersion v2 = instance.incrementSegment( v1, Segment.of( i ) ); assertTrue( v1 + " < " + v2, instance.compare( v1, v2 ) < 0 ); } }