BAEL-1936 Use of FilenameFilter (#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.
This commit is contained in:
Neeraj Yadav 2018-08-01 03:28:48 +05:30 committed by pauljervis
parent 505a9f37eb
commit 0f1e1a49d9
5 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package com.baeldung.file;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class FilenameFilterManualTest {
private static File directory;
@BeforeClass
public static void setupClass() {
directory = new File(FilenameFilterManualTest.class.getClassLoader()
.getResource("testFolder")
.getFile());
}
@Test
public void whenFilteringFilesEndingWithJson_thenEqualExpectedFiles() {
FilenameFilter filter = (dir, name) -> name.endsWith(".json");
String[] expectedFiles = { "people.json", "students.json" };
String[] actualFiles = directory.list(filter);
Assert.assertArrayEquals(expectedFiles, actualFiles);
}
@Test
public void whenFilteringFilesEndingWithXml_thenEqualExpectedFiles() {
Predicate<String> predicate = (name) -> name.endsWith(".xml");
String[] expectedFiles = { "teachers.xml", "workers.xml" };
List<String> files = Arrays.stream(directory.list())
.filter(predicate)
.collect(Collectors.toList());
String[] actualFiles = files.toArray(new String[files.size()]);
Assert.assertArrayEquals(expectedFiles, actualFiles);
}
}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
<xml></xml>

View File

@ -0,0 +1 @@
<xml></xml>