Author: oheger
Date: Sat Sep  7 19:44:31 2013
New Revision: 1520796

URL: http://svn.apache.org/r1520796
Log:
Moved methods dealing with constructor arguments.

Since BeanHelper is no longer a static utility class, the
findMatchingConstructor() method does not really belong here. It was moved to
DefaultBeanFactory and made protected. Here it can be used by custom
BeanFactory implementations extending this class.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.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

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java?rev=1520796&r1=1520795&r2=1520796&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
 Sat Sep  7 19:44:31 2013
@@ -17,13 +17,11 @@
 package org.apache.commons.configuration.beanutils;
 
 import java.beans.PropertyDescriptor;
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -81,10 +79,6 @@ public final class BeanHelper
     private final Map<String, BeanFactory> BEAN_FACTORIES = Collections
             .synchronizedMap(new HashMap<String, BeanFactory>());
 
-    /** A format string for generating error messages for constructor 
matching. */
-    private static final String FMT_CTOR_ERROR =
-            "%s! Bean class = %s, constructor arguments = %s";
-
     /**
      * Stores the default bean factory, which is used if no other factory
      * is provided in a bean declaration.
@@ -446,29 +440,6 @@ public final class BeanHelper
     }
 
     /**
-     * Evaluates constructor arguments in the specified {@code BeanDeclaration}
-     * and tries to find a unique matching constructor. If this is not 
possible,
-     * an exception is thrown. Note: This method is intended to be used by
-     * concrete {@link BeanFactory} implementations and not by client code.
-     *
-     * @param beanClass the class of the bean to be created
-     * @param data the current {@code BeanDeclaration}
-     * @return the single matching constructor
-     * @throws ConfigurationRuntimeException if no single matching constructor
-     *         can be found
-     * @throws NullPointerException if the bean class or bean declaration are
-     *         <b>null</b>
-     */
-    public static <T> Constructor<T> findMatchingConstructor(
-            Class<T> beanClass, BeanDeclaration data)
-    {
-        List<Constructor<T>> matchingConstructors =
-                findMatchingConstructors(beanClass, data);
-        checkSingleMatchingConstructor(beanClass, data, matchingConstructors);
-        return matchingConstructors.get(0);
-    }
-
-    /**
      * Returns a {@code java.lang.Class} object for the specified name.
      * Because class loading can be tricky in some environments the code for
      * retrieving a class by its name was extracted into this helper method. So
@@ -584,122 +555,6 @@ public final class BeanHelper
     }
 
     /**
-     * Returns a list with all constructors which are compatible with the
-     * constructor arguments specified by the given {@code BeanDeclaration}.
-     *
-     * @param beanClass the bean class to be instantiated
-     * @param data the current {@code BeanDeclaration}
-     * @return a list with all matching constructors
-     */
-    private static <T> List<Constructor<T>> findMatchingConstructors(
-            Class<T> beanClass, BeanDeclaration data)
-    {
-        List<Constructor<T>> result = new LinkedList<Constructor<T>>();
-        Collection<ConstructorArg> args = getConstructorArgs(data);
-        for (Constructor<?> ctor : beanClass.getConstructors())
-        {
-            if (matchesConstructor(ctor, args))
-            {
-                // cast should be okay according to the JavaDocs of
-                // getConstructors()
-                @SuppressWarnings("unchecked")
-                Constructor<T> match = (Constructor<T>) ctor;
-                result.add(match);
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Checks whether the given constructor is compatible with the given list 
of
-     * arguments.
-     *
-     * @param ctor the constructor to be checked
-     * @param args the collection of constructor arguments
-     * @return a flag whether this constructor is compatible with the given
-     *         arguments
-     */
-    private static boolean matchesConstructor(Constructor<?> ctor,
-            Collection<ConstructorArg> args)
-    {
-        Class<?>[] types = ctor.getParameterTypes();
-        if (types.length != args.size())
-        {
-            return false;
-        }
-
-        int idx = 0;
-        for (ConstructorArg arg : args)
-        {
-            if (!arg.matches(types[idx++]))
-            {
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    /**
-     * Helper method for extracting constructor arguments from a bean
-     * declaration. Deals with <b>null</b> values.
-     *
-     * @param data the bean declaration
-     * @return the collection with constructor arguments (never <b>null</b>)
-     */
-    private static Collection<ConstructorArg> getConstructorArgs(
-            BeanDeclaration data)
-    {
-        Collection<ConstructorArg> args = data.getConstructorArgs();
-        if (args == null)
-        {
-            args = Collections.emptySet();
-        }
-        return args;
-    }
-
-    /**
-     * Helper method for testing whether exactly one matching constructor was
-     * found. Throws a meaningful exception if there is not a single matching
-     * constructor.
-     *
-     * @param beanClass the bean class
-     * @param data the bean declaration
-     * @param matchingConstructors the list with matching constructors
-     * @throws ConfigurationRuntimeException if there is not exactly one match
-     */
-    private static <T> void checkSingleMatchingConstructor(Class<T> beanClass,
-            BeanDeclaration data, List<Constructor<T>> matchingConstructors)
-    {
-        if (matchingConstructors.isEmpty())
-        {
-            throw constructorMatchingException(beanClass, data,
-                    "No matching constructor found");
-        }
-        if (matchingConstructors.size() > 1)
-        {
-            throw constructorMatchingException(beanClass, data,
-                    "Multiple matching constructors found");
-        }
-    }
-
-    /**
-     * Creates an exception if no single matching constructor was found with a
-     * meaningful error message.
-     *
-     * @param beanClass the affected bean class
-     * @param data the bean declaration
-     * @param msg an error message
-     * @return the exception with the error message
-     */
-    private static ConfigurationRuntimeException constructorMatchingException(
-            Class<?> beanClass, BeanDeclaration data, String msg)
-    {
-        return new ConfigurationRuntimeException(String.format(FMT_CTOR_ERROR,
-                msg, beanClass.getName(), 
getConstructorArgs(data).toString()));
-    }
-
-    /**
      * An implementation of the {@code BeanCreationContext} interface used by
      * {@code BeanHelper} to communicate with a {@code BeanFactory}. This class
      * contains all information required for the creation of a bean. The 
methods

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java?rev=1520796&r1=1520795&r2=1520796&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/DefaultBeanFactory.java
 Sat Sep  7 19:44:31 2013
@@ -19,7 +19,10 @@ package org.apache.commons.configuration
 import java.lang.reflect.Constructor;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
 
+import org.apache.commons.configuration.ConfigurationRuntimeException;
 import org.apache.commons.configuration.convert.ConversionHandler;
 import org.apache.commons.configuration.convert.DefaultConversionHandler;
 
@@ -53,6 +56,10 @@ public class DefaultBeanFactory implemen
     /** Stores the default instance of this class. */
     public static final DefaultBeanFactory INSTANCE = new DefaultBeanFactory();
 
+    /** A format string for generating error messages for constructor 
matching. */
+    private static final String FMT_CTOR_ERROR =
+            "%s! Bean class = %s, constructor arguments = %s";
+
     /** The conversion handler used by this instance. */
     private final ConversionHandler conversionHandler;
 
@@ -133,7 +140,7 @@ public class DefaultBeanFactory implemen
             throws Exception
     {
         Constructor<?> ctor =
-                BeanHelper.findMatchingConstructor(bcc.getBeanClass(),
+                findMatchingConstructor(bcc.getBeanClass(),
                         bcc.getBeanDeclaration());
         Object[] args = fetchConstructorArgs(ctor, bcc);
         return ctor.newInstance(args);
@@ -154,6 +161,29 @@ public class DefaultBeanFactory implemen
     }
 
     /**
+     * Evaluates constructor arguments in the specified {@code BeanDeclaration}
+     * and tries to find a unique matching constructor. If this is not 
possible,
+     * an exception is thrown. Note: This method is intended to be used by
+     * concrete {@link BeanFactory} implementations and not by client code.
+     *
+     * @param beanClass the class of the bean to be created
+     * @param data the current {@code BeanDeclaration}
+     * @return the single matching constructor
+     * @throws ConfigurationRuntimeException if no single matching constructor
+     *         can be found
+     * @throws NullPointerException if the bean class or bean declaration are
+     *         <b>null</b>
+     */
+    protected static <T> Constructor<T> findMatchingConstructor(
+            Class<T> beanClass, BeanDeclaration data)
+    {
+        List<Constructor<T>> matchingConstructors =
+                findMatchingConstructors(beanClass, data);
+        checkSingleMatchingConstructor(beanClass, data, matchingConstructors);
+        return matchingConstructors.get(0);
+    }
+
+    /**
      * Obtains the arguments for a constructor call to create a bean. This 
method
      * resolves nested bean declarations and performs necessary type
      * conversions.
@@ -200,4 +230,120 @@ public class DefaultBeanFactory implemen
         }
         return args;
     }
+
+    /**
+     * Returns a list with all constructors which are compatible with the
+     * constructor arguments specified by the given {@code BeanDeclaration}.
+     *
+     * @param beanClass the bean class to be instantiated
+     * @param data the current {@code BeanDeclaration}
+     * @return a list with all matching constructors
+     */
+    private static <T> List<Constructor<T>> findMatchingConstructors(
+            Class<T> beanClass, BeanDeclaration data)
+    {
+        List<Constructor<T>> result = new LinkedList<Constructor<T>>();
+        Collection<ConstructorArg> args = getConstructorArgs(data);
+        for (Constructor<?> ctor : beanClass.getConstructors())
+        {
+            if (matchesConstructor(ctor, args))
+            {
+                // cast should be okay according to the JavaDocs of
+                // getConstructors()
+                @SuppressWarnings("unchecked")
+                Constructor<T> match = (Constructor<T>) ctor;
+                result.add(match);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Checks whether the given constructor is compatible with the given list 
of
+     * arguments.
+     *
+     * @param ctor the constructor to be checked
+     * @param args the collection of constructor arguments
+     * @return a flag whether this constructor is compatible with the given
+     *         arguments
+     */
+    private static boolean matchesConstructor(Constructor<?> ctor,
+            Collection<ConstructorArg> args)
+    {
+        Class<?>[] types = ctor.getParameterTypes();
+        if (types.length != args.size())
+        {
+            return false;
+        }
+
+        int idx = 0;
+        for (ConstructorArg arg : args)
+        {
+            if (!arg.matches(types[idx++]))
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * Helper method for extracting constructor arguments from a bean
+     * declaration. Deals with <b>null</b> values.
+     *
+     * @param data the bean declaration
+     * @return the collection with constructor arguments (never <b>null</b>)
+     */
+    private static Collection<ConstructorArg> getConstructorArgs(
+            BeanDeclaration data)
+    {
+        Collection<ConstructorArg> args = data.getConstructorArgs();
+        if (args == null)
+        {
+            args = Collections.emptySet();
+        }
+        return args;
+    }
+
+    /**
+     * Helper method for testing whether exactly one matching constructor was
+     * found. Throws a meaningful exception if there is not a single matching
+     * constructor.
+     *
+     * @param beanClass the bean class
+     * @param data the bean declaration
+     * @param matchingConstructors the list with matching constructors
+     * @throws ConfigurationRuntimeException if there is not exactly one match
+     */
+    private static <T> void checkSingleMatchingConstructor(Class<T> beanClass,
+            BeanDeclaration data, List<Constructor<T>> matchingConstructors)
+    {
+        if (matchingConstructors.isEmpty())
+        {
+            throw constructorMatchingException(beanClass, data,
+                    "No matching constructor found");
+        }
+        if (matchingConstructors.size() > 1)
+        {
+            throw constructorMatchingException(beanClass, data,
+                    "Multiple matching constructors found");
+        }
+    }
+
+    /**
+     * Creates an exception if no single matching constructor was found with a
+     * meaningful error message.
+     *
+     * @param beanClass the affected bean class
+     * @param data the bean declaration
+     * @param msg an error message
+     * @return the exception with the error message
+     */
+    private static ConfigurationRuntimeException constructorMatchingException(
+            Class<?> beanClass, BeanDeclaration data, String msg)
+    {
+        return new ConfigurationRuntimeException(String.format(FMT_CTOR_ERROR,
+                msg, beanClass.getName(), 
getConstructorArgs(data).toString()));
+    }
 }

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=1520796&r1=1520795&r2=1520796&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 Sep  7 19:44:31 2013
@@ -21,11 +21,8 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
-import java.lang.reflect.Constructor;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -369,98 +366,6 @@ public class TestBeanHelper
     }
 
     /**
-     * Tests whether the standard constructor can be found.
-     */
-    @Test
-    public void testFindMatchingConstructorNoArgs()
-    {
-        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
-        Constructor<BeanCreationTestBean> ctor =
-                BeanHelper.findMatchingConstructor(BeanCreationTestBean.class, 
decl);
-        assertEquals("Not the standard constructor", 0,
-                ctor.getParameterTypes().length);
-    }
-
-    /**
-     * Tests whether a matching constructor is found if the number of arguments
-     * is unique.
-     */
-    @Test
-    public void testFindMatchingConstructorArgCount()
-    {
-        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
-        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
-        args.add(ConstructorArg.forValue(TEST_STRING));
-        args.add(ConstructorArg.forValue(String.valueOf(TEST_INT)));
-        decl.setConstructorArgs(args);
-        Constructor<BeanCreationTestCtorBean> ctor =
-                
BeanHelper.findMatchingConstructor(BeanCreationTestCtorBean.class, decl);
-        Class<?>[] paramTypes = ctor.getParameterTypes();
-        assertEquals("Wrong number of parameters", 2, paramTypes.length);
-        assertEquals("Wrong parameter type 1", String.class, paramTypes[0]);
-        assertEquals("Wrong parameter type 2", Integer.TYPE, paramTypes[1]);
-    }
-
-    /**
-     * Tests whether ambiguous constructor arguments are detected.
-     */
-    @Test(expected = ConfigurationRuntimeException.class)
-    public void testFindMatchingConstructorAmbiguous()
-    {
-        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
-        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
-        args.add(ConstructorArg.forValue(TEST_STRING));
-        decl.setConstructorArgs(args);
-        BeanHelper.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
-    }
-
-    /**
-     * Tests whether explicit type declarations are used to resolve ambiguous
-     * parameter types.
-     */
-    @Test
-    public void testFindMatchingConstructorExplicitType()
-    {
-        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
-        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
-        args.add(ConstructorArg.forBeanDeclaration(setUpBeanDeclaration(),
-                BeanCreationTestBean.class.getName()));
-        decl.setConstructorArgs(args);
-        Constructor<BeanCreationTestCtorBean> ctor =
-                
BeanHelper.findMatchingConstructor(BeanCreationTestCtorBean.class, decl);
-        Class<?>[] paramTypes = ctor.getParameterTypes();
-        assertEquals("Wrong number of parameters", 1, paramTypes.length);
-        assertEquals("Wrong parameter type", BeanCreationTestBean.class, 
paramTypes[0]);
-    }
-
-    /**
-     * Tests the case that no matching constructor is found.
-     */
-    @Test
-    public void testFindMatchingConstructorNoMatch()
-    {
-        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
-        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
-        args.add(ConstructorArg.forValue(TEST_STRING, getClass().getName()));
-        decl.setConstructorArgs(args);
-        try
-        {
-            BeanHelper.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
-            fail("No exception thrown!");
-        }
-        catch (ConfigurationRuntimeException crex)
-        {
-            String msg = crex.getMessage();
-            assertTrue("Bean class not found:" + msg,
-                    msg.indexOf(BeanCreationTestCtorBean.class.getName()) > 0);
-            assertTrue("Parameter value not found: " + msg,
-                    msg.indexOf(TEST_STRING) > 0);
-            assertTrue("Parameter type not found: " + msg,
-                    msg.indexOf("(" + getClass().getName() + ')') > 0);
-        }
-    }
-
-    /**
      * Returns an initialized bean declaration.
      *
      * @return the bean declaration

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=1520796&r1=1520795&r2=1520796&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 Sep  7 19:44:31 2013
@@ -21,13 +21,16 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.configuration.ConfigurationRuntimeException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.configuration.convert.ConversionHandler;
 import org.apache.commons.configuration.convert.DefaultConversionHandler;
@@ -46,6 +49,12 @@ import org.junit.Test;
  */
 public class TestDefaultBeanFactory
 {
+    /** Constant for the test value of the string property. */
+    private static final String TEST_STRING = "testString";
+
+    /** Constant for the test value of the numeric property. */
+    private static final int TEST_INT = 42;
+
     /** The object to be tested. */
     private DefaultBeanFactory factory;
 
@@ -191,4 +200,121 @@ public class TestDefaultBeanFactory
         assertEquals("Wrong property of buddy bean", "test", bean.getBuddy()
                 .getStringValue());
     }
+
+    /**
+     * Tests whether the standard constructor can be found.
+     */
+    @Test
+    public void testFindMatchingConstructorNoArgs()
+    {
+        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
+        Constructor<BeanCreationTestBean> ctor =
+                
DefaultBeanFactory.findMatchingConstructor(BeanCreationTestBean.class, decl);
+        assertEquals("Not the standard constructor", 0,
+                ctor.getParameterTypes().length);
+    }
+
+    /**
+     * Tests whether a matching constructor is found if the number of arguments
+     * is unique.
+     */
+    @Test
+    public void testFindMatchingConstructorArgCount()
+    {
+        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
+        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
+        args.add(ConstructorArg.forValue(TEST_STRING));
+        args.add(ConstructorArg.forValue(String.valueOf(TEST_INT)));
+        decl.setConstructorArgs(args);
+        Constructor<BeanCreationTestCtorBean> ctor =
+                
DefaultBeanFactory.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
+        Class<?>[] paramTypes = ctor.getParameterTypes();
+        assertEquals("Wrong number of parameters", 2, paramTypes.length);
+        assertEquals("Wrong parameter type 1", String.class, paramTypes[0]);
+        assertEquals("Wrong parameter type 2", Integer.TYPE, paramTypes[1]);
+    }
+
+    /**
+     * Tests whether ambiguous constructor arguments are detected.
+     */
+    @Test(expected = ConfigurationRuntimeException.class)
+    public void testFindMatchingConstructorAmbiguous()
+    {
+        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
+        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
+        args.add(ConstructorArg.forValue(TEST_STRING));
+        decl.setConstructorArgs(args);
+        
DefaultBeanFactory.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
+    }
+
+    /**
+     * Tests whether explicit type declarations are used to resolve ambiguous
+     * parameter types.
+     */
+    @Test
+    public void testFindMatchingConstructorExplicitType()
+    {
+        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
+        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
+        args.add(ConstructorArg.forBeanDeclaration(setUpBeanDeclaration(),
+                BeanCreationTestBean.class.getName()));
+        decl.setConstructorArgs(args);
+        Constructor<BeanCreationTestCtorBean> ctor =
+                
DefaultBeanFactory.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
+        Class<?>[] paramTypes = ctor.getParameterTypes();
+        assertEquals("Wrong number of parameters", 1, paramTypes.length);
+        assertEquals("Wrong parameter type", BeanCreationTestBean.class, 
paramTypes[0]);
+    }
+
+    /**
+     * Returns an initialized bean declaration.
+     *
+     * @return the bean declaration
+     */
+    private static BeanDeclarationTestImpl setUpBeanDeclaration()
+    {
+        BeanDeclarationTestImpl data = new BeanDeclarationTestImpl();
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("stringValue", TEST_STRING);
+        properties.put("intValue", String.valueOf(TEST_INT));
+        data.setBeanProperties(properties);
+        BeanDeclarationTestImpl buddyData = new BeanDeclarationTestImpl();
+        Map<String, Object> properties2 = new HashMap<String, Object>();
+        properties2.put("stringValue", "Another test string");
+        properties2.put("intValue", new Integer(100));
+        buddyData.setBeanProperties(properties2);
+        buddyData.setBeanClassName(BeanCreationTestBean.class.getName());
+
+        Map<String, Object> nested = new HashMap<String, Object>();
+        nested.put("buddy", buddyData);
+        data.setNestedBeanDeclarations(nested);
+        return data;
+    }
+
+    /**
+     * Tests the case that no matching constructor is found.
+     */
+    @Test
+    public void testFindMatchingConstructorNoMatch()
+    {
+        BeanDeclarationTestImpl decl = new BeanDeclarationTestImpl();
+        Collection<ConstructorArg> args = new ArrayList<ConstructorArg>();
+        args.add(ConstructorArg.forValue(TEST_STRING, getClass().getName()));
+        decl.setConstructorArgs(args);
+        try
+        {
+            
DefaultBeanFactory.findMatchingConstructor(BeanCreationTestCtorBean.class, 
decl);
+            fail("No exception thrown!");
+        }
+        catch (ConfigurationRuntimeException crex)
+        {
+            String msg = crex.getMessage();
+            assertTrue("Bean class not found:" + msg,
+                    msg.indexOf(BeanCreationTestCtorBean.class.getName()) > 0);
+            assertTrue("Parameter value not found: " + msg,
+                    msg.indexOf(TEST_STRING) > 0);
+            assertTrue("Parameter type not found: " + msg,
+                    msg.indexOf("(" + getClass().getName() + ')') > 0);
+        }
+    }
 }


Reply via email to