Skip to content

Commit

Permalink
Increase unit test coverage for file filter methods
Browse files Browse the repository at this point in the history
Enhanced the FileFilterTest class to include unit test scenarios for various lambda and anonymous inner class implementations within the file filter. This improvement will facilitate in validating the behavior of these filtering techniques and ensure the right number of files or directories are returned. The update is crucial to the overall code maintainability and robustness.
  • Loading branch information
kousen committed Oct 3, 2023
1 parent 07c2400 commit 8eca393
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.FileFilter;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -17,6 +18,7 @@ public void testListFiles() {
assertEquals(22, files.length);
}

@SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"})
@Test
void listDirectories_anonInnerClass() {
File[] directories = root.listFiles(new FileFilter() {
Expand All @@ -29,12 +31,67 @@ public boolean accept(File pathname) {
assertEquals(14, directories.length);
}

@SuppressWarnings("Convert2MethodRef")
@Test
void listDirectories_expression_lambda() {
File[] directories = root.listFiles(file -> file.isDirectory());
assert directories != null;
assertEquals(14, directories.length);
}

@SuppressWarnings("Convert2MethodRef")
@Test
void listDirectories_lambda_variable() {
FileFilter fileFilter = file -> file.isDirectory();
File[] directories = root.listFiles(fileFilter);
assert directories != null;
assertEquals(14, directories.length);
}

@SuppressWarnings({"Convert2MethodRef", "CodeBlock2Expr"})
@Test
void listDirectories_block_lambda() {
File[] directories = root.listFiles(file -> {
return file.isDirectory();
});
assert directories != null;
assertEquals(14, directories.length);
}

@Test
void listDirectories_method_reference() {
File[] directories = root.listFiles(File::isDirectory);
assert directories != null;
assertEquals(14, directories.length);
}

@Test
void listJavaSrcFiles_fileFilter() {
File[] javaFiles = root.listFiles(file -> file.getName().endsWith(".java"));
assert javaFiles != null;
assertEquals(8, javaFiles.length);
}

@Test
void listJavaSrcFiles_fileNameFilter() {
File[] javaFiles = root.listFiles((dir, name) -> name.endsWith(".java"));
assert javaFiles != null;
assertEquals(8, javaFiles.length);
}

@Test
void forEach_on_a_list() {
List<String> strings = List.of("this", "is", "a", "list");
// Java 5+
for (String string : strings) {
System.out.println(string);
}

// Java 8+
strings.forEach(System.out::println);

// Parallel stream
strings.parallelStream().forEach(System.out::println);
}

}

0 comments on commit 8eca393

Please sign in to comment.