Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 94521 invoked from network); 13 Apr 2003 20:46:07 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 13 Apr 2003 20:46:07 -0000 Received: (qmail 16220 invoked by uid 97); 13 Apr 2003 20:48:06 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 16213 invoked from network); 13 Apr 2003 20:48:05 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 13 Apr 2003 20:48:05 -0000 Received: (qmail 94275 invoked by uid 500); 13 Apr 2003 20:46:04 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 94264 invoked by uid 500); 13 Apr 2003 20:46:04 -0000 Received: (qmail 94259 invoked from network); 13 Apr 2003 20:46:04 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 13 Apr 2003 20:46:04 -0000 Received: (qmail 72734 invoked by uid 1289); 13 Apr 2003 20:46:03 -0000 Date: 13 Apr 2003 20:46:03 -0000 Message-ID: <20030413204603.72733.qmail@icarus.apache.org> From: rdonkin@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester NamedBean.java PrimitiveBean.java CallMethodRuleTestCase.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N rdonkin 2003/04/13 13:46:03 Modified: digester/src/java/org/apache/commons/digester CallParamRule.java Digester.java digester/src/test/org/apache/commons/digester CallMethodRuleTestCase.java Added: digester/src/test/org/apache/commons/digester NamedBean.java PrimitiveBean.java Log: Add ability to set a CallMethodRule parameter from an object at a particular index on the stack. Also added test cases. Revision Changes Path 1.11 +27 -7 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.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- CallParamRule.java 2 Feb 2003 16:09:53 -0000 1.10 +++ CallParamRule.java 13 Apr 2003 20:46:03 -0000 1.11 @@ -167,7 +167,22 @@ this.paramIndex = paramIndex; this.fromStack = fromStack; - + + } + + /** + * Constructs a "call parameter" rule which sets a parameter from the stack. + * If the stack contains too few objects, then the parameter will be set to null. + * + * @param paramIndex The zero-relative parameter number + * @param stackIndex the index of the object which will be passed as a parameter. + * The zeroth object is the top of the stack, 1 is the next object down and so on. + */ + public CallParamRule(int paramIndex, int stackIndex) { + + this.paramIndex = paramIndex; + this.fromStack = true; + this.stackIndex = stackIndex; } // ----------------------------------------------------- Instance Variables @@ -186,9 +201,14 @@ /** - * The position of the object from the top of the stack + * Is the parameter to be set from the stack? */ protected boolean fromStack = false; + + /** + * The position of the object from the top of the stack + */ + protected int stackIndex = 0; // --------------------------------------------------------- Public Methods @@ -209,7 +229,7 @@ } else if(fromStack) { - param = digester.peek(); + param = digester.peek(stackIndex); if (digester.log.isDebugEnabled()) { 1.73 +19 -4 jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java Index: Digester.java =================================================================== RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v retrieving revision 1.72 retrieving revision 1.73 diff -u -r1.72 -r1.73 --- Digester.java 6 Apr 2003 19:53:18 -0000 1.72 +++ Digester.java 13 Apr 2003 20:46:03 -0000 1.73 @@ -1835,6 +1835,21 @@ } + /** + * Add a "call parameter" rule that sets a parameter from the stack. + * This takes a parameter from the given position on the stack. + * + * @param paramIndex The zero-relative parameter number + * @param fromStack Should the call parameter be taken from the top of the stack? + * @param stackIndex set the call parameter to the stackIndex'th object down the stack, + * where 0 is the top of the stack, 1 the next element down and so on + */ + public void addCallParam(String pattern, int paramIndex, int stackIndex) { + + addRule(pattern, + new CallParamRule(paramIndex, stackIndex)); + + } /** * Add a "factory create" rule for the specified parameters. 1.4 +94 -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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CallMethodRuleTestCase.java 6 Feb 2003 23:25:00 -0000 1.3 +++ CallMethodRuleTestCase.java 13 Apr 2003 20:46:03 -0000 1.4 @@ -74,6 +74,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.logging.impl.SimpleLog; + /** *

Tests for the CallMethodRule and associated @@ -323,6 +325,98 @@ } + + public void testPrimitiveReading() throws Exception { + StringReader reader = new StringReader( + ""); + + Digester digester = new Digester(); + + //SimpleLog log = new SimpleLog("[testPrimitiveReading:Digester]"); + //log.setLevel(SimpleLog.LOG_LEVEL_TRACE); + //digester.setLogger(log); + + digester.addObjectCreate("root/bean", PrimitiveBean.class); + digester.addSetNext("root/bean", "add"); + Class [] params = { Boolean.TYPE }; + digester.addCallMethod("root/bean", "setBoolean", 1, params); + digester.addCallParam("root/bean", 0, "good"); + + ArrayList list = new ArrayList(); + digester.push(list); + digester.parse(reader); + + assertEquals("Wrong number of beans in list", 3, list.size()); + PrimitiveBean bean = (PrimitiveBean) list.get(0); + assertTrue("Bean 0 property not called", bean.getSetBooleanCalled()); + assertEquals("Bean 0 property incorrect", true, bean.getBoolean()); + bean = (PrimitiveBean) list.get(1); + assertTrue("Bean 1 property not called", bean.getSetBooleanCalled()); + assertEquals("Bean 1 property incorrect", false, bean.getBoolean()); + bean = (PrimitiveBean) list.get(2); + // no attibute, no call is what's expected + assertTrue("Bean 2 property called", !bean.getSetBooleanCalled()); + } + + public void testFromStack() throws Exception { + + StringReader reader = new StringReader( + ""); + + Digester digester = new Digester(); + + Class [] params = { String.class }; + + digester.addObjectCreate("root/one", NamedBean.class); + digester.addSetNext("root/one", "add"); + digester.addCallMethod("root/one", "setName", 1, params); + digester.addCallParam("root/one", 0, 2); + + digester.addObjectCreate("root/two", NamedBean.class); + digester.addSetNext("root/two", "add"); + digester.addCallMethod("root/two", "setName", 1, params); + digester.addCallParam("root/two", 0, 3); + + digester.addObjectCreate("root/three", NamedBean.class); + digester.addSetNext("root/three", "add"); + digester.addCallMethod("root/three", "setName", 1, params); + digester.addCallParam("root/three", 0, 4); + + digester.addObjectCreate("root/four", NamedBean.class); + digester.addSetNext("root/four", "add"); + digester.addCallMethod("root/four", "setName", 1, params); + digester.addCallParam("root/four", 0, 5); + + digester.addObjectCreate("root/five", NamedBean.class); + digester.addSetNext("root/five", "add"); + Class [] newParams = { String.class, String.class }; + digester.addCallMethod("root/five", "test", 2, newParams); + digester.addCallParam("root/five", 0, 10); + digester.addCallParam("root/five", 1, 3); + + // prepare stack + digester.push("That lamb was sure to go."); + digester.push("And everywhere that Mary went,"); + digester.push("It's fleece was white as snow."); + digester.push("Mary had a little lamb,"); + + ArrayList list = new ArrayList(); + digester.push(list); + digester.parse(reader); + + assertEquals("Wrong number of beans in list", 5, list.size()); + NamedBean bean = (NamedBean) list.get(0); + assertEquals("Parameter not set from stack (1)", "Mary had a little lamb,", bean.getName()); + bean = (NamedBean) list.get(1); + assertEquals("Parameter not set from stack (2)", "It's fleece was white as snow.", bean.getName()); + bean = (NamedBean) list.get(2); + assertEquals("Parameter not set from stack (3)", "And everywhere that Mary went,", bean.getName()); + bean = (NamedBean) list.get(3); + assertEquals("Parameter not set from stack (4)", "That lamb was sure to go.", bean.getName()); + bean = (NamedBean) list.get(4); + assertEquals("Out of stack not set to null", null , bean.getName()); + } + // ------------------------------------------------ Utility Support Methods 1.1 jakarta-commons/digester/src/test/org/apache/commons/digester/NamedBean.java Index: NamedBean.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/NamedBean.java,v 1.1 2003/04/13 20:46:03 rdonkin Exp $ * $Revision: 1.1 $ * $Date: 2003/04/13 20:46:03 $ * * ==================================================================== * * 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 * . * */ package org.apache.commons.digester; /** * Very simple test bean * * @author Robert Burrell Donkin * @version $Revision: 1.1 $ $Date: 2003/04/13 20:46:03 $ */ public class NamedBean { private String name = "**UNSET**"; public NamedBean() {} public NamedBean(String name) {} public String getName() { return name; } public void setName(String name) { this.name = name; } public void test(String name, String ignored) { setName(name); } } 1.1 jakarta-commons/digester/src/test/org/apache/commons/digester/PrimitiveBean.java Index: PrimitiveBean.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/PrimitiveBean.java,v 1.1 2003/04/13 20:46:03 rdonkin Exp $ * $Revision: 1.1 $ * $Date: 2003/04/13 20:46:03 $ * * ==================================================================== * * 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 * . * */ package org.apache.commons.digester; /** * A simple bean with primitive properties. * At the moment only need a boolean property. * Feel free to add others later. * * @author robert burrell donkin */ public class PrimitiveBean { private boolean booleanValue; private boolean setBooleanCalled; public PrimitiveBean() {} public boolean getBoolean() { return booleanValue; } public boolean getSetBooleanCalled() { return setBooleanCalled; } public void setBoolean(boolean booleanValue) { this.booleanValue = booleanValue; setBooleanCalled = true; } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org