From a4bd8b9c5a7970e311f5794ab621f79e07c333ba Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Mon, 27 Jul 2009 07:34:40 +0000 Subject: [PATCH] [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 --- .../ArtifactMissingChecksumsConsumer.java | 73 ++++++++++--------- .../resources/META-INF/spring-context.xml | 11 +-- .../ArtifactMissingChecksumsConsumerTest.java | 57 +++++++++++++++ .../1.0/correct-checksums-1.0.jar | 0 .../1.0/correct-checksums-1.0.jar.md5 | 1 + .../1.0/correct-checksums-1.0.jar.sha1 | 1 + .../1.0/incorrect-checksums-1.0.jar | 0 .../1.0/incorrect-checksums-1.0.jar.md5 | 1 + .../1.0/incorrect-checksums-1.0.jar.sha1 | 1 + .../1.0/no-checksums-artifact-1.0.jar | 0 10 files changed, 100 insertions(+), 45 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.md5 create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.sha1 create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.md5 create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.sha1 create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java index 9befcbf5f..fa2e28987 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java @@ -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,14 +40,14 @@ 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$ */ public class ArtifactMissingChecksumsConsumer extends AbstractMonitoredConsumer implements KnownRepositoryContentConsumer, RegistryListener, Initializable -{ +{ private String id; private String description; @@ -57,36 +56,26 @@ 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"; - + private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure"; - + private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure"; private File repositoryDir; private List includes = new ArrayList(); - + 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() ); + if( !checksum.isValidChecksum( checksumAlgorithm[0] ) ) + { + checksum.fixChecksums( checksumAlgorithm ); + triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() ); + } } - catch ( DigesterException e ) + catch ( IOException e ) { - triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, - "Cannot calculate checksum for file " + checksumFile + ": " + e.getMessage() ); + triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile + + ": " + e.getMessage() ); + } + } + 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() ); + triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile + + ": " + e.getMessage() ); } } - else if ( !checksumFile.isFile() ) + else { triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE, "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." ); } - } public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/resources/META-INF/spring-context.xml index 70d85196f..fdba18c27 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/resources/META-INF/spring-context.xml @@ -8,7 +8,7 @@ create-missing-checksums - Create Missing Checksums (.sha1, .md5) + Create Missing and/or Fix Invalid Checksums (.sha1, .md5) @@ -16,14 +16,5 @@ - - - - - - - - - \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java index d97e0b0b6..13bc4ac6b 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/maven/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java @@ -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 } ) ); + } } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar new file mode 100644 index 000000000..e69de29bb diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.md5 b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.md5 new file mode 100644 index 000000000..a6a1cc670 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.md5 @@ -0,0 +1 @@ +d41d8cd98f00b204e9800998ecf8427e correct-checksums-1.0.jar \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.sha1 b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.sha1 new file mode 100644 index 000000000..d04ba3482 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/correct-checksums/1.0/correct-checksums-1.0.jar.sha1 @@ -0,0 +1 @@ +da39a3ee5e6b4b0d3255bfef95601890afd80709 \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar new file mode 100644 index 000000000..e69de29bb diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.md5 b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.md5 new file mode 100644 index 000000000..8ee73af27 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.md5 @@ -0,0 +1 @@ +902a360ecad98a34b59863c1e65bcf71 incorrect-checksums-1.0.jar \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.sha1 b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.sha1 new file mode 100644 index 000000000..8f734ae6b --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/incorrect-checksums/1.0/incorrect-checksums-1.0.jar.sha1 @@ -0,0 +1 @@ +dcab88fc2a043c2479a6de676a2f8179e9ea2167 \ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/resources/test-repo/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar new file mode 100644 index 000000000..e69de29bb -- 2.39.5