commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils BeanUtilsTestCase.java DynaBeanUtilsTestCase.java DynaPropertyUtilsTestCase.java PropertyUtilsTestCase.java
Date Sun, 07 Jul 2002 23:08:42 GMT
craigmcc    2002/07/07 16:08:42

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        BeanUtils.java PropertyUtils.java
               beanutils/src/test/org/apache/commons/beanutils
                        BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
                        DynaPropertyUtilsTestCase.java
                        PropertyUtilsTestCase.java
  Log:
  Make BeanUtils.describe() and PropertyUtils.describe() support DynaBeans.
  
  Add unit tests for both describe() methods, against standard JavaBeans and
  against DynaBeans.
  
  PR: Bugzilla #10535 (reported against Struts)
  Submitted by:	Matt Raible <matt at raibledesigns.com>
  
  Revision  Changes    Path
  1.24      +19 -10    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.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- BeanUtils.java	5 Jun 2002 20:46:38 -0000	1.23
  +++ BeanUtils.java	7 Jul 2002 23:08:41 -0000	1.24
  @@ -195,13 +195,22 @@
           //            return (Collections.EMPTY_MAP);
               return (new java.util.HashMap());
           }
  -        PropertyDescriptor descriptors[] =
  -                PropertyUtils.getPropertyDescriptors(bean);
  -        Map description = new HashMap(descriptors.length);
  -        for (int i = 0; i < descriptors.length; i++) {
  -            String name = descriptors[i].getName();
  -            if (descriptors[i].getReadMethod() != null)
  +        Map description = new HashMap();
  +        if (bean instanceof DynaBean) {
  +            DynaProperty descriptors[] =
  +                ((DynaBean) bean).getDynaClass().getDynaProperties();
  +            for (int i = 0; i < descriptors.length; i++) {
  +                String name = descriptors[i].getName();
                   description.put(name, getProperty(bean, name));
  +            }
  +        } else {
  +            PropertyDescriptor descriptors[] =
  +                PropertyUtils.getPropertyDescriptors(bean);
  +            for (int i = 0; i < descriptors.length; i++) {
  +                String name = descriptors[i].getName();
  +                if (descriptors[i].getReadMethod() != null)
  +                    description.put(name, getProperty(bean, name));
  +            }
           }
           return (description);
   
  
  
  
  1.26      +19 -10    jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java
  
  Index: PropertyUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- PropertyUtils.java	15 Jun 2002 20:22:01 -0000	1.25
  +++ PropertyUtils.java	7 Jul 2002 23:08:41 -0000	1.26
  @@ -307,13 +307,22 @@
           if (bean == null) {
               throw new IllegalArgumentException("No bean specified");
           }
  -        PropertyDescriptor descriptors[] =
  -                PropertyUtils.getPropertyDescriptors(bean);
  -        Map description = new HashMap(descriptors.length);
  -        for (int i = 0; i < descriptors.length; i++) {
  -            String name = descriptors[i].getName();
  -            if (descriptors[i].getReadMethod() != null)
  +        Map description = new HashMap();
  +        if (bean instanceof DynaBean) {
  +            DynaProperty descriptors[] =
  +                ((DynaBean) bean).getDynaClass().getDynaProperties();
  +            for (int i = 0; i < descriptors.length; i++) {
  +                String name = descriptors[i].getName();
                   description.put(name, getProperty(bean, name));
  +            }
  +        } else {
  +            PropertyDescriptor descriptors[] =
  +                PropertyUtils.getPropertyDescriptors(bean);
  +            for (int i = 0; i < descriptors.length; i++) {
  +                String name = descriptors[i].getName();
  +                if (descriptors[i].getReadMethod() != null)
  +                    description.put(name, getProperty(bean, name));
  +            }
           }
           return (description);
   
  
  
  
  1.11      +76 -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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BeanUtilsTestCase.java	5 Jun 2002 20:46:38 -0000	1.10
  +++ BeanUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.11
  @@ -64,6 +64,7 @@
   
   import java.lang.reflect.InvocationTargetException;
   import java.util.HashMap;
  +import java.util.Map;
   import junit.framework.TestCase;
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -108,6 +109,31 @@
       protected TestBean bean = null;
   
   
  +    /**
  +     * The set of properties that should be described.
  +     */
  +    protected String describes[] =
  +    { "booleanProperty",
  +      "booleanSecond",
  +      "doubleProperty",
  +      "floatProperty",
  +      "intArray",
  +      //      "intIndexed",
  +      "intProperty",
  +      "listIndexed",
  +      "longProperty",
  +      //      "mappedProperty",
  +      //      "mappedIntProperty",
  +      "nested",
  +      "nullProperty",
  +      "readOnlyProperty",
  +      "shortProperty",
  +      "stringArray",
  +      //      "stringIndexed",
  +      "stringProperty"
  +    };
  +
  +
       // ---------------------------------------------------------- Constructors
   
       /**
  @@ -147,6 +173,52 @@
   
   
       // ------------------------------------------------ Individual Test Methods
  +
  +
  +    /**
  +     * Test the describe() method.
  +     */
  +    public void testDescribe() {
  +
  +        Map map = null;
  +        try {
  +            map = BeanUtils.describe(bean);
  +        } catch (Exception e) {
  +            fail("Threw exception " + e);
  +        }
  +
  +        // Verify existence of all the properties that should be present
  +        for (int i = 0; i < describes.length; i++) {
  +            assertTrue("Property '" + describes[i] + "' is present",
  +                       map.containsKey(describes[i]));
  +        }
  +        assertTrue("Property 'writeOnlyProperty' is not present",
  +                   !map.containsKey("writeOnlyProperty"));
  +
  +        // Verify the values of scalar properties
  +        assertEquals("Value of 'booleanProperty'",
  +                     "true",
  +                     (String) map.get("booleanProperty"));
  +        assertEquals("Value of 'doubleProperty'",
  +                     "321.0",
  +                     (String) map.get("doubleProperty"));
  +        assertEquals("Value of 'floatProperty'",
  +                     "123.0",
  +                     (String) map.get("floatProperty"));
  +        assertEquals("Value of 'intProperty'",
  +                     "123",
  +                     (String) map.get("intProperty"));
  +        assertEquals("Value of 'longProperty'",
  +                     "321",
  +                     (String) map.get("longProperty"));
  +        assertEquals("Value of 'shortProperty'",
  +                     "987",
  +                     (String) map.get("shortProperty"));
  +        assertEquals("Value of 'stringProperty'",
  +                     "This is a string",
  +                     (String) map.get("stringProperty"));
  +
  +    }
   
   
       /**
  
  
  
  1.9       +75 -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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DynaBeanUtilsTestCase.java	27 Apr 2002 23:11:23 -0000	1.8
  +++ DynaBeanUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.9
  @@ -98,6 +98,31 @@
       protected TestBean nested = null;
   
   
  +    /**
  +     * The set of properties that should be described.
  +     */
  +    protected String describes[] =
  +    { "booleanProperty",
  +      "booleanSecond",
  +      "doubleProperty",
  +      "floatProperty",
  +      "intArray",
  +      "intIndexed",
  +      "intProperty",
  +      "listIndexed",
  +      "longProperty",
  +      "mappedProperty",
  +      "mappedIntProperty",
  +      "nested",
  +      "nullProperty",
  +      //      "readOnlyProperty",
  +      "shortProperty",
  +      "stringArray",
  +      "stringIndexed",
  +      "stringProperty"
  +    };
  +
  +
       // ----------------------------------------------------------- Constructors
   
   
  @@ -189,6 +214,52 @@
   
   
       // ------------------------------------------------ Individual Test Methods
  +
  +
  +    /**
  +     * Test the describe() method.
  +     */
  +    public void testDescribe() {
  +
  +        Map map = null;
  +        try {
  +            map = PropertyUtils.describe(bean);
  +        } catch (Exception e) {
  +            fail("Threw exception " + e);
  +        }
  +
  +        // Verify existence of all the properties that should be present
  +        for (int i = 0; i < describes.length; i++) {
  +            assertTrue("Property '" + describes[i] + "' is present",
  +                       map.containsKey(describes[i]));
  +        }
  +        assertTrue("Property 'writeOnlyProperty' is not present",
  +                   !map.containsKey("writeOnlyProperty"));
  +
  +        // Verify the values of scalar properties
  +        assertEquals("Value of 'booleanProperty'",
  +                     Boolean.TRUE,
  +                     (Boolean) map.get("booleanProperty"));
  +        assertEquals("Value of 'doubleProperty'",
  +                     new Double(321.0),
  +                     (Double) map.get("doubleProperty"));
  +        assertEquals("Value of 'floatProperty'",
  +                     new Float((float) 123.0),
  +                     (Float) map.get("floatProperty"));
  +        assertEquals("Value of 'intProperty'",
  +                     new Integer(123),
  +                     (Integer) map.get("intProperty"));
  +        assertEquals("Value of 'longProperty'",
  +                     new Long(321),
  +                     (Long) map.get("longProperty"));
  +        assertEquals("Value of 'shortProperty'",
  +                     new Short((short) 987),
  +                     (Short) map.get("shortProperty"));
  +        assertEquals("Value of 'stringProperty'",
  +                     "This is a string",
  +                     (String) map.get("stringProperty"));
  +
  +    }
   
   
       /**
  
  
  
  1.5       +75 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java
  
  Index: DynaPropertyUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/DynaPropertyUtilsTestCase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DynaPropertyUtilsTestCase.java	23 Jan 2002 22:52:26 -0000	1.4
  +++ DynaPropertyUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.5
  @@ -94,6 +94,31 @@
   
   
       /**
  +     * The set of properties that should be described.
  +     */
  +    protected String describes[] =
  +    { "booleanProperty",
  +      "booleanSecond",
  +      "doubleProperty",
  +      "floatProperty",
  +      "intArray",
  +      "intIndexed",
  +      "intProperty",
  +      "listIndexed",
  +      "longProperty",
  +      "mappedProperty",
  +      "mappedIntProperty",
  +      "nested",
  +      "nullProperty",
  +      //      "readOnlyProperty",
  +      "shortProperty",
  +      "stringArray",
  +      "stringIndexed",
  +      "stringProperty"
  +    };
  +
  +
  +    /**
        * The nested bean pointed at by the "nested" property.
        */
       protected TestBean nested = null;
  @@ -190,6 +215,52 @@
   
   
       // ------------------------------------------------ Individual Test Methods
  +
  +
  +    /**
  +     * Test the describe() method.
  +     */
  +    public void testDescribe() {
  +
  +        Map map = null;
  +        try {
  +            map = PropertyUtils.describe(bean);
  +        } catch (Exception e) {
  +            fail("Threw exception " + e);
  +        }
  +
  +        // Verify existence of all the properties that should be present
  +        for (int i = 0; i < describes.length; i++) {
  +            assertTrue("Property '" + describes[i] + "' is present",
  +                       map.containsKey(describes[i]));
  +        }
  +        assertTrue("Property 'writeOnlyProperty' is not present",
  +                   !map.containsKey("writeOnlyProperty"));
  +
  +        // Verify the values of scalar properties
  +        assertEquals("Value of 'booleanProperty'",
  +                     Boolean.TRUE,
  +                     (Boolean) map.get("booleanProperty"));
  +        assertEquals("Value of 'doubleProperty'",
  +                     new Double(321.0),
  +                     (Double) map.get("doubleProperty"));
  +        assertEquals("Value of 'floatProperty'",
  +                     new Float((float) 123.0),
  +                     (Float) map.get("floatProperty"));
  +        assertEquals("Value of 'intProperty'",
  +                     new Integer(123),
  +                     (Integer) map.get("intProperty"));
  +        assertEquals("Value of 'longProperty'",
  +                     new Long(321),
  +                     (Long) map.get("longProperty"));
  +        assertEquals("Value of 'shortProperty'",
  +                     new Short((short) 987),
  +                     (Short) map.get("shortProperty"));
  +        assertEquals("Value of 'stringProperty'",
  +                     "This is a string",
  +                     (String) map.get("stringProperty"));
  +
  +    }
   
   
       /**
  
  
  
  1.17      +76 -4     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
  
  Index: PropertyUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- PropertyUtilsTestCase.java	15 Jun 2002 20:22:01 -0000	1.16
  +++ PropertyUtilsTestCase.java	7 Jul 2002 23:08:42 -0000	1.17
  @@ -67,6 +67,7 @@
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.util.List;
  +import java.util.Map;
   
   import org.apache.commons.beanutils.priv.PrivateBeanFactory;
   import org.apache.commons.beanutils.priv.PrivateDirect;
  @@ -173,6 +174,31 @@
   
   
       /**
  +     * The set of properties that should be described.
  +     */
  +    protected String describes[] =
  +    { "booleanProperty",
  +      "booleanSecond",
  +      "doubleProperty",
  +      "floatProperty",
  +      "intArray",
  +      //      "intIndexed",
  +      "intProperty",
  +      "listIndexed",
  +      "longProperty",
  +      //      "mappedProperty",
  +      //      "mappedIntProperty",
  +      "nested",
  +      "nullProperty",
  +      "readOnlyProperty",
  +      "shortProperty",
  +      "stringArray",
  +      //      "stringIndexed",
  +      "stringProperty"
  +    };
  +
  +
  +    /**
        * The set of property names we expect to have returned when calling
        * <code>getPropertyDescriptors()</code>.  You should update this list
        * when new properties are added to TestBean.
  @@ -256,6 +282,52 @@
   
   
       // ------------------------------------------------ Individual Test Methods
  +
  +
  +    /**
  +     * Test the describe() method.
  +     */
  +    public void testDescribe() {
  +
  +        Map map = null;
  +        try {
  +            map = BeanUtils.describe(bean);
  +        } catch (Exception e) {
  +            fail("Threw exception " + e);
  +        }
  +
  +        // Verify existence of all the properties that should be present
  +        for (int i = 0; i < describes.length; i++) {
  +            assertTrue("Property '" + describes[i] + "' is present",
  +                       map.containsKey(describes[i]));
  +        }
  +        assertTrue("Property 'writeOnlyProperty' is not present",
  +                   !map.containsKey("writeOnlyProperty"));
  +
  +        // Verify the values of scalar properties
  +        assertEquals("Value of 'booleanProperty'",
  +                     "true",
  +                     (String) map.get("booleanProperty"));
  +        assertEquals("Value of 'doubleProperty'",
  +                     "321.0",
  +                     (String) map.get("doubleProperty"));
  +        assertEquals("Value of 'floatProperty'",
  +                     "123.0",
  +                     (String) map.get("floatProperty"));
  +        assertEquals("Value of 'intProperty'",
  +                     "123",
  +                     (String) map.get("intProperty"));
  +        assertEquals("Value of 'longProperty'",
  +                     "321",
  +                     (String) map.get("longProperty"));
  +        assertEquals("Value of 'shortProperty'",
  +                     "987",
  +                     (String) map.get("shortProperty"));
  +        assertEquals("Value of 'stringProperty'",
  +                     "This is a string",
  +                     (String) map.get("stringProperty"));
  +
  +    }
   
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message