Skip to content

Commit

Permalink
Fixes junit-team#278: TemporaryFolder creates files in the current wo…
Browse files Browse the repository at this point in the history
…rking directory if applying the rule fails
  • Loading branch information
luontola committed Aug 8, 2011
1 parent fea0643 commit 70fe94d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions acknowledgements.txt
Expand Up @@ -110,3 +110,6 @@


2011 July 16 2011 July 16
Rob Dawson: Submitted a patch that makes Results serlializable. 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).
11 changes: 7 additions & 4 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Expand Up @@ -51,7 +51,7 @@ public void create() throws IOException {
* Returns a new fresh file with the given name under the temporary folder. * Returns a new fresh file with the given name under the temporary folder.
*/ */
public File newFile(String fileName) throws IOException { public File newFile(String fileName) throws IOException {
File file= new File(folder, fileName); File file= new File(getRoot(), fileName);
file.createNewFile(); file.createNewFile();
return file; return file;
} }
Expand All @@ -60,7 +60,7 @@ public File newFile(String fileName) throws IOException {
* Returns a new fresh folder with the given name under the temporary folder. * Returns a new fresh folder with the given name under the temporary folder.
*/ */
public File newFolder(String folderName) { public File newFolder(String folderName) {
File file= new File(folder, folderName); File file= new File(getRoot(), folderName);
file.mkdir(); file.mkdir();
return file; return file;
} }
Expand All @@ -69,12 +69,15 @@ public File newFolder(String folderName) {
* @return the location of this temporary folder. * @return the location of this temporary folder.
*/ */
public File getRoot() { public File getRoot() {
if (folder == null) {
throw new IllegalStateException("the temporary folder has not yet been created");
}
return folder; return folder;
} }


/** /**
* Delete all files and folders under the temporary 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} * by the {@link Rule}
*/ */
public void delete() { public void delete() {
Expand All @@ -88,4 +91,4 @@ private void recursiveDelete(File file) {
recursiveDelete(each); recursiveDelete(each);
file.delete(); file.delete();
} }
} }
Expand Up @@ -4,11 +4,13 @@
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult; 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 static org.junit.experimental.results.ResultMatchers.isSuccessful;


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;


import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -68,4 +70,44 @@ public void recursiveDeleteFolderWithZeroElements() throws IOException {
folder.delete(); folder.delete();
assertFalse(folder.getRoot().exists()); 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();
}
} }

0 comments on commit 70fe94d

Please sign in to comment.