Permalink
Browse files

Fixes for issue #342

  • Loading branch information...
1 parent 982a750 commit 2d3090bf96357c0d9387a4e8f87525f776e52799 unknown committed Oct 17, 2011
View
33 src/main/java/org/junit/rules/TemporaryFolder.java
@@ -39,10 +39,10 @@ protected void after() {
// testing purposes only
/**
- * for testing purposes only. Do not use.
+ * for testing purposes only. Do not use.
*/
public void create() throws IOException {
- folder= newFolder();
+ folder= createTemporaryFolderIn(null);
}
/**
@@ -58,27 +58,31 @@ public File newFile(String fileName) throws IOException {
* Returns a new fresh file with a random name under the temporary folder.
*/
public File newFile() throws IOException {
- return File.createTempFile("junit", null, folder);
+ return File.createTempFile("junit", null, getRoot());
}
/**
- * 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... folderNames) {
- File file = getRoot();
+ File file= getRoot();
for (String folderName : folderNames) {
- file = new File(file, folderName);
+ file= new File(file, folderName);
file.mkdir();
}
return file;
}
/**
- * Returns a new fresh folder with a random name under the temporary
- * folder.
+ * Returns a new fresh folder with a random name under the temporary folder.
*/
public File newFolder() throws IOException {
- File createdFolder= File.createTempFile("junit", "", folder);
+ return createTemporaryFolderIn(getRoot());
+ }
+
+ private File createTemporaryFolderIn(File parentFolder) throws IOException {
+ File createdFolder= File.createTempFile("junit", "", parentFolder);
createdFolder.delete();
createdFolder.mkdir();
return createdFolder;
@@ -89,18 +93,19 @@ public File newFolder() throws IOException {
*/
public File getRoot() {
if (folder == null) {
- throw new IllegalStateException("the temporary folder has not yet been created");
+ 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
- * by the {@link Rule}
+ * Delete all files and folders under the temporary folder. Usually not
+ * called directly, since it is automatically applied by the {@link Rule}
*/
public void delete() {
- recursiveDelete(folder);
+ if (folder != null)
+ recursiveDelete(folder);
}
private void recursiveDelete(File file) {
View
142 src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java
@@ -0,0 +1,142 @@
+package org.junit.tests.experimental.rules;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * <tt>TemporaryFolderUsageTest</tt> provides tests for API usage correctness
+ * and ensure implementation symmetry of public methods against a root folder.
+ */
+public class TemporaryFolderUsageTest {
+
+ private TemporaryFolder tempFolder;
+
+ @Before
+ public void setUp() {
+ tempFolder= new TemporaryFolder();
+ }
+
+ @After
+ public void tearDown() {
+ tempFolder.delete();
+ }
+
+ @Test(expected= IllegalStateException.class)
+ public void getRootShouldThrowIllegalStateExceptionIfCreateWasNotInvoked() {
+ new TemporaryFolder().getRoot();
+ }
+
+ @Test(expected= IllegalStateException.class)
+ public void newFileThrowsIllegalStateExceptionIfCreateWasNotInvoked()
+ throws IOException {
+ new TemporaryFolder().newFile();
+ }
+
+ @Test(expected= IllegalStateException.class)
+ public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked()
+ throws IOException {
+ new TemporaryFolder().newFile("MyFile.txt");
+ }
+
+ @Test(expected= IllegalStateException.class)
+ public void newFolderThrowsIllegalStateExceptionIfCreateWasNotInvoked()
+ throws IOException {
+ new TemporaryFolder().newFolder();
+ }
+
+ @Test(expected= IllegalStateException.class)
+ public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvoked() {
+ new TemporaryFolder().newFolder("level1", "leve2", "leve3");
+ }
+
+ @Test
+ public void createInitializesRootFolder() throws IOException {
+ tempFolder.create();
+ assertFileExists("Root folder", tempFolder.getRoot());
+ }
+
+ @Test
+ public void deleteShouldDoNothingIfRootFolderWasNotInitialized() {
+ tempFolder.delete();
+ }
+
+ @Test
+ public void deleteRemovesRootFolder() throws IOException {
+ tempFolder.create();
+ tempFolder.delete();
+ assertFileDoesNotExists("Root folder", tempFolder.getRoot());
+ }
+
+ private void assertFileDoesNotExists(String msg, File file) {
+ assertThat(msg + ": is null", file, is(notNullValue()));
+ assertThat(msg + ": still exists", file.exists(), is(false));
+ }
+
+ private void assertFileExists(String msg, File file) {
+ assertThat(msg + ": is null", file, is(notNullValue()));
+ assertThat(msg + ": does not exist", file.exists(), is(true));
+ }
+
+ @Test
+ public void newRandomFileIsCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFile();
+ assertFileExists("Random file", f);
+ assertFileCreatedUnderRootFolder("Random file", f);
+ }
+
+ @Test
+ public void newNamedFileIsCreatedUnderRootFolder() throws IOException {
+ final String fileName= "SampleFile.txt";
+ tempFolder.create();
+
+ File f= tempFolder.newFile(fileName);
+
+ assertFileExists("Named file", f);
+ assertFileCreatedUnderRootFolder("Named file", f);
+ assertThat("file name", f.getName(), equalTo(fileName));
+ }
+
+ private void assertFileCreatedUnderRootFolder(String msg, File f) {
+ assertParentFolderForFileIs(msg, f, tempFolder.getRoot());
+ }
+
+ private void assertParentFolderForFileIs(String msg, File f,
+ File parentFolder) {
+ assertThat(msg + ": not under root", f.getParentFile(),
+ is(parentFolder));
+ }
+
+ @Test
+ public void newRandomFolderIsCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFolder();
+ assertFileExists("Random folder", f);
+ assertFileCreatedUnderRootFolder("Random folder", f);
+ }
+
+ @Test
+ public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFolder("top", "middle", "bottom");
+ assertFileExists("Nested folder", f);
+ assertParentFolderForFileIs("bottom", f, new File(tempFolder.getRoot(),
+ "top/middle"));
+ assertParentFolderForFileIs("middle", f.getParentFile(), new File(
+ tempFolder.getRoot(), "top"));
+ assertFileCreatedUnderRootFolder("top", f.getParentFile()
+ .getParentFile());
+ }
+
+}

0 comments on commit 2d3090b

Please sign in to comment.