diff --git a/src/it/it-use-latest-versions-011/pom.xml b/src/it/it-use-latest-versions-011/pom.xml index 6aba0eb5e3..eadb2621c7 100644 --- a/src/it/it-use-latest-versions-011/pom.xml +++ b/src/it/it-use-latest-versions-011/pom.xml @@ -31,8 +31,6 @@ @project.version@ true - false - true false diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java index 396e6ea7ac..47057ffd46 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDisplayMojo.java @@ -93,6 +93,7 @@ protected AbstractVersionsDisplayMojo( RepositorySystem repositorySystem, super( repositorySystem, projectBuilder, artifactMetadataSource, wagonManager, artifactResolver ); } + @SuppressWarnings( "unchecked" ) protected void logInit() { if ( outputFile != null && !outputFileError ) diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index b593d5263e..8e64ab4915 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -47,14 +47,13 @@ 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.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; /** * Replaces any release versions with the latest snapshot version (if it has been deployed). @@ -191,65 +190,58 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection= 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 ); - getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); + ArtifactVersion upperBound = unchangedSegment + .map( s -> + (ArtifactVersion) new BoundArtifactVersion( lowerBound, Segment.of( s.value() + 1 ) ) ) + .orElse( null ); + + getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + ArtifactVersion[] newer = versions.getVersions( restriction, true ); + getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); - // TODO consider creating a search + filter in the Details services to get latest snapshot. - String latestVersion; - ArrayList snapshotsOnly = new ArrayList<>(); + // TODO consider creating a search + filter in the Details services to get latest snapshot. + String latestVersion; + ArrayList snapshotsOnly = new ArrayList<>(); - for ( ArtifactVersion artifactVersion : newer ) + for ( ArtifactVersion artifactVersion : newer ) + { + String newVersion = artifactVersion.toString(); + if ( matchSnapshotRegex.matcher( newVersion ).matches() ) { - String newVersion = artifactVersion.toString(); - if ( matchSnapshotRegex.matcher( newVersion ).matches() ) - { - snapshotsOnly.add( artifactVersion ); - } + snapshotsOnly.add( artifactVersion ); } - ArtifactVersion[] filteredVersions = snapshotsOnly.toArray( - new ArtifactVersion[snapshotsOnly.size()] ); - if ( filteredVersions.length > 0 ) + } + ArtifactVersion[] filteredVersions = snapshotsOnly.toArray( + new ArtifactVersion[snapshotsOnly.size()] ); + if ( filteredVersions.length > 0 ) + { + latestVersion = filteredVersions[filteredVersions.length - 1].toString(); + if ( getProject().getParent() != null ) { - latestVersion = filteredVersions[filteredVersions.length - 1].toString(); - if ( getProject().getParent() != null ) + final Artifact parentArtifact = getProject().getParentArtifact(); + if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) { - final Artifact parentArtifact = getProject().getParentArtifact(); - if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) + if ( PomHelper.setProjectParentVersion( pom, latestVersion ) ) { - if ( PomHelper.setProjectParentVersion( pom, latestVersion ) ) - { - getLog().debug( "Made parent update from " + version + " to " + latestVersion ); + getLog().debug( "Made parent update from " + version + " to " + latestVersion ); - this.getChangeRecorder() - .recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(), - parentArtifact.getArtifactId(), version, latestVersion ); - } + this.getChangeRecorder() + .recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(), + parentArtifact.getArtifactId(), version, latestVersion ); } } + } - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, - latestVersion, getProject().getModel() ) ) - { - getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion ); + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + latestVersion, getProject().getModel() ) ) + { + getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion ); - this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(), - dep.getArtifactId(), version, latestVersion ); - } + this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(), + dep.getArtifactId(), version, latestVersion ); } } - catch ( InvalidSegmentException e ) - { - getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), - dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); - } } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index a9e693a5bb..b7482490ca 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -46,13 +46,11 @@ 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.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.SegmentUtils; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; - /** * Replaces any release versions with the next snapshot version (if it has been deployed). * @@ -178,38 +176,32 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection + (ArtifactVersion) new BoundArtifactVersion( lowerBound, Segment.of( s.value() + 1 ) ) ) + .orElse( null ); + + getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + ArtifactVersion[] newer = versions.getVersions( restriction, true ); + getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); + for ( ArtifactVersion artifactVersion : newer ) { - 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 ); - getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); - for ( ArtifactVersion artifactVersion : newer ) + String newVersion = artifactVersion.toString(); + if ( matchSnapshotRegex.matcher( newVersion ).matches() ) { - String newVersion = artifactVersion.toString(); - if ( matchSnapshotRegex.matcher( newVersion ).matches() ) + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + newVersion, getProject().getModel() ) ) { - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, - newVersion, getProject().getModel() ) ) - { - getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); - - this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(), - dep.getArtifactId(), version, newVersion ); - } - break; + getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); + + this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(), + dep.getArtifactId(), version, newVersion ); } + break; } } - catch ( InvalidSegmentException e ) - { - getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), - dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); - } } } } 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 ed8134ba89..1d3f9665fd 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -233,20 +233,18 @@ public final ArtifactVersion[] getNewerVersions( String version, Optional upperBoundSegment, + public final ArtifactVersion[] getNewerVersions( String versionString, Optional unchangedSegment, boolean includeSnapshots, boolean allowDowngrade ) throws InvalidSegmentException { ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); ArtifactVersion lowerBound = allowDowngrade - ? getLowerBound( currentVersion, upperBoundSegment ) + ? getLowerBound( currentVersion, unchangedSegment ) .map( DefaultArtifactVersion::new ) .orElse( null ) : currentVersion; ArtifactVersion upperBound = - !upperBoundSegment.isPresent() - ? null - : upperBoundSegment + unchangedSegment .map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion, s.isMajorTo( SUBINCREMENTAL ) ? Segment.of( s.value() + 1 ) diff --git a/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java b/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java index db28d1e990..ec624f677b 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java @@ -23,6 +23,8 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.versioning.ArtifactVersion; @@ -112,6 +114,38 @@ public int compareTo( ArtifactVersions that ) : compare( getVersion(), that.getVersion() ); } + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + + if ( !( o instanceof ArtifactVersions ) ) + { + return false; + } + + ArtifactVersions that = (ArtifactVersions) o; + + return new EqualsBuilder() + .append( getArtifact(), that.getArtifact() ) + .append( getVersions(), that.getVersions() ) + .append( getVersionComparator(), that.getVersionComparator() ) + .isEquals(); + } + + @Override + public int hashCode() + { + return new HashCodeBuilder( 17, 37 ) + .append( getArtifact() ) + .append( getVersions() ) + .append( getVersionComparator() ) + .toHashCode(); + } + /** * Checks if the version is in the range (and ensures that the range respects the -! syntax to rule out * any qualifiers from range boundaries). 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 9b25e60462..fb4867604d 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -432,21 +432,6 @@ else if ( getVersionComparator().compare( result, fromReactor ) < 0 ) return result; } - private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper helper, - Optional unchangedSegment, - boolean includeSnapshots, VersionRange range ) - throws InvalidSegmentException - { - ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion ); - ArtifactVersion upperBound = null; - if ( unchangedSegment.isPresent() ) - { - 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 { public int compare( ArtifactVersion v1, ArtifactVersion v2 ) @@ -507,33 +492,5 @@ public int getSegmentCount( ArtifactVersion v ) } return result; } - - public ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException - { - if ( !isAssociated() ) - { - throw new IllegalStateException( "Cannot compare versions for a property with no associations" ); - } - VersionComparator[] comparators = lookupComparators(); - assert comparators.length >= 1 : "we have at least one association => at least one comparator"; - ArtifactVersion result = comparators[0].incrementSegment( v, segment ); - for ( int i = 1; i < comparators.length; i++ ) - { - ArtifactVersion alt = comparators[i].incrementSegment( v, segment ); - if ( !result.toString().equals( alt.toString() ) ) - { - throw new IllegalStateException( - "Property " + name + " is associated with multiple artifacts" - + " and these artifacts use different version sorting rules and these rules are effectively" - + " incompatible for the two of versions being compared.\n" - + "First rule says incrementSegment(\"" - + v + "\", " + segment + ") = " + result - + "\nSecond rule says incrementSegment(\"" + v + "\", " - + segment + ") = " + alt ); - } - } - return result; - } - } } 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 ecc87f1a39..30a4835026 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java @@ -20,7 +20,6 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.codehaus.mojo.versions.api.Segment; /** * Base class for version comparators. @@ -54,18 +53,6 @@ public final int getSegmentCount( ArtifactVersion v ) protected abstract int innerGetSegmentCount( ArtifactVersion v ); - /** - * {@inheritDoc} - */ - public final ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException - { - return VersionComparators.copySnapshot( v, innerIncrementSegment( VersionComparators.stripSnapshot( v ), - segment ) ); - } - - protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment ) - throws InvalidSegmentException; - /** * Returns a hash code value for the comparator class. * diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java b/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java index 2f923668e6..49b043e82e 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java @@ -3,6 +3,8 @@ import java.util.Iterator; import java.util.List; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.api.Segment; @@ -26,7 +28,6 @@ public class BoundArtifactVersion extends DefaultArtifactVersion * All segments that are more major than this one are held in place. */ private final Segment segment; - private final BoundComparableVersion comparator; /** @@ -62,9 +63,42 @@ public int compareTo( ArtifactVersion other ) return comparator.compareTo( new ComparableVersion( other.toString() ) ); } + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + + if ( !( o instanceof BoundArtifactVersion ) ) + { + return false; + } + + BoundArtifactVersion that = (BoundArtifactVersion) o; + + return new EqualsBuilder() + .appendSuper( super.equals( o ) ) + .append( getSegment(), that.getSegment() ) + .append( comparator, that.comparator ) + .isEquals(); + } + + @Override + public int hashCode() + { + return new HashCodeBuilder( 17, 37 ) + .appendSuper( super.hashCode() ) + .append( getSegment() ) + .append( comparator ) + .toHashCode(); + } + protected static class BoundComparableVersion extends ComparableVersion { private BoundArtifactVersion artifactVersion; + protected BoundComparableVersion( BoundArtifactVersion artifactVersion ) { super( artifactVersion.toString() ); @@ -74,20 +108,43 @@ protected BoundComparableVersion( BoundArtifactVersion artifactVersion ) @Override public int compareTo( ComparableVersion o ) { + // all segments more or equally major than artifactVersion.segment can change return compareTo( ( (List) items ).iterator(), ( (Iterable) o.items ).iterator(), artifactVersion.segment.value() ); } - @SuppressWarnings( "checkstyle:InnerAssignment" ) - private int compareTo( Iterator left, Iterator right, int comparisonLimit ) + private int compareTo( Iterator left, Iterator right, int comparisonsLeft ) + { + if ( comparisonsLeft <= 0 ) + { + // always greater than the other version if all more major segments are equal + return 1; + } + + int result = left.hasNext() && right.hasNext() + ? integerItemOrZero( left.next() ).compareTo( right.next() ) + : left.hasNext() || right.hasNext() + ? compareToZero( left, right ) + : 1; + + return result != 0 + ? result + : compareTo( left, right, comparisonsLeft - 1 ); + } + + private static int compareToZero( Iterator left, Iterator right ) + { + return left.hasNext() + ? integerItemOrZero( left.next() ).compareTo( ZERO ) + : -right.next().compareTo( ZERO ); + } + + private static Item integerItemOrZero( Item item ) { - int r; - return !( comparisonLimit > 0 && right.hasNext() ) - ? 1 - : ( r = -( right.next().compareTo( left.hasNext() ? left.next() : ZERO ) ) ) != 0 - ? r - : compareTo( left, right, comparisonLimit - 1 ); + return item instanceof IntegerItem + ? item + : ZERO; } } } 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 50edfe7e89..91a40848c8 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java @@ -44,17 +44,6 @@ public interface VersionComparator */ int getSegmentCount( ArtifactVersion artifactVersion ); - /** - * Increment the specified segment of the supplied version. - * - * @param artifactVersion The artifact version to increment. - * @param segment The segment number to increment. - * @return An artifact version with the specified segment incremented. - * @since 1.0-beta-1 - * @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.

@@ -73,10 +62,11 @@ public interface VersionComparator 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 ) + ArtifactVersion nextVersion = scope.filter( s -> s.isMajorTo( SUBINCREMENTAL ) ) + .map( s -> (ArtifactVersion) + new BoundArtifactVersion( currentVersion, Segment.of( s.value() + 1 ) ) ) + .orElse( currentVersion ); + return new Restriction( nextVersion, false, scope.filter( MAJOR::isMajorTo ) .map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion, s ) ).orElse( null ), false ); } diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java index 3b31c751f4..37850e7dc9 100644 --- a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java @@ -343,4 +343,28 @@ public void testWrongReportBounds() throws IOException, MavenReportException matchesPattern( ".*report.overview.numNewerMajorAvailable\\s*1.*" ), matchesPattern( ".*report.overview.numUpToDate\\s*0.*" ) ) ); } + + @Test + public void testIt001Overview() throws IOException, MavenReportException + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + new TestDependencyUpdatesReportMojo() + .withOnlyUpgradable( true ) + .withDependencies( + dependencyOf( "test-artifact", "1.1" ) ) + .withArtifactMetadataSource( mockArtifactMetadataSource( new HashMap() + {{ + put( "test-artifact", new String[] { "1.1.0-2", "1.1", "1.1.1", "1.1.1-2", + "1.1.2", "1.1.3", "1.2", "1.2.1", "1.2.2", "1.3", "2.0", "2.1", "3.0"} ); + }} ) ) + .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString() + .replaceAll( "<[^>]+>", " " ) + .replaceAll( "&[^;]+;", " " ) + .replaceAll( "\\s+", " " ); + assertThat( "Did not generate summary correctly", output, + containsString( "groupA test-artifact 1.1 compile pom default 1.1.0-2 1.1.3 1.3 3.0" ) ); + } } 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 05de76fc17..1f4b3633e1 100644 --- a/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java +++ b/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java @@ -26,15 +26,16 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MavenVersionComparator; import org.codehaus.mojo.versions.ordering.MercuryVersionComparator; -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.arrayContaining; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -56,14 +57,14 @@ public void test4DigitVersions() throws Exception assertEquals( "artifact", instance.getArtifactId() ); assertEquals( "group", instance.getGroupId() ); assertThat( instance.getVersions(), - Matchers.arrayContaining( versions( "1.0.0.1", "1.0.0.2", "1.1.1", "2.100.0.1", "2.121.2.1", + arrayContaining( versions( "1.0.0.1", "1.0.0.2", "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1" ) ) ); assertThat( instance.getVersions( new DefaultArtifactVersion( "1.1" ), null ), - Matchers.arrayContaining( versions( "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1" ) ) ); + arrayContaining( versions( "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1" ) ) ); assertThat( instance.getVersions( new DefaultArtifactVersion( "1.0.0.2" ), null ), //Matchers.arrayContaining(versions("1.1.1", "2.121.2.1", "2.100.0.1", "3.1.0.1"))); - Matchers.arrayContaining( versions( "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1" ) ) ); + arrayContaining( versions( "1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1" ) ) ); assertEquals( new DefaultArtifactVersion( "2.121.2.1" ), instance.getNewestVersion( new DefaultArtifactVersion( "1.0" ), @@ -126,4 +127,18 @@ public void testReportLabels() assertThat( instance.getNewestUpdate( of( SUBINCREMENTAL ) ).toString(), is( "1.1.0-2" ) ); assertThat( instance.getNewestUpdate( of( INCREMENTAL ) ).toString(), is( "1.1.3" ) ); } + + @Test + public void testGetNewerVersionsWithSnapshot() throws InvalidSegmentException + { + ArtifactVersion[] versions = versions( "1.0.0-SNAPSHOT", "1.0.0" ); + ArtifactVersions instance = + new ArtifactVersions( new DefaultArtifact( "default-group", "dummy-api", + "1.0.0-SNAPSHOT", "foo", "bar", + "jar", null ), + Arrays.asList( versions ), new MavenVersionComparator() ); + + assertThat( instance.getNewerVersions( "1.0.0-SNAPSHOT", of( SUBINCREMENTAL ), false, false ), + arrayContaining( new DefaultArtifactVersion( "1.0.0" ) ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java new file mode 100644 index 0000000000..d62e972303 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java @@ -0,0 +1,106 @@ +package org.codehaus.mojo.versions.ordering; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 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.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.lessThan; + +/** + * Unit tests for {@link BoundArtifactVersion} + */ +public class BoundArtifactVersionTest +{ + @Test + public void testMajorUpperBoundGreaterThanNextMajor() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.2.3" ), + Segment.MAJOR ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "2.0.0" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testSubIncrementalUpperBoundGreaterThanNextSubIncremental() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.2.3-2" ), + Segment.SUBINCREMENTAL ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.2.3-3" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testVersionShorterThanSegment() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + Segment.INCREMENTAL ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.1.3" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testVersionBoundArtifactVersionShorterThanConcreteVersionAndSegment() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + Segment.SUBINCREMENTAL ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.1.3" ); + assertThat( bound.compareTo( artifactVersion ), lessThan( 0 ) ); + } + + @Test + public void testVersionSubIncrementalBoundGreaterThanSubIncremental() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + Segment.SUBINCREMENTAL ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.1.0-2" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testVersionSubIncrementalBoundGreaterThanIncremental() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + Segment.INCREMENTAL ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.1.3" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testVersionSubIncrementalBoundGreaterThanMinor() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + Segment.MINOR ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.3" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } + + @Test + public void testSnapshotWithSubIncremental() + { + BoundArtifactVersion bound = new BoundArtifactVersion( new DefaultArtifactVersion( "1.0.0-SNAPSHOT" ), + Segment.MINOR ); + ArtifactVersion artifactVersion = new DefaultArtifactVersion( "1.0.0" ); + assertThat( bound.compareTo( artifactVersion ), greaterThan( 0 ) ); + } +} 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 2369712084..452ef34786 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java @@ -20,88 +20,63 @@ */ 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.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 +public class MavenVersionComparatorTest extends VersionComparatorTestBase { - private MavenVersionComparator instance = new MavenVersionComparator(); - - @Test - public void testSegmentCounting() - throws Exception + public MavenVersionComparatorTest() { - 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( "" ) ) ) ); + super( new MavenVersionComparator() ); } @Test - public void testSegmentIncrementing() throws InvalidSegmentException - { - assertIncrement( "6-SNAPSHOT", "5", MAJOR ); - assertIncrement( "6.0-SNAPSHOT", "5.0", MAJOR ); - assertIncrement( "5.1-SNAPSHOT", "5.0", MINOR ); - assertIncrement( "5.1.0-SNAPSHOT", "5.0.1", MINOR ); - assertIncrement( "5.alpha.2-SNAPSHOT", "5.alpha.1", MAJOR ); - assertIncrement( "5.alpha-1.2-SNAPSHOT", "5.alpha-1.1", MAJOR ); - assertIncrement( "5.alpha-1.ba-SNAPSHOT", "5.alpha-1.az", MAJOR ); - assertIncrement( "5.alpha-wins.2-SNAPSHOT", "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, Segment segment ) throws InvalidSegmentException + public void testSegmentCounting() { - assertThat( instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString(), - is( expected ) ); + assertThat( 3, is( instance.getSegmentCount( version( "5" ) ) ) ); + assertThat( 3, is( instance.getSegmentCount( version( "5.0" ) ) ) ); + assertThat( 4, is( instance.getSegmentCount( version( "5-0" ) ) ) ); + assertThat( 1, is( instance.getSegmentCount( version( "5.3.a" ) ) ) ); + assertThat( 1, is( instance.getSegmentCount( version( "5.0.a.1.4.5" ) ) ) ); + assertThat( 3, is( instance.getSegmentCount( version( "" ) ) ) ); } @Test public void testUpperBoundaryCustom() { - assertThat( instance.compare( new BoundArtifactVersion( new DefaultArtifactVersion( "1.2.3" ), - INCREMENTAL ), new DefaultArtifactVersion( "1.2.3-ANDRZEJ" ) ), greaterThan( 0 ) ); + assertThat( instance.compare( new BoundArtifactVersion( version( "1.2.3" ), + INCREMENTAL ), version( "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 ) ); + assertThat( instance.compare( new BoundArtifactVersion( version( "1.1.0" ), + INCREMENTAL ), version( "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 ) ); + assertThat( instance.compare( new BoundArtifactVersion( version( "1.1.0" ), + INCREMENTAL ), version( "1.1.0-SNAPSHOT" ) ), greaterThan( 0 ) ); } @Test public void testScopeLessThanNumSegmentsUpper() { - ArtifactVersion artifactVersion = new BoundArtifactVersion( new DefaultArtifactVersion( "1.1" ), + ArtifactVersion artifactVersion = new BoundArtifactVersion( version( "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 ) ); + assertThat( artifactVersion.compareTo( version( "1.0.1" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( version( "1.1-SNAPSHOT" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( version( "1.1" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( version( "1.1.0-2" ) ), greaterThan( 0 ) ); + assertThat( artifactVersion.compareTo( version( "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 52d2b6553d..9af7bbc4c6 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java @@ -22,17 +22,17 @@ 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 +public class MercuryVersionComparatorTest extends VersionComparatorTestBase { - private MercuryVersionComparator instance = new MercuryVersionComparator(); + public MercuryVersionComparatorTest() + { + super( new MercuryVersionComparator() ); + } @Test - public void testSegmentCounting() throws Exception + public void testSegmentCounting() { assertEquals( 1, instance.getSegmentCount( new DefaultArtifactVersion( "5" ) ) ); assertEquals( 2, instance.getSegmentCount( new DefaultArtifactVersion( "5.0" ) ) ); @@ -42,24 +42,5 @@ public void testSegmentCounting() throws Exception assertEquals( 0, instance.getSegmentCount( new DefaultArtifactVersion( "" ) ) ); } - @Test - public void testSegmentIncrementing() throws Exception - { - assertEquals( new DefaultArtifactVersion( "6-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "6.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.1-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.1.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.beta.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.beta-0.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.alpha-2.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), INCREMENTAL ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.beta-0.0-SNAPSHOT" ).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 817468f86d..3b858ee5d4 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/NumericVersionComparatorTest.java @@ -22,15 +22,15 @@ 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; -public class NumericVersionComparatorTest +public class NumericVersionComparatorTest extends VersionComparatorTestBase { - private NumericVersionComparator instance = new NumericVersionComparator(); + public NumericVersionComparatorTest() + { + super( new NumericVersionComparator() ); + } private int instanceCompare( String v1, String v2 ) { @@ -39,7 +39,6 @@ private int instanceCompare( String v1, String v2 ) @Test public void testSmokes() - throws Exception { assertTrue( instanceCompare( "1.0.0.0.0", "1.0.0.0.1" ) < 0 ); assertTrue( instanceCompare( "1.0.0.0.0", "2.0.0.0.1" ) < 0 ); @@ -50,7 +49,6 @@ public void testSmokes() @Test public void testBigValues() - throws Exception { assertTrue( instanceCompare( "1.92.0", "1.100000000000000000000000.0" ) < 0 ); assertTrue( instanceCompare( "1.100000000000000000000000.0", "1.92.0" ) > 0 ); @@ -59,7 +57,6 @@ public void testBigValues() @Test public void testStringValues() - throws Exception { assertTrue( instanceCompare( "1.a20.0", "1.a3.0" ) < 0 ); assertTrue( instanceCompare( "1.a20.0", "1.b10.0" ) < 0 ); @@ -81,7 +78,6 @@ public void testStringValues() @Test public void testQualifiers() - throws Exception { assertTrue( instanceCompare( "1.0-alpha.10", "1.0-alpha.20" ) < 0 ); assertTrue( instanceCompare( "1.0-alpha.10", "1.0-beta.1" ) < 0 ); @@ -92,7 +88,6 @@ public void testQualifiers() @Test public void testSegmentCounting() - throws Exception { assertEquals( 1, instance.getSegmentCount( new DefaultArtifactVersion( "5" ) ) ); assertEquals( 2, instance.getSegmentCount( new DefaultArtifactVersion( "5.0" ) ) ); @@ -102,25 +97,15 @@ public void testSegmentCounting() assertEquals( 0, instance.getSegmentCount( new DefaultArtifactVersion( "" ) ) ); } - @Test - public void testSegmentIncrementing() throws Exception + @Override + public void testVersionComparatorRow5() + { + // non-numeric -- does not apply + } + + @Override + public void testVersionComparatorRow6() { - assertEquals( new DefaultArtifactVersion( "6-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5" ), MAJOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "6.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MAJOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.1-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.1.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.0.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.beta.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.alpha-2.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), MINOR ).toString() ); - assertEquals( new DefaultArtifactVersion( "5.alpha-1.2-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-1.1" ), INCREMENTAL ) - .toString() ); - assertEquals( new DefaultArtifactVersion( "5.beta.0-SNAPSHOT" ).toString(), - instance.incrementSegment( new DefaultArtifactVersion( "5.alpha-wins.1" ), MINOR ).toString() ); + // non-numeric -- does not apply } } diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java new file mode 100644 index 0000000000..28e326a2ec --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java @@ -0,0 +1,100 @@ +package org.codehaus.mojo.versions.ordering; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.lessThan; + +/** + * Abstract base class for {@link MavenVersionComparatorTest}, {@link MercuryVersionComparatorTest}, + * and {@link NumericVersionComparatorTest} + */ +public abstract class VersionComparatorTestBase +{ + protected final VersionComparator instance; + + public VersionComparatorTestBase( VersionComparator instance ) + { + this.instance = instance; + } + + protected static ArtifactVersion version( String version ) + { + return new DefaultArtifactVersion( version ); + } + + @Test + public void testVersionComparatorRow1() + { + assertThat( instance.compare( version( "1" ), version( "2" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0" ), version( "2.0" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0" ), version( "1.1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0" ), version( "1.1" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow2() + { + assertThat( instance.compare( version( "1.0" ), version( "2.0" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0" ), version( "1.1" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow3() + { + assertThat( instance.compare( version( "1.0.0" ), version( "2.0.0" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0" ), version( "1.1.0" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0" ), version( "1.0.1" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow4() + { + assertThat( instance.compare( version( "1.0.0-1" ), version( "2.0.0-1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-1" ), version( "1.1.0-1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-1" ), version( "1.0.1-1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-1" ), version( "1.0.0-2" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow5() + { + assertThat( instance.compare( version( "1.0.0-sp1" ), version( "2.0.0-sp1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-sp1" ), version( "1.1.0-sp1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-sp1" ), version( "1.0.1-sp1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1.0.0-sp1" ), version( "1.0.0-1-sp1" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow6() + { + assertThat( instance.compare( version( "foobar" ), version( "foobar-1" ) ), lessThan( 0 ) ); + } + + @Test + public void testVersionComparatorRow7() + { + assertThat( instance.compare( version( "1-alpha-1" ), version( "2-alpha-1" ) ), lessThan( 0 ) ); + assertThat( instance.compare( version( "1-alpha-1" ), version( "1-alpha-2" ) ), lessThan( 0 ) ); + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java deleted file mode 100644 index c33852d23e..0000000000 --- a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.codehaus.mojo.versions.ordering; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import 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; - -public class VersionComparatorsTest -{ - private final String[] versionDataset = { - "1", - "1.0", - "1.0.0", - "1.0.0-1", - "1.0.0-sp1", - "foobar", - "1-alpha-1", - }; - - @Test - public void testMavenVersionComparator() throws InvalidSegmentException - { - assertVersions( new MavenVersionComparator() ); - } - - @Test - public void testMercuryVersionComparator() throws InvalidSegmentException - { - assertVersions( new MercuryVersionComparator() ); - } - - @Test - public void testNumericVersionComparator() throws InvalidSegmentException - { - assertVersions( new NumericVersionComparator() ); - } - - public void assertVersions( VersionComparator instance ) throws InvalidSegmentException - { - for ( String s : versionDataset ) - { - assertLater( s, instance ); - assertLater( s + "-SNAPSHOT", instance ); - } - } - - public void assertLater( String version, VersionComparator instance ) throws InvalidSegmentException - { - ArtifactVersion v1 = new DefaultArtifactVersion( version ); - int count = instance.getSegmentCount( v1 ); - for ( int i = 0; i < count; i++ ) - { - ArtifactVersion v2 = instance.incrementSegment( v1, Segment.of( i ) ); - assertTrue( v1 + " < " + v2, instance.compare( v1, v2 ) < 0 ); - } - } -}