Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6763c81

Browse files
committedJan 6, 2017
newFolder(String...) now supports passing in paths with file separators.
The error messages on failures now include the full relative path that could not be created.
1 parent 89063fb commit 6763c81

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed
 

‎src/main/java/org/junit/rules/TemporaryFolder.java

+23-36
Original file line numberDiff line numberDiff line change
@@ -171,56 +171,43 @@ public File newFile() throws IOException {
171171
* folder.
172172
*/
173173
public File newFolder(String path) throws IOException {
174-
if (new File(path).isAbsolute()) {
175-
throw new IOException("folder path must be a relative path");
176-
}
177-
File file = new File(getRoot(), path);
178-
if (!file.mkdirs()) {
179-
if (file.isDirectory()) {
180-
throw new IOException(
181-
"a folder with the path \'" + path + "\' already exists");
182-
}
183-
throw new IOException(
184-
"could not create a folder with the path \'" + path + "\'");
185-
}
186-
return file;
174+
return newFolder(new String[]{path});
187175
}
188176

189177
/**
190-
* Returns a new fresh folder with the given path under the temporary
178+
* Returns a new fresh folder with the given paths under the temporary
191179
* folder. For example, if you pass in the strings {@code "parent"} and {@code "child"}
192180
* then a directory named {@code "parent"} will be created under the temporary folder
193181
* and a directory named {@code "child"} will be created under the newly-created
194182
* {@code "parent"} directory.
195183
*/
196184
public File newFolder(String... paths) throws IOException {
197-
File file = getRoot();
185+
// Before checking the paths, check if create() was ever called, and if it wasn't, throw
186+
File root = getRoot();
187+
188+
for (String path : paths) {
189+
if (new File(path).isAbsolute()) {
190+
throw new IOException("folder path \'" + path + "\' is not a relative path");
191+
}
192+
}
193+
194+
File relativePath = null;
195+
File file = root;
198196
for (int i = 0; i < paths.length; i++) {
199-
String folderName = paths[i];
200-
validateFolderName(folderName);
201-
file = new File(file, folderName);
202-
if (!file.mkdir() && isLastElementInArray(i, paths)) {
203-
throw new IOException(
204-
"a folder with the name \'" + folderName + "\' already exists");
197+
relativePath = relativePath == null ? new File(paths[i]) : new File(relativePath, paths[i]);
198+
file = new File(root, relativePath.getPath());
199+
if (!file.mkdirs()) {
200+
if (!file.isDirectory()) {
201+
throw new IOException(
202+
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
203+
} else if (isLastElementInArray(i, paths)) {
204+
throw new IOException(
205+
"a folder with the path \'" + relativePath.getPath() + "\' already exists");
206+
}
205207
}
206208
}
207209
return file;
208210
}
209-
210-
/**
211-
* Validates if multiple path components were used while creating a folder.
212-
*
213-
* @param folderName
214-
* Name of the folder being created
215-
*/
216-
private void validateFolderName(String folderName) throws IOException {
217-
File tempFile = new File(folderName);
218-
if (tempFile.getParent() != null) {
219-
String errorMsg = "Folder name cannot consist of multiple path components separated by a file separator."
220-
+ " Please use newFolder('MyParentFolder','MyFolder') to create hierarchies of folders";
221-
throw new IOException(errorMsg);
222-
}
223-
}
224211

225212
private boolean isLastElementInArray(int index, String[] array) {
226213
return index == array.length - 1;

‎src/test/java/org/junit/rules/TemporaryFolderUsageTest.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void newFolderWithPathStartingWithFileSeparatorThrowsIOException()
9999
throws IOException {
100100
tempFolder.create();
101101
thrown.expect(IOException.class);
102-
thrown.expectMessage("folder path must be a relative path");
102+
thrown.expectMessage("folder path '/temp1' is not a relative path");
103103
tempFolder.newFolder(File.separator + "temp1");
104104
}
105105

@@ -124,22 +124,46 @@ public void newFolderWithPathContainingForwardSlashCreatesDirectories()
124124
}
125125

126126
@Test
127-
public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() throws IOException {
127+
public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfFolderExists() throws IOException {
128128
tempFolder.create();
129129
tempFolder.newFolder("level1", "level2", "level3");
130130

131131
thrown.expect(IOException.class);
132-
thrown.expectMessage("a folder with the name 'level3' already exists");
132+
String path = "level1" + File.separator + "level2" + File.separator + "level3";
133+
thrown.expectMessage("a folder with the path '" + path + "' already exists");
133134
tempFolder.newFolder("level1", "level2", "level3");
134135
}
135136

136137
@Test
137-
public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultiplePathComponents()
138+
public void newFolderWithPathsContainingForwardSlashCreatesFullPath()
138139
throws IOException {
139140
tempFolder.create();
140-
thrown.expect(IOException.class);
141-
thrown.expectMessage("name cannot consist of multiple path components");
142141
tempFolder.newFolder("temp1", "temp2", "temp3/temp4");
142+
143+
File directory = new File(tempFolder.getRoot(), "temp1");
144+
assertFileIsDirectory(directory);
145+
directory = new File(directory, "temp2");
146+
assertFileIsDirectory(directory);
147+
directory = new File(directory, "temp3");
148+
assertFileIsDirectory(directory);
149+
directory = new File(directory, "temp4");
150+
assertFileIsDirectory(directory);
151+
}
152+
153+
@Test
154+
public void newFolderWithPathsContainingFileSeparatorCreatesFullPath()
155+
throws IOException {
156+
tempFolder.create();
157+
tempFolder.newFolder("temp1", "temp2", "temp3" + File.separator + "temp4");
158+
159+
File directory = new File(tempFolder.getRoot(), "temp1");
160+
assertFileIsDirectory(directory);
161+
directory = new File(directory, "temp2");
162+
assertFileIsDirectory(directory);
163+
directory = new File(directory, "temp3");
164+
assertFileIsDirectory(directory);
165+
directory = new File(directory, "temp4");
166+
assertFileIsDirectory(directory);
143167
}
144168

145169
@Test

0 commit comments

Comments
 (0)
Please sign in to comment.