Author: oheger
Date: Wed Dec 26 20:11:43 2012
New Revision: 1426005
URL: http://svn.apache.org/viewvc?rev=1426005&view=rev
Log:
BasicBuilderParameters now supports properties related to interpolation.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderProperties.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java?rev=1426005&r1=1426004&r2=1426005&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderParameters.java
Wed Dec 26 20:11:43 2012
@@ -16,9 +16,13 @@
*/
package org.apache.commons.configuration.builder;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+import org.apache.commons.configuration.interpol.Lookup;
import org.apache.commons.logging.Log;
/**
@@ -58,6 +62,15 @@ public class BasicBuilderParameters impl
/** The key of the <em>logger</em> property. */
private static final String PROP_LOGGER = "logger";
+ /** The key for the <em>interpolator</em> property. */
+ private static final String PROP_INTERPOLATOR = "interpolator";
+
+ /** The key for the <em>prefixLookups</em> property. */
+ private static final String PROP_PREFIX_LOOKUPS = "prefixLookups";
+
+ /** The key for the <em>defaultLookups</em> property. */
+ private static final String PROP_DEFAULT_LOOKUPS = "defaultLookups";
+
/** The map for storing the current property values. */
private final Map<String, Object> properties;
@@ -76,7 +89,15 @@ public class BasicBuilderParameters impl
*/
public Map<String, Object> getParameters()
{
- return new HashMap<String, Object>(properties);
+ HashMap<String, Object> result =
+ new HashMap<String, Object>(properties);
+ if (result.containsKey(PROP_INTERPOLATOR))
+ {
+ // A custom ConfigurationInterpolator overrides lookups
+ result.remove(PROP_PREFIX_LOOKUPS);
+ result.remove(PROP_DEFAULT_LOOKUPS);
+ }
+ return result;
}
/**
@@ -138,6 +159,55 @@ public class BasicBuilderParameters impl
}
/**
+ * {@inheritDoc} The passed in {@code ConfigurationInterpolator} is set
+ * without modifications.
+ */
+ public BasicBuilderParameters setInterpolator(ConfigurationInterpolator ci)
+ {
+ return setProperty(PROP_INTERPOLATOR, ci);
+ }
+
+ /**
+ * {@inheritDoc} A defensive copy of the passed in map is created. A
+ * <b>null</b> argument causes all prefix lookups to be removed from the
+ * internal parameters map.
+ */
+ public BasicBuilderParameters setPrefixLookups(
+ Map<String, ? extends Lookup> lookups)
+ {
+ if (lookups == null)
+ {
+ properties.remove(PROP_PREFIX_LOOKUPS);
+ return this;
+ }
+ else
+ {
+ return setProperty(PROP_PREFIX_LOOKUPS,
+ new HashMap<String, Lookup>(lookups));
+ }
+ }
+
+ /**
+ * {@inheritDoc} A defensive copy of the passed in collection is created. A
+ * <b>null</b> argument causes all default lookups to be removed from the
+ * internal parameters map.
+ */
+ public BasicBuilderParameters setDefaultLookups(
+ Collection<? extends Lookup> lookups)
+ {
+ if (lookups == null)
+ {
+ properties.remove(PROP_DEFAULT_LOOKUPS);
+ return this;
+ }
+ else
+ {
+ return setProperty(PROP_DEFAULT_LOOKUPS, new ArrayList<Lookup>(
+ lookups));
+ }
+ }
+
+ /**
* Merges this object with the given parameters object. This method adds
all
* property values defined by the passed in parameters object to the
* internal storage which are not already in. So properties already defined
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderProperties.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderProperties.java?rev=1426005&r1=1426004&r2=1426005&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderProperties.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicBuilderProperties.java
Wed Dec 26 20:11:43 2012
@@ -16,6 +16,11 @@
*/
package org.apache.commons.configuration.builder;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+import org.apache.commons.configuration.interpol.Lookup;
import org.apache.commons.logging.Log;
/**
@@ -81,4 +86,46 @@ public interface BasicBuilderProperties<
* @see #setDelimiterParsingDisabled(boolean)
*/
T setListDelimiter(char c);
+
+ /**
+ * Sets the {@code ConfigurationInterpolator} to be used for this
+ * configuration. Using this method a custom
+ * {@code ConfigurationInterpolator} can be set which can be freely
+ * configured. Alternatively, it is possible to add custom {@code Lookup}
+ * objects using other methods provided by this interface.
+ *
+ * @param ci the {@code ConfigurationInterpolator} for this configuration
+ * @return a reference to this object for method chaining
+ */
+ T setInterpolator(ConfigurationInterpolator ci);
+
+ /**
+ * Sets additional {@code Lookup} objects for specific prefixes for this
+ * configuration object. All {@code Lookup} objects contained in the given
+ * map are added to the configuration's {@code ConfigurationInterpolator}.
+ * Note: This method only takes effect if no
+ * {@code ConfigurationInterpolator} is set using the
+ * {@link #setInterpolator(ConfigurationInterpolator)} method.
+ *
+ * @param lookups a map with {@code Lookup} objects and their associated
+ * prefixes
+ * @return a reference to this object for method chaining
+ * @see ConfigurationInterpolator#registerLookups(Map)
+ */
+ T setPrefixLookups(Map<String, ? extends Lookup> lookups);
+
+ /**
+ * Adds additional default {@code Lookup} objects (i.e. lookups which are
+ * not associated with a specific prefix) to this configuration object.
+ * Note: This method only takes effect if no
+ * {@code ConfigurationInterpolator} is set using the
+ * {@link #setInterpolator(ConfigurationInterpolator)} method.
+ *
+ * @param lookups a collection with {@code Lookup} objects to be added as
+ * default lookups at the configuration's
+ * {@code ConfigurationInterpolator}
+ * @return a reference to this object for method chaining
+ * @see ConfigurationInterpolator#addDefaultLookups(Collection)
+ */
+ T setDefaultLookups(Collection<? extends Lookup> lookups);
}
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java?rev=1426005&r1=1426004&r2=1426005&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicBuilderParameters.java
Wed Dec 26 20:11:43 2012
@@ -21,9 +21,14 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+import org.apache.commons.configuration.interpol.Lookup;
import org.apache.commons.logging.Log;
import org.easymock.EasyMock;
import org.junit.Before;
@@ -121,6 +126,94 @@ public class TestBasicBuilderParameters
}
/**
+ * Tests whether a {@code ConfigurationInterpolator} can be set.
+ */
+ @Test
+ public void testSetInterpolator()
+ {
+ ConfigurationInterpolator ci =
+ EasyMock.createMock(ConfigurationInterpolator.class);
+ EasyMock.replay(ci);
+ assertSame("Wrong result", params, params.setInterpolator(ci));
+ assertSame("Wrong interpolator", ci,
+ params.getParameters().get("interpolator"));
+ }
+
+ /**
+ * Tests whether prefix lookups can be set.
+ */
+ @Test
+ public void testSetPrefixLookups()
+ {
+ Lookup look = EasyMock.createMock(Lookup.class);
+ Map<String, Lookup> lookups = Collections.singletonMap("test", look);
+ assertSame("Wrong result", params, params.setPrefixLookups(lookups));
+ Map<?, ?> map = (Map<?, ?>)
params.getParameters().get("prefixLookups");
+ assertNotSame("No copy was created", lookups, map);
+ assertEquals("Wrong lookup", look, map.get("test"));
+ assertEquals("Wrong number of lookups", 1, map.size());
+ }
+
+ /**
+ * Tests whether null values are handled by setPrefixLookups().
+ */
+ @Test
+ public void testSetPrefixLookupsNull()
+ {
+ params.setPrefixLookups(new HashMap<String, Lookup>());
+ params.setPrefixLookups(null);
+ assertFalse("Found key",
+ params.getParameters().containsKey("prefixLookups"));
+ }
+
+ /**
+ * Tests whether default lookups can be set.
+ */
+ @Test
+ public void testSetDefaultLookups()
+ {
+ Lookup look = EasyMock.createMock(Lookup.class);
+ Collection<Lookup> looks = Collections.singleton(look);
+ assertSame("Wrong result", params, params.setDefaultLookups(looks));
+ Collection<?> col =
+ (Collection<?>) params.getParameters().get("defaultLookups");
+ assertNotSame("No copy was created", col, looks);
+ assertEquals("Wrong number of lookups", 1, col.size());
+ assertSame("Wrong lookup", look, col.iterator().next());
+ }
+
+ /**
+ * Tests whether null values are handled by setDefaultLookups().
+ */
+ @Test
+ public void testSetDefaultLookupsNull()
+ {
+ params.setDefaultLookups(new ArrayList<Lookup>());
+ params.setDefaultLookups(null);
+ assertFalse("Found key",
+ params.getParameters().containsKey("defaultLookups"));
+ }
+
+ /**
+ * Tests whether a custom {@code ConfigurationInterpolator} overrides
+ * settings for custom lookups.
+ */
+ @Test
+ public void testSetLookupsAndInterpolator()
+ {
+ Lookup look1 = EasyMock.createMock(Lookup.class);
+ Lookup look2 = EasyMock.createMock(Lookup.class);
+ ConfigurationInterpolator ci =
+ EasyMock.createMock(ConfigurationInterpolator.class);
+ params.setDefaultLookups(Collections.singleton(look1));
+ params.setPrefixLookups(Collections.singletonMap("test", look2));
+ params.setInterpolator(ci);
+ Map<String, Object> map = params.getParameters();
+ assertFalse("Got prefix lookups", map.containsKey("prefixLookups"));
+ assertFalse("Got default lookups", map.containsKey("defaultLookups"));
+ }
+
+ /**
* Tries a merge with a null object.
*/
@Test(expected = IllegalArgumentException.class)