-
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 FileFilter tests and simplify stream collection usage
Introduce new unit tests in `FileFilterTest.java` to demonstrate various file filtering and iteration approaches using lambdas and method references. Additionally, refactor `AntarcticaTimeZones.java` to use the simplified `toList()` method for stream collection, enhancing code readability.
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
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
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 java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FileFilterTest { | ||
private final File root = new File("src"); | ||
|
||
@Test | ||
void listFiles() { | ||
File[] files = root.listFiles(); | ||
assert files != null; | ||
for (File file : files) { | ||
System.out.println(file); | ||
} | ||
} | ||
|
||
@Test | ||
void listDirectories_fileFilter_anoninnerclass() { | ||
File[] files = root.listFiles(new FileFilter() { | ||
@Override | ||
public boolean accept(File pathname) { | ||
return pathname.isDirectory(); | ||
} | ||
}); | ||
assert files != null; | ||
for (File file : files) { | ||
System.out.println(file); | ||
} | ||
} | ||
|
||
@Test | ||
void listDirectories_fileFilter_expressionlambda() { | ||
File[] files = root.listFiles(pathname -> pathname.isDirectory()); | ||
assert files != null; | ||
assertEquals(2, files.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_fileNameFilter_expressionlambda() { | ||
File[] files = root.listFiles((dir, name) -> new File(dir, name).isDirectory()); | ||
assert files != null; | ||
assertEquals(2, files.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_fileFilter_variable() { | ||
FileFilter fileFilter = (File pathname) -> pathname.isDirectory(); | ||
File[] files = root.listFiles(fileFilter); | ||
assert files != null; | ||
assertEquals(2, files.length); | ||
} | ||
|
||
@Test | ||
void listDirectories_fileFilter_methodReference() { | ||
File[] files = root.listFiles(File::isDirectory); | ||
assert files != null; | ||
assertEquals(2, files.length); | ||
} | ||
|
||
@Test | ||
void iterateOverMap() { | ||
Map.of("a", 1, "b", 2, "c", 2) | ||
.forEach((k, v) -> System.out.println(k + " -> " + v)); | ||
} | ||
|
||
@Test | ||
void iterateOverList() { | ||
List.of("this", "is", "a", "list") | ||
.forEach(System.out::println); | ||
|
||
} | ||
} |