From 70eadfd14d4d62433200c8ec9687b5acd9f5129f Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Tue, 9 Aug 2005 09:18:08 +0000 Subject: [PATCH] if a version part has a leading 0, fallback to string comparison but to retain the extra 0. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@231016 13f79535-47bb-0310-9956-ffa450edef68 --- .../versioning/DefaultArtifactVersion.java | 39 ++++++++++++++----- .../DefaultArtifactVersionTest.java | 7 ++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java index b9df3a6939..53ed68970a 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java @@ -163,7 +163,14 @@ public class DefaultArtifactVersion { try { - buildNumber = Integer.valueOf( part2 ); + if ( part2.length() == 1 || !part2.startsWith( "0" ) ) + { + buildNumber = Integer.valueOf( part2 ); + } + else + { + qualifier = part2; + } } catch ( NumberFormatException e ) { @@ -185,37 +192,51 @@ public class DefaultArtifactVersion } else { + boolean fallback = false; StringTokenizer tok = new StringTokenizer( part1, "." ); try { - majorVersion = Integer.valueOf( tok.nextToken() ); + majorVersion = getNextIntegerToken( tok ); if ( tok.hasMoreTokens() ) { - minorVersion = Integer.valueOf( tok.nextToken() ); + minorVersion = getNextIntegerToken( tok ); } if ( tok.hasMoreTokens() ) { - incrementalVersion = Integer.valueOf( tok.nextToken() ); + incrementalVersion = getNextIntegerToken( tok ); } if ( tok.hasMoreTokens() ) { - // qualifier is the whole version, including "-" - qualifier = version; - majorVersion = null; - minorVersion = null; - incrementalVersion = null; + fallback = true; } } catch ( NumberFormatException e ) + { + fallback = true; + } + + if ( fallback ) { // qualifier is the whole version, including "-" qualifier = version; majorVersion = null; minorVersion = null; + incrementalVersion = null; } } } + private static Integer getNextIntegerToken( StringTokenizer tok ) + { + + 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 ); + } + public String toString() { StringBuffer buf = new StringBuffer(); diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java index 4b9d4341fc..4956eaa3a3 100644 --- a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java +++ b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/DefaultArtifactVersionTest.java @@ -105,6 +105,13 @@ public class DefaultArtifactVersionTest assertEquals( "check incremental version", 0, version.getIncrementalVersion() ); assertEquals( "check build number", 0, version.getBuildNumber() ); assertEquals( "check qualifier", "1.7.3.0", version.getQualifier() ); + + version = new DefaultArtifactVersion( "0.09" ); + assertEquals( "check major version", 0, version.getMajorVersion() ); + assertEquals( "check minor version", 0, version.getMinorVersion() ); + assertEquals( "check incremental version", 0, version.getIncrementalVersion() ); + assertEquals( "check build number", 0, version.getBuildNumber() ); + assertEquals( "check qualifier", "0.09", version.getQualifier() ); } public void testVersionComparing()