Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #278: TemporaryFolder creates files in the current working dire…

…ctory if applying the rule fails
  • Loading branch information...
commit 70fe94d031c7066899fc464cd4171db0884f1356 1 parent fea0643
@orfjackal authored
View
3  acknowledgements.txt
@@ -110,3 +110,6 @@
2011 July 16
Rob Dawson: Submitted a patch that makes Results serlializable.
+
+2011 Aug 7
+ Esko Luontola: Fixed TemporaryFolder creating files in the current working directory (github#278).
View
11 src/main/java/org/junit/rules/TemporaryFolder.java
@@ -51,7 +51,7 @@ public void create() throws IOException {
* Returns a new fresh file with the given name under the temporary folder.
*/
public File newFile(String fileName) throws IOException {
- File file= new File(folder, fileName);
+ File file= new File(getRoot(), fileName);
file.createNewFile();
return file;
}
@@ -60,7 +60,7 @@ public File newFile(String fileName) throws IOException {
* Returns a new fresh folder with the given name under the temporary folder.
*/
public File newFolder(String folderName) {
- File file= new File(folder, folderName);
+ File file= new File(getRoot(), folderName);
file.mkdir();
return file;
}
@@ -69,12 +69,15 @@ public File newFolder(String folderName) {
* @return the location of this temporary folder.
*/
public File getRoot() {
+ if (folder == null) {
+ throw new IllegalStateException("the temporary folder has not yet been created");
+ }
return folder;
}
/**
* Delete all files and folders under the temporary folder.
- * Usually not called directly, since it is automatically applied
+ * Usually not called directly, since it is automatically applied
* by the {@link Rule}
*/
public void delete() {
@@ -88,4 +91,4 @@ private void recursiveDelete(File file) {
recursiveDelete(each);
file.delete();
}
-}
View
42 src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java
@@ -4,11 +4,13 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
+import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import java.io.File;
import java.io.IOException;
+import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -68,4 +70,44 @@ public void recursiveDeleteFolderWithZeroElements() throws IOException {
folder.delete();
assertFalse(folder.getRoot().exists());
}
+
+ private static final String GET_ROOT_DUMMY= "dummy-getRoot";
+
+ private static final String NEW_FILE_DUMMY= "dummy-newFile";
+
+ private static final String NEW_FOLDER_DUMMY= "dummy-newFolder";
+
+ public static class IncorrectUsage {
+ public TemporaryFolder folder= new TemporaryFolder();
+
+ @Test
+ public void testGetRoot() throws IOException {
+ new File(folder.getRoot(), GET_ROOT_DUMMY).createNewFile();
+ }
+
+ @Test
+ public void testNewFile() throws IOException {
+ folder.newFile(NEW_FILE_DUMMY);
+ }
+
+ @Test
+ public void testNewFolder() throws IOException {
+ folder.newFolder(NEW_FOLDER_DUMMY);
+ }
+ }
+
+ @Test
+ public void incorrectUsageWithoutApplyingTheRuleShouldNotPolluteTheCurrentWorkingDirectory() {
+ assertThat(testResult(IncorrectUsage.class), failureCountIs(3));
+ assertFalse("getRoot should have failed early", new File(GET_ROOT_DUMMY).exists());
+ assertFalse("newFile should have failed early", new File(NEW_FILE_DUMMY).exists());
+ assertFalse("newFolder should have failed early", new File(NEW_FOLDER_DUMMY).exists());
+ }
+
+ @After
+ public void cleanCurrentWorkingDirectory() {
+ new File(GET_ROOT_DUMMY).delete();
+ new File(NEW_FILE_DUMMY).delete();
+ new File(NEW_FOLDER_DUMMY).delete();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.