Skip to content

Commit 288c6df

Browse files
committed
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 288c6df

File tree

2 files changed

+54
-45
lines changed

2 files changed

+54
-45
lines changed

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

+24-39
Original file line numberDiff line numberDiff line change
@@ -171,59 +171,44 @@ 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;
196+
boolean lastMkdirsCallSuccessful = true;
198197
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)) {
198+
relativePath = relativePath == null ? new File(paths[i]) : new File(relativePath, paths[i]);
199+
file = new File(root, relativePath.getPath());
200+
201+
lastMkdirsCallSuccessful = file.mkdirs();
202+
if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
203203
throw new IOException(
204-
"a folder with the name \'" + folderName + "\' already exists");
204+
"could not create a folder with the path \'" + relativePath.getPath() + "\'");
205205
}
206206
}
207-
return file;
208-
}
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);
207+
if (!lastMkdirsCallSuccessful) {
208+
throw new IOException(
209+
"a folder with the path \'" + relativePath.getPath() + "\' already exists");
222210
}
223-
}
224-
225-
private boolean isLastElementInArray(int index, String[] array) {
226-
return index == array.length - 1;
211+
return file;
227212
}
228213

229214
/**

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)