commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1407876 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/beanutils/ main/java/org/apache/commons/configuration/builder/ test/java/org/apache/commons/configuration/beanutils/
Date Sat, 10 Nov 2012 20:11:35 GMT
Author: oheger
Date: Sat Nov 10 20:11:33 2012
New Revision: 1407876

URL: http://svn.apache.org/viewvc?rev=1407876&view=rev
Log:
[CONFIGURATION-514] Extended BeanDeclaration interface by a method for returning the constructor
arguments.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanDeclaration.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanDeclaration.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanDeclaration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanDeclaration.java
Sat Nov 10 20:11:33 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.configuration.beanutils;
 
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -93,10 +94,22 @@ public interface BeanDeclaration
      * these complex properties. The returned map's key are the names of the
      * properties to initialize. The values are either {@code BeanDeclaration}
      * implementations or collections thereof. They will be treated like this
-     * declaration (in a* recursive manner), and the resulting beans are
+     * declaration (in a recursive manner), and the resulting beans are
      * assigned to the corresponding properties.
      *
      * @return a map with nested bean declarations
      */
     Map<String, Object> getNestedBeanDeclarations();
+
+    /**
+     * Returns a collection with constructor arguments. This data is used to
+     * determine the constructor of the bean class to be invoked. The values of
+     * the arguments are passed to the constructor. An implementation can return
+     * <b>null</b> or an empty collection; then the standard constructor of the
+     * bean class is called.
+     *
+     * @return a collection with the arguments to be passed to the bean class's
+     *         constructor
+     */
+    Collection<ConstructorArg> getConstructorArgs();
 }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/XMLBeanDeclaration.java
Sat Nov 10 20:11:33 2012
@@ -17,8 +17,10 @@
 package org.apache.commons.configuration.beanutils;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -45,6 +47,7 @@ import org.apache.commons.configuration.
  *   ...
  *   &lt;personBean config-class=&quot;my.model.PersonBean&quot;
  *       lastName=&quot;Doe&quot; firstName=&quot;John&quot;&gt;
+ *       &lt;config-constrarg config-value=&quot;ID03493&quot; config-type=&quot;java.lang.String&quot;/&gt;
  *       &lt;address config-class=&quot;my.model.AddressBean&quot;
  *           street=&quot;21st street 11&quot; zip=&quot;1234&quot;
  *           city=&quot;TestCity&quot;/&gt;
@@ -90,6 +93,14 @@ import org.apache.commons.configuration.
  * nested elements for complex bean properties.
  * </p>
  * <p>
+ * If the bean should be created using a specific constructor, the constructor
+ * arguments have to be specified. This is done by an arbitrary number of
+ * nested {@code <config-constrarg>} elements. Each element can either have the
+ * {@code config-value} attribute - then it defines a simple value - or must be
+ * again a bean declaration (conforming to the format defined here) defining
+ * the complex value of this constructor argument.
+ * </p>
+ * <p>
  * A {@code XMLBeanDeclaration} object is usually created from a
  * {@code HierarchicalConfiguration}. From this it will derive a
  * {@code SubnodeConfiguration}, which is used to access the needed
@@ -124,6 +135,24 @@ public class XMLBeanDeclaration implemen
     public static final String ATTR_FACTORY_PARAM = ATTR_PREFIX
             + "factoryParam]";
 
+    /** Constant for the name of the bean class attribute. */
+    private static final String ATTR_BEAN_CLASS_NAME = RESERVED_PREFIX + "class";
+
+    /** Constant for the name of the element for constructor arguments. */
+    private static final String ELEM_CTOR_ARG = RESERVED_PREFIX + "constrarg";
+
+    /**
+     * Constant for the name of the attribute with the value of a constructor
+     * argument.
+     */
+    private static final String ATTR_CTOR_VALUE = RESERVED_PREFIX + "value";
+
+    /**
+     * Constant for the name of the attribute with the data type of a
+     * constructor argument.
+     */
+    private static final String ATTR_CTOR_TYPE = RESERVED_PREFIX + "type";
+
     /** Stores the associated configuration. */
     private final SubnodeConfiguration configuration;
 
@@ -347,6 +376,23 @@ public class XMLBeanDeclaration implemen
     }
 
     /**
+     * {@inheritDoc} This implementation processes all child nodes with the name
+     * {@code config-constrarg}. If such a node has a {@code config-class}
+     * attribute, it is considered a nested bean declaration; otherwise it is
+     * interpreted as a simple value. If no nested constructor argument
+     * declarations are found, result is an empty collection.
+     */
+    public Collection<ConstructorArg> getConstructorArgs()
+    {
+        Collection<ConstructorArg> args = new LinkedList<ConstructorArg>();
+        for (ConfigurationNode child : getNode().getChildren(ELEM_CTOR_ARG))
+        {
+            args.add(createConstructorArg(child));
+        }
+        return args;
+    }
+
+    /**
      * Performs interpolation for the specified value. This implementation will
      * interpolate against the current subnode configuration's parent. If sub
      * classes need a different interpolation mechanism, they should override
@@ -364,18 +410,16 @@ public class XMLBeanDeclaration implemen
     /**
      * Checks if the specified node is reserved and thus should be ignored. This
      * method is called when the maps for the bean's properties and complex
-     * properties are collected. It checks whether the given node is an
-     * attribute node and if its name starts with the reserved prefix.
+     * properties are collected. It checks whether the name of the given node
+     * starts with the reserved prefix.
      *
      * @param nd the node to be checked
      * @return a flag whether this node is reserved (and does not point to a
-     * property)
+     *         property)
      */
     protected boolean isReservedNode(ConfigurationNode nd)
     {
-        return nd.isAttribute()
-                && (nd.getName() == null || nd.getName().startsWith(
-                        RESERVED_PREFIX));
+        return nd.getName() == null || nd.getName().startsWith(RESERVED_PREFIX);
     }
 
     /**
@@ -425,4 +469,52 @@ public class XMLBeanDeclaration implemen
         conf.setThrowExceptionOnMissing(false);
         conf.setExpressionEngine(null);
     }
+
+    /**
+     * Creates a {@code ConstructorArg} object for the specified configuration
+     * node.
+     *
+     * @param child the configuration node
+     * @return the corresponding {@code ConstructorArg} object
+     */
+    private ConstructorArg createConstructorArg(ConfigurationNode child)
+    {
+        String type = getAttribute(child, ATTR_CTOR_TYPE);
+        if (isBeanDeclarationArgument(child))
+        {
+            return ConstructorArg.forValue(
+                    getAttribute(child, ATTR_CTOR_VALUE), type);
+        }
+        else
+        {
+            return ConstructorArg.forBeanDeclaration(
+                    createBeanDeclaration(child), type);
+        }
+    }
+
+    /**
+     * Checks whether the constructor argument represented by the given
+     * configuration node is a bean declaration.
+     *
+     * @param nd the configuration node in question
+     * @return a flag whether this constructor argument is a bean declaration
+     */
+    private static boolean isBeanDeclarationArgument(ConfigurationNode nd)
+    {
+        return nd.getAttributes(ATTR_BEAN_CLASS_NAME).isEmpty();
+    }
+
+    /**
+     * Helper method for obtaining an attribute of a configuration node.
+     *
+     * @param nd the node
+     * @param attr the name of the attribute
+     * @return the string value of this attribute (can be <b>null</b>)
+     */
+    private static String getAttribute(ConfigurationNode nd, String attr)
+    {
+        List<ConfigurationNode> attributes = nd.getAttributes(attr);
+        return attributes.isEmpty() ? null : String.valueOf(attributes.get(0)
+                .getValue());
+    }
 }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
Sat Nov 10 20:11:33 2012
@@ -28,6 +28,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.ConfigurationRuntimeException;
 import org.apache.commons.configuration.beanutils.BeanDeclaration;
 import org.apache.commons.configuration.beanutils.BeanHelper;
+import org.apache.commons.configuration.beanutils.ConstructorArg;
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.event.EventSource;
@@ -513,6 +514,12 @@ public class BasicConfigurationBuilder<T
                 return Collections.emptyMap();
             }
 
+            public Collection<ConstructorArg> getConstructorArgs()
+            {
+                // no constructor arguments
+                return Collections.emptySet();
+            }
+
             public Map<String, Object> getBeanProperties()
             {
                 // the properties are equivalent to the parameters

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
Sat Nov 10 20:11:33 2012
@@ -22,6 +22,7 @@ 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.Map;
 
@@ -517,5 +518,11 @@ public class TestBeanHelper
         {
             this.nestedBeanDeclarations = nestedBeanDeclarations;
         }
+
+        public Collection<ConstructorArg> getConstructorArgs()
+        {
+            //TODO implementation
+            return null;
+        }
     }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
Sat Nov 10 20:11:33 2012
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -104,5 +105,10 @@ public class TestDefaultBeanFactory
         {
             return null;
         }
+
+        public Collection<ConstructorArg> getConstructorArgs()
+        {
+            return null;
+        }
     }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java?rev=1407876&r1=1407875&r2=1407876&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestXMLBeanDeclaration.java
Sat Nov 10 20:11:33 2012
@@ -17,10 +17,13 @@
 package org.apache.commons.configuration.beanutils;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.commons.configuration.BaseHierarchicalConfiguration;
@@ -41,42 +44,52 @@ import org.junit.Test;
 public class TestXMLBeanDeclaration
 {
     /** An array with some test properties. */
-    static final String[] TEST_PROPS =
+    private static final String[] TEST_PROPS =
     { "firstName", "lastName", "department", "age", "hobby"};
 
     /** An array with the values for the test properties. */
-    static final String[] TEST_VALUES =
+    private static final String[] TEST_VALUES =
     { "John", "Smith", "Engineering", "42", "TV"};
 
     /** An array with the names of nested (complex) properties. */
-    static final String[] COMPLEX_PROPS =
+    private static final String[] COMPLEX_PROPS =
     { "address", "car"};
 
     /** An array with the names of the classes of the complex properties. */
-    static final String[] COMPLEX_CLASSES =
+    private static final String[] COMPLEX_CLASSES =
     { "org.apache.commons.configuration.test.AddressTest",
             "org.apache.commons.configuration.test.CarTest"};
 
     /** An array with the property names of the complex properties. */
-    static final String[][] COMPLEX_ATTRIBUTES =
+    private static final String[][] COMPLEX_ATTRIBUTES =
     {
     { "street", "zip", "city", "country"},
     { "brand", "color"}};
 
     /** An array with the values of the complex properties. */
-    static final String[][] COMPLEX_VALUES =
+    private static final String[][] COMPLEX_VALUES =
     {
     { "Baker Street", "12354", "London", "UK"},
     { "Bentley", "silver"}};
 
+    /** An array with property names for a complex constructor argument. */
+    private static final String[] CTOR_COMPLEX_ATTRIBUTES = {
+            "secCode", "validTo"
+    };
+
+    /** An array with values of a complex constructor argument. */
+    private static final String[] CTOR_COMPLEX_VALUES = {
+            "20121110181559", "2015-01-31"
+    };
+
+    /** Constant for an ID value passed as constructor argument. */
+    private static final String CTOR_ID = "20121110182006";
+
     /** Constant for the key with the bean declaration. */
-    static final String KEY = "myBean";
+    private static final String KEY = "myBean";
 
     /** Constant for the section with the variables.*/
-    static final String VARS = "variables.";
-
-    /** Stores the object to be tested. */
-    XMLBeanDeclaration decl;
+    private static final String VARS = "variables.";
 
     /**
      * Tests creating a declaration from a null node. This should cause an
@@ -85,7 +98,7 @@ public class TestXMLBeanDeclaration
     @Test(expected = IllegalArgumentException.class)
     public void testInitFromNullNode()
     {
-        decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration().configurationAt(null),
+        new XMLBeanDeclaration(new BaseHierarchicalConfiguration().configurationAt(null),
                 (ConfigurationNode) null);
     }
 
@@ -96,7 +109,7 @@ public class TestXMLBeanDeclaration
     @Test(expected = IllegalArgumentException.class)
     public void testInitFromNullConfiguration()
     {
-        decl = new XMLBeanDeclaration((HierarchicalConfiguration) null);
+        new XMLBeanDeclaration((HierarchicalConfiguration) null);
     }
 
     /**
@@ -106,7 +119,7 @@ public class TestXMLBeanDeclaration
     @Test(expected = IllegalArgumentException.class)
     public void testInitFromNullConfigurationAndKey()
     {
-        decl = new XMLBeanDeclaration(null, KEY);
+        new XMLBeanDeclaration(null, KEY);
     }
 
     /**
@@ -116,7 +129,7 @@ public class TestXMLBeanDeclaration
     @Test(expected = IllegalArgumentException.class)
     public void testInitFromNullConfigurationAndNode()
     {
-        decl = new XMLBeanDeclaration(null, new BaseHierarchicalConfiguration()
+        new XMLBeanDeclaration(null, new BaseHierarchicalConfiguration()
                 .getRootNode());
     }
 
@@ -128,7 +141,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         config.addProperty(KEY + "[@config-class]", getClass().getName());
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         assertEquals("Wrong class name", getClass().getName(), decl
                 .getBeanClassName());
     }
@@ -139,7 +152,7 @@ public class TestXMLBeanDeclaration
     @Test
     public void testGetBeanClassNameUndefined()
     {
-        decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
         assertNull(decl.getBeanClassName());
     }
 
@@ -151,7 +164,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         config.addProperty(KEY + "[@config-factory]", "myFactory");
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         assertEquals("Wrong factory name", "myFactory", decl
                 .getBeanFactoryName());
     }
@@ -162,7 +175,7 @@ public class TestXMLBeanDeclaration
     @Test
     public void testGetBeanFactoryNameUndefined()
     {
-        decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
         assertNull(decl.getBeanFactoryName());
     }
 
@@ -176,7 +189,7 @@ public class TestXMLBeanDeclaration
         config
                 .addProperty(KEY + "[@config-factoryParam]",
                         "myFactoryParameter");
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         assertEquals("Wrong factory parameter", "myFactoryParameter", decl
                 .getBeanFactoryParameter());
     }
@@ -187,7 +200,7 @@ public class TestXMLBeanDeclaration
     @Test
     public void testGetBeanFactoryParameterUndefined()
     {
-        decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
         assertNull(decl.getBeanFactoryParameter());
     }
 
@@ -200,7 +213,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         checkProperties(decl, TEST_PROPS, TEST_VALUES);
     }
 
@@ -215,7 +228,7 @@ public class TestXMLBeanDeclaration
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
         config.addProperty(KEY + "[@config-testattr]", "yes");
         config.addProperty(KEY + "[@config-anothertest]", "this, too");
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         checkProperties(decl, TEST_PROPS, TEST_VALUES);
     }
 
@@ -225,19 +238,27 @@ public class TestXMLBeanDeclaration
     @Test
     public void testGetBeanPropertiesEmpty()
     {
-        decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(new BaseHierarchicalConfiguration());
         Map<String, Object> props = decl.getBeanProperties();
         assertTrue("Properties found", props == null || props.isEmpty());
     }
 
     /**
-     * Creates a configuration with data for testing nested bean declarations.
+     * Creates a configuration with data for testing nested bean declarations
+     * including constructor arguments.
      * @return the initialized test configuration
      */
     private HierarchicalConfiguration prepareNestedBeanDeclarations()
     {
-        BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
+        BaseHierarchicalConfiguration config =
+                new BaseHierarchicalConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
+        String keyCtorArg = KEY + ".config-constrarg";
+        setupBeanDeclaration(config, keyCtorArg, CTOR_COMPLEX_ATTRIBUTES,
+                CTOR_COMPLEX_VALUES);
+        config.addProperty(keyCtorArg + "[@config-class]", "TestClass");
+        config.addProperty(keyCtorArg + "(-1)[@config-value]", CTOR_ID);
+        config.addProperty(keyCtorArg + "[@config-type]", "long");
         for (int i = 0; i < COMPLEX_PROPS.length; i++)
         {
             setupBeanDeclaration(config, KEY + '.' + COMPLEX_PROPS[i],
@@ -256,7 +277,7 @@ public class TestXMLBeanDeclaration
     public void testGetNestedBeanDeclarations()
     {
         HierarchicalConfiguration config = prepareNestedBeanDeclarations();
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         checkProperties(decl, TEST_PROPS, TEST_VALUES);
 
         Map<String, Object> nested = decl.getNestedBeanDeclarations();
@@ -281,7 +302,7 @@ public class TestXMLBeanDeclaration
     public void testGetNestedBeanDeclarationsFactoryMethod()
     {
         HierarchicalConfiguration config = prepareNestedBeanDeclarations();
-        decl = new XMLBeanDeclaration(config, KEY)
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY)
         {
             @Override
             protected BeanDeclaration createBeanDeclaration(
@@ -308,7 +329,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         Map<String, Object> nested = decl.getNestedBeanDeclarations();
         assertTrue("Found nested declarations", nested == null
                 || nested.isEmpty());
@@ -328,7 +349,7 @@ public class TestXMLBeanDeclaration
             config.addProperty(VARS + TEST_PROPS[i], TEST_VALUES[i]);
         }
         setupBeanDeclaration(config, KEY, TEST_PROPS, varValues);
-        decl = new XMLBeanDeclaration(config, KEY);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
         checkProperties(decl, TEST_PROPS, TEST_VALUES);
     }
 
@@ -341,7 +362,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
-        decl = new XMLBeanDeclaration(config, "undefined_key");
+        new XMLBeanDeclaration(config, "undefined_key");
     }
 
     /**
@@ -355,7 +376,7 @@ public class TestXMLBeanDeclaration
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
-        decl = new XMLBeanDeclaration(config, "undefined_key", true);
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, "undefined_key", true);
         assertNull("Found a bean class", decl.getBeanClassName());
     }
 
@@ -369,7 +390,48 @@ public class TestXMLBeanDeclaration
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         config.addProperty(KEY, "myFirstKey");
         config.addProperty(KEY, "mySecondKey");
-        decl = new XMLBeanDeclaration(config, KEY);
+        new XMLBeanDeclaration(config, KEY);
+    }
+
+    /**
+     * Tests whether constructor arguments can be queried.
+     */
+    @Test
+    public void testGetConstructorArgs()
+    {
+        HierarchicalConfiguration config = prepareNestedBeanDeclarations();
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
+        Collection<ConstructorArg> args = decl.getConstructorArgs();
+        assertEquals("Wrong number of constructor arguments", 2, args.size());
+        Iterator<ConstructorArg> it = args.iterator();
+        ConstructorArg arg1 = it.next();
+        assertTrue("No bean declaration", arg1.isNestedBeanDeclaration());
+        checkProperties(arg1.getBeanDeclaration(), CTOR_COMPLEX_ATTRIBUTES,
+                CTOR_COMPLEX_VALUES);
+        assertNull("Got a type", arg1.getTypeName());
+        assertEquals("Wrong class name", "TestClass", arg1.getBeanDeclaration()
+                .getBeanClassName());
+        ConstructorArg arg2 = it.next();
+        assertFalse("A bean declaration", arg2.isNestedBeanDeclaration());
+        assertEquals("Wrong value", CTOR_ID, arg2.getValue());
+        assertEquals("Wrong type", "long", arg2.getTypeName());
+    }
+
+    /**
+     * Tests whether a constructor argument with a null value can be defined.
+     */
+    @Test
+    public void testGetConstructorArgsNullArg()
+    {
+        HierarchicalConfiguration config = new BaseHierarchicalConfiguration();
+        setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
+        config.addProperty(KEY + ".config-constrarg", "");
+        XMLBeanDeclaration decl = new XMLBeanDeclaration(config, KEY);
+        Collection<ConstructorArg> args = decl.getConstructorArgs();
+        assertEquals("Wrong number of constructor arguments", 1, args.size());
+        ConstructorArg arg = args.iterator().next();
+        assertFalse("A bean declaration", arg.isNestedBeanDeclaration());
+        assertNull("Got a value", arg.getValue());
     }
 
     /**
@@ -381,7 +443,7 @@ public class TestXMLBeanDeclaration
      * @param names an array with the names of the properties
      * @param values an array with the corresponding values
      */
-    private void setupBeanDeclaration(BaseHierarchicalConfiguration config,
+    private void setupBeanDeclaration(HierarchicalConfiguration config,
             String key, String[] names, String[] values)
     {
         for (int i = 0; i < names.length; i++)



Mime
View raw message