From 8b19e8e2abffe24fdc4860a60539b965b571574f Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 13 Sep 2007 02:20:48 +0000 Subject: [PATCH] [MRM-463] fix merging of latest/release fields in metadata git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@575145 13f79535-47bb-0310-9956-ffa450edef68 --- .../common/utils/VersionComparator.java | 91 ++++++++----------- .../repository/metadata/MetadataTools.java | 27 +++++- .../metadata/MetadataToolsTest.java | 18 +++- .../proxied_multi/maven-metadata-central.xml | 2 + .../proxied_multi/maven-metadata-java.net.xml | 2 + .../tests/proxied_multi/maven-metadata.xml | 2 + 6 files changed, 85 insertions(+), 57 deletions(-) diff --git a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionComparator.java b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionComparator.java index 597b93c9a..9acd26869 100644 --- a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionComparator.java +++ b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionComparator.java @@ -29,20 +29,20 @@ import java.util.List; /** * VersionComparator - compare the parts of two version strings. - * + *

* Technique. - * - * * Split the version strings into parts by splitting on "-._" first, then breaking apart words from numbers. - * + *

+ * * Split the version strings into parts by splitting on "-._" first, then breaking apart words from numbers. + *

* - * "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" * - * + *

* compare each part individually, and when they do not match, perform the following test. - * + *

* 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 { - private static Comparator INSTANCE = new VersionComparator(); + private static Comparator INSTANCE = new VersionComparator(); - private List specialWords; + private List specialWords; public VersionComparator() { - specialWords = new ArrayList(); + specialWords = new ArrayList(); // 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 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 parts = new ArrayList(); 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()] ); } } diff --git a/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java b/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java index 2d5873bff..b9ea5039d 100644 --- a/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java +++ b/archiva-base/archiva-repository-layer/src/main/java/org/apache/maven/archiva/repository/metadata/MetadataTools.java @@ -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 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 ); } diff --git a/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.java b/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.java index 8dda32182..df041583e 100644 --- a/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.java +++ b/archiva-base/archiva-repository-layer/src/test/java/org/apache/maven/archiva/repository/metadata/MetadataToolsTest.java @@ -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( " \n" ); + if ( latestVersion != null ) + { + buf.append( " " ).append( latestVersion ).append( "\n" ); + } + if ( releaseVersion != null ) + { + buf.append( " " ).append( releaseVersion ).append( "\n" ); + } + buf.append( " \n" ); for ( int i = 0; i < expectedVersions.length; i++ ) { diff --git a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-central.xml b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-central.xml index b0dfc33e5..a8b764bd7 100644 --- a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-central.xml +++ b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-central.xml @@ -3,6 +3,8 @@ proxied_multi 1.0 + 3.1 + 3.1 1.0 1.0.1 diff --git a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-java.net.xml b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-java.net.xml index 481986dcd..13024c74e 100644 --- a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-java.net.xml +++ b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata-java.net.xml @@ -3,6 +3,8 @@ proxied_multi 1.0 + 2.0.1 + 2.0.1 1.0-spec 2.0 diff --git a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata.xml b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata.xml index 091227452..a1e402c94 100644 --- a/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata.xml +++ b/archiva-base/archiva-repository-layer/src/test/repositories/metadata-repository/org/apache/archiva/metadata/tests/proxied_multi/maven-metadata.xml @@ -3,6 +3,8 @@ proxied_multi 1.0 + 2.1 + 2.1 1.0 1.1 -- 2.39.5