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 e06e2b48f1..7480668ae1 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 @@ -32,6 +32,8 @@ public class Restriction private final boolean upperBoundInclusive; + static final Restriction EVERYTHING = new Restriction( null, false, null, false ); + public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound, boolean upperBoundInclusive ) { 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 bdf07ee5fc..a840547a2b 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 @@ -110,6 +110,7 @@ public class VersionRange else { version = new DefaultArtifactVersion( process ); + restrictions.add( Restriction.EVERYTHING ); } } @@ -182,13 +183,9 @@ public class VersionRange List r1 = this.restrictions; List r2 = restriction.restrictions; List restrictions; - if ( r1.isEmpty() ) + if ( r1.isEmpty() || r2.isEmpty() ) { - restrictions = r2; - } - else if ( r2.isEmpty() ) - { - restrictions = r1; + restrictions = Collections.EMPTY_LIST; } else { 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 17b11ff357..a77822e949 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 @@ -55,9 +55,14 @@ public class VersionRangeTest assertNull( CHECK_VERSION_RECOMMENDATION, range.getRecommendedVersion() ); range = VersionRange.createFromVersionSpec( "1.0" ); - restrictions = range.getRestrictions(); - assertEquals( CHECK_NUM_RESTRICTIONS, 0, restrictions.size() ); assertEquals( CHECK_VERSION_RECOMMENDATION, "1.0", range.getRecommendedVersion().toString() ); + restrictions = range.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() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); range = VersionRange.createFromVersionSpec( "[1.0]" ); restrictions = range.getRestrictions(); @@ -141,20 +146,32 @@ public class VersionRangeTest 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() ); + List restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); + Restriction restriction = (Restriction) restrictions.get( 0 ); + assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() ); + assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); mergedRange = range2.restrict( range1 ); assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() ); - assertEquals( CHECK_NUM_RESTRICTIONS, 0, mergedRange.getRestrictions().size() ); + 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() ); + assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() ); + assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); // TODO: test reversed restrictions on all below 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(); + restrictions = mergedRange.getRestrictions(); assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() ); - Restriction restriction = (Restriction) restrictions.get( 0 ); + 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() ); @@ -403,7 +420,6 @@ public class VersionRangeTest assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getUpperBound().toString() ); assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); - // TODO: specify this differently from no restriction - no restriction should be null, or one restriction with null ends? range1 = VersionRange.createFromVersionSpec( "[1.0,1.1)" ); range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" ); mergedRange = range1.restrict( range2 ); @@ -584,6 +600,23 @@ public class VersionRangeTest assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() ); assertEquals( CHECK_UPPER_BOUND, "1.6", restriction.getUpperBound().toString() ); assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() ); + + // test restricting empty sets + range1 = VersionRange.createFromVersionSpec( "[,1.1],[1.4,]" ); + range2 = VersionRange.createFromVersionSpec( "[1.2,1.3]" ); + range1 = range1.restrict( range2 ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 0, restrictions.size() ); + + range1 = VersionRange.createFromVersionSpec( "[,1.1],[1.4,]" ); + range2 = VersionRange.createFromVersionSpec( "[1.2,1.3]" ); + range2 = range1.restrict( range2 ); + mergedRange = range1.restrict( range2 ); + assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() ); + restrictions = mergedRange.getRestrictions(); + assertEquals( CHECK_NUM_RESTRICTIONS, 0, restrictions.size() ); } private void checkInvalidRange( String version )