Skip to content

Commit

Permalink
Add unit tests for FileFilter functionality
Browse files Browse the repository at this point in the history
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
kousen committed Sep 26, 2023
1 parent ebcd9a5 commit a58a3b2
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
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);
}
}
}

0 comments on commit a58a3b2

Please sign in to comment.