craigmcc 2002/12/21 11:33:20
Modified: beanutils/src/java/org/apache/commons/beanutils
BeanUtils.java
beanutils/src/test/org/apache/commons/beanutils
BeanUtilsTestCase.java DynaBeanUtilsTestCase.java
Log:
Enhance the behavior of BeanUtils.setProperty() such that, if the destination
property is a String and there is a registered Converter for the value's
class, the value's toString() method will be called, followed by conversion
to the appropriate destination type. Among other things, this makes it
possible to pass in a primitive wrapper type (such as Integer) and have it
converted to a String.
PR: Bugzilla #15170
Submitted by: Runako Godfrey <rg at onepercentsoftware.com>
Revision Changes Path
1.32 +6 -4
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.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- BeanUtils.java 16 Dec 2002 01:31:29 -0000 1.31
+++ BeanUtils.java 21 Dec 2002 19:33:19 -0000 1.32
@@ -905,6 +905,8 @@
} else if (value instanceof String[]) {
newValue = ConvertUtils.convert(((String[]) value)[0],
type);
+ } else if (ConvertUtils.lookup(value.getClass()) != null) {
+ newValue = ConvertUtils.convert(value.toString(), type);
} else {
newValue = value;
}
1.16 +14 -6
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.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- BeanUtilsTestCase.java 9 Dec 2002 22:17:12 -0000 1.15
+++ BeanUtilsTestCase.java 21 Dec 2002 19:33:20 -0000 1.16
@@ -782,10 +782,18 @@
}
- /** See http://issues.apache.org/bugzilla/show_bug.cgi?id=15170 */
- public void testSetPropertyOnPrimitavieWrappers() throws Exception {
+ /**
+ * Test converting to and from primitive wrapper types.
+ */
+ public void testSetPropertyOnPrimitiveWrappers() throws Exception {
+
BeanUtils.setProperty(bean,"intProperty", new Integer(1));
assertEquals(1,bean.getIntProperty());
+ BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
+ assertEquals(1, Integer.parseInt(bean.getStringProperty()));
+
}
+
+
}
1.13 +17 -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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- DynaBeanUtilsTestCase.java 21 Jul 2002 00:20:45 -0000 1.12
+++ DynaBeanUtilsTestCase.java 21 Dec 2002 19:33:20 -0000 1.13
@@ -832,6 +832,19 @@
}
+ /**
+ * Test converting to and from primitive wrapper types.
+ */
+ public void testSetPropertyOnPrimitiveWrappers() throws Exception {
+
+ BeanUtils.setProperty(bean,"intProperty", new Integer(1));
+ assertEquals(1,((Integer) bean.get("intProperty")).intValue());
+ BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
+ assertEquals(1, Integer.parseInt((String) bean.get("stringProperty")));
+
+ }
+
+
// ------------------------------------------------------ Protected Methods
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>