From aba3090f03ff03e9d0dc9daedb469bb54f5eba34 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Mon, 18 Nov 2024 18:04:49 -0600 Subject: [PATCH] Replicate Maven rules more exactly Fixes the issue where `2.13.5.SP1-redhat-00002` was coming _before_ `2.13.5.Final-redhat-00002`. --- .../version/AbstractVersionIterator.java | 4 ++ .../common/version/MavenVersionIterator.java | 41 ++++++++----------- .../common/version/MavenVersionScheme.java | 4 +- .../common/version/MavenVersionTest.java | 22 ++++++++++ 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/version/src/main/java/io/smallrye/common/version/AbstractVersionIterator.java b/version/src/main/java/io/smallrye/common/version/AbstractVersionIterator.java index fde8f986..b9221334 100644 --- a/version/src/main/java/io/smallrye/common/version/AbstractVersionIterator.java +++ b/version/src/main/java/io/smallrye/common/version/AbstractVersionIterator.java @@ -573,6 +573,10 @@ public void next() throws NoSuchElementException, VersionSyntaxException { i += Character.charCount(cp); } } + if (end == start) { + // capture the zero string (last digit only) + start = end - 1; + } } else if (isSeparatorCodePoint(cp)) { token = TokenType.SEP; end = start + Character.charCount(cp); diff --git a/version/src/main/java/io/smallrye/common/version/MavenVersionIterator.java b/version/src/main/java/io/smallrye/common/version/MavenVersionIterator.java index ec9e791b..ff1298fd 100644 --- a/version/src/main/java/io/smallrye/common/version/MavenVersionIterator.java +++ b/version/src/main/java/io/smallrye/common/version/MavenVersionIterator.java @@ -16,11 +16,11 @@ public boolean isNonEmptySeparator() { } public boolean isNumberPart() { - return super.isNumberPart() || isReleaseString(); + return super.isNumberPart() || super.isAlphaPart() && super.alphaPartEquals("", true); } public boolean isAlphaPart() { - return super.isAlphaPart() && !isReleaseString(); + return super.isAlphaPart() && !isReleaseString() && !super.alphaPartEquals("", true); } public int getSeparatorCodePoint() { @@ -85,7 +85,7 @@ public boolean hasNext() { public void next() throws NoSuchElementException, VersionSyntaxException { super.next(); // skip trailing 0. segments until next - or EOS - if (isDotSeparator()) { + if (isSeparator()) { final long mark = mark(); try { skipTrailer(mark); @@ -123,35 +123,31 @@ boolean nextSeparatorIsEmpty() { } boolean isReleaseString() { - return alphaPartEquals("", true) || alphaPartEquals("ga", true) || alphaPartEquals("final", true) - || alphaPartEquals("release", true); + return alphaPartEquals("ga", true) || alphaPartEquals("final", true) || alphaPartEquals("release", true); } boolean isZeroSegment() { return super.isNumberPart() && super.numberPartEquals(0) || super.isAlphaPart() && isReleaseString(); } - boolean isDashSeparator() { - return isSeparator() && getSeparatorCodePoint() == '-'; - } - - boolean isDotSeparator() { - return isSeparator() && getSeparatorCodePoint() == '.'; - } - void skipTrailer(long mark) { - assert isDotSeparator(); + assert isSeparator(); assert hasNext(); - next(); - if (isZeroSegment()) { + long sep = mark(); + super.next(); + if (isNumberPart() && numberPartEquals(0)) { // could be more zeros - assert hasNext(); - next(); - if (!isDashSeparator()) { - assert isDotSeparator(); + if (hasNext()) { + super.next(); + assert isSeparator(); skipTrailer(mark); + } else { + // done! it was all trailing junk + return; } - // return the dash separator + } else if (isAlphaPart() || getMilestoneMagnitude() != -1) { + // qualifier! + reset(sep); } else { // can't skip reset(mark); @@ -182,8 +178,6 @@ int getMilestoneMagnitude() { } else if (alphaPartEquals("sp", true)) { return 6; } - } else if (isZeroSegment()) { - return 5; } return -1; // not a milestone } @@ -194,7 +188,6 @@ int getMilestoneMagnitude() { "milestone", "rc", "snapshot", - "", // this is really lame but it's how Maven does it "sp", }; } diff --git a/version/src/main/java/io/smallrye/common/version/MavenVersionScheme.java b/version/src/main/java/io/smallrye/common/version/MavenVersionScheme.java index eeba00c3..de165491 100644 --- a/version/src/main/java/io/smallrye/common/version/MavenVersionScheme.java +++ b/version/src/main/java/io/smallrye/common/version/MavenVersionScheme.java @@ -61,10 +61,10 @@ protected int compareZero(MavenVersionIterator i) { protected int compare(MavenVersionIterator i1, MavenVersionIterator i2) { // pad separators if (i1.isSeparator()) { - i1.insertEmptyAlpha(); + i1.insertEmptyNumber(); } if (i2.isSeparator()) { - i2.insertEmptyAlpha(); + i2.insertEmptyNumber(); } // there is a current element diff --git a/version/src/test/java/io/smallrye/common/version/MavenVersionTest.java b/version/src/test/java/io/smallrye/common/version/MavenVersionTest.java index e3439b62..776d4dfc 100644 --- a/version/src/test/java/io/smallrye/common/version/MavenVersionTest.java +++ b/version/src/test/java/io/smallrye/common/version/MavenVersionTest.java @@ -59,6 +59,28 @@ public void testMavenCompare() throws Exception { checkMavenConsistency("3.6.0.SP1", "3.6.0"); } + @Test + public void testMavenCompare2() throws Exception { + checkMavenConsistency("3.6.0-CR1", "3.6.0"); + checkMavenConsistency("3.6.0-FINAL", "3.6.0"); + checkMavenConsistency("3.6.0-SP1", "3.6.0"); + checkMavenConsistency("2.13.5.SP1.redhat-00002", "2.13.5.0.redhat-00002"); + checkMavenConsistency("2.13.5.SP1.redhat-00002", "2.13.5.Final.redhat-00002"); + checkMavenConsistency("2.13.5.SP1-redhat-00002", "2.13.5.Final-redhat-00002"); + checkMavenConsistency("2.13.5-redhat-00002", "2.13.5.Final-redhat-00002"); + checkMavenConsistency("2.1", "2.0.0.0.0.1"); + checkMavenConsistency("2.0.0.0.0.1", "2.final.0.0.0.1"); + checkMavenConsistency("2.1", "2.0.0.final.0.1"); + checkMavenConsistency("2.sp", "2.0.0.0.0.sp"); + checkMavenConsistency("2.beta", "2.0.0.0.0.beta"); + checkMavenConsistency("2.0.0.foo", "2.0.foo"); + checkMavenConsistency("4.0.4", "4.final.4"); + checkMavenConsistency("4.0.4", "4..4"); + checkMavenConsistency("4.final.4", "4..4"); + checkMavenConsistency("4.final.4", "4.0.4"); + checkMavenConsistency("0.4.final.4", "0.4.0.4"); + } + private static final org.eclipse.aether.version.VersionScheme MR_SCHEME = new GenericVersionScheme(); private static void checkMavenConsistency(String v1, String v2) throws InvalidVersionSpecificationException {