-
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.
Introduce unit tests to validate file and directory listing using FileFilter. Tests include anonymous inner class, expression lambda, block lambda, and a pre-assigned variable for filtering directories. Ensure the tests cover multiple approach for better code reliability.
- Loading branch information
Showing
1 changed file
with
55 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,55 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FileFilterTest { | ||
private final File root = new File("src/main/java"); | ||
|
||
@Test | ||
void listFiles() { | ||
File[] files = root.listFiles(); | ||
assert files != null; | ||
for (File file : files) { | ||
System.out.println(file); | ||
} | ||
assertEquals(22, files.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_anonInnerClass() { | ||
File[] directories = root.listFiles(new FileFilter() { | ||
@Override | ||
public boolean accept(File file) { | ||
return file.isDirectory(); | ||
} | ||
}); | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_expressionLambda() { | ||
File[] directories = root.listFiles(file -> file.isDirectory()); | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_blockLambda() { | ||
File[] directories = root.listFiles(file -> { | ||
System.out.println("Checking: " + file); | ||
return file.isDirectory(); | ||
}); | ||
assertEquals(14, directories.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_assignedToVariable() { | ||
FileFilter filter = (File file) -> file.isDirectory(); | ||
File[] directories = root.listFiles(filter); | ||
assertEquals(14, directories.length); | ||
} | ||
} |