From 67e43ab827da24baa6477e33e4e883a36dfcf645 Mon Sep 17 00:00:00 2001 From: Matthew Farwell Date: Tue, 19 Jun 2012 13:58:32 +0200 Subject: [PATCH] Fixes #441 fix for issue 283 (recursive temp folders) caused incompatibility Put back in the method folder.newFolder(String) that was removed in #283. Tests that the method is there using reflection. --- .../java/org/junit/rules/TemporaryFolder.java | 10 +++++++- .../rules/TempFolderRuleTest.java | 23 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java index 1e7d3941fc03..3b49fc2ef6d5 100644 --- a/src/main/java/org/junit/rules/TemporaryFolder.java +++ b/src/main/java/org/junit/rules/TemporaryFolder.java @@ -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); } /** @@ -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) { diff --git a/src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java b/src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java index 3700b6b90212..31fcc15c221e 100644 --- a/src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java +++ b/src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java @@ -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; @@ -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()); }