Skip to content

Commit

Permalink
[MNG-7644] Fix version comparison where .X1 < -X2 for any string qual…
Browse files Browse the repository at this point in the history
…ifier X

This closes #930
  • Loading branch information
sultan authored and michael-o committed Dec 21, 2022
1 parent 7d45894 commit da4246a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
* </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>
Expand Down Expand Up @@ -676,6 +679,14 @@ 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;

Expand All @@ -702,6 +713,14 @@ else if ( Character.isDigit( c ) )

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private Comparable newComparable( String version )
"1-1", "1-2", "1-123" };

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 )
{
Expand Down Expand Up @@ -337,4 +337,21 @@ public void testReuse()

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

0 comments on commit da4246a

Please sign in to comment.