[MNG-7644] Fix version comparison where .X1 < -X2 for any string qualifier X

This #932
This commit is contained in:
Swell 2022-12-20 18:29:26 +01:00 committed by Michael Osipov
parent 8833330b00
commit 35c81bedde
2 changed files with 37 additions and 3 deletions

View File

@ -52,7 +52,10 @@ import java.util.Properties;
* </ul> * </ul>
* Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive), * Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive),
* </li> * </li>
* <li>a hyphen usually precedes a qualifier, and is always less important than something preceded with a dot.</li> * <li>a hyphen usually precedes a qualifier, and is always less important than digits/number, for example
* {@code 1.0.RC2 < 1.0-RC3 < 1.0.1}; but prefer {@code 1.0.0-RC1} over {@code 1.0.0.RC1}, and more
* generally: {@code 1.0.X2 < 1.0-X3 < 1.0.1} for any string {@code X}; but prefer {@code 1.0.0-X1}
* over {@code 1.0.0.X1}.</li>
* </ul> * </ul>
* *
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a> * @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@ -570,6 +573,13 @@ public class ComparableVersion implements Comparable<ComparableVersion> {
stack.push(list); stack.push(list);
} else if (Character.isDigit(c)) { } else if (Character.isDigit(c)) {
if (!isDigit && i > startIndex) { if (!isDigit && i > startIndex) {
// 1.0.0.X1 < 1.0.0-X2
// treat .X as -X for any string qualifier X
if (!list.isEmpty()) {
list.add(list = new ListItem());
stack.push(list);
}
list.add(new StringItem(version.substring(startIndex, i), true)); list.add(new StringItem(version.substring(startIndex, i), true));
startIndex = i; startIndex = i;
@ -592,6 +602,13 @@ public class ComparableVersion implements Comparable<ComparableVersion> {
} }
if (version.length() > startIndex) { if (version.length() > startIndex) {
// 1.0.0.X1 < 1.0.0-X2
// treat .X as -X for any string qualifier X
if (!isDigit && !list.isEmpty()) {
list.add(list = new ListItem());
stack.push(list);
}
list.add(parseItem(isDigit, version.substring(startIndex))); list.add(parseItem(isDigit, version.substring(startIndex)));
} }

View File

@ -71,8 +71,8 @@ public class ComparableVersionTest {
}; };
private static final String[] VERSIONS_NUMBER = { private static final String[] VERSIONS_NUMBER = {
"2.0", "2-1", "2.0.a", "2.0.0.a", "2.0.2", "2.0.123", "2.1.0", "2.1-a", "2.1b", "2.1-c", "2.1-1", "2.1.0.1", "2.0", "2.0.a", "2-1", "2.0.2", "2.0.123", "2.1.0", "2.1-a", "2.1b", "2.1-c", "2.1-1", "2.1.0.1", "2.2",
"2.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m" "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m"
}; };
private void checkVersionsOrder(String[] versions) { private void checkVersionsOrder(String[] versions) {
@ -346,4 +346,21 @@ public class ComparableVersionTest {
assertEquals(c1, c2, "reused instance should be equivalent to new instance"); assertEquals(c1, c2, "reused instance should be equivalent to new instance");
} }
/**
* Test <a href="https://issues.apache.org/jira/browse/MNG-7644">MNG-7644</a> edge cases
* 1.0.0.RC1 < 1.0.0-RC2 and more generally:
* 1.0.0.X1 < 1.0.0-X2 for any string X
*/
@Test
public void testMng7644() {
for (String x : new String[] {"abc", "alpha", "a", "beta", "b", "def", "milestone", "m", "RC"}) {
// 1.0.0.X1 < 1.0.0-X2 for any string x
checkVersionsOrder("1.0.0." + x + "1", "1.0.0-" + x + "2");
// 2.0.X == 2-X == 2.0.0.X for any string x
checkVersionsEqual("2-" + x, "2.0." + x); // previously ordered, now equals
checkVersionsEqual("2-" + x, "2.0.0." + x); // previously ordered, now equals
checkVersionsEqual("2.0." + x, "2.0.0." + x); // previously ordered, now equals
}
}
} }