KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > substitution > VariableExpansionTestCase


1 /* $Id: VariableExpansionTestCase.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.substitution;
20
21 import org.apache.commons.digester.CallMethodRule;
22 import org.apache.commons.digester.Digester;
23 import org.apache.commons.digester.SimpleTestBean;
24
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.LinkedList JavaDoc;
29
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * <p>Test Case for the variable expansion facility in Digester.
38  *
39  * @author Simon Kitching
40  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
41  */

42
43 public class VariableExpansionTestCase extends TestCase {
44
45     // ----------------------------------------------------------- Constructors
46

47     /**
48      * Construct a new instance of this test case.
49      *
50      * @param name Name of the test case
51      */

52     public VariableExpansionTestCase(String JavaDoc name) {
53
54         super(name);
55
56     }
57
58     // --------------------------------------------------- Overall Test Methods
59

60     /**
61      * Set up instance variables required by this test case.
62      */

63     public void setUp() {
64     }
65
66
67     /**
68      * Return the tests included in this test suite.
69      */

70     public static Test suite() {
71         return (new TestSuite(VariableExpansionTestCase.class));
72     }
73
74
75     /**
76      * Tear down instance variables required by this test case.
77      */

78     public void tearDown() {
79     }
80
81     // method used in tests4
82
private LinkedList JavaDoc simpleTestBeans = new LinkedList JavaDoc();
83     public void addSimpleTestBean(SimpleTestBean bean) {
84         simpleTestBeans.add(bean);
85     }
86     
87     // implementation of source shared by the variable expander and
88
// is updatable during digesting via an Ant-like property element
89
private HashMap JavaDoc mutableSource=new HashMap JavaDoc();
90
91     /**
92      * Used in test case "testExpansionWithMutableSource", where the
93      * set of variables available to be substituted into the xml is
94      * updated as the xml is parsed.
95      */

96     public void addProperty(String JavaDoc key, String JavaDoc value) {
97         mutableSource.put(key, value);
98     }
99
100     /**
101      * Creates a Digester configured to show Ant-like capability.
102      *
103      * @return a Digester with rules and variable substitutor
104      */

105     private Digester createDigesterThatCanDoAnt() {
106         Digester digester = new Digester();
107
108         MultiVariableExpander expander = new MultiVariableExpander();
109         expander.addSource("$", mutableSource);
110         digester.setSubstitutor(new VariableSubstitutor(expander));
111
112         int useRootObj = -1;
113         Class JavaDoc[] callerArgTypes = new Class JavaDoc[] {String JavaDoc.class, String JavaDoc.class};
114         CallMethodRule caller = new CallMethodRule(useRootObj, "addProperty",
115             callerArgTypes.length, callerArgTypes);
116         digester.addRule("root/property", caller);
117         digester.addCallParam("root/property", 0, "name");
118         digester.addCallParam("root/property", 1, "value");
119
120         digester.addObjectCreate("root/bean", SimpleTestBean.class);
121         digester.addSetProperties("root/bean");
122         digester.addSetNext("root/bean", "addSimpleTestBean");
123         return digester;
124     }
125
126     // ------------------------------------------------ Individual Test Methods
127

128     /**
129      * Test that by default no expansion occurs.
130      */

131     public void testNoExpansion() throws SAXException JavaDoc, IOException JavaDoc {
132
133         String JavaDoc xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
134         StringReader JavaDoc input = new StringReader JavaDoc(xml);
135         Digester digester = new Digester();
136         
137         // Configure the digester as required
138
digester.addObjectCreate("root", SimpleTestBean.class);
139         digester.addSetProperties("root");
140
141         // Parse our test input.
142
Object JavaDoc root = digester.parse(input);
143
144         assertNotNull("Digester returned no object", root);
145         SimpleTestBean bean = (SimpleTestBean) root;
146         
147         assertEquals("${attr1}", bean.getAlpha());
148         assertEquals("var{attr2}", bean.getBeta());
149     }
150
151     /**
152      * Test that a MultiVariableExpander with no sources does no expansion.
153      */

154     public void testExpansionWithNoSource() throws SAXException JavaDoc, IOException JavaDoc {
155
156         String JavaDoc xml = "<root alpha='${attr1}' beta='var{attr2}'/>";
157         StringReader JavaDoc input = new StringReader JavaDoc(xml);
158         Digester digester = new Digester();
159         
160         // Configure the digester as required
161
MultiVariableExpander expander = new MultiVariableExpander();
162         digester.setSubstitutor(new VariableSubstitutor(expander));
163         digester.addObjectCreate("root", SimpleTestBean.class);
164         digester.addSetProperties("root");
165
166         // Parse our test input.
167
Object JavaDoc root = digester.parse(input);
168
169         assertNotNull("Digester returned no object", root);
170         SimpleTestBean bean = (SimpleTestBean) root;
171         
172         assertEquals("${attr1}", bean.getAlpha());
173         assertEquals("var{attr2}", bean.getBeta());
174     }
175
176     /**
177      * Test that a MultiVariableExpander with multiple sources works.
178      * It also tests that expansion works ok where multiple elements
179      * exist.
180      */

181     public void testExpansionWithMultipleSources() throws SAXException JavaDoc, IOException JavaDoc {
182
183         String JavaDoc xml =
184             "<root>" +
185               "<bean alpha='${attr1}' beta='var{attr1}'/>" +
186               "<bean alpha='${attr2}' beta='var{attr2}'/>" +
187             "</root>";
188             
189         StringReader JavaDoc input = new StringReader JavaDoc(xml);
190         Digester digester = new Digester();
191         
192         // Configure the digester as required
193
HashMap JavaDoc source1 = new HashMap JavaDoc();
194         source1.put("attr1", "source1.attr1");
195         source1.put("attr2", "source1.attr2"); // should not be used
196

197         HashMap JavaDoc source2 = new HashMap JavaDoc();
198         source2.put("attr1", "source2.attr1"); // should not be used
199
source2.put("attr2", "source2.attr2");
200         
201         
202         MultiVariableExpander expander = new MultiVariableExpander();
203         expander.addSource("$", source1);
204         expander.addSource("var", source2);
205         
206         digester.setSubstitutor(new VariableSubstitutor(expander));
207         digester.addObjectCreate("root/bean", SimpleTestBean.class);
208         digester.addSetProperties("root/bean");
209         digester.addSetNext("root/bean", "addSimpleTestBean");
210
211         // Parse our test input.
212
this.simpleTestBeans.clear();
213         digester.push(this);
214         digester.parse(input);
215
216         assertEquals(2, this.simpleTestBeans.size());
217
218         {
219         SimpleTestBean bean = (SimpleTestBean) this.simpleTestBeans.get(0);
220         assertEquals("source1.attr1", bean.getAlpha());
221         assertEquals("source2.attr1", bean.getBeta());
222         }
223
224         {
225         SimpleTestBean bean = (SimpleTestBean) this.simpleTestBeans.get(1);
226         assertEquals("source1.attr2", bean.getAlpha());
227         assertEquals("source2.attr2", bean.getBeta());
228         }
229     }
230
231     /**
232      * Test expansion of text in element bodies.
233      */

234     public void testBodyExpansion() throws SAXException JavaDoc, IOException JavaDoc {
235
236         String JavaDoc xml =
237             "<root>" +
238             "Twas noun{1} and the noun{2}" +
239             " did verb{1} and verb{2} in the noun{3}" +
240             "</root>";
241
242         StringReader JavaDoc input = new StringReader JavaDoc(xml);
243         Digester digester = new Digester();
244         
245         // Configure the digester as required
246
HashMap JavaDoc nouns = new HashMap JavaDoc();
247         nouns.put("1", "brillig");
248         nouns.put("2", "slithy toves");
249         nouns.put("3", "wabe");
250         
251         HashMap JavaDoc verbs = new HashMap JavaDoc();
252         verbs.put("1", "gyre");
253         verbs.put("2", "gimble");
254         
255         MultiVariableExpander expander = new MultiVariableExpander();
256         expander.addSource("noun", nouns);
257         expander.addSource("verb", verbs);
258         digester.setSubstitutor(new VariableSubstitutor(expander));
259         
260         digester.addObjectCreate("root", SimpleTestBean.class);
261         digester.addCallMethod("root", "setAlpha", 0);
262
263         // Parse our test input.
264
Object JavaDoc root = digester.parse(input);
265
266         assertNotNull("Digester returned no object", root);
267         SimpleTestBean bean = (SimpleTestBean) root;
268         
269         assertEquals(
270             "Twas brillig and the slithy toves" +
271             " did gyre and gimble in the wabe",
272             bean.getAlpha());
273     }
274
275     /**
276      * Test that an unknown variable causes a RuntimeException.
277      */

278     public void testExpansionException() throws IOException JavaDoc {
279
280         String JavaDoc xml = "<root alpha='${attr1}'/>";
281         StringReader JavaDoc input = new StringReader JavaDoc(xml);
282         Digester digester = new Digester();
283         
284         // Configure the digester as required
285
MultiVariableExpander expander = new MultiVariableExpander();
286         expander.addSource("$", new HashMap JavaDoc());
287         digester.setSubstitutor(new VariableSubstitutor(expander));
288         
289         digester.addObjectCreate("root", SimpleTestBean.class);
290         digester.addSetProperties("root");
291
292         // Parse our test input.
293
try {
294             digester.parse(input);
295             fail("Exception expected due to unknown variable.");
296         } catch(SAXException JavaDoc e) {
297             // expected, due to reference to undefined variable
298
}
299     }
300
301     /**
302      * First of two tests added to verify that the substitution
303      * framework is capable of processing Ant-like properties.
304      *
305      * The tests above essentially verify that if a property
306      * was pre-set (e.g. using the "-D" option to Ant), then
307      * the property could be expanded via a variable used either
308      * in an attribute or in body text.
309      *
310      * This test shows that if properties were also set while
311      * processing a document, you could still perform variable
312      * expansion (i.e. just like using the "property" task in Ant).
313      *
314      * @throws IOException
315      * @throws SAXException
316      */

317     public void testExpansionWithMutableSource() throws SAXException JavaDoc, IOException JavaDoc {
318         String JavaDoc xml =
319             "<root>" +
320               "<property name='attr' value='prop.value'/>" +
321               "<bean alpha='${attr}'/>" +
322             "</root>";
323         StringReader JavaDoc input = new StringReader JavaDoc(xml);
324         Digester digester = createDigesterThatCanDoAnt();
325
326         simpleTestBeans.clear();
327         digester.push(this);
328         digester.parse(input);
329
330         assertEquals(1, simpleTestBeans.size());
331         SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0);
332         assertEquals("prop.value", bean.getAlpha());
333     }
334
335     /**
336      * Second of two tests added to verify that the substitution
337      * framework is capable of processing Ant-like properties.
338      *
339      * This test shows that if properties were also set while
340      * processing a document, the resulting variables could also
341      * be expanded within a property element. This is thus
342      * effectively a "closure" test, since it shows that the
343      * mechanism used to bind properties is also capable of
344      * having property values that are driven by property variables.
345      *
346      * @throws IOException
347      * @throws SAXException
348      */

349     public void testExpansionOfPropertyInProperty() throws SAXException JavaDoc, IOException JavaDoc {
350         String JavaDoc xml =
351             "<root>" +
352               "<property name='attr1' value='prop.value1'/>" +
353               "<property name='attr2' value='substituted-${attr1}'/>" +
354               "<bean alpha='${attr2}'/>" +
355             "</root>";
356         StringReader JavaDoc input = new StringReader JavaDoc(xml);
357         Digester digester = createDigesterThatCanDoAnt();
358
359         simpleTestBeans.clear();
360         digester.push(this);
361         digester.parse(input);
362
363         assertEquals(1, simpleTestBeans.size());
364         SimpleTestBean bean = (SimpleTestBean) simpleTestBeans.get(0);
365         assertEquals("substituted-prop.value1", bean.getAlpha());
366     }
367
368 }
369
Popular Tags