Skip to content

Commit a9e9651

Browse files
committed
Create multiple directories at once
Users don't have to call `createDirectory` for each directory. Fixes #3.
1 parent 00f0113 commit a9e9651

File tree

4 files changed

+136
-42
lines changed

4 files changed

+136
-42
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Fake SFTP Server Rule is available from [Maven Central](http://search.maven.org/
2121
<dependency>
2222
<groupId>com.github.stefanbirkner</groupId>
2323
<artifactId>fake-sftp-server-rule</artifactId>
24-
<version>1.1.1</version>
24+
<version>1.2.0</version>
2525
</dependency>
2626

2727

@@ -95,6 +95,17 @@ If you need an empty directory then you can use the method
9595
//code that reads from or writes to that directory
9696
}
9797

98+
You may create multiple directories at once with `createDirectories(String...)`.
99+
100+
@Test
101+
public void testDirectories() {
102+
sftpServer.createDirectories(
103+
"/a/directory",
104+
"/another/directory"
105+
);
106+
//code that reads from or writes to that directories
107+
}
108+
98109

99110
### Testing code that writes files
100111

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.2.0-SNAPSHOT</version>
12+
<version>1.2.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: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,18 @@
8989
* //code that reads from or writes to that directory
9090
* }
9191
* </pre>
92-
*
92+
* <p>You may create multiple directories at once with
93+
* {@link #createDirectories(String...)}.
94+
* <pre>
95+
* &#064;Test
96+
* public void testDirectories() {
97+
* sftpServer.{@link #createDirectories(String...) createDirectories}(
98+
* "/a/directory",
99+
* "/another/directory"
100+
* );
101+
* //code that reads from or writes to that directories
102+
* }
103+
* </pre>
93104
* <h2>Testing code that writes files</h2>
94105
* <p>If you test code that writes files to an SFTP server then you need to
95106
* verify the upload. Fake SFTP Server Rule provides a shortcut for getting the
@@ -235,7 +246,19 @@ public void createDirectory(
235246
) throws IOException {
236247
verifyThatTestIsRunning("create directory");
237248
Path pathAsObject = fileSystem.getPath(path);
238-
createDirectories(pathAsObject);
249+
Files.createDirectories(pathAsObject);
250+
}
251+
252+
/**
253+
* Create multiple directories on the SFTP server.
254+
* @param paths the directories' paths.
255+
* @throws IOException if at least one directory cannot be created.
256+
*/
257+
public void createDirectories(
258+
String... paths
259+
) throws IOException {
260+
for (String path: paths)
261+
createDirectory(path);
239262
}
240263

241264
/**
@@ -339,7 +362,7 @@ private void ensureDirectoryOfPathExists(
339362
) throws IOException {
340363
Path directory = path.getParent();
341364
if (directory != null && !directory.equals(path.getRoot()))
342-
createDirectories(directory);
365+
Files.createDirectories(directory);
343366
}
344367

345368
private void verifyThatTestIsRunning(

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

Lines changed: 97 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -305,49 +305,109 @@ public void cannot_be_put_after_the_test_is_finished() {
305305
}
306306
}
307307

308+
@RunWith(Enclosed.class)
308309
public static class directory_creation {
309-
@Test
310-
public void a_directory_that_is_created_with_the_rule_can_be_read_by_a_client() {
311-
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
312-
executeTestWithRule(
313-
() -> {
314-
sftpServer.createDirectory("/a/directory");
315-
assertEmptyDirectory(sftpServer, "/a/directory");
316-
},
317-
sftpServer
318-
);
319-
}
320310

321-
@Test
322-
public void a_directory_cannot_be_created_before_the_test_is_started() {
323-
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
324-
Throwable exception = exceptionThrownBy(
325-
() -> sftpServer.createDirectory("/a/directory")
326-
);
327-
assertThat(exception)
328-
.isInstanceOf(IllegalStateException.class)
329-
.hasMessage(
330-
"Failed to create directory because test has not been"
331-
+ " started or is already finished."
311+
public static class a_single_directory {
312+
@Test
313+
public void that_is_created_with_the_rule_can_be_read_by_a_client() {
314+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
315+
executeTestWithRule(
316+
() -> {
317+
sftpServer.createDirectory("/a/directory");
318+
assertEmptyDirectory(sftpServer, "/a/directory");
319+
},
320+
sftpServer
321+
);
322+
}
323+
324+
@Test
325+
public void cannot_be_created_before_the_test_is_started() {
326+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
327+
Throwable exception = exceptionThrownBy(
328+
() -> sftpServer.createDirectory("/a/directory")
332329
);
330+
assertThat(exception)
331+
.isInstanceOf(IllegalStateException.class)
332+
.hasMessage(
333+
"Failed to create directory because test has not been"
334+
+ " started or is already finished."
335+
);
336+
}
337+
338+
@Test
339+
public void cannot_be_created_after_the_test_is_finished() {
340+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
341+
executeTestWithRule(
342+
() -> {},
343+
sftpServer
344+
);
345+
Throwable exception = exceptionThrownBy(
346+
() -> sftpServer.createDirectory("/a/directory")
347+
);
348+
assertThat(exception)
349+
.isInstanceOf(IllegalStateException.class)
350+
.hasMessage(
351+
"Failed to create directory because test has not been"
352+
+ " started or is already finished."
353+
);
354+
}
333355
}
334356

335-
@Test
336-
public void a_directory_cannot_be_created_after_the_test_is_finished() {
337-
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
338-
executeTestWithRule(
339-
() -> {},
340-
sftpServer
341-
);
342-
Throwable exception = exceptionThrownBy(
343-
() -> sftpServer.createDirectory("/a/directory")
344-
);
345-
assertThat(exception)
346-
.isInstanceOf(IllegalStateException.class)
347-
.hasMessage(
348-
"Failed to create directory because test has not been"
349-
+ " started or is already finished."
357+
public static class multiple_directories {
358+
@Test
359+
public void that_are_created_with_the_rule_can_be_read_by_a_client() {
360+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
361+
executeTestWithRule(
362+
() -> {
363+
sftpServer.createDirectories(
364+
"/a/directory",
365+
"/another/directory"
366+
);
367+
assertEmptyDirectory(sftpServer, "/a/directory");
368+
assertEmptyDirectory(sftpServer, "/another/directory");
369+
},
370+
sftpServer
371+
);
372+
}
373+
374+
@Test
375+
public void cannot_be_created_before_the_test_is_started() {
376+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
377+
Throwable exception = exceptionThrownBy(
378+
() -> sftpServer.createDirectories(
379+
"/a/directory",
380+
"/another/directory"
381+
)
350382
);
383+
assertThat(exception)
384+
.isInstanceOf(IllegalStateException.class)
385+
.hasMessage(
386+
"Failed to create directory because test has not been"
387+
+ " started or is already finished."
388+
);
389+
}
390+
391+
@Test
392+
public void cannot_be_created_after_the_test_is_finished() {
393+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
394+
executeTestWithRule(
395+
() -> {},
396+
sftpServer
397+
);
398+
Throwable exception = exceptionThrownBy(
399+
() -> sftpServer.createDirectories(
400+
"/a/directory",
401+
"/another/directory"
402+
)
403+
);
404+
assertThat(exception)
405+
.isInstanceOf(IllegalStateException.class)
406+
.hasMessage(
407+
"Failed to create directory because test has not been"
408+
+ " started or is already finished."
409+
);
410+
}
351411
}
352412

353413
private static void assertEmptyDirectory(

0 commit comments

Comments
 (0)