From 1e5e969496ac10c8d4e27bec627935e59d66f8a1 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Fri, 16 Jun 2006 06:01:50 +0000 Subject: [PATCH] Changed boolean Digester methods to throw an exception instead for a more informative failure reason git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@414745 13f79535-47bb-0310-9956-ffa450edef68 --- .../converter/DefaultRepositoryConverter.java | 27 ++- .../indexing/ArtifactRepositoryIndex.java | 13 +- .../indexing/EclipseRepositoryIndex.java | 19 +- .../indexing/PomRepositoryIndex.java | 15 +- .../reporting/ChecksumArtifactReporter.java | 19 +- .../reporting/ChecksumMetadataReporter.java | 19 +- .../DuplicateArtifactFileReportProcessor.java | 9 +- ...tractChecksumArtifactReporterTestCase.java | 10 +- .../ChecksumArtifactReporterTest.java | 8 +- .../repository/digest/DefaultDigester.java | 50 ++++-- .../maven/repository/digest/Digester.java | 9 +- .../repository/digest/DigesterException.java | 34 ++++ .../maven/repository/digest/DigesterTest.java | 168 +++++++++++++++--- 13 files changed, 260 insertions(+), 140 deletions(-) create mode 100644 maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigesterException.java 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 e8252dca0..28d398bee 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 @@ -19,8 +19,6 @@ package org.apache.maven.repository.converter; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; -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.Metadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; @@ -39,6 +37,7 @@ import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader; import org.apache.maven.repository.converter.transaction.FileTransaction; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.apache.maven.repository.reporting.ArtifactReporter; import org.codehaus.plexus.i18n.I18N; import org.codehaus.plexus.util.FileUtils; @@ -51,7 +50,6 @@ import java.io.FileReader; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; -import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -612,17 +610,10 @@ public class DefaultRepositoryConverter { boolean result; - try - { - result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact, - "failure.incorrect.md5" ); - result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact, - "failure.incorrect.sha1" ); - } - catch ( NoSuchAlgorithmException e ) - { - throw new RepositoryConversionException( "Error copying artifact: " + e.getMessage(), e ); - } + result = verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact, + "failure.incorrect.md5" ); + result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact, + "failure.incorrect.sha1" ); return result; } @@ -632,7 +623,7 @@ public class DefaultRepositoryConverter ArtifactReporter reporter, Artifact artifact, String key ) - throws IOException, NoSuchAlgorithmException + throws IOException { boolean result = true; @@ -640,7 +631,11 @@ public class DefaultRepositoryConverter if ( md5.exists() ) { String checksum = FileUtils.fileRead( md5 ); - if ( !digester.verifyChecksum( file, checksum, algorithm ) ) + try + { + digester.verifyChecksum( file, checksum, algorithm ); + } + catch ( DigesterException e ) { reporter.addFailure( artifact, getI18NString( key ) ); result = false; diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java index 8a1f8fb55..c86908109 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java @@ -22,11 +22,10 @@ import org.apache.lucene.index.Term; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; @@ -158,18 +157,10 @@ public class ArtifactRepositoryIndex sha1sum = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); md5sum = digester.createChecksum( artifact.getFile(), Digester.MD5 ); } - catch ( NoSuchAlgorithmException e ) + catch ( DigesterException e ) { throw new RepositoryIndexException( "Unable to create a checksum", e ); } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file", e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Error reading from artifact file", e ); - } try { diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java index a0a298c84..20eb10375 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/EclipseRepositoryIndex.java @@ -26,21 +26,20 @@ import org.apache.lucene.document.Field; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Reader; -import java.security.NoSuchAlgorithmException; -import java.util.Enumeration; -import java.util.Collections; -import java.util.List; import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; import java.util.Iterator; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; @@ -150,15 +149,7 @@ public class EclipseRepositoryIndex { md5 = digester.createChecksum( artifactFile, "MD5" ); } - catch ( FileNotFoundException e ) - { - throw new RepositoryIndexException( "Unable to compute checksum.", e ); - } - catch ( NoSuchAlgorithmException e ) - { - throw new RepositoryIndexException( "Unable to compute checksum.", e ); - } - catch ( IOException e ) + catch ( DigesterException e ) { throw new RepositoryIndexException( "Unable to compute checksum.", e ); } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java index 0a13c9716..7396ecc02 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java @@ -28,12 +28,11 @@ import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.ReportPlugin; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.codehaus.plexus.util.StringUtils; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -310,17 +309,9 @@ public class PomRepositoryIndex { return digester.createChecksum( new File( file ), algorithm ); } - catch ( FileNotFoundException e ) + catch ( DigesterException e ) { - throw new RepositoryIndexException( e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( e.getMessage(), e ); - } - catch ( NoSuchAlgorithmException e ) - { - throw new RepositoryIndexException( e.getMessage(), e ); + throw new RepositoryIndexException( "Failed to create checksum", e ); } } diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumArtifactReporter.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumArtifactReporter.java index 7048fae23..838045a68 100644 --- a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumArtifactReporter.java +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumArtifactReporter.java @@ -20,11 +20,11 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; -import java.security.NoSuchAlgorithmException; /** * This class reports invalid and mismatched checksums of artifacts and metadata files. @@ -74,22 +74,17 @@ public class ChecksumArtifactReporter { try { - if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) ) - { - reporter.addSuccess( artifact ); - } - else - { - reporter.addFailure( artifact, checksumAlgorithm + " checksum does not match." ); - } + digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ); + + reporter.addSuccess( artifact ); } - catch ( NoSuchAlgorithmException e ) + catch ( DigesterException e ) { - reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); + reporter.addFailure( artifact, e.getMessage() ); } catch ( IOException e ) { - reporter.addFailure( artifact, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); + reporter.addFailure( artifact, "Read file error: " + e.getMessage() ); } } else diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumMetadataReporter.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumMetadataReporter.java index e33e6f8ca..885d44d15 100644 --- a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumMetadataReporter.java +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/ChecksumMetadataReporter.java @@ -19,11 +19,11 @@ package org.apache.maven.repository.reporting; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; -import java.security.NoSuchAlgorithmException; /** * This class reports invalid and mismatched checksums of artifacts and metadata files. @@ -69,22 +69,17 @@ public class ChecksumMetadataReporter { try { - if ( digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ) ) - { - reporter.addSuccess( metadata ); - } - else - { - reporter.addFailure( metadata, checksumAlgorithm + " checksum does not match." ); - } + digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm ); + + reporter.addSuccess( metadata ); } - catch ( NoSuchAlgorithmException e ) + catch ( DigesterException e ) { - reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); + reporter.addFailure( metadata, e.getMessage() ); } catch ( IOException e ) { - reporter.addFailure( metadata, "Unable to read " + checksumAlgorithm + ": " + e.getMessage() ); + reporter.addFailure( metadata, "Read file error: " + e.getMessage() ); } } else diff --git a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/DuplicateArtifactFileReportProcessor.java b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/DuplicateArtifactFileReportProcessor.java index 3d6655531..b5f5d7c3d 100644 --- a/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/DuplicateArtifactFileReportProcessor.java +++ b/maven-repository-reports-standard/src/main/java/org/apache/maven/repository/reporting/DuplicateArtifactFileReportProcessor.java @@ -20,6 +20,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.apache.maven.repository.indexing.RepositoryIndex; import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexSearchException; @@ -30,8 +31,6 @@ import org.apache.maven.repository.indexing.query.Query; import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import java.io.File; -import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.List; @@ -88,11 +87,7 @@ public class DuplicateArtifactFileReportProcessor { checksum = digester.createChecksum( artifact.getFile(), algorithm ); } - catch ( IOException e ) - { - throw new ReportProcessorException( "Failed to generate checksum", e ); - } - catch ( NoSuchAlgorithmException e ) + catch ( DigesterException e ) { throw new ReportProcessorException( "Failed to generate checksum", e ); } diff --git a/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractChecksumArtifactReporterTestCase.java b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractChecksumArtifactReporterTestCase.java index c8d03eb81..86b60cdca 100644 --- a/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractChecksumArtifactReporterTestCase.java +++ b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/AbstractChecksumArtifactReporterTestCase.java @@ -17,6 +17,7 @@ package org.apache.maven.repository.reporting; */ import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; @@ -28,7 +29,6 @@ import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.security.NoSuchAlgorithmException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; @@ -61,7 +61,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase * @param type The type of checksum file to be created. */ protected void createChecksumFile( String type ) - throws NoSuchAlgorithmException, IOException + throws DigesterException, IOException { //loop through the valid artifact names.. if ( "VALID".equals( type ) ) @@ -86,7 +86,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase * @param type The type of checksum to be created. (Valid or invalid) */ protected void createMetadataFile( String type ) - throws NoSuchAlgorithmException, IOException + throws DigesterException, IOException { //loop through the valid artifact names.. if ( "VALID".equals( type ) ) @@ -110,7 +110,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase * @param isValid Indicates whether the checksum to be created is valid or not. */ private void writeChecksumFile( String relativePath, String filename, String type, boolean isValid ) - throws IOException, NoSuchAlgorithmException + throws IOException, DigesterException { //Initialize variables for creating jar files String repoUrl = repository.getBasedir(); @@ -177,7 +177,7 @@ public abstract class AbstractChecksumArtifactReporterTestCase * @param isValid Indicates whether the checksum to be created is valid or not. */ private void writeMetadataFile( String relativePath, String filename, String type, boolean isValid ) - throws IOException, NoSuchAlgorithmException + throws IOException, DigesterException { //create checksum for the metadata file.. String repoUrl = repository.getBasedir(); diff --git a/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/ChecksumArtifactReporterTest.java b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/ChecksumArtifactReporterTest.java index 1e2f2a8da..64585c52d 100644 --- a/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/ChecksumArtifactReporterTest.java +++ b/maven-repository-reports-standard/src/test/java/org/apache/maven/repository/reporting/ChecksumArtifactReporterTest.java @@ -25,10 +25,10 @@ import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.repository.digest.DigesterException; import java.io.File; import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.util.Iterator; /** @@ -56,7 +56,7 @@ public class ChecksumArtifactReporterTest * Test the ChecksumArtifactReporter when the checksum files are valid. */ public void testChecksumArtifactReporterSuccess() - throws ReportProcessorException, NoSuchAlgorithmException, IOException + throws ReportProcessorException, IOException, DigesterException { createChecksumFile( "VALID" ); createChecksumFile( "INVALID" ); @@ -90,7 +90,7 @@ public class ChecksumArtifactReporterTest * The reporter should report 2 success validation. */ public void testChecksumMetadataReporterSuccess() - throws ReportProcessorException, NoSuchAlgorithmException, IOException + throws ReportProcessorException, DigesterException, IOException { createMetadataFile( "VALID" ); createMetadataFile( "INVALID" ); @@ -195,7 +195,7 @@ public class ChecksumArtifactReporterTest * Test the conditional when the checksum files of the artifact & metadata do not exist. */ public void testChecksumFilesDoNotExist() - throws ReportProcessorException, NoSuchAlgorithmException, IOException + throws ReportProcessorException, DigesterException, IOException { createChecksumFile( "VALID" ); createMetadataFile( "VALID" ); diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java index 4974fb05d..a342227a3 100644 --- a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DefaultDigester.java @@ -17,11 +17,13 @@ package org.apache.maven.repository.digest; */ import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.FileNotFoundException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; @@ -41,11 +43,28 @@ public class DefaultDigester private static final int BYTE_MASK = 0xFF; public String createChecksum( File file, String algorithm ) - throws IOException, NoSuchAlgorithmException + throws DigesterException { - MessageDigest digest = MessageDigest.getInstance( algorithm ); + MessageDigest digest; + try + { + digest = MessageDigest.getInstance( algorithm ); + } + catch ( NoSuchAlgorithmException e ) + { + throw new DigesterException( "Specified algorithm not found: " + algorithm, e ); + } + + InputStream fis = null; + try + { + fis = new FileInputStream( file ); + } + catch ( FileNotFoundException e ) + { + throw new DigesterException( "Specified file not found: " + file.getAbsolutePath(), e ); + } - InputStream fis = new FileInputStream( file ); try { byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE]; @@ -60,6 +79,10 @@ public class DefaultDigester } while ( numRead != -1 ); } + catch( IOException e ) + { + throw new DigesterException( "Failed to read from file: " + file.getAbsolutePath(), e ); + } finally { IOUtil.close( fis ); @@ -68,11 +91,9 @@ public class DefaultDigester return byteArrayToHexStr( digest.digest() ); } - public boolean verifyChecksum( File file, String checksum, String algorithm ) - throws NoSuchAlgorithmException, IOException + public void verifyChecksum( File file, String checksum, String algorithm ) + throws DigesterException { - boolean result = true; - String trimmedChecksum = checksum.replace( '\n', ' ' ).trim(); // Free-BSD / openssl Matcher m = @@ -83,8 +104,7 @@ public class DefaultDigester String filename = m.group( 1 ); if ( !filename.equals( file.getName() ) ) { - // TODO: provide better warning - result = false; + throw new DigesterException( "Supplied checksum does not match checksum pattern" ); } trimmedChecksum = m.group( 2 ); } @@ -97,20 +117,18 @@ public class DefaultDigester String filename = m.group( 2 ); if ( !filename.equals( file.getName() ) ) { - // TODO: provide better warning - result = false; + throw new DigesterException( "Supplied checksum does not match checksum pattern" ); } trimmedChecksum = m.group( 1 ); } } - if ( result ) + //Create checksum for jar file + String sum = createChecksum( file, algorithm ); + if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) ) { - //Create checksum for jar file - String sum = createChecksum( file, algorithm ); - result = trimmedChecksum.toUpperCase().equals( sum.toUpperCase() ); + throw new DigesterException( "Checksum failed" ); } - return result; } /** diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java index 1757dab56..ef555bb74 100644 --- a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/Digester.java @@ -17,9 +17,6 @@ package org.apache.maven.repository.digest; */ import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.NoSuchAlgorithmException; /** * Create a digest for a file. @@ -35,8 +32,8 @@ public interface Digester String MD5 = "MD5"; String createChecksum( File file, String algorithm ) - throws FileNotFoundException, IOException, NoSuchAlgorithmException; + throws DigesterException; - boolean verifyChecksum( File file, String checksum, String algorithm ) - throws NoSuchAlgorithmException, IOException; + void verifyChecksum( File file, String checksum, String algorithm ) + throws DigesterException; } diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigesterException.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigesterException.java new file mode 100644 index 000000000..789957b84 --- /dev/null +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/digest/DigesterException.java @@ -0,0 +1,34 @@ +package org.apache.maven.repository.digest; + +/* + * 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. + */ + +/** + * @author Edwin Punzalan + */ +public class DigesterException + extends Exception +{ + public DigesterException( String message ) + { + super( message ); + } + + public DigesterException( String message, Throwable cause ) + { + super( message, cause ); + } +} diff --git a/maven-repository-utils/src/test/java/org/apache/maven/repository/digest/DigesterTest.java b/maven-repository-utils/src/test/java/org/apache/maven/repository/digest/DigesterTest.java index 20a86e18b..31a47376c 100644 --- a/maven-repository-utils/src/test/java/org/apache/maven/repository/digest/DigesterTest.java +++ b/maven-repository-utils/src/test/java/org/apache/maven/repository/digest/DigesterTest.java @@ -39,54 +39,172 @@ public class DigesterTest private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2"; public void testBareDigestFormat() - throws NoSuchAlgorithmException, IOException + throws DigesterException, IOException { File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); - assertTrue( "test bare format MD5", digester.verifyChecksum( file, MD5, Digester.MD5 ) ); - assertTrue( "test bare format SHA1", digester.verifyChecksum( file, SHA1, Digester.SHA1 ) ); - assertFalse( "test wrong sha1", digester.verifyChecksum( file, WRONG_SHA1, Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, MD5, Digester.MD5 ); + } + catch ( DigesterException e ) + { + fail( "Bare format MD5 must not throw exception" ); + } + + try + { + digester.verifyChecksum( file, SHA1, Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "Bare format SHA1 must not throw exception" ); + } + + try + { + digester.verifyChecksum( file, WRONG_SHA1, Digester.SHA1 ); + fail( "wrong checksum must throw an exception" ); + } + catch ( DigesterException e ) + { + //expected + } } public void testOpensslDigestFormat() - throws NoSuchAlgorithmException, IOException + throws IOException { File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); - assertTrue( "test openssl format MD5", - digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 ) ); - assertTrue( "test openssl format SHA1", - digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 ); + } + catch ( DigesterException e ) + { + fail( "OpenSSL MD5 format must not cause exception" ); + } - assertTrue( "test freebsd format MD5", - digester.verifyChecksum( file, "MD5 (test-file.txt) = " + MD5, Digester.MD5 ) ); - assertTrue( "test freebsd format SHA1", - digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + SHA1, Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "OpenSSL SHA1 format must not cause exception" ); + } - assertFalse( "test wrong filename", digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 ) ); - assertFalse( "test wrong sha1", - digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, "MD5 (test-file.txt) = " + MD5, Digester.MD5 ); + } + catch ( DigesterException e ) + { + fail( "FreeBSD MD5 format must not cause exception" ); + } + + try + { + digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + SHA1, Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "FreeBSD SHA1 format must not cause exception" ); + } + + try + { + digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 ); + fail( "Wrong filename should cause an exception" ); + } + catch ( DigesterException e ) + { + //expected + } + + try + { + digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 ); + fail( "Wrong sha1 should cause an exception" ); + } + catch ( DigesterException e ) + { + //expected + } } public void testGnuDigestFormat() throws NoSuchAlgorithmException, IOException { File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); - assertTrue( "test GNU format MD5", digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 ) ); - assertTrue( "test GNU format SHA1", digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 ); + } + catch ( DigesterException e ) + { + fail( "GNU format MD5 must not cause exception" ); + } - assertTrue( "test GNU text format MD5", digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 ) ); - assertTrue( "test GNU text format SHA1", - digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "GNU format SHA1 must not cause exception" ); + } - assertFalse( "test wrong filename", digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 ) ); - assertFalse( "test wrong sha1", digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 ); + } + catch ( DigesterException e ) + { + fail( "GNU text format MD5 must not cause exception" ); + } + + try + { + digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "GNU text format SHA1 must not cause exception" ); + } + + try + { + digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 ); + fail( "Wrong filename cause an exception" ); + } + catch ( DigesterException e ) + { + //expected + } + + try + { + digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 ); + fail( "Wrong SHA1 cause an exception" ); + } + catch ( DigesterException e ) + { + //expected + } } public void testUntrimmedContent() throws NoSuchAlgorithmException, IOException { File file = new File( getClass().getResource( "/test-file.txt" ).getPath() ); - assertTrue( "test untrimmed GNU format SHA1", - digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 ) ); + try + { + digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 ); + } + catch ( DigesterException e ) + { + fail( "GNU untrimmed SHA1 must not cause exception" ); + } } }