Skip to content

Commit

Permalink
Create multiple directories at once
Browse files Browse the repository at this point in the history
Users don't have to call `createDirectory` for each directory.

Fixes #3.
  • Loading branch information
stefanbirkner committed Sep 20, 2017
1 parent 00f0113 commit 2ee5de0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 42 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Fake SFTP Server Rule is available from [Maven Central](http://search.maven.org/
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>fake-sftp-server-rule</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>


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

You may create multiple directories at once with `createDirectories(String...)`.

@Test
public void testDirectories() {
sftpServer.createDirectories(
"/a/directory",
"/another/directory"
);
//code that reads from or writes to that directories
}


### Testing code that writes files

Expand Down
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-rule</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>Fake SFTP Server Rule</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,18 @@
* //code that reads from or writes to that directory
* }
* </pre>
*
* <p>You may create multiple directories at once with
* {@link #createDirectories(String...)}.
* <pre>
* &#064;Test
* public void testDirectories() {
* sftpServer.{@link #createDirectories(String...) createDirectories}(
* "/a/directory",
* "/another/directory"
* );
* //code that reads from or writes to that directories
* }
* </pre>
* <h2>Testing code that writes files</h2>
* <p>If you test code that writes files to an SFTP server then you need to
* verify the upload. Fake SFTP Server Rule provides a shortcut for getting the
Expand Down Expand Up @@ -235,7 +246,19 @@ public void createDirectory(
) throws IOException {
verifyThatTestIsRunning("create directory");
Path pathAsObject = fileSystem.getPath(path);
createDirectories(pathAsObject);
Files.createDirectories(pathAsObject);
}

/**
* Create multiple directories on the SFTP server.
* @param paths the directories' paths.
* @throws IOException if at least one directory cannot be created.
*/
public void createDirectories(
String... paths
) throws IOException {
for (String path: paths)
createDirectory(path);
}

/**
Expand Down Expand Up @@ -339,7 +362,7 @@ private void ensureDirectoryOfPathExists(
) throws IOException {
Path directory = path.getParent();
if (directory != null && !directory.equals(path.getRoot()))
createDirectories(directory);
Files.createDirectories(directory);
}

private void verifyThatTestIsRunning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,49 +305,110 @@ public void cannot_be_put_after_the_test_is_finished() {
}
}

@RunWith(Enclosed.class)
public static class directory_creation {
@Test
public void a_directory_that_is_created_with_the_rule_can_be_read_by_a_client() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {
sftpServer.createDirectory("/a/directory");
assertEmptyDirectory(sftpServer, "/a/directory");
},
sftpServer
);
}

@Test
public void a_directory_cannot_be_created_before_the_test_is_started() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectory("/a/directory")
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
public static class a_single_directory {
@Test
public void that_is_created_with_the_rule_can_be_read_by_a_client() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {
sftpServer.createDirectory("/a/directory");
assertEmptyDirectory(sftpServer, "/a/directory");
},
sftpServer
);
}

@Test
public void cannot_be_created_before_the_test_is_started() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectory("/a/directory")
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
);
}

@Test
public void cannot_be_created_after_the_test_is_finished() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {
},
sftpServer
);
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectory("/a/directory")
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
);
}
}

@Test
public void a_directory_cannot_be_created_after_the_test_is_finished() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {},
sftpServer
);
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectory("/a/directory")
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
public static class multiple_directories {
@Test
public void that_are_created_with_the_rule_can_be_read_by_a_client() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {
sftpServer.createDirectories(
"/a/directory",
"/another/directory"
);
assertEmptyDirectory(sftpServer, "/a/directory");
assertEmptyDirectory(sftpServer, "/another/directory");
},
sftpServer
);
}

@Test
public void cannot_be_created_before_the_test_is_started() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectories(
"/a/directory",
"/another/directory"
)
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
);
}

@Test
public void cannot_be_created_after_the_test_is_finished() {
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
executeTestWithRule(
() -> {},
sftpServer
);
Throwable exception = exceptionThrownBy(
() -> sftpServer.createDirectories(
"/a/directory",
"/another/directory"
)
);
assertThat(exception)
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Failed to create directory because test has not been"
+ " started or is already finished."
);
}
}

private static void assertEmptyDirectory(
Expand Down

0 comments on commit 2ee5de0

Please sign in to comment.