summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/test
diff options
context:
space:
mode:
authorRobert Gemmell <robbie@apache.org>2011-07-21 13:18:51 +0000
committerRobert Gemmell <robbie@apache.org>2011-07-21 13:18:51 +0000
commit1f55f866f3d1f377031656440dd4fd3326b4638b (patch)
tree355f24b32b95d2205943c0a153ea6aaf7304c301 /qpid/java/common/src/test
parenta8e7aeb469037dfaf8b2bb0bb6ca75d4181a4b1c (diff)
downloadqpid-python-1f55f866f3d1f377031656440dd4fd3326b4638b.tar.gz
QPID-3367: FileUtils improvements. #openFileOrDefaultResource now tries the override filename in the classpath too, before falling back to the default.
Applied patch from Keith Wall <keith.wall@gmail.com> git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1149165 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/common/src/test')
-rw-r--r--qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java82
-rw-r--r--qpid/java/common/src/test/java/org/apache/qpid/util/default.properties2
-rw-r--r--qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties2
3 files changed, 86 insertions, 0 deletions
diff --git a/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java b/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
index 7eba5f092e..d6767eb9c0 100644
--- a/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
+++ b/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
@@ -27,7 +27,9 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
import java.util.List;
+import java.util.Properties;
public class FileUtilsTest extends TestCase
{
@@ -182,6 +184,20 @@ public class FileUtilsTest extends TestCase
}
}
+
+ /**
+ * Helper method to create a temporary file with test content.
+ *
+ * @param test_data The data to store in the file
+ *
+ * @return The File reference
+ */
+ private File createTestFileInTmpDir(final String testData) throws Exception
+ {
+ final File tmpFile = File.createTempFile("test", "tmp");
+
+ return createTestFile(tmpFile.getCanonicalPath(), testData);
+ }
/**
* Helper method to create a test file with a string content
*
@@ -302,8 +318,74 @@ public class FileUtilsTest extends TestCase
// expected path
}
}
+
+ /**
+ * Tests that openFileOrDefaultResource can open a file on the filesystem.
+ *
+ */
+ public void testOpenFileOrDefaultResourceOpensFileOnFileSystem() throws Exception
+ {
+ final File testFile = createTestFileInTmpDir("src=tmpfile");
+ final String filenameOnFilesystem = testFile.getCanonicalPath();
+ final String defaultResource = "org/apache/qpid/util/default.properties";
+
+
+ final InputStream is = FileUtils.openFileOrDefaultResource(filenameOnFilesystem, defaultResource, this.getClass().getClassLoader());
+ assertNotNull("Stream must not be null", is);
+ final Properties p = new Properties();
+ p.load(is);
+ assertEquals("tmpfile", p.getProperty("src"));
+ }
/**
+ * Tests that openFileOrDefaultResource can open a file on the classpath.
+ *
+ */
+ public void testOpenFileOrDefaultResourceOpensFileOnClasspath() throws Exception
+ {
+ final String mydefaultsResource = "org/apache/qpid/util/mydefaults.properties";
+ final String defaultResource = "org/apache/qpid/util/default.properties";
+
+
+ final InputStream is = FileUtils.openFileOrDefaultResource(mydefaultsResource, defaultResource, this.getClass().getClassLoader());
+ assertNotNull("Stream must not be null", is);
+ final Properties p = new Properties();
+ p.load(is);
+ assertEquals("mydefaults", p.getProperty("src"));
+ }
+
+ /**
+ * Tests that openFileOrDefaultResource returns the default resource when file cannot be found.
+ */
+ public void testOpenFileOrDefaultResourceOpensDefaultResource() throws Exception
+ {
+ final File fileThatDoesNotExist = new File("/does/not/exist.properties");
+ assertFalse("Test must not exist", fileThatDoesNotExist.exists());
+
+ final String defaultResource = "org/apache/qpid/util/default.properties";
+
+ final InputStream is = FileUtils.openFileOrDefaultResource(fileThatDoesNotExist.getCanonicalPath(), defaultResource, this.getClass().getClassLoader());
+ assertNotNull("Stream must not be null", is);
+ Properties p = new Properties();
+ p.load(is);
+ assertEquals("default.properties", p.getProperty("src"));
+ }
+
+ /**
+ * Tests that openFileOrDefaultResource returns null if neither the file nor
+ * the default resource can be found..
+ */
+ public void testOpenFileOrDefaultResourceReturnsNullWhenNeitherCanBeFound() throws Exception
+ {
+
+ final String mydefaultsResource = "org/apache/qpid/util/doesnotexisteiether.properties";
+ final String defaultResource = "org/apache/qpid/util/doesnotexisteiether.properties";
+
+ final InputStream is = FileUtils.openFileOrDefaultResource(mydefaultsResource, defaultResource, this.getClass().getClassLoader());
+ assertNull("Stream must be null", is);
+ }
+
+ /**
* Given two lists of File arrays ensure they are the same length and all entries in Before are in After
*
* @param filesBefore File[]
diff --git a/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties b/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
new file mode 100644
index 0000000000..cb522ea9a7
--- /dev/null
+++ b/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
@@ -0,0 +1,2 @@
+# Used by FileUtilsTests
+src=default.properties \ No newline at end of file
diff --git a/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties b/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
new file mode 100644
index 0000000000..6a49d927d0
--- /dev/null
+++ b/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
@@ -0,0 +1,2 @@
+# Used by FileUtilsTests
+src=mydefaults \ No newline at end of file