[MENFORCER-317] - Fix RequireFileChecksum ignores configured message
authorFalko Modler <famod@users.noreply.github.com>
Tue, 4 Sep 2018 21:44:32 +0000 (23:44 +0200)
committerKarl Heinz Marbaise <khmarbaise@apache.org>
Sat, 6 Oct 2018 09:58:08 +0000 (11:58 +0200)
 - Introduce configurable message for nonexistent file
 - Including small refactoring: extract checksum calculation into private method.

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFileChecksum.java
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFileChecksum.java

index 1ab2aba..1a4c160 100644 (file)
@@ -45,6 +45,8 @@ public class RequireFileChecksum
 
     private String type;
 
+    private String nonexistentFileMessage;
+
     @Override
     public void execute( EnforcerRuleHelper helper )
         throws EnforcerRuleException
@@ -64,53 +66,38 @@ public class RequireFileChecksum
             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 );
-            }
+        if ( this.file.isDirectory() )
+        {
+            throw new EnforcerRuleException( "Cannot calculate the checksum of directory: "
+                + this.file.getAbsolutePath() );
         }
-        catch ( IOException e )
+
+        if ( !this.file.canRead() )
         {
-            throw new EnforcerRuleException( "Unable to calculate checksum", e );
+            throw new EnforcerRuleException( "Cannot read file: " + this.file.getAbsolutePath() );
         }
-        finally
+
+        String checksum = calculateChecksum();
+
+        if ( !checksum.equalsIgnoreCase( this.checksum ) )
         {
-            IOUtil.close( inputStream );
+            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 @@ public class RequireFileChecksum
         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 );
+        }
+    }
 }
index 4333cb3..c7075ce 100644 (file)
@@ -74,10 +74,45 @@ public class TestRequireFileChecksum
     }
 
     @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 @@ public class TestRequireFileChecksum
         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 @@ public class TestRequireFileChecksum
     }
 
     @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
     {