You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/06/29 21:44:36 UTC

svn commit: r1606587 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/io/FileLocatorUtils.java test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java

Author: oheger
Date: Sun Jun 29 19:44:35 2014
New Revision: 1606587

URL: http://svn.apache.org/r1606587
Log:
A FileLocator object can now be stored in a map and created from there.

The goal is to simplify the definition of parameters for file-based
configurations in typical IoC frameworks.

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=1606587&r1=1606586&r2=1606587&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 Jun 29 19:44:35 2014
@@ -21,6 +21,7 @@ import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.util.Arrays;
+import java.util.Map;
 
 import org.apache.commons.configuration.ex.ConfigurationException;
 import org.apache.commons.lang3.ObjectUtils;
@@ -92,6 +93,24 @@ public final class FileLocatorUtils
     /** The logger.*/
     private static final Log LOG = LogFactory.getLog(FileLocatorUtils.class);
 
+    /** Property key for the base path. */
+    private static final String PROP_BASE_PATH = "basePath";
+
+    /** Property key for the encoding. */
+    private static final String PROP_ENCODING = "encoding";
+
+    /** Property key for the file name. */
+    private static final String PROP_FILE_NAME = "fileName";
+
+    /** Property key for the file system. */
+    private static final String PROP_FILE_SYSTEM = "fileSystem";
+
+    /** Property key for the location strategy. */
+    private static final String PROP_STRATEGY = "locationStrategy";
+
+    /** Property key for the source URL. */
+    private static final String PROP_SOURCE_URL = "sourceURL";
+
     /**
      * Private constructor so that no instances can be created.
      */
@@ -150,6 +169,61 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Creates a new {@code FileLocator} object with the properties defined in
+     * the given map. The map must be conform to the structure generated by the
+     * {@link #put(FileLocator, Map)} method; unexpected data can cause
+     * {@code ClassCastException} exceptions. The map can be <b>null</b>, then
+     * an uninitialized {@code FileLocator} is returned.
+     *
+     * @param map the map
+     * @return the new {@code FileLocator}
+     * @throws ClassCastException if the map contains invalid data
+     */
+    public static FileLocator fromMap(Map<String, Object> map)
+    {
+        FileLocator.FileLocatorBuilder builder = fileLocator();
+        if (map != null)
+        {
+            builder.basePath((String) map.get(PROP_BASE_PATH))
+                    .encoding((String) map.get(PROP_ENCODING))
+                    .fileName((String) map.get(PROP_FILE_NAME))
+                    .fileSystem((FileSystem) map.get(PROP_FILE_SYSTEM))
+                    .locationStrategy(
+                            (FileLocationStrategy) map.get(PROP_STRATEGY))
+                    .sourceURL((URL) map.get(PROP_SOURCE_URL));
+        }
+        return builder.create();
+    }
+
+    /**
+     * Stores the specified {@code FileLocator} in the given map. With the
+     * {@link #fromMap(Map)} method a new {@code FileLocator} with the same
+     * properties as the original one can be created.
+     *
+     * @param locator the {@code FileLocator} to be stored
+     * @param map the map in which to store the {@code FileLocator} (must not be
+     *        <b>null</b>)
+     * @throws IllegalArgumentException if the map is <b>null</b>
+     */
+    public static void put(FileLocator locator, Map<String, Object> map)
+    {
+        if (map == null)
+        {
+            throw new IllegalArgumentException("Map must not be null!");
+        }
+
+        if (locator != null)
+        {
+            map.put(PROP_BASE_PATH, locator.getBasePath());
+            map.put(PROP_ENCODING, locator.getEncoding());
+            map.put(PROP_FILE_NAME, locator.getFileName());
+            map.put(PROP_FILE_SYSTEM, locator.getFileSystem());
+            map.put(PROP_SOURCE_URL, locator.getSourceURL());
+            map.put(PROP_STRATEGY, locator.getLocationStrategy());
+        }
+    }
+
+    /**
      * 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

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=1606587&r1=1606586&r2=1606587&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 Jun 29 19:44:35 2014
@@ -28,7 +28,9 @@ import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.commons.configuration.ConfigurationAssert;
 import org.apache.commons.configuration.XMLConfiguration;
@@ -558,4 +560,56 @@ public class TestFileLocatorUtils
                         .create();
         FileLocatorUtils.locateOrThrow(locator);
     }
+
+    /**
+     * Tests whether a file locator can be stored in a map and read again from
+     * there.
+     */
+    @Test
+    public void testStoreFileLocatorInMap()
+    {
+        FileLocationStrategy strategy =
+                EasyMock.createMock(FileLocationStrategy.class);
+        EasyMock.replay(strategy);
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().basePath(BASE_PATH)
+                        .encoding(ENCODING).fileName(FILE_NAME)
+                        .fileSystem(fileSystem).locationStrategy(strategy)
+                        .sourceURL(sourceURL).create();
+        Map<String, Object> map = new HashMap<String, Object>();
+        FileLocatorUtils.put(locator, map);
+        FileLocator locator2 = FileLocatorUtils.fromMap(map);
+        assertEquals("Different locators", locator, locator2);
+    }
+
+    /**
+     * Tests whether put() deals with a null locator.
+     */
+    @Test
+    public void testPutNoLocator()
+    {
+        Map<String, Object> map = new HashMap<String, Object>();
+        FileLocatorUtils.put(null, map);
+        assertTrue("Got properties", map.isEmpty());
+    }
+
+    /**
+     * Tries to call put() without a map.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testPutNoMap()
+    {
+        FileLocatorUtils.put(FileLocatorUtils.fileLocator().create(), null);
+    }
+
+    /**
+     * Tests whether fromMap() can handle a null map.
+     */
+    @Test
+    public void testFromMapNoMap()
+    {
+        FileLocator fileLocator = FileLocatorUtils.fromMap(null);
+        assertEquals("Locator is initialized", FileLocatorUtils.fileLocator()
+                .create(), fileLocator);
+    }
 }