craigmcc 2003/02/04 17:47:42
Modified: digester/src/java/org/apache/commons/digester
BeanPropertySetterRule.java SetPropertyRule.java
digester/src/test/org/apache/commons/digester
BeanPropertySetterRuleTestCase.java
SetPropertyRuleTestCase.java SimpleTestBean.java
Log:
Make BeanPropertySetterRule and SetPropertyRule continue to report an
exception on an invalid property name (as the previous patch did), but
ensure that they can still set a write-only property (as the previous
patch broke). Add unit tests to protect from future regressions on
this issue.
PR: Bugzilla #16233 (fixed correctly this time :-)
Submitted by: Howard Miller <me at howardmiller.co.uk>
Revision Changes Path
1.11 +25 -5
jakarta-commons/digester/src/java/org/apache/commons/digester/BeanPropertySetterRule.java
Index: BeanPropertySetterRule.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/BeanPropertySetterRule.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- BeanPropertySetterRule.java 18 Jan 2003 18:47:08 -0000 1.10
+++ BeanPropertySetterRule.java 5 Feb 2003 01:47:42 -0000 1.11
@@ -63,9 +63,12 @@
package org.apache.commons.digester;
+import java.beans.PropertyDescriptor;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.beanutils.DynaBean;
+import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
@@ -196,6 +199,9 @@
* no namespace
* @param name the local name if the parser is namespace aware, or just
* the element name otherwise
+ *
+ * @exception NoSuchMethodException if the bean does not
+ * have a writeable property of the specified name
*/
public void end(String namespace, String name) throws Exception {
@@ -219,7 +225,21 @@
// Force an exception if the property does not exist
// (BeanUtils.setProperty() silently returns in this case)
- PropertyUtils.getProperty(top, property);
+ if (top instanceof DynaBean) {
+ DynaProperty desc =
+ ((DynaBean) top).getDynaClass().getDynaProperty(property);
+ if (desc == null) {
+ throw new NoSuchMethodException
+ ("Bean has no property named " + property);
+ }
+ } else /* this is a standard JavaBean */ {
+ PropertyDescriptor desc =
+ PropertyUtils.getPropertyDescriptor(top, property);
+ if (desc == null) {
+ throw new NoSuchMethodException
+ ("Bean has no property named " + property);
+ }
+ }
// Set the property (with conversion as necessary)
BeanUtils.setProperty(top, property, bodyText);
1.10 +26 -6
jakarta-commons/digester/src/java/org/apache/commons/digester/SetPropertyRule.java
Index: SetPropertyRule.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/SetPropertyRule.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- SetPropertyRule.java 18 Jan 2003 18:47:08 -0000 1.9
+++ SetPropertyRule.java 5 Feb 2003 01:47:42 -0000 1.10
@@ -63,10 +63,13 @@
package org.apache.commons.digester;
+import java.beans.PropertyDescriptor;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.beanutils.DynaBean;
+import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
@@ -142,6 +145,9 @@
*
* @param context The associated context
* @param attributes The attribute list of this element
+ *
+ * @exception NoSuchMethodException if the bean does not
+ * have a writeable property of the specified name
*/
public void begin(Attributes attributes) throws Exception {
@@ -172,8 +178,22 @@
}
// Force an exception if the property does not exist
- // (BeanUtils.setProperty() sildently returns in this case)
- PropertyUtils.getProperty(top, actualName);
+ // (BeanUtils.setProperty() silently returns in this case)
+ if (top instanceof DynaBean) {
+ DynaProperty desc =
+ ((DynaBean) top).getDynaClass().getDynaProperty(actualName);
+ if (desc == null) {
+ throw new NoSuchMethodException
+ ("Bean has no property named " + actualName);
+ }
+ } else /* this is a standard JavaBean */ {
+ PropertyDescriptor desc =
+ PropertyUtils.getPropertyDescriptor(top, actualName);
+ if (desc == null) {
+ throw new NoSuchMethodException
+ ("Bean has no property named " + actualName);
+ }
+ }
// Set the property (with conversion as necessary)
BeanUtils.setProperty(top, actualName, actualValue);
1.9 +16 -2
jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java
Index: BeanPropertySetterRuleTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/BeanPropertySetterRuleTestCase.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- BeanPropertySetterRuleTestCase.java 18 Jan 2003 18:47:08 -0000 1.8
+++ BeanPropertySetterRuleTestCase.java 5 Feb 2003 01:47:42 -0000 1.9
@@ -88,8 +88,13 @@
* Simple test xml document used in the tests.
*/
protected final static String TEST_XML =
- "<?xml version='1.0'?><root>ROOT BODY<alpha>ALPHA BODY</alpha>" +
- "<beta>BETA BODY</beta><gamma>GAMMA BODY</gamma></root>";
+ "<?xml version='1.0'?>" +
+ "<root>ROOT BODY" +
+ "<alpha>ALPHA BODY</alpha>" +
+ "<beta>BETA BODY</beta>" +
+ "<gamma>GAMMA BODY</gamma>" +
+ "<delta>DELTA BODY</delta>" +
+ "</root>";
/**
@@ -308,6 +313,9 @@
// we'll leave property gamma alone
+ // we'll set property delta (a write-only property) also
+ digester.addRule("root/delta", new BeanPropertySetterRule("delta"));
+
SimpleTestBean bean = (SimpleTestBean) digester.parse(xmlTestReader());
// check properties are set correctly
@@ -325,7 +333,13 @@
"Property gamma not set correctly",
bean.getGamma() == null);
+ assertEquals("Property delta not set correctly",
+ "DELTA BODY",
+ bean.getDeltaValue());
+
+
}
+
/**
* Test that trying to set an unknown property throws an exception.
1.2 +4 -0
jakarta-commons/digester/src/test/org/apache/commons/digester/SetPropertyRuleTestCase.java
Index: SetPropertyRuleTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/SetPropertyRuleTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SetPropertyRuleTestCase.java 18 Jan 2003 18:47:08 -0000 1.1
+++ SetPropertyRuleTestCase.java 5 Feb 2003 01:47:42 -0000 1.2
@@ -89,6 +89,7 @@
"<?xml version='1.0'?><root>" +
"<set name='alpha' value='ALPHA VALUE'/>" +
"<set name='beta' value='BETA VALUE'/>" +
+ "<set name='delta' value='DELTA VALUE'/>" +
"</root>";
/**
@@ -181,6 +182,9 @@
bean.getBeta());
assertNull("gamma property not set",
bean.getGamma());
+ assertEquals("delta property set",
+ "DELTA VALUE",
+ bean.getDeltaValue());
}
1.5 +12 -0
jakarta-commons/digester/src/test/org/apache/commons/digester/SimpleTestBean.java
Index: SimpleTestBean.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/SimpleTestBean.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SimpleTestBean.java 2 Feb 2003 15:52:14 -0000 1.4
+++ SimpleTestBean.java 5 Feb 2003 01:47:42 -0000 1.5
@@ -70,6 +70,8 @@
private String gamma;
+ private String delta;
+
public String getAlpha() {
return alpha;
}
@@ -94,6 +96,14 @@
this.gamma = gamma;
}
+ public String getDeltaValue() { // Retrieves "write only" value
+ return delta;
+ }
+
+ public void setDelta(String delta) { // "delta" is a write-only property
+ this.delta = delta;
+ }
+
public String toString() {
StringBuffer sb = new StringBuffer("[SimpleTestBean]");
sb.append(" alpha=");
@@ -102,6 +112,8 @@
sb.append(beta);
sb.append(" gamma=");
sb.append(gamma);
+ sb.append(" delta=");
+ sb.append(delta);
return sb.toString();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]