[MENFORCER-364] requireFilesExist rule should be case sensitive
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 5727bd8..5675f55 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 @@
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 @@
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 b254175..06a01ca 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 @@
@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 @@
@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 @@
@Test
public void testFileDoesNotExist()
- throws EnforcerRuleException, IOException
+ throws Exception
{
File f = temporaryFolder.newFile();
f.delete();
@@ -117,15 +122,11 @@
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() );
+
}
/**