Repository: release-engineering/pom-manipulation-ext
Issue: REGRESSION: versions no longer zero-filled for matching

given POM version: '7'
given metadata versions:
- '7'
- '7.0.0.build-3'

The new version should be '7.0.0.build-4', but it's '7.0.0.build-1' (it's not zero-filling the version when matching against the metadata versions). I've confirmed that this worked when we were using atlas for version parsing / sorting, but broke when we moved to the internal Version class for parsing/sorting.


Changed files:
src/test/java/org/commonjava/maven/ext/manip/impl/VersionTest.java, src/main/java/org/commonjava/maven/ext/manip/impl/Version.java

diff --git a/src/main/java/org/commonjava/maven/ext/manip/impl/Version.java b/src/main/java/org/commonjava/maven/ext/manip/impl/Version.java
index ace29106..c6aafb2a 100644
--- a/src/main/java/org/commonjava/maven/ext/manip/impl/Version.java
+++ b/src/main/java/org/commonjava/maven/ext/manip/impl/Version.java
@@ -611,10 +611,11 @@ public class Version
     {
         int highestBuildNum = 0;
 
-        // Build version pattern regex
+        // Build version pattern regex, matches something like "<mmm>.<qualifier>.<buildnum>".
         StringBuffer versionPatternBuf = new StringBuffer();
         versionPatternBuf.append( "(" );
         versionPatternBuf.append( Pattern.quote( getOriginalMMM() ) );
+        versionPatternBuf.append( "(" + DELIMITER_REGEX + "0)*" ); // Match zeros appended to a major only version
         versionPatternBuf.append( ")?" );
         versionPatternBuf.append( DELIMITER_REGEX );
         if ( version.getQualifierBase() != null )
@@ -622,9 +623,7 @@ public class Version
             versionPatternBuf.append( Pattern.quote( version.getQualifierBase() ) );
             versionPatternBuf.append( DELIMITER_REGEX );
         }
-        versionPatternBuf.append( "(" );
-        versionPatternBuf.append( "\\\\d+" );
-        versionPatternBuf.append( ")" );
+        versionPatternBuf.append( "(\\\\d+)" );
         String candidatePatternStr = versionPatternBuf.toString();
 
         logger.debug( "Using pattern: '{}' to find compatible versions from metadata.", candidatePatternStr );
@@ -635,7 +634,7 @@ public class Version
             final Matcher candidateSuffixMatcher = candidateSuffixPattern.matcher( compareVersion );
             if ( candidateSuffixMatcher.matches() )
             {
-                String buildNumberStr = candidateSuffixMatcher.group( 2 );
+                String buildNumberStr = candidateSuffixMatcher.group( 3 );
                 int compareBuildNum = Integer.parseInt( buildNumberStr );
                 if ( compareBuildNum > highestBuildNum )
                 {
diff --git a/src/test/java/org/commonjava/maven/ext/manip/impl/VersionTest.java b/src/test/java/org/commonjava/maven/ext/manip/impl/VersionTest.java
index db51b0ee..3d3c968a 100644
--- a/src/test/java/org/commonjava/maven/ext/manip/impl/VersionTest.java
+++ b/src/test/java/org/commonjava/maven/ext/manip/impl/VersionTest.java
@@ -269,13 +269,21 @@ public class VersionTest
     }
 
     @Test
-    @Ignore( "PENDING: https://github.com/release-engineering/pom-manipulation-ext/issues/122" )
     public void testZeroFill_FindHighestMatchingBuildNumber()
     {
         final Set<String> versionSet = new HashSet<String>();
-        final Version version = new Version( "7" );
+        final Version majorOnlyVersion = new Version( "7" );
+        majorOnlyVersion.appendQualifierSuffix( "redhat" );
+        System.out.println("OSGi version: " + majorOnlyVersion.getOSGiVersionString());
         versionSet.add( "7.0.0.redhat-2" );
-        assertThat( version.findHighestMatchingBuildNumber( version, versionSet ), equalTo( 2 ) );
+        assertThat( majorOnlyVersion.findHighestMatchingBuildNumber( majorOnlyVersion, versionSet ), equalTo( 2 ) );
+        versionSet.clear();
+
+        final Version majorMinorVersion = new Version( "7.1" );
+        majorMinorVersion.appendQualifierSuffix( "redhat" );
+        System.out.println("OSGi version: " + majorMinorVersion.getOSGiVersionString());
+        versionSet.add( "7.1.0.redhat-2" );
+        assertThat( majorMinorVersion.findHighestMatchingBuildNumber( majorMinorVersion, versionSet ), equalTo( 2 ) );
         versionSet.clear();
     }