Skip to content

Commit

Permalink
Merge pull request junit-team#428 from shs96c/master
Browse files Browse the repository at this point in the history
Allow users to define the parent directory used by the TemporaryFolder
  • Loading branch information
dsaff committed May 22, 2012
2 parents 1e391af + ecf0ea1 commit c77cf4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@
* </pre>
*/
public class TemporaryFolder extends ExternalResource {
private final File parentFolder;
private File folder;

public TemporaryFolder() {
this(null);
}

public TemporaryFolder(File parentFolder) {
this.parentFolder = parentFolder;
}

@Override
protected void before() throws Throwable {
create();
Expand All @@ -42,7 +51,7 @@ protected void after() {
* for testing purposes only. Do not use.
*/
public void create() throws IOException {
folder= createTemporaryFolderIn(null);
folder = createTemporaryFolderIn(parentFolder);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,24 @@ public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
.getParentFile());
}

private void assertFileDoesNotExist(File file) {
@Test
public void canSetTheBaseFileForATemporaryFolder() throws IOException {
File tempDir = createTemporaryFolder();

TemporaryFolder folder = new TemporaryFolder(tempDir);
folder.create();

assertThat(tempDir, is(folder.getRoot().getParentFile()));
}

private File createTemporaryFolder() throws IOException {
File tempDir = File.createTempFile("junit", "tempFolder");
assertTrue("Unable to delete temporary file", tempDir.delete());
assertTrue("Unable to create temp directory", tempDir.mkdir());
return tempDir;
}

private void assertFileDoesNotExist(File file) {
checkFileExists("exists", file, false);
}

Expand Down

0 comments on commit c77cf4d

Please sign in to comment.