diff --git a/src/test/java/lambdas/FileFilterTest.java b/src/test/java/lambdas/FileFilterTest.java new file mode 100644 index 0000000..eb81181 --- /dev/null +++ b/src/test/java/lambdas/FileFilterTest.java @@ -0,0 +1,72 @@ +package lambdas; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.FileFilter; + +import static java.util.Map.*; +import static org.assertj.core.api.Assertions.assertThat; + +public class FileFilterTest { + private final File root = new File("src/main/java"); + + @Test + void listFiles() { + File[] files = root.listFiles(); + assert files != null; + assertThat(files.length).isEqualTo(22); + } + + @Test + void listDirectories_anonInnerClass() { + File[] directories = root.listFiles(new java.io.FileFilter() { + @Override + public boolean accept(File file) { + return file.isDirectory(); + } + }); + assert directories != null; + assertThat(directories.length).isEqualTo(14); + } + + @Test + void listDirectories_expressionLambda() { + File[] directories = root.listFiles(file -> file.isDirectory()); + assert directories != null; + assertThat(directories.length).isEqualTo(14); + } + + @Test + void listJavaSrcFiles_fileFilter() { + File[] javaSrcFiles = root.listFiles(file -> file.getName() + .endsWith(".java")); + assert javaSrcFiles != null; + assertThat(javaSrcFiles.length).isEqualTo(8); + } + + @Test + void listJavaSrcFiles_fileNameFilter() { + File[] javaSrcFiles = root.listFiles((File dir, String name) -> name.endsWith(".java")); + assert javaSrcFiles != null; + assertThat(javaSrcFiles.length).isEqualTo(8); + } + + @Test + void listJavaSrcFiles_fileFilter_variable() { + FileFilter filter = (File file) -> file.getName() + .endsWith(".java"); + File[] javaSrcFiles = root.listFiles(filter); + assert javaSrcFiles != null; + assertThat(javaSrcFiles.length).isEqualTo(8); + } + + @Test + void forEachMap() { + ofEntries( + entry("a", 1), + entry("b", 2), + entry("c", 2)) + .forEach((k, v) -> System.out.println(k + " -> " + v)); + } +} diff --git a/src/test/java/lambdas/LazySupplierTest.java b/src/test/java/lambdas/LazySupplierTest.java new file mode 100644 index 0000000..e2042f3 --- /dev/null +++ b/src/test/java/lambdas/LazySupplierTest.java @@ -0,0 +1,35 @@ +package lambdas; + +import org.junit.jupiter.api.Test; + +import java.util.logging.Logger; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class LazySupplierTest { + private final Logger logger = Logger.getLogger(LazySupplierTest.class.getName()); + + private String getErrorMessage() { + System.out.println("calling getErrorMessage()"); + return "An error occurred"; + } + + private String getLogMessage() { + System.out.println("calling getLogMessage()"); + return "log message"; + } + + @Test + void eagerMessages() { + boolean condition = true; + assertTrue(condition, getErrorMessage()); + logger.fine(getLogMessage()); + } + + @Test + void lazyMessages() { + boolean condition = true; + assertTrue(condition, () -> getErrorMessage()); + logger.fine(() -> getLogMessage()); + } +} diff --git a/src/test/java/lambdas/PredicateTest.java b/src/test/java/lambdas/PredicateTest.java new file mode 100644 index 0000000..9d3fd07 --- /dev/null +++ b/src/test/java/lambdas/PredicateTest.java @@ -0,0 +1,47 @@ +package lambdas; + +import org.junit.jupiter.api.Test; + +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PredicateTest { + + @Test + void implementPredicateAsAnonInnerClass() { + Predicate p = new Predicate() { + @Override + public boolean test(String s) { + return s.length() > 5; + } + }; + assertFalse(p.test("Hello")); + assertTrue(p.test("Hello World")); + } + + @Test + void implementPredicateAsExpressionLambda() { + Predicate p = s -> s.length() > 5; + assertFalse(p.test("Hello")); + assertTrue(p.test("Hello World")); + } + + @Test + void implementPredicateAsBlockLambda() { + Predicate p = s -> { + System.out.println("Evaluating whether s length > 5: " + s); + return s.length() > 5; + }; + assertFalse(p.test("Hello")); + assertTrue(p.test("Hello World")); + } + + @Test + void testRunnable() { + Runnable r = () -> System.out.println("Hello World"); + Thread thread = new Thread(r); + thread.start(); + } +}