]> source.dussan.org Git - archiva.git/commitdiff
[MRM-463] fix merging of latest/release fields in metadata
authorBrett Porter <brett@apache.org>
Thu, 13 Sep 2007 02:20:48 +0000 (02:20 +0000)
committerBrett Porter <brett@apache.org>
Thu, 13 Sep 2007 02:20:48 +0000 (02:20 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@575145 13f79535-47bb-0310-9956-ffa450edef68

archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionComparator.java
archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java
archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.java
archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-central.xml
archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-java.net.xml
archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata.xml

index 597b93c9a37d8efdd57f19c3f89d97b572ae9a68..9acd26869addc34331bf85e4b3f9dc6e74e4895d 100644 (file)
@@ -29,20 +29,20 @@ import java.util.List;
 
 /**
  * VersionComparator - compare the parts of two version strings.
- * 
+ * <p/>
  * Technique.
- * 
- *  * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
- * 
+ * <p/>
+ * * Split the version strings into parts by splitting on <code>"-._"</code> first, then breaking apart words from numbers.
+ * <p/>
  * <code>
- *   "1.0"         = "1", "0"
- *   "1.0-alpha-1" = "1", "0", "alpha", "1"
- *   "2.0-rc2"     = "2", "0", "rc", "2"
- *   "1.3-m2"      = "1", "3", "m", "3"
+ * "1.0"         = "1", "0"
+ * "1.0-alpha-1" = "1", "0", "alpha", "1"
+ * "2.0-rc2"     = "2", "0", "rc", "2"
+ * "1.3-m2"      = "1", "3", "m", "3"
  * </code>
- * 
+ * <p/>
  * compare each part individually, and when they do not match, perform the following test.
- * 
+ * <p/>
  * Numbers are calculated per normal comparison rules.
  * Words that are part of the "special word list" will be treated as their index within that heirarchy.
  * Words that cannot be identified as special, are treated using normal case-insensitive comparison rules.
@@ -51,15 +51,15 @@ import java.util.List;
  * @version $Id$
  */
 public class VersionComparator
-    implements Comparator
+    implements Comparator<String>
 {
-    private static Comparator INSTANCE = new VersionComparator();
+    private static Comparator<String> INSTANCE = new VersionComparator();
 
-    private List specialWords;
+    private List<String> specialWords;
 
     public VersionComparator()
     {
-        specialWords = new ArrayList();
+        specialWords = new ArrayList<String>();
 
         // ids that refer to LATEST
         specialWords.add( "final" );
@@ -91,61 +91,53 @@ public class VersionComparator
         specialWords.add( "snapshot" );
     }
 
-    public static Comparator getInstance()
+    public static Comparator<String> getInstance()
     {
         return INSTANCE;
     }
 
-    public int compare( Object o1, Object o2 )
+    public int compare( String o1, String o2 )
     {
         if ( o1 == null && o2 == null )
         {
             return 0;
         }
 
-        if ( o1 == null && o2 != null )
+        if ( o1 == null )
         {
             return 1;
         }
 
-        if ( o1 != null && o2 == null )
+        if ( o2 == null )
         {
             return -1;
         }
 
-        if ( ( o1 instanceof String ) && ( o2 instanceof String ) )
-        {
-            String s1 = ( (String) o1 );
-            String s2 = ( (String) o2 );
-
-            String parts1[] = toParts( s1 );
-            String parts2[] = toParts( s2 );
-
-            int diff;
-            int partLen = Math.max( parts1.length, parts2.length );
-            for ( int i = 0; i < partLen; i++ )
-            {
-                diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
-                if ( diff != 0 )
-                {
-                    return diff;
-                }
-            }
-
-            diff = parts2.length - parts1.length;
+        String[] parts1 = toParts( o1 );
+        String[] parts2 = toParts( o2 );
 
+        int diff;
+        int partLen = Math.max( parts1.length, parts2.length );
+        for ( int i = 0; i < partLen; i++ )
+        {
+            diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
             if ( diff != 0 )
             {
                 return diff;
             }
+        }
 
-            return s1.compareToIgnoreCase( s2 );
+        diff = parts2.length - parts1.length;
+
+        if ( diff != 0 )
+        {
+            return diff;
         }
 
-        return 0;
+        return o1.compareToIgnoreCase( o2 );
     }
 
-    private String safePart( String parts[], int idx )
+    private String safePart( String[] parts, int idx )
     {
         if ( idx < parts.length )
         {
@@ -177,7 +169,7 @@ public class VersionComparator
 
             // Only operate perform index based operation, if both strings
             // are found in the specialWords index.
-            if ( ( idx1 >= 0 ) && ( idx2 >= 0 ) )
+            if ( idx1 >= 0 && idx2 >= 0 )
             {
                 return idx1 - idx2;
             }
@@ -206,11 +198,11 @@ public class VersionComparator
             return ArrayUtils.EMPTY_STRING_ARRAY;
         }
 
-        final int modeOther = 0;
-        final int modeDigit = 1;
-        final int modeText = 2;
+        int modeOther = 0;
+        int modeDigit = 1;
+        int modeText = 2;
 
-        List parts = new ArrayList();
+        List<String> parts = new ArrayList<String>();
         int len = version.length();
         int i = 0;
         int start = 0;
@@ -249,10 +241,7 @@ public class VersionComparator
                 // Other.
                 if ( mode != modeOther )
                 {
-                    if ( mode != modeOther )
-                    {
-                        parts.add( version.substring( start, i ) );
-                    }
+                    parts.add( version.substring( start, i ) );
                     mode = modeOther;
                 }
             }
@@ -266,6 +255,6 @@ public class VersionComparator
             parts.add( version.substring( start, i ) );
         }
 
-        return (String[]) parts.toArray( new String[parts.size()] );
+        return parts.toArray( new String[parts.size()] );
     }
 }
index 2d5873bffedf53f5d0c06368d99bf58ad1a5dd0e..b9ea5039dba34c58593a1c14718125c01357d7e2 100644 (file)
@@ -46,6 +46,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -577,6 +578,8 @@ public class MetadataTools
     public void updateMetadata( ArchivaRepository managedRepository, ProjectReference reference )
         throws LayoutException, RepositoryMetadataException, IOException
     {
+        Comparator<String> comparator = VersionComparator.getInstance();
+
         File metadataFile = new File( managedRepository.getUrl().getPath(), toPath( reference ) );
 
         ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
@@ -589,6 +592,8 @@ public class MetadataTools
         // Does this repository have a set of remote proxied repositories?
         Set proxiedRepoIds = this.proxies.get( managedRepository.getId() );
 
+        String latestVersion = null;
+        String releaseVersion = null;
         if ( proxiedRepoIds != null )
         {
             // Add in the proxied repo version ids too.
@@ -598,13 +603,22 @@ public class MetadataTools
                 String proxyId = it.next();
 
                 ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
-                if ( proxyMetadata == null )
+                if ( proxyMetadata != null )
                 {
-                    // There is no proxy metadata, skip it.
-                    continue;
-                }
+                    availableVersions.addAll( proxyMetadata.getAvailableVersions() );
+
+                    if ( latestVersion == null ||
+                        comparator.compare( proxyMetadata.getLatestVersion(), latestVersion ) > 0 )
+                    {
+                        latestVersion = proxyMetadata.getLatestVersion();
+                    }
 
-                availableVersions.addAll( proxyMetadata.getAvailableVersions() );
+                    if ( releaseVersion == null ||
+                        comparator.compare( proxyMetadata.getReleasedVersion(), releaseVersion ) > 0 )
+                    {
+                        releaseVersion = proxyMetadata.getReleasedVersion();
+                    }
+                }
             }
         }
 
@@ -621,6 +635,9 @@ public class MetadataTools
         // Add the versions to the metadata model.
         metadata.setAvailableVersions( sortedVersions );
 
+        metadata.setLatestVersion( latestVersion );
+        metadata.setReleasedVersion( releaseVersion );
+
         // Save the metadata model to disk.
         RepositoryMetadataWriter.write( metadata, metadataFile );
     }
index 8dda32182bfc50c38853612908441bf410f875aa..df041583ee16a86ba9b1df72fde36759cd6ae2a2 100644 (file)
@@ -155,7 +155,7 @@ public class MetadataToolsTest
         assertUpdatedProjectMetadata( "proxied_multi", new String[]{"1.0-spec" /* in java.net */, "1.0"
             /* in managed, and central */, "1.0.1" /* in central */, "1.1" /* in managed */, "2.0-proposal-beta"
             /* in java.net */, "2.0-spec" /* in java.net */, "2.0" /* in central, and java.net */, "2.0.1"
-            /* in java.net */, "2.1" /* in managed */, "3.0" /* in central */, "3.1" /* in central */} );
+            /* in java.net */, "2.1" /* in managed */, "3.0" /* in central */, "3.1" /* in central */}, "3.1", "3.1" );
     }
 
     public void testUpdateProjectSimpleYetIncomplete()
@@ -387,6 +387,13 @@ public class MetadataToolsTest
 
     private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
         throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
+    {
+        assertUpdatedProjectMetadata( artifactId, expectedVersions, null, null );
+    }
+
+    private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions, String latestVersion,
+                                               String releaseVersion )
+        throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
     {
         ArchivaRepository testRepo = createTestRepo();
         ProjectReference reference = new ProjectReference();
@@ -406,6 +413,15 @@ public class MetadataToolsTest
         if ( expectedVersions != null )
         {
             buf.append( "  <versioning>\n" );
+            if ( latestVersion != null )
+            {
+                buf.append( "    <latest>" ).append( latestVersion ).append( "</latest>\n" );
+            }
+            if ( releaseVersion != null )
+            {
+                buf.append( "    <release>" ).append( releaseVersion ).append( "</release>\n" );
+            }
+
             buf.append( "    <versions>\n" );
             for ( int i = 0; i < expectedVersions.length; i++ )
             {