Skip to content

Commit a6d2f63

Browse files
committed
Add method "deleteAllFilesAndDirectories"
Provide a method for people who have complex tests and need to clear the server within the test. Fixes #5.
1 parent 8def25c commit a6d2f63

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Fake SFTP Server Rule is published under the
1111
[open an issue](https://github.com/stefanbirkner/fake-sftp-server-rule/issues/new)
1212
if you want to use it with an older version of Java.
1313

14-
I want to thank my former team SAM at ThoughtWorks for using this library.
14+
I want to thank my former team SAM at ThoughtWorks for using this library and
15+
@OArtyomov for his feature request.
1516

1617

1718
## Installation
@@ -22,7 +23,7 @@ Fake SFTP Server Rule is available from
2223
<dependency>
2324
<groupId>com.github.stefanbirkner</groupId>
2425
<artifactId>fake-sftp-server-rule</artifactId>
25-
<version>1.2.0</version>
26+
<version>1.3.0</version>
2627
</dependency>
2728

2829

@@ -142,6 +143,13 @@ verify that it exists or not.
142143

143144
The method returns `true` iff the file exists and it is not a directory.
144145

146+
### Delete all files
147+
148+
If you want to reuse the SFTP server then you can delete all files and
149+
directories on the SFTP server. (This is rarely necessary because the rule
150+
itself takes care that every test starts and ends with a clean SFTP server.)
151+
152+
sftpServer.deleteAllFilesAndDirectories()
145153

146154
## Contributing
147155

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>fake-sftp-server-rule</artifactId>
12-
<version>1.3.0-SNAPSHOT</version>
12+
<version>1.3.0</version>
1313
<packaging>jar</packaging>
1414

1515
<name>Fake SFTP Server Rule</name>

src/main/java/com/github/stefanbirkner/fakesftpserver/rule/FakeSftpServerRule.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import java.io.InputStream;
1313
import java.nio.charset.Charset;
1414
import java.nio.file.*;
15+
import java.nio.file.attribute.BasicFileAttributes;
1516
import java.nio.file.attribute.UserPrincipalLookupService;
1617
import java.nio.file.spi.FileSystemProvider;
1718
import java.util.Set;
1819

1920
import static com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder.newLinux;
21+
import static java.nio.file.FileVisitResult.CONTINUE;
2022
import static java.nio.file.Files.*;
2123
import static java.util.Collections.singletonList;
2224

@@ -133,8 +135,25 @@
133135
* }
134136
* </pre>
135137
* <p>The method returns {@code true} iff the file exists and it is not a directory.
138+
*
139+
* <h2>Delete all files</h2>
140+
* <p>If you want to reuse the SFTP server then you can delete all files and
141+
* directories on the SFTP server. (This is rarely necessary because the rule
142+
* itself takes care that every test starts and ends with a clean SFTP server.)
143+
* <pre>{@link #deleteAllFilesAndDirectories() sftpServer.deleteAllFilesAndDirectories()};</pre>
136144
*/
137145
public class FakeSftpServerRule implements TestRule {
146+
private static final SimpleFileVisitor<Path> DELETE_FILES_AND_DIRECTORIES
147+
= new SimpleFileVisitor<Path>() {
148+
@Override
149+
public FileVisitResult visitFile(
150+
Path file,
151+
BasicFileAttributes attrs
152+
) throws IOException {
153+
delete(file);
154+
return CONTINUE;
155+
}
156+
};
138157
private int port = 23454;
139158

140159
private FileSystem fileSystem;
@@ -308,6 +327,16 @@ public boolean existsFile(
308327
return exists(pathAsObject) && !isDirectory(pathAsObject);
309328
}
310329

330+
/**
331+
* Deletes all files and directories.
332+
* @throws IOException if an I/O error is thrown while deleting the files
333+
* and directories
334+
*/
335+
public void deleteAllFilesAndDirectories() throws IOException {
336+
for (Path directory: fileSystem.getRootDirectories())
337+
walkFileTree(directory, DELETE_FILES_AND_DIRECTORIES);
338+
}
339+
311340
@Override
312341
public Statement apply(
313342
Statement base,

src/test/java/com/github/stefanbirkner/fakesftpserver/rule/FakeSftpServerRuleTest.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,98 @@ public void port_can_be_read_after_the_test() {
794794
}
795795
}
796796

797+
public static class cleanup {
798+
799+
@Test
800+
public void deletes_file_in_root_directory() {
801+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
802+
executeTestWithRule(
803+
() -> {
804+
uploadFile(
805+
sftpServer,
806+
"/dummy_file.bin",
807+
DUMMY_CONTENT
808+
);
809+
sftpServer.deleteAllFilesAndDirectories();
810+
assertFileDoesNotExist(
811+
sftpServer,
812+
"/dummy_file.bin"
813+
);
814+
},
815+
sftpServer
816+
);
817+
}
818+
819+
@Test
820+
public void deletes_file_in_directory() {
821+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
822+
executeTestWithRule(
823+
() -> {
824+
uploadFile(
825+
sftpServer,
826+
"/dummy_directory/dummy_file.bin",
827+
DUMMY_CONTENT
828+
);
829+
sftpServer.deleteAllFilesAndDirectories();
830+
assertFileDoesNotExist(
831+
sftpServer,
832+
"/dummy_directory/dummy_file.bin"
833+
);
834+
},
835+
sftpServer
836+
);
837+
}
838+
839+
@Test
840+
public void deletes_directory() {
841+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
842+
executeTestWithRule(
843+
() -> {
844+
sftpServer.createDirectory("dummy_directory");
845+
sftpServer.deleteAllFilesAndDirectories();
846+
assertDirectoryDoesNotExist(
847+
sftpServer,
848+
"/dummy_directory"
849+
);
850+
},
851+
sftpServer
852+
);
853+
}
854+
855+
@Test
856+
public void works_on_an_empty_filesystem() {
857+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
858+
executeTestWithRule(
859+
sftpServer::deleteAllFilesAndDirectories,
860+
sftpServer
861+
);
862+
}
863+
864+
private static void assertFileDoesNotExist(
865+
FakeSftpServerRule sftpServer,
866+
String path
867+
) {
868+
boolean exists = sftpServer.existsFile(path);
869+
assertThat(exists).isFalse();
870+
}
871+
872+
private static void assertDirectoryDoesNotExist(
873+
FakeSftpServerRule sftpServer,
874+
String directory
875+
) throws JSchException {
876+
Session session = connectToServer(sftpServer);
877+
ChannelSftp channel = connectSftpChannel(session);
878+
try {
879+
assertThatThrownBy(() -> channel.ls(directory))
880+
.isInstanceOf(SftpException.class)
881+
.hasMessage("Internal FileNotFoundException: " + directory);
882+
} finally {
883+
channel.disconnect();
884+
session.disconnect();
885+
}
886+
}
887+
}
888+
797889
private static Session connectToServer(
798890
FakeSftpServerRule sftpServer
799891
) throws JSchException {
@@ -852,7 +944,8 @@ private static void uploadFile(
852944
ChannelSftp channel = connectSftpChannel(session);
853945
try {
854946
Path path = Paths.get(pathAsString);
855-
channel.mkdir(path.getParent().toString());
947+
if (!path.getParent().equals(path.getRoot()))
948+
channel.mkdir(path.getParent().toString());
856949
channel.put(new ByteArrayInputStream(content), pathAsString);
857950
} finally {
858951
channel.disconnect();

0 commit comments

Comments
 (0)