commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester ParamBean.java CallMethodRuleTestCase.java NamedBean.java
Date Thu, 17 Apr 2003 11:08:17 GMT
rdonkin     2003/04/17 04:08:17

  Modified:    digester/src/java/org/apache/commons/digester
                        CallParamRule.java
               digester/src/test/org/apache/commons/digester
                        CallMethodRuleTestCase.java NamedBean.java
  Added:       digester/src/test/org/apache/commons/digester ParamBean.java
  Log:
  Fixed bug in nested body texts introduced after digester 1.3. Thanks to Joel Gautschi for
help in tracking this one down.
  
  Revision  Changes    Path
  1.13      +29 -6     jakarta-commons/digester/src/java/org/apache/commons/digester/CallParamRule.java
  
  Index: CallParamRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallParamRule.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- CallParamRule.java	16 Apr 2003 11:23:50 -0000	1.12
  +++ CallParamRule.java	17 Apr 2003 11:08:16 -0000	1.13
  @@ -65,6 +65,8 @@
   
   import org.xml.sax.Attributes;
   
  +import org.apache.commons.collections.ArrayStack;
  +
   
   /**
    * <p>Rule implementation that saves a parameter for use by a surrounding 
  @@ -208,6 +210,11 @@
        */
       protected int stackIndex = 0;
   
  +    /** 
  +     * Stack is used to allow nested body text to be processed.
  +     * Lazy creation.
  +     */
  +    protected ArrayStack bodyTextStack;
   
       // --------------------------------------------------------- Public Methods
   
  @@ -260,10 +267,26 @@
       public void body(String bodyText) throws Exception {
   
           if (attributeName == null && !fromStack) {
  -            Object parameters[] = (Object[]) digester.peekParams();
  -            parameters[paramIndex] = bodyText.trim();
  +            // We must wait to set the parameter until end
  +            // so that we can make sure that the right set of parameters
  +            // is at the top of the stack
  +            if (bodyTextStack == null) {
  +                bodyTextStack = new ArrayStack();
  +            }
  +            bodyTextStack.push(bodyText.trim());
           }
   
  +    }
  +    
  +    /**
  +     * Process any body texts now.
  +     */
  +    public void end(String namespace, String name) {
  +        if (bodyTextStack != null && !bodyTextStack.empty()) {
  +            // what we do now is push one parameter onto the top set of parameters
  +            Object parameters[] = (Object[]) digester.peekParams();
  +            parameters[paramIndex] = bodyTextStack.pop();
  +        }
       }
   
       /**
  
  
  
  1.7       +96 -0     jakarta-commons/digester/src/test/org/apache/commons/digester/CallMethodRuleTestCase.java
  
  Index: CallMethodRuleTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/CallMethodRuleTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CallMethodRuleTestCase.java	16 Apr 2003 11:23:49 -0000	1.6
  +++ CallMethodRuleTestCase.java	17 Apr 2003 11:08:16 -0000	1.7
  @@ -72,6 +72,7 @@
   
   import org.xml.sax.SAXException;
   
  +//import org.apache.commons.logging.impl.SimpleLog;
   
   /**
    * <p>Tests for the <code>CallMethodRule</code> and associated 
  @@ -431,6 +432,101 @@
           assertEquals("Out of stack not set to null", null , bean.getName());
       }
       
  +    public void testTwoCalls() throws Exception {
  +        
  +    
  +        StringReader reader = new StringReader(
  +            "<?xml version='1.0' ?><root>"
  +            + "<param class='int' coolness='true'>25</param>"
  +            + "<param class='long'>50</param>"
  +            + "<param class='float' coolness='false'>90</param></root>");
  +            
  +        Digester digester = new Digester();
  +        //SimpleLog log = new SimpleLog("{testTwoCalls:Digester]");
  +        //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  +        //digester.setLogger(log);
  +        
  +        digester.addObjectCreate( "root/param", ParamBean.class );
  +        digester.addSetNext( "root/param", "add" );
  +        digester.addCallMethod( "root/param", "setThisAndThat", 2 );
  +        digester.addCallParam( "root/param", 0, "class" );
  +        digester.addCallParam( "root/param", 1 );
  +        digester.addCallMethod( "root/param", "setCool", 1, new Class[] {boolean.class
} );
  +        digester.addCallParam( "root/param", 0, "coolness" );
  +        
  +        ArrayList list = new ArrayList();
  +        digester.push(list);
  +        digester.parse(reader);
  +    
  +        assertEquals("Wrong number of objects created", 3, list.size());
  +        ParamBean bean = (ParamBean) list.get(0);
  +        assertEquals("Coolness wrong (1)", true, bean.isCool());
  +        assertEquals("This wrong (1)", "int", bean.getThis());
  +        assertEquals("That wrong (1)", "25", bean.getThat());
  +        bean = (ParamBean) list.get(1);
  +        assertEquals("Coolness wrong (2)", false, bean.isCool());
  +        assertEquals("This wrong (2)", "long", bean.getThis());
  +        assertEquals("That wrong (2)", "50", bean.getThat());
  +        bean = (ParamBean) list.get(2);
  +        assertEquals("Coolness wrong (3)", false, bean.isCool());
  +        assertEquals("This wrong (3)", "float", bean.getThis());
  +        assertEquals("That wrong (3)", "90", bean.getThat());
  +    }
  +
  +    public void testNestedBody() throws Exception {
  +        
  +        StringReader reader = new StringReader(
  +            "<?xml version='1.0' ?><root>"
  +            + "<spam>Simple</spam>"
  +            + "<spam>Complex<spam>Deep<spam>Deeper<spam>Deepest</spam></spam></spam></spam>"
  +            + "</root>");
  +            
  +        Digester digester = new Digester();        
  +
  +        //SimpleLog log = new SimpleLog("[testPrimitiveReading:Digester]");
  +        //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
  +        //digester.setLogger(log);
  +        
  +        
  +        digester.addObjectCreate("root/spam", NamedBean.class);
  +        digester.addSetRoot("root/spam", "add");
  +        digester.addCallMethod( "root/spam", "setName", 1 );
  +        digester.addCallParam( "root/spam", 0);
  +        
  +        digester.addObjectCreate("root/spam/spam", NamedBean.class);
  +        digester.addSetRoot("root/spam/spam", "add");
  +        digester.addCallMethod( "root/spam/spam", "setName", 1 );
  +        digester.addCallParam( "root/spam/spam", 0);        
  +        
  +        digester.addObjectCreate("root/spam/spam/spam", NamedBean.class);
  +        digester.addSetRoot("root/spam/spam/spam", "add");
  +        digester.addCallMethod( "root/spam/spam/spam", "setName", 1 );
  +        digester.addCallParam( "root/spam/spam/spam", 0);      
  +
  +        
  +        digester.addObjectCreate("root/spam/spam/spam/spam", NamedBean.class);
  +        digester.addSetRoot("root/spam/spam/spam/spam", "add");
  +        digester.addCallMethod( "root/spam/spam/spam/spam", "setName", 1 );
  +        digester.addCallParam( "root/spam/spam/spam/spam", 0);   
  +        
  +        ArrayList list = new ArrayList();
  +        digester.push(list);
  +        digester.parse(reader);
  +        
  +        System.out.println(list);
  +        
  +        NamedBean bean = (NamedBean) list.get(0);
  +        assertEquals("Wrong name (1)", "Simple", bean.getName());
  +        // these are added in deepest first order by the addRootRule
  +        bean = (NamedBean) list.get(4);
  +        assertEquals("Wrong name (2)", "Complex", bean.getName());
  +        bean = (NamedBean) list.get(3);
  +        assertEquals("Wrong name (3)", "Deep", bean.getName());
  +        bean = (NamedBean) list.get(2);
  +        assertEquals("Wrong name (4)", "Deeper", bean.getName());
  +        bean = (NamedBean) list.get(1);
  +        assertEquals("Wrong name (5)", "Deepest", bean.getName());
  +    }
   
       // ------------------------------------------------ Utility Support Methods
   
  
  
  
  1.2       +8 -4      jakarta-commons/digester/src/test/org/apache/commons/digester/NamedBean.java
  
  Index: NamedBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/NamedBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NamedBean.java	13 Apr 2003 20:46:03 -0000	1.1
  +++ NamedBean.java	17 Apr 2003 11:08:17 -0000	1.2
  @@ -89,4 +89,8 @@
       public void test(String name, String ignored) {
           setName(name);
       }
  +    
  +    public String toString() {
  +        return "NamedBean[" + getName() + "]";
  +    }
   }
  
  
  
  1.1                  jakarta-commons/digester/src/test/org/apache/commons/digester/ParamBean.java
  
  Index: ParamBean.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/ParamBean.java,v
1.1 2003/04/17 11:08:17 rdonkin Exp $
   * $Revision: 1.1 $
   * $Date: 2003/04/17 11:08:17 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.digester;
  
  
  /** 
   * This bean is used to replicate a reasonably complex use case 
   * whose behaviour has changed from Digester 1.3 to 1.4.
   *
   * @author robert burrell donkin
   */
  public class ParamBean {
      
      private boolean cool;
      private String that;
      private String _this;
      
      public ParamBean() {}
      
      public boolean isCool() {
          return cool;
      }	
      
      public void setCool(boolean cool) {
          this.cool = cool;
      }
      
      public String getThis() {
          return _this;
      }
      
      public String getThat() {
          return that;
      }
      
      public void setThisAndThat(String _this, String that) {
          this._this = _this;
          this.that = that;
      }
  }
  
  
  

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


Mime
View raw message