[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
This commit is contained in:
Brett Porter 2007-09-13 02:20:48 +00:00
parent 7c2f63c4f5
commit 8b19e8e2ab
6 changed files with 86 additions and 58 deletions

View 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[] parts1 = toParts( o1 );
String[] parts2 = toParts( o2 );
int diff;
int partLen = Math.max( parts1.length, parts2.length );
for ( int i = 0; i < partLen; i++ )
{
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;
diff = comparePart( safePart( parts1, i ), safePart( parts2, i ) );
if ( diff != 0 )
{
return diff;
}
return s1.compareToIgnoreCase( s2 );
}
return 0;
diff = parts2.length - parts1.length;
if ( diff != 0 )
{
return diff;
}
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()] );
}
}

View 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() );
availableVersions.addAll( proxyMetadata.getAvailableVersions() );
if ( latestVersion == null ||
comparator.compare( proxyMetadata.getLatestVersion(), latestVersion ) > 0 )
{
latestVersion = proxyMetadata.getLatestVersion();
}
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 );
}

View 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++ )
{

View File

@ -3,6 +3,8 @@
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<latest>3.1</latest>
<release>3.1</release>
<versions>
<version>1.0</version>
<version>1.0.1</version>

View File

@ -3,6 +3,8 @@
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<latest>2.0.1</latest>
<release>2.0.1</release>
<versions>
<version>1.0-spec</version>
<version>2.0</version>

View File

@ -3,6 +3,8 @@
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<latest>2.1</latest>
<release>2.1</release>
<versions>
<version>1.0</version>
<version>1.1</version>