From dfde2d71d5df8b87d9c66812092b8716c7ca6d2f Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Tue, 13 Feb 2024 12:30:46 -0500 Subject: [PATCH] Add test cases for lambdas functionality Three new unit test files have been added: FileFilterTest, LazySupplierTest, and PredicateTest. These tests will validate various features and behaviors related to Java lambda expressions. Specifically, testing file filtering, the use of suppliers and predicates, and functionality with both expression and block lambda syntax. --- src/test/java/lambdas/FileFilterTest.java | 72 +++++++++++++++++++++ src/test/java/lambdas/LazySupplierTest.java | 35 ++++++++++ src/test/java/lambdas/PredicateTest.java | 47 ++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/test/java/lambdas/FileFilterTest.java create mode 100644 src/test/java/lambdas/LazySupplierTest.java create mode 100644 src/test/java/lambdas/PredicateTest.java 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(); + } +}