Author: oheger Date: Sat Sep 28 19:31:27 2013 New Revision: 1527228 URL: http://svn.apache.org/r1527228 Log: Added support for a location strategy to FileHandler.
FileHandler now provides methods for setting and querying the FileLocationStrategy of the underlying FileLocator. Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java?rev=1527228&r1=1527227&r2=1527228&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java Sat Sep 28 19:31:27 2013 @@ -557,6 +557,41 @@ public class FileHandler } /** + * Returns the {@code FileLocationStrategy} to be applied when accessing the + * associated file. This method never returns <b>null</b>. If a + * {@code FileLocationStrategy} has been set, it is returned. Otherwise, + * result is the default {@code FileLocationStrategy}. + * + * @return the {@code FileLocationStrategy} to be used + */ + public FileLocationStrategy getLocationStrategy() + { + return FileLocatorUtils.obtainLocationStrategy(getFileLocator()); + } + + /** + * Sets the {@code FileLocationStrategy} to be applied when accessing the + * associated file. The strategy is stored in the underlying + * {@link FileLocator}. The argument can be <b>null</b>; this causes the + * default {@code FileLocationStrategy} to be used. + * + * @param strategy the {@code FileLocationStrategy} + * @see FileLocatorUtils#DEFAULT_LOCATION_STRATEGY + */ + public void setLocationStrategy(final FileLocationStrategy strategy) + { + new Updater() + { + @Override + protected void updateBuilder(FileLocatorBuilder builder) + { + builder.locationStrategy(strategy); + } + + }.update(); + } + + /** * Locates the referenced file if necessary and ensures that the associated * {@link FileLocator} is fully initialized. When accessing the referenced * file the information stored in the associated {@code FileLocator} is Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java?rev=1527228&r1=1527227&r2=1527228&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java Sat Sep 28 19:31:27 2013 @@ -217,6 +217,37 @@ public class TestFileHandler } /** + * Tests whether a newly created instance uses the default location + * strategy. + */ + @Test + public void testGetLocationStrategyDefault() + { + FileHandler handler = new FileHandler(); + assertNull("Strategy in locator", handler.getFileLocator() + .getLocationStrategy()); + assertSame("Wrong default strategy", + FileLocatorUtils.DEFAULT_LOCATION_STRATEGY, + handler.getLocationStrategy()); + } + + /** + * Tests whether the location strategy can be changed. + */ + @Test + public void testSetLocationStrategy() + { + FileLocationStrategy strategy = + EasyMock.createMock(FileLocationStrategy.class); + EasyMock.replay(strategy); + FileHandler handler = new FileHandler(); + handler.setLocationStrategy(strategy); + assertSame("Wrong strategy in locator", strategy, handler + .getFileLocator().getLocationStrategy()); + assertSame("Wrong strategy", strategy, handler.getLocationStrategy()); + } + + /** * Tests whether a URL can be set. */ @Test @@ -1334,6 +1365,8 @@ public class TestFileHandler { final String encoding = "TestEncoding"; final FileSystem fileSystem = new DefaultFileSystem(); + final FileLocationStrategy locationStrategy = + new ProvidedURLLocationStrategy(); final int loops = 8; for (int i = 0; i < loops; i++) @@ -1363,7 +1396,15 @@ public class TestFileHandler handler.setEncoding(encoding); }; }; - List<Thread> threads = Arrays.asList(t1, t2, t3); + Thread t4 = new Thread() + { + @Override + public void run() + { + handler.setLocationStrategy(locationStrategy); + } + }; + List<Thread> threads = Arrays.asList(t1, t2, t3, t4); for (Thread t : threads) { t.start(); @@ -1378,6 +1419,8 @@ public class TestFileHandler assertNull("Got a URL", locator.getSourceURL()); assertEquals("Wrong encoding", encoding, locator.getEncoding()); assertSame("Wrong file system", fileSystem, locator.getFileSystem()); + assertSame("Wrong location strategy", locationStrategy, + locator.getLocationStrategy()); } }