-
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 FileFilterTest for lambda expressions
Created a new test class `FileFilterTest` to validate the correct functionality of file filters using anonymous inner classes and lambda expressions. This includes tests for listing all files and directories, ensuring the improved readability and simplicity of lambda expressions does not compromise the functionality.
- Loading branch information
Showing
1 changed file
with
54 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,54 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
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; | ||
assertEquals(22, files.length); | ||
assertThat(files.length).isEqualTo(22); | ||
} | ||
|
||
@Test | ||
void listDirectories_anonInnerClass() { | ||
File[] directories = root.listFiles(new FileFilter() { | ||
@Override | ||
public boolean accept(File pathname) { | ||
return pathname.isDirectory(); | ||
} | ||
}); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
assertThat(directories.length).isEqualTo(14); | ||
} | ||
|
||
@Test | ||
void listDirectories_expressionLambda() { | ||
FileFilter filter = pathname -> pathname.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 -> { | ||
return pathname.isDirectory(); | ||
}; | ||
File[] directories = root.listFiles(filter); | ||
assert directories != null; | ||
assertEquals(14, directories.length); | ||
assertThat(directories.length).isEqualTo(14); | ||
} | ||
} |