craigmcc    02/04/27 16:11:23

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        BeanUtils.java
               beanutils/src/test/org/apache/commons/beanutils
                        BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
  Log:
  Fix BeanUtils.populate() so that you can use mapped property selectors in
  the keys of the map.  Added JUnit tests to verify correct operation for both
  standard JavaBeans and DynaBeans.
  
  Patch Submitted By:  Miguel Zapaton <commonslist at sslmail.de>
  
  Thanks for the patch!
  
  Revision  Changes    Path
  1.21      +8 -5      
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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- BeanUtils.java    8 Apr 2002 17:19:59 -0000       1.20
  +++ BeanUtils.java    27 Apr 2002 23:11:23 -0000      1.21
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
 1.20 2002/04/08 17:19:59 craigmcc Exp $
  - * $Revision: 1.20 $
  - * $Date: 2002/04/08 17:19:59 $
  + * $Header: 
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v
 1.21 2002/04/27 23:11:23 craigmcc Exp $
  + * $Revision: 1.21 $
  + * $Date: 2002/04/27 23:11:23 $
    *
    * ====================================================================
    *
  @@ -87,7 +87,7 @@
    * @author Chris Audley
    * @author Rey Fran�ois
    * @author Gregor Ra�man
  - * @version $Revision: 1.20 $ $Date: 2002/04/08 17:19:59 $
  + * @version $Revision: 1.21 $ $Date: 2002/04/27 23:11:23 $
    */
   
   public class BeanUtils {
  @@ -618,7 +618,10 @@
               } catch (NoSuchMethodException e) {
                   return; // Skip this property setter
               }
  -            if (descriptor instanceof IndexedPropertyDescriptor) {
  +            if (descriptor instanceof MappedPropertyDescriptor) {
  +                type = ((MappedPropertyDescriptor) descriptor).
  +                    getMappedPropertyType();
  +            } else if (descriptor instanceof IndexedPropertyDescriptor) {
                   type = ((IndexedPropertyDescriptor) descriptor).
                       getIndexedPropertyType();
               } else {
  
  
  
  1.9       +38 -4     
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- BeanUtilsTestCase.java    6 Apr 2002 23:31:04 -0000       1.8
  +++ BeanUtilsTestCase.java    27 Apr 2002 23:11:23 -0000      1.9
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
 1.8 2002/04/06 23:31:04 craigmcc Exp $
  - * $Revision: 1.8 $
  - * $Date: 2002/04/06 23:31:04 $
  + * $Header: 
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v
 1.9 2002/04/27 23:11:23 craigmcc Exp $
  + * $Revision: 1.9 $
  + * $Date: 2002/04/27 23:11:23 $
    *
    * ====================================================================
    *
  @@ -95,7 +95,7 @@
    * </ul>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  - * @version $Revision: 1.8 $
  + * @version $Revision: 1.9 $
    */
   
   public class BeanUtilsTestCase extends TestCase {
  @@ -361,6 +361,40 @@
               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() on mapped properties.
  +     */
  +    public void testPopulateMapped() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("mappedProperty(First Key)", "New First Value");
  +            map.put("mappedProperty(Third Key)", "New Third Value");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertEquals("mappedProperty(First Key)",
  +                         "New First Value",
  +                         bean.getMappedProperty("First Key"));
  +            assertEquals("mappedProperty(Second Key)",
  +                         "Second Value",
  +                         bean.getMappedProperty("Second Key"));
  +            assertEquals("mappedProperty(Third Key)",
  +                         "New Third Value",
  +                         bean.getMappedProperty("Third Key"));
  +            assertNull("mappedProperty(Fourth Key",
  +                       bean.getMappedProperty("Fourth Key"));
   
           } catch (IllegalAccessException e) {
               fail("IllegalAccessException");
  
  
  
  1.8       +38 -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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DynaBeanUtilsTestCase.java        6 Apr 2002 23:28:35 -0000       1.7
  +++ DynaBeanUtilsTestCase.java        27 Apr 2002 23:11:23 -0000      1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v
 1.7 2002/04/06 23:28:35 craigmcc Exp $
  - * $Revision: 1.7 $
  - * $Date: 2002/04/06 23:28:35 $
  + * $Header: 
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaBeanUtilsTestCase.java,v
 1.8 2002/04/27 23:11:23 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2002/04/27 23:11:23 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * Test case for BeanUtils when the underlying bean is actually a DynaBean.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.7 $ $Date: 2002/04/06 23:28:35 $
  + * @version $Revision: 1.8 $ $Date: 2002/04/27 23:11:23 $
    */
   
   public class DynaBeanUtilsTestCase extends TestCase {
  @@ -413,6 +413,40 @@
               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() on mapped properties.
  +     */
  +    public void testPopulateMapped() {
  +
  +        try {
  +
  +            HashMap map = new HashMap();
  +            map.put("mappedProperty(First Key)", "New First Value");
  +            map.put("mappedProperty(Third Key)", "New Third Value");
  +
  +            BeanUtils.populate(bean, map);
  +
  +            assertEquals("mappedProperty(First Key)",
  +                         "New First Value",
  +                         (String) bean.get("mappedProperty", "First Key"));
  +            assertEquals("mappedProperty(Second Key)",
  +                         "Second Value",
  +                         (String) bean.get("mappedProperty", "Second Key"));
  +            assertEquals("mappedProperty(Third Key)",
  +                         "New Third Value",
  +                         (String) bean.get("mappedProperty", "Third Key"));
  +            assertNull("mappedProperty(Fourth Key",
  +                       (String) bean.get("mappedProperty", "Fourth Key"));
   
           } catch (IllegalAccessException e) {
               fail("IllegalAccessException");
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to