Author: oheger
Date: Sun Sep 22 17:48:22 2013
New Revision: 1525397

URL: http://svn.apache.org/r1525397
Log:
Added methods for file to URL conversions to FileLocatorUtils.

When locating files it is often necessary to convert a File object to a URL.
The new methods simplify this task by already handling exceptions and returning
null in this case. This is the result returned by location strategies if a file
could not be resolved.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java?rev=1525397&r1=1525396&r2=1525397&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
 Sun Sep 22 17:48:22 2013
@@ -18,6 +18,7 @@ package org.apache.commons.configuration
 
 import java.io.File;
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URL;
 
 import org.apache.commons.configuration.ConfigurationUtils;
@@ -450,6 +451,37 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Tries to convert the specified URI to a URL. If this causes an 
exception,
+     * result is <b>null</b>.
+     *
+     * @param uri the URI to be converted
+     * @return the resulting URL or <b>null</b>
+     */
+    static URL convertURIToURL(URI uri)
+    {
+        try
+        {
+            return uri.toURL();
+        }
+        catch (MalformedURLException e)
+        {
+            return null;
+        }
+    }
+
+    /**
+     * Tries to convert the specified file to a URL. If this causes an
+     * exception, result is <b>null</b>.
+     *
+     * @param file the file to be converted
+     * @return the resulting URL or <b>null</b>
+     */
+    static URL convertFileToURL(File file)
+    {
+        return convertURIToURL(file.toURI());
+    }
+
+    /**
      * Tries to find a resource with the given name in the classpath.
      *
      * @param resourceName the name of the resource

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java?rev=1525397&r1=1525396&r2=1525397&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
 Sun Sep 22 17:48:22 2013
@@ -25,6 +25,8 @@ import static org.junit.Assert.assertTru
 
 import java.io.File;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 
 import org.apache.commons.configuration.ConfigurationAssert;
@@ -378,4 +380,26 @@ public class TestFileLocatorUtils
         assertFalse("Wrong result",
                 FileLocatorUtils.isFullyInitialized(locator));
     }
+
+    /**
+     * Tests whether exceptions are handled when converting a URI to a URL.
+     */
+    @Test
+    public void testConvertToURIException() throws URISyntaxException
+    {
+        URI uri = new URI("test://test/path/file.tst");
+        assertNull("Got a URL", FileLocatorUtils.convertURIToURL(uri));
+    }
+
+    /**
+     * Tests a successful conversion from a file to a URL.
+     */
+    @Test
+    public void testConvertFileToURL() throws ConfigurationException
+    {
+        File file = ConfigurationAssert.getTestFile(FILE_NAME);
+        FileHandler handler = new FileHandler();
+        handler.setURL(FileLocatorUtils.convertFileToURL(file));
+        checkTestConfiguration(handler);
+    }
 }


Reply via email to