diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java new file mode 100644 index 0000000..86ed8be --- /dev/null +++ b/src/test/java/lambdas/FileFilterTest.java @@ -0,0 +1,55 @@ +package lambdas; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileFilter; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FileFilterTest { + private final File root = new File("src/main/java"); + + @Test + void listFiles() { + File[] files = root.listFiles(); + assert files != null; + for (File file : files) { + System.out.println(file); + } + assertEquals(22, files.length); + } + + @Test + void listDirectories_anonInnerClass() { + File[] directories = root.listFiles(new FileFilter() { + @Override + public boolean accept(File file) { + return file.isDirectory(); + } + }); + assertEquals(14, directories.length); + } + + @Test + void listDirectories_expressionLambda() { + File[] directories = root.listFiles(file -> file.isDirectory()); + assertEquals(14, directories.length); + } + + @Test + void listDirectories_blockLambda() { + File[] directories = root.listFiles(file -> { + System.out.println("Checking: " + file); + return file.isDirectory(); + }); + assertEquals(14, directories.length); + } + + @Test + void listDirectories_assignedToVariable() { + FileFilter filter = (File file) -> file.isDirectory(); + File[] directories = root.listFiles(filter); + assertEquals(14, directories.length); + } +}