Author: oheger
Date: Mon Sep 16 20:25:18 2013
New Revision: 1523797

URL: http://svn.apache.org/r1523797
Log:
Added a method for checking whether a FileLocator points to a file.

This is certainly a useful information. It is also required by FileHandler's
load() and save() methods to verify that sufficient information is available.

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=1523797&r1=1523796&r2=1523797&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
 Mon Sep 16 20:25:18 2013
@@ -134,6 +134,22 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Checks whether the specified {@code FileLocator} contains enough
+     * information to locate a file. This is the case if a file name or a URL 
is
+     * defined. If the passed in {@code FileLocator} is <b>null</b>, result is
+     * <b>false</b>.
+     *
+     * @param locator the {@code FileLocator} to check
+     * @return a flag whether a file location is defined by this
+     *         {@code FileLocator}
+     */
+    public static boolean isLocationDefined(FileLocator locator)
+    {
+        return (locator != null)
+                && (locator.getFileName() != null || locator.getSourceURL() != 
null);
+    }
+
+    /**
      * Return the location of the specified resource by searching the user home
      * directory, the current classpath and the system classpath.
      *

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=1523797&r1=1523796&r2=1523797&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
 Mon Sep 16 20:25:18 2013
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.net.MalformedURLException;
@@ -356,4 +357,51 @@ public class TestFileLocatorUtils
         assertSame("Wrong file system", fs,
                 FileLocatorUtils.obtainFileSystem(locator));
     }
+
+    /**
+     * Tests whether isLocationDefined() can handle null input.
+     */
+    @Test
+    public void testIsLocationDefinedNull()
+    {
+        assertFalse("Wrong result", FileLocatorUtils.isLocationDefined(null));
+    }
+
+    /**
+     * Tests isLocationDefined() if no location is defined.
+     */
+    @Test
+    public void testIsLocationDefinedFalse()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().encoding(ENCODING)
+                        .basePath(BASE_PATH)
+                        .fileSystem(FileLocatorUtils.DEFAULT_FILE_SYSTEM)
+                        .create();
+        assertFalse("Wrong result", 
FileLocatorUtils.isLocationDefined(locator));
+    }
+
+    /**
+     * Tests isLocationDefined() if a file name is set.
+     */
+    @Test
+    public void testIsLocationDefinedFileName()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
+        assertTrue("Wrong result", 
FileLocatorUtils.isLocationDefined(locator));
+    }
+
+    /**
+     * Tests isLocationDefined() if a URL is set.
+     */
+    @Test
+    public void testIsLocationDefinedURL()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator()
+                        .sourceURL(ConfigurationAssert.getTestURL("test.xml"))
+                        .create();
+        assertTrue("Wrong result", 
FileLocatorUtils.isLocationDefined(locator));
+    }
 }


Reply via email to