Skip to content

Commit

Permalink
Improve error message when TemporaryFolder.newFolder() fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
kcooney committed Jan 6, 2017
1 parent c85c614 commit 89063fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,32 +170,36 @@ public File newFile() throws IOException {
* Returns a new fresh folder with the given path under the temporary
* folder.
*/
public File newFolder(String folder) throws IOException {
if (new File(folder).isAbsolute()) {
throw new IOException("folder name must be a relative path");
public File newFolder(String path) throws IOException {
if (new File(path).isAbsolute()) {
throw new IOException("folder path must be a relative path");
}
File file = new File(getRoot(), folder);
File file = new File(getRoot(), path);
if (!file.mkdirs()) {
if (file.isDirectory()) {
throw new IOException(
"a folder with the path \'" + path + "\' already exists");
}
throw new IOException(
"a folder with the name \'" + folder + "\' already exists");
"could not create a folder with the path \'" + path + "\'");
}
return file;
}

/**
* Returns a new fresh folder with the given name(s) under the temporary
* Returns a new fresh folder with the given path under the temporary
* 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 {
public File newFolder(String... paths) throws IOException {
File file = getRoot();
for (int i = 0; i < folderNames.length; i++) {
String folderName = folderNames[i];
for (int i = 0; i < paths.length; i++) {
String folderName = paths[i];
validateFolderName(folderName);
file = new File(file, folderName);
if (!file.mkdir() && isLastElementInArray(i, folderNames)) {
if (!file.mkdir() && isLastElementInArray(i, paths)) {
throw new IOException(
"a folder with the name \'" + folderName + "\' already exists");
}
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/org/junit/rules/TemporaryFolderUsageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,18 @@ public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists
tempFolder.newFolder("level1");

thrown.expect(IOException.class);
thrown.expectMessage("a folder with the name 'level1' already exists");
thrown.expectMessage("a folder with the path 'level1' already exists");
tempFolder.newFolder("level1");
}

@Test
public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFileExists() throws IOException {
tempFolder.create();
File file = new File(tempFolder.getRoot(), "level1");
assertTrue("Could not create" + file, file.createNewFile());

thrown.expect(IOException.class);
thrown.expectMessage("could not create a folder with the path 'level1'");
tempFolder.newFolder("level1");
}

Expand All @@ -88,7 +99,7 @@ public void newFolderWithPathStartingWithFileSeparatorThrowsIOException()
throws IOException {
tempFolder.create();
thrown.expect(IOException.class);
thrown.expectMessage("folder name must be a relative path");
thrown.expectMessage("folder path must be a relative path");
tempFolder.newFolder(File.separator + "temp1");
}

Expand Down

0 comments on commit 89063fb

Please sign in to comment.