[MENFORCER-317] - Fix RequireFileChecksum ignores configured message
- Introduce configurable message for nonexistent file
- Including small refactoring: extract checksum calculation into private method.
diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFileChecksum.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFileChecksum.java
index 1ab2aba..1a4c160 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFileChecksum.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFileChecksum.java
@@ -45,6 +45,8 @@
private String type;
+ private String nonexistentFileMessage;
+
@Override
public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
@@ -64,53 +66,38 @@
throw new EnforcerRuleException( "Checksum unspecified" );
}
- InputStream inputStream = null;
- try
+ if ( !this.file.exists() )
{
- if ( this.file.isDirectory() || !this.file.canRead() )
+ String message = nonexistentFileMessage;
+ if ( message == null )
{
- throw new EnforcerRuleException( "Cannot read file: " + this.file.getAbsolutePath() );
+ message = "File does not exist: " + this.file.getAbsolutePath();
}
+ throw new EnforcerRuleException( message );
+ }
- inputStream = new FileInputStream( this.file );
- String checksum;
- if ( "md5".equals( this.type ) )
- {
- checksum = DigestUtils.md5Hex( inputStream );
- }
- else if ( "sha1".equals( this.type ) )
- {
- checksum = DigestUtils.shaHex( inputStream );
- }
- else if ( "sha256".equals( this.type ) )
- {
- checksum = DigestUtils.sha256Hex( inputStream );
- }
- else if ( "sha384".equals( this.type ) )
- {
- checksum = DigestUtils.sha384Hex( inputStream );
- }
- else if ( "sha512".equals( this.type ) )
- {
- checksum = DigestUtils.sha512Hex( inputStream );
- }
- else
- {
- throw new EnforcerRuleException( "Unsupported hash type: " + this.type );
- }
- if ( !checksum.equalsIgnoreCase( this.checksum ) )
- {
- throw new EnforcerRuleException( this.type + " hash of " + this.file + " was " + checksum
- + " but expected " + this.checksum );
- }
- }
- catch ( IOException e )
+ if ( this.file.isDirectory() )
{
- throw new EnforcerRuleException( "Unable to calculate checksum", e );
+ throw new EnforcerRuleException( "Cannot calculate the checksum of directory: "
+ + this.file.getAbsolutePath() );
}
- finally
+
+ if ( !this.file.canRead() )
{
- IOUtil.close( inputStream );
+ throw new EnforcerRuleException( "Cannot read file: " + this.file.getAbsolutePath() );
+ }
+
+ String checksum = calculateChecksum();
+
+ if ( !checksum.equalsIgnoreCase( this.checksum ) )
+ {
+ String exceptionMessage = getMessage();
+ if ( exceptionMessage == null )
+ {
+ exceptionMessage = this.type + " hash of " + this.file + " was " + checksum
+ + " but expected " + this.checksum;
+ }
+ throw new EnforcerRuleException( exceptionMessage );
}
}
@@ -144,4 +131,57 @@
this.type = type;
}
+ /**
+ * The friendly message to use when the file does not exist.
+ *
+ * @param nonexistentFileMessage message
+ */
+ public void setNonexistentFileMessage( String nonexistentFileMessage )
+ {
+ this.nonexistentFileMessage = nonexistentFileMessage;
+ }
+
+ private String calculateChecksum()
+ throws EnforcerRuleException
+ {
+ InputStream inputStream = null;
+ try
+ {
+ inputStream = new FileInputStream( this.file );
+ String checksum;
+ if ( "md5".equals( this.type ) )
+ {
+ checksum = DigestUtils.md5Hex( inputStream );
+ }
+ else if ( "sha1".equals( this.type ) )
+ {
+ checksum = DigestUtils.shaHex( inputStream );
+ }
+ else if ( "sha256".equals( this.type ) )
+ {
+ checksum = DigestUtils.sha256Hex( inputStream );
+ }
+ else if ( "sha384".equals( this.type ) )
+ {
+ checksum = DigestUtils.sha384Hex( inputStream );
+ }
+ else if ( "sha512".equals( this.type ) )
+ {
+ checksum = DigestUtils.sha512Hex( inputStream );
+ }
+ else
+ {
+ throw new EnforcerRuleException( "Unsupported hash type: " + this.type );
+ }
+ return checksum;
+ }
+ catch ( IOException e )
+ {
+ throw new EnforcerRuleException( "Unable to calculate checksum", e );
+ }
+ finally
+ {
+ IOUtil.close( inputStream );
+ }
+ }
}
diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFileChecksum.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFileChecksum.java
index 4333cb3..c7075ce 100644
--- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFileChecksum.java
+++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFileChecksum.java
@@ -74,10 +74,45 @@
}
@Test
- public void testFileChecksumMd5NoFileFailure()
+ public void testFileChecksumMd5GivenFileDoesNotExistFailure()
throws IOException, EnforcerRuleException
{
- File f = new File( "foo" )
+ File f = new File( "nonExistent" );
+
+ expectedException.expect( EnforcerRuleException.class );
+ expectedException.expectMessage( "File does not exist: " + f.getAbsolutePath() );
+
+ rule.setFile( f );
+ rule.setChecksum( "78e731027d8fd50ed642340b7c9a63b3" );
+ rule.setType( "md5" );
+
+ rule.execute( EnforcerTestUtils.getHelper() );
+ }
+
+ @Test
+ public void testFileChecksumMd5GivenFileDoesNotExistFailureWithMessage()
+ throws IOException, EnforcerRuleException
+ {
+ File f = new File( "nonExistent" );
+ String configuredMessage = "testMessageFileDoesNotExist";
+
+ expectedException.expect( EnforcerRuleException.class );
+ expectedException.expectMessage( configuredMessage );
+
+ rule.setFile( f );
+ rule.setChecksum( "78e731027d8fd50ed642340b7c9a63b3" );
+ rule.setType( "md5" );
+ rule.setNonexistentFileMessage( configuredMessage );
+
+ rule.execute( EnforcerTestUtils.getHelper() );
+ }
+
+ @Test
+ public void testFileChecksumMd5GivenFileIsNotReadableFailure()
+ throws IOException, EnforcerRuleException
+ {
+ File f = temporaryFolder.newFile();
+ f = new File( f.getAbsolutePath() )
{
private static final long serialVersionUID = 6987790643999338089L;
@@ -105,7 +140,7 @@
File f = temporaryFolder.newFolder();
expectedException.expect( EnforcerRuleException.class );
- expectedException.expectMessage( "Cannot read file: " + f.getAbsolutePath() );
+ expectedException.expectMessage( "Cannot calculate the checksum of directory: " + f.getAbsolutePath() );
rule.setFile( f );
rule.setChecksum( "78e731027d8fd50ed642340b7c9a63b3" );
@@ -176,6 +211,25 @@
}
@Test
+ public void testFileChecksumMd5ChecksumMismatchFailureWithMessage()
+ throws IOException, EnforcerRuleException
+ {
+ File f = temporaryFolder.newFile();
+ FileUtils.fileWrite( f, "message" );
+ String configuredMessage = "testMessage";
+
+ expectedException.expect( EnforcerRuleException.class );
+ expectedException.expectMessage( configuredMessage );
+
+ rule.setFile( f );
+ rule.setChecksum( "ffeeddccbbaa99887766554433221100" );
+ rule.setType( "md5" );
+ rule.setMessage( configuredMessage );
+
+ rule.execute( EnforcerTestUtils.getHelper() );
+ }
+
+ @Test
public void testFileChecksumSha1()
throws IOException, EnforcerRuleException
{