From 8eca393a04346ff0b3a5915f20fa9bfaf7982f46 Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Tue, 3 Oct 2023 09:59:41 -0400 Subject: [PATCH] Increase unit test coverage for file filter methods 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. --- src/test/java/lambdas/FileFilterTest.java | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java index 489b4dc..76a6c88 100644 --- a/src/test/java/lambdas/FileFilterTest.java +++ b/src/test/java/lambdas/FileFilterTest.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileFilter; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -17,6 +18,7 @@ public void testListFiles() { assertEquals(22, files.length); } + @SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef"}) @Test void listDirectories_anonInnerClass() { File[] directories = root.listFiles(new FileFilter() { @@ -29,6 +31,7 @@ public boolean accept(File pathname) { assertEquals(14, directories.length); } + @SuppressWarnings("Convert2MethodRef") @Test void listDirectories_expression_lambda() { File[] directories = root.listFiles(file -> file.isDirectory()); @@ -36,5 +39,59 @@ void listDirectories_expression_lambda() { 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 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); + } }