diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java index 8d1ddbc3a3..89fa4fc2dd 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java @@ -60,7 +60,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo /** * A comma separated list of artifact patterns to include. Follows the pattern - * "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of includes from the command + * "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of includes from the command * line. When specifying includes from the pom, use the {@link #includes} configuration instead. If this property is * specified then the {@link # include} configuration is ignored. * @@ -71,7 +71,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo /** * A comma separated list of artifact patterns to exclude. Follows the pattern - * "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of excludes from the command + * "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of excludes from the command * line. When specifying excludes from the pom, use the {@link #excludes} configuration instead. If this property is * specified then the {@link # exclude} configuration is ignored. * diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReportRenderer.java index 7769801caf..8e5cdb1660 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReportRenderer.java @@ -780,40 +780,23 @@ private Set getVersionsInRange( Property property, PropertyVersions vers protected String getLabel( ArtifactVersion version, AbstractVersionDetails versions ) { - String label = null; - if ( equals( version, versions.getNewestUpdate( of( MAJOR ) ) ) ) - { - label = getText( "report.latestMajor" ); - } - else if ( equals( version, versions.getOldestUpdate( of( MAJOR ) ) ) ) - { - label = getText( "report.nextMajor" ); - } - else if ( equals( version, versions.getNewestUpdate( of( MINOR ) ) ) ) - { - label = getText( "report.latestMinor" ); - } - else if ( equals( version, versions.getOldestUpdate( of( MINOR ) ) ) ) - { - label = getText( "report.nextMinor" ); - } - else if ( equals( version, versions.getNewestUpdate( of( INCREMENTAL ) ) ) ) - { - label = getText( "report.latestIncremental" ); - } - else if ( equals( version, versions.getOldestUpdate( of( INCREMENTAL ) ) ) ) - { - label = getText( "report.nextIncremental" ); - } - else if ( equals( version, versions.getNewestUpdate( of( SUBINCREMENTAL ) ) ) ) - { - label = getText( "report.latestSubIncremental" ); - } - else if ( equals( version, versions.getOldestUpdate( of( SUBINCREMENTAL ) ) ) ) - { - label = getText( "report.nextVersion" ); - } - return label; + return equals( version, versions.getNewestUpdate( of( SUBINCREMENTAL ) ) ) + ? getText( "report.latestSubIncremental" ) + : equals( version, versions.getOldestUpdate( of( SUBINCREMENTAL ) ) ) + ? getText( "report.nextVersion" ) + : equals( version, versions.getOldestUpdate( of( INCREMENTAL ) ) ) + ? getText( "report.nextIncremental" ) + : equals( version, versions.getNewestUpdate( of( INCREMENTAL ) ) ) + ? getText( "report.latestIncremental" ) + : equals( version, versions.getOldestUpdate( of( MINOR ) ) ) + ? getText( "report.nextMinor" ) + : equals( version, versions.getNewestUpdate( of( MINOR ) ) ) + ? getText( "report.latestMinor" ) + : equals( version, versions.getOldestUpdate( of( MAJOR ) ) ) + ? getText( "report.nextMajor" ) + : equals( version, versions.getNewestUpdate( of( MAJOR ) ) ) + ? getText( "report.latestMajor" ) + : ""; } } diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 9b9b4dca77..15e7449670 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -356,6 +356,7 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver * @param versionRange The version range. * @param allowingSnapshots null for no override, otherwise the local override to apply. * @param usePluginRepositories Use plugin repositories + * @param allowDowngrade whether downgrades should be allowed * @return The latest version of the specified artifact that matches the specified version range or * null if no matching version could be found. * @throws ArtifactMetadataRetrievalException If the artifact metadata could not be found. @@ -515,6 +516,7 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A * @param artifact The artifact. * @param currentVersion The current version of the artifact. * @param updateVersion The proposed new version of the artifact. + * @param forceUpdate if true, LATEST and RELEASE versions will be overwritten with the real version * @return true if the update should be applied to the pom. * @since 2.9 */ @@ -575,7 +577,7 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A * @param allowMajorUpdates Allow major updates * @param allowMinorUpdates Allow minor updates * @param allowIncrementalUpdates Allow incremental updates - * @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1. + * @return Returns the segment (0-based) that is unchangeable. If any segment can change, returns -1. */ protected Optional determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, boolean allowIncrementalUpdates ) diff --git a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReport.java b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReport.java index 4f16f7a43c..80b66a0607 100644 --- a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReport.java +++ b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReport.java @@ -65,7 +65,7 @@ public class DependencyUpdatesReport extends AbstractVersionsReport protected boolean processDependencyManagement; /** - * Whether to process the depdendencyManagement part transitive or not. + * Whether to process the dependencyManagement part transitive or not. * In case of <type>pom</type>and * <scope>import</scope> this means * by default to report also the imported dependencies. diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java index 22439e28c8..92b0a39c1a 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java @@ -94,7 +94,7 @@ public class DisplayDependencyUpdatesMojo private boolean processDependencyManagement; /** - * Whether to process the depdendencyManagement part transitive or not. + * Whether to process the dependencyManagement part transitive or not. * In case of <type>pom</type>and * <scope>import</scope> this means * by default to report also the imported dependencies. @@ -691,12 +691,19 @@ private DependencyManagement getProjectDependencyManagement( MavenProject projec private Optional calculateUpdateScope() { - return !allowAnyUpdates - ? allowMajorUpdates ? of( MAJOR ) - : allowMinorUpdates ? of( MINOR ) - : allowIncrementalUpdates ? of( INCREMENTAL ) - : empty() - : empty(); + if ( !allowIncrementalUpdates && !allowMinorUpdates && !allowMajorUpdates && !allowAnyUpdates ) + { + throw new IllegalArgumentException( "One of: allowAnyUpdates, allowMajorUpdates, allowMinorUpdates, " + + "allowIncrementalUpdates must be true" ); + } + + return allowAnyUpdates && allowMajorUpdates && allowMinorUpdates + ? empty() + : allowMajorUpdates && allowMinorUpdates + ? of( MAJOR ) + : allowMinorUpdates + ? of( MINOR ) + : of( INCREMENTAL ); } private void logUpdates( Map updates, String section ) diff --git a/src/main/java/org/codehaus/mojo/versions/SetMojo.java b/src/main/java/org/codehaus/mojo/versions/SetMojo.java index 56205ecf8f..0305a2968c 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetMojo.java @@ -402,9 +402,10 @@ public void execute() throws MojoExecutionException, MojoFailureException /** * Returns the incremented version, with the nextSnapshotIndexToIncrement indicating the 1-based index, - * conunting from the left, or the most major version component, of the version string. + * from the left, or the most major version component, of the version string. * * @param version input version + * @param nextSnapshotIndexToIncrement 1-based segment number to be incremented * @return version with the incremented index specified by nextSnapshotIndexToIncrement or last index * @throws MojoExecutionException thrown if the input parameters are invalid */ 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 1d03bcdc2c..2eac6fe7db 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -31,11 +31,13 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; +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 java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; /** * Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}. @@ -171,7 +173,6 @@ private static Iterable reverse( T[] array ) public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction, boolean includeSnapshots, boolean allowDowngrade ) { - final VersionComparator versionComparator = getVersionComparator(); // reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest, // so we only need to find the first candidate fulfilling the criteria for ( ArtifactVersion candidate : reverse( getVersions( includeSnapshots ) ) ) @@ -260,13 +261,22 @@ public final ArtifactVersion[] getNewerVersions( String versionString, Optional< throws InvalidSegmentException { ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); - ArtifactVersion lowerBound = - allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion; - ArtifactVersion upperBound = !upperBoundSegment.isPresent() ? null - : getVersionComparator().incrementSegment( lowerBound, upperBoundSegment.get() ); + ArtifactVersion lowerBound = allowDowngrade + ? getLowerBound( currentVersion, upperBoundSegment ) + .map( DefaultArtifactVersion::new ) + .orElse( null ) + : currentVersion; + ArtifactVersion upperBound = + !upperBoundSegment.isPresent() + ? null + : upperBoundSegment + .map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion, + s.isMajorTo( SUBINCREMENTAL ) + ? Segment.of( s.value() + 1 ) + : s ) ) + .orElse( null ); Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade ); - // TODO shouldn't allowDowngrade boolean be passed to this call ? return getVersions( restriction, includeSnapshots ); } @@ -346,7 +356,8 @@ public final ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, Op { try { - return getOldestVersion( restrictionFor( currentVersion, updateScope ), includeSnapshots ); + return getOldestVersion( getVersionComparator().restrictionFor( currentVersion, updateScope ), + includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -359,7 +370,8 @@ public final ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, Op { try { - return getNewestVersion( restrictionFor( currentVersion, updateScope ), includeSnapshots ); + return getNewestVersion( getVersionComparator().restrictionFor( currentVersion, updateScope ), + includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -372,7 +384,8 @@ public final ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Op { try { - return getVersions( restrictionFor( currentVersion, updateScope ), includeSnapshots ); + return getVersions( getVersionComparator().restrictionFor( currentVersion, updateScope ), + includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -433,32 +446,12 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu return getVersions( versionRange, restriction, includeSnapshots ); } - /** - * Returns the lower bound version based on the given artifact version - * and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed, - * 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 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, - Optional unchangedSegment ) - throws InvalidSegmentException - { - Optional lowerBound = getLowerBound( version, unchangedSegment ); - return lowerBound.map( DefaultArtifactVersion::new ).orElse( null ); - } - /** * Returns the string designation of the lower bound version based on the given artifact version * and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed, * 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 version {@link ArtifactVersion} object specifying the version for which the lower bound is being computed * @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 @@ -527,26 +520,4 @@ private boolean isVersionInRestriction( Restriction restriction, ArtifactVersion } return ( includeLower || lower != 0 ) && ( includeUpper || upper != 0 ); } - - /** - * Helper method to get the artifact boundaries for computing updates - * - * @param currentVersion The current version. - * @param scope scope of the restriction or Optional.empty() for no restriction - * @return {@linkplain Restriction} object based on the arguments - * @throws InvalidSegmentException if {@code segment} ∉ [0, segmentCount) - */ - protected Restriction restrictionFor( ArtifactVersion currentVersion, Optional scope ) - throws InvalidSegmentException - { - VersionComparator versionComparator = getVersionComparator(); - ArtifactVersion lowerBound = scope.isPresent() && scope.get().value() < Segment.SUBINCREMENTAL.value() - ? versionComparator.incrementSegment( currentVersion, scope.get() ) - : currentVersion; - ArtifactVersion upperBound = scope.isPresent() && scope.get().value() > Segment.MAJOR.value() - ? 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/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index 74a344b169..8c66929c67 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -1476,9 +1476,10 @@ public static String getGroupId( Model model ) * Finds the local root of the specified project. * * @param project The project to find the local root for. + * @param builder {@linkplain MavenProjectBuilder} object * @param localRepository the local repo. * @param globalProfileManager the global profile manager. - * @param logger The logger to log to. + * @param logger The logger to log tog * @return The local root (note this may be the project passed as an argument). */ public static MavenProject getLocalRoot( MavenProjectBuilder builder, MavenProject project, 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 c329898914..2c4c910631 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/Segment.java +++ b/src/main/java/org/codehaus/mojo/versions/api/Segment.java @@ -26,14 +26,7 @@ */ public enum Segment implements Comparable { - MAJOR( 0 ), MINOR( 1 ), INCREMENTAL( 2 ), SUBINCREMENTAL( 3 ); - - private final int index; - - Segment( int index ) - { - this.index = index; - } + MAJOR, MINOR, INCREMENTAL, SUBINCREMENTAL; /** * Returns the 0-based sendex index @@ -42,7 +35,7 @@ public enum Segment implements Comparable */ public int value() { - return index; + return ordinal(); } public static Segment of( int index ) @@ -58,6 +51,16 @@ public static Segment of( int index ) } } + /** + * Returns true if the given segment is more major than the other + * @param other other segment to compare + * @return true if the given segment is more major + */ + public boolean isMajorTo( Segment other ) + { + return value() < other.value(); + } + @Override public String toString() { diff --git a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java index 26e63e4fc1..60b48fe1a9 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java @@ -221,6 +221,7 @@ ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion up * @param includeSnapshots true if snapshots are to be included. * @return the oldest version after currentVersion within the specified update scope or null if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, Optional updateScope, @@ -235,6 +236,7 @@ ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, Optionaltrue if snapshots are to be included. * @return the newest version after currentVersion within the specified update scope or null if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, Optional updateScope, @@ -247,6 +249,7 @@ ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, Optionaltrue if snapshots are to be included. * @return the all versions after currentVersion within the specified update scope. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optional updateScope, @@ -295,6 +298,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optionalnull if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getOldestUpdate( Optional updateScope ) throws InvalidSegmentException; @@ -306,6 +310,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optionalnull if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getNewestUpdate( Optional updateScope ) throws InvalidSegmentException; @@ -315,6 +320,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optional updateScope ) throws InvalidSegmentException; @@ -327,6 +333,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Optionaltrue if snapshots are to be included. * @return the oldest version after currentVersion within the specified update scope or null if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getOldestUpdate( Optional updateScope, boolean includeSnapshots ) @@ -340,6 +347,7 @@ ArtifactVersion getOldestUpdate( Optional updateScope, boolean includeS * @param includeSnapshots true if snapshots are to be included. * @return the newest version after currentVersion within the specified update scope or null if no * version is available. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion getNewestUpdate( Optional updateScope, boolean includeSnapshots ) @@ -351,6 +359,7 @@ ArtifactVersion getNewestUpdate( Optional updateScope, boolean includeS * @param updateScope the update scope to include. * @param includeSnapshots true if snapshots are to be included. * @return the all versions after currentVersion within the specified update scope. + * @throws InvalidSegmentException thrown if the updateScope is greater than the number of segments * @since 1.0-beta-1 */ ArtifactVersion[] getAllUpdates( Optional updateScope, boolean includeSnapshots ) 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 773914180a..1baeea0a35 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java @@ -59,8 +59,17 @@ public final int getSegmentCount( ArtifactVersion v ) */ public final ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException { - return VersionComparators.copySnapshot( v, innerIncrementSegment( VersionComparators.stripSnapshot( v ), - segment ) ); + 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, Segment segment ) diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java b/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java new file mode 100644 index 0000000000..2f923668e6 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java @@ -0,0 +1,93 @@ +package org.codehaus.mojo.versions.ordering; + +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.api.Segment; + +import static org.codehaus.mojo.versions.ordering.ComparableVersion.IntegerItem.ZERO; + +/** + *

Represents an artifact version with all segments more major or equal to a given segment + * held in place. It can be thought of as an artifact having +∞ as its upper bound + * on all segments less major than the held segment.

+ *

When compared with another artifact versions, this results with the other object + * with the segment versions up to the held segment being equal, + * always comparing lower than this object.

+ *

This is particularly helpful for -SNAPSHOT and other versions with qualifiers, which + * are lower than version 0 in the Maven versioning system.

+ */ +public class BoundArtifactVersion extends DefaultArtifactVersion +{ + /** + * Most major segment that can change, i.e. not held in place. + * All segments that are more major than this one are held in place. + */ + private final Segment segment; + + private final BoundComparableVersion comparator; + + /** + * Constructs the instance + * @param artifactVersion artifact version containing the segment version values + * @param segment most major segment that can change, i.e. not held in place + */ + public BoundArtifactVersion( ArtifactVersion artifactVersion, Segment segment ) + { + super( artifactVersion.toString() ); + this.segment = segment; + this.comparator = new BoundComparableVersion( this ); + } + + /** + * Returns the most major segment that can change. + * All segments that are more major than this one are held in place. + * @return segment that can change + */ + public Segment getSegment() + { + return segment; + } + + @Override + public int compareTo( ArtifactVersion other ) + { + if ( other == null ) + { + return -1; + } + + return comparator.compareTo( new ComparableVersion( other.toString() ) ); + } + + protected static class BoundComparableVersion extends ComparableVersion + { + private BoundArtifactVersion artifactVersion; + protected BoundComparableVersion( BoundArtifactVersion artifactVersion ) + { + super( artifactVersion.toString() ); + this.artifactVersion = artifactVersion; + } + + @Override + public int compareTo( ComparableVersion o ) + { + return compareTo( ( (List) items ).iterator(), + ( (Iterable) o.items ).iterator(), artifactVersion.segment.value() ); + + } + + @SuppressWarnings( "checkstyle:InnerAssignment" ) + private int compareTo( Iterator left, Iterator right, int comparisonLimit ) + { + int r; + return !( comparisonLimit > 0 && right.hasNext() ) + ? 1 + : ( r = -( right.next().compareTo( left.hasNext() ? left.next() : ZERO ) ) ) != 0 + ? r + : compareTo( left, right, comparisonLimit - 1 ); + } + } +} diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java b/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java index 9cb107f6a3..55327cfaf3 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java @@ -43,9 +43,9 @@ public class ComparableVersion private String canonical; - private ListItem items; + protected ListItem items; - private interface Item + protected interface Item { int INTEGER_ITEM = 0; @@ -63,10 +63,10 @@ private interface Item /** * Represents a numeric item in the version item list. */ - private static class IntegerItem + protected static class IntegerItem implements Item { - private static final BigInteger BIG_INTEGEGER_ZERO = new BigInteger( "0" ); + private static final BigInteger BIG_INTEGER_ZERO = new BigInteger( "0" ); private final BigInteger value; @@ -74,7 +74,7 @@ private static class IntegerItem private IntegerItem() { - this.value = BIG_INTEGEGER_ZERO; + this.value = BIG_INTEGER_ZERO; } IntegerItem( String str ) @@ -89,14 +89,14 @@ public int getType() public boolean isNull() { - return BIG_INTEGEGER_ZERO.equals( value ); + return BIG_INTEGER_ZERO.equals( value ); } public int compareTo( Item item ) { if ( item == null ) { - return BIG_INTEGEGER_ZERO.equals( value ) ? 0 : 1; // 1.0 == 1, 1.1 > 1 + return BIG_INTEGER_ZERO.equals( value ) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } switch ( item.getType() ) 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 c0bc1c2682..c4f5332ed9 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java @@ -38,6 +38,10 @@ public class MavenVersionComparator extends AbstractVersionComparator */ public int compare( ArtifactVersion o1, ArtifactVersion o2 ) { + if ( o1 instanceof BoundArtifactVersion ) + { + return o1.compareTo( o2 ); + } return new ComparableVersion( o1.toString() ).compareTo( new ComparableVersion( o2.toString() ) ); } 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 08d3973a78..50edfe7e89 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java @@ -20,10 +20,15 @@ */ import java.util.Comparator; +import java.util.Optional; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.Restriction; import org.codehaus.mojo.versions.api.Segment; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; + /** * A rule for comparing and manipulating versions. */ @@ -49,4 +54,30 @@ public interface VersionComparator * @throws InvalidSegmentException if {@code segment} ∉ [0, segmentCount) */ ArtifactVersion incrementSegment( ArtifactVersion artifactVersion, Segment segment ) throws InvalidSegmentException; + + /** + *

Returns a {@linkplain Restriction} object for computing version upgrades + * with the given segment allowing updates, with all more major segments locked in place.

+ *

The resulting restriction could be thought of as one + * retaining the versions on positions up to the held position, + * the position right after the position held in place will be incremented by one, + * and on all positions which are more minor than that, the range would contain -∞ + * for the bottom bound and +∞ for the above bound.

+ *

This will allow matching the required versions while not matching versions which are considered + * inferior than the zeroth version, i.e. versions with a qualifier.

+ * + * @param currentVersion The current version. + * @param scope most major segment where updates are allowed Optional.empty() for no restriction + * @return {@linkplain Restriction} object based on the arguments + */ + default Restriction restrictionFor( ArtifactVersion currentVersion, Optional scope ) + throws InvalidSegmentException + { + ArtifactVersion nextVersion = scope.isPresent() && scope.get().isMajorTo( SUBINCREMENTAL ) + ? incrementSegment( currentVersion, scope.get() ) + : currentVersion; + return new Restriction( nextVersion, nextVersion != currentVersion, scope.filter( MAJOR::isMajorTo ) + .map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion, s ) ).orElse( null ), + false ); + } } diff --git a/src/site/apt/examples/advancing-dependency-versions.apt.vm b/src/site/apt/examples/advancing-dependency-versions.apt.vm index 2db59aba9b..cf9eeb4f9f 100644 --- a/src/site/apt/examples/advancing-dependency-versions.apt.vm +++ b/src/site/apt/examples/advancing-dependency-versions.apt.vm @@ -72,13 +72,13 @@ Goal Matrix The columns in the above goal matrix are as follows: - * when scanning the <<>> for depenendency to update, include those dependencies which + * when scanning the <<>> for dependency to update, include those dependencies which are for release versions (i.e. non-SNAPSHOT versions). - * when scanning the <<>> for depenendency to update, include those dependencies which + * when scanning the <<>> for dependency to update, include those dependencies which are for -SNAPSHOT versions. - * when building the list of newer versions of a dependency, include release versions (i.e. non-SNAPSHOT + * when building the list of newer versions of a dependency, include release versions (i.e. non-SNAPSHOT versions) in the list. * when building the list of newer versions of a dependency, include -SNAPSHOT versions in the list. diff --git a/src/site/apt/examples/display-plugin-updates.apt b/src/site/apt/examples/display-plugin-updates.apt index fbecd09ddd..2ad794cf6b 100644 --- a/src/site/apt/examples/display-plugin-updates.apt +++ b/src/site/apt/examples/display-plugin-updates.apt @@ -88,7 +88,7 @@ mvn versions:display-plugin-updates The plugin will also warn if you have not specified the versions of any plugins that you are using and tell you what version you are currently using. Best practice in Maven is to always specify the plugin versions in order to - ensure that builds are reproducable. + ensure that builds are reproducible. For example the following <<>>: diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java index 12c149f223..cd99c1d250 100644 --- a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportTest.java @@ -24,9 +24,11 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Set; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.doxia.module.xhtml5.Xhtml5SinkFactory; import org.apache.maven.doxia.sink.SinkFactory; import org.apache.maven.model.Dependency; @@ -55,6 +57,7 @@ */ public class DependencyUpdatesReportTest { + private static class TestDependencyUpdatesReport extends DependencyUpdatesReport { @SuppressWarnings( "deprecation" ) @@ -128,6 +131,12 @@ public TestDependencyUpdatesReport withIgnoredVersions( this.ignoredVersions = ignoredVersions; return this; } + + public TestDependencyUpdatesReport withArtifactMetadataSource( ArtifactMetadataSource artifactMetadataSource ) + { + this.artifactMetadataSource = artifactMetadataSource; + return this; + } } private static Dependency dependencyOf( String artifactId ) @@ -223,4 +232,43 @@ public void testOnlyProjectDependenciesWithIgnoredVersions() throws IOException, String output = os.toString().replaceAll( "\n", "" ); assertThat( output, containsString( "report.noUpdatesAvailable" ) ); } + + + @Test + public void testSubincrementalUpdates() + throws IOException, MavenReportException + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + new TestDependencyUpdatesReport() + .withDependencies( DependencyBuilder.newBuilder() + .withGroupId( "localhost" ) + .withArtifactId( "dummy-api" ) + .withVersion( "1.1" ) + .withScope( SCOPE_COMPILE ) + .withType( "jar" ) + .build() ) + .withArtifactMetadataSource( mockArtifactMetadataSource( new LinkedHashMap() + {{ + put( "dummy-api", new String[] { "1.0.1", "1.0", "1.1.0-2", "1.1.1", "1.1.1-2", "1.1.2", + "1.1.2-SNAPSHOT", "1.1.3", "1.1", "1.1-SNAPSHOT", "1.2.1", "1.2.2", "1.2", "1.3", + "1.9.1-SNAPSHOT", "2.0", "2.1.1-SNAPSHOT", "2.1", "3.0", "3.1.1-SNAPSHOT", + "3.1.5-SNAPSHOT", "3.4.0-SNAPSHOT"} ); + }} ) ) + .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString() + .replaceAll( "<[^>]+>", " " ) + .replaceAll( "&[^;]+;", " " ) + .replaceAll( "\\s+", " " ); + assertThat( output, containsString( "localhost dummy-api 1.1 compile jar 1.1.0-2 1.1.3 1.3 3.0" ) ); + assertThat( output, containsString( "1.1.0-2 report.latestSubIncremental" ) ); + assertThat( output, containsString( "1.1.1 report.nextIncremental" ) ); + assertThat( output, containsString( "1.1.3 report.latestIncremental" ) ); + assertThat( output, containsString( "1.2 report.nextMinor" ) ); + assertThat( output, containsString( "1.3 report.latestMinor" ) ); + assertThat( output, containsString( "2.0 report.nextMajor" ) ); + assertThat( output, containsString( "3.0 report.latestMajor" ) ); + } + } diff --git a/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java b/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java index e7ec9d8633..6fe8c5b82d 100644 --- a/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java @@ -20,24 +20,37 @@ */ import java.io.File; +import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.ArtifactStubFactory; import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.plugin.testing.stubs.StubArtifactResolver; +import org.apache.maven.project.MavenProject; +import org.codehaus.mojo.versions.filtering.WildcardMatcher; import org.codehaus.mojo.versions.model.TestIgnoreVersions; +import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.hamcrest.Matchers; import org.junit.Rule; import org.junit.Test; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; import static org.codehaus.mojo.versions.model.TestIgnoreVersions.TYPE_REGEX; import static org.codehaus.mojo.versions.model.TestIgnoreVersions.matches; import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; +import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasItem; @@ -114,4 +127,107 @@ public void testRuleSetPresentAndWorking() throws Exception assert outputFile == null || !outputFile.exists() || outputFile.delete(); } } + + private MavenProject createProject() + { + return new MavenProject( new Model() + {{ + setGroupId( "default-group" ); + setArtifactId( "default-artifact" ); + setVersion( "1.0.0-SNAPSHOT" ); + + setDependencies( singletonList( DependencyBuilder.newBuilder() + .withGroupId( "default-group" ) + .withArtifactId( "default-dependency" ) + .withVersion( "1.0.0" ) + .build() ) ); + }} ) + {{ + setOriginalModel( getModel() ); + }}; + } + + @Test + public void testVersionsWithQualifiersNotConsideredAsMinorUpdates() + throws MojoExecutionException, MojoFailureException, IllegalAccessException, IOException + { + Path tempPath = null; + try + { + tempPath = Files.createTempFile( "display-dependency-updates", "" ); + final File tempFile = tempPath.toFile(); + new DisplayDependencyUpdatesMojo( mockRepositorySystem(), + null, mockArtifactMetadataSource( new HashMap() + {{ + put( "default-dependency", new String[] {"1.0.0", "1.1.0", "2.0.0-SNAPSHOT", "2.0.0-beta", + "2.0.0-rc1"} ); + }} ), null, + new StubArtifactResolver( new ArtifactStubFactory(), false, false ) ) + {{ + setProject( createProject() ); + setVariableValueToObject( this, "allowMinorUpdates", true ); + setVariableValueToObject( this, "processDependencies", true ); + setVariableValueToObject( this, "dependencyIncludes", + singletonList( WildcardMatcher.WILDCARD ) ); + setVariableValueToObject( this, "dependencyExcludes", emptyList() ); + this.allowSnapshots = true; + this.outputFile = tempFile; + setPluginContext( new HashMap<>() ); + }}.execute(); + + assertThat( String.join( "", Files.readAllLines( tempPath ) ), + not( anyOf( containsString( "2.0.0-SNAPSHOT" ), + containsString( "2.0.0-beta" ), + containsString( "2.0.0-rc1" ) ) ) ); + } + finally + { + if ( tempPath != null && Files.exists( tempPath ) ) + { + Files.delete( tempPath ); + } + } + } + + @Test + public void testVersionsWithQualifiersNotConsideredAsIncrementalUpdates() + throws MojoExecutionException, MojoFailureException, IllegalAccessException, IOException + { + Path tempPath = null; + try + { + tempPath = Files.createTempFile( "display-dependency-updates", "" ); + final File tempFile = tempPath.toFile(); + new DisplayDependencyUpdatesMojo( mockRepositorySystem(), + null, mockArtifactMetadataSource( new HashMap() + {{ + put( "default-dependency", new String[] {"1.0.0", "1.1.0", "1.9.0-SNAPSHOT", "1.9.0-beta", + "1.9.0-rc1"} ); + }} ), null, + new StubArtifactResolver( new ArtifactStubFactory(), false, false ) ) + {{ + setProject( createProject() ); + setVariableValueToObject( this, "allowIncrementalUpdates", true ); + setVariableValueToObject( this, "processDependencies", true ); + setVariableValueToObject( this, "dependencyIncludes", + singletonList( WildcardMatcher.WILDCARD ) ); + setVariableValueToObject( this, "dependencyExcludes", emptyList() ); + this.allowSnapshots = true; + this.outputFile = tempFile; + setPluginContext( new HashMap<>() ); + }}.execute(); + + assertThat( String.join( "", Files.readAllLines( tempPath ) ), + not( anyOf( containsString( "1.9.0-SNAPSHOT" ), + containsString( "1.9.0-beta" ), + containsString( "1.9.0-rc1" ) ) ) ); + } + finally + { + if ( tempPath != null && Files.exists( tempPath ) ) + { + Files.delete( tempPath ); + } + } + } } diff --git a/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java new file mode 100644 index 0000000000..beb93a2b89 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/UseLatestReleasesMojoTest.java @@ -0,0 +1,106 @@ +package org.codehaus.mojo.versions; + +import javax.xml.stream.XMLStreamException; + +import java.util.HashMap; + +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +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.project.MavenProject; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.api.PomHelper; +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.singletonList; +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.mockArtifactMetadataSource; +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; + +@SuppressWarnings( "deprecation" ) +public class UseLatestReleasesMojoTest +{ + private UseLatestReleasesMojo 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", null ); + } ); + + ArtifactMetadataSource artifactMetadataSourceMock = mockArtifactMetadataSource( new HashMap() + {{ + put( "dependency-artifact", new String[] {"0.9.0", "1.0.0-beta"} ); + }} ); + + mojo = new UseLatestReleasesMojo( repositorySystemMock, + null, + artifactMetadataSourceMock, + null, + null ) + {{ + MavenProject project = new MavenProject() + {{ + setModel( new Model() + {{ + setGroupId( "default-group" ); + setArtifactId( "project-artifact" ); + setVersion( "1.0.0-SNAPSHOT" ); + + setDependencies( singletonList( + DependencyBuilder.newBuilder() + .withGroupId( "default-group" ) + .withArtifactId( "dependency-artifact" ) + .withVersion( "0.9.0" ) + .withScope( SCOPE_COMPILE ) + .withType( "jar" ) + .withClassifier( "default" ) + .build() ) ); + }} ); + }}; + setProject( project ); + + changeRecorder = new TestChangeRecorder(); + setVariableValueToObject( this, "changeRecorder", changeRecorder ); + }}; + } + + @Test + public void testDontUpgradeToBeta() + throws MojoExecutionException, XMLStreamException, MojoFailureException, IllegalAccessException + { + setVariableValueToObject( mojo, "processDependencies", true ); + setVariableValueToObject( mojo, "allowSnapshots", false ); + setVariableValueToObject( mojo, "allowMajorUpdates", false ); + setVariableValueToObject( mojo, "allowMinorUpdates", true ); + setVariableValueToObject( mojo, "allowIncrementalUpdates", false ); + + try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) + { + pomHelper.when( () -> PomHelper.setDependencyVersion( any(), any(), any(), any(), any(), any() ) ) + .thenReturn( true ); + mojo.update( null ); + } + assertThat( changeRecorder.getChanges(), Matchers.empty() ); + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java b/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java index ac72d98996..1cc1d974d2 100644 --- a/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java +++ b/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java @@ -31,10 +31,14 @@ import org.hamcrest.Matchers; import org.junit.Test; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; public class ArtifactVersionsTest { @@ -105,4 +109,21 @@ private ArtifactVersion[] versions( String... versions ) } return artifactVersions; } + + @Test + public void testReportLabels() + { + ArtifactVersion[] versions = versions( "1.0.1", "1.0", "1.1.0-2", "1.1.1", "1.1.1-2", "1.1.2", + "1.1.2-SNAPSHOT", "1.1.3", "1.1", "1.1-SNAPSHOT", "1.2.1", "1.2.2", "1.2", "1.3", + "1.9.1-SNAPSHOT", "2.0", "2.1.1-SNAPSHOT", "2.1", "3.0", "3.1.1-SNAPSHOT", + "3.1.5-SNAPSHOT", "3.4.0-SNAPSHOT" ); + ArtifactVersions instance = + new ArtifactVersions( new DefaultArtifact( "default-group", "dummy-api", + "1.1", "foo", "bar", + "jar", null ), + Arrays.asList( versions ), new MavenVersionComparator() ); + + assertThat( instance.getNewestUpdate( of( SUBINCREMENTAL ) ).toString(), is( "1.1.0-2" ) ); + assertThat( instance.getOldestUpdate( of( INCREMENTAL ) ).toString(), is( "1.1.1" ) ); + } } 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 4b520f7c28..8edc29b3d1 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java @@ -19,14 +19,19 @@ * under the License. */ +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.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.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.core.Is.is; public class MavenVersionComparatorTest { @@ -36,12 +41,12 @@ public class MavenVersionComparatorTest public void testSegmentCounting() throws Exception { - assertEquals( 3, instance.getSegmentCount( new DefaultArtifactVersion( "5" ) ) ); - assertEquals( 3, instance.getSegmentCount( new DefaultArtifactVersion( "5.0" ) ) ); - assertEquals( 4, instance.getSegmentCount( new DefaultArtifactVersion( "5-0" ) ) ); - assertEquals( 1, instance.getSegmentCount( new DefaultArtifactVersion( "5.3.a" ) ) ); - assertEquals( 1, instance.getSegmentCount( new DefaultArtifactVersion( "5.0.a.1.4.5" ) ) ); - assertEquals( 3, instance.getSegmentCount( new DefaultArtifactVersion( "" ) ) ); + assertThat( 3, is( instance.getSegmentCount( new DefaultArtifactVersion( "5" ) ) ) ); + assertThat( 3, is( instance.getSegmentCount( new DefaultArtifactVersion( "5.0" ) ) ) ); + assertThat( 4, is( instance.getSegmentCount( new DefaultArtifactVersion( "5-0" ) ) ) ); + assertThat( 1, is( instance.getSegmentCount( new DefaultArtifactVersion( "5.3.a" ) ) ) ); + assertThat( 1, is( instance.getSegmentCount( new DefaultArtifactVersion( "5.0.a.1.4.5" ) ) ) ); + assertThat( 3, is( instance.getSegmentCount( new DefaultArtifactVersion( "" ) ) ) ); } @Test @@ -55,15 +60,48 @@ public void testSegmentIncrementing() throws InvalidSegmentException 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", "1.0-alpha-2-SNAPSHOT", SUBINCREMENTAL ); - assertIncrement( "1.0-alpha-90", "1.0-alpha-9-SNAPSHOT", SUBINCREMENTAL ); - assertIncrement( "1.0-za", "1.0-z-SNAPSHOT", SUBINCREMENTAL ); - assertIncrement( "1.0-z90", "1.0-z9-SNAPSHOT", SUBINCREMENTAL ); + 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, Segment segment ) throws InvalidSegmentException { - assertEquals( expected + "-SNAPSHOT", - instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString() ); + assertThat( instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString(), + is( expected ) ); + } + + @Test + public void testUpperBoundaryCustom() + { + assertThat( instance.compare( new BoundArtifactVersion( new DefaultArtifactVersion( "1.2.3" ), + INCREMENTAL ), new DefaultArtifactVersion( "1.2.3-ANDRZEJ" ) ), greaterThan( 0 ) ); + } + + @Test + public void testUpperBoundaryRelease() + { + assertThat( instance.compare( new BoundArtifactVersion( new DefaultArtifactVersion( "1.1.0" ), + INCREMENTAL ), new DefaultArtifactVersion( "1.1.0" ) ), greaterThan( 0 ) ); + } + + @Test + public void testUpperBoundarySnapshot() + { + assertThat( instance.compare( new BoundArtifactVersion( new DefaultArtifactVersion( "1.1.0" ), + INCREMENTAL ), new DefaultArtifactVersion( "1.1.0-SNAPSHOT" ) ), greaterThan( 0 ) ); + } + + @Test + public void testScopeLessThanNumSegmentsUpper() + { + ArtifactVersion artifactVersion = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + SUBINCREMENTAL ); + assertThat( artifactVersion.compareTo( new DefaultArtifactVersion( "1.0.1" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( new DefaultArtifactVersion( "1.1-SNAPSHOT" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( new DefaultArtifactVersion( "1.1" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( new DefaultArtifactVersion( "1.1.0-2" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( new DefaultArtifactVersion( "1.1.1-2" ) ), lessThan( 0 ) ); } } 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 4d64da3a24..27d75f4d17 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java @@ -20,7 +20,6 @@ */ 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.INCREMENTAL; @@ -46,19 +45,21 @@ public void testSegmentCounting() throws Exception @Test public void testSegmentIncrementing() throws Exception { - 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.beta.0", "5.alpha.1", MINOR ); - assertIncrement( "5.beta-0.0", "5.alpha-1.1", MINOR ); - assertIncrement( "5.alpha-2.0", "5.alpha-1.1", INCREMENTAL ); - assertIncrement( "5.beta-0.0", "5.alpha-wins.1", MINOR ); - } - - private void assertIncrement( String expected, String initial, Segment segment ) throws InvalidSegmentException - { - assertEquals( expected + "-SNAPSHOT", - instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString() ); + assertEquals( new DefaultArtifactVersion( "6" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "6.0" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.1" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.1.0" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.beta.0" ).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" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.alpha-2.0" ).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" ), MINOR ).toString() ); } } 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 d437aa62ca..58fb889516 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java @@ -20,7 +20,6 @@ */ 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.INCREMENTAL; @@ -106,19 +105,22 @@ public void testSegmentCounting() @Test public void testSegmentIncrementing() throws Exception { - 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.beta.0", "5.alpha.1", MINOR ); - assertIncrement( "5.alpha-2.0", "5.alpha-1.1", MINOR ); - assertIncrement( "5.alpha-1.2", "5.alpha-1.1", INCREMENTAL ); - assertIncrement( "5.beta.0", "5.alpha-wins.1", MINOR ); - } - - private void assertIncrement( String expected, String initial, Segment segment ) throws InvalidSegmentException - { - assertEquals( expected + "-SNAPSHOT", - instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString() ); + assertEquals( new DefaultArtifactVersion( "6" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "6.0" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.1" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.1.0" ).toString(), + instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.beta.0" ).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" ), MINOR ).toString() ); + assertEquals( new DefaultArtifactVersion( "5.alpha-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" ), MINOR ).toString() ); } } diff --git a/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java b/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java index efc108dd25..991a944098 100644 --- a/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java +++ b/src/test/java/org/codehaus/mojo/versions/utils/MockUtils.java @@ -32,6 +32,7 @@ import org.apache.maven.doxia.tools.SiteTool; import org.apache.maven.doxia.tools.SiteToolException; import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; import org.apache.maven.repository.RepositorySystem; import org.codehaus.plexus.i18n.I18N; @@ -124,7 +125,7 @@ public static RepositorySystem mockRepositorySystem() Dependency dependency = invocation.getArgument( 0 ); return new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(), dependency.getType(), - dependency.getClassifier(), null ); + dependency.getClassifier(), new DefaultArtifactHandlerStub( "default" ) ); } ); return repositorySystem; }