rdonkin     02/04/18 14:23:44

  Modified:    digester/src/java/org/apache/commons/digester
                        CallMethodRule.java Digester.java
               digester/src/test/org/apache/commons/digester
                        RuleTestCase.java
  Added:       digester/src/test/org/apache/commons/digester Test5.xml
  Log:
  CallMethodRule enhancement created by Emmanuel Bourg (gug#8244). This adds support 
to CallMethodRule for calls to methods which accept no arguments plus tests for same. 
Patch should be backwards compatible for all real life applications.
  
  Revision  Changes    Path
  1.16      +41 -9     
jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java
  
  Index: CallMethodRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- CallMethodRule.java       23 Mar 2002 17:45:57 -0000      1.15
  +++ CallMethodRule.java       18 Apr 2002 21:23:44 -0000      1.16
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
 1.15 2002/03/23 17:45:57 rdonkin Exp $
  - * $Revision: 1.15 $
  - * $Date: 2002/03/23 17:45:57 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
 1.16 2002/04/18 21:23:44 rdonkin Exp $
  + * $Revision: 1.16 $
  + * $Date: 2002/04/18 21:23:44 $
    *
    * ====================================================================
    *
  @@ -75,11 +75,14 @@
    * Rule implementation that calls a method on the top (parent)
    * object, passing arguments collected from subsequent
    * <code>CallParamRule</code> rules or from the body of this
  - * element.
  + * element. 
  + * By using {@link #CallMethodRule(String methodName)} 
  + * a method call can be made to a method which accepts no
  + * arguments.
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.15 $ $Date: 2002/03/23 17:45:57 $
  + * @version $Revision: 1.16 $ $Date: 2002/04/18 21:23:44 $
    */
   
   public class CallMethodRule extends Rule {
  @@ -166,13 +169,38 @@
       public CallMethodRule(String methodName,
                             int paramCount) {
   
  -        this( methodName, paramCount, (Class[]) null);
  +        this.methodName = methodName;
  +        this.paramCount = paramCount;        
  +        if (paramCount == 0) {
  +            this.paramTypes = new Class[] { String.class };
  +        } else {
  +            this.paramTypes = new Class[paramCount];
  +            for (int i = 0; i < this.paramTypes.length; i++) {
  +                this.paramTypes[i] = String.class;
  +            }
  +        }
   
       }
   
  +    /**
  +     * Construct a "call method" rule with the specified method name.  
  +     * The method should accept no parameters.
  +     *
  +     * @param methodName Method name of the parent method to call
  +     */
  +    public CallMethodRule(String methodName) {
  +    
  +        this(methodName, 0, (Class[]) null);
  +    
  +    }
  +    
   
       /**
  -     * Construct a "call method" rule with the specified method name.
  +     * Construct a "call method" rule with the specified method name and
  +     * parameter types. If <code>paramCount</code> is set to zero the rule
  +     * will use the body of this element as the single argument of the
  +     * method, unless <code>paramTypes</code> is null or empty, in this
  +     * case the rule will call the specified method with no arguments.
        *
        * @param methodName Method name of the parent method to call
        * @param paramCount The number of parameters to collect, or
  @@ -210,7 +238,11 @@
   
   
       /**
  -     * Construct a "call method" rule with the specified method name.
  +     * Construct a "call method" rule with the specified method name and
  +     * parameter types. If <code>paramCount</code> is set to zero the rule
  +     * will use the body of this element as the single argument of the
  +     * method, unless <code>paramTypes</code> is null or empty, in this
  +     * case the rule will call the specified method with no arguments.
        *
        * @param methodName Method name of the parent method to call
        * @param paramCount The number of parameters to collect, or
  @@ -327,7 +359,7 @@
                   return;
               }
   
  -        } else {
  +        } else if (paramTypes != null && paramTypes.length != 0) {
   
               // In the case where the parameter for the method
               // is taken from the body text, but there is no
  
  
  
  1.51      +25 -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.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- Digester.java     23 Mar 2002 17:45:57 -0000      1.50
  +++ Digester.java     18 Apr 2002 21:23:44 -0000      1.51
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
 1.50 2002/03/23 17:45:57 rdonkin Exp $
  - * $Revision: 1.50 $
  - * $Date: 2002/03/23 17:45:57 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
 1.51 2002/04/18 21:23:44 rdonkin Exp $
  + * $Revision: 1.51 $
  + * $Date: 2002/04/18 21:23:44 $
    *
    * ====================================================================
    *
  @@ -112,7 +112,7 @@
    *
    * @author Craig McClanahan
    * @author Scott Sanders
  - * @version $Revision: 1.50 $ $Date: 2002/03/23 17:45:57 $
  + * @version $Revision: 1.51 $ $Date: 2002/04/18 21:23:44 $
    */
   
   public class Digester extends DefaultHandler {
  @@ -1423,6 +1423,19 @@
   
       }
   
  +    /**
  +     * Add an "call method" rule for a method which accepts no arguments.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param methodName Method name to be called
  +     */
  +    public void addCallMethod(String pattern, String methodName) {
  +
  +        addRule(
  +                pattern,
  +                new CallMethodRule(methodName));
  +
  +    }
   
       /**
        * Add an "call method" rule for the specified parameters.
  @@ -1443,6 +1456,10 @@
   
       /**
        * Add an "call method" rule for the specified parameters.
  +     * If <code>paramCount</code> is set to zero the rule will use
  +     * the body of the matched element as the single argument of the
  +     * method, unless <code>paramTypes</code> is null or empty, in this
  +     * case the rule will call the specified method with no arguments.
        *
        * @param pattern Element matching pattern
        * @param methodName Method name to be called
  @@ -1468,6 +1485,10 @@
   
       /**
        * Add an "call method" rule for the specified parameters.
  +     * If <code>paramCount</code> is set to zero the rule will use
  +     * the body of the matched element as the single argument of the
  +     * method, unless <code>paramTypes</code> is null or empty, in this
  +     * case the rule will call the specified method with no arguments.
        *
        * @param pattern Element matching pattern
        * @param methodName Method name to be called
  
  
  
  1.12      +70 -4     
jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java
  
  Index: RuleTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- RuleTestCase.java 20 Mar 2002 20:28:28 -0000      1.11
  +++ RuleTestCase.java 18 Apr 2002 21:23:44 -0000      1.12
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java,v
 1.11 2002/03/20 20:28:28 rdonkin Exp $
  - * $Revision: 1.11 $
  - * $Date: 2002/03/20 20:28:28 $
  + * $Header: 
/home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java,v
 1.12 2002/04/18 21:23:44 rdonkin Exp $
  + * $Revision: 1.12 $
  + * $Date: 2002/04/18 21:23:44 $
    *
    * ====================================================================
    *
  @@ -79,7 +79,7 @@
    *
    * @author Craig R. McClanahan
    * @author Janek Bogucki
  - * @version $Revision: 1.11 $ $Date: 2002/03/20 20:28:28 $
  + * @version $Revision: 1.12 $ $Date: 2002/04/18 21:23:44 $
    */
   
   public class RuleTestCase extends TestCase {
  @@ -288,6 +288,41 @@
                   "Last Name",
                   employee.getLastName());
   
  +    }
  +
  +
  +    /**
  +     * Same as testObjectCreate1(), except use individual call method rules
  +     * to set the properties of the Employee. Bean data are defined using 
  +     * elements instead of attributes. The purpose is to test CallMethod with
  +     * a paramCount=0 (ie the body of the element is the argument of the 
  +     * method).
  +     */
  +    public void testObjectCreate5() {
  +
  +        // Configure the digester as required
  +        digester.addObjectCreate("employee", Employee.class);
  +        digester.addCallMethod("employee/firstName", "setFirstName", 0);
  +        digester.addCallMethod("employee/lastName", "setLastName", 0);
  +
  +
  +        // Parse our test input.
  +        Object root = null;
  +        try {
  +            root = digester.parse(getInputStream("Test5.xml"));
  +        } catch (Throwable t) {
  +            fail("Digester threw IOException: " + t);
  +        }
  +        assertNotNull("Digester returned an object", root);
  +        assertTrue("Digester returned an Employee",
  +                root instanceof Employee);
  +        Employee employee = (Employee) root;
  +        assertEquals("First name is correct",
  +                "First Name",
  +                employee.getFirstName());
  +        assertEquals("Last name is correct",
  +                "Last Name",
  +                employee.getLastName());
   
       }
   
  @@ -594,6 +629,37 @@
           assertEquals("Wrong name (6)", "FOUR", five.getParent().getName());
   
       }
  +
  +
  +    /**
  +     * Test method calls with the CallMethodRule rule. It should be possible
  +     * to call any accessible method of the object on the top of the stack,
  +     * even methods with no arguments.
  +     */
  +    public void testCallMethod() throws Exception {
  +        
  +        // Configure the digester as required
  +        digester.addObjectCreate("employee", Employee.class);
  +        // try all syntax permutations
  +        digester.addCallMethod("employee", "toString", 0, (Class[])null);
  +        digester.addCallMethod("employee", "toString", 0, (String[])null);
  +        digester.addCallMethod("employee", "toString", 0, new Class[] {});
  +        digester.addCallMethod("employee", "toString", 0, new String[] {});
  +        digester.addCallMethod("employee", "toString");
  +
  +        // Parse our test input
  +        Object root1 = null;
  +        try {
  +            // an exception will be thrown if the method can't be found
  +            root1 = digester.parse(getInputStream("Test5.xml"));
  +            
  +        } catch (Throwable t) {
  +            // this means that the method can't be found and so the test fails
  +            fail("Digester threw Exception:  " + t);
  +        }
  +
  +    }
  +
   
       // ------------------------------------------------ Utility Support Methods
   
  
  
  
  1.1                  
jakarta-commons/digester/src/test/org/apache/commons/digester/Test5.xml
  
  Index: Test5.xml
  ===================================================================
  <?xml version="1.0"?>
<employee>
  <firstName> First Name  </firstName>
  <lastName> 
 Last Name </lastName>
  <address>
    <type>home</type>
    <street>Home 
Street</street>
    <city>Home City</city>
    <state>HS</state>
    
<zipCode>HmZip</zipCode>
  </address>
  <address>
    <type>office</type>
    
<street>Office Street</street>
    <city>Office City</city>
    <state>OS</state>
    
<zipCode>OfZip</zipCode>
  </address>
</employee>
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to