API refactoring

This commit is contained in:
Martin Stockhammer 2020-06-17 08:55:04 +02:00
parent 1201ef3974
commit 735a03f2b0
5 changed files with 97 additions and 406 deletions

View File

@ -33,6 +33,7 @@ import org.apache.archiva.repository.RepositoryException;
import org.apache.archiva.repository.RepositoryNotFoundException;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.MetadataTools;
@ -219,26 +220,22 @@ public class MetadataUpdaterConsumer
private void updateVersionMetadata( Artifact artifact, String path )
{
VersionedReference versionRef = new VersionedReference( );
versionRef.setGroupId( artifact.getNamespace( ).getId() );
versionRef.setArtifactId( artifact.getId( ) );
versionRef.setVersion( artifact.getVersion( ).getId() );
try
{
String metadataPath = this.metadataTools.toPath( versionRef );
String metadataPath = this.metadataTools.toPath( artifact.getVersion() );
StorageAsset projectMetadata = this.repositoryDir.resolve( metadataPath );
if ( projectMetadata.exists() && ( projectMetadata.getModificationTime().toEpochMilli() >= this.scanStartTimestamp ) )
{
// This metadata is up to date. skip it.
log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
log.debug( "Skipping uptodate metadata: {}", metadataPath );
return;
}
metadataTools.updateMetadata( this.repository, metadataPath );
log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
log.debug( "Updated metadata: {}", metadataPath );
}
catch ( RepositoryMetadataException e )
{

View File

@ -33,15 +33,12 @@ import org.apache.archiva.configuration.FileTypes;
import org.apache.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.Plugin;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.SnapshotVersion;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
import org.apache.archiva.repository.ContentNotFoundException;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.RepositoryContent;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.content.Artifact;
@ -227,80 +224,6 @@ public class MetadataTools
return foundVersions;
}
/**
* Gather the set of snapshot versions found in a particular versioned reference.
*
* @return the Set of snapshot artifact versions found.
* @throws LayoutException
* @throws ContentNotFoundException
*/
public Set<String> gatherSnapshotVersions( ManagedRepositoryContent managedRepository,
VersionedReference reference )
throws LayoutException, IOException, ContentNotFoundException
{
Set<String> foundVersions = null;
try
{
ArchivaItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( reference.getGroupId( ) )
.withProjectId( reference.getArtifactId( ) )
.withArtifactId( reference.getArtifactId( ) )
.withVersion( reference.getVersion( ) )
.build( );
try(Stream<? extends Artifact> stream = managedRepository.getLayout( BaseRepositoryContentLayout.class ).newArtifactStream( selector )) {
foundVersions = stream.map( a -> a.getArtifactVersion( ) )
.filter( StringUtils::isNotEmpty )
.collect( Collectors.toSet( ) );
}
}
catch ( org.apache.archiva.repository.ContentAccessException e )
{
log.error( "Error while accessing content {}", e.getMessage( ) );
throw new IOException( "Could not access repository content: " + e.getMessage( ) );
}
// Next gather up the referenced 'latest' versions found in any proxied repositories
// maven-metadata-${proxyId}.xml files that may be present.
// Does this repository have a set of remote proxied repositories?
Set<String> proxiedRepoIds = this.proxies.get( managedRepository.getId() );
if ( CollectionUtils.isNotEmpty( proxiedRepoIds ) )
{
String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
baseVersion = baseVersion.substring( 0, baseVersion.indexOf( VersionUtil.SNAPSHOT ) - 1 );
// 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;
}
// Is there some snapshot info?
SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
if ( snapshot != null )
{
String timestamp = snapshot.getTimestamp();
int buildNumber = snapshot.getBuildNumber();
// Only interested in the timestamp + buildnumber.
if ( StringUtils.isNotBlank( timestamp ) && ( buildNumber > 0 ) )
{
foundVersions.add( baseVersion + "-" + timestamp + "-" + buildNumber );
}
}
}
}
return foundVersions;
}
/**
* Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
@ -308,7 +231,7 @@ public class MetadataTools
* @param path
* @return
*/
public VersionedReference toVersionedReference( String path )
public ItemSelector toVersionedSelector( String path )
throws RepositoryMetadataException
{
if ( !path.endsWith( "/" + MAVEN_METADATA ) )
@ -316,7 +239,7 @@ public class MetadataTools
throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
}
VersionedReference reference = new VersionedReference();
ArchivaItemSelector.Builder builder = ArchivaItemSelector.builder( );
String normalizedPath = StringUtils.replace( path, "\\", "/" );
String pathParts[] = StringUtils.split( normalizedPath, '/' );
@ -325,16 +248,17 @@ public class MetadataTools
int artifactIdOffset = versionOffset - 1;
int groupIdEnd = artifactIdOffset - 1;
reference.setVersion( pathParts[versionOffset] );
builder.withVersion( pathParts[versionOffset] );
if ( !hasNumberAnywhere( reference.getVersion() ) )
if ( !hasNumberAnywhere( pathParts[versionOffset] ) )
{
// 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." );
}
reference.setArtifactId( pathParts[artifactIdOffset] );
builder.withArtifactId( pathParts[artifactIdOffset] );
builder.withProjectId( pathParts[artifactIdOffset] );
StringBuilder gid = new StringBuilder();
for ( int i = 0; i <= groupIdEnd; i++ )
@ -346,9 +270,9 @@ public class MetadataTools
gid.append( pathParts[i] );
}
reference.setGroupId( gid.toString() );
builder.withNamespace( gid.toString( ) );
return reference;
return builder.build();
}
private boolean hasNumberAnywhere( String version )
@ -356,7 +280,7 @@ public class MetadataTools
return StringUtils.indexOfAny( version, NUMS ) != ( -1 );
}
public ProjectReference toProjectReference( String path )
public ItemSelector toProjectSelector( String path )
throws RepositoryMetadataException
{
if ( !path.endsWith( "/" + MAVEN_METADATA ) )
@ -364,7 +288,7 @@ public class MetadataTools
throw new RepositoryMetadataException( "Cannot convert to versioned reference, not a metadata file. " );
}
ProjectReference reference = new ProjectReference();
ArchivaItemSelector.Builder builder = ArchivaItemSelector.builder( );
String normalizedPath = StringUtils.replace( path, "\\", "/" );
String pathParts[] = StringUtils.split( normalizedPath, '/' );
@ -374,7 +298,8 @@ public class MetadataTools
int artifactIdOffset = pathParts.length - 2;
int groupIdEnd = artifactIdOffset - 1;
reference.setArtifactId( pathParts[artifactIdOffset] );
builder.withArtifactId( pathParts[artifactIdOffset] );
builder.withProjectId( pathParts[artifactIdOffset] );
StringBuilder gid = new StringBuilder();
for ( int i = 0; i <= groupIdEnd; i++ )
@ -386,9 +311,8 @@ public class MetadataTools
gid.append( pathParts[i] );
}
reference.setGroupId( gid.toString() );
return reference;
builder.withNamespace( gid.toString( ) );
return builder.build();
}
public String toPath( ContentItem reference )
@ -412,33 +336,6 @@ public class MetadataTools
}
public String toPath( ProjectReference reference )
{
StringBuilder path = new StringBuilder();
path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
path.append( MAVEN_METADATA );
return path.toString();
}
public String toPath( VersionedReference reference )
{
StringBuilder path = new StringBuilder();
path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
if ( reference.getVersion() != null )
{
// add the version only if it is present
path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
}
path.append( MAVEN_METADATA );
return path.toString();
}
private String formatAsDirectory( String directory )
{
return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
@ -500,17 +397,6 @@ public class MetadataTools
return readMetadataFile( managedRepository, metadataFile );
}
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
ProjectReference reference, String proxyId )
{
MetadataReader reader = getMetadataReader( managedRepository );
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
return readMetadataFile( managedRepository, metadataFile );
}
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
String logicalResource, String proxyId )
{
@ -519,14 +405,6 @@ public class MetadataTools
return readMetadataFile( managedRepository, metadataFile );
}
public ArchivaRepositoryMetadata readProxyMetadata( ManagedRepositoryContent managedRepository,
VersionedReference reference, String proxyId )
{
String metadataPath = getRepositorySpecificName( proxyId, toPath( reference ) );
StorageAsset metadataFile = managedRepository.getRepository().getAsset( metadataPath );
return readMetadataFile( managedRepository, metadataFile );
}
public void updateMetadata( ManagedRepositoryContent managedRepository, String logicalResource )
throws RepositoryMetadataException
{
@ -995,135 +873,6 @@ public class MetadataTools
checksum.fixChecksums( algorithms );
}
/**
* Update the metadata based on the following rules.
* <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
* @throws ContentNotFoundException
* @deprecated
*/
public void updateMetadata( ManagedRepositoryContent managedRepository, VersionedReference reference )
throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException
{
StorageAsset metadataFile = managedRepository.getRepository().getAsset( toPath( reference ) );
ArchivaRepositoryMetadata existingMetadata = readMetadataFile(managedRepository, metadataFile );
long lastUpdated = getExistingLastUpdated( existingMetadata );
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, and any
// proxied maven-metadata.xml files.
Set<String> snapshotVersions = gatherSnapshotVersions( managedRepository, reference );
if ( snapshotVersions.isEmpty() )
{
throw new ContentNotFoundException(
"No snapshot versions found on reference [" + VersionedReference.toKey( reference ) + "]." );
}
// sort the list to determine to aide in determining the Latest version.
List<String> sortedVersions = new ArrayList<>();
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 );
long snapshotLastUpdated = toLastUpdatedLong( tsDate + tsTime );
lastUpdated = Math.max( lastUpdated, snapshotLastUpdated );
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() );
/* Disabled due to decision in [MRM-535].
* Do not set metadata.lastUpdated to file.lastModified.
*
* 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 = managedRepository.toFile( 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() );
}
// Set last updated
if ( lastUpdated > 0 )
{
metadata.setLastUpdatedTimestamp( toLastUpdatedDate( lastUpdated ) );
}
// Save the metadata model to disk.
RepositoryMetadataWriter.write( metadata, metadataFile );
ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
checksum.fixChecksums( algorithms );
}
private void initConfigVariables()
{
assert(this.artifactPatterns!=null);

View File

@ -24,9 +24,7 @@ import org.apache.archiva.common.utils.VersionUtil;
import org.apache.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.Plugin;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.SnapshotVersion;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.policies.CachedFailuresPolicy;
import org.apache.archiva.policies.ChecksumPolicy;
import org.apache.archiva.policies.ReleasesPolicy;
@ -34,14 +32,15 @@ import org.apache.archiva.policies.SnapshotsPolicy;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.DataItem;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.metadata.base.RepositoryMetadataWriter;
import org.apache.archiva.repository.storage.fs.FilesystemStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.storage.fs.FilesystemStorage;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.wagon.TransferFailedException;
import org.easymock.EasyMock;
@ -1009,11 +1008,6 @@ public class MetadataTransferTest
assertNoTempFiles( expectedFile );
}
private ProjectReference createProjectReference( String path )
throws RepositoryMetadataException
{
return metadataTools.toProjectReference( path );
}
/**
* Transfer the metadata file, not expected to succeed.
@ -1067,11 +1061,6 @@ public class MetadataTransferTest
assertNoTempFiles( expectedFile );
}
private VersionedReference createVersionedReference( String path )
throws RepositoryMetadataException
{
return metadataTools.toVersionedReference( path );
}
/**
* Transfer the metadata file, not expected to succeed.
@ -1171,18 +1160,30 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue( "Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
ProjectReference actualMetadata = createGroupReference( requestedResource );
ItemSelector actualMetadata = createGroupSelector( requestedResource );
assertGroupMetadata( actualFile, actualMetadata, expectedPlugins );
}
private ProjectReference createGroupReference( String requestedResource )
private ItemSelector createProjectSelector(String path) throws RepositoryMetadataException
{
return metadataTools.toProjectSelector( path );
}
private ItemSelector createVersionedSelector(String path) throws RepositoryMetadataException
{
return metadataTools.toVersionedSelector( path );
}
private ItemSelector createGroupSelector( String requestedResource )
throws RepositoryMetadataException
{
ProjectReference projectReference = createProjectReference( requestedResource );
projectReference.setGroupId( projectReference.getGroupId() + "." + projectReference.getArtifactId() );
projectReference.setArtifactId( null );
return projectReference;
ItemSelector projectSelector = createProjectSelector( requestedResource );
ArchivaItemSelector.Builder projectReference = ArchivaItemSelector.builder( ).withSelector( projectSelector );
projectReference.withNamespace( projectSelector.getNamespace() + "." + projectSelector.getProjectId() );
projectReference.withArtifactId( null );
projectReference.withProjectId( null );
return projectReference.build();
}
private void assertRepoGroupMetadataContents( String proxiedRepoId, String requestedResource,
@ -1194,18 +1195,18 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue( "Repo Specific Group Metadata should exist: " + requestedResource, Files.exists(actualFile) );
ProjectReference actualMetadata = createGroupReference( requestedResource );
ItemSelector actualMetadata = createGroupSelector( requestedResource );
assertGroupMetadata( actualFile, actualMetadata, expectedPlugins );
}
private void assertGroupMetadata( Path actualFile, ProjectReference actualMetadata, String expectedPlugins[] )
private void assertGroupMetadata( Path actualFile, ItemSelector actualMetadata, String expectedPlugins[] )
throws Exception
{
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( actualMetadata.getGroupId() );
m.setGroupId( actualMetadata.getNamespace() );
for ( String pluginId : expectedPlugins )
{
@ -1236,12 +1237,12 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue( Files.exists(actualFile) );
ProjectReference metadata = createProjectReference( requestedResource );
ItemSelector metadata = createProjectSelector( requestedResource );
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( metadata.getGroupId() );
m.setGroupId( metadata.getNamespace() );
m.setArtifactId( metadata.getArtifactId() );
m.setLatestVersion( latestVersion );
m.setReleasedVersion( releaseVersion );
@ -1270,12 +1271,12 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue( "Release Metadata should exist: " + requestedResource, Files.exists(actualFile) );
VersionedReference metadata = createVersionedReference( requestedResource );
ItemSelector metadata = createVersionedSelector( requestedResource );
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( metadata.getGroupId() );
m.setGroupId( metadata.getNamespace() );
m.setArtifactId( metadata.getArtifactId() );
m.setVersion( metadata.getVersion() );
RepositoryMetadataWriter.write( m, expectedMetadataXml );
@ -1301,7 +1302,7 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(requestedResource);
assertTrue( "Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
VersionedReference actualMetadata = createVersionedReference( requestedResource );
ItemSelector actualMetadata = createVersionedSelector( requestedResource );
assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
}
@ -1326,19 +1327,19 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue( "Repo Specific Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile) );
VersionedReference actualMetadata = createVersionedReference( requestedResource );
ItemSelector actualMetadata = createVersionedSelector( requestedResource );
assertSnapshotMetadata( actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber );
}
private void assertSnapshotMetadata( Path actualFile, VersionedReference actualMetadata, String expectedDate,
private void assertSnapshotMetadata( Path actualFile, ItemSelector actualMetadata, String expectedDate,
String expectedTime, int expectedBuildnumber )
throws RepositoryMetadataException, Exception
{
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( actualMetadata.getGroupId() );
m.setGroupId( actualMetadata.getNamespace() );
m.setArtifactId( actualMetadata.getArtifactId() );
m.setVersion( VersionUtil.getBaseVersion( actualMetadata.getVersion() ) );
@ -1376,12 +1377,12 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue( Files.exists(actualFile) );
ProjectReference metadata = createProjectReference( requestedResource );
ItemSelector metadata = createProjectSelector( requestedResource );
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( metadata.getGroupId() );
m.setGroupId( metadata.getNamespace() );
m.setArtifactId( metadata.getArtifactId() );
if ( expectedProxyVersions != null )
@ -1410,12 +1411,12 @@ public class MetadataTransferTest
Path actualFile = managedDefaultDir.resolve(proxiedFile);
assertTrue( "Release metadata for repo should exist: " + actualFile, Files.exists(actualFile) );
VersionedReference metadata = createVersionedReference( requestedResource );
ItemSelector metadata = createVersionedSelector( requestedResource );
// Build expected metadata XML
StringWriter expectedMetadataXml = new StringWriter();
ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
m.setGroupId( metadata.getGroupId() );
m.setGroupId( metadata.getNamespace() );
m.setArtifactId( metadata.getArtifactId() );
m.setVersion( metadata.getVersion() );
RepositoryMetadataWriter.write( m, expectedMetadataXml );

View File

@ -254,22 +254,24 @@ public class MetadataToolsTest
@Test
public void testToPathFromVersionReference()
{
VersionedReference reference = new VersionedReference();
reference.setGroupId( "com.foo" );
reference.setArtifactId( "foo-tool" );
reference.setVersion( "1.0" );
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( "com.foo" )
.withArtifactId( "foo-tool" )
.withProjectId( "foo-tool" )
.withVersion( "1.0" ).build( );
assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( reference ) );
assertEquals( "com/foo/foo-tool/1.0/maven-metadata.xml", tools.toPath( selector ) );
}
@Test
public void testToPathFromProjectReference()
{
ProjectReference reference = new ProjectReference();
reference.setGroupId( "com.foo" );
reference.setArtifactId( "foo-tool" );
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( "com.foo" )
.withProjectId( "foo-tool" )
.withArtifactId( "foo-tool" ).build( );
assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( reference ) );
assertEquals( "com/foo/foo-tool/maven-metadata.xml", tools.toPath( selector ) );
}
@Test
@ -304,10 +306,10 @@ public class MetadataToolsTest
private void assertProjectReference( String groupId, String artifactId, String path )
throws RepositoryMetadataException
{
ProjectReference reference = tools.toProjectReference( path );
ItemSelector reference = tools.toProjectSelector( path );
assertNotNull( "Reference should not be null.", reference );
assertEquals( "ProjectReference.groupId", groupId, reference.getGroupId() );
assertEquals( "ProjectReference.groupId", groupId, reference.getNamespace() );
assertEquals( "ProjectReference.artifactId", artifactId, reference.getArtifactId() );
}
@ -362,10 +364,10 @@ public class MetadataToolsTest
private void assertVersionedReference( String groupId, String artifactId, String version, String path )
throws RepositoryMetadataException
{
VersionedReference reference = tools.toVersionedReference( path );
ItemSelector reference = tools.toVersionedSelector( path );
assertNotNull( "Reference should not be null.", reference );
assertEquals( "VersionedReference.groupId", groupId, reference.getGroupId() );
assertEquals( "VersionedReference.groupId", groupId, reference.getNamespace() );
assertEquals( "VersionedReference.artifactId", artifactId, reference.getArtifactId() );
assertEquals( "VersionedReference.version", version, reference.getVersion() );
}
@ -375,10 +377,11 @@ public class MetadataToolsTest
{
Path repoRootDir = getRepositoryPath( "metadata-repository" );
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
ItemSelector reference = ArchivaItemSelector.builder( )
.withNamespace( "org.apache.archiva.metadata.tests" )
.withArtifactId( artifactId )
.withProjectId( artifactId )
.withVersion( version ).build( );
MavenManagedRepository repo =
createRepository( "test-repo", "Test Repository: " + name.getMethodName(), repoRootDir );
@ -424,25 +427,7 @@ public class MetadataToolsTest
}
private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
ProjectReference reference )
throws LayoutException, IOException, SAXException, ParserConfigurationException
{
Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve(tools.toPath( reference ) );
String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );
Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
if ( detailedDiff.hasDifferences() )
{
for ( Difference diff : detailedDiff.getDifferences() ) {
System.out.println( diff );
}
// If it isn't similar, dump the difference.
assertEquals( expectedMetadata, actualMetadata );
}
}
private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
VersionedReference reference )
ItemSelector reference )
throws LayoutException, IOException, SAXException, ParserConfigurationException
{
Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve( tools.toPath( reference ) );
@ -543,62 +528,30 @@ public class MetadataToolsTest
assertProjectMetadata( buf.toString(), testRepo, reference );
}
private void assertProjectMetadata( ManagedRepositoryContent testRepo, ProjectReference reference,
String artifactId, String[] expectedVersions, String latestVersion,
String releaseVersion )
throws Exception
{
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" );
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++ )
{
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 Exception
{
ManagedRepositoryContent testRepo = createTestRepoContent();
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
prepTestRepo( testRepo, reference );
ItemSelector selector = ArchivaItemSelector.builder()
.withNamespace( "org.apache.archiva.metadata.tests" )
.withProjectId( artifactId )
.withArtifactId( artifactId )
.withVersion( version ).build();
tools.updateMetadata( testRepo, reference );
prepTestRepo( testRepo, selector );
tools.updateVersionMetadata( testRepo, selector );
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" );
buf.append( " <version>" ).append( reference.getVersion() ).append( "</version>\n" );
buf.append( " <groupId>" ).append( selector.getNamespace() ).append( "</groupId>\n" );
buf.append( " <artifactId>" ).append( selector.getArtifactId() ).append( "</artifactId>\n" );
buf.append( " <version>" ).append( selector.getVersion() ).append( "</version>\n" );
buf.append( "</metadata>" );
assertMetadata( buf.toString(), testRepo, reference );
assertMetadata( buf.toString(), testRepo, selector );
}
private void assertUpdatedSnapshotVersionMetadata( String artifactId, String version, String expectedDate,
@ -606,18 +559,19 @@ public class MetadataToolsTest
throws Exception
{
ManagedRepositoryContent testRepo = createTestRepoContent();
VersionedReference reference = new VersionedReference();
reference.setGroupId( "org.apache.archiva.metadata.tests" );
reference.setArtifactId( artifactId );
reference.setVersion( version );
ItemSelector reference = ArchivaItemSelector.builder()
.withNamespace( "org.apache.archiva.metadata.tests" )
.withArtifactId( artifactId )
.withProjectId( artifactId )
.withVersion( version ).build();
prepTestRepo( testRepo, reference );
tools.updateMetadata( testRepo, reference );
tools.updateVersionMetadata( testRepo, reference );
StringBuilder buf = new StringBuilder();
buf.append( "<metadata>\n" );
buf.append( " <groupId>" ).append( reference.getGroupId() ).append( "</groupId>\n" );
buf.append( " <groupId>" ).append( reference.getNamespace() ).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" );
@ -719,10 +673,10 @@ public class MetadataToolsTest
FileUtils.copyDirectory( srcDir.toFile(), destDir.toFile() );
}
private void prepTestRepo( ManagedRepositoryContent repo, ProjectReference reference )
private void prepTestRepo( ManagedRepositoryContent repo, ItemSelector reference )
throws IOException
{
String groupDir = StringUtils.replaceChars( reference.getGroupId(), '.', '/' );
String groupDir = StringUtils.replaceChars( reference.getNamespace(), '.', '/' );
String path = groupDir + "/" + reference.getArtifactId();
Path srcRepoDir = getRepositoryPath( "metadata-repository" );
@ -735,15 +689,5 @@ public class MetadataToolsTest
FileUtils.copyDirectory( srcDir.toFile(), destDir.toFile() );
}
private void prepTestRepo( ManagedRepositoryContent repo, VersionedReference reference )
throws IOException
{
ProjectReference projectRef = new ProjectReference();
projectRef.setGroupId( reference.getGroupId() );
projectRef.setArtifactId( reference.getArtifactId() );
prepTestRepo( repo, projectRef );
}
}

View File

@ -1295,7 +1295,7 @@ public class ArchivaDavResourceFactory
{
try
{
metadataTools.toVersionedReference( requestedResource );
metadataTools.toVersionedSelector( requestedResource );
return false;
}
catch ( RepositoryMetadataException re )