Skip to content

Commit

Permalink
Added enhancements and new tests in 'FileFilterTest.java' and a new f…
Browse files Browse the repository at this point in the history
…ile 'LazySupplierTest.java'

Enhanced 'FileFilterTest.java' by adding code that includes new tests and methods for directory listing and '.java' files filtering using lambda expressions and method references. The reason for these enhancements is to validate the correct functionality of file fiters with lambda expressions and method references in addition to anonymous inner classes.

Further, a new test file 'LazySupplierTest.java' has been introduced in the lamdabas package to test Lazy supplier performance. This is to ensure that 'Supplier' actually provides lazy behaviour when using Lambda expressions, which are part of Java8 language improvements.
  • Loading branch information
kousen committed Nov 8, 2023
1 parent 74ca467 commit b41160e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 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.io.FilenameFilter;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -41,6 +43,15 @@ void listDirectories_expressionLambda() {
assertThat(directories.length).isEqualTo(14);
}

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

@Test
void listDirectories_blockLambda() {
FileFilter filter = pathname -> {
Expand All @@ -51,4 +62,31 @@ void listDirectories_blockLambda() {
assertEquals(14, directories.length);
assertThat(directories.length).isEqualTo(14);
}

// Add a test that uses a FileFilter to list only .java files
@Test
void listJavaSrcFiles_fileFilter() {
FileFilter filter = pathname -> pathname.getName().endsWith(".java");
File[] javaFiles = root.listFiles(filter);
assert javaFiles != null;
assertThat(javaFiles.length).isEqualTo(8);
}

// Add a test that uses a FilenameFilter to list only .java files
@Test
void listJavaSrcFiles_filenameFilter() {
FilenameFilter filter = (dir, name) -> name.endsWith(".java");
File[] javaFiles = root.listFiles(filter);
assert javaFiles != null;
assertThat(javaFiles.length).isEqualTo(8);
}

@Test
void iterateOverMap() {
Map.ofEntries(
Map.entry("a", 1),
Map.entry("b", 2),
Map.entry("c", 3)
).forEach((k, v) -> System.out.println(k + " = " + v));
}
}
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 "Log message";
}

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

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

0 comments on commit b41160e

Please sign in to comment.