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

This closes #931
This commit is contained in:
Swell 2022-12-20 18:33:39 +01:00 committed by Michael Osipov
parent ffb5428c48
commit 6de6877c2f
2 changed files with 36 additions and 3 deletions

View File

@ -52,7 +52,10 @@ import java.util.Properties;
* </ul>
* Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive),
* </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>
*
* @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);
} else if (Character.isDigit(c)) {
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));
startIndex = i;
@ -592,6 +602,13 @@ public class ComparableVersion implements Comparable<ComparableVersion> {
}
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)));
}

View File

@ -68,8 +68,8 @@ public class ComparableVersionTest extends TestCase {
};
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.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m"
"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.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m"
};
private void checkVersionsOrder(String[] versions) {
@ -332,4 +332,20 @@ public class ComparableVersionTest extends TestCase {
assertEquals("reused instance should be equivalent to new instance", c1, c2);
}
/**
* 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
*/
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
}
}
}