Permalink
Browse files

Merge pull request #344 from vyazelenko/master

Fixes gh-342: TemporaryFolder.newFile(String) throws IllegalStateException in junit-4.10
  • Loading branch information...
2 parents 982a750 + 6f8a359 commit a681b760978751ca4c4ccc6de616b0643d6c2b0e @dsaff dsaff committed Oct 24, 2011
@@ -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) {
@@ -32,6 +32,7 @@
import org.junit.tests.experimental.rules.RuleFieldValidatorTest;
import org.junit.tests.experimental.rules.RuleChainTest;
import org.junit.tests.experimental.rules.TempFolderRuleTest;
+import org.junit.tests.experimental.rules.TemporaryFolderUsageTest;
import org.junit.tests.experimental.rules.TestRuleTest;
import org.junit.tests.experimental.rules.TimeoutRuleTest;
import org.junit.tests.experimental.rules.VerifierRuleTest;
@@ -148,6 +149,7 @@
ClassRulesTest.class,
ExpectedExceptionRuleTest.class,
TempFolderRuleTest.class,
+ TemporaryFolderUsageTest.class,
ExternalResourceRuleTest.class,
VerifierRuleTest.class,
CategoryTest.class,
@@ -0,0 +1,144 @@
+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(tempFolder.getRoot());
+ }
+
+ @Test
+ public void deleteShouldDoNothingIfRootFolderWasNotInitialized() {
+ tempFolder.delete();
+ }
+
+ @Test
+ public void deleteRemovesRootFolder() throws IOException {
+ tempFolder.create();
+ tempFolder.delete();
+ assertFileDoesNotExist(tempFolder.getRoot());
+ }
+
+ @Test
+ public void newRandomFileIsCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFile();
+ assertFileExists(f);
+ assertFileCreatedUnderRootFolder("Random file", f);
+ }
+
+ @Test
+ public void newNamedFileIsCreatedUnderRootFolder() throws IOException {
+ final String fileName= "SampleFile.txt";
+ tempFolder.create();
+
+ File f= tempFolder.newFile(fileName);
+
+ assertFileExists(f);
+ assertFileCreatedUnderRootFolder("Named file", f);
+ assertThat("file name", f.getName(), equalTo(fileName));
+ }
+
+ @Test
+ public void newRandomFolderIsCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFolder();
+ assertFileExists(f);
+ assertFileCreatedUnderRootFolder("Random folder", f);
+ }
+
+ @Test
+ public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
+ tempFolder.create();
+
+ File f= tempFolder.newFolder("top", "middle", "bottom");
+ assertFileExists(f);
+ assertParentFolderForFileIs(f, new File(tempFolder.getRoot(),
+ "top/middle"));
+ assertParentFolderForFileIs(f.getParentFile(),
+ new File(tempFolder.getRoot(), "top"));
+ assertFileCreatedUnderRootFolder("top", f.getParentFile()
+ .getParentFile());
+ }
+
+ private void assertFileDoesNotExist(File file) {
+ checkFileExists("exists", file, false);
+ }
+
+ private void checkFileExists(String msg, File file, boolean exists) {
+ assertThat("File is null", file, is(notNullValue()));
+ assertThat("File '" + file.getAbsolutePath() + "' " + msg,
+ file.exists(), is(exists));
+ }
+
+ private void assertFileExists(File file) {
+ checkFileExists("does not exist", file, true);
+ }
+
+ private void assertFileCreatedUnderRootFolder(String msg, File f) {
+ assertParentFolderForFileIs(f, tempFolder.getRoot());
+ }
+
+ private void assertParentFolderForFileIs(File f, File parentFolder) {
+ assertThat("'" + f.getAbsolutePath() + "': not under root",
+ f.getParentFile(), is(parentFolder));
+ }
+}

0 comments on commit a681b76

Please sign in to comment.