import java.util.regex.Matcher;
/**
- * MetadataTools
+ * MetadataTools
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
- *
* @plexus.component role="org.apache.maven.archiva.repository.metadata.MetadataTools"
+ * @todo use the maven-repository-metadata classes instead for merging
*/
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 );
public static final String MAVEN_METADATA = "maven-metadata.xml";
private Map<String, Set<String>> proxies;
- private static final char NUMS[] = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
+ private static final char NUMS[] = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
{
/**
* 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
+ * @throws LayoutException
*/
public Set<String> gatherAvailableVersions( ArchivaRepository managedRepository, ProjectReference reference )
throws LayoutException, IOException
if ( !repoDir.exists() )
{
- throw new IOException( "Unable to calculate Available Local Versions on a non-existant directory: "
- + repoDir.getAbsolutePath() );
+ 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() );
+ throw new IOException(
+ "Unable to calculate Available Local Versions on a non-directory: " + repoDir.getAbsolutePath() );
}
Set<String> foundVersions = new HashSet<String>();
}
/**
- * Get the first Artifact found in the provided VersionedReference location.
- *
+ * 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
+ * @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
+ * @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
if ( !repoDir.exists() )
{
- throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
- + repoDir.getAbsolutePath() );
+ 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() );
+ throw new IOException(
+ "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
}
File repoFiles[] = repoDir.listFiles();
/**
* Gather the set of snapshot versions found in a particular versioned reference.
- *
+ *
* @return the Set of snapshot artifact versions found.
- * @throws LayoutException
+ * @throws LayoutException
*/
public Set<String> gatherSnapshotVersions( ArchivaRepository managedRepository, VersionedReference reference )
throws LayoutException, IOException
if ( !repoDir.exists() )
{
- throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
- + repoDir.getAbsolutePath() );
+ 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() );
+ throw new IOException(
+ "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
}
Set<String> foundVersions = new HashSet<String>();
}
/**
- * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
- *
+ * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
+ *
* @param path
* @return
*/
{
// Scary check, but without it, all paths are version references;
throw new RepositoryMetadataException(
- "Not a versioned reference, as version id on path has no number in it." );
+ "Not a versioned reference, as version id on path has no number in it." );
}
reference.setArtifactId( pathParts[artifactIdOffset] );
}
/**
- * Adjusts a path for a metadata.xml file to its repository specific path.
- *
+ * 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.
- *
+ * @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 )
}
/**
- * Adjusts a path for a metadata.xml file to its repository specific 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.
- *
+ * @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 )
configuration.addChangeListener( this );
}
- public ArchivaRepositoryMetadata readProxyMetadata( ArchivaRepository managedRepository,
- ProjectReference reference, String proxyId )
+ public ArchivaRepositoryMetadata readProxyMetadata( ArchivaRepository managedRepository, ProjectReference reference,
+ String proxyId )
{
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
File metadataFile = new File( managedRepository.getUrl().getPath(), metadataPath );
}
/**
- * Update the metadata to represent the all versions of
+ * 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.
- *
+ * 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
+ * @param reference the versioned referencfe to update.
+ * @throws LayoutException
+ * @throws RepositoryMetadataException
+ * @throws IOException
*/
public void updateMetadata( ArchivaRepository managedRepository, ProjectReference reference )
throws LayoutException, RepositoryMetadataException, IOException
/**
* 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
- *
+ * <p/>
+ * 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
+ * @param reference the versioned reference to update
+ * @throws LayoutException
+ * @throws RepositoryMetadataException
+ * @throws IOException
*/
public void updateMetadata( ArchivaRepository managedRepository, VersionedReference reference )
throws LayoutException, RepositoryMetadataException, IOException
}
else
{
- throw new RepositoryMetadataException( "Unable to process snapshot version <" + latestVersion
- + "> reference <" + reference + ">" );
+ throw new RepositoryMetadataException(
+ "Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">" );
}
}
else
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.policies.DownloadPolicy;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.codehaus.plexus.PlexusTestCase;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.XMLAssert;
import org.xml.sax.SAXException;
+import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
-import javax.xml.parsers.ParserConfigurationException;
-
/**
- * MetadataToolsTest
+ * MetadataToolsTest
*
* @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
* @version $Id$
public void testGatherAvailableVersionsBadArtifact()
throws Exception
{
- assertAvailableVersions( "bad_artifact", new String[] {} );
+ assertAvailableVersions( "bad_artifact", Collections.EMPTY_LIST );
}
public void testGatherAvailableVersionsMissingMultipleVersions()
throws Exception
{
- assertAvailableVersions( "missing_metadata_b", new String[] {
- "1.0",
- "1.0.1",
- "2.0",
- "2.0.1",
- "2.0-20070821-dev" } );
+ assertAvailableVersions( "missing_metadata_b",
+ Arrays.asList( "1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev" ) );
}
public void testGatherAvailableVersionsSimpleYetIncomplete()
throws Exception
{
- assertAvailableVersions( "incomplete_metadata_a", new String[] { "1.0" } );
+ assertAvailableVersions( "incomplete_metadata_a", Collections.singletonList( "1.0" ) );
}
public void testGatherAvailableVersionsSimpleYetMissing()
throws Exception
{
- assertAvailableVersions( "missing_metadata_a", new String[] { "1.0" } );
+ assertAvailableVersions( "missing_metadata_a", Collections.singletonList( "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" } );
+ 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 testGatherSnapshotVersionsAWithProxies()
createProxyConnector( "test-repo", "internal-snapshots" );
createProxyConnector( "test-repo", "snapshots.codehaus.org" );
- 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",
+ 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-20070315.033030-10" /* Arrives in via snapshots.codehaus.org proxy */,
- "1.0-alpha-11-20070316.175232-11" } );
+ "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/" );
+ 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" );
+ String 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 );
public void testUpdateProjectMissingMultipleVersions()
throws Exception
{
- assertUpdatedProjectMetadata( "missing_metadata_b", new String[] {
- "1.0",
- "1.0.1",
- "2.0",
- "2.0.1",
- "2.0-20070821-dev" } );
+ assertUpdatedProjectMetadata( "missing_metadata_b",
+ new String[]{"1.0", "1.0.1", "2.0", "2.0.1", "2.0-20070821-dev"} );
}
public void testUpdateProjectMissingMultipleVersionsWithProxies()
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 */} );
+ 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" } );
+ assertUpdatedProjectMetadata( "incomplete_metadata_a", new String[]{"1.0"} );
}
public void testUpdateProjectSimpleYetMissing()
throws Exception
{
- assertUpdatedProjectMetadata( "missing_metadata_a", new String[] { "1.0" } );
+ assertUpdatedProjectMetadata( "missing_metadata_a", new String[]{"1.0"} );
}
public void testUpdateVersionSimple10()
assertEquals( "VersionedReference.version", version, reference.getVersion() );
}
- private void assertAvailableVersions( String artifactId, String[] expectedVersions )
+ private void assertAvailableVersions( String artifactId, List<String> expectedVersions )
throws Exception
{
File repoRootDir = new File( "src/test/repositories/metadata-repository" );
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 );
- }
+ assertEquals( "available versions", expectedVersions, testedVersions );
}
private void assertSnapshotVersions( String artifactId, String version, String[] expectedVersions )
for ( int i = 0; i < expectedVersions.length; i++ )
{
- String actualVersion = (String) testedVersions.get( i );
+ String actualVersion = testedVersions.get( i );
assertEquals( "Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion );
}
}
assertEquals( "Repository Specific Metadata Path", expected, actual );
}
- private void assertUpdatedProjectMetadata( String artifactId, String expectedVersions[] )
+ private void assertUpdatedProjectMetadata( String artifactId, String[] expectedVersions )
throws IOException, LayoutException, RepositoryMetadataException, SAXException, ParserConfigurationException
{
ArchivaRepository testRepo = createTestRepo();
tools.updateMetadata( testRepo, reference );
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
tools.updateMetadata( testRepo, reference );
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
tools.updateMetadata( testRepo, reference );
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( reference.getArtifactId() ).append( "</artifactId>\n" );
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 );
+ connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, DownloadPolicy.IGNORED );
+ connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, DownloadPolicy.IGNORED );
+ connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, DownloadPolicy.IGNORED );
+ connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, DownloadPolicy.IGNORED );
int count = config.getConfiguration().getProxyConnectors().size();
config.getConfiguration().addProxyConnector( connectorConfig );
repoRoot.mkdirs();
String repoRootURL = PathUtil.toUrl( repoRoot );
- ArchivaRepository repo = new ArchivaRepository( "test-repo", "Test Repository: " + getName(), repoRootURL );
- return repo;
+ return new ArchivaRepository( "test-repo", "Test Repository: " + getName(), repoRootURL );
}
private void prepTestRepo( ArchivaRepository repo, ProjectReference reference )