FilenameUtils directoryContains() should handle files with the
same prefix
Apply a modified PR from trungPa to resolve conflicts.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -59,6 +59,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Davide Angelocola">
Fix usage of assertNotNull #269.
</action>
+ <action issue="IO-727" dev="ggregory" type="fix" due-to="trungPa, Gary Gregory">
+ FilenameUtils directoryContains() should handle files with the same prefix #217.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BrokenReader.INSTANCE.

MODIFY
src/main/java/org/apache/commons/io/FilenameUtils.java
src/main/java/org/apache/commons/io/FilenameUtils.java
import java.util.Collection;
import java.util.Deque;
import java.util.List;
-import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -539,9 +538,7 @@ public static String concat(final String basePath, final String fullFileNameToAd
* @see FileUtils#directoryContains(File, File)
*/
public static boolean directoryContains(final String canonicalParent, final String canonicalChild) {
- Objects.requireNonNull(canonicalParent, "canonicalParent");
-
- if (canonicalChild == null) {
+ if (isEmpty(canonicalParent) || isEmpty(canonicalChild)) {
return false;
}
@@ -549,7 +546,14 @@ public static boolean directoryContains(final String canonicalParent, final Stri
return false;
}
- return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);
+ final char separator = canonicalParent.charAt(0) == UNIX_SEPARATOR ? UNIX_SEPARATOR : WINDOWS_SEPARATOR;
+ final String parentWithEndSeparator = canonicalParent.charAt(canonicalParent.length() - 1) == separator ? canonicalParent : canonicalParent + separator;
+
+ return IOCase.SYSTEM.checkStartsWith(canonicalChild, parentWithEndSeparator);
+ }
+
+ private static boolean isEmpty(final String string) {
+ return string == null || string.isEmpty();
}
/**
@@ -1235,7 +1239,7 @@ public static boolean isExtension(final String fileName, final String extension)
}
requireNonNullChars(fileName);
- if (extension == null || extension.isEmpty()) {
+ if (isEmpty(extension)) {
return indexOfExtension(fileName) == NOT_FOUND;
}
final String fileExt = getExtension(fileName);

MODIFY
src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
@@ -139,6 +139,19 @@ public void testIO466() throws IOException {
assertFalse(FileUtils.directoryContains(directory1, fooFile));
}
+ @Test
+ public void testFileHavingSamePrefixBug() throws IOException {
+ final File foo = new File(top, "foo");
+ final File foobar = new File(top, "foobar");
+ final File fooTxt = new File(top, "foo.txt");
+ foo.mkdir();
+ foobar.mkdir();
+ FileUtils.touch(fooTxt);
+
+ assertFalse(FileUtils.directoryContains(foo, foobar));
+ assertFalse(FileUtils.directoryContains(foo, fooTxt));
+ }
+
@Test
public void testFileDoesNotExist() throws IOException {
assertFalse(FileUtils.directoryContains(top, null));

MODIFY
src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
@@ -1184,4 +1184,21 @@ public void testIsExtensionCollection() {
assertFalse(FilenameUtils.isExtension("a.b\\file.txt", new ArrayList<>(Arrays.asList("TXT"))));
assertFalse(FilenameUtils.isExtension("a.b\\file.txt", new ArrayList<>(Arrays.asList("TXT", "RTF"))));
}
+
+ //-----------------------------------------------------------------------
+ @Test
+ public void testDirectoryContains() throws IOException {
+ assertTrue(FilenameUtils.directoryContains("/foo", "/foo/bar"));
+ assertTrue(FilenameUtils.directoryContains("/foo/", "/foo/bar"));
+ assertTrue(FilenameUtils.directoryContains("C:\\foo", "C:\\foo\\bar"));
+ assertTrue(FilenameUtils.directoryContains("C:\\foo\\", "C:\\foo\\bar"));
+
+ assertFalse(FilenameUtils.directoryContains("/foo", "/foo"));
+ assertFalse(FilenameUtils.directoryContains("/foo", "/foobar"));
+ assertFalse(FilenameUtils.directoryContains("C:\\foo", "C:\\foobar"));
+ assertFalse(FilenameUtils.directoryContains("/foo", null));
+ assertFalse(FilenameUtils.directoryContains("", ""));
+ assertFalse(FilenameUtils.directoryContains("", "/foo"));
+ assertFalse(FilenameUtils.directoryContains("/foo", ""));
+ }
}

[IO-727] FilenameUtils directoryContains() should handle files with the
same prefix #217.

Apply a modified PR #217 from trungPa to resolve conflicts.