mirror of https://github.com/apache/archiva.git
[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
This commit is contained in:
parent
426cafec16
commit
d215b81192
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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.
|
||||
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.
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
incorrectMd5
|
|
@ -0,0 +1,22 @@
|
|||
<!--
|
||||
~ Copyright 2005-2006 The Apache Software Foundation.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<project>
|
||||
<pomVersion>3</pomVersion>
|
||||
<artifactId>incorrectArtifactMetadata</artifactId>
|
||||
<groupId>test</groupId>
|
||||
<currentVersion>1.0.0</currentVersion>
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
<!--
|
||||
~ Copyright 2005-2006 The Apache Software Foundation.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<metadata>
|
||||
<groupId>test</groupId>
|
||||
<artifactId>incorrectArtifactMetadata</artifactId>
|
||||
<versioning>
|
||||
<versions>
|
||||
<version>0.9</version>
|
||||
</versions>
|
||||
</versioning>
|
||||
</metadata>
|
|
@ -0,0 +1 @@
|
|||
incorrectMd5
|
|
@ -0,0 +1,22 @@
|
|||
<!--
|
||||
~ Copyright 2005-2006 The Apache Software Foundation.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<project>
|
||||
<pomVersion>3</pomVersion>
|
||||
<artifactId>incorrectSnapshotMetadata</artifactId>
|
||||
<groupId>test</groupId>
|
||||
<currentVersion>1.0.0-20060102.030405-6</currentVersion>
|
||||
</project>
|
Loading…
Reference in New Issue