Skip to content

Commit

Permalink
Cleanup TemporaryFolderUsageTest and add it to AllTests suite
Browse files Browse the repository at this point in the history
  • Loading branch information
vyazelenko committed Oct 18, 2011
1 parent 2d3090b commit 6f8a359
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/test/java/org/junit/tests/AllTests.java
Expand Up @@ -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;
Expand Down Expand Up @@ -148,6 +149,7 @@
ClassRulesTest.class,
ExpectedExceptionRuleTest.class,
TempFolderRuleTest.class,
TemporaryFolderUsageTest.class,
ExternalResourceRuleTest.class,
VerifierRuleTest.class,
CategoryTest.class,
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvok
@Test
public void createInitializesRootFolder() throws IOException {
tempFolder.create();
assertFileExists("Root folder", tempFolder.getRoot());
assertFileExists(tempFolder.getRoot());
}

@Test
Expand All @@ -72,25 +72,15 @@ public void deleteShouldDoNothingIfRootFolderWasNotInitialized() {
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));
assertFileDoesNotExist(tempFolder.getRoot());
}

@Test
public void newRandomFileIsCreatedUnderRootFolder() throws IOException {
tempFolder.create();

File f= tempFolder.newFile();
assertFileExists("Random file", f);
assertFileExists(f);
assertFileCreatedUnderRootFolder("Random file", f);
}

Expand All @@ -101,27 +91,17 @@ public void newNamedFileIsCreatedUnderRootFolder() throws IOException {

File f= tempFolder.newFile(fileName);

assertFileExists("Named file", f);
assertFileExists(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);
assertFileExists(f);
assertFileCreatedUnderRootFolder("Random folder", f);
}

Expand All @@ -130,13 +110,35 @@ 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(),
assertFileExists(f);
assertParentFolderForFileIs(f, new File(tempFolder.getRoot(),
"top/middle"));
assertParentFolderForFileIs("middle", f.getParentFile(), new File(
tempFolder.getRoot(), "top"));
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 6f8a359

Please sign in to comment.