Skip to content

Commit 326f9bb

Browse files
kcooneystefanbirkner
authored andcommitted
newFolder(String...) now throws IAE on an empty array
1 parent 852762b commit 326f9bb

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Diff for: src/main/java/org/junit/rules/TemporaryFolder.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,15 @@ public File newFolder(String path) throws IOException {
182182
* {@code "parent"} directory.
183183
*/
184184
public File newFolder(String... paths) throws IOException {
185-
// Before checking the paths, check if create() was ever called, and if it wasn't, throw
186-
File root = getRoot();
185+
if (paths.length == 0) {
186+
throw new IllegalArgumentException("must pass at least one path");
187+
}
187188

189+
/*
190+
* Before checking if the paths are absolute paths, check if create() was ever called,
191+
* and if it wasn't, throw IllegalStateException.
192+
*/
193+
File root = getRoot();
188194
for (String path : paths) {
189195
if (new File(path).isAbsolute()) {
190196
throw new IOException("folder path \'" + path + "\' is not a relative path");

Diff for: src/test/java/org/junit/rules/TemporaryFolderUsageTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfFolderExists()
141141
tempFolder.newFolder("level1", "level2", "level3");
142142
}
143143

144+
@Test
145+
public void newFolderWithGivenEmptyArrayThrowsIllegalArgumentException() throws IOException {
146+
tempFolder.create();
147+
148+
thrown.expect(IllegalArgumentException.class);
149+
thrown.expectMessage("must pass at least one path");
150+
tempFolder.newFolder(new String[0]);
151+
}
152+
144153
@Test
145154
public void newFolderWithPathsContainingForwardSlashCreatesFullPath()
146155
throws IOException {

0 commit comments

Comments
 (0)