KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > OverlappingCallMethodRuleTestCase


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

16
17
18 package org.apache.commons.digester;
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.xml.sax.SAXException JavaDoc;
29
30 /**
31  * <p>Tests for situations where CallMethodRule instances and their
32  * parameters overlap each other.</p>
33  */

34 public class OverlappingCallMethodRuleTestCase extends TestCase {
35
36     // ----------------------------------------------------------- Constructors
37

38
39     /**
40      * Construct a new instance of this test case.
41      *
42      * @param name Name of the test case
43      */

44     public OverlappingCallMethodRuleTestCase(String JavaDoc name) {
45
46         super(name);
47
48     }
49
50
51     // --------------------------------------------------- Overall Test Methods
52

53
54     /**
55      * Set up instance variables required by this test case.
56      */

57     public void setUp() {
58     }
59
60
61     /**
62      * Return the tests included in this test suite.
63      */

64     public static Test suite() {
65         return (new TestSuite(OverlappingCallMethodRuleTestCase.class));
66     }
67
68
69     /**
70      * Tear down instance variables required by this test case.
71      */

72     public void tearDown() {
73     }
74
75
76
77     String JavaDoc itemId;
78     String JavaDoc itemName;
79     
80     public void setItemId(String JavaDoc id) { itemId = id; }
81     public void setItemName(String JavaDoc name) { itemName = name; }
82     
83     // ------------------------------------------------ Individual Test Methods
84

85
86     public void testItem1() throws SAXException JavaDoc, IOException JavaDoc {
87         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
88         input.append("<root>");
89         input.append(" <item id='1'>anitem</item>");
90         input.append("</root>");
91
92         Digester digester = new Digester();
93         
94         digester.addCallMethod("root/item", "setItemId", 1);
95         digester.addCallParam("root/item", 0, "id");
96         digester.addCallMethod("root/item", "setItemName", 1);
97         digester.addCallParam("root/item", 0);
98
99         this.itemId = null;
100         this.itemName = null;
101         digester.push(this);
102         digester.parse(new StringReader JavaDoc(input.toString()));
103
104         assertEquals("1", this.itemId);
105         assertEquals("anitem", this.itemName);
106     }
107
108     public void testItem2() throws SAXException JavaDoc, IOException JavaDoc {
109         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
110         input.append("<root>");
111         input.append(" <item id='1'>anitem</item>");
112         input.append("</root>");
113
114         Digester digester = new Digester();
115
116         digester.addCallMethod("root/item", "setItemName", 1);
117         digester.addCallParam("root/item", 0);
118         digester.addCallMethod("root/item", "setItemId", 1);
119         digester.addCallParam("root/item", 0, "id");
120
121         this.itemId = null;
122         this.itemName = null;
123         digester.push(this);
124         digester.parse(new StringReader JavaDoc(input.toString()));
125
126         assertEquals("1", this.itemId);
127         assertEquals("anitem", this.itemName);
128     }
129
130     public void testItem3() throws SAXException JavaDoc, IOException JavaDoc {
131         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
132         input.append("<root>");
133         input.append(" <item>1</item>");
134         input.append("</root>");
135
136         Digester digester = new Digester();
137
138         digester.addCallMethod("root/item", "setItemId", 1);
139         digester.addCallParam("root/item", 0);
140         digester.addCallMethod("root/item", "setItemName", 1);
141         digester.addCallParam("root/item", 0);
142
143         this.itemId = null;
144         this.itemName = null;
145         digester.push(this);
146         digester.parse(new StringReader JavaDoc(input.toString()));
147
148         assertEquals("1", this.itemId);
149         assertEquals("1", this.itemName);
150     }
151
152     /**
153      * This is an "anti-test" that demonstrates how digester can <i>fails</i>
154      * to produce the correct results, due to a design flaw (or at least
155      * limitation) in the way that CallMethodRule and CallParamRule work.
156      * <p>
157      * The following sequence always fails:
158      * <ul>
159      * <li>CallMethodRule A fires (pushing params array)</li>
160      * <li>CallMethodRule B fires (pushing params array)</li>
161      * <li>params rule for A fires --> writes to params of method B!</li>
162      * <li>params rule for B fires --> overwrites params for method B</li>
163      * </ul>
164      * The result is that method "b" appears to work ok, but method "a"
165      * loses its input parameters.
166      * <p>
167      * One solution is for CallParamRule objects to know which CallMethodRule
168      * they are associated with. Even this might fail in corner cases where
169      * the same rule is associated with multiple patterns, or with wildcard
170      * patterns which cause a rule to fire in a "recursive" manner. However
171      * implementing this is not possible with the current digester design.
172      */

173     
174     public void testItem4() throws SAXException JavaDoc, IOException JavaDoc {
175         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
176         input.append("<root>");
177         input.append(" <item>");
178         input.append(" <id value='1'/>");
179         input.append(" <name value='name'/>");
180         input.append(" </item>");
181         input.append("</root>");
182
183         Digester digester = new Digester();
184
185         digester.addCallMethod("root/item", "setItemId", 1);
186         digester.addCallParam("root/item/id", 0, "value");
187         digester.addCallMethod("root/item", "setItemName", 1);
188         digester.addCallParam("root/item/name", 0, "value");
189
190         this.itemId = null;
191         this.itemName = null;
192         digester.push(this);
193         digester.parse(new StringReader JavaDoc(input.toString()));
194
195         // These are the "correct" results
196
//assertEquals("1", this.itemId);
197
//assertEquals("name", this.itemName);
198

199         // These are what actually happens
200
assertEquals(null, this.itemId);
201         assertEquals("name", this.itemName);
202     }
203
204     /**
205      * This test checks that CallParamRule instances which fetch data
206      * from xml attributes work ok when invoked "recursively",
207      * ie a rule instances' methods gets called in the order
208      * begin[1]/begin[2]/body[2]/end[2]/body[1]/end[1]
209      */

210     public void testWildcard1() throws SAXException JavaDoc, IOException JavaDoc {
211         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
212         input.append("<box id='A1'>");
213         input.append(" <box id='B1'>");
214         input.append(" <box id='C1'/>");
215         input.append(" <box id='C2'/>");
216         input.append(" </box>");
217         input.append("</box>");
218
219         Digester digester = new Digester();
220
221         digester.addObjectCreate("*/box", Box.class);
222         digester.addCallMethod("*/box", "setId", 1);
223         digester.addCallParam("*/box", 0, "id");
224         digester.addSetNext("*/box", "addChild");
225
226         Box root = new Box();
227         root.setId("root");
228         digester.push(root);
229         digester.parse(new StringReader JavaDoc(input.toString()));
230
231         // walk the object tree, concatenating the id strings
232
String JavaDoc ids = root.getIds();
233         assertEquals("root A1 B1 C1 C2", ids);
234     }
235
236     /**
237      * This test checks that CallParamRule instances which fetch data
238      * from the xml element body work ok when invoked "recursively",
239      * ie a rule instances' methods gets called in the order
240      * begin[1]/begin[2]/body[2]/end[2]/body[1]/end[1]
241      */

242     public void testWildcard2() throws SAXException JavaDoc, IOException JavaDoc {
243         StringBuffer JavaDoc input = new StringBuffer JavaDoc();
244         input.append("<box>A1");
245         input.append(" <box>B1");
246         input.append(" <box>C1</box>");
247         input.append(" <box>C2</box>");
248         input.append(" </box>");
249         input.append("</box>");
250
251         Digester digester = new Digester();
252
253         digester.addObjectCreate("*/box", Box.class);
254         digester.addCallMethod("*/box", "setId", 1);
255         digester.addCallParam("*/box", 0);
256         digester.addSetNext("*/box", "addChild");
257
258         Box root = new Box();
259         root.setId("root");
260         digester.push(root);
261         digester.parse(new StringReader JavaDoc(input.toString()));
262
263         // walk the object tree, concatenating the id strings
264
String JavaDoc ids = root.getIds();
265         assertEquals("root A1 B1 C1 C2", ids);
266     }
267 }
268
269
Popular Tags