Skip to content

Commit

Permalink
Merge pull request junit-team#454 from matthewfarwell/fix-441-recursi…
Browse files Browse the repository at this point in the history
…ve-temp-folders

Fixes junit-team#441 fix for issue 283 (recursive temp folders) caused incompatibility
  • Loading branch information
kcooney committed Jun 23, 2012
2 parents e8b91fa + 67e43ab commit 28938e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/junit/rules/TemporaryFolder.java
Expand Up @@ -52,7 +52,7 @@ protected void after() {
* for testing purposes only. Do not use.
*/
public void create() throws IOException {
folder = createTemporaryFolderIn(parentFolder);
folder= createTemporaryFolderIn(parentFolder);
}

/**
Expand All @@ -77,6 +77,14 @@ public File newFile() throws IOException {
* Returns a new fresh folder with the given name under the temporary
* folder.
*/
public File newFolder(String folder) {
return newFolder(new String[]{folder});
}

/**
* Returns a new fresh folder with the given name(s) under the temporary
* folder.
*/
public File newFolder(String... folderNames) {
File file= getRoot();
for (String folderName : folderNames) {
Expand Down
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.junit.After;
Expand Down Expand Up @@ -43,14 +44,30 @@ public static class CreatesSubFolder {
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingTempFolder() throws IOException {
public void testUsingTempFolderStringReflection() throws Exception {
String subfolder = "subfolder";
String filename = "a.txt";
// force usage of folder.newFolder(String),
// check is available and works, to avoid a potential NoSuchMethodError with non-recompiled code.
Method method = folder.getClass().getMethod("newFolder", new Class<?>[] {String.class});
createdFiles[0]= (File) method.invoke(folder, subfolder);
new File(createdFiles[0], filename).createNewFile();

File expectedFile = new File(folder.getRoot(), join(subfolder, filename));

assertTrue(expectedFile.exists());
}

@Test
public void testUsingTempFolderString() throws IOException {
String subfolder = "subfolder";
String filename = "a.txt";
// this uses newFolder(String), ensure that a single String works
createdFiles[0]= folder.newFolder(subfolder);
new File(createdFiles[0], filename).createNewFile();

File expectedFile = new File(folder.getRoot(), join(subfolder, filename));

assertTrue(expectedFile.exists());
}

Expand Down

0 comments on commit 28938e9

Please sign in to comment.