Skip to content

Commit 335f26e

Browse files
committed
[MENFORCER-364] requireFilesExist rule should be case sensitive
1 parent faaf5c1 commit 335f26e

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireFilesExist.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
import java.io.File;
23+
import java.io.IOException;
2324

2425
/**
2526
* The Class RequireFilesExist.
@@ -31,7 +32,7 @@ public class RequireFilesExist
3132
boolean checkFile( File file )
3233
{
3334
// if we get here and the handle is null, treat it as a success
34-
return file == null ? true : file.exists();
35+
return file == null ? true : file.exists() && osIndependentNameMatch( file, true );
3536
}
3637

3738
@Override
@@ -40,4 +41,34 @@ String getErrorMsg()
4041
return "Some required files are missing:" + System.lineSeparator();
4142
}
4243

44+
/**
45+
* OSes like Windows are case insensitive, so this method will compare the file path with the actual path. A simple
46+
* {@link File#exists()} is not enough for such OS.
47+
*
48+
* @param file the file to verify
49+
* @param defaultValue value to return in case an IO exception occurs, should never happen as the file already
50+
* exists
51+
* @return
52+
*/
53+
private boolean osIndependentNameMatch( File file, boolean defaultValue )
54+
{
55+
try
56+
{
57+
File absFile;
58+
if ( !file.isAbsolute() )
59+
{
60+
absFile = new File( new File( "." ).getCanonicalFile(), file.getPath() );
61+
}
62+
else
63+
{
64+
absFile = file;
65+
}
66+
67+
return absFile.toURI().equals( absFile.getCanonicalFile().toURI() );
68+
}
69+
catch ( IOException e )
70+
{
71+
return defaultValue;
72+
}
73+
}
4374
}

enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireFilesExist.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertFalse;
2424
import static org.junit.Assert.assertNotNull;
25-
import static org.junit.Assert.fail;
25+
import static org.junit.Assert.assertThrows;
2626

2727
import java.io.File;
28-
import java.io.IOException;
2928

3029
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
3130
import org.junit.Rule;
@@ -46,34 +45,43 @@ public class TestRequireFilesExist
4645

4746
@Test
4847
public void testFileExists()
49-
throws EnforcerRuleException, IOException
48+
throws Exception
5049
{
5150
File f = temporaryFolder.newFile();
5251

53-
rule.setFiles( new File[] { f } );
52+
rule.setFiles( new File[] { f.getCanonicalFile() } );
5453

5554
rule.execute( EnforcerTestUtils.getHelper() );
5655
}
56+
57+
@Test
58+
public void testFileOsIndependentExists()
59+
throws Exception
60+
{
61+
rule.setFiles( new File[] { new File( "POM.xml" ) } );
62+
63+
EnforcerRuleException e =
64+
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );
65+
66+
assertNotNull( e.getMessage() );
67+
}
68+
5769

5870
@Test
5971
public void testEmptyFile()
60-
throws EnforcerRuleException, IOException
72+
throws Exception
6173
{
6274
rule.setFiles( new File[] { null } );
63-
try
64-
{
65-
rule.execute( EnforcerTestUtils.getHelper() );
66-
fail( "Should get exception" );
67-
}
68-
catch ( EnforcerRuleException e )
69-
{
70-
assertNotNull( e.getMessage() );
71-
}
75+
76+
EnforcerRuleException e =
77+
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );
78+
79+
assertNotNull( e.getMessage() );
7280
}
7381

7482
@Test
7583
public void testEmptyFileAllowNull()
76-
throws EnforcerRuleException, IOException
84+
throws Exception
7785
{
7886
rule.setFiles( new File[] { null } );
7987
rule.setAllowNulls( true );
@@ -82,24 +90,21 @@ public void testEmptyFileAllowNull()
8290

8391
@Test
8492
public void testEmptyFileList()
85-
throws EnforcerRuleException, IOException
93+
throws Exception
8694
{
8795
rule.setFiles( new File[] {} );
8896
assertEquals( 0, rule.getFiles().length );
89-
try
90-
{
91-
rule.execute( EnforcerTestUtils.getHelper() );
92-
fail( "Should get exception" );
93-
}
94-
catch ( EnforcerRuleException e )
95-
{
96-
assertNotNull( e.getMessage() );
97-
}
97+
98+
EnforcerRuleException e =
99+
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );
100+
101+
assertNotNull( e.getMessage() );
102+
98103
}
99104

100105
@Test
101106
public void testEmptyFileListAllowNull()
102-
throws EnforcerRuleException, IOException
107+
throws Exception
103108
{
104109
rule.setFiles( new File[] {} );
105110
assertEquals( 0, rule.getFiles().length );
@@ -109,23 +114,19 @@ public void testEmptyFileListAllowNull()
109114

110115
@Test
111116
public void testFileDoesNotExist()
112-
throws EnforcerRuleException, IOException
117+
throws Exception
113118
{
114119
File f = temporaryFolder.newFile();
115120
f.delete();
116121

117122
assertFalse( f.exists() );
118123
rule.setFiles( new File[] { f } );
119124

120-
try
121-
{
122-
rule.execute( EnforcerTestUtils.getHelper() );
123-
fail( "Should get exception" );
124-
}
125-
catch ( EnforcerRuleException e )
126-
{
127-
assertNotNull( e.getMessage() );
128-
}
125+
EnforcerRuleException e =
126+
assertThrows( EnforcerRuleException.class, () -> rule.execute( EnforcerTestUtils.getHelper() ) );
127+
128+
assertNotNull( e.getMessage() );
129+
129130
}
130131

131132
/**

0 commit comments

Comments
 (0)