Author: oheger
Date: Sat Sep 28 19:30:49 2013
New Revision: 1527227

URL: http://svn.apache.org/r1527227
Log:
Added a method for extracting a location strategy from a FileLocator.

In a FileLocator the default location strategy can be overridden. The new
method always returns a valid strategy, no matter whether the locator
defines one or not.

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=1527227&r1=1527226&r2=1527227&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
 Sat Sep 28 19:30:49 2013
@@ -618,6 +618,23 @@ public final class FileLocatorUtils
     }
 
     /**
+     * Obtains a non <b>null</b> {@code FileLocationStrategy} object from the
+     * passed in {@code FileLocator}. If the {@code FileLocator} is not
+     * <b>null</b> and has a {@code FileLocationStrategy} defined, this 
strategy
+     * is returned. Otherwise, result is the default
+     * {@code FileLocationStrategy}.
+     *
+     * @param locator the {@code FileLocator}
+     * @return the {@code FileLocationStrategy} for this {@code FileLocator}
+     */
+    static FileLocationStrategy obtainLocationStrategy(FileLocator locator)
+    {
+        return (locator != null) ? ObjectUtils.defaultIfNull(
+                locator.getLocationStrategy(), DEFAULT_LOCATION_STRATEGY)
+                : DEFAULT_LOCATION_STRATEGY;
+    }
+
+    /**
      * Creates a fully initialized {@code FileLocator} based on a URL.
      *
      * @param locator the source {@code FileLocator}

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=1527227&r1=1527226&r2=1527227&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
 Sat Sep 28 19:30:49 2013
@@ -435,4 +435,46 @@ public class TestFileLocatorUtils
         assertTrue("Wrong strategy (7)",
                 it.next() instanceof ClasspathLocationStrategy);
     }
+
+    /**
+     * Tests whether a location strategy can be obtained if it is defined by 
the
+     * locator.
+     */
+    @Test
+    public void testObtainLocationStrategySetInLocator()
+    {
+        FileLocationStrategy strategy =
+                EasyMock.createMock(FileLocationStrategy.class);
+        EasyMock.replay(strategy);
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().locationStrategy(strategy)
+                        .create();
+        assertSame("Wrong strategy", strategy,
+                FileLocatorUtils.obtainLocationStrategy(locator));
+    }
+
+    /**
+     * Tests whether a location strategy can be obtained if it is not defined 
by
+     * the locator.
+     */
+    @Test
+    public void testObtainLocationStrategyNotSetInLocator()
+    {
+        FileLocator locator = FileLocatorUtils.fileLocator().create();
+        assertSame("Wrong strategy",
+                FileLocatorUtils.DEFAULT_LOCATION_STRATEGY,
+                FileLocatorUtils.obtainLocationStrategy(locator));
+    }
+
+    /**
+     * Tests whether a location strategy can be obtained if a null locator is
+     * passed.
+     */
+    @Test
+    public void testObtainLocationStrategyNullLocator()
+    {
+        assertSame("Wrong strategy",
+                FileLocatorUtils.DEFAULT_LOCATION_STRATEGY,
+                FileLocatorUtils.obtainLocationStrategy(null));
+    }
 }


Reply via email to