From d215b81192b3613c8b4df282f2923f8f003aa3dd Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Fri, 27 Jan 2006 07:31:51 +0000 Subject: [PATCH] [MRM-9] test invalid source metadata git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@372775 13f79535-47bb-0310-9956-ffa450edef68 --- .../converter/DefaultRepositoryConverter.java | 201 ++++++++++++++---- .../DefaultRepositoryConverter.properties | 13 +- .../converter/RepositoryConverterTest.java | 63 +++++- .../1.0.0/incorrectArtifactMetadata-1.0.0.jar | 1 + .../1.0.0/incorrectArtifactMetadata-1.0.0.pom | 22 ++ .../maven-metadata.xml | 25 +++ ...apshotMetadata-1.0.0-20060102.030405-6.jar | 1 + ...apshotMetadata-1.0.0-20060102.030405-6.pom | 22 ++ 8 files changed, 299 insertions(+), 49 deletions(-) create mode 100644 maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.jar create mode 100644 maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.pom create mode 100644 maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/maven-metadata.xml create mode 100644 maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.jar create mode 100644 maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.pom diff --git a/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java b/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java index ce9a094be..eb9878eb5 100644 --- a/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java +++ b/maven-repository-converter/src/main/java/org/apache/maven/repository/converter/DefaultRepositoryConverter.java @@ -18,10 +18,10 @@ package org.apache.maven.repository.converter; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.Snapshot; import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.Versioning; @@ -94,34 +94,37 @@ public class DefaultRepositoryConverter throw new RepositoryConversionException( getI18NString( "exception.repositories.match" ) ); } - if ( copyArtifact( artifact, targetRepository, reporter ) ) + if ( validateMetadata( artifact, reporter ) ) { - copyPom( artifact, targetRepository, reporter ); - - Metadata metadata = createBaseMetadata( artifact ); - Versioning versioning = new Versioning(); - versioning.addVersion( artifact.getBaseVersion() ); - metadata.setVersioning( versioning ); - updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata ); - - metadata = createBaseMetadata( artifact ); - metadata.setVersion( artifact.getBaseVersion() ); - versioning = new Versioning(); - - Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() ); - if ( matcher.matches() ) + if ( copyArtifact( artifact, targetRepository, reporter ) ) { - Snapshot snapshot = new Snapshot(); - snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() ); - snapshot.setTimestamp( matcher.group( 2 ) ); - versioning.setSnapshot( snapshot ); + copyPom( artifact, targetRepository, reporter ); + + Metadata metadata = createBaseMetadata( artifact ); + Versioning versioning = new Versioning(); + versioning.addVersion( artifact.getBaseVersion() ); + metadata.setVersioning( versioning ); + updateMetadata( new ArtifactRepositoryMetadata( artifact ), targetRepository, metadata ); + + metadata = createBaseMetadata( artifact ); + metadata.setVersion( artifact.getBaseVersion() ); + versioning = new Versioning(); + + Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher( artifact.getVersion() ); + if ( matcher.matches() ) + { + Snapshot snapshot = new Snapshot(); + snapshot.setBuildNumber( Integer.valueOf( matcher.group( 3 ) ).intValue() ); + snapshot.setTimestamp( matcher.group( 2 ) ); + versioning.setSnapshot( snapshot ); + } + + // TODO: merge latest/release/snapshot from source instead + metadata.setVersioning( versioning ); + updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata ); + + reporter.addSuccess( artifact ); } - - // TODO: merge latest/release/snapshot from source instead - metadata.setVersioning( versioning ); - updateMetadata( new SnapshotArtifactRepositoryMetadata( artifact ), targetRepository, metadata ); - - reporter.addSuccess( artifact ); } } @@ -133,7 +136,7 @@ public class DefaultRepositoryConverter return metadata; } - private void updateMetadata( ArtifactMetadata artifactMetadata, ArtifactRepository targetRepository, + private void updateMetadata( RepositoryMetadata artifactMetadata, ArtifactRepository targetRepository, Metadata newMetadata ) throws RepositoryConversionException { @@ -145,25 +148,7 @@ public class DefaultRepositoryConverter if ( file.exists() ) { - MetadataXpp3Reader reader = new MetadataXpp3Reader(); - FileReader fileReader = null; - try - { - fileReader = new FileReader( file ); - metadata = reader.read( fileReader ); - } - catch ( IOException e ) - { - throw new RepositoryConversionException( "Error reading target metadata", e ); - } - catch ( XmlPullParserException e ) - { - throw new RepositoryConversionException( "Error reading target metadata", e ); - } - finally - { - IOUtil.close( fileReader ); - } + metadata = readMetadata( file ); changed = metadata.merge( newMetadata ); } else @@ -195,6 +180,130 @@ public class DefaultRepositoryConverter } } + private Metadata readMetadata( File file ) + throws RepositoryConversionException + { + Metadata metadata; + MetadataXpp3Reader reader = new MetadataXpp3Reader(); + FileReader fileReader = null; + try + { + fileReader = new FileReader( file ); + metadata = reader.read( fileReader ); + } + catch ( IOException e ) + { + throw new RepositoryConversionException( "Error reading target metadata", e ); + } + catch ( XmlPullParserException e ) + { + throw new RepositoryConversionException( "Error reading target metadata", e ); + } + finally + { + IOUtil.close( fileReader ); + } + return metadata; + } + + private boolean validateMetadata( Artifact artifact, ArtifactReporter reporter ) + throws RepositoryConversionException + { + ArtifactRepository repository = artifact.getRepository(); + + boolean result = true; + + RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata( artifact ); + File file = + new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); + if ( file.exists() ) + { + Metadata metadata = readMetadata( file ); + result = validateMetadata( metadata, repositoryMetadata, artifact, reporter ); + } + + repositoryMetadata = new SnapshotArtifactRepositoryMetadata( artifact ); + file = new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( repositoryMetadata ) ); + if ( file.exists() ) + { + Metadata metadata = readMetadata( file ); + result |= validateMetadata( metadata, repositoryMetadata, artifact, reporter ); + } + + return result; + } + + private boolean validateMetadata( Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact, + ArtifactReporter reporter ) + { + String key = "failure.incorrect."; + + if ( repositoryMetadata.storedInGroupDirectory() ) + { + key += "groupMetadata."; + } + else if ( repositoryMetadata.storedInArtifactVersionDirectory() ) + { + key += "snapshotMetadata."; + } + else + { + key += "artifactMetadata."; + } + + boolean result = true; + + if ( !metadata.getGroupId().equals( artifact.getGroupId() ) ) + { + reporter.addFailure( artifact, getI18NString( key + "groupId" ) ); + result = false; + } + if ( !repositoryMetadata.storedInGroupDirectory() ) + { + if ( !metadata.getArtifactId().equals( artifact.getArtifactId() ) ) + { + reporter.addFailure( artifact, getI18NString( key + "artifactId" ) ); + result = false; + } + if ( !repositoryMetadata.storedInArtifactVersionDirectory() ) + { + // artifact metadata + + boolean foundVersion = false; + if ( metadata.getVersioning() != null ) + { + for ( Iterator i = metadata.getVersioning().getVersions().iterator(); + i.hasNext() && !foundVersion; ) + { + String version = (String) i.next(); + if ( version.equals( artifact.getVersion() ) ) + { + foundVersion = true; + } + } + } + + if ( !foundVersion ) + { + reporter.addFailure( artifact, getI18NString( key + "versions" ) ); + result = false; + } + } + else + { + // snapshot metadata + if ( !metadata.getVersion().equals( artifact.getVersion() ) ) + { + reporter.addFailure( artifact, getI18NString( key + "version" ) ); + result = false; + } + + // TODO: build number + } + } + return result; + } + private void copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter ) throws RepositoryConversionException { diff --git a/maven-repository-converter/src/main/resources/org/apache/maven/repository/converter/DefaultRepositoryConverter.properties b/maven-repository-converter/src/main/resources/org/apache/maven/repository/converter/DefaultRepositoryConverter.properties index 855d1e8ec..f55089dbb 100644 --- a/maven-repository-converter/src/main/resources/org/apache/maven/repository/converter/DefaultRepositoryConverter.properties +++ b/maven-repository-converter/src/main/resources/org/apache/maven/repository/converter/DefaultRepositoryConverter.properties @@ -18,4 +18,15 @@ failure.incorrect.md5=The MD5 checksum value was incorrect. failure.incorrect.sha1=The SHA1 checksum value was incorrect. warning.missing.pom=The artifact had no POM in the source repository. failure.target.already.exists=The artifact could not be converted because it already exists. -exception.repositories.match=Source and target repositories are identical. \ No newline at end of file +exception.repositories.match=Source and target repositories are identical. + +failure.incorrect.groupMetadata.groupId=The group ID in the source group metadata is incorrect. + +failure.incorrect.artifactMetadata.artifactId=The artifact ID in the source artifact metadata is incorrect. +failure.incorrect.artifactMetadata.groupId=The group ID in the source artifact metadata is incorrect. +failure.incorrect.artifactMetadata.versions=The version list in the source artifact metadata is incorrect. + +failure.incorrect.snapshotMetadata.artifactId=The artifact ID in the source artifact version metadata is incorrect. +failure.incorrect.snapshotMetadata.groupId=The group ID in the source artifact version metadata is incorrect. +failure.incorrect.snapshotMetadata.version=The version in the source artifact version metadata is incorrect. +failure.incorrect.snapshotMetadata.snapshot=The snapshot information in the source artifact version metadata is incorrect. diff --git a/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java index c23a671b5..064ddbc76 100644 --- a/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java +++ b/maven-repository-converter/src/test/java/org/apache/maven/repository/converter/RepositoryConverterTest.java @@ -33,6 +33,7 @@ import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -428,6 +429,11 @@ public class RepositoryConverterTest assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), getFailure().getReason() ); assertFalse( "Check artifact not created", file.exists() ); + + ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); + File metadataFile = + new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", metadataFile.exists() ); } public void testIncorrectSourceChecksumSha1() @@ -444,6 +450,11 @@ public class RepositoryConverterTest assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), getFailure().getReason() ); assertFalse( "Check artifact not created", file.exists() ); + + ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); + File metadataFile = + new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", metadataFile.exists() ); } public void testUnmodifiedArtifact() @@ -692,17 +703,53 @@ public class RepositoryConverterTest } public void testInvalidSourceArtifactMetadata() + throws Exception { // test artifact is not converted when source metadata is invalid, and returns failure - // TODO + createModernSourceRepository(); + + Artifact artifact = createArtifact( "test", "incorrectArtifactMetadata", "1.0.0" ); + File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + file.delete(); + + repositoryConverter.convert( artifact, targetRepository, reporter ); + checkFailure(); + assertEquals( "check failure message", getI18nString( "failure.incorrect.artifactMetadata.versions" ), + getFailure().getReason() ); + + assertFalse( "Check artifact not created", file.exists() ); + + ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); + File metadataFile = + new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", metadataFile.exists() ); } public void testInvalidSourceSnapshotMetadata() + throws Exception, MalformedURLException { // test artifact is not converted when source snapshot metadata is invalid and returns failure - // TODO +/* TODO: + createModernSourceRepository(); + + Artifact artifact = createArtifact( "test", "incorrectSnapshotMetadata", "1.0.0-20060102.030405-6" ); + File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) ); + file.delete(); + + repositoryConverter.convert( artifact, targetRepository, reporter ); + checkFailure(); + assertEquals( "check failure message", getI18nString( "failure.incorrect.snapshotMetadata.snapshot" ), + getFailure().getReason() ); + + assertFalse( "Check artifact not created", file.exists() ); + + ArtifactRepositoryMetadata metadata = new ArtifactRepositoryMetadata( artifact ); + File metadataFile = + new File( targetRepository.getBasedir(), targetRepository.pathOfRemoteRepositoryMetadata( metadata ) ); + assertFalse( "Check metadata not created", metadataFile.exists() ); +*/ } public void testMergeArtifactMetadata() @@ -834,4 +881,16 @@ public class RepositoryConverterTest return (ArtifactResult) reporter.getArtifactWarningIterator().next(); } + private void createModernSourceRepository() + throws Exception + { + ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File sourceBase = getTestFile( "src/test/source-modern-repository" ); + sourceRepository = + factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null, null ); + } + } diff --git a/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.jar b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.jar new file mode 100644 index 000000000..72af4bc10 --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.jar @@ -0,0 +1 @@ +incorrectMd5 diff --git a/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.pom b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.pom new file mode 100644 index 000000000..e9ec6a15c --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/1.0.0/incorrectArtifactMetadata-1.0.0.pom @@ -0,0 +1,22 @@ + + + + 3 + incorrectArtifactMetadata + test + 1.0.0 + diff --git a/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/maven-metadata.xml b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/maven-metadata.xml new file mode 100644 index 000000000..5ed142599 --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/incorrectArtifactMetadata/maven-metadata.xml @@ -0,0 +1,25 @@ + + + + test + incorrectArtifactMetadata + + + 0.9 + + + \ No newline at end of file diff --git a/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.jar b/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.jar new file mode 100644 index 000000000..72af4bc10 --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.jar @@ -0,0 +1 @@ +incorrectMd5 diff --git a/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.pom b/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.pom new file mode 100644 index 000000000..70118f64a --- /dev/null +++ b/maven-repository-converter/src/test/source-modern-repository/test/incorrectSnapshotMetadata/1.0.0-SNAPSHOT/incorrectSnapshotMetadata-1.0.0-20060102.030405-6.pom @@ -0,0 +1,22 @@ + + + + 3 + incorrectSnapshotMetadata + test + 1.0.0-20060102.030405-6 +