Skip to content
Browse files

Changes suggested by kcooney

kcooney suggested some changes for #479
  • Loading branch information...
1 parent 6012fa3 commit 6b822aa3037d731669ace4a4bb6b859bf9986def Arjan Wulder committed
View
15 src/main/java/org/junit/rules/TemporaryFolder.java
@@ -61,7 +61,7 @@ public void create() throws IOException {
public File newFile(String fileName) throws IOException {
File file= new File(getRoot(), fileName);
if (!file.createNewFile())
- throw new IllegalStateException(
+ throw new IllegalArgumentException(
"a file with the name \'" + fileName + "\' already exists in the test folder");
return file;
}
@@ -87,15 +87,20 @@ public File newFolder(String folder) {
*/
public File newFolder(String... folderNames) {
File file= getRoot();
- for (String folderName : folderNames) {
- file= new File(file, folderName);
- if (!file.mkdir())
- throw new IllegalStateException(
+ for (int i = 0; i < folderNames.length; i++) {
+ String folderName = folderNames[i];
+ file = new File(file, folderName);
+ if (!file.mkdir() && isLastElementInArray(i, folderNames))
+ throw new IllegalArgumentException(
"a folder with the name \'" + folderName + "\' already exists");
}
return file;
}
+ private boolean isLastElementInArray(int index, String[] array) {
+ return index == array.length - 1;
+ }
+
/**
* Returns a new fresh folder with a random name under the temporary folder.
*/
View
39 src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java
@@ -1,14 +1,19 @@
package org.junit.tests.experimental.rules;
-import static org.junit.Assert.*;
-import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
/**
@@ -19,6 +24,9 @@
private TemporaryFolder tempFolder;
+ @Rule
+ public final ExpectedException thrown = ExpectedException.none();
+
@Before
public void setUp() {
tempFolder= new TemporaryFolder();
@@ -46,6 +54,16 @@ public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked
new TemporaryFolder().newFile("MyFile.txt");
}
+ @Test
+ public void newFileWithGivenFilenameThrowsIllegalArgumentExceptionIfFileExists() throws IOException {
+ tempFolder.create();
+ tempFolder.newFile("MyFile.txt");
+
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("a file with the name 'MyFile.txt' already exists in the test folder");
+ tempFolder.newFile("MyFile.txt");
+ }
+
@Test(expected= IllegalStateException.class)
public void newFolderThrowsIllegalStateExceptionIfCreateWasNotInvoked()
throws IOException {
@@ -57,10 +75,23 @@ public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvok
new TemporaryFolder().newFolder("level1", "level2", "level3");
}
- @Test(expected = IllegalStateException.class)
- public void newFolderWithGivenPathThrowsIllegalStateExceptionIfPathExists() throws IOException {
+ @Test
+ public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists() throws IOException {
+ tempFolder.create();
+ tempFolder.newFolder("level1");
+
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("a folder with the name 'level1' already exists");
+ tempFolder.newFolder("level1");
+ }
+
+ @Test
+ public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() throws IOException {
tempFolder.create();
tempFolder.newFolder("level1", "level2", "level3");
+
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("a folder with the name 'level3' already exists");
tempFolder.newFolder("level1", "level2", "level3");
}

0 comments on commit 6b822aa

Please sign in to comment.
Something went wrong with that request. Please try again.