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 2013/01/13 22:13:21 UTC

svn commit: r1432740 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/

Author: oheger
Date: Sun Jan 13 21:13:20 2013
New Revision: 1432740

URL: http://svn.apache.org/viewvc?rev=1432740&view=rev
Log:
Added a parameters class for a multi-file configuration builder.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java   (with props)
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java   (with props)
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java   (with props)
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java   (with props)

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java?rev=1432740&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java Sun Jan 13 21:13:20 2013
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.combined;
+
+import java.util.Map;
+
+import org.apache.commons.configuration.builder.BasicBuilderParameters;
+import org.apache.commons.configuration.builder.BuilderParameters;
+import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
+
+/**
+ * <p>
+ * A specialized parameters object for {@link MultiFileConfigurationBuilder}.
+ * </p>
+ * <p>
+ * A parameters object of this type is used by a configuration builder with
+ * manages multiple file-based configurations. Such a builder is a bit special
+ * because it does not create a configuration on its own, but delegates to a
+ * file-based builder for this purpose. Therefore, parameters inherited from the
+ * super class are treated differently:
+ * <ul>
+ * <li>The {@link ConfigurationInterpolator} is needed by a
+ * {@code MultiFileConfigurationBuilder} to resolve the file pattern. It is
+ * expected to be set and will not be passed to sub configurations created by
+ * the builder.</li>
+ * <li>All other parameters are evaluated when creating sub configurations.
+ * However, it is preferred to use the
+ * {@link #setManagedBuilderParameters(BuilderParameters)} method to define all
+ * properties of sub configurations in a single place. If such a parameters
+ * object is set, its properties take precedence.</li>
+ * </ul>
+ * </p>
+ * <p>
+ * This class is not thread-safe. It is intended that an instance is constructed
+ * and initialized by a single thread during configuration of a
+ * {@code ConfigurationBuilder}.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class MultiFileBuilderParametersImpl extends BasicBuilderParameters
+        implements MultiFileBuilderProperties<MultiFileBuilderParametersImpl>
+{
+    /** Constant for the key in the parameters map used by this class. */
+    private static final String PARAM_KEY = RESERVED_PARAMETER_PREFIX
+            + MultiFileBuilderParametersImpl.class.getName();
+
+    /** The parameters object for managed builders. */
+    private BuilderParameters managedBuilderParameters;
+
+    /** The file pattern. */
+    private String filePattern;
+
+    /**
+     * Obtains an instance of this class from the given map with parameters. If
+     * this map does not contain an instance, result is <b>null</b>. This is
+     * equivalent to {@code fromParameters(params, false)}.
+     *
+     * @param params the map with parameters (must not be <b>null</b>)
+     * @return an instance of this class fetched from the map or <b>null</b>
+     * @throws NullPointerException if the map with parameters is <b>null</b>
+     */
+    public static MultiFileBuilderParametersImpl fromParameters(
+            Map<String, Object> params)
+    {
+        return fromParameters(params, false);
+    }
+
+    /**
+     * Obtains an instance of this class from the given map with parameters and
+     * creates a new object if such an instance cannot be found. This method can
+     * be used to obtain an instance from a map which has been created using the
+     * {@code getParameters()} method. If the map does not contain an instance
+     * under the expected key and the {@code createIfMissing} parameter is
+     * <b>true</b>, a new instance is created. Otherwise, result is <b>null</b>.
+     *
+     * @param params the map with parameters (must not be <b>null</b>)
+     * @param createIfMissing a flag whether a new instance should be created if
+     *        necessary
+     * @return an instance of this class fetched from the map or <b>null</b>
+     * @throws NullPointerException if the map with parameters is <b>null</b>
+     */
+    public static MultiFileBuilderParametersImpl fromParameters(
+            Map<String, Object> params, boolean createIfMissing)
+    {
+        MultiFileBuilderParametersImpl instance =
+                (MultiFileBuilderParametersImpl) params.get(PARAM_KEY);
+        if (instance == null && createIfMissing)
+        {
+            instance = new MultiFileBuilderParametersImpl();
+        }
+        return instance;
+    }
+
+    /**
+     * Returns the pattern for determining file names for managed
+     * configurations.
+     *
+     * @return the file pattern
+     */
+    public String getFilePattern()
+    {
+        return filePattern;
+    }
+
+    public MultiFileBuilderParametersImpl setFilePattern(String p)
+    {
+        filePattern = p;
+        return this;
+    }
+
+    /**
+     * Returns the parameters object for managed configuration builders.
+     *
+     * @return the parameters for sub configurations
+     */
+    public BuilderParameters getManagedBuilderParameters()
+    {
+        return managedBuilderParameters;
+    }
+
+    public MultiFileBuilderParametersImpl setManagedBuilderParameters(
+            BuilderParameters p)
+    {
+        managedBuilderParameters = p;
+        return this;
+    }
+
+    /**
+     * {@inheritDoc} This implementation puts a reference to this object under a
+     * reserved key in the resulting parameters map.
+     */
+    @Override
+    public Map<String, Object> getParameters()
+    {
+        Map<String, Object> params = super.getParameters();
+        params.put(PARAM_KEY, this);
+        return params;
+    }
+}

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java?rev=1432740&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java Sun Jan 13 21:13:20 2013
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.combined;
+
+import org.apache.commons.configuration.builder.BuilderParametersBeanInfo;
+
+/**
+ * An additional {@code BeanInfo} class for
+ * {@link MultiFileBuilderParametersImpl}.
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class MultiFileBuilderParametersImplBeanInfo extends
+        BuilderParametersBeanInfo
+{
+    /**
+     * Creates a new instance of {@code MultiFileBuilderParametersImplBeanInfo}.
+     */
+    public MultiFileBuilderParametersImplBeanInfo()
+    {
+        super(MultiFileBuilderParametersImpl.class);
+    }
+}

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderParametersImplBeanInfo.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java?rev=1432740&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java Sun Jan 13 21:13:20 2013
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.combined;
+
+import org.apache.commons.configuration.builder.BuilderParameters;
+
+/**
+ * <p>
+ * Definition of a properties interface for the parameters of a multiple file
+ * configuration builder.
+ * </p>
+ * <p>
+ * This interface defines a number of properties for configuring a builder
+ * managing multiple file-based configurations which are selected by a pattern.
+ * Properties can be set in a fluent style.
+ * </p>
+ *
+ * @version $Id $
+ * @since 2.0
+ * @param <T> the return type of all methods for allowing method chaining
+ */
+public interface MultiFileBuilderProperties<T>
+{
+    /**
+     * Sets the pattern string. Based on this pattern the configuration file to
+     * be loaded is determined.
+     *
+     * @param p the pattern string
+     * @return a reference to this object for method chaining
+     */
+    T setFilePattern(String p);
+
+    /**
+     * Sets a parameters object to be used when creating a managed
+     * configuration. These parameters configure sub configurations.
+     *
+     * @param p the parameters object for a sub configuration
+     * @return a reference to this object for method chaining
+     */
+    T setManagedBuilderParameters(BuilderParameters p);
+}

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileBuilderProperties.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java?rev=1432740&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java Sun Jan 13 21:13:20 2013
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.combined;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.configuration.builder.BuilderParameters;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code MultiFileBuilderParametersImpl}.
+ *
+ * @version $Id$
+ */
+public class TestMultiFileBuilderParametersImpl
+{
+    /** The parameters object to be tested. */
+    private MultiFileBuilderParametersImpl params;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        params = new MultiFileBuilderParametersImpl();
+    }
+
+    /**
+     * Tests whether an instance can be obtained from a map if it cannot be
+     * found.
+     */
+    @Test
+    public void testFromParatersNotFound()
+    {
+        assertNull("Got an instance",
+                MultiFileBuilderParametersImpl
+                        .fromParameters(new HashMap<String, Object>()));
+    }
+
+    /**
+     * Tests whether an instance can be obtained from a parameters map.
+     */
+    @Test
+    public void testFromParametersFound()
+    {
+        Map<String, Object> map = params.getParameters();
+        assertSame("Instance not found", params,
+                MultiFileBuilderParametersImpl.fromParameters(map, true));
+    }
+
+    /**
+     * Tests whether a new instance is created if the parameters map does not
+     * contain one.
+     */
+    @Test
+    public void testFromParametersNewInstance()
+    {
+        params =
+                MultiFileBuilderParametersImpl.fromParameters(
+                        new HashMap<String, Object>(), true);
+        assertNotNull("No new instance", params);
+    }
+
+    /**
+     * Tests whether a file pattern can be set.
+     */
+    @Test
+    public void testSetFilePattern()
+    {
+        String pattern = "somePattern";
+        assertSame("Wrong result", params, params.setFilePattern(pattern));
+        assertEquals("Pattern not set", pattern, params.getFilePattern());
+    }
+
+    /**
+     * Tests whether parameters for managed configurations can be set.
+     */
+    @Test
+    public void testSetManagedBuilderParameters()
+    {
+        BuilderParameters bp = EasyMock.createMock(BuilderParameters.class);
+        EasyMock.replay(bp);
+        assertSame("Wrong result", params, params.setManagedBuilderParameters(bp));
+        assertSame("Parameters not set", bp,
+                params.getManagedBuilderParameters());
+    }
+
+    /**
+     * Tests whether bean property access is possible.
+     */
+    @Test
+    public void testBeanProperties() throws Exception
+    {
+        BuilderParameters bp = EasyMock.createMock(BuilderParameters.class);
+        EasyMock.replay(bp);
+        String pattern = "testPattern";
+        PropertyUtils.setProperty(params, "filePattern", pattern);
+        PropertyUtils.setProperty(params, "managedBuilderParameters", bp);
+        PropertyUtils.setProperty(params, "throwExceptionOnMissing",
+                Boolean.TRUE);
+        Map<String, Object> map = params.getParameters();
+        assertEquals("Exception flag not set", Boolean.TRUE,
+                map.get("throwExceptionOnMissing"));
+        assertSame("Wrong parameters instance", params,
+                MultiFileBuilderParametersImpl.fromParameters(map));
+        assertEquals("Wrong pattern", pattern, params.getFilePattern());
+        assertSame("Wrong managed parameters", bp,
+                params.getManagedBuilderParameters());
+    }
+}

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileBuilderParametersImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain