[ 
https://issues.apache.org/jira/browse/MENFORCER-317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16640668#comment-16640668
 ] 

ASF GitHub Bot commented on MENFORCER-317:
------------------------------------------

khmarbaise closed pull request #41: MENFORCER-317 - Fix RequireFileChecksum 
ignores configured message + new nonexistentFileMessage
URL: https://github.com/apache/maven-enforcer/pull/41
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 @@ public void execute( EnforcerRuleHelper helper )
             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 void setType( String type )
         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 @@ public void testFileChecksumMd5UpperCase()
     }
 
     @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 void 
testFileChecksumMd5GivenFileIsADirectoryFailure()
         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" );
@@ -175,6 +210,25 @@ public void testFileChecksumMd5ChecksumMismatchFailure()
         rule.execute( EnforcerTestUtils.getHelper() );
     }
 
+    @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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> RequireFileChecksum ignores configured message
> ----------------------------------------------
>
>                 Key: MENFORCER-317
>                 URL: https://issues.apache.org/jira/browse/MENFORCER-317
>             Project: Maven Enforcer Plugin
>          Issue Type: Bug
>          Components: Standard Rules
>    Affects Versions: 3.0.0-M2
>            Reporter: Falko Modler
>            Assignee: Karl Heinz Marbaise
>            Priority: Major
>              Labels: up-for-grabs
>             Fix For: 3.0.0
>
>
> The [documentation for RequireFileChecksum 
> |https://maven.apache.org/enforcer/enforcer-rules/requireFileChecksum.html] 
> says, that you can set a {{message}} that is used for a violation message.
> However, this configuable attribute coming from 
> {{AbstractStandardEnforcerRule}} is simply ignored/not evaluated.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to