Skip to content

Commit

Permalink
Add FileFilterTest for lambda expressions
Browse files Browse the repository at this point in the history
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
kousen committed Nov 8, 2023
1 parent 12e46ae commit 74ca467
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
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);
}
}

0 comments on commit 74ca467

Please sign in to comment.