summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'qpid/java/common/src/test')
-rw-r--r--qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java50
1 files changed, 50 insertions, 0 deletions
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;
+ }
}