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:
Edwin L. Punzalan 2005-12-05 07:12:35 +00:00
parent 5c3468fad9
commit 86e6409019
6 changed files with 262 additions and 42 deletions

View File

@ -49,38 +49,41 @@ public class BadMetadataReportProcessor
{ {
boolean hasFailures = false; 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() ) if ( metadata.storedInGroupDirectory() )
{ {
checkPluginMetadata( metadata, repository, reporter ); checkPluginMetadata( metadata, repository, reporter );
} }
else if ( metadata.storedInArtifactVersionDirectory() )
{
checkSnapshotMetadata( metadata, repository, reporter );
}
else 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; hasFailures = true;
} }
try if ( metadata.storedInArtifactVersionDirectory() )
{ {
if ( checkRepositoryVersions( metadata, repository, reporter ) ) checkSnapshotMetadata( metadata, repository, reporter );
}
else
{
if ( !checkMetadataVersions( metadata, repository, reporter ) )
{ {
hasFailures = true; hasFailures = true;
} }
}
catch ( IOException e ) try
{ {
throw new ReportProcessorException( "Error getting versions", e ); if ( checkRepositoryVersions( metadata, repository, reporter ) )
{
hasFailures = true;
}
}
catch ( IOException e )
{
throw new ReportProcessorException( "Error getting versions", e );
}
} }
} }
@ -93,7 +96,7 @@ public class BadMetadataReportProcessor
/** /**
* Checks the plugin metadata * Checks the plugin metadata
*/ */
public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository, protected boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
@ -123,7 +126,7 @@ public class BadMetadataReportProcessor
{ {
if ( prefixes.containsKey( prefix ) ) if ( prefixes.containsKey( prefix ) )
{ {
reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix ); reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix + "." );
hasFailures = true; hasFailures = true;
} }
else else
@ -132,11 +135,14 @@ public class BadMetadataReportProcessor
} }
} }
File pluginDir = new File( metadataDir, artifactId ); if ( artifactId != null )
if ( !pluginDir.exists() )
{ {
reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" ); File pluginDir = new File( metadataDir, artifactId );
hasFailures = true; 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 * Checks the snapshot metadata
*/ */
private boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository, protected boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
RepositoryQueryLayer repositoryQueryLayer =
repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
boolean hasFailures = false; boolean hasFailures = false;
Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot(); Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
String timestamp = snapshot.getTimestamp(); String timestamp = snapshot.getTimestamp();
String buildNumber = String.valueOf( snapshot.getBuildNumber() ); String buildNumber = String.valueOf( snapshot.getBuildNumber() );
String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom";
//@todo use wagon instead
Artifact artifact = createArtifact( metadata ); Artifact artifact = createArtifact( metadata );
File artifactFile = new File( repository.pathOf( artifact ) ); if ( !repositoryQueryLayer.containsArtifact( artifact, snapshot ) )
File snapshotFile = new File( artifactFile.getParentFile(), artifactName );
if ( !snapshotFile.exists() )
{ {
reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." ); reporter.addFailure( metadata, "Snapshot artifact " + timestamp + "-" + buildNumber + " does not exist." );
hasFailures = true; hasFailures = true;
} }
@ -172,7 +177,7 @@ public class BadMetadataReportProcessor
/** /**
* Checks the declared metadata versions if the artifacts are present in the repository * 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 ) ArtifactReporter reporter )
{ {
RepositoryQueryLayer repositoryQueryLayer = 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 * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
* metadata file. * metadata file.
*/ */
private boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository, protected boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
throws IOException throws IOException
{ {

View File

@ -18,6 +18,7 @@ package org.apache.maven.repository.reporting;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Snapshot;
import java.io.File; import java.io.File;
@ -39,4 +40,13 @@ public class DefaultRepositoryQueryLayer
File f = new File( repository.getBasedir(), repository.pathOf( artifact ) ); File f = new File( repository.getBasedir(), repository.pathOf( artifact ) );
return f.exists(); 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();
}
} }

View File

@ -18,6 +18,7 @@ package org.apache.maven.repository.reporting;
*/ */
import org.apache.maven.artifact.Artifact; 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. * 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(); String ROLE = RepositoryQueryLayer.class.getName();
boolean containsArtifact( Artifact artifact ); boolean containsArtifact( Artifact artifact );
boolean containsArtifact( Artifact artifact, Snapshot snapshot );
} }

View File

@ -19,11 +19,18 @@ package org.apache.maven.repository.reporting;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; 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.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Snapshot;
import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.Versioning;
import java.util.Iterator; 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 public class BadMetadataReportProcessorTest
extends AbstractRepositoryReportsTestCase extends AbstractRepositoryReportsTestCase
{ {
@ -41,6 +48,51 @@ public class BadMetadataReportProcessorTest
badMetadataReportProcessor = (MetadataReportProcessor) lookup( MetadataReportProcessor.ROLE ); 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() public void testMetadataMissingADirectory()
throws ReportProcessorException throws ReportProcessorException
{ {
@ -67,24 +119,174 @@ public class BadMetadataReportProcessorTest
assertFalse( "check no more failures", failures.hasNext() ); 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 testCheckPluginMetadata() 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 testCheckSnapshotMetadata() 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 testCheckMetadataVersions() 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 testCheckRepositoryVersions() 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 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 testInvalidSnapshotMetadata()
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( 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;
}
} }