-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> p = new Predicate<String>() { | ||
@Override | ||
public boolean test(String s) { | ||
return s.length() > 5; | ||
} | ||
}; | ||
assertFalse(p.test("Hello")); | ||
assertTrue(p.test("Hello World")); | ||
} | ||
|
||
@Test | ||
void implementPredicateAsExpressionLambda() { | ||
Predicate<String> p = s -> s.length() > 5; | ||
assertFalse(p.test("Hello")); | ||
assertTrue(p.test("Hello World")); | ||
} | ||
|
||
@Test | ||
void implementPredicateAsBlockLambda() { | ||
Predicate<String> 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(); | ||
} | ||
} |