MNG-5645: When a number is not used in a version correctly, provide a better error.

Submitted by: Phil Pratt-Szeliga
This commit is contained in:
Jason van Zyl 2014-06-11 08:44:42 -04:00
parent c6529932f9
commit af1ecd5f00
3 changed files with 54 additions and 32 deletions

View File

@ -21,6 +21,7 @@ package org.apache.maven.artifact.versioning;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.NoSuchElementException;
/**
* Default implementation of artifact versioning.
@ -204,12 +205,18 @@ public class DefaultArtifactVersion
private static Integer getNextIntegerToken( StringTokenizer tok )
{
String s = tok.nextToken();
if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
try {
String s = tok.nextToken();
if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
{
throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
}
return Integer.valueOf( s );
}
catch( NoSuchElementException e )
{
throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
throw new NumberFormatException( "Number is invalid" );
}
return Integer.valueOf( s );
}
@Override

View File

@ -38,9 +38,10 @@ public class DefaultArtifactVersionTest
String qualifier )
{
ArtifactVersion artifactVersion = newArtifactVersion( version );
String parsed = "'" + version + "' parsed as ('" + artifactVersion.getMajorVersion() + "', '"
+ artifactVersion.getMinorVersion() + "', '" + artifactVersion.getIncrementalVersion() + "', '"
+ artifactVersion.getBuildNumber() + "', '" + artifactVersion.getQualifier() + "'), ";
String parsed =
"'" + version + "' parsed as ('" + artifactVersion.getMajorVersion() + "', '"
+ artifactVersion.getMinorVersion() + "', '" + artifactVersion.getIncrementalVersion() + "', '"
+ artifactVersion.getBuildNumber() + "', '" + artifactVersion.getQualifier() + "'), ";
assertEquals( parsed + "check major version", major, artifactVersion.getMajorVersion() );
assertEquals( parsed + "check minor version", minor, artifactVersion.getMinorVersion() );
assertEquals( parsed + "check incremental version", incremental, artifactVersion.getIncrementalVersion() );
@ -51,39 +52,39 @@ public class DefaultArtifactVersionTest
public void testVersionParsing()
{
checkVersionParsing( "1" , 1, 0, 0, 0, null );
checkVersionParsing( "1.2" , 1, 2, 0, 0, null );
checkVersionParsing( "1.2.3" , 1, 2, 3, 0, null );
checkVersionParsing( "1.2.3-1" , 1, 2, 3, 1, null );
checkVersionParsing( "1.2.3-alpha-1" , 1, 2, 3, 0, "alpha-1" );
checkVersionParsing( "1.2-alpha-1" , 1, 2, 0, 0, "alpha-1" );
checkVersionParsing( "1.2-alpha-1-20050205.060708-1" , 1, 2, 0, 0, "alpha-1-20050205.060708-1" );
checkVersionParsing( "RELEASE" , 0, 0, 0, 0, "RELEASE" );
checkVersionParsing( "2.0-1" , 2, 0, 0, 1, null );
checkVersionParsing( "1", 1, 0, 0, 0, null );
checkVersionParsing( "1.2", 1, 2, 0, 0, null );
checkVersionParsing( "1.2.3", 1, 2, 3, 0, null );
checkVersionParsing( "1.2.3-1", 1, 2, 3, 1, null );
checkVersionParsing( "1.2.3-alpha-1", 1, 2, 3, 0, "alpha-1" );
checkVersionParsing( "1.2-alpha-1", 1, 2, 0, 0, "alpha-1" );
checkVersionParsing( "1.2-alpha-1-20050205.060708-1", 1, 2, 0, 0, "alpha-1-20050205.060708-1" );
checkVersionParsing( "RELEASE", 0, 0, 0, 0, "RELEASE" );
checkVersionParsing( "2.0-1", 2, 0, 0, 1, null );
// 0 at the beginning of a number has a special handling
checkVersionParsing( "02" , 0, 0, 0, 0, "02" );
checkVersionParsing( "0.09" , 0, 0, 0, 0, "0.09" );
checkVersionParsing( "0.2.09" , 0, 0, 0, 0, "0.2.09" );
checkVersionParsing( "2.0-01" , 2, 0, 0, 0, "01" );
checkVersionParsing( "02", 0, 0, 0, 0, "02" );
checkVersionParsing( "0.09", 0, 0, 0, 0, "0.09" );
checkVersionParsing( "0.2.09", 0, 0, 0, 0, "0.2.09" );
checkVersionParsing( "2.0-01", 2, 0, 0, 0, "01" );
// version schemes not really supported: fully transformed as qualifier
checkVersionParsing( "1.0.1b" , 0, 0, 0, 0, "1.0.1b" );
checkVersionParsing( "1.0M2" , 0, 0, 0, 0, "1.0M2" );
checkVersionParsing( "1.0RC2" , 0, 0, 0, 0, "1.0RC2" );
checkVersionParsing( "1.0.1b", 0, 0, 0, 0, "1.0.1b" );
checkVersionParsing( "1.0M2", 0, 0, 0, 0, "1.0M2" );
checkVersionParsing( "1.0RC2", 0, 0, 0, 0, "1.0RC2" );
checkVersionParsing( "1.1.2.beta1", 1, 1, 2, 0, "beta1" );
checkVersionParsing( "1.7.3.beta1", 1, 7, 3, 0, "beta1" );
checkVersionParsing( "1.7.3.0" , 0, 0, 0, 0, "1.7.3.0" );
checkVersionParsing( "1.7.3.0-1" , 0, 0, 0, 0, "1.7.3.0-1" );
checkVersionParsing( "PATCH-1193602" , 0, 0, 0, 0, "PATCH-1193602" );
checkVersionParsing( "5.0.0alpha-2006020117" , 0, 0, 0, 0, "5.0.0alpha-2006020117" );
checkVersionParsing( "1.7.3.0", 0, 0, 0, 0, "1.7.3.0" );
checkVersionParsing( "1.7.3.0-1", 0, 0, 0, 0, "1.7.3.0-1" );
checkVersionParsing( "PATCH-1193602", 0, 0, 0, 0, "PATCH-1193602" );
checkVersionParsing( "5.0.0alpha-2006020117", 0, 0, 0, 0, "5.0.0alpha-2006020117" );
checkVersionParsing( "1.0.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.0.-SNAPSHOT" );
checkVersionParsing( "1..0-SNAPSHOT", 0, 0, 0, 0, "1..0-SNAPSHOT" );
checkVersionParsing( "1.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.-SNAPSHOT" );
checkVersionParsing( ".1.0-SNAPSHOT", 0, 0, 0, 0, ".1.0-SNAPSHOT" );
checkVersionParsing( "1.2.3.200705301630" , 0, 0, 0, 0, "1.2.3.200705301630" );
checkVersionParsing( "1.2.3-200705301630" , 1, 2, 3, 0, "200705301630" );
checkVersionParsing( "1.2.3.200705301630", 0, 0, 0, 0, "1.2.3.200705301630" );
checkVersionParsing( "1.2.3-200705301630", 1, 2, 3, 0, "200705301630" );
}
public void testVersionComparing()
@ -146,7 +147,7 @@ public class DefaultArtifactVersionTest
assertVersionOlder( "1.0.0-SNAPSHOT", "1.1-SNAPSHOT" );
assertVersionOlder( "1.1-SNAPSHOT", "1.2.0-SNAPSHOT" );
//assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-SNAPSHOT" );
// assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-SNAPSHOT" );
assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-2-SNAPSHOT" );
assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-beta-1-SNAPSHOT" );
@ -156,7 +157,7 @@ public class DefaultArtifactVersionTest
assertVersionOlder( "1.0-SNAPSHOT", "1.0-1-SNAPSHOT" );
assertVersionOlder( "1.0-1-SNAPSHOT", "1.0-2-SNAPSHOT" );
//assertVersionEqual( "2.0-0-SNAPSHOT", "2.0-SNAPSHOT" );
// assertVersionEqual( "2.0-0-SNAPSHOT", "2.0-SNAPSHOT" );
assertVersionOlder( "2.0-SNAPSHOT", "2.0-1-SNAPSHOT" );
assertVersionOlder( "2.0.0-SNAPSHOT", "2.0-1-SNAPSHOT" );
assertVersionOlder( "2.0-1-SNAPSHOT", "2.0.1-SNAPSHOT" );
@ -192,6 +193,18 @@ public class DefaultArtifactVersionTest
assertFalse( newArtifactVersion( "1" ).equals( "non-an-artifact-version-instance" ) );
}
public void testNonNumericVersionRepresentationReturnsANumberFormatException()
{
try
{
new DefaultArtifactVersion( "..." );
}
catch ( Exception e )
{
assertTrue("We expect a NumberFormatException to be thrown.", e instanceof NumberFormatException);
}
}
private void assertVersionOlder( String left, String right )
{
assertTrue( left + " should be older than " + right,
@ -207,5 +220,4 @@ public class DefaultArtifactVersionTest
assertTrue( right + " should be equal to " + left,
newArtifactVersion( right ).compareTo( newArtifactVersion( left ) ) == 0 );
}
}

View File

@ -120,6 +120,9 @@
<contributor>
<name>Sébastian Le Merdy (MNG-5613)</name>
</contributor>
<contributor>
<name>Phil Pratt-Szeliga (MNG-5645)</name>
</contributor>
</contributors>
<prerequisites>