You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/08/30 03:04:28 UTC

cvs commit: jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core UseBeanTag.java

dion        2004/08/29 18:04:28

  Modified:    jelly/src/test/org/apache/commons/jelly/core
                        TestUseBeanTag.java
               jelly/src/java/org/apache/commons/jelly/tags/core
                        UseBeanTag.java
  Log:
  JELLY-120.
  
  Revision  Changes    Path
  1.3       +18 -2     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestUseBeanTag.java	30 Aug 2004 00:41:46 -0000	1.2
  +++ TestUseBeanTag.java	30 Aug 2004 01:04:28 -0000	1.3
  @@ -15,9 +15,10 @@
    */
   package org.apache.commons.jelly.core;
   
  -import org.apache.commons.jelly.Script;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.jelly.Script;
  +
   /**
    * Tests for UseBean tag
    */
  @@ -35,7 +36,7 @@
        * Test a simple useBean tag works ok
        * @throws Exception
        */
  -    public void testSimple() throws Exception {
  +    public void testSimple() throws Exception{
           setUpScript("testUseBeanTag.jelly");
           Script script = getJelly().compileScript();
           getJellyContext().setVariable("test.simple",Boolean.TRUE);
  @@ -44,6 +45,21 @@
           assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
           Customer customer = (Customer)(getJellyContext().getVariable("foo"));
           assertEquals("name not set", "testing", customer.getName());
  +        assertEquals("city not set", "sydney", customer.getCity());
  +    }
  +    
  +    /**
  +     * test extension
  +     */
  +    public void testExtension() throws Exception {
  +        setUpScript("testUseBeanTag.jelly");
  +        Script script = getJelly().compileScript();
  +        getJellyContext().setVariable("test.extension",Boolean.TRUE);
  +        script.run(getJellyContext(),getXMLOutput());
  +        assertNotNull(getJellyContext().getVariable("foo"));
  +        assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
  +        Customer customer = (Customer)(getJellyContext().getVariable("foo"));
  +        assertNull("name set wrongly", customer.getName());
           assertEquals("city not set", "sydney", customer.getCity());
       }
   }
  
  
  
  1.15      +39 -4     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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- UseBeanTag.java	24 Feb 2004 14:10:38 -0000	1.14
  +++ UseBeanTag.java	30 Aug 2004 01:04:28 -0000	1.15
  @@ -16,13 +16,15 @@
   package org.apache.commons.jelly.tags.core;
   
   import java.lang.reflect.InvocationTargetException;
  +import java.util.HashMap;
  +import java.util.HashSet;
   import java.util.Map;
  +import java.util.Set;
   
   import org.apache.commons.beanutils.BeanUtils;
  -
   import org.apache.commons.jelly.JellyTagException;
  -import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.MapTagSupport;
  +import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
   import org.apache.commons.jelly.impl.BeanSource;
   
  @@ -51,6 +53,12 @@
       /** the default class to use if no Class is specified */
       private Class defaultClass;
   
  +    /**
  +     * a Set of Strings of property names to ignore (remove from the
  +     * Map of attributes before passing to ConvertUtils)
  +     */
  +    private Set ignoreProperties;
  +
       public UseBeanTag() {
       }
   
  @@ -74,7 +82,8 @@
       public void doTag(XMLOutput output) throws JellyTagException {
           Map attributes = getAttributes();
           String var = (String) attributes.get( "var" );
  -        Object classObject = attributes.remove( "class" );
  +        Object classObject = attributes.get( "class" );
  +        addIgnoreProperty("class");
           
           try {
               // this method could return null in derived classes
  @@ -163,10 +172,15 @@
       /**
        * Sets the properties on the bean. Derived tags could implement some custom 
        * type conversion etc.
  +     * <p/>
  +     * This method ignores all property names in the Set returned by {@link #getIgnorePropertySet()}.
        */
       protected void setBeanProperties(Object bean, Map attributes) throws JellyTagException {
  +        Map attrsToUse = new HashMap(attributes);
  +        attrsToUse.keySet().removeAll(getIgnorePropertySet());
  +
           try {
  -            BeanUtils.populate(bean, attributes);
  +            BeanUtils.populate(bean, attrsToUse);
           } catch (IllegalAccessException e) {
               throw new JellyTagException("could not set the properties of the bean",e);
           } catch (InvocationTargetException e) {
  @@ -196,5 +210,26 @@
        */
       protected Class getDefaultClass() {
           return defaultClass;
  +    }
  +
  +    /** 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
  +     */
  +    protected void addIgnoreProperty(String name) {
  +        getIgnorePropertySet().add(name);
  +    }
  +
  +    /**
  +     * @return the Set of property names that should be ignored when setting the
  +     * properties of the bean.
  +     */
  +    protected Set getIgnorePropertySet() {
  +        if (ignoreProperties == null) {
  +            ignoreProperties = new HashSet();
  +        }
  +
  +        return ignoreProperties;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org