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/19 22:17:00 UTC
svn commit: r1524820 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/io/FileHandler.java
test/java/org/apache/commons/configuration/io/TestFileHandler.java
Author: oheger
Date: Thu Sep 19 20:16:59 2013
New Revision: 1524820
URL: http://svn.apache.org/r1524820
Log:
Added a locate() method to FileHandler.
This method ensures that the referenced file is once located (if possible).
Then a locate() operation does not have to be performed every time the file is
accessed.
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=1524820&r1=1524819&r2=1524820&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 Thu Sep 19 20:16:59 2013
@@ -557,6 +557,51 @@ public class FileHandler
}
/**
+ * 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
+ * used. If this information is incomplete (e.g. only the file name is set),
+ * an attempt to locate the file may have to be performed on each access. By
+ * calling this method such an attempt is performed once, and the results of
+ * a successful localization are stored. Hence, later access to the
+ * referenced file can be more efficient. Also, all properties pointing to
+ * the referenced file in this object's {@code FileLocator} are set (i.e.
+ * the URL, the base path, and the file name). If the referenced file cannot
+ * be located, result is <b>false</b>. This means that the information in
+ * the current {@code FileLocator} is insufficient or wrong. If the
+ * {@code FileLocator} is already fully defined, it is not changed.
+ *
+ * @return a flag whether the referenced file could be located successfully
+ * @see FileLocatorUtils#fullyInitializedLocator(FileLocator)
+ */
+ public boolean locate()
+ {
+ boolean result = false;
+ boolean done;
+
+ do
+ {
+ FileLocator locator = getFileLocator();
+ FileLocator fullLocator =
+ FileLocatorUtils.fullyInitializedLocator(locator);
+ if (fullLocator == null)
+ {
+ result = false;
+ fullLocator = locator;
+ }
+ else
+ {
+ result =
+ fullLocator != locator
+ || FileLocatorUtils.isFullyInitialized(locator);
+ }
+ done = fileLocator.compareAndSet(locator, fullLocator);
+ } while (!done);
+
+ return result;
+ }
+
+ /**
* Loads the associated file from the underlying location. If no location
* has been set, an exception is thrown.
*
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=1524820&r1=1524819&r2=1524820&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 Thu Sep 19 20:16:59 2013
@@ -18,6 +18,7 @@ package org.apache.commons.configuration
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.assertTrue;
@@ -1405,6 +1406,55 @@ public class TestFileHandler
}
/**
+ * Tests a successful locate() operation.
+ */
+ @Test
+ public void testLocateSuccess() throws ConfigurationException
+ {
+ FileHandler handler = new FileHandler();
+ handler.setFileName(TEST_FILENAME);
+ assertTrue("Wrong result", handler.locate());
+ FileLocator locator = handler.getFileLocator();
+ assertNotNull("URL not filled", locator.getSourceURL());
+ assertNotNull("Base path not filled", locator.getBasePath());
+ assertEquals("Wrong file name", TEST_FILENAME, locator.getFileName());
+
+ // check whether the correct URL was obtained
+ PropertiesConfiguration config = new PropertiesConfiguration();
+ FileHandler h2 = new FileHandler(config);
+ h2.setURL(locator.getSourceURL());
+ h2.load();
+ assertTrue("Configuration not loaded",
+ config.getBoolean("configuration.loaded"));
+ }
+
+ /**
+ * Tests a locate() operation if the specified file cannot be resolved.
+ */
+ @Test
+ public void testLocateUnknownFile()
+ {
+ FileHandler handler = new FileHandler();
+ handler.setFileName("unknown file");
+ FileLocator locator = handler.getFileLocator();
+ assertFalse("Wrong result", handler.locate());
+ assertSame("Locator was changed", locator, handler.getFileLocator());
+ }
+
+ /**
+ * Tests a locate() operation if there is not enough information.
+ */
+ @Test
+ public void testLocateUndefinedLocator()
+ {
+ FileHandler handler = new FileHandler();
+ handler.setBasePath("only/a/base/path");
+ FileLocator locator = handler.getFileLocator();
+ assertFalse("Wrong result", handler.locate());
+ assertSame("Locator was changed", locator, handler.getFileLocator());
+ }
+
+ /**
* An implementation of the FileBased interface used for test purposes.
*/
private static class FileBasedTestImpl implements FileBased