craigmcc 01/05/06 17:32:33
Modified: beanutils/src/java/org/apache/commons/beanutils
PropertyUtils.java
beanutils/src/test/org/apache/commons/beanutils
PropertyUtilsTestCase.java
Added: beanutils/src/test/org/apache/commons/beanutils
TestBeanPackageSubclass.java
TestBeanPublicSubclass.java
Log:
Make the getReadMethod() and getWriteMethod() methods of PropertyUtils
public, because the task they perform is generally useful to apps using
reflection even if they don't use the property getting and setting
functionality.
Add unit tests for the current behavior of getReadMethod() and
getWriteMethod() to establish a baseline of correct behavior. This is in
preparation for fixing Bugzilla bug report #1200 reported against Struts,
where the introspection performed by these methods does not trace down
nested interface "implements" chains.
Revision Changes Path
1.4 +30 -30
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PropertyUtils.java 2001/05/06 22:52:11 1.3
+++ PropertyUtils.java 2001/05/07 00:32:32 1.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.3 2001/05/06 22:52:11 craigmcc Exp $
- * $Revision: 1.3 $
- * $Date: 2001/05/06 22:52:11 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/PropertyUtils.java,v
1.4 2001/05/07 00:32:32 craigmcc Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/05/07 00:32:32 $
*
* ====================================================================
*
@@ -121,7 +121,7 @@
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
- * @version $Revision: 1.3 $ $Date: 2001/05/06 22:52:11 $
+ * @version $Revision: 1.4 $ $Date: 2001/05/07 00:32:32 $
*/
public class PropertyUtils {
@@ -598,6 +598,19 @@
/**
+ * Return an accessible property getter method for this property,
+ * if there is one; otherwise return <code>null</code>.
+ *
+ * @param descriptor Property descriptor to return a getter for
+ */
+ public static Method getReadMethod(PropertyDescriptor descriptor) {
+
+ return (getAccessibleMethod(descriptor.getReadMethod()));
+
+ }
+
+
+ /**
* Return the value of the specified simple property of the specified
* bean, with no type conversions.
*
@@ -644,6 +657,19 @@
/**
+ * Return an accessible property setter method for this property,
+ * if there is one; otherwise return <code>null</code>.
+ *
+ * @param descriptor Property descriptor to return a setter for
+ */
+ public static Method getWriteMethod(PropertyDescriptor descriptor) {
+
+ return (getAccessibleMethod(descriptor.getWriteMethod()));
+
+ }
+
+
+ /**
* Set the value of the specified indexed property of the specified
* bean, with no type conversions. The zero-relative index of the
* required value must be included (in square brackets) as a suffix to
@@ -936,32 +962,6 @@
// We are out of luck
return (null);
-
- }
-
-
- /**
- * Return an accessible property getter method for this property,
- * if there is one; otherwise return <code>null</code>.
- *
- * @param descriptor Property descriptor to return a getter for
- */
- private static Method getReadMethod(PropertyDescriptor descriptor) {
-
- return (getAccessibleMethod(descriptor.getReadMethod()));
-
- }
-
-
- /**
- * Return an accessible property setter method for this property,
- * if there is one; otherwise return <code>null</code>.
- *
- * @param descriptor Property descriptor to return a setter for
- */
- private static Method getWriteMethod(PropertyDescriptor descriptor) {
-
- return (getAccessibleMethod(descriptor.getWriteMethod()));
}
1.4 +182 -5
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PropertyUtilsTestCase.java 2001/04/03 18:21:23 1.3
+++ PropertyUtilsTestCase.java 2001/05/07 00:32:32 1.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
1.3 2001/04/03 18:21:23 craigmcc Exp $
- * $Revision: 1.3 $
- * $Date: 2001/04/03 18:21:23 $
+ * $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java,v
1.4 2001/05/07 00:32:32 craigmcc Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/05/07 00:32:32 $
*
* ====================================================================
*
@@ -88,7 +88,7 @@
* </ul>
*
* @author Craig R. McClanahan
- * @version $Revision: 1.3 $ $Date: 2001/04/03 18:21:23 $
+ * @version $Revision: 1.4 $ $Date: 2001/05/07 00:32:32 $
*/
public class PropertyUtilsTestCase extends TestCase {
@@ -98,12 +98,24 @@
/**
- * The test bean for each test.
+ * The basic test bean for each test.
*/
protected TestBean bean = null;
/**
+ * The "package private subclass" test bean for each test.
+ */
+ protected TestBeanPackageSubclass beanPackageSubclass = null;
+
+
+ /**
+ * The "public subclass" test bean for each test.
+ */
+ protected TestBeanPublicSubclass beanPublicSubclass = null;
+
+
+ /**
* 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.
@@ -152,6 +164,8 @@
public void setUp() {
bean = new TestBean();
+ beanPackageSubclass = new TestBeanPackageSubclass();
+ beanPublicSubclass = new TestBeanPublicSubclass();
}
@@ -172,6 +186,8 @@
public void tearDown() {
bean = null;
+ beanPackageSubclass = null;
+ beanPublicSubclass = null;
}
@@ -595,6 +611,40 @@
/**
+ * Test getting accessible property reader methods for a specified
+ * list of properties of our standard test bean.
+ */
+ public void testGetReadMethodBasic() {
+
+ testGetReadMethod(bean, properties);
+
+ }
+
+
+ /**
+ * Test getting accessible property reader methods for a specified
+ * list of properties of a package private subclass of our standard
+ * test bean.
+ */
+ public void testGetReadMethodPackageSubclass() {
+
+ testGetReadMethod(beanPackageSubclass, properties);
+
+ }
+
+
+ /**
+ * Test getting accessible property reader methods for a specified
+ * list of properties of a public subclass of our standard test bean.
+ */
+ public void testGetReadMethodPublicSubclass() {
+
+ testGetReadMethod(beanPublicSubclass, properties);
+
+ }
+
+
+ /**
* Test getSimpleProperty on a boolean property.
*/
public void testGetSimpleBoolean() {
@@ -905,6 +955,40 @@
/**
+ * Test getting accessible property writer methods for a specified
+ * list of properties of our standard test bean.
+ */
+ public void testGetWriteMethodBasic() {
+
+ testGetWriteMethod(bean, properties);
+
+ }
+
+
+ /**
+ * Test getting accessible property writer methods for a specified
+ * list of properties of a package private subclass of our standard
+ * test bean.
+ */
+ public void testGetWriteMethodPackageSubclass() {
+
+ testGetWriteMethod(beanPackageSubclass, properties);
+
+ }
+
+
+ /**
+ * Test getting accessible property writer methods for a specified
+ * list of properties of a public subclass of our standard test bean.
+ */
+ public void testGetWriteMethodPublicSubclass() {
+
+ testGetWriteMethod(beanPublicSubclass, properties);
+
+ }
+
+
+ /**
* Test setNextedProperty on a boolean property.
*/
public void testSetNestedBoolean() {
@@ -1528,6 +1612,99 @@
fail("InvocationTargetException");
} catch (NoSuchMethodException e) {
fail("NoSuchMethodException");
+ }
+
+ }
+
+
+ /**
+ * Base for testGetReadMethod() series of tests.
+ *
+ * @param bean Bean for which to retrieve read methods.
+ * @param properties Property names to search for
+ */
+ protected void testGetReadMethod(Object bean, String properties[]) {
+
+ PropertyDescriptor pd[] =
+ PropertyUtils.getPropertyDescriptors(bean);
+ for (int i = 0; i < properties.length; i++) {
+
+ // Identify the property descriptor for this property
+ if (properties[i].equals("intIndexed"))
+ continue;
+ if (properties[i].equals("stringIndexed"))
+ continue;
+ if (properties[i].equals("writeOnlyProperty"))
+ continue;
+ int n = -1;
+ for (int j = 0; j < pd.length; j++) {
+ if (properties[i].equals(pd[j].getName())) {
+ n = j;
+ break;
+ }
+ }
+ assert("PropertyDescriptor for " + properties[i],
+ n >= 0);
+
+ // Locate an accessible property reader method for it
+ Method reader = PropertyUtils.getReadMethod(pd[n]);
+ assertNotNull("Reader for " + properties[i],
+ reader);
+ Class clazz = reader.getDeclaringClass();
+ assertNotNull("Declaring class for " + properties[i],
+ clazz);
+ assertEquals("Correct declaring class for " + properties[i],
+ clazz.getName(),
+ "org.apache.commons.beanutils.TestBean");
+
+ }
+
+ }
+
+
+ /**
+ * Base for testGetWriteMethod() series of tests.
+ *
+ * @param bean Bean for which to retrieve write methods.
+ * @param properties Property names to search for
+ */
+ protected void testGetWriteMethod(Object bean, String properties[]) {
+
+
+ PropertyDescriptor pd[] =
+ PropertyUtils.getPropertyDescriptors(bean);
+ for (int i = 0; i < properties.length; i++) {
+
+ // Identify the property descriptor for this property
+ if (properties[i].equals("intIndexed"))
+ continue;
+ if (properties[i].equals("nested"))
+ continue; // This property is read only
+ if (properties[i].equals("readOnlyProperty"))
+ continue;
+ if (properties[i].equals("stringIndexed"))
+ continue;
+ int n = -1;
+ for (int j = 0; j < pd.length; j++) {
+ if (properties[i].equals(pd[j].getName())) {
+ n = j;
+ break;
+ }
+ }
+ assert("PropertyDescriptor for " + properties[i],
+ n >= 0);
+
+ // Locate an accessible property reader method for it
+ Method writer = PropertyUtils.getWriteMethod(pd[n]);
+ assertNotNull("Writer for " + properties[i],
+ writer);
+ Class clazz = writer.getDeclaringClass();
+ assertNotNull("Declaring class for " + properties[i],
+ clazz);
+ assertEquals("Correct declaring class for " + properties[i],
+ clazz.getName(),
+ "org.apache.commons.beanutils.TestBean");
+
}
}
1.1
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBeanPackageSubclass.java
Index: TestBeanPackageSubclass.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBeanPackageSubclass.java,v
1.1 2001/05/07 00:32:33 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2001/05/07 00:32:33 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.beanutils;
/**
* This is a package private subclass of TestBean. All of our properties
* should still be accessible via reflection.
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2001/05/07 00:32:33 $
*/
class TestBeanPackageSubclass extends TestBean {
}
1.1
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBeanPublicSubclass.java
Index: TestBeanPublicSubclass.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/TestBeanPublicSubclass.java,v
1.1 2001/05/07 00:32:33 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2001/05/07 00:32:33 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.beanutils;
/**
* This is a public subclass of TestBean. All of our properties should still
* be accessible via reflection.
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2001/05/07 00:32:33 $
*/
public class TestBeanPublicSubclass extends TestBean {
}