Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.flattened-pom.xml
target
.idea
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>fake-sftp-server-lambda</artifactId>
<version>2.0.0</version>
<version>2.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Fake SFTP Server Lambda</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.stream.Stream;

import static com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder.newLinux;
import static java.nio.file.FileVisitResult.CONTINUE;
Expand Down Expand Up @@ -413,6 +414,21 @@ public boolean existsFile(
return exists(pathAsObject) && !isDirectory(pathAsObject);
}

/**
* List all files and directories in a given directory
* @param path the path to the directory
* @return a stream with of all files in the directory
* @throws IOException see {@link Files#list(Path)}
*/
public Stream<Path> listFilesAndDirectories(
String path
) throws IOException
{
verifyWithSftpServerIsNotFinished("list files");
Path pathAsObject = fileSystem.getPath(path);
return Files.list(pathAsObject);
}

/**
* Deletes all files and directories.
* @throws IOException if an I/O error is thrown while deleting the files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer;
import static com.github.stefanbirkner.fishbowl.Fishbowl.exceptionThrownBy;
Expand Down Expand Up @@ -603,6 +607,70 @@ public void existence_of_a_file_cannot_be_checked_outside_of_the_lambda(
}
}

public static class list_files_and_directories_check {
@Test
public void list_returns_all_existing_files_in_given_directory(
) throws Exception {
withSftpServer(
server -> {
uploadFile(
server,
"/dummy_directory/dummy_file.bin",
DUMMY_CONTENT
);
uploadFile(
server,
"/dummy_directory/directory/dummy_file.bin",
DUMMY_CONTENT
);
Stream<Path> fileList = server.listFilesAndDirectories(
"/dummy_directory"
);
assertThat(fileList)
.hasSize(2)
.extracting(Path::toString)
.contains("/dummy_directory/dummy_file.bin", "/dummy_directory/directory");
}
);
}

@Test
public void list_on_not_existing_directory_will_fail_with_exception(
) throws Exception {
withSftpServer(
server -> {
uploadFile(
server,
"/dummy_directory/dummy_file.bin",
DUMMY_CONTENT
);
assertThatThrownBy(() -> server.listFilesAndDirectories(
"/unknown_directory"
)).isInstanceOf(NoSuchFileException.class);
}
);
}

@Test
public void list_cannot_be_checked_outside_of_the_lambda(
) throws Exception {
AtomicReference<FakeSftpServer> serverCapture
= new AtomicReference<>();
withSftpServer(
serverCapture::set
);
Throwable exception = exceptionThrownBy(
() -> serverCapture.get().listFilesAndDirectories("/dummy_directory")
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to list files because withSftpServer"
+ " is already finished."
);
}
}

public static class server_shutdown {
@Test
public void after_a_successful_test_SFTP_server_is_shutdown(
Expand Down