[MRM-463] Metadata merging doesn't work.

Baseline Components for metadata merging.



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@569760 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-08-26 03:26:00 +00:00
parent 05d0e9e6f5
commit 8887f9cbe9
128 changed files with 1336 additions and 7 deletions

View File

@ -0,0 +1,580 @@
package org.apache.maven.archiva.repository.metadata;
/*
* Copyright 2001-2007 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.lang.math.NumberUtils;
import org.apache.maven.archiva.common.utils.PathUtil;
import org.apache.maven.archiva.common.utils.VersionComparator;
import org.apache.maven.archiva.common.utils.VersionUtil;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ConfigurationNames;
import org.apache.maven.archiva.configuration.FileTypes;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.maven.archiva.model.ArchivaRepository;
import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.model.ProjectReference;
import org.apache.maven.archiva.model.SnapshotVersion;
import org.apache.maven.archiva.model.VersionedReference;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryListener;
import org.codehaus.plexus.util.SelectorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
/**
* MetadataTools
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.repository.metadata.MetadataTools"
*/
public class MetadataTools
implements RegistryListener, Initializable
{
/**
* Static Logger. So what? Try and prove to me that IoC monitors are better.
*/
private static Logger log = LoggerFactory.getLogger( MetadataTools.class );
/**
* @plexus.requirement
*/
private BidirectionalRepositoryLayoutFactory layoutFactory;
/**
* @plexus.requirement
*/
private ArchivaConfiguration configuration;
/**
* @plexus.requirement
*/
private FileTypes filetypes;
private List<String> artifactPatterns;
private Map<String, Set<String>> proxies;
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
{
if ( ConfigurationNames.isProxyConnector( propertyName ) )
{
initConfigVariables();
}
}
public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
{
/* nothing to do */
}
/**
* Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
* information.
*
* @return the Set of available versions, based on the project reference.
* @throws LayoutException
*/
public Set<String> gatherAvailableVersions( ArchivaRepository managedRepository, ProjectReference reference )
throws LayoutException, IOException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
String path = layout.toPath( reference );
int idx = path.lastIndexOf( '/' );
if ( idx > 0 )
{
path = path.substring( 0, idx );
}
File repoDir = new File( managedRepository.getUrl().getPath(), path );
if ( !repoDir.exists() )
{
throw new IOException( "Unable to calculate Available Local Versions on a non-existant directory: "
+ repoDir.getAbsolutePath() );
}
if ( !repoDir.isDirectory() )
{
throw new IOException( "Unable to calculate Available Local Versions on a non-directory: "
+ repoDir.getAbsolutePath() );
}
Set<String> foundVersions = new HashSet<String>();
// TODO: should really determine if valid based on artifactPatterns, not POM existance.
// this method was written before the gatherSnapshotVersions() method, consider
// using the routines from that method used to determine artifacts via pattern.
ArtifactReference pomReference = new ArtifactReference();
pomReference.setGroupId( reference.getGroupId() );
pomReference.setArtifactId( reference.getArtifactId() );
pomReference.setType( "pom" );
File repoFiles[] = repoDir.listFiles();
for ( int i = 0; i < repoFiles.length; i++ )
{
if ( !repoFiles[i].isDirectory() )
{
// Skip it. not a directory.
continue;
}
// Test if dir has pom, which proves to us that it is a valid version directory.
String version = repoFiles[i].getName();
pomReference.setVersion( version );
File artifactFile = new File( managedRepository.getUrl().getPath(), layout.toPath( pomReference ) );
if ( artifactFile.exists() )
{
// Found a pom, must be a valid version.
foundVersions.add( version );
}
}
return foundVersions;
}
/**
* Get the first Artifact found in the provided VersionedReference location.
*
* @param managedRepository the repository to search within.
* @param reference the reference to the versioned reference to search within
* @return the ArtifactReference to the first artifact located within the versioned reference. or null if
* no artifact was found within the versioned reference.
* @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
* @throws LayoutException
*/
public ArtifactReference getFirstArtifact( ArchivaRepository managedRepository, VersionedReference reference )
throws LayoutException, IOException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
String path = layout.toPath( reference );
int idx = path.lastIndexOf( '/' );
if ( idx > 0 )
{
path = path.substring( 0, idx );
}
File repoDir = new File( managedRepository.getUrl().getPath(), path );
if ( !repoDir.exists() )
{
throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
+ repoDir.getAbsolutePath() );
}
if ( !repoDir.isDirectory() )
{
throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
+ repoDir.getAbsolutePath() );
}
File repoFiles[] = repoDir.listFiles();
for ( int i = 0; i < repoFiles.length; i++ )
{
if ( repoFiles[i].isDirectory() )
{
// Skip it. it's a directory.
continue;
}
String relativePath = PathUtil.getRelative( managedRepository.getUrl().getPath(), repoFiles[i] );
if ( matchesArtifactPattern( relativePath ) )
{
ArtifactReference artifact = layout.toArtifactReference( relativePath );
return artifact;
}
}
// No artifact was found.
return null;
}
/**
* Gather the set of snapshot versions found in a particular versioned reference.
*
* @return the Set of snapshot artifact versions found.
* @throws LayoutException
*/
public Set<String> gatherSnapshotVersions( ArchivaRepository managedRepository, VersionedReference reference )
throws LayoutException, IOException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
String path = layout.toPath( reference );
int idx = path.lastIndexOf( '/' );
if ( idx > 0 )
{
path = path.substring( 0, idx );
}
File repoDir = new File( managedRepository.getUrl().getPath(), path );
if ( !repoDir.exists() )
{
throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
+ repoDir.getAbsolutePath() );
}
if ( !repoDir.isDirectory() )
{
throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
+ repoDir.getAbsolutePath() );
}
Set<String> foundVersions = new HashSet<String>();
File repoFiles[] = repoDir.listFiles();
for ( int i = 0; i < repoFiles.length; i++ )
{
if ( repoFiles[i].isDirectory() )
{
// Skip it. it's a directory.
continue;
}
String relativePath = PathUtil.getRelative( managedRepository.getUrl().getPath(), repoFiles[i] );
if ( matchesArtifactPattern( relativePath ) )
{
ArtifactReference artifact = layout.toArtifactReference( relativePath );
if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
{
foundVersions.add( artifact.getVersion() );
}
}
}
return foundVersions;
}
private boolean matchesArtifactPattern( String relativePath )
{
Iterator<String> it = this.artifactPatterns.iterator();
while ( it.hasNext() )
{
String pattern = it.next();
if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
{
// Found match
return true;
}
}
// No match.
return false;
}
/**
* Adjusts a path for a metadata.xml file to its repository specific path.
*
* @param repository the repository to base new path off of.
* @param path the path to the metadata.xml file to adjust the name of.
*
* @return the newly adjusted path reference to the repository specific metadata path.
*/
public String getRepositorySpecificName( ArchivaRepository repository, String path )
{
return getRepositorySpecificName( repository.getId(), path );
}
/**
* Adjusts a path for a metadata.xml file to its repository specific path.
*
* @param proxyId the repository id to base new path off of.
* @param path the path to the metadata.xml file to adjust the name of.
*
* @return the newly adjusted path reference to the repository specific metadata path.
*/
public String getRepositorySpecificName( String proxyId, String path )
{
StringBuffer ret = new StringBuffer();
int idx = path.lastIndexOf( "/" );
if ( idx > 0 )
{
ret.append( path.substring( 0, idx + 1 ) );
}
// TODO: need to filter out 'bad' characters from the proxy id.
ret.append( "maven-metadata-" ).append( proxyId ).append( ".xml" );
return ret.toString();
}
public void initialize()
throws InitializationException
{
this.artifactPatterns = new ArrayList<String>();
this.proxies = new HashMap();
initConfigVariables();
configuration.addChangeListener( this );
}
public ArchivaRepositoryMetadata readProxyMetadata( ArchivaRepository managedRepository,
ProjectReference reference, String proxyId )
throws LayoutException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
String metadataPath = getRepositorySpecificName( proxyId, layout.toPath( reference ) );
File metadataFile = new File( managedRepository.getUrl().getPath(), metadataPath );
try
{
return RepositoryMetadataReader.read( metadataFile );
}
catch ( RepositoryMetadataException e )
{
// TODO: [monitor] consider a monitor for this event.
// TODO: consider a read-redo on monitor return code?
log.warn( "Unable to read metadata: " + metadataFile.getAbsolutePath(), e );
return null;
}
}
/**
* Update the metadata to represent the all versions of
* the provided groupId:artifactId project reference,
* based off of information present in the repository,
* the maven-metadata.xml files, and the proxy/repository specific
* metadata file contents.
*
* @param managedRepository the managed repository where the metadata is kept.
* @param reference the versioned referencfe to update.
* @throws LayoutException
* @throws RepositoryMetadataException
* @throws IOException
*/
public void updateMetadata( ArchivaRepository managedRepository, ProjectReference reference )
throws LayoutException, RepositoryMetadataException, IOException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
File metadataFile = new File( managedRepository.getUrl().getPath(), layout.toPath( reference ) );
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId( reference.getGroupId() );
metadata.setArtifactId( reference.getArtifactId() );
// Gather up the versions found in the managed repository.
Set<String> availableVersions = gatherAvailableVersions( managedRepository, reference );
// Does this repository have a set of remote proxied repositories?
Set proxiedRepoIds = this.proxies.get( managedRepository.getId() );
if ( proxiedRepoIds != null )
{
// Add in the proxied repo version ids too.
Iterator<String> it = proxiedRepoIds.iterator();
while ( it.hasNext() )
{
String proxyId = it.next();
ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata( managedRepository, reference, proxyId );
if ( proxyMetadata == null )
{
// There is no proxy metadata, skip it.
continue;
}
availableVersions.addAll( proxyMetadata.getAvailableVersions() );
}
}
if ( availableVersions.size() == 0 )
{
throw new IOException( "No versions found for reference." );
}
// Sort the versions
List<String> sortedVersions = new ArrayList<String>();
sortedVersions.addAll( availableVersions );
Collections.sort( sortedVersions, new VersionComparator() );
// Add the versions to the metadata model.
metadata.setAvailableVersions( sortedVersions );
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
}
/**
* Update the metadata based on the following rules.
*
* 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
* metadata files to represent the current / latest SNAPSHOT available.
* 2) If this is a RELEASE reference, and the metadata file does not exist, then
* create the metadata file with contents required of the VersionedReference
*
* @param managedRepository the managed repository where the metadata is kept.
* @param reference the versioned reference to update
* @throws LayoutException
* @throws RepositoryMetadataException
* @throws IOException
*/
public void updateMetadata( ArchivaRepository managedRepository, VersionedReference reference )
throws LayoutException, RepositoryMetadataException, IOException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( managedRepository.getLayoutType() );
File metadataFile = new File( managedRepository.getUrl().getPath(), layout.toPath( reference ) );
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
metadata.setGroupId( reference.getGroupId() );
metadata.setArtifactId( reference.getArtifactId() );
if ( VersionUtil.isSnapshot( reference.getVersion() ) )
{
// Do SNAPSHOT handling.
metadata.setVersion( VersionUtil.getBaseVersion( reference.getVersion() ) );
// Gather up all of the versions found in the reference dir.
Set snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
if ( snapshotVersions.isEmpty() )
{
throw new IOException( "Not snapshot versions found to reference." );
}
// sort the list to determine to aide in determining the Latest version.
List<String> sortedVersions = new ArrayList<String>();
sortedVersions.addAll( snapshotVersions );
Collections.sort( sortedVersions, new VersionComparator() );
String latestVersion = sortedVersions.get( sortedVersions.size() - 1 );
if ( VersionUtil.isUniqueSnapshot( latestVersion ) )
{
// The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
// This needs to be broken down into ${base}-${timestamp}-${build_number}
Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher( latestVersion );
if ( m.matches() )
{
metadata.setSnapshotVersion( new SnapshotVersion() );
int buildNumber = NumberUtils.toInt( m.group( 3 ), -1 );
metadata.getSnapshotVersion().setBuildNumber( buildNumber );
Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher( m.group( 2 ) );
if ( mtimestamp.matches() )
{
String tsDate = mtimestamp.group( 1 );
String tsTime = mtimestamp.group( 2 );
metadata.setLastUpdated( tsDate + tsTime );
metadata.getSnapshotVersion().setTimestamp( m.group( 2 ) );
}
}
}
else if ( VersionUtil.isGenericSnapshot( latestVersion ) )
{
// The latestVersion ends with the generic version string.
// Example: 1.0-alpha-5-SNAPSHOT
metadata.setSnapshotVersion( new SnapshotVersion() );
/* TODO: Should this be the last updated timestamp of the file, or in the case of an
* archive, the most recent timestamp in the archive?
*/
ArtifactReference artifact = getFirstArtifact( managedRepository, reference );
if ( artifact == null )
{
throw new IOException( "Not snapshot artifact found to reference in " + reference );
}
File artifactFile = new File( managedRepository.getUrl().getPath(), layout.toPath( artifact ) );
if( artifactFile.exists() )
{
Date lastModified = new Date( artifactFile.lastModified() );
metadata.setLastUpdatedTimestamp( lastModified );
}
}
else
{
throw new RepositoryMetadataException( "Unable to process snapshot version <" + latestVersion
+ "> reference <" + reference + ">" );
}
}
else
{
// Do RELEASE handling.
metadata.setVersion( reference.getVersion() );
}
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
}
private void initConfigVariables()
{
synchronized ( this.artifactPatterns )
{
this.artifactPatterns.clear();
this.artifactPatterns.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
}
synchronized ( proxies )
{
this.proxies.clear();
List proxyConfigs = configuration.getConfiguration().getProxyConnectors();
Iterator it = proxyConfigs.iterator();
while ( it.hasNext() )
{
ProxyConnectorConfiguration proxyConfig = (ProxyConnectorConfiguration) it.next();
String key = proxyConfig.getSourceRepoId();
Set remoteRepoIds = this.proxies.get( key );
if ( remoteRepoIds == null )
{
remoteRepoIds = new HashSet<String>();
}
remoteRepoIds.add( proxyConfig.getTargetRepoId() );
this.proxies.put( key, remoteRepoIds );
}
}
}
}

View File

@ -44,7 +44,7 @@ public class RepositoryMetadataReader
* @return the archiva repository metadata object that represents the provided file contents.
* @throws RepositoryMetadataException
*/
public ArchivaRepositoryMetadata read( File metadataFile )
public static ArchivaRepositoryMetadata read( File metadataFile )
throws RepositoryMetadataException
{
try

View File

@ -0,0 +1,461 @@
package org.apache.maven.archiva.repository.metadata;
/*
* Copyright 2001-2007 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.common.utils.PathUtil;
import org.apache.maven.archiva.common.utils.VersionComparator;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.maven.archiva.model.ArchivaRepository;
import org.apache.maven.archiva.model.ProjectReference;
import org.apache.maven.archiva.model.VersionedReference;
import org.apache.maven.archiva.policies.CachedFailuresPolicy;
import org.apache.maven.archiva.policies.ChecksumPolicy;
import org.apache.maven.archiva.policies.ReleasesPolicy;
import org.apache.maven.archiva.policies.SnapshotsPolicy;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.codehaus.plexus.PlexusTestCase;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLAssert;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
/**
* MetadataToolsTest
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*/
public class MetadataToolsTest
extends PlexusTestCase
{
private BidirectionalRepositoryLayoutFactory layoutFactory;
private MetadataTools tools;
protected MockConfiguration config;
public void testGatherAvailableVersionsBadArtifact()
throws Exception
{
assertAvailableVersions( "bad_artifact", new String[] {} );
}
public void testGatherAvailableVersionsMissingMultipleVersions()
throws Exception
{
assertAvailableVersions( "missing_metadata_b", new String[] { "1.0", "1.0.1", "2.0", "2.0-20070821-dev" } );
}
public void testGatherAvailableVersionsSimpleYetIncomplete()
throws Exception
{
assertAvailableVersions( "incomplete_metadata_a", new String[] { "1.0" } );
}
public void testGatherAvailableVersionsSimpleYetMissing()
throws Exception
{
assertAvailableVersions( "missing_metadata_a", new String[] { "1.0" } );
}
public void testGatherSnapshotVersionsA()
throws Exception
{
assertSnapshotVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", new String[] {
"1.0-alpha-11-SNAPSHOT",
"1.0-alpha-11-20070221.194724-2",
"1.0-alpha-11-20070302.212723-3",
"1.0-alpha-11-20070303.152828-4",
"1.0-alpha-11-20070305.215149-5",
"1.0-alpha-11-20070307.170909-6",
"1.0-alpha-11-20070314.211405-9",
"1.0-alpha-11-20070316.175232-11" } );
}
public void testGetRepositorySpecificName()
{
ArchivaRepository repoJavaNet = new ArchivaRepository( "maven2-repository.dev.java.net",
"Java.net Repository for Maven 2",
"http://download.java.net/maven/2/" );
ArchivaRepository repoCentral = new ArchivaRepository( "central", "Central Global Repository",
"http://repo1.maven.org/maven2/" );
String convertedName;
convertedName = tools.getRepositorySpecificName( repoJavaNet, "commons-lang/commons-lang/maven-metadata.xml" );
assertMetadataPath( "commons-lang/commons-lang/maven-metadata-maven2-repository.dev.java.net.xml",
convertedName );
convertedName = tools.getRepositorySpecificName( repoCentral, "commons-lang/commons-lang/maven-metadata.xml" );
assertMetadataPath( "commons-lang/commons-lang/maven-metadata-central.xml", convertedName );
}
public void testUpdateProjectBadArtifact()
throws LayoutException, SAXException, ParserConfigurationException, RepositoryMetadataException
{
try
{
assertUpdatedProjectMetadata( "bad_artifact", null );
fail( "Should have thrown an IOException on a bad artifact." );
}
catch ( IOException e )
{
// Expected path
}
}
public void testUpdateProjectMissingMultipleVersions()
throws Exception
{
assertUpdatedProjectMetadata( "missing_metadata_b", new String[] { "1.0", "1.0.1", "2.0", "2.0-20070821-dev" } );
}
public void testUpdateProjectMissingMultipleVersionsWithProxies()
throws Exception
{
// Attach the (bogus) proxies to the managed repo.
// These proxied repositories do not need to exist for the purposes of this unit test,
// just the repository ids are important.
createProxyConnector( "test-repo", "central" );
createProxyConnector( "test-repo", "java.net" );
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 */} );
}
public void testUpdateProjectSimpleYetIncomplete()
throws Exception
{
assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[] { "1.0" } );
}
public void testUpdateProjectSimpleYetMissing()
throws Exception
{
assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" } );
}
public void testUpdateVersionSimple10()
throws Exception
{
assertUpdatedReleaseVersionMetadata( "missing_metadata_a", "1.0" );
}
public void testUpdateVersionSimple20()
throws Exception
{
assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0" );
}
public void testUpdateVersionSimple20NotSnapshot()
throws Exception
{
assertUpdatedReleaseVersionMetadata( "missing_metadata_b", "2.0-20070821-dev" );
}
public void testUpdateVersionSnapshotA()
throws Exception
{
assertUpdatedSnapshotVersionMetadata( "snap_shots_a", "1.0-alpha-11-SNAPSHOT", "20070316", "175232", "11" );
}
private void assertAvailableVersions( String artifactId, String[] expectedVersions )
throws Exception
{
File repoRootDir = new File( "src/test/repositories/metadata-repository" );
ProjectReference reference = new ProjectReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
String repoRootURL = PathUtil.toUrl( repoRootDir );
ArchivaRepository repo = new ArchivaRepository( "test-repo", "Test Repository: " + getName(), repoRootURL );
Set<String> testedVersionSet = tools.gatherAvailableVersions( repo, reference );
// Sort the list (for asserts)
List<String> testedVersions = new ArrayList<String>();
testedVersions.addAll( testedVersionSet );
Collections.sort( testedVersions, new VersionComparator() );
// Test the expected array of versions, to the actual tested versions
assertEquals( "Assert Available Versions: length/size", expectedVersions.length, testedVersions.size() );
for ( int i = 0; i < expectedVersions.length; i++ )
{
String actualVersion = (String) testedVersions.get( i );
assertEquals( "Available Versions[" + i + "]", expectedVersions[i], actualVersion );
}
}
private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
throws Exception
{
File repoRootDir = new File( "src/test/repositories/metadata-repository" );
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
String repoRootURL = PathUtil.toUrl( repoRootDir );
ArchivaRepository repo = new ArchivaRepository( "test-repo", "Test Repository: " + getName(), repoRootURL );
Set<String> testedVersionSet = tools.gatherSnapshotVersions( repo, reference );
// Sort the list (for asserts)
List<String> testedVersions = new ArrayList<String>();
testedVersions.addAll( testedVersionSet );
Collections.sort( testedVersions, new VersionComparator() );
// Test the expected array of versions, to the actual tested versions
assertEquals( "Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size() );
for ( int i = 0; i < expectedVersions.length; i++ )
{
String actualVersion = (String) testedVersions.get( i );
assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
}
}
private void assertMetadata( String expectedMetadata, ArchivaRepository repository, ProjectReference reference )
throws LayoutException, IOException, SAXException, ParserConfigurationException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( repository.getLayoutType() );
File metadataFile = new File( repository.getUrl().getPath(), layout.toPath( reference ) );
String actualMetadata = FileUtils.readFileToString( metadataFile, null );
XMLAssert.assertXMLEqual( expectedMetadata, actualMetadata );
}
private void assertMetadata( String expectedMetadata, ArchivaRepository repository, VersionedReference reference )
throws LayoutException, IOException, SAXException, ParserConfigurationException
{
BidirectionalRepositoryLayout layout = layoutFactory.getLayout( repository.getLayoutType() );
File metadataFile = new File( repository.getUrl().getPath(), layout.toPath( reference ) );
String actualMetadata = FileUtils.readFileToString( metadataFile, null );
DetailedDiff detailedDiff = new DetailedDiff( new Diff( expectedMetadata, actualMetadata ) );
if( !detailedDiff.similar() )
{
assertEquals( expectedMetadata, actualMetadata );
}
// assertTrue( "Metadata is similar: " + detailedDiff, detailedDiff.similar() );
// XMLAssert.assertXMLEqual( expectedMetadata, actualMetadata );
}
private void assertMetadataPath( String expected, String actual )
{
assertEquals( "Repository Specific Metadata Path", expected, actual );
}
private void assertUpdatedProjectMetadata( String artifactId, String expectedVersions[] )
throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
{
ArchivaRepository testRepo = createTestRepo();
ProjectReference reference = new ProjectReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
prepTestRepo( testRepo, reference );
tools.updateMetadata( testRepo, reference );
StringBuffer buf = new StringBuffer();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
// buf.append( " <version>1.0</version>\n" );
if ( expectedVersions != null )
{
buf.append( " <versioning>\n" );
buf.append( " <versions>\n" );
for ( int i = 0; i < expectedVersions.length; i++ )
{
buf.append( " <version>" ).append( expectedVersions[i] ).append( "</version>\n" );
}
buf.append( " </versions>\n" );
buf.append( " </versioning>\n" );
}
buf.append( "</metadata>" );
assertMetadata( buf.toString(), testRepo, reference );
}
private void assertUpdatedReleaseVersionMetadata( String artifactId, String version )
throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
{
ArchivaRepository testRepo = createTestRepo();
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
prepTestRepo( testRepo, reference );
tools.updateMetadata( testRepo, reference );
StringBuffer buf = new StringBuffer();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
buf.append( "</metadata>" );
assertMetadata( buf.toString(), testRepo, reference );
}
private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
String expectedTime, String expectedBuildNumber )
throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
{
ArchivaRepository testRepo = createTestRepo();
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
prepTestRepo( testRepo, reference );
tools.updateMetadata( testRepo, reference );
StringBuffer buf = new StringBuffer();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
buf.append( " <versioning>\n" );
buf.append( " <snapshot>\n" );
buf.append( " <buildNumber>" ).append( expectedBuildNumber ).append( "</buildNumber>\n" );
buf.append( " <timestamp>" );
buf.append( expectedDate ).append( "." ).append( expectedTime );
buf.append( "</timestamp>\n" );
buf.append( " </snapshot>\n" );
buf.append( " <lastUpdated>" ).append( expectedDate ).append( expectedTime ).append( "</lastUpdated>\n" );
buf.append( " </versioning>\n" );
buf.append( "</metadata>" );
assertMetadata( buf.toString(), testRepo, reference );
}
private void createProxyConnector( String sourceRepoId, String targetRepoId )
{
String checksumPolicy = ChecksumPolicy.IGNORED;
String releasePolicy = ReleasesPolicy.IGNORED;
String snapshotPolicy = SnapshotsPolicy.IGNORED;
String cacheFailuresPolicy = CachedFailuresPolicy.IGNORED;
ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
connectorConfig.setSourceRepoId( sourceRepoId );
connectorConfig.setTargetRepoId( targetRepoId );
connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, checksumPolicy );
connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasePolicy );
connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotPolicy );
connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, cacheFailuresPolicy );
int count = config.getConfiguration().getProxyConnectors().size();
config.getConfiguration().addProxyConnector( connectorConfig );
// Proper Triggering ...
String prefix = "proxyConnectors.proxyConnector(" + count + ")";
config.triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
config.triggerChange( prefix + ".targetRepoId", connectorConfig.getTargetRepoId() );
config.triggerChange( prefix + ".proxyId", connectorConfig.getProxyId() );
config.triggerChange( prefix + ".policies.releases", connectorConfig.getPolicy( "releases", "" ) );
config.triggerChange( prefix + ".policies.checksum", connectorConfig.getPolicy( "checksum", "" ) );
config.triggerChange( prefix + ".policies.snapshots", connectorConfig.getPolicy( "snapshots", "" ) );
config.triggerChange( prefix + ".policies.cache-failures", connectorConfig.getPolicy( "cache-failures", "" ) );
}
private ArchivaRepository createTestRepo()
throws IOException
{
File repoRoot = new File( "target/metadata-tests/" + getName() );
if ( repoRoot.exists() )
{
FileUtils.deleteDirectory( repoRoot );
}
repoRoot.mkdirs();
String repoRootURL = PathUtil.toUrl( repoRoot );
ArchivaRepository repo = new ArchivaRepository( "test-repo", "Test Repository: " + getName(), repoRootURL );
return repo;
}
private void prepTestRepo( ArchivaRepository repo, ProjectReference reference )
throws IOException
{
String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
String path = groupDir + "/" + reference.getArtifactId();
File srcRepoDir = new File( "src/test/repositories/metadata-repository" );
File srcDir = new File( srcRepoDir, path );
File destDir = new File( repo.getUrl().getPath(), path );
assertTrue( "Source Dir exists: " + srcDir, srcDir.exists() );
destDir.mkdirs();
FileUtils.copyDirectory( srcDir, destDir );
}
private void prepTestRepo( ArchivaRepository repo, VersionedReference reference )
throws IOException
{
ProjectReference projectRef = new ProjectReference();
projectRef.setGroupId( reference.getGroupId() );
projectRef.setArtifactId( reference.getArtifactId() );
prepTestRepo( repo, projectRef );
}
protected void setUp()
throws Exception
{
super.setUp();
layoutFactory = (BidirectionalRepositoryLayoutFactory) lookup( BidirectionalRepositoryLayoutFactory.class );
config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
tools = (MetadataTools) lookup( MetadataTools.class );
}
}

View File

@ -0,0 +1,91 @@
package org.apache.maven.archiva.repository.metadata;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.registry.RegistryListener;
import org.easymock.MockControl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* MockConfiguration
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.configuration.ArchivaConfiguration"
* role-hint="mock"
*/
public class MockConfiguration
implements ArchivaConfiguration
{
private Configuration configuration = new Configuration();
private List listeners = new ArrayList();
private MockControl registryControl;
private Registry registryMock;
public MockConfiguration()
{
registryControl = MockControl.createNiceControl( Registry.class );
registryMock = (Registry) registryControl.getMock();
}
public void addChangeListener( RegistryListener listener )
{
listeners.add( listener );
}
public Configuration getConfiguration()
{
return configuration;
}
public void save( Configuration configuration )
throws RegistryException
{
/* do nothing */
}
public void triggerChange( String name, String value )
{
Iterator it = listeners.iterator();
while ( it.hasNext() )
{
RegistryListener listener = (RegistryListener) it.next();
try
{
listener.afterConfigurationChange( registryMock, name, value );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
}

View File

@ -37,9 +37,7 @@ public class RepositoryMetadataReaderTest extends PlexusTestCase
File defaultRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
File metadataFile = new File( defaultRepoDir, "org/apache/maven/shared/maven-downloader/maven-metadata.xml" );
RepositoryMetadataReader reader = new RepositoryMetadataReader();
ArchivaRepositoryMetadata metadata = reader.read( metadataFile );
ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "Group Id", "org.apache.maven.shared", metadata.getGroupId() );
@ -55,9 +53,7 @@ public class RepositoryMetadataReaderTest extends PlexusTestCase
File defaultRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
File metadataFile = new File( defaultRepoDir, "org/apache/maven/samplejar/maven-metadata.xml" );
RepositoryMetadataReader reader = new RepositoryMetadataReader();
ArchivaRepositoryMetadata metadata = reader.read( metadataFile );
ArchivaRepositoryMetadata metadata = RepositoryMetadataReader.read( metadataFile );
assertNotNull( metadata );
assertEquals( "Group Id", "org.apache.maven", metadata.getGroupId() );

View File

@ -0,0 +1,5 @@
<metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>incomplete_metadata_a</artifactId>
<version>1.0</version>
</metadata>

View File

@ -0,0 +1,14 @@
<metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<versions>
<version>1.0</version>
<version>1.0.1</version>
<version>2.0</version>
<version>3.0</version>
<version>3.1</version>
</versions>
</versioning>
</metadata>

View File

@ -0,0 +1,14 @@
<metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<versions>
<version>1.0-spec</version>
<version>2.0</version>
<version>2.0-proposal-beta</version>
<version>2.0-spec</version>
<version>2.0.1</version>
</versions>
</versioning>
</metadata>

View File

@ -0,0 +1,12 @@
<metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>proxied_multi</artifactId>
<version>1.0</version>
<versioning>
<versions>
<version>1.0</version>
<version>1.1</version>
<version>2.1</version>
</versions>
</versioning>
</metadata>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>snap_shots_a</artifactId>
<version>1.0-alpha-11-SNAPSHOT</version>
</metadata>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>snap_shots_a</artifactId>
<version>1.0-alpha-11-SNAPSHOT</version>
</metadata>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>snap_shots_a</artifactId>
<version>1.0-alpha-11-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20070316.175232</timestamp>
<buildNumber>11</buildNumber>
</snapshot>
<lastUpdated>20070316175456</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>snap_shots_a</artifactId>
<version>1.0-alpha-11-SNAPSHOT</version>
<versioning>
<snapshot />
<lastUpdated>20070314163205</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?><metadata>
<groupId>org.apache.archiva.metadata.tests</groupId>
<artifactId>snap_shots_b</artifactId>
<version>2.2-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20070316.153953</timestamp>
<buildNumber>10</buildNumber>
</snapshot>
<lastUpdated>20070316153953</lastUpdated>
</versioning>
</metadata>

Some files were not shown because too many files have changed in this diff Show More