Author: oheger
Date: Sun Jun 29 19:45:58 2014
New Revision: 1606589
URL: http://svn.apache.org/r1606589
Log:
FileBasedBuilderParametersImpl can not be initialized from a map.
This simplifies the creation of instances in IoC frameworks.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParametersImpl.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParametersImpl.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParametersImpl.java?rev=1606589&r1=1606588&r2=1606589&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParametersImpl.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParametersImpl.java
Sun Jun 29 19:45:58 2014
@@ -53,6 +53,13 @@ public class FileBasedBuilderParametersI
private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX
+ FileBasedBuilderParametersImpl.class.getName();
+ /** Property name for the reloading refresh delay. */
+ private static final String PROP_REFRESH_DELAY = "reloadingRefreshDelay";
+
+ /** Property name of the reloading detector factory. */
+ private static final String PROP_DETECTOR_FACTORY =
+ "reloadingDetectorFactory";
+
/**
* Stores the associated file handler for the location of the
configuration.
*/
@@ -125,6 +132,32 @@ public class FileBasedBuilderParametersI
}
/**
+ * Creates a new {@code FileBasedBuilderParametersImpl} object from the
+ * content of the given map. While {@code fromParameters()} expects that an
+ * object already exists and is stored in the given map, this method
creates
+ * a new instance based on the content of the map. The map can contain
+ * properties of a {@code FileHandler} and some additional settings which
+ * are stored directly in the newly created object. If the map is
+ * <b>null</b>, an uninitialized instance is returned.
+ *
+ * @param map the map with properties (must not be <b>null</b>)
+ * @return the newly created instance
+ * @throws ClassCastException if the map contains invalid data
+ */
+ public static FileBasedBuilderParametersImpl fromMap(Map<String, Object>
map)
+ {
+ FileBasedBuilderParametersImpl params =
+ new FileBasedBuilderParametersImpl(FileHandler.fromMap(map));
+ if (map != null)
+ {
+ params.setReloadingRefreshDelay((Long)
map.get(PROP_REFRESH_DELAY));
+ params.setReloadingDetectorFactory((ReloadingDetectorFactory) map
+ .get(PROP_DETECTOR_FACTORY));
+ }
+ return params;
+ }
+
+ /**
* Returns the {@code FileHandler} managed by this object. This object is
* updated every time the file location is changed.
*
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java?rev=1606589&r1=1606588&r2=1606589&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
Sun Jun 29 19:45:58 2014
@@ -303,4 +303,46 @@ public class TestFileBasedBuilderParamet
assertNotSame("No copy of file handler", params.getFileHandler(),
clone.getFileHandler());
}
+
+ /**
+ * Tests whether an instance can be created from a map.
+ */
+ @Test
+ public void testFromMap()
+ {
+ ReloadingDetectorFactory factory =
+ EasyMock.createMock(ReloadingDetectorFactory.class);
+ EasyMock.replay(factory);
+ Map<String, Object> map = new HashMap<String, Object>();
+ final String fileName = "someFileName";
+ final String basePath = "someBasePath";
+ final Long refreshDelay = 20140628222302L;
+ map.put("basePath", basePath);
+ map.put("fileName", fileName);
+ map.put("reloadingDetectorFactory", factory);
+ map.put("reloadingRefreshDelay", refreshDelay);
+
+ FileBasedBuilderParametersImpl params =
+ FileBasedBuilderParametersImpl.fromMap(map);
+ assertEquals("Wrong base path", basePath, params.getFileHandler()
+ .getBasePath());
+ assertEquals("Wrong file name", fileName, params.getFileHandler()
+ .getFileName());
+ assertEquals("Wrong detector factory", factory,
+ params.getReloadingDetectorFactory());
+ assertEquals("Wrong refresh delay", refreshDelay,
+ params.getReloadingRefreshDelay());
+ }
+
+ /**
+ * Tests fromMap() for null input.
+ */
+ @Test
+ public void testFromMapNull()
+ {
+ FileBasedBuilderParametersImpl params =
+ FileBasedBuilderParametersImpl.fromMap(null);
+ assertNull("Got refresh delay", params.getReloadingRefreshDelay());
+ assertNull("Got a file name", params.getFileHandler().getFileName());
+ }
}