Try to workaround test failure on Ubuntu by using the NIO API to get a
file's last modified timestamp.
MODIFY
src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
+import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
@@ -102,6 +103,98 @@ void assertFiltering(final IOFileFilter filter, final Path path, final boolean e
assertNotNull(filter.toString());
}
+ private void assertFooBarFileFiltering(IOFileFilter filter) {
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("fred"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("fred").toPath(), false);
+
+ filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.SENSITIVE);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("FOO"), false);
+ assertFiltering(filter, new File("BAR"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), false);
+ assertFiltering(filter, new File("BAR").toPath(), false);
+
+ filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.INSENSITIVE);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("FOO"), true);
+ assertFiltering(filter, new File("BAR"), true);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), true);
+ assertFiltering(filter, new File("BAR").toPath(), true);
+
+ filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.SYSTEM);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("FOO"), WINDOWS);
+ assertFiltering(filter, new File("BAR"), WINDOWS);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), WINDOWS);
+ assertFiltering(filter, new File("BAR").toPath(), WINDOWS);
+
+ filter = new NameFileFilter(new String[] {"foo", "bar"}, null);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("FOO"), false);
+ assertFiltering(filter, new File("BAR"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), false);
+ assertFiltering(filter, new File("BAR").toPath(), false);
+
+ // repeat for a List
+ final java.util.ArrayList<String> list = new java.util.ArrayList<>();
+ list.add("foo");
+ list.add("bar");
+ filter = new NameFileFilter(list);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("bar"), true);
+ assertFiltering(filter, new File("fred"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("bar").toPath(), true);
+ assertFiltering(filter, new File("fred").toPath(), false);
+
+ filter = new NameFileFilter("foo");
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("FOO"), false); // case-sensitive
+ assertFiltering(filter, new File("barfoo"), false);
+ assertFiltering(filter, new File("foobar"), false);
+ assertFiltering(filter, new File("fred"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), false); // case-sensitive
+ assertFiltering(filter, new File("barfoo").toPath(), false);
+ assertFiltering(filter, new File("foobar").toPath(), false);
+ assertFiltering(filter, new File("fred").toPath(), false);
+
+ // FileFilterUtils.nameFileFilter(String, IOCase) tests
+ filter = FileFilterUtils.nameFileFilter("foo", IOCase.INSENSITIVE);
+ assertFiltering(filter, new File("foo"), true);
+ assertFiltering(filter, new File("FOO"), true); // case-insensitive
+ assertFiltering(filter, new File("barfoo"), false);
+ assertFiltering(filter, new File("foobar"), false);
+ assertFiltering(filter, new File("fred"), false);
+ assertFiltering(filter, new File("foo").toPath(), true);
+ assertFiltering(filter, new File("FOO").toPath(), true); // case-insensitive
+ assertFiltering(filter, new File("barfoo").toPath(), false);
+ assertFiltering(filter, new File("foobar").toPath(), false);
+ assertFiltering(filter, new File("fred").toPath(), false);
+ }
+
+ private boolean equalsLastModified(final File left, final File right) throws IOException {
+ return Files.getLastModifiedTime(left.toPath()).equals(Files.getLastModifiedTime(right.toPath()));
+ }
+
@Test
public void testAgeFilter() throws Exception {
final File oldFile = new File(temporaryFolder, "old.txt");
@@ -129,7 +222,7 @@ public void testAgeFilter() throws Exception {
try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(reference))) {
TestUtils.generateTestData(output, 0);
}
- } while (oldFile.lastModified() == reference.lastModified());
+ } while (equalsLastModified(oldFile, reference));
final Date date = new Date();
final long now = date.getTime();
@@ -146,7 +239,7 @@ public void testAgeFilter() throws Exception {
try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(newFile))) {
TestUtils.generateTestData(output, 0);
}
- } while (reference.lastModified() == newFile.lastModified());
+ } while (equalsLastModified(reference, newFile));
final IOFileFilter filter1 = FileFilterUtils.ageFileFilter(now);
final IOFileFilter filter2 = FileFilterUtils.ageFileFilter(now, true);
@@ -516,6 +609,12 @@ public void testFalse() {
assertNotNull(FalseFileFilter.INSTANCE.toString());
}
+ @Test
+ public void testFileEqualsFilter() {
+ assertFooBarFileFiltering(
+ new FileEqualsFileFilter(new File("foo")).or(new FileEqualsFileFilter(new File("bar"))));
+ }
+
@Test
public void testFileFilterUtils_and() {
final IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
@@ -887,6 +986,8 @@ public void testMagicNumberFileFilterBytesOffset() throws Exception {
assertFiltering(filter, dir, false);
}
+ // -----------------------------------------------------------------------
+
@Test
public void testMagicNumberFileFilterString() throws Exception {
final byte[] classFileMagicNumber = new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
@@ -953,8 +1054,6 @@ public void testMagicNumberFileFilterStringOffset() throws Exception {
assertFiltering(filter, dir, false);
}
- // -----------------------------------------------------------------------
-
@Test
public void testMagicNumberFileFilterValidation() {
try {
@@ -1162,106 +1261,6 @@ public void testNameFilter() {
assertFooBarFileFiltering(new NameFileFilter(new String[] {"foo", "bar"}));
}
- @Test
- public void testFileEqualsFilter() {
- assertFooBarFileFiltering(
- new FileEqualsFileFilter(new File("foo")).or(new FileEqualsFileFilter(new File("bar"))));
- }
-
- @Test
- public void testPathEqualsFilter() {
- assertFooBarFileFiltering(
- new PathEqualsFileFilter(Paths.get("foo")).or(new PathEqualsFileFilter(Paths.get("bar"))));
- }
-
- private void assertFooBarFileFiltering(IOFileFilter filter) {
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("fred"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("fred").toPath(), false);
-
- filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.SENSITIVE);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("FOO"), false);
- assertFiltering(filter, new File("BAR"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), false);
- assertFiltering(filter, new File("BAR").toPath(), false);
-
- filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.INSENSITIVE);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("FOO"), true);
- assertFiltering(filter, new File("BAR"), true);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), true);
- assertFiltering(filter, new File("BAR").toPath(), true);
-
- filter = new NameFileFilter(new String[] {"foo", "bar"}, IOCase.SYSTEM);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("FOO"), WINDOWS);
- assertFiltering(filter, new File("BAR"), WINDOWS);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), WINDOWS);
- assertFiltering(filter, new File("BAR").toPath(), WINDOWS);
-
- filter = new NameFileFilter(new String[] {"foo", "bar"}, null);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("FOO"), false);
- assertFiltering(filter, new File("BAR"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), false);
- assertFiltering(filter, new File("BAR").toPath(), false);
-
- // repeat for a List
- final java.util.ArrayList<String> list = new java.util.ArrayList<>();
- list.add("foo");
- list.add("bar");
- filter = new NameFileFilter(list);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("bar"), true);
- assertFiltering(filter, new File("fred"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("bar").toPath(), true);
- assertFiltering(filter, new File("fred").toPath(), false);
-
- filter = new NameFileFilter("foo");
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("FOO"), false); // case-sensitive
- assertFiltering(filter, new File("barfoo"), false);
- assertFiltering(filter, new File("foobar"), false);
- assertFiltering(filter, new File("fred"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), false); // case-sensitive
- assertFiltering(filter, new File("barfoo").toPath(), false);
- assertFiltering(filter, new File("foobar").toPath(), false);
- assertFiltering(filter, new File("fred").toPath(), false);
-
- // FileFilterUtils.nameFileFilter(String, IOCase) tests
- filter = FileFilterUtils.nameFileFilter("foo", IOCase.INSENSITIVE);
- assertFiltering(filter, new File("foo"), true);
- assertFiltering(filter, new File("FOO"), true); // case-insensitive
- assertFiltering(filter, new File("barfoo"), false);
- assertFiltering(filter, new File("foobar"), false);
- assertFiltering(filter, new File("fred"), false);
- assertFiltering(filter, new File("foo").toPath(), true);
- assertFiltering(filter, new File("FOO").toPath(), true); // case-insensitive
- assertFiltering(filter, new File("barfoo").toPath(), false);
- assertFiltering(filter, new File("foobar").toPath(), false);
- assertFiltering(filter, new File("fred").toPath(), false);
- }
-
@Test
public void testNameFilterNullArgument() {
final String test = null;
@@ -1360,6 +1359,12 @@ public void testOr() {
assertThrows(NullPointerException.class, () -> new OrFileFilter(falseFilter, (IOFileFilter) null));
}
+ @Test
+ public void testPathEqualsFilter() {
+ assertFooBarFileFiltering(
+ new PathEqualsFileFilter(Paths.get("foo")).or(new PathEqualsFileFilter(Paths.get("bar"))));
+ }
+
@Test
public void testPrefix() {
IOFileFilter filter = new PrefixFileFilter(new String[] {"foo", "bar"});

Try to workaround test failure on Ubuntu by using the NIO API to get a
file's last modified timestamp.