1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.digester3;
20
21 import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
22 import static org.junit.Assert.assertEquals;
23
24 import java.io.IOException;
25 import java.io.StringReader;
26
27 import org.apache.commons.digester3.binder.AbstractRulesModule;
28 import org.junit.Test;
29 import org.xml.sax.SAXException;
30
31
32
33
34
35
36 public class OverlappingCallMethodRuleTestCase
37 {
38
39
40
41 String itemId;
42
43 String itemName;
44
45 public void setItemId( String id )
46 {
47 itemId = id;
48 }
49
50 public void setItemName( String name )
51 {
52 itemName = name;
53 }
54
55
56
57 @Test
58 public void testItem1()
59 throws SAXException, IOException
60 {
61 StringBuilder input = new StringBuilder();
62 input.append( "<root>" );
63 input.append( " <item id='1'>anitem</item>" );
64 input.append( "</root>" );
65
66 Digester digester = newLoader( new AbstractRulesModule()
67 {
68
69 @Override
70 protected void configure()
71 {
72 forPattern( "root/item" ).callMethod( "setItemId" ).withParamCount( 1 )
73 .then()
74 .callParam().fromAttribute( "id" )
75 .then()
76 .callMethod( "setItemName" ).withParamCount( 1 )
77 .then()
78 .callParam();
79 }
80
81 }).newDigester();
82
83 this.itemId = null;
84 this.itemName = null;
85 digester.push( this );
86 digester.parse( new StringReader( input.toString() ) );
87
88 assertEquals( "1", this.itemId );
89 assertEquals( "anitem", this.itemName );
90 }
91
92 @Test
93 public void testItem2()
94 throws SAXException, IOException
95 {
96 StringBuilder input = new StringBuilder();
97 input.append( "<root>" );
98 input.append( " <item id='1'>anitem</item>" );
99 input.append( "</root>" );
100
101 Digester digester = newLoader( new AbstractRulesModule()
102 {
103
104 @Override
105 protected void configure()
106 {
107 forPattern( "root/item" ).callMethod( "setItemId" ).withParamCount( 1 )
108 .then()
109 .callParam().fromAttribute( "id" )
110 .then()
111 .callMethod( "setItemName" ).withParamCount( 1 )
112 .then()
113 .callParam();
114 }
115
116 }).newDigester();
117
118 this.itemId = null;
119 this.itemName = null;
120 digester.push( this );
121 digester.parse( new StringReader( input.toString() ) );
122
123 assertEquals( "1", this.itemId );
124 assertEquals( "anitem", this.itemName );
125 }
126
127 @Test
128 public void testItem3()
129 throws SAXException, IOException
130 {
131 StringBuilder input = new StringBuilder();
132 input.append( "<root>" );
133 input.append( " <item>1</item>" );
134 input.append( "</root>" );
135
136 Digester digester = newLoader( new AbstractRulesModule()
137 {
138
139 @Override
140 protected void configure()
141 {
142 forPattern( "root/item" ).callMethod( "setItemId" ).withParamCount( 1 )
143 .then()
144 .callParam().fromAttribute( "id" )
145 .then()
146 .callMethod( "setItemName" ).withParamCount( 1 )
147 .then()
148 .callParam();
149 }
150
151 }).newDigester();
152
153 digester.addCallMethod( "root/item", "setItemId", 1 );
154 digester.addCallParam( "root/item", 0 );
155 digester.addCallMethod( "root/item", "setItemName", 1 );
156 digester.addCallParam( "root/item", 0 );
157
158 this.itemId = null;
159 this.itemName = null;
160 digester.push( this );
161 digester.parse( new StringReader( input.toString() ) );
162
163 assertEquals( "1", this.itemId );
164 assertEquals( "1", this.itemName );
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 @Test
186 public void testItem4()
187 throws SAXException, IOException
188 {
189 StringBuilder input = new StringBuilder();
190 input.append( "<root>" );
191 input.append( " <item>" );
192 input.append( " <id value='1'/>" );
193 input.append( " <name value='name'/>" );
194 input.append( " </item>" );
195 input.append( "</root>" );
196
197 Digester digester = newLoader( new AbstractRulesModule()
198 {
199
200 @Override
201 protected void configure()
202 {
203 forPattern( "root/item" ).callMethod( "setItemId" ).withParamCount( 1 )
204 .then()
205 .callMethod( "setItemName" ).withParamCount( 1 );
206 forPattern( "root/item/id" ).callParam().fromAttribute( "value" );
207 forPattern( "root/item/name" ).callParam().fromAttribute( "value" );
208 }
209
210 }).newDigester();
211
212 this.itemId = null;
213 this.itemName = null;
214 digester.push( this );
215 digester.parse( new StringReader( input.toString() ) );
216
217
218
219
220
221
222 assertEquals( null, this.itemId );
223 assertEquals( "name", this.itemName );
224 }
225
226
227
228
229
230
231 @Test
232 public void testWildcard1()
233 throws SAXException, IOException
234 {
235 StringBuilder input = new StringBuilder();
236 input.append( "<box id='A1'>" );
237 input.append( " <box id='B1'>" );
238 input.append( " <box id='C1'/>" );
239 input.append( " <box id='C2'/>" );
240 input.append( " </box>" );
241 input.append( "</box>" );
242
243 Digester digester = newLoader( new AbstractRulesModule()
244 {
245
246 @Override
247 protected void configure()
248 {
249 forPattern( "*/box" ).createObject().ofType( Box.class )
250 .then()
251 .callMethod( "setId" ).withParamCount( 1 )
252 .then()
253 .callParam().fromAttribute( "id" )
254 .then()
255 .setNext( "addChild" );
256 }
257
258 }).newDigester();
259
260 Box root = new Box();
261 root.setId( "root" );
262 digester.push( root );
263 digester.parse( new StringReader( input.toString() ) );
264
265
266 String ids = root.getIds();
267 assertEquals( "root A1 B1 C1 C2", ids );
268 }
269
270
271
272
273
274
275 @Test
276 public void testWildcard2()
277 throws SAXException, IOException
278 {
279 StringBuilder input = new StringBuilder();
280 input.append( "<box>A1" );
281 input.append( " <box>B1" );
282 input.append( " <box>C1</box>" );
283 input.append( " <box>C2</box>" );
284 input.append( " </box>" );
285 input.append( "</box>" );
286
287 Digester digester = newLoader( new AbstractRulesModule()
288 {
289
290 @Override
291 protected void configure()
292 {
293 forPattern( "*/box" ).createObject().ofType( Box.class )
294 .then()
295 .callMethod( "setId" ).withParamCount( 1 )
296 .then()
297 .callParam()
298 .then()
299 .setNext( "addChild" );
300 }
301
302 }).newDigester();
303
304 Box root = new Box();
305 root.setId( "root" );
306 digester.push( root );
307 digester.parse( new StringReader( input.toString() ) );
308
309
310 String ids = root.getIds();
311 assertEquals( "root A1 B1 C1 C2", ids );
312 }
313 }