PR: MNG-505

range and soft requirement intersections

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@219633 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-07-19 09:30:03 +00:00
parent d7d6c11a4b
commit 4e9bd7a8e9
3 changed files with 179 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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 )