-
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 unit tests for FileFilter functionality
FileFilterTest.java is a new file containing unit tests for FileFilter functions. It includes tests for listing files, directories, Java source files, use of anonymous inner classes, lambda expressions, and block lambda in file filtering. It aims to ensure the functionality of the system's file filtering operations.
- Loading branch information
Showing
1 changed file
with
79 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,79 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class FileFilterTest { | ||
private final File root = new File("src/main/java"); | ||
|
||
@Test | ||
public void testListFiles() { | ||
File[] files = root.listFiles(); | ||
assert files != null; | ||
for (File file : files) { | ||
System.out.println(file); | ||
} | ||
assertThat(files.length).isEqualTo(22); | ||
} | ||
|
||
@Test | ||
void listDirectories_anonInnerClass() { | ||
File[] directories = root.listFiles(new 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 listDirectories_blockLambda() { | ||
File[] directories = root.listFiles(file -> { | ||
System.out.println("Checking " + file + " to see if it's a directory."); | ||
return file.isDirectory(); | ||
}); | ||
assert directories != null; | ||
assertThat(directories.length).isEqualTo(14); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_fileFilter() { | ||
File[] javaFiles = root.listFiles(file -> file.getName().endsWith(".java")); | ||
assert javaFiles != null; | ||
assertThat(javaFiles.length).isEqualTo(8); | ||
} | ||
|
||
@Test | ||
void listJavaSrcFiles_fileNameFilter() { | ||
File[] javaFiles = root.listFiles((dir, name) -> name.endsWith(".java")); | ||
assert javaFiles != null; | ||
assertThat(javaFiles.length).isEqualTo(8); | ||
} | ||
|
||
@Test | ||
void forEachOnList() { | ||
List<String> strings = List.of("this", "is", "a", "list", "of", "strings"); | ||
|
||
// Using the forEach(Consumer) method | ||
strings.forEach(s -> System.out.println("The next string is " + s)); | ||
|
||
// For-each loop | ||
for (String s : strings) { | ||
System.out.println("The next string is " + s); | ||
} | ||
} | ||
} |