From 335f26e39d1f20e157c46485481e36f858135a14 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Fri, 23 Jul 2021 18:24:54 +0200 Subject: [PATCH] [MENFORCER-364] requireFilesExist rule should be case sensitive --- .../plugins/enforcer/RequireFilesExist.java | 33 ++++++++- .../enforcer/TestRequireFilesExist.java | 73 ++++++++++--------- 2 files changed, 69 insertions(+), 37 deletions(-) diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java index 5727bd81..5675f552 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java @@ -20,6 +20,7 @@ */ import java.io.File; +import java.io.IOException; /** * The Class RequireFilesExist. @@ -31,7 +32,7 @@ public class RequireFilesExist boolean checkFile( File file ) { // if we get here and the handle is null, treat it as a success - return file == null ? true : file.exists(); + return file == null ? true : file.exists() && osIndependentNameMatch( file, true ); } @Override @@ -40,4 +41,34 @@ String getErrorMsg() return "Some required files are missing:" + System.lineSeparator(); } + /** + * OSes like Windows are case insensitive, so this method will compare the file path with the actual path. A simple + * {@link File#exists()} is not enough for such OS. + * + * @param file the file to verify + * @param defaultValue value to return in case an IO exception occurs, should never happen as the file already + * exists + * @return + */ + private boolean osIndependentNameMatch( File file, boolean defaultValue ) + { + try + { + File absFile; + if ( !file.isAbsolute() ) + { + absFile = new File( new File( "." ).getCanonicalFile(), file.getPath() ); + } + else + { + absFile = file; + } + + return absFile.toURI().equals( absFile.getCanonicalFile().toURI() ); + } + catch ( IOException e ) + { + return defaultValue; + } + } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java index b254175a..06a01caa 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java @@ -22,10 +22,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; import java.io.File; -import java.io.IOException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.junit.Rule; @@ -46,34 +45,43 @@ public class TestRequireFilesExist @Test public void testFileExists() - throws EnforcerRuleException, IOException + throws Exception { File f = temporaryFolder.newFile(); - rule.setFiles( new File[] { f } ); + rule.setFiles( new File[] { f.getCanonicalFile() } ); rule.execute( EnforcerTestUtils.getHelper() ); } + + @Test + public void testFileOsIndependentExists() + throws Exception + { + rule.setFiles( new File[] { new File( "POM.xml" ) } ); + + EnforcerRuleException e = + assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) ); + + assertNotNull( e.getMessage() ); + } + @Test public void testEmptyFile() - throws EnforcerRuleException, IOException + throws Exception { rule.setFiles( new File[] { null } ); - try - { - rule.execute( EnforcerTestUtils.getHelper() ); - fail( "Should get exception" ); - } - catch ( EnforcerRuleException e ) - { - assertNotNull( e.getMessage() ); - } + + EnforcerRuleException e = + assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) ); + + assertNotNull( e.getMessage() ); } @Test public void testEmptyFileAllowNull() - throws EnforcerRuleException, IOException + throws Exception { rule.setFiles( new File[] { null } ); rule.setAllowNulls( true ); @@ -82,24 +90,21 @@ public void testEmptyFileAllowNull() @Test public void testEmptyFileList() - throws EnforcerRuleException, IOException + throws Exception { rule.setFiles( new File[] {} ); assertEquals( 0, rule.getFiles().length ); - try - { - rule.execute( EnforcerTestUtils.getHelper() ); - fail( "Should get exception" ); - } - catch ( EnforcerRuleException e ) - { - assertNotNull( e.getMessage() ); - } + + EnforcerRuleException e = + assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) ); + + assertNotNull( e.getMessage() ); + } @Test public void testEmptyFileListAllowNull() - throws EnforcerRuleException, IOException + throws Exception { rule.setFiles( new File[] {} ); assertEquals( 0, rule.getFiles().length ); @@ -109,7 +114,7 @@ public void testEmptyFileListAllowNull() @Test public void testFileDoesNotExist() - throws EnforcerRuleException, IOException + throws Exception { File f = temporaryFolder.newFile(); f.delete(); @@ -117,15 +122,11 @@ public void testFileDoesNotExist() assertFalse( f.exists() ); rule.setFiles( new File[] { f } ); - try - { - rule.execute( EnforcerTestUtils.getHelper() ); - fail( "Should get exception" ); - } - catch ( EnforcerRuleException e ) - { - assertNotNull( e.getMessage() ); - } + EnforcerRuleException e = + assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) ); + + assertNotNull( e.getMessage() ); + } /**