[MRM-9] checksum tests

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@372727 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-01-27 04:35:37 +00:00
parent ae38360d9c
commit 00ea26352a
17 changed files with 241 additions and 126 deletions

View File

@ -37,5 +37,10 @@
<groupId>org.apache.maven.repository</groupId>
<artifactId>maven-repository-reports-standard</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-i18n</artifactId>
<version>1.0-beta-6</version>
</dependency>
</dependencies>
</project>

View File

@ -28,6 +28,9 @@ import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
import org.apache.maven.model.converter.ArtifactPomRewriter;
import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.reporting.ArtifactReporter;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@ -38,9 +41,10 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
/**
@ -52,6 +56,11 @@ import java.util.regex.Matcher;
public class DefaultRepositoryConverter
implements RepositoryConverter
{
/**
* @plexus.requirement
*/
private Digester digester;
/**
* @plexus.requirement
*/
@ -63,46 +72,52 @@ public class DefaultRepositoryConverter
private ArtifactPomRewriter rewriter;
/**
* @plexus.configuration
* @plexus.configuration default-value="false"
*/
private boolean force;
/**
* @plexus.configuration
* @plexus.configuration default-value="false"
*/
private boolean dryrun;
public List convert( Artifact artifact, ArtifactRepository targetRepository )
/**
* @plexus.requirement
*/
private I18N i18n;
public void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException
{
copyArtifact( artifact, targetRepository );
List warnings = copyPom( artifact, targetRepository );
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 );
return warnings;
}
private static Metadata createBaseMetadata( Artifact artifact )
@ -175,11 +190,9 @@ public class DefaultRepositoryConverter
}
}
private List copyPom( Artifact artifact, ArtifactRepository targetRepository )
private void copyPom( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException
{
List warnings = new ArrayList();
Artifact pom = artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
artifact.getVersion() );
pom.setBaseVersion( artifact.getBaseVersion() );
@ -191,17 +204,22 @@ public class DefaultRepositoryConverter
// TODO: utility methods in the model converter
File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pom ) );
String contents;
String contents = null;
boolean checksumsValid = false;
try
{
contents = FileUtils.fileRead( file );
if ( testChecksums( artifact, file, reporter ) )
{
checksumsValid = true;
contents = FileUtils.fileRead( file );
}
}
catch ( IOException e )
{
throw new RepositoryConversionException( "Unable to read source POM: " + e.getMessage(), e );
}
if ( contents.indexOf( "modelVersion" ) >= 0 )
if ( checksumsValid && contents.indexOf( "modelVersion" ) >= 0 )
{
// v4 POM
try
@ -239,7 +257,13 @@ public class DefaultRepositoryConverter
rewriter.rewrite( stringReader, fileWriter, false, artifact.getGroupId(), artifact.getArtifactId(),
artifact.getVersion(), artifact.getType() );
warnings = rewriter.getWarnings();
List warnings = rewriter.getWarnings();
for ( Iterator i = warnings.iterator(); i.hasNext(); )
{
String message = (String) i.next();
reporter.addWarning( artifact, message );
}
IOUtil.close( fileWriter );
}
@ -254,16 +278,54 @@ public class DefaultRepositoryConverter
}
}
}
return warnings;
}
private void copyArtifact( Artifact artifact, ArtifactRepository targetRepository )
private boolean testChecksums( Artifact artifact, File file, ArtifactReporter reporter )
throws IOException, RepositoryConversionException
{
boolean result = true;
try
{
File md5 = new File( file.getParentFile(), file.getName() + ".md5" );
if ( md5.exists() )
{
String checksum = FileUtils.fileRead( md5 );
if ( !digester.verifyChecksum( file, checksum, Digester.MD5 ) )
{
reporter.addFailure( artifact, i18n.getString( getClass().getName(), Locale.getDefault(),
"failure.incorrect.md5" ) );
result = false;
}
}
File sha1 = new File( file.getParentFile(), file.getName() + ".sha1" );
if ( sha1.exists() )
{
String checksum = FileUtils.fileRead( sha1 );
if ( !digester.verifyChecksum( file, checksum, Digester.SHA1 ) )
{
reporter.addFailure( artifact, i18n.getString( getClass().getName(), Locale.getDefault(),
"failure.incorrect.sha1" ) );
result = false;
}
}
}
catch ( NoSuchAlgorithmException e )
{
throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e );
}
return result;
}
private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException
{
File sourceFile = artifact.getFile();
File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
boolean result = true;
try
{
boolean matching = false;
@ -273,9 +335,16 @@ public class DefaultRepositoryConverter
}
if ( force || !matching )
{
if ( !dryrun )
if ( testChecksums( artifact, sourceFile, reporter ) )
{
FileUtils.copyFile( sourceFile, targetFile );
if ( !dryrun )
{
FileUtils.copyFile( sourceFile, targetFile );
}
}
else
{
result = false;
}
}
}
@ -283,15 +352,16 @@ public class DefaultRepositoryConverter
{
throw new RepositoryConversionException( "Error copying artifact", e );
}
return result;
}
public void convert( List artifacts, ArtifactRepository targetRepository )
public void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException
{
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
convert( artifact, targetRepository );
convert( artifact, targetRepository, reporter );
}
}
}

View File

@ -18,6 +18,7 @@ package org.apache.maven.repository.converter;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.reporting.ArtifactReporter;
import java.util.List;
@ -35,9 +36,9 @@ public interface RepositoryConverter
*
* @param artifact the artifact to convert
* @param targetRepository the target repository
* @return a list of warnings occuring during the conversion
* @param reporter reporter to track the results of the conversion
*/
List convert( Artifact artifact, ArtifactRepository targetRepository )
void convert( Artifact artifact, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException;
/**
@ -45,7 +46,8 @@ public interface RepositoryConverter
*
* @param artifacts the set of artifacts to convert
* @param targetRepository the target repository
* @param reporter reporter to track the results of the conversions
*/
void convert( List artifacts, ArtifactRepository targetRepository )
void convert( List artifacts, ArtifactRepository targetRepository, ArtifactReporter reporter )
throws RepositoryConversionException;
}

View File

@ -0,0 +1,18 @@
#
# 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.
#
failure.incorrect.md5=The MD5 checksum value was incorrect.
failure.incorrect.sha1=The SHA1 checksum value was incorrect.

View File

@ -24,7 +24,11 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.repository.reporting.ArtifactReporter;
import org.apache.maven.repository.reporting.ArtifactResult;
import org.apache.maven.repository.reporting.DefaultArtifactReporter;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
@ -32,6 +36,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
/**
@ -54,8 +59,12 @@ public class RepositoryConverterTest
private ArtifactFactory artifactFactory;
private ArtifactReporter reporter;
private static final int SLEEP_MILLIS = 100;
private I18N i18n;
protected void setUp()
throws Exception
{
@ -80,6 +89,10 @@ public class RepositoryConverterTest
repositoryConverter = (RepositoryConverter) lookup( RepositoryConverter.ROLE, "default" );
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
i18n = (I18N) lookup( I18N.ROLE );
reporter = new DefaultArtifactReporter();
}
public void testV4PomConvert()
@ -98,7 +111,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
versionMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -140,7 +154,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
versionMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -182,7 +197,10 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( versionMetadata ) );
versionMetadataFile.delete();
List warnings = repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
assertEquals( "check no errors", 0, reporter.getFailures() );
assertEquals( "check number of warnings", 2, reporter.getWarnings() );
assertEquals( "check success", 1, reporter.getSuccesses() );
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -195,8 +213,6 @@ public class RepositoryConverterTest
compareFiles( expectedPomFile, pomFile );
assertEquals( "check number of warnings", 2, warnings.size() );
// TODO: check 2 warnings (extend and versions) matched on i18n key
}
@ -216,7 +232,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
snapshotMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -258,7 +275,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
snapshotMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -300,7 +318,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
snapshotMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -342,7 +361,8 @@ public class RepositoryConverterTest
targetRepository.pathOfRemoteRepositoryMetadata( snapshotMetadata ) );
snapshotMetadataFile.delete();
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -374,7 +394,8 @@ public class RepositoryConverterTest
// test that a POM is created when there was none at the source
Artifact artifact = createArtifact( "test", "noPomArtifact", "1.0.0" );
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -388,36 +409,38 @@ public class RepositoryConverterTest
assertFalse( "No source POM", sourcePomFile.exists() );
}
public void testInvalidSourceChecksumMd5()
throws RepositoryConversionException
{
// test that it fails when the source md5 is not a valid md5
Artifact artifact = createArtifact( "test", "invalidMd5Artifact", "1.0.0" );
repositoryConverter.convert( artifact, targetRepository );
// TODO: check for failure
}
public void testInvalidSourceChecksumSha1()
{
// test that it fails when the source sha1 is not a valid sha1
// TODO: using exceptions at this level, or passing in reporter?
}
public void testIncorrectSourceChecksumMd5()
throws RepositoryConversionException
{
// test that it fails when the source md5 is wrong
// TODO: using exceptions at this level, or passing in reporter?
Artifact artifact = createArtifact( "test", "incorrectMd5Artifact", "1.0.0" );
File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
file.delete();
repositoryConverter.convert( artifact, targetRepository, reporter );
checkFailure();
ArtifactResult failure = (ArtifactResult) reporter.getArtifactFailureIterator().next();
assertEquals( "check failure message", getI18nString( "failure.incorrect.md5" ), failure.getReason() );
assertFalse( "Check artifact not created", file.exists() );
}
public void testIncorrectSourceChecksumSha1()
throws RepositoryConversionException
{
// test that it fails when the source sha1 is wrong
// TODO: using exceptions at this level, or passing in reporter?
Artifact artifact = createArtifact( "test", "incorrectSha1Artifact", "1.0.0" );
File file = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
file.delete();
repositoryConverter.convert( artifact, targetRepository, reporter );
checkFailure();
ArtifactResult failure = (ArtifactResult) reporter.getArtifactFailureIterator().next();
assertEquals( "check failure message", getI18nString( "failure.incorrect.sha1" ), failure.getReason() );
assertFalse( "Check artifact not created", file.exists() );
}
public void testUnmodifiedArtifact()
@ -445,7 +468,8 @@ public class RepositoryConverterTest
// Need to guarantee last modified is not equal
Thread.sleep( SLEEP_MILLIS );
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
compareFiles( sourceFile, targetFile );
compareFiles( sourcePomFile, targetPomFile );
@ -486,7 +510,8 @@ public class RepositoryConverterTest
// Need to guarantee last modified is not equal
Thread.sleep( SLEEP_MILLIS );
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
compareFiles( sourceFile, targetFile );
compareFiles( sourcePomFile, targetPomFile );
@ -510,7 +535,8 @@ public class RepositoryConverterTest
File targetFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
File targetPomFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( pomArtifact ) );
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
assertTrue( "Check source file exists", sourceFile.exists() );
assertTrue( "Check source POM exists", sourcePomFile.exists() );
@ -563,7 +589,10 @@ public class RepositoryConverterTest
artifacts.add( createArtifact( "test", "artifact-one", "1.0.0" ) );
artifacts.add( createArtifact( "test", "artifact-two", "1.0.0" ) );
artifacts.add( createArtifact( "test", "artifact-three", "1.0.0" ) );
repositoryConverter.convert( artifacts, targetRepository );
repositoryConverter.convert( artifacts, targetRepository, reporter );
assertEquals( "check no errors", 0, reporter.getFailures() );
assertEquals( "check no warnings", 0, reporter.getWarnings() );
assertEquals( "check successes", 3, reporter.getSuccesses() );
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
{
@ -604,7 +633,8 @@ public class RepositoryConverterTest
Artifact artifact = createArtifact( "test", "newversion-artifact", "1.0.1" );
repositoryConverter.convert( artifact, targetRepository );
repositoryConverter.convert( artifact, targetRepository, reporter );
checkSuccess();
File artifactFile = new File( targetRepository.getBasedir(), targetRepository.pathOf( artifact ) );
assertTrue( "Check artifact created", artifactFile.exists() );
@ -678,4 +708,23 @@ public class RepositoryConverterTest
return path.trim().replaceAll( "\r\n", "\n" ).replace( '\r', '\n' );
}
private void checkSuccess()
{
assertEquals( "check no errors", 0, reporter.getFailures() );
assertEquals( "check no warnings", 0, reporter.getWarnings() );
assertEquals( "check success", 1, reporter.getSuccesses() );
}
private void checkFailure()
{
assertEquals( "check num errors", 1, reporter.getFailures() );
assertEquals( "check no warnings", 0, reporter.getWarnings() );
assertEquals( "check no success", 0, reporter.getSuccesses() );
}
private String getI18nString( String key )
{
return i18n.getString( repositoryConverter.getClass().getName(), Locale.getDefault(), key );
}
}

View File

@ -0,0 +1 @@
379dcfcd1e6312cc859111f696047eb4

View File

@ -0,0 +1 @@
52e07b82d944741f66bba5896d4cd74e9879e289

View File

@ -0,0 +1,6 @@
<project>
<pomVersion>3</pomVersion>
<artifactId>incorrectMd5Artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0</currentVersion>
</project>

View File

@ -0,0 +1,6 @@
<project>
<pomVersion>3</pomVersion>
<artifactId>incorrectSha1Artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0</currentVersion>
</project>

View File

@ -1,22 +0,0 @@
<!--
~ 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>invalidMd5Artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0</currentVersion>
</project>

View File

@ -19,13 +19,6 @@
<artifactId>v3artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0-20060105.130101-3</currentVersion>
<versions>
<version>
<id>1.0</id>
<name>1.0</name>
<tag>1_0</tag>
</version>
</versions>
<dependencies>
<dependency>
<groupId>groupId</groupId>

View File

@ -19,13 +19,6 @@
<artifactId>v3artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0-SNAPSHOT</currentVersion>
<versions>
<version>
<id>1.0</id>
<name>1.0</name>
<tag>1_0</tag>
</version>
</versions>
<dependencies>
<dependency>
<groupId>groupId</groupId>

View File

@ -3,13 +3,6 @@
<artifactId>v3artifact</artifactId>
<groupId>test</groupId>
<currentVersion>1.0.0</currentVersion>
<versions>
<version>
<id>1.0</id>
<name>1.0</name>
<tag>1_0</tag>
</version>
</versions>
<dependencies>
<dependency>
<groupId>groupId</groupId>