Author: oheger
Date: Mon Sep 16 20:26:44 2013
New Revision: 1523799

URL: http://svn.apache.org/r1523799
Log:
Added a method to FileLocatorUtils for obtaining a fully initialized 
FileLocator.

There are different ways to specify the file to be loaded depending on the
initialized fields of a FileLocator. If a file can be successfully located
based on this information, it makes sense to populate all the corresponding
member fields in a consistent way to point to this file.

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=1523799&r1=1523798&r2=1523799&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:26:44 2013
@@ -150,6 +150,50 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Returns a {@code FileLocator} object based on the passed in one whose
+     * location is fully defined. This method ensures that all components of 
the
+     * {@code FileLocator} pointing to the file are set in a consistent way. In
+     * detail it behaves as follows:
+     * <ul>
+     * <li>If the {@code FileLocator} has no location (refer to
+     * {@link #isLocationDefined(FileLocator)}), result is <b>null</b>.</li>
+     * <li>If the {@code FileLocator} has already all components set which
+     * define the file, it is returned unchanged. <em>Note:</em> It is not
+     * checked whether all components are really consistent!</li>
+     * <li>If a source URL is set, the file name and base path are determined
+     * based on this URL.</li>
+     * <li>Otherwise, an attempt to locate the URL of the file based on the
+     * information available is made. A {@code FileLocator} with the resulting
+     * information is returned; this may be incomplete if it was not possible 
to
+     * determine the URL.</li>
+     * </ul>
+     *
+     * @param locator the {@code FileLocator} to be completed
+     * @return a {@code FileLocator} with a fully initialized location if
+     *         possible
+     */
+    public static FileLocator fullyInitializedLocator(FileLocator locator)
+    {
+        if (!isLocationDefined(locator))
+        {
+            return null;
+        }
+
+        if (locator.getBasePath() != null && locator.getFileName() != null
+                && locator.getSourceURL() != null)
+        {
+            // already fully initialized
+            return locator;
+        }
+
+        if (locator.getSourceURL() != null)
+        {
+            return fullyInitializedLocatorFromURL(locator);
+        }
+        return fullyInitializedLocatorFromPathAndName(locator);
+    }
+
+    /**
      * Return the location of the specified resource by searching the user home
      * directory, the current classpath and the system classpath.
      *
@@ -471,6 +515,53 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Creates a fully initialized {@code FileLocator} based on a URL.
+     *
+     * @param locator the source {@code FileLocator}
+     * @return the fully initialized {@code FileLocator}
+     */
+    private static FileLocator fullyInitializedLocatorFromURL(
+            FileLocator locator)
+    {
+        return createFullyInitializedLocator(locator, locator.getSourceURL());
+    }
+
+    /**
+     * Creates a fully initialized {@code FileLocator} based on a base path and
+     * file name combination.
+     *
+     * @param locator the source {@code FileLocator}
+     * @return the fully initialized {@code FileLocator}
+     */
+    private static FileLocator fullyInitializedLocatorFromPathAndName(
+            FileLocator locator)
+    {
+        URL url =
+                locate(obtainFileSystem(locator), locator.getBasePath(),
+                        locator.getFileName());
+        if (url == null)
+        {
+            return locator;
+        }
+        return createFullyInitializedLocator(locator, url);
+    }
+
+    /**
+     * Creates a fully initialized {@code FileLocator} based on the specified
+     * URL.
+     *
+     * @param src the source {@code FileLocator}
+     * @param url the URL
+     * @return the fully initialized {@code FileLocator}
+     */
+    private static FileLocator createFullyInitializedLocator(FileLocator src,
+            URL url)
+    {
+        return fileLocator(src).sourceURL(url).fileName(getFileName(url))
+                .basePath(getBasePath(url)).create();
+    }
+
+    /**
      * A typical <em>builder</em> implementation for creating
      * {@code FileLocator} objects. An instance of this class is returned by 
the
      * {@code fileLocator()} method of {@code FileLocatorUtils}. It can be used

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=1523799&r1=1523798&r2=1523799&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:26:44 2013
@@ -19,6 +19,7 @@ package org.apache.commons.configuration
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
@@ -29,6 +30,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 
 import org.apache.commons.configuration.ConfigurationAssert;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.XMLConfiguration;
 import org.easymock.EasyMock;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -41,7 +44,7 @@ import org.junit.Test;
 public class TestFileLocatorUtils
 {
     /** Constant for a file name. */
-    private static final String FILE_NAME = "testFile.dat";
+    private static final String FILE_NAME = "test.xml";
 
     /** Constant for a base path. */
     private static final String BASE_PATH = "/etc/test/path/";
@@ -58,7 +61,7 @@ public class TestFileLocatorUtils
     @BeforeClass
     public static void setUpOnce() throws Exception
     {
-        sourceURL = ConfigurationAssert.getTestURL("test.xml");
+        sourceURL = ConfigurationAssert.getTestURL(FILE_NAME);
         fileSystem = EasyMock.createMock(FileSystem.class);
         EasyMock.replay(fileSystem);
     }
@@ -400,8 +403,115 @@ public class TestFileLocatorUtils
     {
         FileLocator locator =
                 FileLocatorUtils.fileLocator()
-                        .sourceURL(ConfigurationAssert.getTestURL("test.xml"))
+                        .sourceURL(ConfigurationAssert.getTestURL(FILE_NAME))
                         .create();
         assertTrue("Wrong result", 
FileLocatorUtils.isLocationDefined(locator));
     }
+
+    /**
+     * Tries to obtain a fully initialized locator if the source locator is not
+     * defined.
+     */
+    @Test
+    public void testFullyInitializedLocatorUndefined()
+    {
+        assertNull("Got a result",
+                FileLocatorUtils.fullyInitializedLocator(FileLocatorUtils
+                        .fileLocator().create()));
+    }
+
+    /**
+     * Checks whether the expected test configuration can be loaded using the
+     * specified handler.
+     *
+     * @param handler the file handler
+     * @throws ConfigurationException if an error occurs
+     */
+    private static void checkTestConfiguration(FileHandler handler)
+            throws ConfigurationException
+    {
+        XMLConfiguration config = new XMLConfiguration();
+        FileHandler h2 = new FileHandler(config, handler);
+        h2.load();
+        assertEquals("Wrong content", "value", config.getString("element"));
+    }
+
+    /**
+     * Checks whether the specified locator points to the expected test
+     * configuration file.
+     *
+     * @param locator the locator to check
+     * @throws ConfigurationException if an error occurs
+     */
+    private static void checkFullyInitializedLocator(FileLocator locator)
+            throws ConfigurationException
+    {
+        assertNotNull("No base path", locator.getBasePath());
+        assertNotNull("No file name", locator.getFileName());
+        assertNotNull("No source URL", locator.getSourceURL());
+
+        FileHandler handler = new FileHandler();
+        handler.setBasePath(locator.getBasePath());
+        handler.setFileName(locator.getFileName());
+        checkTestConfiguration(handler);
+
+        handler = new FileHandler();
+        handler.setURL(locator.getSourceURL());
+        checkTestConfiguration(handler);
+    }
+
+    /**
+     * Tests whether a fully initialized locator can be obtained if a file name
+     * is available.
+     */
+    @Test
+    public void testFullyInitializedLocatorFileName()
+            throws ConfigurationException
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
+        checkFullyInitializedLocator(FileLocatorUtils
+                .fullyInitializedLocator(locator));
+    }
+
+    /**
+     * Tests whether a fully initialized locator can be obtained if a URL is
+     * available.
+     */
+    @Test
+    public void testFullyInitializedLocatorURL() throws ConfigurationException
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().sourceURL(sourceURL).create();
+        checkFullyInitializedLocator(FileLocatorUtils
+                .fullyInitializedLocator(locator));
+    }
+
+    /**
+     * Tests fullyInitializedLocator() if the locator is already fully
+     * initialized.
+     */
+    @Test
+    public void testFullyInitializedLocatorAlreadyComplete()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().fileName(FILE_NAME).create();
+        FileLocator fullLocator =
+                FileLocatorUtils.fullyInitializedLocator(locator);
+        assertSame("Different instance", fullLocator,
+                FileLocatorUtils.fullyInitializedLocator(fullLocator));
+    }
+
+    /**
+     * Tests fullyInitializedLocator() if a locate() operation fails.
+     */
+    @Test
+    public void testFullyInitializedLocatorLocateFails()
+    {
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().fileName("non existing file")
+                        .create();
+        assertSame("Wrong result", locator,
+                FileLocatorUtils.fullyInitializedLocator(locator));
+    }
 }


Reply via email to