BAEL-5712: Improvement in Listing files under directory (#12643)

Co-authored-by: Harpal Singh <harpal.singh@kaleyra.com>
This commit is contained in:
Harry9656 2022-09-08 22:48:29 +02:00 committed by GitHub
parent b8bab5bcbe
commit cbcf5d4050
2 changed files with 58 additions and 47 deletions

View File

@ -2,12 +2,7 @@ package com.baeldung.listfiles;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream; import java.nio.file.*;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -15,7 +10,6 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class ListFiles { public class ListFiles {
public static final int DEPTH = 1;
public Set<String> listFilesUsingJavaIO(String dir) { public Set<String> listFilesUsingJavaIO(String dir) {
return Stream.of(new File(dir).listFiles()) return Stream.of(new File(dir).listFiles())
@ -48,10 +42,9 @@ public class ListFiles {
Set<String> fileList = new HashSet<>(); Set<String> fileList = new HashSet<>();
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() { Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (!Files.isDirectory(file)) { if (!Files.isDirectory(file)) {
fileList.add(file.getFileName() fileList.add(file.getFileName().toString());
.toString());
} }
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@ -60,16 +53,14 @@ public class ListFiles {
} }
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException { public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
Set<String> fileList = new HashSet<>(); Set<String> fileSet = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
for (Path path : stream) { for (Path path : stream) {
if (!Files.isDirectory(path)) { if (!Files.isDirectory(path)) {
fileList.add(path.getFileName() fileSet.add(path.getFileName().toString());
.toString());
} }
} }
} }
return fileList; return fileSet;
} }
} }

View File

@ -1,21 +1,21 @@
package com.baeldung.listfiles; package com.baeldung.listfiles;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test; import org.junit.Test;
import com.baeldung.listfiles.ListFiles; import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ListFilesUnitTest { public class ListFilesUnitTest {
private ListFiles listFiles = new ListFiles(); private static final String VALID_DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder"; private static final String INVALID_DIRECTORY = "src/test/resources/thisDirectoryDoesNotExist";
private static final int DEPTH = 1; private static final Set<String> EXPECTED_FILE_SET = new HashSet<String>() {
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
{ {
add("test.xml"); add("test.xml");
add("employee.json"); add("employee.json");
@ -23,29 +23,49 @@ public class ListFilesUnitTest {
add("country.txt"); add("country.txt");
} }
}; };
private static final int DEPTH = 1;
private final ListFiles listFiles = new ListFiles();
@Test @Test
public void givenDir_whenUsingJAVAIO_thenListAllFiles() { public void givenAValidDirectoryWhenUsingJavaIOThenReturnSetOfFileNames() {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY)); assertThat(listFiles.listFilesUsingJavaIO(VALID_DIRECTORY))
.isEqualTo(EXPECTED_FILE_SET);
} }
@Test @Test
public void givenDir_whenUsingFilesList_thenListAllFiles() throws IOException { public void givenAInvalidValidDirectoryWhenUsingJavaIOThenThrowsNullPointerExceptino() {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFilesList(DIRECTORY)); assertThrows(NullPointerException.class,
() -> listFiles.listFilesUsingJavaIO(INVALID_DIRECTORY));
} }
@Test @Test
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException { public void givenAValidDirectoryWhenUsingFilesListThenReturnSetOfFileNames() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH)); assertThat(listFiles.listFilesUsingFilesList(VALID_DIRECTORY))
.isEqualTo(EXPECTED_FILE_SET);
} }
@Test @Test
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException { public void givenAValidDirectoryWhenUsingFilesWalkWithDepth1ThenReturnSetOfFileNames() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY)); assertThat(listFiles.listFilesUsingFileWalk(VALID_DIRECTORY, DEPTH))
.isEqualTo(EXPECTED_FILE_SET);
} }
@Test @Test
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException { public void givenAValidDirectoryWhenUsingFilesWalkFileTreeThenReturnSetOfFileNames() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY)); assertThat(listFiles.listFilesUsingFileWalkAndVisitor(VALID_DIRECTORY))
.isEqualTo(EXPECTED_FILE_SET);
}
@Test
public void givenAValidDirectoryWhenUsingDirectoryStreamThenReturnSetOfFileNames() throws IOException {
assertThat(listFiles.listFilesUsingDirectoryStream(VALID_DIRECTORY))
.isEqualTo(EXPECTED_FILE_SET);
}
@Test
public void givenAValidFileWhenUsingFilesWalkWithDepth1ThenReturnSetOfTheSameFile() throws IOException {
Set<String> expectedFileSet = Collections.singleton("test.xml");
String filePathString = "src/test/resources/listFilesUnitTestFolder/test.xml";
assertEquals(expectedFileSet, listFiles.listFilesUsingFileWalk(filePathString, DEPTH));
} }
} }