commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1546643 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/
Date Fri, 29 Nov 2013 20:53:46 GMT
Author: oheger
Date: Fri Nov 29 20:53:46 2013
New Revision: 1546643

URL: http://svn.apache.org/r1546643
Log:
CombinedBuilderProperties now supports DefaultParametersHandler objects.

The intension is to use the same mechanism for specifying default values for
child configuration sources as is used for normal parameter objects.

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=1546643&r1=1546642&r2=1546643&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
Fri Nov 29 20:53:46 2013
@@ -28,6 +28,8 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.builder.BasicBuilderParameters;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilder;
+import org.apache.commons.configuration.builder.DefaultParametersHandler;
+import org.apache.commons.configuration.builder.DefaultParametersManager;
 
 /**
  * <p>
@@ -67,6 +69,9 @@ public class CombinedBuilderParametersIm
     /** A list with default parameters for child configuration sources. */
     private final Collection<BuilderParameters> childParameters;
 
+    /** The manager for default handlers. */
+    private DefaultParametersManager childDefaultParametersManager;
+
     /** The base path for configuration sources to be loaded. */
     private String basePath;
 
@@ -344,6 +349,64 @@ public class CombinedBuilderParametersIm
     }
 
     /**
+     * Returns the {@code DefaultParametersManager} object for initializing
+     * parameter objects for child configuration sources. This method never
+     * returns <b>null</b>. If no manager was set, a new instance is created
+     * right now.
+     *
+     * @return the {@code DefaultParametersManager} for child configuration
+     *         sources
+     */
+    public DefaultParametersManager getChildDefaultParametersManager()
+    {
+        if (childDefaultParametersManager == null)
+        {
+            childDefaultParametersManager = new DefaultParametersManager();
+        }
+        return childDefaultParametersManager;
+    }
+
+    /**
+     * {@inheritDoc} This implementation stores the passed in manager object. An
+     * already existing manager object (either explicitly set or created on
+     * demand) is overridden. This also removes all default handlers registered
+     * before!
+     */
+    public CombinedBuilderParametersImpl setChildDefaultParametersManager(
+            DefaultParametersManager manager)
+    {
+        childDefaultParametersManager = manager;
+        return this;
+    }
+
+    /**
+     * {@inheritDoc} This implementation registers the passed in handler at an
+     * internal {@link DefaultParametersManager} instance. If none was set, a
+     * new instance is created now.
+     */
+    public <D> CombinedBuilderParametersImpl registerChildDefaultsHandler(
+            Class<D> paramClass, DefaultParametersHandler<? super D> handler)
+    {
+        getChildDefaultParametersManager().registerDefaultsHandler(paramClass,
+                handler);
+        return this;
+    }
+
+    /**
+     * {@inheritDoc} This implementation registers the passed in handler at an
+     * internal {@link DefaultParametersManager} instance. If none was set, a
+     * new instance is created now.
+     */
+    public <D> CombinedBuilderParametersImpl registerChildDefaultsHandler(
+            Class<D> paramClass, DefaultParametersHandler<? super D> handler,
+            Class<?> startClass)
+    {
+        getChildDefaultParametersManager().registerDefaultsHandler(paramClass,
+                handler, startClass);
+        return this;
+    }
+
+    /**
      * {@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=1546643&r1=1546642&r2=1546643&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
Fri Nov 29 20:53:46 2013
@@ -19,6 +19,8 @@ package org.apache.commons.configuration
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilder;
+import org.apache.commons.configuration.builder.DefaultParametersHandler;
+import org.apache.commons.configuration.builder.DefaultParametersManager;
 
 /**
  * <p>
@@ -111,4 +113,58 @@ public interface CombinedBuilderProperti
      * @return a reference to this object for method chaining
      */
     T addChildParameters(BuilderParameters params);
+
+    /**
+     * Sets a {@code DefaultParametersManager} object responsible for managing the default
+     * parameter handlers to be applied on child configuration sources. When creating
+     * builders for child configuration sources their parameters are initialized using
+     * this {@code DefaultParametersManager} instance. This way, meaningful defaults can
+     * be set. Note that calling this method overrides all
+     * {@code DefaultParametersHandler} objects previously set by one of the
+     * {@code registerChildDefaultsHandler()} methods! So either use this method if a
+     * pre-configured manager object is to be set or call the
+     * {@code registerChildDefaultHandler()} methods with the handlers to be registered
+     * (in the latter case, it is not necessary to set a {@code DefaultParametersManager}
+     * explicitly; a default one is created behind the scenes).
+     *
+     * @param manager the {@code DefaultParametersManager}
+     * @return a reference to this object for method chaining
+     */
+    T setChildDefaultParametersManager(DefaultParametersManager manager);
+
+    /**
+     * Registers a {@code DefaultParametersHandler} for child configuration sources. With
+     * this method an arbitrary number of handler objects can be set. When creating
+     * builders for child configuration sources their parameters are initialized by
+     * invoking all matching {@code DefaultParametersHandler}s on them. So, basically the
+     * same mechanism is used for the initialization of parameters for child configuration
+     * sources as for normal parameter objects.
+     *
+     * @param <D> the type of the handler to be registered
+     * @param paramClass the parameter class supported by the handler
+     * @param handler the {@code DefaultParametersHandler} to be registered
+     * @return a reference to this object for method chaining
+     * @see DefaultParametersManager#registerDefaultsHandler(Class,
+     * DefaultParametersHandler)
+     */
+    <D> T registerChildDefaultsHandler(Class<D> paramClass,
+            DefaultParametersHandler<? super D> handler);
+
+    /**
+     * Registers a {@code DefaultParametersHandler} for child configuration sources
+     * derived from the given start class. This method works like the overloaded variant,
+     * but limits the application of the defaults handler to specific child configuration
+     * sources.
+     *
+     * @param <D> the type of the handler to be registered
+     * @param paramClass the parameter class supported by the handler
+     * @param handler the {@code DefaultParametersHandler} to be registered
+     * @param startClass an optional start class in the hierarchy of parameter objects for
+     * which this handler should be applied
+     * @return a reference to this object for method chaining
+     * @see DefaultParametersManager#registerDefaultsHandler(Class,
+     * DefaultParametersHandler, Class)
+     */
+    <D> T registerChildDefaultsHandler(Class<D> paramClass,
+            DefaultParametersHandler<? super D> handler, Class<?> startClass);
 }

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=1546643&r1=1546642&r2=1546643&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
Fri Nov 29 20:53:46 2013
@@ -34,8 +34,11 @@ 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.DefaultParametersHandler;
+import org.apache.commons.configuration.builder.DefaultParametersManager;
 import org.apache.commons.configuration.builder.PropertiesBuilderParametersImpl;
 import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
+import org.apache.commons.configuration.builder.fluent.FileBasedBuilderParameters;
 import org.apache.commons.configuration.tree.ExpressionEngine;
 import org.easymock.EasyMock;
 import org.junit.Test;
@@ -390,4 +393,71 @@ public class TestCombinedBuilderParamete
         assertTrue("Got child parameters", params.getDefaultChildParameters()
                 .isEmpty());
     }
+
+    /**
+     * Tests whether a default parameters manager is dynamically created if it has not
+     * been set.
+     */
+    @Test
+    public void testGetChildDefaultParametersManagerUndefined() {
+        CombinedBuilderParametersImpl params = new CombinedBuilderParametersImpl();
+        assertNotNull("No default manager", params.getChildDefaultParametersManager());
+    }
+
+    /**
+     * Tests whether a default parameters manager can be set and queried.
+     */
+    @Test
+    public void testGetChildDefaultParametersManagerSpecific() {
+        DefaultParametersManager manager = EasyMock
+                .createMock(DefaultParametersManager.class);
+        EasyMock.replay(manager);
+        CombinedBuilderParametersImpl params = new CombinedBuilderParametersImpl();
+        assertSame("Wrong result", params,
+                params.setChildDefaultParametersManager(manager));
+        assertSame("Wrong manager", manager, params.getChildDefaultParametersManager());
+    }
+
+    /**
+     * Creates a mock for a defaults handler.
+     * @return the handler mock
+     */
+    private static DefaultParametersHandler<BuilderParameters> createDefaultsHandlerMock()
+    {
+        @SuppressWarnings("unchecked")
+        DefaultParametersHandler<BuilderParameters> mock = EasyMock.createMock(DefaultParametersHandler.class);
+        return mock;
+    }
+
+    /**
+     * Tests whether a defaults handler for a child source can be registered.
+     */
+    @Test
+    public void testRegisterChildDefaultsHandler()
+    {
+        DefaultParametersManager manager = EasyMock.createMock(DefaultParametersManager.class);
+        DefaultParametersHandler<BuilderParameters> handler = createDefaultsHandlerMock();
+        manager.registerDefaultsHandler(BuilderParameters.class, handler);
+        EasyMock.replay(manager, handler);
+        CombinedBuilderParametersImpl params = new CombinedBuilderParametersImpl();
+        params.setChildDefaultParametersManager(manager);
+        assertSame("Wrong result", params, params.registerChildDefaultsHandler(BuilderParameters.class,
handler));
+        EasyMock.verify(manager);
+    }
+
+    /**
+     * Tests whether a defaults handler for a child source with a class restriction can be
registered.
+     */
+    @Test
+    public void testRegisterChildDefaultsHandlerWithStartClass()
+    {
+        DefaultParametersManager manager = EasyMock.createMock(DefaultParametersManager.class);
+        DefaultParametersHandler<BuilderParameters> handler = createDefaultsHandlerMock();
+        manager.registerDefaultsHandler(BuilderParameters.class, handler, FileBasedBuilderParameters.class);
+        EasyMock.replay(manager, handler);
+        CombinedBuilderParametersImpl params = new CombinedBuilderParametersImpl();
+        params.setChildDefaultParametersManager(manager);
+        assertSame("Wrong result", params, params.registerChildDefaultsHandler(BuilderParameters.class,
handler, FileBasedBuilderParameters.class));
+        EasyMock.verify(manager);
+    }
 }



Mime
View raw message