Skip to content

Commit

Permalink
Merge pull request junit-team#419 from simplyarjen/master
Browse files Browse the repository at this point in the history
Make TemporaryFolder.createFile fail if the file already exists (Fixes junit-team#413)
  • Loading branch information
dsaff committed Apr 10, 2012
2 parents c4279e4 + 5baa714 commit 3624da3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/org/junit/rules/TemporaryFolder.java
Expand Up @@ -50,7 +50,9 @@ public void create() throws IOException {
*/ */
public File newFile(String fileName) throws IOException { public File newFile(String fileName) throws IOException {
File file= new File(getRoot(), fileName); File file= new File(getRoot(), fileName);
file.createNewFile(); if (!file.createNewFile())
throw new IllegalStateException(
"a file with the name \'" + fileName + "\' already exists in the test folder");
return file; return file;
} }


Expand Down
Expand Up @@ -158,6 +158,28 @@ public void recursiveDeleteFolderWithZeroElements() throws IOException {
assertFalse(folder.getRoot().exists()); assertFalse(folder.getRoot().exists());
} }


public static class NameClashes {
@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void fileWithFileClash() throws IOException {
folder.newFile("something.txt");
folder.newFile("something.txt");
}

@Test
public void fileWithFolderTest() throws IOException {
folder.newFolder("dummy");
folder.newFile("dummy");
}
}

@Test
public void nameClashesResultInTestFailures() {
assertThat(testResult(NameClashes.class), failureCountIs(2));
}

private static final String GET_ROOT_DUMMY= "dummy-getRoot"; private static final String GET_ROOT_DUMMY= "dummy-getRoot";


private static final String NEW_FILE_DUMMY= "dummy-newFile"; private static final String NEW_FILE_DUMMY= "dummy-newFile";
Expand Down

0 comments on commit 3624da3

Please sign in to comment.