dion 2004/08/30 21:26:51
Modified: jelly/src/test/org/apache/commons/jelly/core
TestUseBeanTag.java
jelly/src/test/org/apache/commons/jelly/core/extension
CoreExtensionTagLibrary.java
jelly/src/java/org/apache/commons/jelly/tags/core
UseBeanTag.java
Added: jelly/src/test/org/apache/commons/jelly/core/extension
UseBeanIgnoreBadProps.java
Log:
JELLY-120. Allow subclasses to ignore bad properties
Revision Changes Path
1.4 +24 -0
jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/TestUseBeanTag.java
Index: TestUseBeanTag.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/TestUseBeanTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestUseBeanTag.java 30 Aug 2004 01:04:28 -0000 1.3
+++ TestUseBeanTag.java 31 Aug 2004 04:26:51 -0000 1.4
@@ -62,4 +62,28 @@
assertNull("name set wrongly", customer.getName());
assertEquals("city not set", "sydney", customer.getCity());
}
+
+ /** Test set a bad property name on a bean, should fail.
+ * @throws Exception
+ */
+ public void testBadProperty() throws Exception {
+ setUpScript("testUseBeanTag.jelly");
+ Script script = getJelly().compileScript();
+ getJellyContext().setVariable("test.badProperty",Boolean.TRUE);
+ script.run(getJellyContext(),getXMLOutput());
+ Exception e = (Exception)getJellyContext().getVariable("ex");
+ assertNotNull("Should have failed to set invalid bean property", e);
+ }
+
+ /** Test set a bad property name on a bean, this should be silently ignored.
+ * @throws Exception
+ */
+ public void testIgnoredBadProperty() throws Exception {
+ setUpScript("testUseBeanTag.jelly");
+ Script script = getJelly().compileScript();
+ getJellyContext().setVariable("test.badPropertyIgnored",Boolean.TRUE);
+ script.run(getJellyContext(),getXMLOutput());
+ Customer customer = (Customer)(getJellyContext().getVariable("foo"));
+ assertNotNull("Should have ignored invalid bean property", customer);
+ }
}
1.2 +1 -0
jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/extension/CoreExtensionTagLibrary.java
Index: CoreExtensionTagLibrary.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/extension/CoreExtensionTagLibrary.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CoreExtensionTagLibrary.java 30 Aug 2004 01:04:12 -0000 1.1
+++ CoreExtensionTagLibrary.java 31 Aug 2004 04:26:51 -0000 1.2
@@ -25,5 +25,6 @@
public CoreExtensionTagLibrary() {
super();
registerTag("useBeanX", UseBeanExtendedTag.class);
+ registerTag("useBeanXP", UseBeanIgnoreBadProps.class);
}
}
1.1
jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/extension/UseBeanIgnoreBadProps.java
Index: UseBeanIgnoreBadProps.java
===================================================================
/*
* Copyright 2002,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jelly.core.extension;
import org.apache.commons.jelly.tags.core.UseBeanTag;
/**
* @author Hans Gilde
*/
public class UseBeanIgnoreBadProps extends UseBeanTag {
public UseBeanIgnoreBadProps() {
setIgnoreUnknownProperties(true);
}
}
1.16 +54 -2
jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
Index: UseBeanTag.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- UseBeanTag.java 30 Aug 2004 01:04:28 -0000 1.15
+++ UseBeanTag.java 31 Aug 2004 04:26:51 -0000 1.16
@@ -18,10 +18,12 @@
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.MapTagSupport;
import org.apache.commons.jelly.MissingAttributeException;
@@ -58,6 +60,15 @@
* Map of attributes before passing to ConvertUtils)
*/
private Set ignoreProperties;
+
+ /**
+ * If this tag finds an attribute in the XML that's not
+ * ignored by [EMAIL PROTECTED] #ignoreProperties} and isn't a
+ * bean property, should it throw an exception?
+ * @see #setIgnoreUnknownProperties(boolean)
+ */
+ private boolean ignoreUnknownProperties = false;
+
public UseBeanTag() {
}
@@ -84,6 +95,7 @@
String var = (String) attributes.get( "var" );
Object classObject = attributes.get( "class" );
addIgnoreProperty("class");
+ addIgnoreProperty("var");
try {
// this method could return null in derived classes
@@ -178,7 +190,9 @@
protected void setBeanProperties(Object bean, Map attributes) throws
JellyTagException {
Map attrsToUse = new HashMap(attributes);
attrsToUse.keySet().removeAll(getIgnorePropertySet());
-
+
+ validateBeanProperties(bean, attrsToUse);
+
try {
BeanUtils.populate(bean, attrsToUse);
} catch (IllegalAccessException e) {
@@ -187,6 +201,25 @@
throw new JellyTagException("could not set the properties of the
bean",e);
}
}
+
+ /**
+ * If [EMAIL PROTECTED] #isIgnoreUnknownProperties()} return true, make sure
that
+ * every non ignored ([EMAIL PROTECTED] #addIgnoreProperty(String)}) property
+ * matches a writable property on the target bean.
+ * @param bean
+ * @param attributes
+ * @throws JellyTagException
+ */
+ protected void validateBeanProperties(Object bean, Map attributes) throws
JellyTagException {
+ if (!isIgnoreUnknownProperties()) {
+ for (Iterator i=attributes.keySet().iterator();i.hasNext();) {
+ String attrName = (String)i.next();
+ if (! PropertyUtils.isWriteable(bean, attrName)) {
+ throw new JellyTagException("No bean property found: " +
attrName);
+ }
+ }
+ }
+ }
/**
* By default this will export the bean using the given variable if it is
defined.
@@ -212,7 +245,8 @@
return defaultClass;
}
- /** Adds a name to the Set of property names that will be skipped when setting
+ /**
+ * Adds a name to the Set of property names that will be skipped when setting
* bean properties. In other words, names added here won't be set into the bean
* if they're present in the attribute Map.
* @param name
@@ -231,5 +265,23 @@
}
return ignoreProperties;
+ }
+
+ /**
+ * @see [EMAIL PROTECTED] #setIgnoreUnknownProperties(boolean)}
+ * @return
+ */
+ public boolean isIgnoreUnknownProperties() {
+ return ignoreUnknownProperties;
+ }
+
+ /**
+ * If this tag finds an attribute in the XML that's not
+ * ignored by [EMAIL PROTECTED] #ignoreProperties} and isn't a
+ * bean property, should it throw an exception?
+ * @param ignoreUnknownProperties Sets [EMAIL PROTECTED]
#ignoreUnknownProperties}.
+ */
+ public void setIgnoreUnknownProperties(boolean ignoreUnknownProps) {
+ this.ignoreUnknownProperties = ignoreUnknownProps;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]