[MRM-1212]

o move fixing of incorrect checksums to ArtifactMissingChecksumConsumer
o added tests


git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@798059 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2009-07-27 07:34:40 +00:00
parent 71b644e878
commit a4bd8b9c5a
10 changed files with 102 additions and 47 deletions

View File

@ -19,6 +19,8 @@ package org.apache.maven.archiva.consumers.core;
* under the License.
*/
import org.apache.archiva.checksum.ChecksumAlgorithm;
import org.apache.archiva.checksum.ChecksummedFile;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ConfigurationNames;
import org.apache.maven.archiva.configuration.FileTypes;
@ -26,9 +28,6 @@ import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
import org.apache.maven.archiva.consumers.ConsumerException;
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
import org.codehaus.plexus.digest.ChecksumFile;
import org.codehaus.plexus.digest.Digester;
import org.codehaus.plexus.digest.DigesterException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.registry.Registry;
@ -41,7 +40,7 @@ import java.util.Date;
import java.util.List;
/**
* ArtifactMissingChecksumsConsumer - Create missing checksums for the artifact.
* ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
*
* @version $Id$
*/
@ -57,11 +56,7 @@ public class ArtifactMissingChecksumsConsumer
private FileTypes filetypes;
private Digester digestSha1;
private Digester digestMd5;
private ChecksumFile checksum;
private ChecksummedFile checksum;
private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
@ -76,17 +71,11 @@ public class ArtifactMissingChecksumsConsumer
public ArtifactMissingChecksumsConsumer(String id,
String description,
ArchivaConfiguration configuration,
FileTypes filetypes,
Digester digestSha1,
Digester digestMd5,
ChecksumFile checksum) {
FileTypes filetypes) {
this.id = id;
this.description = description;
this.configuration = configuration;
this.filetypes = filetypes;
this.digestSha1 = digestSha1;
this.digestMd5 = digestMd5;
this.checksum = checksum;
}
public String getId()
@ -128,37 +117,51 @@ public class ArtifactMissingChecksumsConsumer
public void processFile( String path )
throws ConsumerException
{
createIfMissing( path, digestSha1 );
createIfMissing( path, digestMd5 );
createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } );
}
private void createIfMissing( String path, Digester digester )
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
{
File checksumFile = new File( this.repositoryDir, path + digester.getFilenameExtension() );
if ( !checksumFile.exists() )
File artifactFile = new File( this.repositoryDir, path );
File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
if( checksumFile.exists() )
{
checksum = new ChecksummedFile( artifactFile );
try
{
checksum.createChecksum( new File( this.repositoryDir, path ), digester );
triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
}
catch ( DigesterException e )
{
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC,
"Cannot calculate checksum for file " + checksumFile + ": " + e.getMessage() );
if( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
{
checksum.fixChecksums( checksumAlgorithm );
triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
}
}
catch ( IOException e )
{
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE,
"Cannot create checksum for file " + checksumFile + ": " + e.getMessage() );
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
": " + e.getMessage() );
}
}
else if ( !checksumFile.isFile() )
else if( !checksumFile.exists() )
{
checksum = new ChecksummedFile( artifactFile );
try
{
checksum.createChecksum( checksumAlgorithm[0] );
triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
}
catch ( IOException e )
{
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
": " + e.getMessage() );
}
}
else
{
triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
"Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
}
}
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )

View File

@ -8,7 +8,7 @@
<value>create-missing-checksums</value>
</constructor-arg>
<constructor-arg>
<value>Create Missing Checksums (.sha1, .md5)</value>
<value>Create Missing and/or Fix Invalid Checksums (.sha1, .md5)</value>
</constructor-arg>
<constructor-arg>
<ref bean="archivaConfiguration"/>
@ -16,14 +16,5 @@
<constructor-arg>
<ref bean="fileTypes"/>
</constructor-arg>
<constructor-arg>
<ref bean="digester#sha1"/>
</constructor-arg>
<constructor-arg>
<ref bean="digester#md5"/>
</constructor-arg>
<constructor-arg>
<ref bean="checksumFile"/>
</constructor-arg>
</bean>
</beans>

View File

@ -1,5 +1,12 @@
package org.apache.maven.archiva.consumers.core;
import java.io.File;
import java.util.Calendar;
import org.apache.archiva.checksum.ChecksumAlgorithm;
import org.apache.archiva.checksum.ChecksummedFile;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -22,12 +29,62 @@ package org.apache.maven.archiva.consumers.core;
public class ArtifactMissingChecksumsConsumerTest
extends AbstractArtifactConsumerTest
{
private ManagedRepositoryConfiguration repoConfig;
@Override
protected void setUp()
throws Exception
{
super.setUp();
repoConfig = new ManagedRepositoryConfiguration();
repoConfig.setId( "test-repo" );
repoConfig.setName( "Test Repository" );
repoConfig.setLayout( "default" );
repoConfig.setLocation( new File( getBasedir(), "target/test-classes/test-repo/" ).getPath() );
consumer = (ArtifactMissingChecksumsConsumer) lookup( "artifactMissingChecksumsConsumer" );
}
public void testNoExistingChecksums()
throws Exception
{
String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
File md5File = new File( repoConfig.getLocation(), path + ".md5" );
assertFalse( sha1File.exists() );
assertFalse( md5File.exists() );
consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
consumer.processFile( path );
assertTrue( sha1File.exists() );
assertTrue( md5File.exists() );
}
public void testExistingIncorrectChecksums()
throws Exception
{
String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
File md5File = new File( repoConfig.getLocation(), path + ".md5" );
ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
assertTrue( sha1File.exists() );
assertTrue( md5File.exists() );
assertFalse( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
consumer.processFile( path );
assertTrue( sha1File.exists() );
assertTrue( md5File.exists() );
assertTrue( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
}
}