diff options
Diffstat (limited to 'qpid/java/common/src')
| -rw-r--r-- | qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java | 13 | ||||
| -rw-r--r-- | qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java | 50 |
2 files changed, 63 insertions, 0 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java index 2d3e321812..7362099070 100644 --- a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java +++ b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java @@ -220,6 +220,19 @@ public class FileUtils public static void copyCheckedEx(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); + copy(in, dst); + } + + /** + * Copies the specified InputStream to the specified destination file. If the destination file does not exist, + * it is created. + * + * @param in The InputStream + * @param dst The destination file name. + * @throws IOException + */ + public static void copy(InputStream in, File dst) throws IOException + { try { if (!dst.exists()) diff --git a/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java b/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java index 056d11faaa..d77049c780 100644 --- a/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java +++ b/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java @@ -21,6 +21,10 @@ package org.apache.qpid.test.utils; import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.TestCase; import org.apache.qpid.util.FileUtils; @@ -30,6 +34,7 @@ import org.apache.qpid.util.FileUtils; public class TestFileUtils { private static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir"); + private static final String SUFFIX = "tmp"; /** * Create and return a temporary directory that will be deleted on exit. @@ -60,4 +65,49 @@ public class TestFileUtils return testDir; } + + public static File createTempFile(TestCase testcase) + { + return createTempFile(testcase, SUFFIX); + } + + public static File createTempFile(TestCase testcase, String suffix) + { + String prefix = testcase.getClass().getSimpleName() + "-" + testcase.getName(); + + File tmpFile; + try + { + tmpFile = File.createTempFile(prefix, suffix); + tmpFile.deleteOnExit(); + } + catch (IOException e) + { + throw new RuntimeException("Cannot create temporary file with prefix " + prefix + " and suffix " + SUFFIX, e); + } + + return tmpFile; + } + + /** + * Creates a temporary file from the resource name given, using the resource name as the file suffix. + * + * This is required because the tests use the jar files as their class path. + */ + public static File createTempFileFromResource(TestCase testCase, String resourceName) + { + File dst = createTempFile(testCase, resourceName); + InputStream in = testCase.getClass().getResourceAsStream(resourceName); + try + { + FileUtils.copy(in, dst); + } + catch (Exception e) + { + throw new RuntimeException("Cannot copy resource " + resourceName + + " to temp file " + dst.getAbsolutePath(), e); + } + dst.deleteOnExit(); + return dst; + } } |
