Skip to content

Commit

Permalink
Refactor FileFilterTest and add LazySupplierTest
Browse files Browse the repository at this point in the history
Updated the FileFilterTest.java by revising existing tests and adding new ones for improved code coverage. The refactoring increases the usage of lambda expressions and method references for readability and conciseness. Simultaneously, a new test class LazySupplierTest.java has been added, which includes tests for both eager and lazy suppliers.
  • Loading branch information
kousen committed Feb 6, 2024
1 parent 4b92b75 commit 02e0a13
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

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

Expand Down Expand Up @@ -36,6 +38,13 @@ void listDirectories_expressionLambda() {
assertEquals(14, directories.length);
}

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

@Test
void listDirectories_blockLambda() {
File[] directories = root.listFiles(file -> {
Expand All @@ -48,17 +57,39 @@ void listDirectories_blockLambda() {

@Test
void listDirectories_fileFilterVariable() {
FileFilter filter = (File file) -> file.isDirectory();
FileFilter filter = File::isDirectory;
File[] directories = root.listFiles(filter);
assert directories != null;
assertEquals(14, directories.length);
}

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

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

@Test
void forEachList() {
List<String> strings = List.of("this", "is", "a", "list", "of", "strings");
// for-each loop
for (String string : strings) {
System.out.println(string);
}

// forEach method in Iterable
strings.forEach(string -> System.out.println(string));

Map.of("a", 1, "b", 2, "c", 2)
.forEach((key, value) -> System.out.println(key + " = " + value));
}
}
35 changes: 35 additions & 0 deletions src/test/java/lambdas/LazySupplierTest.java
Original file line number Diff line number Diff line change
@@ -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("getErrorMessage() called");
return "Error message";
}

private String getLogMessage() {
System.out.println("getLogMessage() called");
return "Test passed";
}

@Test
void eagerSupplier() {
boolean x = true;
assertTrue(x, getErrorMessage());
logger.fine(getLogMessage());
}

@Test
void lazySupplier() {
boolean x = true;
assertTrue(x, () -> getErrorMessage());
logger.fine(() -> getLogMessage());
}
}

0 comments on commit 02e0a13

Please sign in to comment.