Skip to content

Commit 0f1e1a4

Browse files
neerajnydpauljervis
authored andcommitted
BAEL-1936 Use of FilenameFilter (eugenp#4520)
* Added tests for FilenameFilter demo -added a test to show FilenameFilter implementation -added another test to show similar functionality using Predicate * refactored code to get directory at a single location * fixing formatting * changed test class name to conform to custom rule UnitTestNamingConventionRule lists the allowed test class names. Added ManualTest at the end to conform to the rule.
1 parent 505a9f3 commit 0f1e1a4

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.file;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.function.Predicate;
8+
import java.util.stream.Collectors;
9+
10+
import org.junit.Assert;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
14+
public class FilenameFilterManualTest {
15+
16+
private static File directory;
17+
18+
@BeforeClass
19+
public static void setupClass() {
20+
directory = new File(FilenameFilterManualTest.class.getClassLoader()
21+
.getResource("testFolder")
22+
.getFile());
23+
}
24+
25+
@Test
26+
public void whenFilteringFilesEndingWithJson_thenEqualExpectedFiles() {
27+
FilenameFilter filter = (dir, name) -> name.endsWith(".json");
28+
29+
String[] expectedFiles = { "people.json", "students.json" };
30+
String[] actualFiles = directory.list(filter);
31+
32+
Assert.assertArrayEquals(expectedFiles, actualFiles);
33+
}
34+
35+
@Test
36+
public void whenFilteringFilesEndingWithXml_thenEqualExpectedFiles() {
37+
Predicate<String> predicate = (name) -> name.endsWith(".xml");
38+
39+
String[] expectedFiles = { "teachers.xml", "workers.xml" };
40+
List<String> files = Arrays.stream(directory.list())
41+
.filter(predicate)
42+
.collect(Collectors.toList());
43+
String[] actualFiles = files.toArray(new String[files.size()]);
44+
45+
Assert.assertArrayEquals(expectedFiles, actualFiles);
46+
}
47+
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<xml></xml>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<xml></xml>

0 commit comments

Comments
 (0)