commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester/substitution VariableExpansionTestCase.java
Date Sat, 18 Sep 2004 09:31:11 GMT
skitching    2004/09/18 02:31:11

  Modified:    digester/src/test/org/apache/commons/digester/substitution
                        VariableExpansionTestCase.java
  Log:
  New test cases demonstrate that the substitution code can be used to
  provide variable features also present in the Ant tool. Thanks to
  Reid M. Pinchback for the patch (see bugzilla 31207).
  
  Revision  Changes    Path
  1.4       +111 -4    jakarta-commons/digester/src/test/org/apache/commons/digester/substitution/VariableExpansionTestCase.java
  
  Index: VariableExpansionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/substitution/VariableExpansionTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- VariableExpansionTestCase.java	7 May 2004 01:30:00 -0000	1.3
  +++ VariableExpansionTestCase.java	18 Sep 2004 09:31:11 -0000	1.4
  @@ -18,8 +18,9 @@
   
   package org.apache.commons.digester.substitution;
   
  -import org.apache.commons.digester.SimpleTestBean;
  +import org.apache.commons.digester.CallMethodRule;
   import org.apache.commons.digester.Digester;
  +import org.apache.commons.digester.SimpleTestBean;
   
   import java.io.IOException;
   import java.io.StringReader;
  @@ -83,6 +84,45 @@
           simpleTestBeans.add(bean);
       }
       
  +    // implementation of source shared by the variable expander and
  +    // is updatable during digesting via an Ant-like property element
  +    private HashMap mutableSource=new HashMap();
  +
  +    /**
  +     * Used in test case "testExpansionWithMutableSource", where the
  +     * set of variables available to be substituted into the xml is
  +     * updated as the xml is parsed.
  +     */
  +    public void addProperty(String key, String value) {
  +        mutableSource.put(key, value);
  +    }
  +
  +    /**
  +     * Creates a Digester configured to show Ant-like capability.
  +     * 
  +     * @return a Digester with rules and variable substitutor
  +     */
  +    private Digester createDigesterThatCanDoAnt() {
  +        Digester digester = new Digester();
  +
  +        MultiVariableExpander expander = new MultiVariableExpander();
  +        expander.addSource("$", mutableSource);
  +        digester.setSubstitutor(new VariableSubstitutor(expander));
  +
  +        int useRootObj = -1;
  +        Class[] callerArgTypes = new Class[] {String.class, String.class};
  +        CallMethodRule caller = new CallMethodRule(useRootObj, "addProperty",
  +            callerArgTypes.length, callerArgTypes);
  +        digester.addRule("root/property", caller);
  +        digester.addCallParam("root/property", 0, "name");
  +        digester.addCallParam("root/property", 1, "value");
  +
  +        digester.addObjectCreate("root/bean", SimpleTestBean.class);
  +        digester.addSetProperties("root/bean");
  +        digester.addSetNext("root/bean", "addSimpleTestBean");
  +        return digester;
  +    }
  +
       // ------------------------------------------------ Individual Test Methods
   
       /**
  @@ -257,5 +297,72 @@
               // expected, due to reference to undefined variable
           }
       }
  -}
   
  +    /**
  +     * First of two tests added to verify that the substitution
  +     * framework is capable of processing Ant-like properties.
  +     *
  +     * The tests above essentially verify that if a property
  +     * was pre-set (e.g. using the "-D" option to Ant), then
  +     * the property could be expanded via a variable used either
  +     * in an attribute or in body text.
  +     *
  +     * This test shows that if properties were also set while
  +     * processing a document, you could still perform variable
  +     * expansion (i.e. just like using the "property" task in Ant).
  +     *
  +     * @throws IOException
  +     * @throws SAXException
  +     */
  +    public void testExpansionWithMutableSource() throws SAXException, IOException {
  +        String xml =
  +            "<root>" +
  +              "<property name='attr' value='prop.value'/>" +
  +              "<bean alpha='${attr}'/>" +
  +            "</root>";
  +        StringReader input = new StringReader(xml);
  +        Digester digester = createDigesterThatCanDoAnt();
  +
  +        simpleTestBeans.clear();
  +        digester.push(this);
  +        digester.parse(input);
  +
  +        assertEquals(1, simpleTestBeans.size());
  +        SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0);
  +        assertEquals("prop.value", bean.getAlpha());
  +    }
  +
  +    /**
  +     * Second of two tests added to verify that the substitution
  +     * framework is capable of processing Ant-like properties.
  +     *
  +     * This test shows that if properties were also set while
  +     * processing a document, the resulting variables could also
  +     * be expanded within a property element.  This is thus
  +     * effectively a "closure" test, since it shows that the
  +     * mechanism used to bind properties is also capable of
  +     * having property values that are driven by property variables.
  +     *
  +     * @throws IOException
  +     * @throws SAXException
  +     */
  +    public void testExpansionOfPropertyInProperty() throws SAXException, IOException {
  +        String xml =
  +            "<root>" +
  +              "<property name='attr1' value='prop.value1'/>" +
  +              "<property name='attr2' value='substituted-${attr1}'/>" +
  +              "<bean alpha='${attr2}'/>" +
  +            "</root>";
  +        StringReader input = new StringReader(xml);
  +        Digester digester = createDigesterThatCanDoAnt();
  +
  +        simpleTestBeans.clear();
  +        digester.push(this);
  +        digester.parse(input);
  +
  +        assertEquals(1, simpleTestBeans.size());
  +        SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0);
  +        assertEquals("substituted-prop.value1", bean.getAlpha());
  +    }
  +
  +}
  
  
  

---------------------------------------------------------------------
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