From 78edd431226204316e2d472d152b869a4a17ce61 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 13 Oct 2022 15:29:50 +0200 Subject: [PATCH] [MNG-7106] Accept same lower and upper bound in version spec string (#825) --- .../maven/artifact/versioning/VersionRange.java | 12 ++++++------ .../maven/artifact/versioning/VersionRangeTest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) 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 8267a45b89..c4fbf9b431 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 @@ -211,10 +211,6 @@ public class VersionRange { String lowerBound = process.substring( 0, index ).trim(); String upperBound = process.substring( index + 1 ).trim(); - if ( lowerBound.equals( upperBound ) ) - { - throw new InvalidVersionSpecificationException( "Range cannot have identical boundaries: " + spec ); - } ArtifactVersion lowerVersion = null; if ( lowerBound.length() > 0 ) @@ -227,9 +223,13 @@ public class VersionRange upperVersion = new DefaultArtifactVersion( upperBound ); } - if ( upperVersion != null && lowerVersion != null && upperVersion.compareTo( lowerVersion ) < 0 ) + if ( upperVersion != null && lowerVersion != null ) { - throw new InvalidVersionSpecificationException( "Range defies version ordering: " + spec ); + int result = upperVersion.compareTo( lowerVersion ); + if ( result < 0 || ( result == 0 && ( !lowerBoundInclusive || !upperBoundInclusive ) ) ) + { + throw new InvalidVersionSpecificationException( "Range defies version ordering: " + spec ); + } } restriction = new Restriction( lowerVersion, lowerBoundInclusive, upperVersion, upperBoundInclusive ); 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 50270a00d4..7c05940c05 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 @@ -160,6 +160,14 @@ public class VersionRangeTest assertTrue( range.containsVersion( new DefaultArtifactVersion( "5.0.9.0" ) ) ); } + @Test + public void testSameUpperAndLowerBoundRoundtrip() throws InvalidVersionSpecificationException + { + VersionRange range = VersionRange.createFromVersionSpec( "[1.0]" ); + VersionRange range2 = VersionRange.createFromVersionSpec( range.toString() ); + assertEquals( range, range2 ); + } + @Test public void testInvalidRanges() {