commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1422683 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
Date Sun, 16 Dec 2012 20:35:56 GMT
Author: oheger
Date: Sun Dec 16 20:35:55 2012
New Revision: 1422683

URL: http://svn.apache.org/viewvc?rev=1422683&view=rev
Log:
Added CombinedBeanDeclaration class.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java
  (with props)
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
  (with props)

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java?rev=1422683&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java
(added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java
Sun Dec 16 20:35:55 2012
@@ -0,0 +1,187 @@
+/*
+ * 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.beanutils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * <p>
+ * A special implementation of the {@code BeanDeclaration} interface which
+ * allows combining multiple {@code BeanDeclaration} objects.
+ * </p>
+ * <p>
+ * An instance of this class can be used if a bean is defined using multiple
+ * sources. For instance, there can be one definition with default values and
+ * one with actual values; if actual values are provided, they are used;
+ * otherwise, the default values apply.
+ * </p>
+ * <p>
+ * When constructing an instance an arbitrary number of child
+ * {@code BeanDeclaration} objects can be specified. The implementations of the
+ * {@code BeanDeclaration} methods implement a logical combination of the data
+ * returned by these child declarations. The order in which child declarations
+ * are added is relevant; first entries take precedence over later ones. The
+ * comments of the single methods explain in which way a combination of the
+ * child declarations is built.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class CombinedBeanDeclaration implements BeanDeclaration
+{
+    /** A list with the child declarations. */
+    private final List<BeanDeclaration> childDeclarations;
+
+    /**
+     * Creates a new instance of {@code CombinedBeanDeclaration} and initializes
+     * it with the given child declarations.
+     *
+     * @param decl the child declarations
+     * @throws NullPointerException if the array with child declarations is
+     *         <b>null</b>
+     */
+    public CombinedBeanDeclaration(BeanDeclaration... decl)
+    {
+        childDeclarations = new ArrayList<BeanDeclaration>(Arrays.asList(decl));
+    }
+
+    /**
+     * {@inheritDoc} This implementation iterates over the list of child
+     * declarations and asks them for a bean factory name. The first
+     * non-<b>null</b> value is returned. If none of the child declarations have
+     * a defined bean factory name, result is <b>null</b>.
+     */
+    public String getBeanFactoryName()
+    {
+        for (BeanDeclaration d : childDeclarations)
+        {
+            String factoryName = d.getBeanFactoryName();
+            if (factoryName != null)
+            {
+                return factoryName;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc} This implementation iterates over the list of child
+     * declarations and asks them for a bean factory parameter. The first
+     * non-<b>null</b> value is returned. If none of the child declarations have
+     * a defined bean factory parameter, result is <b>null</b>.
+     */
+    public Object getBeanFactoryParameter()
+    {
+        for (BeanDeclaration d : childDeclarations)
+        {
+            Object factoryParam = d.getBeanFactoryParameter();
+            if (factoryParam != null)
+            {
+                return factoryParam;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc} This implementation iterates over the list of child
+     * declarations and asks them for the bean class name. The first
+     * non-<b>null</b> value is returned. If none of the child declarations have
+     * a defined bean class, result is <b>null</b>.
+     */
+    public String getBeanClassName()
+    {
+        for (BeanDeclaration d : childDeclarations)
+        {
+            String beanClassName = d.getBeanClassName();
+            if (beanClassName != null)
+            {
+                return beanClassName;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc} This implementation creates a union of the properties
+     * returned by all child declarations. If a property is defined in multiple
+     * child declarations, the declaration that comes before in the list of
+     * children takes precedence.
+     */
+    public Map<String, Object> getBeanProperties()
+    {
+        Map<String, Object> result = new HashMap<String, Object>();
+        for (int i = childDeclarations.size() - 1; i >= 0; i--)
+        {
+            Map<String, Object> props =
+                    childDeclarations.get(i).getBeanProperties();
+            if (props != null)
+            {
+                result.putAll(props);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * {@inheritDoc} This implementation creates a union of the nested bean
+     * declarations returned by all child declarations. If a complex property is
+     * defined in multiple child declarations, the declaration that comes before
+     * in the list of children takes precedence.
+     */
+    public Map<String, Object> getNestedBeanDeclarations()
+    {
+        Map<String, Object> result = new HashMap<String, Object>();
+        for (int i = childDeclarations.size() - 1; i >= 0; i--)
+        {
+            Map<String, Object> decls =
+                    childDeclarations.get(i).getNestedBeanDeclarations();
+            if (decls != null)
+            {
+                result.putAll(decls);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * {@inheritDoc} This implementation iterates over the list of child
+     * declarations and asks them for constructor arguments. The first
+     * non-<b>null</b> and non empty collection is returned. If none of the
+     * child declarations provide constructor arguments, result is an empty
+     * collection.
+     */
+    public Collection<ConstructorArg> getConstructorArgs()
+    {
+        for (BeanDeclaration d : childDeclarations)
+        {
+            Collection<ConstructorArg> args = d.getConstructorArgs();
+            if (args != null && !args.isEmpty())
+            {
+                return args;
+            }
+        }
+        return Collections.emptyList();
+    }
+}

Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/CombinedBeanDeclaration.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java?rev=1422683&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
(added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
Sun Dec 16 20:35:55 2012
@@ -0,0 +1,295 @@
+/*
+ * 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.beanutils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+/**
+ * Test class for {@code CombinedBeanDeclaration}.
+ *
+ * @version $Id$
+ */
+public class TestCombinedBeanDeclaration
+{
+    /** An array with the mocks for the child bean declarations. */
+    private BeanDeclaration[] declarations;
+
+    /**
+     * Helper method for replaying the mock objects used by the tests.
+     */
+    private void replay()
+    {
+        if (declarations != null)
+        {
+            EasyMock.replay((Object[]) declarations);
+        }
+    }
+
+    /**
+     * Helper method for verifying the mock objects used by the tests.
+     */
+    private void verify()
+    {
+        if (declarations != null)
+        {
+            EasyMock.verify((Object[]) declarations);
+        }
+    }
+
+    /**
+     * Convenience method for accessing a mock declaration with the given index.
+     *
+     * @param idx the index
+     * @return the corresponding mock child bean declaration
+     */
+    private BeanDeclaration decl(int idx)
+    {
+        return declarations[idx];
+    }
+
+    /**
+     * Creates a test instance with a number of mock child declarations.
+     *
+     * @return the test instance
+     */
+    private CombinedBeanDeclaration createCombinedDeclaration()
+    {
+        declarations = new BeanDeclaration[3];
+        for (int i = 0; i < declarations.length; i++)
+        {
+            declarations[i] = EasyMock.createMock(BeanDeclaration.class);
+        }
+        return new CombinedBeanDeclaration(declarations);
+    }
+
+    /**
+     * Tests whether the bean factory can be obtained if it is set for one child
+     * declarations.
+     */
+    @Test
+    public void testGetBeanFactoryNameDefined()
+    {
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        String name = "someTestBeanFactory";
+        EasyMock.expect(decl(0).getBeanFactoryName()).andReturn(null);
+        EasyMock.expect(decl(1).getBeanFactoryName()).andReturn(name);
+        replay();
+        assertEquals("Wrong factory name", name, cd.getBeanFactoryName());
+        verify();
+    }
+
+    /**
+     * Tests getBeanFactoryName() if none of the child declarations provide a
+     * value.
+     */
+    @Test
+    public void testGetBeanFactoryNameUndefined()
+    {
+        CombinedBeanDeclaration cd = new CombinedBeanDeclaration();
+        assertNull("Got a factory name", cd.getBeanFactoryName());
+    }
+
+    /**
+     * Tests whether the bean factory parameter can be obtained if it is set for
+     * one of the child declarations.
+     */
+    @Test
+    public void testGetBeanFactoryParameterDefined()
+    {
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        Object param = new Object();
+        EasyMock.expect(decl(0).getBeanFactoryParameter()).andReturn(null);
+        EasyMock.expect(decl(1).getBeanFactoryParameter()).andReturn(param);
+        replay();
+        assertSame("Wrong parameter", param, cd.getBeanFactoryParameter());
+        verify();
+    }
+
+    /**
+     * Tests getBeanFactoryParameter() if none of the child declarations provide
+     * a value.
+     */
+    @Test
+    public void testGetBeanFactoryParameterUndefined()
+    {
+        CombinedBeanDeclaration cd = new CombinedBeanDeclaration();
+        assertNull("Got a factory parameter", cd.getBeanFactoryParameter());
+    }
+
+    /**
+     * Tests whether the bean class name can be obtained if it is set for one of
+     * the child declarations.
+     */
+    @Test
+    public void testGetBeanClassNameDefined()
+    {
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        EasyMock.expect(decl(0).getBeanClassName()).andReturn(null);
+        EasyMock.expect(decl(1).getBeanClassName()).andReturn(
+                getClass().getName());
+        replay();
+        assertEquals("Wrong bean class", getClass().getName(),
+                cd.getBeanClassName());
+        verify();
+    }
+
+    /**
+     * Tests getBeanClassName() if none of the child declarations provide a
+     * value.
+     */
+    @Test
+    public void testGetBeanClassNameUndefined()
+    {
+        CombinedBeanDeclaration cd = new CombinedBeanDeclaration();
+        assertNull("Got a bean class name", cd.getBeanClassName());
+    }
+
+    /**
+     * Tests whether constructor arguments can be obtained if one of the child
+     * declarations provide this data.
+     */
+    @Test
+    public void testGetConstructorArgsDefined()
+    {
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        Collection<ConstructorArg> args =
+                Arrays.asList(ConstructorArg.forValue(42));
+        EasyMock.expect(decl(0).getConstructorArgs()).andReturn(null);
+        EasyMock.expect(decl(1).getConstructorArgs()).andReturn(args);
+        replay();
+        assertSame("Wrong constructor arguments", args, cd.getConstructorArgs());
+        verify();
+    }
+
+    /**
+     * Tests getConstructorArgs() if none of the child declarations provide a
+     * value.
+     */
+    @Test
+    public void testGetConstructorArgsUndefined()
+    {
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        EasyMock.expect(decl(0).getConstructorArgs()).andReturn(null);
+        EasyMock.expect(decl(1).getConstructorArgs()).andReturn(
+                new ArrayList<ConstructorArg>());
+        EasyMock.expect(decl(2).getConstructorArgs()).andReturn(null);
+        replay();
+        assertTrue("Got constructor arguments", cd.getConstructorArgs()
+                .isEmpty());
+        verify();
+    }
+
+    /**
+     * Tests whether a combined map of bean properties can be obtained.
+     */
+    @Test
+    public void testGetBeanProperties()
+    {
+        Map<String, Object> props1 = new HashMap<String, Object>();
+        Map<String, Object> props2 = new HashMap<String, Object>();
+        Map<String, Object> props3 = new HashMap<String, Object>();
+        props1.put("param1", "value1");
+        props1.put("param2", "value2");
+        props2.put("param2", "othervalue");
+        props2.put("param3", "value3");
+        props3.put("param1", "differentvalue");
+        props3.put("param4", "value4");
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        EasyMock.expect(decl(0).getBeanProperties()).andReturn(props1);
+        EasyMock.expect(decl(1).getBeanProperties()).andReturn(props2);
+        EasyMock.expect(decl(2).getBeanProperties()).andReturn(props3);
+        replay();
+        Map<String, Object> props = cd.getBeanProperties();
+        assertEquals("Wrong number of properties", 4, props.size());
+        for (int i = 1; i <= 4; i++)
+        {
+            assertEquals("Wrong property", "value" + i, props.get("param" + i));
+        }
+        verify();
+    }
+
+    /**
+     * Tests whether null return values of bean property maps are handled
+     * correctly.
+     */
+    @Test
+    public void testGetBeanPropertiesNull()
+    {
+        BeanDeclaration child = EasyMock.createMock(BeanDeclaration.class);
+        EasyMock.expect(child.getBeanProperties()).andReturn(null);
+        EasyMock.replay(child);
+        CombinedBeanDeclaration cd = new CombinedBeanDeclaration(child);
+        assertTrue("Got bean properties", cd.getBeanProperties().isEmpty());
+    }
+
+    /**
+     * Tests whether a combined map of nested bean declarations can be obtained.
+     */
+    @Test
+    public void testGetNestedBeanDeclarations()
+    {
+        Map<String, Object> decls1 = new HashMap<String, Object>();
+        Map<String, Object> decls2 = new HashMap<String, Object>();
+        Map<String, Object> decls3 = new HashMap<String, Object>();
+        decls1.put("param1", "value1");
+        decls1.put("param2", "value2");
+        decls2.put("param2", "othervalue");
+        decls2.put("param3", "value3");
+        decls3.put("param1", "differentvalue");
+        decls3.put("param4", "value4");
+        CombinedBeanDeclaration cd = createCombinedDeclaration();
+        EasyMock.expect(decl(0).getNestedBeanDeclarations()).andReturn(decls1);
+        EasyMock.expect(decl(1).getNestedBeanDeclarations()).andReturn(decls2);
+        EasyMock.expect(decl(2).getNestedBeanDeclarations()).andReturn(decls3);
+        replay();
+        Map<String, Object> decls = cd.getNestedBeanDeclarations();
+        assertEquals("Wrong number of declarations", 4, decls.size());
+        for (int i = 1; i <= 4; i++)
+        {
+            assertEquals("Wrong declaration", "value" + i,
+                    decls.get("param" + i));
+        }
+        verify();
+    }
+
+    /**
+     * Tests whether null return values of bean declaration maps are handled
+     * correctly.
+     */
+    @Test
+    public void testGetNestedBeanDeclarationsNull()
+    {
+        BeanDeclaration child = EasyMock.createMock(BeanDeclaration.class);
+        EasyMock.expect(child.getNestedBeanDeclarations()).andReturn(null);
+        EasyMock.replay(child);
+        CombinedBeanDeclaration cd = new CombinedBeanDeclaration(child);
+        assertTrue("Got bean declarations", cd.getNestedBeanDeclarations()
+                .isEmpty());
+    }
+}

Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestCombinedBeanDeclaration.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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



Mime
View raw message