craigmcc 2003/01/03 12:32:36
Modified: beanutils/src/java/org/apache/commons/beanutils
BeanUtils.java ResultSetDynaClass.java
beanutils/src/test/org/apache/commons/beanutils
BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
Log:
Check for null "new" values in setProperty() appropriately, to avoid NPEs.
PR: Bugzilla #15773
Submitted by: Ben Tomasini <btomasini at neteverything.com>
Revision Changes Path
1.33 +11 -6
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.32
retrieving revision 1.33
diff -u -r1.32 -r1.33
--- BeanUtils.java 21 Dec 2002 19:33:19 -0000 1.32
+++ BeanUtils.java 3 Jan 2003 20:32:35 -0000 1.33
@@ -880,7 +880,12 @@
// Convert the specified value to the required type
Object newValue = null;
if (type.isArray() && (index < 0)) { // Scalar value into array
- if (value instanceof String) {
+ log.debug("CONVERTING SCALAR '" + value + "' TO ARRAY");
+ if (value == null) {
+ String values[] = new String[1];
+ values[0] = (String) value;
+ newValue = ConvertUtils.convert((String[]) values, type);
+ } else if (value instanceof String) {
String values[] = new String[1];
values[0] = (String) value;
newValue = ConvertUtils.convert((String[]) values, type);
@@ -900,7 +905,7 @@
newValue = value;
}
} else { // Value into scalar
- if (value instanceof String || (value == null && type.isPrimitive())) {
+ if ((value instanceof String) || (value == null)) {
newValue = ConvertUtils.convert((String) value, type);
} else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[]) value)[0],
1.8 +8 -8
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java
Index: ResultSetDynaClass.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ResultSetDynaClass.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ResultSetDynaClass.java 16 Dec 2002 01:53:20 -0000 1.7
+++ ResultSetDynaClass.java 3 Jan 2003 20:32:35 -0000 1.8
@@ -87,8 +87,8 @@
* </pre>
*
* <p>Each column in the result set will be represented as a DynaBean
- * property of the corresponding name (forced to lower case for portability).
- * </p>
+ * property of the corresponding name (optionally forced to lower case
+ * for portability).</p>
*
* <p><strong>WARNING</strong> - Any {@link DynaBean} instance returned by
* this class, or from the <code>Iterator</code> returned by the
@@ -142,7 +142,7 @@
* @param resultSet The result set to be wrapped
*
* @exception NullPointerException if <code>resultSet</code>
- * is <code>false</code>
+ * is <code>null</code>
* @exception SQLException if the metadata for this result set
* cannot be introspected
*/
@@ -170,7 +170,7 @@
* @param lowerCase Should property names be lower cased?
*
* @exception NullPointerException if <code>resultSet</code>
- * is <code>false</code>
+ * is <code>null</code>
* @exception SQLException if the metadata for this result set
* cannot be introspected
*/
1.17 +46 -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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- BeanUtilsTestCase.java 21 Dec 2002 19:33:20 -0000 1.16
+++ BeanUtilsTestCase.java 3 Jan 2003 20:32:35 -0000 1.17
@@ -783,6 +783,48 @@
/**
+ * Test calling setProperty() with null property values.
+ */
+ public void testSetPropertyNullValues() throws Exception {
+
+ Object oldValue = null;
+ Object newValue = null;
+
+ // Scalar value into array
+ oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ BeanUtils.setProperty(bean, "stringArray", (String) null);
+ newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ assertNotNull("stringArray is not null", newValue);
+ assertTrue("stringArray of correct type",
+ newValue instanceof String[]);
+ assertEquals("stringArray length",
+ 1, ((String[]) newValue).length);
+ assertTrue("stringArray[0] is null",
+ ((String[]) newValue)[0] == null);
+ PropertyUtils.setProperty(bean, "stringArray", oldValue);
+
+ // Indexed value into array
+ oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
+ newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ assertNotNull("stringArray is not null", newValue);
+ assertTrue("stringArray of correct type",
+ newValue instanceof String[]);
+ assertEquals("stringArray length",
+ 5, ((String[]) newValue).length);
+ assertTrue("stringArray[2] is null",
+ ((String[]) newValue)[2] == null);
+ PropertyUtils.setProperty(bean, "stringArray", oldValue);
+
+ // Value into scalar
+ BeanUtils.setProperty(bean, "stringProperty", null);
+ assertTrue("stringProperty is now null",
+ BeanUtils.getProperty(bean, "stringProperty") == null);
+
+ }
+
+
+ /**
* Test converting to and from primitive wrapper types.
*/
public void testSetPropertyOnPrimitiveWrappers() throws Exception {
1.14 +46 -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.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- DynaBeanUtilsTestCase.java 21 Dec 2002 19:33:20 -0000 1.13
+++ DynaBeanUtilsTestCase.java 3 Jan 2003 20:32:35 -0000 1.14
@@ -833,6 +833,48 @@
/**
+ * Test calling setProperty() with null property values.
+ */
+ public void testSetPropertyNullValues() throws Exception {
+
+ Object oldValue = null;
+ Object newValue = null;
+
+ // Scalar value into array
+ oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ BeanUtils.setProperty(bean, "stringArray", (String) null);
+ newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ assertNotNull("stringArray is not null", newValue);
+ assertTrue("stringArray of correct type",
+ newValue instanceof String[]);
+ assertEquals("stringArray length",
+ 1, ((String[]) newValue).length);
+ assertTrue("stringArray[0] is null",
+ ((String[]) newValue)[0] == null);
+ PropertyUtils.setProperty(bean, "stringArray", oldValue);
+
+ // Indexed value into array
+ oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
+ newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
+ assertNotNull("stringArray is not null", newValue);
+ assertTrue("stringArray of correct type",
+ newValue instanceof String[]);
+ assertEquals("stringArray length",
+ 5, ((String[]) newValue).length);
+ assertTrue("stringArray[2] is null",
+ ((String[]) newValue)[2] == null);
+ PropertyUtils.setProperty(bean, "stringArray", oldValue);
+
+ // Value into scalar
+ BeanUtils.setProperty(bean, "stringProperty", null);
+ assertTrue("stringProperty is now null",
+ BeanUtils.getProperty(bean, "stringProperty") == null);
+
+ }
+
+
+ /**
* Test converting to and from primitive wrapper types.
*/
public void testSetPropertyOnPrimitiveWrappers() throws Exception {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>