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 2013/09/28 21:32:07 UTC
svn commit: r1527229 - 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: Sat Sep 28 19:32:06 2013
New Revision: 1527229
URL: http://svn.apache.org/r1527229
Log:
Added new locate() methods based on FileLocator objects.
These methods use a FileLocationStrategy to resolve the passed in locator
objects. They are going to replace the existing locate() variant based on a
base path and file name. This allows hooking into the search algorithm by
providing a custom FileLocationStrategy.
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=1527229&r1=1527228&r2=1527229&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:32:06 2013
@@ -22,6 +22,7 @@ import java.net.URI;
import java.net.URL;
import java.util.Arrays;
+import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -355,6 +356,53 @@ public final class FileLocatorUtils
}
/**
+ * Locates the provided {@code FileLocator}, returning a URL for accessing
+ * the referenced file. This method uses a {@link FileLocationStrategy} to
+ * locate the file the passed in {@code FileLocator} points to. If the
+ * {@code FileLocator} contains itself a {@code FileLocationStrategy}, it is
+ * used. Otherwise, the default {@code FileLocationStrategy} is applied. The
+ * strategy is passed the locator and a {@code FileSystem}. The resulting
+ * URL is returned. If the {@code FileLocator} is <b>null</b>, result is
+ * <b>null</b>.
+ *
+ * @param locator the {@code FileLocator} to be resolved
+ * @return the URL pointing to the referenced file or <b>null</b> if the
+ * {@code FileLocator} could not be resolved
+ * @see #DEFAULT_LOCATION_STRATEGY
+ */
+ public static URL locate(FileLocator locator)
+ {
+ if (locator == null)
+ {
+ return null;
+ }
+
+ return obtainLocationStrategy(locator).locate(
+ obtainFileSystem(locator), locator);
+ }
+
+ /**
+ * Tries to locate the file referenced by the passed in {@code FileLocator}.
+ * If this fails, an exception is thrown. This method works like
+ * {@link #locate(FileLocator)}; however, in case of a failed location
+ * attempt an exception is thrown.
+ *
+ * @param locator the {@code FileLocator} to be resolved
+ * @return the URL pointing to the referenced file
+ * @throws ConfigurationException if the file cannot be resolved
+ */
+ public static URL locateOrThrow(FileLocator locator)
+ throws ConfigurationException
+ {
+ URL url = locate(locator);
+ if (url == null)
+ {
+ throw new ConfigurationException("Could not locate: " + locator);
+ }
+ return url;
+ }
+
+ /**
* Return the path without the file name, for example http://xyz.net/foo/bar.xml
* results in http://xyz.net/foo/
*
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=1527229&r1=1527228&r2=1527229&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:32:06 2013
@@ -477,4 +477,86 @@ public class TestFileLocatorUtils
FileLocatorUtils.DEFAULT_LOCATION_STRATEGY,
FileLocatorUtils.obtainLocationStrategy(null));
}
+
+ /**
+ * Tests a locate() operation with a null locator.
+ */
+ @Test
+ public void testLocateNullLocator()
+ {
+ assertNull("Wrong result", FileLocatorUtils.locate(null));
+ }
+
+ /**
+ * Tests a successful locate() operation if the passed in locator contains a
+ * strategy and a file system.
+ */
+ @Test
+ public void testLocateSuccessWithStrategyAndFileSystem()
+ throws ConfigurationException
+ {
+ FileSystem fs = EasyMock.createMock(FileSystem.class);
+ FileLocationStrategy strategy =
+ EasyMock.createMock(FileLocationStrategy.class);
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().fileSystem(fs)
+ .locationStrategy(strategy).create();
+ EasyMock.expect(strategy.locate(fs, locator)).andReturn(sourceURL);
+ EasyMock.replay(fs, strategy);
+ assertSame("Wrong URL", sourceURL,
+ FileLocatorUtils.locateOrThrow(locator));
+ EasyMock.verify(strategy);
+ }
+
+ /**
+ * Tests a successful locate() operation if the passed in locator contains a
+ * strategy, but no file system.
+ */
+ @Test
+ public void testLocateSuccessWithStrategyDefaultFileSystem()
+ throws ConfigurationException
+ {
+ FileLocationStrategy strategy =
+ EasyMock.createMock(FileLocationStrategy.class);
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().locationStrategy(strategy)
+ .create();
+ EasyMock.expect(
+ strategy.locate(FileLocatorUtils.DEFAULT_FILE_SYSTEM, locator))
+ .andReturn(sourceURL);
+ EasyMock.replay(strategy);
+ assertSame("Wrong URL", sourceURL,
+ FileLocatorUtils.locateOrThrow(locator));
+ EasyMock.verify(strategy);
+ }
+
+ /**
+ * Tests a successful locate() operation that uses defaults for location
+ * strategy and file system.
+ */
+ @Test
+ public void testLocateSuccessWithDefaults()
+ {
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().sourceURL(sourceURL).create();
+ assertSame("Wrong URL", sourceURL, FileLocatorUtils.locate(locator));
+ }
+
+ /**
+ * Tests whether an exception is thrown for a failed locate() operation.
+ */
+ @Test(expected = ConfigurationException.class)
+ public void testLocateOrThrowFailed() throws ConfigurationException
+ {
+ FileLocationStrategy strategy =
+ EasyMock.createMock(FileLocationStrategy.class);
+ EasyMock.expect(
+ strategy.locate(EasyMock.anyObject(FileSystem.class),
+ EasyMock.anyObject(FileLocator.class))).andReturn(null);
+ EasyMock.replay(strategy);
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().locationStrategy(strategy)
+ .create();
+ FileLocatorUtils.locateOrThrow(locator);
+ }
}