diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/Restriction.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/Restriction.java index 04068950f7..e06e2b48f1 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/Restriction.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/Restriction.java @@ -60,4 +60,33 @@ public class Restriction { return upperBoundInclusive; } + + public boolean containsVersion( ArtifactVersion version ) + { + if ( lowerBound != null ) + { + int comparison = lowerBound.compareTo( version ); + if ( comparison == 0 && !lowerBoundInclusive ) + { + return false; + } + if ( comparison > 0 ) + { + return false; + } + } + if ( upperBound != null ) + { + int comparison = upperBound.compareTo( version ); + if ( comparison == 0 && !upperBoundInclusive ) + { + return false; + } + if ( comparison < 0 ) + { + return false; + } + } + return true; + } } diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java index 5d1c8b583e..b54ab9402d 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java @@ -18,6 +18,7 @@ package org.apache.maven.artifact.versioning; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; /** @@ -178,14 +179,47 @@ public class VersionRange { ArtifactVersion version = max( recommendedVersion, restriction.getRecommendedVersion() ); - // TODO + List r1 = this.restrictions; + List r2 = restriction.restrictions; + List restrictions = Collections.EMPTY_LIST; + if ( !r1.isEmpty() || !r2.isEmpty() ) + { + // TODO: amalgamate + restrictions = new ArrayList( r1.size() + r2.size() ); + restrictions.addAll( r1 ); + restrictions.addAll( r2 ); - return new VersionRange( version, Collections.EMPTY_LIST ); + boolean found = false; + for ( Iterator i = restrictions.iterator(); i.hasNext() && !found; ) + { + Restriction r = (Restriction) i.next(); + + if ( r.containsVersion( version ) ) + { + found = true; + } + } + + if ( !found ) + { + version = null; + } + } + + return new VersionRange( version, restrictions ); } private ArtifactVersion max( ArtifactVersion v1, ArtifactVersion v2 ) { - if ( v1.compareTo( v2 ) > 0 ) + if ( v1 == null ) + { + return v2; + } + if ( v2 == null ) + { + return v1; + } + else if ( v1.compareTo( v2 ) > 0 ) { return v1; } diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java index 7c04378f49..fdb2395e97 100644 --- a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java @@ -135,14 +135,125 @@ public class VersionRangeTest } public void testIntersections() + throws InvalidVersionSpecificationException { - VersionRange range1 = VersionRange.createFromVersion( "1.0" ); - VersionRange range2 = VersionRange.createFromVersion( "1.1" ); + VersionRange range1 = VersionRange.createFromVersionSpec( "1.0" ); + VersionRange range2 = VersionRange.createFromVersionSpec( "1.1" ); VersionRange mergedRange = range1.restrict( range2 ); assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + assertEquals( CHECK_NUM_RESTRICTIONS, 0, mergedRange.getRestrictions().size() ); mergedRange = range2.restrict( range1 ); assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + assertEquals( CHECK_NUM_RESTRICTIONS, 0, mergedRange.getRestrictions().size() ); + + range1 = VersionRange.createFromVersionSpec( "[1.0,)" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + List restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + Restriction restriction = (Restriction) restrictions.get( 0 ); + assertEquals( CHECK_LOWER_BOUND, "1.0", restriction.getLowerBound().toString() ); + assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "[1.1,)" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() ); + assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "[1.1]" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() ); + assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getLowerBound().toString() ); + assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "(1.1,)" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "[1.2,)" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() ); + assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "(,1.2]" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertEquals( CHECK_UPPER_BOUND, "1.2", restriction.getUpperBound().toString() ); + assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "(,1.1]" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getUpperBound().toString() ); + assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "(,1.1)" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getUpperBound().toString() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + range1 = VersionRange.createFromVersionSpec( "(,1.0]" ); + range2 = VersionRange.createFromVersionSpec( "1.1" ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + restriction = (Restriction) restrictions.get( 0 ); + assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertEquals( CHECK_UPPER_BOUND, "1.0", restriction.getUpperBound().toString() ); + assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); } private void checkInvalidRange( String version )