mirror of https://github.com/apache/archiva.git
Completed the junit tests for the BadMetadataProcessor
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@354002 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5c3468fad9
commit
86e6409019
|
@ -49,41 +49,44 @@ public class BadMetadataReportProcessor
|
|||
{
|
||||
boolean hasFailures = false;
|
||||
|
||||
String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
|
||||
if ( lastUpdated == null || lastUpdated.length() == 0 )
|
||||
{
|
||||
reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
|
||||
hasFailures = true;
|
||||
}
|
||||
|
||||
if ( metadata.storedInGroupDirectory() )
|
||||
{
|
||||
checkPluginMetadata( metadata, repository, reporter );
|
||||
}
|
||||
else if ( metadata.storedInArtifactVersionDirectory() )
|
||||
{
|
||||
checkSnapshotMetadata( metadata, repository, reporter );
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !checkMetadataVersions( metadata, repository, reporter ) )
|
||||
String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
|
||||
if ( lastUpdated == null || lastUpdated.length() == 0 )
|
||||
{
|
||||
reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
|
||||
hasFailures = true;
|
||||
}
|
||||
|
||||
try
|
||||
if ( metadata.storedInArtifactVersionDirectory() )
|
||||
{
|
||||
if ( checkRepositoryVersions( metadata, repository, reporter ) )
|
||||
checkSnapshotMetadata( metadata, repository, reporter );
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !checkMetadataVersions( metadata, repository, reporter ) )
|
||||
{
|
||||
hasFailures = true;
|
||||
}
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new ReportProcessorException( "Error getting versions", e );
|
||||
|
||||
try
|
||||
{
|
||||
if ( checkRepositoryVersions( metadata, repository, reporter ) )
|
||||
{
|
||||
hasFailures = true;
|
||||
}
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
throw new ReportProcessorException( "Error getting versions", e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( !hasFailures )
|
||||
{
|
||||
reporter.addSuccess( metadata );
|
||||
|
@ -93,7 +96,7 @@ public class BadMetadataReportProcessor
|
|||
/**
|
||||
* Checks the plugin metadata
|
||||
*/
|
||||
public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
protected boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
ArtifactReporter reporter )
|
||||
{
|
||||
boolean hasFailures = false;
|
||||
|
@ -123,7 +126,7 @@ public class BadMetadataReportProcessor
|
|||
{
|
||||
if ( prefixes.containsKey( prefix ) )
|
||||
{
|
||||
reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix );
|
||||
reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix + "." );
|
||||
hasFailures = true;
|
||||
}
|
||||
else
|
||||
|
@ -132,11 +135,14 @@ public class BadMetadataReportProcessor
|
|||
}
|
||||
}
|
||||
|
||||
File pluginDir = new File( metadataDir, artifactId );
|
||||
if ( !pluginDir.exists() )
|
||||
if ( artifactId != null )
|
||||
{
|
||||
reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" );
|
||||
hasFailures = true;
|
||||
File pluginDir = new File( metadataDir, artifactId );
|
||||
if ( !pluginDir.exists() )
|
||||
{
|
||||
reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" );
|
||||
hasFailures = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,23 +152,22 @@ public class BadMetadataReportProcessor
|
|||
/**
|
||||
* Checks the snapshot metadata
|
||||
*/
|
||||
private boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
protected boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
ArtifactReporter reporter )
|
||||
{
|
||||
RepositoryQueryLayer repositoryQueryLayer =
|
||||
repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
|
||||
|
||||
boolean hasFailures = false;
|
||||
|
||||
Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
|
||||
String timestamp = snapshot.getTimestamp();
|
||||
String buildNumber = String.valueOf( snapshot.getBuildNumber() );
|
||||
String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom";
|
||||
|
||||
//@todo use wagon instead
|
||||
Artifact artifact = createArtifact( metadata );
|
||||
File artifactFile = new File( repository.pathOf( artifact ) );
|
||||
File snapshotFile = new File( artifactFile.getParentFile(), artifactName );
|
||||
if ( !snapshotFile.exists() )
|
||||
if ( !repositoryQueryLayer.containsArtifact( artifact, snapshot ) )
|
||||
{
|
||||
reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." );
|
||||
reporter.addFailure( metadata, "Snapshot artifact " + timestamp + "-" + buildNumber + " does not exist." );
|
||||
hasFailures = true;
|
||||
}
|
||||
|
||||
|
@ -172,7 +177,7 @@ public class BadMetadataReportProcessor
|
|||
/**
|
||||
* Checks the declared metadata versions if the artifacts are present in the repository
|
||||
*/
|
||||
private boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
protected boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
ArtifactReporter reporter )
|
||||
{
|
||||
RepositoryQueryLayer repositoryQueryLayer =
|
||||
|
@ -203,7 +208,7 @@ public class BadMetadataReportProcessor
|
|||
* Searches the artifact repository directory for all versions and verifies that all of them are listed in the
|
||||
* metadata file.
|
||||
*/
|
||||
private boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
protected boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
|
||||
ArtifactReporter reporter )
|
||||
throws IOException
|
||||
{
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.maven.repository.reporting;
|
|||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||
import org.apache.maven.artifact.repository.metadata.Snapshot;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
@ -39,4 +40,13 @@ public class DefaultRepositoryQueryLayer
|
|||
File f = new File( repository.getBasedir(), repository.pathOf( artifact ) );
|
||||
return f.exists();
|
||||
}
|
||||
|
||||
public boolean containsArtifact( Artifact artifact, Snapshot snapshot )
|
||||
{
|
||||
File f = new File( repository.getBasedir(), repository.pathOf( artifact ) );
|
||||
String snapshotInfo = artifact.getVersion().replaceAll( "SNAPSHOT", snapshot.getTimestamp() + "-" +
|
||||
snapshot.getBuildNumber() + ".pom" );
|
||||
File snapshotFile = new File( f.getParentFile(), artifact.getArtifactId() + "-" + snapshotInfo );
|
||||
return snapshotFile.exists();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.maven.repository.reporting;
|
|||
*/
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.repository.metadata.Snapshot;
|
||||
|
||||
/**
|
||||
* The transitive and metadata validation reports will need to query the repository for artifacts.
|
||||
|
@ -27,4 +28,6 @@ public interface RepositoryQueryLayer
|
|||
String ROLE = RepositoryQueryLayer.class.getName();
|
||||
|
||||
boolean containsArtifact( Artifact artifact );
|
||||
|
||||
boolean containsArtifact( Artifact artifact, Snapshot snapshot );
|
||||
}
|
||||
|
|
|
@ -19,11 +19,18 @@ package org.apache.maven.repository.reporting;
|
|||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
|
||||
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
|
||||
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
|
||||
import org.apache.maven.artifact.repository.metadata.Snapshot;
|
||||
import org.apache.maven.artifact.repository.metadata.Versioning;
|
||||
|
||||
import java.util.Iterator;
|
||||
import org.apache.maven.artifact.repository.metadata.Plugin;
|
||||
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
|
||||
|
||||
/**
|
||||
* @todo??? should use MetadataXpp3Reader instead ?
|
||||
*/
|
||||
public class BadMetadataReportProcessorTest
|
||||
extends AbstractRepositoryReportsTestCase
|
||||
{
|
||||
|
@ -40,6 +47,51 @@ public class BadMetadataReportProcessorTest
|
|||
|
||||
badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE );
|
||||
}
|
||||
|
||||
public void testMetadataMissingLastUpdated()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
|
||||
|
||||
Versioning versioning = new Versioning();
|
||||
versioning.addVersion( "1.0-alpha-1" );
|
||||
versioning.addVersion( "1.0-alpha-2" );
|
||||
|
||||
RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
assertEquals( "check metadata", metadata, result.getMetadata() );
|
||||
assertEquals( "check reason",
|
||||
"Missing lastUpdated element inside the metadata.",
|
||||
result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testMetadataValidVersions()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
|
||||
|
||||
Versioning versioning = new Versioning();
|
||||
versioning.addVersion( "1.0-alpha-1" );
|
||||
versioning.addVersion( "1.0-alpha-2" );
|
||||
versioning.setLastUpdated( "20050611.202020" );
|
||||
|
||||
RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertFalse( "check there are no failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testMetadataMissingADirectory()
|
||||
throws ReportProcessorException
|
||||
|
@ -66,25 +118,175 @@ public class BadMetadataReportProcessorTest
|
|||
result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testProcessMetadata()
|
||||
|
||||
public void testMetadataInvalidArtifactVersion()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
|
||||
|
||||
Versioning versioning = new Versioning();
|
||||
versioning.addVersion( "1.0-alpha-1" );
|
||||
versioning.addVersion( "1.0-alpha-2" );
|
||||
versioning.addVersion( "1.0-alpha-3" );
|
||||
versioning.setLastUpdated( "20050611.202020" );
|
||||
|
||||
RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
assertEquals( "check metadata", metadata, result.getMetadata() );
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason",
|
||||
"Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
|
||||
result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testMoreThanOneMetadataVersionErrors()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "artifactId", "1.0-alpha-1", "type" );
|
||||
|
||||
Versioning versioning = new Versioning();
|
||||
versioning.addVersion( "1.0-alpha-1" );
|
||||
versioning.addVersion( "1.0-alpha-3" );
|
||||
versioning.setLastUpdated( "20050611.202020" );
|
||||
|
||||
RepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact, versioning );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
assertEquals( "check metadata", metadata, result.getMetadata() );
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason",
|
||||
"Artifact version 1.0-alpha-3 is present in metadata but missing in the repository.",
|
||||
result.getReason() );
|
||||
assertTrue( "check there is a 2nd failure", failures.hasNext() );
|
||||
result = (RepositoryMetadataResult) failures.next();
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason",
|
||||
"Artifact version 1.0-alpha-2 found in the repository but missing in the metadata.",
|
||||
result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testCheckPluginMetadata()
|
||||
public void testValidPluginMetadata()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
|
||||
metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertFalse( "check there are no failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testInvalidPluginArtifactId()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
|
||||
metadata.getMetadata().addPlugin( createMetadataPlugin( null, "default" ) );
|
||||
metadata.getMetadata().addPlugin( createMetadataPlugin( "", "default2" ) );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
|
||||
assertTrue( "check there is a 2nd failure", failures.hasNext() );
|
||||
result = (RepositoryMetadataResult) failures.next();
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason", "Missing or empty artifactId in group metadata.", result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testDuplicatePluginPrefixes()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
RepositoryMetadata metadata = new GroupRepositoryMetadata( "groupId" );
|
||||
metadata.getMetadata().addPlugin( createMetadataPlugin( "artifactId", "default" ) );
|
||||
metadata.getMetadata().addPlugin( createMetadataPlugin( "snapshot-artifact", "default" ) );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason", "Duplicate plugin prefix found: default.", result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testCheckSnapshotMetadata()
|
||||
public void testValidSnapshotMetadata()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact",
|
||||
"1.0-alpha-1-SNAPSHOT", "type" );
|
||||
|
||||
Snapshot snapshot = new Snapshot();
|
||||
snapshot.setBuildNumber( 1 );
|
||||
snapshot.setTimestamp( "20050611.202024" );
|
||||
|
||||
RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertFalse( "check there are no failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
public void testCheckMetadataVersions()
|
||||
public void testInvalidSnapshotMetadata()
|
||||
throws ReportProcessorException
|
||||
{
|
||||
}
|
||||
ArtifactReporter reporter = new MockArtifactReporter();
|
||||
|
||||
Artifact artifact = artifactFactory.createBuildArtifact( "groupId", "snapshot-artifact",
|
||||
"1.0-alpha-1-SNAPSHOT", "type" );
|
||||
|
||||
public void testCheckRepositoryVersions()
|
||||
Snapshot snapshot = new Snapshot();
|
||||
snapshot.setBuildNumber( 2 );
|
||||
snapshot.setTimestamp( "20050611.202024" );
|
||||
|
||||
RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
|
||||
|
||||
badMetadataReportProcessor.processMetadata( metadata, repository, reporter );
|
||||
|
||||
Iterator failures = reporter.getRepositoryMetadataFailureIterator();
|
||||
assertTrue( "check there is a failure", failures.hasNext() );
|
||||
RepositoryMetadataResult result = (RepositoryMetadataResult) failures.next();
|
||||
assertEquals( "check metadata", metadata, result.getMetadata() );
|
||||
// TODO: should be more robust
|
||||
assertEquals( "check reason", "Snapshot artifact 20050611.202024-2 does not exist.", result.getReason() );
|
||||
assertFalse( "check no more failures", failures.hasNext() );
|
||||
}
|
||||
|
||||
private Plugin createMetadataPlugin( String artifactId, String prefix )
|
||||
{
|
||||
Plugin plugin = new Plugin();
|
||||
plugin.setArtifactId( artifactId );
|
||||
plugin.setName( artifactId );
|
||||
plugin.setPrefix( prefix );
|
||||
return plugin;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue