Author: robbie
Date: Thu Jul 21 13:18:51 2011
New Revision: 1149165

URL: http://svn.apache.org/viewvc?rev=1149165&view=rev
Log:
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 <[email protected]>

Added:
    
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
    
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
Modified:
    
qpid/trunk/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
    
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java

Modified: 
qpid/trunk/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java?rev=1149165&r1=1149164&r2=1149165&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java 
(original)
+++ 
qpid/trunk/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java 
Thu Jul 21 13:18:51 2011
@@ -143,8 +143,9 @@ public class FileUtils
     }
 
     /**
-     * Either opens the specified filename as an input stream, or uses the 
default resource loaded using the
-     * specified class loader, if opening the file fails or no file name is 
specified.
+     * Either opens the specified filename as an input stream or either the 
filesystem or classpath,
+     * or uses the default resource loaded using the specified class loader, 
if opening the file fails
+     * or no file name is specified.
      *
      * @param filename        The name of the file to open.
      * @param defaultResource The name of the default resource on the 
classpath if the file cannot be opened.
@@ -156,28 +157,28 @@ public class FileUtils
     {
         InputStream is = null;
 
-        // Flag to indicate whether the default resource should be used. By 
default this is true, so that the default
-        // is used when opening the file fails.
-        boolean useDefault = true;
-
         // Try to open the file if one was specified.
         if (filename != null)
         {
+            // try on filesystem
             try
             {
                 is = new BufferedInputStream(new FileInputStream(new 
File(filename)));
-
-                // Clear the default flag because the file was succesfully 
opened.
-                useDefault = false;
             }
             catch (FileNotFoundException e)
             {
-                // Ignore this exception, the default will be used instead.
+                is = null;
+            }
+
+            if (is == null)
+            {
+                // failed on filesystem, so try on classpath
+                is = cl.getResourceAsStream(filename);
             }
         }
 
         // Load the default resource if a file was not specified, or if 
opening the file failed.
-        if (useDefault)
+        if (is == null)
         {
             is = cl.getResourceAsStream(defaultResource);
         }

Modified: 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java?rev=1149165&r1=1149164&r2=1149165&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
 (original)
+++ 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
 Thu Jul 21 13:18:51 2011
@@ -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 TestC
         }
     }
 
+
+    /**
+     * 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 TestC
             // 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[]

Added: 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties?rev=1149165&view=auto
==============================================================================
--- 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
 (added)
+++ 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/default.properties
 Thu Jul 21 13:18:51 2011
@@ -0,0 +1,2 @@
+# Used by FileUtilsTests
+src=default.properties
\ No newline at end of file

Added: 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties?rev=1149165&view=auto
==============================================================================
--- 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
 (added)
+++ 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/util/mydefaults.properties
 Thu Jul 21 13:18:51 2011
@@ -0,0 +1,2 @@
+# Used by FileUtilsTests
+src=mydefaults
\ No newline at end of file



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to