You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/03/11 05:49:53 UTC

cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils BeanUtilsTestCase.java DynaBeanUtilsTestCase.java

craigmcc    02/03/10 20:49:53

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        BeanUtils.java
               beanutils/src/test/org/apache/commons/beanutils
                        BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
  Log:
  Repair and enhance the BeanUtils.populate() method so that it supports the
  following functionality on indexed properties:
  * If the property name is specified in the Map without an index expression,
    the underlying regular property setter (as opposed to the indexed property
    setter) is called to replace the entire array.  This fixes #6125.
  * If the property name is specified in the Map with an index expression,
    the corresponding element of the array is updated through the indexed
    property setter, instead of ignoring this Map entry entirely.
  
  PR: Bugzilla #6125
  Submitted by:	mail at pocketclub.com
  
  Revision  Changes    Path
  1.15      +65 -11    jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java
  
  Index: BeanUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- BeanUtils.java	7 Mar 2002 06:43:17 -0000	1.14
  +++ BeanUtils.java	11 Mar 2002 04:49:53 -0000	1.15
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v 1.14 2002/03/07 06:43:17 martinc Exp $
  - * $Revision: 1.14 $
  - * $Date: 2002/03/07 06:43:17 $
  + * $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v 1.15 2002/03/11 04:49:53 craigmcc Exp $
  + * $Revision: 1.15 $
  + * $Date: 2002/03/11 04:49:53 $
    *
    * ====================================================================
    *
  @@ -86,7 +86,7 @@
    * @author Chris Audley
    * @author Rey Fran�ois
    * @author Gregor Ra�man
  - * @version $Revision: 1.14 $ $Date: 2002/03/07 06:43:17 $
  + * @version $Revision: 1.15 $ $Date: 2002/03/11 04:49:53 $
    */
   
   public class BeanUtils {
  @@ -499,8 +499,17 @@
               DynaProperty dynaProperty = null;
               try {
                   if (bean instanceof DynaBean) {
  -                    dynaProperty =
  -                            ((DynaBean) bean).getDynaClass().getDynaProperty(name);
  +                    String dynaName = name;
  +                    int i = dynaName.indexOf(PropertyUtils.INDEXED_DELIM);
  +                    if (i >= 0) {
  +                        dynaName = dynaName.substring(0, i);
  +                    }
  +                    int j = dynaName.indexOf(PropertyUtils.MAPPED_DELIM);
  +                    if (j >= 0) {
  +                        dynaName = dynaName.substring(0, j);
  +                    }
  +                    DynaClass dynaClass = ((DynaBean) bean).getDynaClass();
  +                    dynaProperty = dynaClass.getDynaProperty(dynaName);
                   } else {
                       descriptor =
                               PropertyUtils.getPropertyDescriptor(bean, name);
  @@ -539,11 +548,13 @@
   
                   // Identify the relevant setter method (if there is one)
                   Method setter = null;
  -                if (descriptor instanceof IndexedPropertyDescriptor)
  +                if ((descriptor instanceof IndexedPropertyDescriptor) &&
  +                    (name.indexOf(PropertyUtils.INDEXED_DELIM) > 0)) {
                       setter = ((IndexedPropertyDescriptor) descriptor).
                               getIndexedWriteMethod();
  -                else if (descriptor instanceof MappedPropertyDescriptor)
  +                } else if (descriptor instanceof MappedPropertyDescriptor) {
                       setter = ((MappedPropertyDescriptor) descriptor).getMappedWriteMethod();
  +                }
   
                   if (setter == null)
                       setter = descriptor.getWriteMethod();
  @@ -609,7 +620,31 @@
                   // Handle scalar and indexed properties differently
                   Object newValue = null;
                   Class type = dynaProperty.getType();
  -                if (type.isArray()) {
  +                String dynaName = name;
  +                int index = -1;
  +                String key = null;
  +                int delim1 = name.indexOf(PropertyUtils.INDEXED_DELIM);
  +                int delim2 = name.indexOf(PropertyUtils.INDEXED_DELIM2);
  +                if (delim1 >= 0) {
  +                    dynaName = name.substring(0, delim1);
  +                    try {
  +                        index =
  +                            Integer.parseInt(name.substring(delim1+1, delim2));
  +                    } catch (NumberFormatException e) {
  +                        ;
  +                    }
  +                }
  +                delim1 = name.indexOf(PropertyUtils.MAPPED_DELIM);
  +                delim2 = name.indexOf(PropertyUtils.MAPPED_DELIM2);
  +                if (delim1 >= 0) {
  +                    dynaName = name.substring(0, delim1);
  +                    try {
  +                        key = name.substring(delim1 + 1, delim2);
  +                    } catch (IndexOutOfBoundsException e) {
  +                        ;
  +                    }
  +                }
  +                if (type.isArray() && (index < 0)) {
                       if (value instanceof String) {
                           String values[] = new String[1];
                           values[0] = (String) value;
  @@ -621,6 +656,18 @@
                       } else {
                           newValue = value;
                       }
  +                } else if (type.isArray() /* && (index >= 0) */ ) {
  +                    if (value instanceof String) {
  +                        newValue =
  +                            ConvertUtils.convert((String) value,
  +                                                 type.getComponentType());
  +                    } else if (value instanceof String[]) {
  +                        newValue =
  +                            ConvertUtils.convert(((String[]) value)[0],
  +                                                 type.getComponentType());
  +                    } else {
  +                        newValue = value;
  +                    }
                   } else {
                       if (value instanceof String) {
                           newValue = ConvertUtils.convert((String) value, type);
  @@ -630,12 +677,19 @@
                       } else {
                           newValue = value;
                       }
  -
                   }
   
                   // Invoke the setter method
                   try {
  -                    PropertyUtils.setProperty(bean, name, newValue);
  +                    if (index >= 0) {
  +                        PropertyUtils.setIndexedProperty(bean, dynaName,
  +                                                         index, newValue);
  +                    } else if (key != null) {
  +                        PropertyUtils.setMappedProperty(bean, dynaName,
  +                                                        key, newValue);
  +                    } else {
  +                        PropertyUtils.setProperty(bean, name, newValue);
  +                    }
                   } catch (NoSuchMethodException e) {
                       log.error("    CANNOT HAPPEN (setProperty())", e);
                   }
  
  
  
  1.6       +142 -5    jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java
  
  Index: BeanUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BeanUtilsTestCase.java	23 Jan 2002 22:52:26 -0000	1.5
  +++ BeanUtilsTestCase.java	11 Mar 2002 04:49:53 -0000	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v 1.5 2002/01/23 22:52:26 sanders Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/01/23 22:52:26 $
  + * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v 1.6 2002/03/11 04:49:53 craigmcc Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/03/11 04:49:53 $
    *
    * ====================================================================
    *
  @@ -63,7 +63,7 @@
   
   
   import java.lang.reflect.InvocationTargetException;
  -
  +import java.util.HashMap;
   import junit.framework.TestCase;
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -95,7 +95,7 @@
    * </ul>
    *
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    */
   
   public class BeanUtilsTestCase extends TestCase {
  @@ -276,5 +276,142 @@
               fail("NoSuchMethodException");
           }
       }
  +
  +    /**
  +     * Test populate() method on individual array elements.
  +     */
  +    public void testPopulateArrayElements() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("intIndexed[0]", "100");
  +            map.put("intIndexed[2]", "120");
  +            map.put("intIndexed[4]", "140");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertEquals("intIndexed[0] is 100",
  +                         100, bean.getIntIndexed(0));
  +            assertEquals("intIndexed[1] is 10",
  +                         10, bean.getIntIndexed(1));
  +            assertEquals("intIndexed[2] is 120",
  +                         120, bean.getIntIndexed(2));
  +            assertEquals("intIndexed[3] is 30",
  +                         30, bean.getIntIndexed(3));
  +            assertEquals("intIndexed[4] is 140",
  +                         140, bean.getIntIndexed(4));
  +
  +            map.clear();
  +            map.put("stringIndexed[1]", "New String 1");
  +            map.put("stringIndexed[3]", "New String 3");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertEquals("stringIndexed[0] is \"String 0\"",
  +                         "String 0", bean.getStringIndexed(0));
  +            assertEquals("stringIndexed[1] is \"New String 1\"",
  +                         "New String 1", bean.getStringIndexed(1));
  +            assertEquals("stringIndexed[2] is \"String 2\"",
  +                         "String 2", bean.getStringIndexed(2));
  +            assertEquals("stringIndexed[3] is \"New String 3\"",
  +                         "New String 3", bean.getStringIndexed(3));
  +            assertEquals("stringIndexed[4] is \"String 4\"",
  +                         "String 4", bean.getStringIndexed(4));
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
  +    }
  +
  +    /**
  +     * Test populate() method on array properties as a whole.
  +     */
  +    public void testPopulateArrayProperties() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            int intArray[] = new int[] { 123, 456, 789 };
  +            map.put("intArray", intArray);
  +            String stringArray[] = new String[]
  +                { "New String 0", "New String 1" };
  +            map.put("stringArray", stringArray);
  +
  +            BeanUtils.populate(bean, map);
  +
  +            intArray = bean.getIntArray();
  +            assertNotNull("intArray is present", intArray);
  +            assertEquals("intArray length",
  +                         3, intArray.length);
  +            assertEquals("intArray[0]", 123, intArray[0]);
  +            assertEquals("intArray[1]", 456, intArray[1]);
  +            assertEquals("intArray[2]", 789, intArray[2]);
  +            stringArray = bean.getStringArray();
  +            assertNotNull("stringArray is present", stringArray);
  +            assertEquals("stringArray length", 2, stringArray.length);
  +            assertEquals("stringArray[0]", "New String 0", stringArray[0]);
  +            assertEquals("stringArray[1]", "New String 1", stringArray[1]);
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
  +    }
  +
  +    /**
  +     * Test populate() method on scalar properties.
  +     */
  +    public void testPopulateScalar() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("booleanProperty", "false");
  +            // booleanSecond is left at true
  +            map.put("doubleProperty", "432.0");
  +            // floatProperty is left at 123.0
  +            map.put("intProperty", "543");
  +            // longProperty is left at 321
  +            map.put("shortProperty", "654");
  +            // stringProperty is left at "This is a string"
  +            map.put("writeOnlyProperty", "New writeOnlyProperty value");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertTrue("booleanProperty is false", !bean.getBooleanProperty());
  +            assertTrue("booleanSecond is true", bean.isBooleanSecond());
  +            assertEquals("doubleProperty is 432.0",
  +                         (double) 432.0, bean.getDoubleProperty(),
  +                         (double) 0.005);
  +            assertEquals("floatProperty is 123.0",
  +                         (float) 123.0, bean.getFloatProperty(),
  +                         (float) 0.005);
  +            assertEquals("intProperty is 543",
  +                         543, bean.getIntProperty());
  +            assertEquals("longProperty is 321",
  +                         (long) 321, bean.getLongProperty());
  +            assertEquals("shortProperty is 654",
  +                         (short) 654, bean.getShortProperty());
  +            assertEquals("stringProperty is \"This is a string\"",
  +                         "This is a string", bean.getStringProperty());
  +            assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"",
  +                         "New writeOnlyProperty value",
  +                         bean.getWriteOnlyPropertyValue());
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
  +    }
  +
  +
   }
   
  
  
  
  1.5       +154 -4    jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java
  
  Index: DynaBeanUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DynaBeanUtilsTestCase.java	23 Jan 2002 22:52:26 -0000	1.4
  +++ DynaBeanUtilsTestCase.java	11 Mar 2002 04:49:53 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v 1.4 2002/01/23 22:52:26 sanders Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/01/23 22:52:26 $
  + * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v 1.5 2002/03/11 04:49:53 craigmcc Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/03/11 04:49:53 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * Test case for BeanUtils when the underlying bean is actually a DynaBean.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.4 $ $Date: 2002/01/23 22:52:26 $
  + * @version $Revision: 1.5 $ $Date: 2002/03/11 04:49:53 $
    */
   
   public class DynaBeanUtilsTestCase extends TestCase {
  @@ -317,6 +317,156 @@
           } catch (NoSuchMethodException e) {
               fail("NoSuchMethodException");
           }
  +    }
  +
  +    /**
  +     * Test populate() method on individual array elements.
  +     */
  +    public void testPopulateArrayElements() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("intIndexed[0]", "100");
  +            map.put("intIndexed[2]", "120");
  +            map.put("intIndexed[4]", "140");
  +
  +            BeanUtils.populate(bean, map);
  +            Integer intIndexed0 = (Integer) bean.get("intIndexed", 0);
  +            assertEquals("intIndexed[0] is 100",
  +                         100, intIndexed0.intValue());
  +            Integer intIndexed1 = (Integer) bean.get("intIndexed", 1);
  +            assertEquals("intIndexed[1] is 10",
  +                         10, intIndexed1.intValue());
  +            Integer intIndexed2 = (Integer) bean.get("intIndexed", 2);
  +            assertEquals("intIndexed[2] is 120",
  +                         120, intIndexed2.intValue());
  +            Integer intIndexed3 = (Integer) bean.get("intIndexed", 3);
  +            assertEquals("intIndexed[3] is 30",
  +                         30, intIndexed3.intValue());
  +            Integer intIndexed4 = (Integer) bean.get("intIndexed", 4);
  +            assertEquals("intIndexed[4] is 140",
  +                         140, intIndexed4.intValue());
  +
  +            map.clear();
  +            map.put("stringIndexed[1]", "New String 1");
  +            map.put("stringIndexed[3]", "New String 3");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertEquals("stringIndexed[0] is \"String 0\"",
  +                         "String 0",
  +                         (String) bean.get("stringIndexed", 0));
  +            assertEquals("stringIndexed[1] is \"New String 1\"",
  +                         "New String 1",
  +                         (String) bean.get("stringIndexed", 1));
  +            assertEquals("stringIndexed[2] is \"String 2\"",
  +                         "String 2",
  +                         (String) bean.get("stringIndexed", 2));
  +            assertEquals("stringIndexed[3] is \"New String 3\"",
  +                         "New String 3",
  +                         (String) bean.get("stringIndexed", 3));
  +            assertEquals("stringIndexed[4] is \"String 4\"",
  +                         "String 4",
  +                         (String) bean.get("stringIndexed", 4));
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
  +    }
  +
  +    /**
  +     * Test populate() method on array properties as a whole.
  +     */
  +    public void testPopulateArrayProperties() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            //            int intArray[] = new int[] { 123, 456, 789 };
  +            String intArrayIn[] = new String[] { "123", "456", "789" };
  +            map.put("intArray", intArrayIn);
  +            String stringArray[] = new String[]
  +                { "New String 0", "New String 1" };
  +            map.put("stringArray", stringArray);
  +
  +            BeanUtils.populate(bean, map);
  +
  +            int intArray[] = (int[]) bean.get("intArray");
  +            assertNotNull("intArray is present", intArray);
  +            assertEquals("intArray length",
  +                         3, intArray.length);
  +            assertEquals("intArray[0]", 123, intArray[0]);
  +            assertEquals("intArray[1]", 456, intArray[1]);
  +            assertEquals("intArray[2]", 789, intArray[2]);
  +            stringArray = (String[]) bean.get("stringArray");
  +            assertNotNull("stringArray is present", stringArray);
  +            assertEquals("stringArray length", 2, stringArray.length);
  +            assertEquals("stringArray[0]", "New String 0", stringArray[0]);
  +            assertEquals("stringArray[1]", "New String 1", stringArray[1]);
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
  +    }
  +
  +    /**
  +     * Test populate() method on scalar properties.
  +     */
  +    public void testPopulateScalar() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("booleanProperty", "false");
  +            // booleanSecond is left at true
  +            map.put("doubleProperty", "432.0");
  +            // floatProperty is left at 123.0
  +            map.put("intProperty", "543");
  +            // longProperty is left at 321
  +            map.put("shortProperty", "654");
  +            // stringProperty is left at "This is a string"
  +            map.put("writeOnlyProperty", "New writeOnlyProperty value");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            Boolean booleanProperty = (Boolean) bean.get("booleanProperty");
  +            assertTrue("booleanProperty is false", !booleanProperty.booleanValue());
  +            Boolean booleanSecond = (Boolean) bean.get("booleanSecond");
  +            assertTrue("booleanSecond is true", booleanSecond.booleanValue());
  +            Double doubleProperty = (Double) bean.get("doubleProperty");
  +            assertEquals("doubleProperty is 432.0",
  +                         (double) 432.0, doubleProperty.doubleValue(),
  +                         (double) 0.005);
  +            Float floatProperty = (Float) bean.get("floatProperty");
  +            assertEquals("floatProperty is 123.0",
  +                         (float) 123.0, floatProperty.floatValue(),
  +                         (float) 0.005);
  +            Integer intProperty = (Integer) bean.get("intProperty");
  +            assertEquals("intProperty is 543",
  +                         543, intProperty.intValue());
  +            Long longProperty = (Long) bean.get("longProperty");
  +            assertEquals("longProperty is 321",
  +                         (long) 321, longProperty.longValue());
  +            Short shortProperty = (Short) bean.get("shortProperty");
  +            assertEquals("shortProperty is 654",
  +                         (short) 654, shortProperty.shortValue());
  +            assertEquals("stringProperty is \"This is a string\"",
  +                         "This is a string",
  +                         (String) bean.get("stringProperty"));
  +
  +        } catch (IllegalAccessException e) {
  +            fail("IllegalAccessException");
  +        } catch (InvocationTargetException e) {
  +            fail("InvocationTargetException");
  +        }
  +
       }
   
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>