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 2016/02/14 20:00:29 UTC
svn commit: r1730375 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java
test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java
Author: oheger
Date: Sun Feb 14 19:00:28 2016
New Revision: 1730375
URL: http://svn.apache.org/viewvc?rev=1730375&view=rev
Log:
[CONFIGURATION-619] Added inheritFrom() method to BasicBuilderParameters.
This method copies a dedicated subset of properties from a source
parameters map into the builder parameters. That way a parameters
object can decide itself which properties can be inherited.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java?rev=1730375&r1=1730374&r2=1730375&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/BasicBuilderParameters.java Sun Feb 14 19:00:28 2016
@@ -309,6 +309,37 @@ public class BasicBuilderParameters impl
}
/**
+ * Inherits properties from the specified map. This can be used for instance
+ * to reuse parameters from one builder in another builder - also in
+ * parent-child relations in which a parent builder creates child builders.
+ * The purpose of this method is to let a concrete implementation decide
+ * which properties can be inherited. Because parameters are basically
+ * organized as a map it would be possible to simply copy over all
+ * properties from the source object. However, this is not appropriate in
+ * all cases. For instance, some properties - like a
+ * {@code ConfigurationInterpolator} - are tightly connected to a
+ * configuration and cannot be reused in a different context. For other
+ * properties, e.g. a file name, it does not make sense to copy it.
+ * Therefore, an implementation has to be explicit in the properties it
+ * wants to take over.
+ *
+ * @param source the source properties to inherit from
+ * @throws IllegalArgumentException if the source map is <b>null</b>
+ */
+ public void inheritFrom(Map<String, ?> source)
+ {
+ if (source == null)
+ {
+ throw new IllegalArgumentException(
+ "Source properties must not be null!");
+ }
+ copyPropertiesFrom(source, PROP_BEAN_HELPER, PROP_CONFIGURATION_DECODER,
+ PROP_CONVERSION_HANDLER, PROP_LIST_DELIMITER_HANDLER,
+ PROP_LOGGER, PROP_SYNCHRONIZER,
+ PROP_THROW_EXCEPTION_ON_MISSING);
+ }
+
+ /**
* Obtains a specification for a {@link ConfigurationInterpolator} from the
* specified map with parameters. All properties related to interpolation
* are evaluated and added to the specification object.
@@ -416,6 +447,25 @@ public class BasicBuilderParameters impl
}
/**
+ * Copies a number of properties from the given map into this object.
+ * Properties are only copied if they are defined in the source map.
+ *
+ * @param source the source map
+ * @param keys the keys to be copied
+ */
+ protected void copyPropertiesFrom(Map<String, ?> source, String... keys)
+ {
+ for (String key : keys)
+ {
+ Object value = source.get(key);
+ if (value != null)
+ {
+ storeProperty(key, value);
+ }
+ }
+ }
+
+ /**
* Helper method for setting a property value.
*
* @param key the key of the property
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java?rev=1730375&r1=1730374&r2=1730375&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestBasicBuilderParameters.java Sun Feb 14 19:00:28 2016
@@ -33,10 +33,13 @@ import org.apache.commons.configuration2
import org.apache.commons.configuration2.ConfigurationLogger;
import org.apache.commons.configuration2.beanutils.BeanHelper;
import org.apache.commons.configuration2.convert.ConversionHandler;
+import org.apache.commons.configuration2.convert.DefaultConversionHandler;
+import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.convert.ListDelimiterHandler;
import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
import org.apache.commons.configuration2.interpol.InterpolatorSpecification;
import org.apache.commons.configuration2.interpol.Lookup;
+import org.apache.commons.configuration2.sync.ReadWriteSynchronizer;
import org.apache.commons.configuration2.sync.Synchronizer;
import org.easymock.EasyMock;
import org.junit.Before;
@@ -531,4 +534,66 @@ public class TestBasicBuilderParameters
assertSame("Decoder not set", decoder,
params.getParameters().get("configurationDecoder"));
}
+
+ /**
+ * Tests whether null input is handled by inheritFrom().
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testInheritFromNull()
+ {
+ params.inheritFrom(null);
+ }
+
+ /**
+ * Tests whether properties can be inherited from another parameters map.
+ */
+ @Test
+ public void testInheritFrom()
+ {
+ BeanHelper beanHelper = new BeanHelper();
+ ConfigurationDecoder decoder =
+ EasyMock.createMock(ConfigurationDecoder.class);
+ ConversionHandler conversionHandler = new DefaultConversionHandler();
+ ListDelimiterHandler listDelimiterHandler =
+ new DefaultListDelimiterHandler('#');
+ ConfigurationLogger logger = new ConfigurationLogger("test");
+ Synchronizer synchronizer = new ReadWriteSynchronizer();
+ params.setBeanHelper(beanHelper).setConfigurationDecoder(decoder)
+ .setConversionHandler(conversionHandler)
+ .setListDelimiterHandler(listDelimiterHandler).setLogger(logger)
+ .setSynchronizer(synchronizer).setThrowExceptionOnMissing(true);
+ BasicBuilderParameters p2 = new BasicBuilderParameters();
+
+ p2.inheritFrom(params.getParameters());
+ Map<String, Object> parameters = p2.getParameters();
+ assertEquals("Bean helper not set", beanHelper,
+ parameters.get("config-BeanHelper"));
+ assertEquals("Decoder not set", decoder,
+ parameters.get("configurationDecoder"));
+ assertEquals("Conversion handler not set", conversionHandler,
+ parameters.get("conversionHandler"));
+ assertEquals("Delimiter handler not set", listDelimiterHandler,
+ parameters.get("listDelimiterHandler"));
+ assertEquals("Logger not set", logger, parameters.get("logger"));
+ assertEquals("Synchronizer not set", synchronizer,
+ parameters.get("synchronizer"));
+ assertEquals("Exception flag not set", Boolean.TRUE,
+ parameters.get("throwExceptionOnMissing"));
+ }
+
+ /**
+ * Tests that undefined properties are not copied over by inheritFrom().
+ */
+ @Test
+ public void testInheritFromUndefinedProperties()
+ {
+ BasicBuilderParameters p2 =
+ new BasicBuilderParameters().setThrowExceptionOnMissing(true);
+
+ p2.inheritFrom(Collections.<String, Object> emptyMap());
+ Map<String, Object> parameters = p2.getParameters();
+ assertEquals("Wrong number of properties", 1, parameters.size());
+ assertEquals("Exception flag not set", Boolean.TRUE,
+ parameters.get("throwExceptionOnMissing"));
+ }
}