Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TemporaryFolder.newFolder(String) to support passing in paths #1402

Merged
Merged
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
17 changes: 14 additions & 3 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,27 @@ public File newFile() throws IOException {
}

/**
* Returns a new fresh folder with the given name under the temporary
* Returns a new fresh folder with the given path under the temporary
* folder.
*/
public File newFolder(String folder) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably rename folder to path, right?

return newFolder(new String[]{folder});
if (new File(folder).isAbsolute()) {
throw new IOException("folder name must be a relative path");
}
File file = new File(getRoot(), folder);
if (!file.mkdirs()) {
throw new IOException(
"a folder with the name \'" + folder + "\' already exists");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this error message really accurate? I know it's the same as in the other method but I'm sure there are other reasons why a folder could not be created.

}
return file;
}

/**
* Returns a new fresh folder with the given name(s) under the temporary
* folder.
* folder. For example, if you pass in the strings {@code "parent"} and {@code "child"}
* then a directory named {@code "parent"} will be created under the temporary folder
* and a directory named {@code "child"} will be created under the newly-created
* {@code "parent"} directory.
*/
public File newFolder(String... folderNames) throws IOException {
File file = getRoot();
Expand Down
42 changes: 37 additions & 5 deletions src/test/java/org/junit/rules/TemporaryFolderUsageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,32 @@ public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists
}

@Test
public void newFolderWithGivenFolderThrowsIOExceptionIfFolderNameConsistsOfMultiplePathComponents()
public void newFolderWithPathStartingWithFileSeparatorThrowsIOException()
throws IOException {
tempFolder.create();
thrown.expect(IOException.class);
thrown.expectMessage("name cannot consist of multiple path components");
thrown.expectMessage("folder name must be a relative path");
tempFolder.newFolder(File.separator + "temp1");
}

@Test
public void newFolderWithPathContainingFileSeparaterCreatesDirectories()
throws IOException {
tempFolder.create();
tempFolder.newFolder("temp1" + File.separator + "temp2");
File temp1 = new File(tempFolder.getRoot(), "temp1");
assertFileIsDirectory(temp1);
assertFileIsDirectory(new File(temp1, "temp2"));
}

@Test
public void newFolderWithPathContainingForwardSlashCreatesDirectories()
throws IOException {
tempFolder.create();
tempFolder.newFolder("temp1/temp2");
File temp1 = new File(tempFolder.getRoot(), "temp1");
assertFileIsDirectory(temp1);
assertFileIsDirectory(new File(temp1, "temp2"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test won't run on Windows, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

@Test
Expand All @@ -114,7 +134,7 @@ public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultipl
@Test
public void createInitializesRootFolder() throws IOException {
tempFolder.create();
assertFileExists(tempFolder.getRoot());
assertFileIsDirectory(tempFolder.getRoot());
}

@Test
Expand Down Expand Up @@ -155,7 +175,7 @@ public void newRandomFolderIsCreatedUnderRootFolder() throws IOException {
tempFolder.create();

File f = tempFolder.newFolder();
assertFileExists(f);
assertFileIsDirectory(f);
assertFileCreatedUnderRootFolder("Random folder", f);
}

Expand All @@ -164,7 +184,7 @@ public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
tempFolder.create();

File f = tempFolder.newFolder("top", "middle", "bottom");
assertFileExists(f);
assertFileIsDirectory(f);
assertParentFolderForFileIs(f, new File(tempFolder.getRoot(),
"top/middle"));
assertParentFolderForFileIs(f.getParentFile(),
Expand Down Expand Up @@ -200,8 +220,20 @@ private void checkFileExists(String msg, File file, boolean exists) {
file.exists(), is(exists));
}

private void checkFileIsDirectory(String msg, File file, boolean isDirectory) {
assertThat("File is null", file, is(notNullValue()));
assertThat("File '" + file.getAbsolutePath() + "' " + msg,
file.isDirectory(), is(isDirectory));
}

private void assertFileExists(File file) {
checkFileExists("does not exist", file, true);
checkFileIsDirectory("is a directory", file, false);
}

private void assertFileIsDirectory(File file) {
checkFileExists("does not exist", file, true);
checkFileIsDirectory("is not a directory", file, true);
}

private void assertFileCreatedUnderRootFolder(String msg, File f) {
Expand Down