commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1463685 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/
Date Tue, 02 Apr 2013 19:04:40 GMT
Author: oheger
Date: Tue Apr  2 19:04:39 2013
New Revision: 1463685

URL: http://svn.apache.org/r1463685
Log:
CombinedBuilderParametersImpl now supports a way of adding default parameters for child configuration
sources.

To a parameters object an arbitrary number of child parameter objects can be
added. These parameters are copied to parameter objects for child
configuration sources. So this is a convenient way to set default properties
for child configuration sources of a combined configuration builder.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderParametersImpl.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderProperties.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedBuilderParametersImpl.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderParametersImpl.java?rev=1463685&r1=1463684&r2=1463685&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderParametersImpl.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderParametersImpl.java
Tue Apr  2 19:04:39 2013
@@ -16,8 +16,11 @@
  */
 package org.apache.commons.configuration.builder.combined;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.Map;
 
 import org.apache.commons.configuration.ConfigurationUtils;
@@ -61,6 +64,9 @@ public class CombinedBuilderParametersIm
     /** A map with registered configuration builder providers. */
     private final Map<String, ConfigurationBuilderProvider> providers;
 
+    /** A list with default parameters for child configuration sources. */
+    private final Collection<BuilderParameters> childParameters;
+
     /** The base path for configuration sources to be loaded. */
     private String basePath;
 
@@ -70,6 +76,7 @@ public class CombinedBuilderParametersIm
     public CombinedBuilderParametersImpl()
     {
         providers = new HashMap<String, ConfigurationBuilderProvider>();
+        childParameters = new LinkedList<BuilderParameters>();
     }
 
     /**
@@ -308,6 +315,35 @@ public class CombinedBuilderParametersIm
     }
 
     /**
+     * {@inheritDoc} This implementation stores the passed in object in an
+     * internal collection. From there it can be queried later to perform
+     * initialization of a combined builder's child configuration sources.
+     */
+    public CombinedBuilderParametersImpl addChildParameters(
+            BuilderParameters params)
+    {
+        if (params != null)
+        {
+            childParameters.add(params);
+        }
+        return this;
+    }
+
+    /**
+     * Returns a collection with default parameter objects for child
+     * configuration sources. This collection contains the same objects (in the
+     * same order) that were passed to {@code addChildParameters()}. The
+     * returned collection is a defensive copy; it can be modified, but this has
+     * no effect on the parameters stored in this object.
+     *
+     * @return a map with default parameters for child sources
+     */
+    public Collection<? extends BuilderParameters> getDefaultChildParameters()
+    {
+        return new ArrayList<BuilderParameters>(childParameters);
+    }
+
+    /**
      * {@inheritDoc} This implementation returns a map which contains this
      * object itself under a specific key. The static {@code fromParameters()}
      * method can be used to extract an instance from a parameters map.

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderProperties.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderProperties.java?rev=1463685&r1=1463684&r2=1463685&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderProperties.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/CombinedBuilderProperties.java
Tue Apr  2 19:04:39 2013
@@ -88,4 +88,27 @@ public interface CombinedBuilderProperti
      * @return a reference to this object for method chaining
      */
     T setDefinitionBuilderParameters(BuilderParameters params);
+
+    /**
+     * Adds a parameters object with default parameters for child configuration
+     * sources. With this method an arbitrary number of parameters objects can
+     * be set. When creating builders for child configuration sources, their
+     * parameters are initialized with all matching properties from these
+     * default parameters. For instance, this method could be called passing in
+     * a parameters object for XML configurations. Then all XML configuration
+     * sources declared in the definition configuration will be initialized with
+     * the properties defined here. If a parameters object for a base class is
+     * passed (e.g. {@code FileBasedBuilderParametersImpl} or
+     * {@code BasicBuilderParameters}), all parameter objects derived from these
+     * base classes are initialized with the corresponding properties. So by
+     * making use of classes in the hierarchy of parameter objects, default
+     * values for specific types of configuration sources can be set in a
+     * fine-grained way. (It is still possible to override properties for a
+     * specific configuration source in the definition file.) This method can be
+     * called multiple times for defining different default parameter objects.
+     *
+     * @param params the default parameters object (can be <b>null</b>)
+     * @return a reference to this object for method chaining
+     */
+    T addChildParameters(BuilderParameters params);
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedBuilderParametersImpl.java?rev=1463685&r1=1463684&r2=1463685&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedBuilderParametersImpl.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestCombinedBuilderParametersImpl.java
Tue Apr  2 19:04:39 2013
@@ -23,7 +23,9 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.commons.beanutils.PropertyUtils;
@@ -32,7 +34,9 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.builder.BasicConfigurationBuilder;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilder;
+import org.apache.commons.configuration.builder.PropertiesBuilderParametersImpl;
 import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
+import org.apache.commons.configuration.tree.ExpressionEngine;
 import org.easymock.EasyMock;
 import org.junit.Test;
 
@@ -347,4 +351,45 @@ public class TestCombinedBuilderParamete
                 clone.getDefinitionBuilderParameters().getParameters()
                         .get("systemID"));
     }
+
+    /**
+     * Tests whether default parameters for child sources can be added.
+     */
+    @Test
+    public void testAddChildParameters()
+    {
+        ExpressionEngine engine = EasyMock.createMock(ExpressionEngine.class);
+        CombinedBuilderParametersImpl params =
+                new CombinedBuilderParametersImpl();
+        XMLBuilderParametersImpl xmlParams = new XMLBuilderParametersImpl();
+        xmlParams.setExpressionEngine(engine);
+        xmlParams.setValidating(true);
+        assertSame("Wrong result (1)", params,
+                params.addChildParameters(xmlParams));
+        PropertiesBuilderParametersImpl prpParams =
+                new PropertiesBuilderParametersImpl();
+        prpParams.setIncludesAllowed(false);
+        assertSame("Wrong result (2)", params,
+                params.addChildParameters(prpParams));
+        Collection<? extends BuilderParameters> list =
+                params.getDefaultChildParameters();
+        assertEquals("Wrong number of parameters objects", 2, list.size());
+        Iterator<? extends BuilderParameters> it = list.iterator();
+        assertSame("Wrong parameters object (1)", xmlParams, it.next());
+        assertSame("Wrong parameters object (2)", prpParams, it.next());
+    }
+
+    /**
+     * Tests whether a null parameters object is handled correctly by
+     * addChildParameters().
+     */
+    @Test
+    public void testAddChildParametersNullParams()
+    {
+        CombinedBuilderParametersImpl params =
+                new CombinedBuilderParametersImpl();
+        params.addChildParameters((BuilderParameters) null);
+        assertTrue("Got child parameters", params.getDefaultChildParameters()
+                .isEmpty());
+    }
 }



Mime
View raw message