KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: EBRTestCase.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;
20
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28
29 /**
30  * <p> Runs standard tests for RulesBase as well as tests of extensions.
31  *
32  * @author Robert Burrell Donkin <robertdonkin@mac.com>
33  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
34  */

35
36
37 public class EBRTestCase extends RulesBaseTestCase {
38
39
40     // ----------------------------------------------------------- Constructors
41

42     /**
43      * Construct a new instance of this test case.
44      *
45      * @param name Name of the test case
46      */

47     public EBRTestCase(String JavaDoc name) {
48
49         super(name);
50     }
51
52
53     // -------------------------------------------------- Overall Test Methods
54

55     /**
56      * <p> This should be overriden by subclasses.
57      *
58      * @return the matching rules to be tested.
59      */

60     protected Rules createMatchingRulesForTest() {
61
62         return new ExtendedBaseRules();
63     }
64
65
66     /**
67      * Return the tests included in this test suite.
68      */

69     public static Test suite() {
70
71         return (new TestSuite(EBRTestCase.class));
72
73     }
74
75
76     /**
77      * Basic test of parent matching rules.
78      * A parent match matches any child of a particular kind of parent.
79      * A wild parent has a wildcard prefix.
80      * This method tests non-universal wildcards.
81      */

82     public void testBasicParentMatch() {
83
84         // clear any existing rules
85
digester.getRules().clear();
86
87         assertEquals("Initial rules list is empty",
88                 0, digester.getRules().rules().size());
89
90         // Set up rules
91
// since these are all NON-UNIVERSAL matches
92
// only expect one match at each stage
93
digester.addRule("alpha/beta/gamma/delta", new TestRule("exact"));
94         digester.addRule("*/beta/gamma/epsilon", new TestRule("wild_child"));
95         digester.addRule("alpha/beta/gamma/?", new TestRule("exact_parent"));
96         digester.addRule("*/beta/gamma/?", new TestRule("wild_parent"));
97
98
99         List JavaDoc list = null;
100         Iterator JavaDoc it = null;
101
102         // this should match just the exact since this has presidence
103
list = digester.getRules().match(null, "alpha/beta/gamma/delta");
104
105         // all three rules should match
106
assertEquals("Testing basic parent mismatch (A)", 1, list.size());
107
108         it = list.iterator();
109         assertEquals("Testing basic parent mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier());
110
111
112         // we don't have an exact match for this child so we should get the exact parent
113
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
114
115         // all three rules should match
116
assertEquals("Testing basic parent mismatch (C)", 1, list.size());
117
118         it = list.iterator();
119         assertEquals("Testing basic parent mismatch (D)", "exact_parent", ((TestRule) it.next()).getIdentifier());
120
121
122         // wild child overrides wild parent
123
list = digester.getRules().match(null, "alpha/omega/beta/gamma/epsilon");
124
125         // all three rules should match
126
assertEquals("Testing basic parent mismatch (E)", 1, list.size());
127
128         it = list.iterator();
129         assertEquals("Testing basic parent mismatch (F)", "wild_child", ((TestRule) it.next()).getIdentifier());
130
131
132         // nothing else matches so return wild parent
133
list = digester.getRules().match(null, "alpha/omega/beta/gamma/zeta");
134
135         // all three rules should match
136
assertEquals("Testing basic parent mismatch (G)", 1, list.size());
137
138         it = list.iterator();
139         assertEquals("Testing basic parent mismatch (H)", "wild_parent", ((TestRule) it.next()).getIdentifier());
140
141
142         // clean up
143
digester.getRules().clear();
144
145     }
146
147
148     /**
149      * Basic test of universal matching rules.
150      * Universal rules act independent.
151      */

152     public void testBasicUniversal() {
153
154         // clear any existing rules
155
digester.getRules().clear();
156
157         assertEquals("Initial rules list is empty",
158                 0, digester.getRules().rules().size());
159
160         // Set up rules
161
// set up universal matches against non-universal ones
162
digester.addRule("alpha/beta/gamma", new TestRule("exact"));
163         digester.addRule("*/beta/gamma", new TestRule("non_wild_head"));
164         digester.addRule("!*/beta/gamma", new TestRule("universal_wild_head"));
165         digester.addRule("!alpha/beta/gamma/?", new TestRule("universal_wild_child"));
166         digester.addRule("alpha/beta/gamma/?", new TestRule("non_wild_child"));
167         digester.addRule("alpha/beta/gamma/epsilon", new TestRule("exact2"));
168         digester.addRule("alpha/epsilon/beta/gamma/zeta", new TestRule("exact3"));
169         digester.addRule("*/gamma/?", new TestRule("non_wildhead_child"));
170         digester.addRule("!*/epsilon/beta/gamma/?", new TestRule("universal_wildhead_child"));
171
172
173         List JavaDoc list = null;
174         Iterator JavaDoc it = null;
175
176         // test universal wild head
177
list = digester.getRules().match(null, "alpha/beta/gamma");
178
179         assertEquals("Testing universal wildcard mismatch (A)", 2, list.size());
180
181         it = list.iterator();
182         assertEquals("Testing universal wildcard mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier());
183         assertEquals("Testing universal wildcard mismatch (C)", "universal_wild_head", ((TestRule) it.next()).getIdentifier());
184
185
186         // test universal parent
187
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
188
189         assertEquals("Testing universal wildcard mismatch (D)", 2, list.size());
190
191         it = list.iterator();
192         assertEquals("Testing universal wildcard mismatch (E)", "universal_wild_child", ((TestRule) it.next()).getIdentifier());
193         assertEquals("Testing universal wildcard mismatch (F)", "exact2", ((TestRule) it.next()).getIdentifier());
194
195         // test universal parent
196
list = digester.getRules().match(null, "alpha/beta/gamma/zeta");
197
198         assertEquals("Testing universal wildcard mismatch (G)", 2, list.size());
199
200         it = list.iterator();
201         assertEquals("Testing universal wildcard mismatch (H)", "universal_wild_child", ((TestRule) it.next()).getIdentifier());
202         assertEquals("Testing universal wildcard mismatch (I)", "non_wild_child", ((TestRule) it.next()).getIdentifier());
203
204
205         // test wildcard universal parent
206
list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/alpha");
207
208         assertEquals("Testing universal wildcard mismatch (J)", 2, list.size());
209
210         it = list.iterator();
211         assertEquals("Testing universal wildcard mismatch (K)", "non_wildhead_child", ((TestRule) it.next()).getIdentifier());
212         assertEquals("Testing universal wildcard mismatch (L)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier());
213
214         // test wildcard universal parent
215
list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/zeta");
216
217         assertEquals("Testing universal wildcard mismatch (M)", 2, list.size());
218
219         it = list.iterator();
220         assertEquals("Testing universal wildcard mismatch (M)", "exact3", ((TestRule) it.next()).getIdentifier());
221         assertEquals("Testing universal wildcard mismatch (O)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier());
222
223
224         // clean up
225
digester.getRules().clear();
226
227     }
228
229
230     /**
231      * Basic test of wild matches.
232      * A universal will match matches anything!
233      * A non-universal will match matches anything not matched by something else.
234      * This method tests non-universal and universal wild matches.
235      */

236     public void testWildMatch() {
237
238         // clear any existing rules
239
digester.getRules().clear();
240
241         assertEquals("Initial rules list is empty",
242                 0, digester.getRules().rules().size());
243
244         // Set up rules
245
// The combinations a little large to test everything but we'll pick a couple and try them.
246
digester.addRule("*", new TestRule("basic_wild"));
247         digester.addRule("!*", new TestRule("universal_wild"));
248         digester.addRule("alpha/beta/gamma/delta", new TestRule("exact"));
249         digester.addRule("*/beta/gamma/?", new TestRule("wild_parent"));
250
251
252         List JavaDoc list = null;
253         Iterator JavaDoc it = null;
254
255         // The universal wild will always match whatever else does
256
list = digester.getRules().match(null, "alpha/beta/gamma/delta");
257
258         // all three rules should match
259
assertEquals("Testing wild mismatch (A)", 2, list.size());
260
261         it = list.iterator();
262         assertEquals("Testing wild mismatch (B)", "universal_wild", ((TestRule) it.next()).getIdentifier());
263         assertEquals("Testing wild mismatch (C)", "exact", ((TestRule) it.next()).getIdentifier());
264
265
266         // The universal wild will always match whatever else does
267
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
268
269         assertEquals("Testing wild mismatch (D)", 2, list.size());
270
271         it = list.iterator();
272         assertEquals("Testing wild mismatch (E)", "universal_wild", ((TestRule) it.next()).getIdentifier());
273         assertEquals("Testing wild mismatch (F)", "wild_parent", ((TestRule) it.next()).getIdentifier());
274
275
276         // The universal wild will always match whatever else does
277
// we have no other non-universal matching so this will match the non-universal wild as well
278
list = digester.getRules().match(null, "alpha/gamma");
279
280         assertEquals("Testing wild mismatch (G)", 2, list.size());
281
282         it = list.iterator();
283         assertEquals("Testing wild mismatch (H)", "basic_wild", ((TestRule) it.next()).getIdentifier());
284         assertEquals("Testing wild mismatch (I)", "universal_wild", ((TestRule) it.next()).getIdentifier());
285
286
287         // clean up
288
digester.getRules().clear();
289
290     }
291     
292     
293
294     /**
295      * Basic test of wild matches.
296      * A universal will match matches anything!
297      * A non-universal will match matches anything not matched by something else.
298      * This method tests non-universal and universal wild matches.
299      */

300     public void testRootTailMatch() {
301
302         // clear any existing rules
303
digester.getRules().clear();
304
305         assertEquals("Initial rules list is empty",
306                 0, digester.getRules().rules().size());
307
308         // Set up rules
309
// The combinations a little large to test everything but we'll pick a couple and try them.
310
digester.addRule("*/a", new TestRule("a_tail"));
311
312
313         List JavaDoc list = null;
314         Iterator JavaDoc it = null;
315
316         list = digester.getRules().match(null, "a");
317
318         assertEquals("Testing tail wrong size (A)", 1, list.size());
319         assertEquals("Testing tail mismatch (B)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
320
321
322         list = digester.getRules().match(null, "beta/a");
323
324         assertEquals("Testing tail wrong size (C)", 1, list.size());
325         assertEquals("Testing tail mismatch (D)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
326
327         list = digester.getRules().match(null, "be/aaa");
328
329         assertEquals("Testing tail no matches (E)", 0, list.size());
330         
331         list = digester.getRules().match(null, "aaa");
332
333         assertEquals("Testing tail no matches (F)", 0, list.size());
334         
335         list = digester.getRules().match(null, "a/beta");
336
337         assertEquals("Testing tail no matches (G)", 0, list.size());
338
339         // clean up
340
digester.getRules().clear();
341
342     }
343     
344     public void testAncesterMatch() throws Exception JavaDoc {
345         // test fixed root ancester
346
digester.getRules().clear();
347         
348         digester.addRule("!a/b/*", new TestRule("uni-a-b-star"));
349         digester.addRule("a/b/*", new TestRule("a-b-star"));
350         digester.addRule("a/b/c", new TestRule("a-b-c"));
351         digester.addRule("a/b/?", new TestRule("a-b-child"));
352         
353         List JavaDoc
354         list = digester.getRules().match(null, "a/b/c");
355         
356         assertEquals("Simple ancester matches (1)", 2, list.size());
357         assertEquals("Univeral ancester mismatch (1)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
358         assertEquals("Parent precedence failure", "a-b-c" , ((TestRule) list.get(1)).getIdentifier());
359         
360         list = digester.getRules().match(null, "a/b/b");
361         assertEquals("Simple ancester matches (2)", 2, list.size());
362         assertEquals("Univeral ancester mismatch (2)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
363         assertEquals("Child precedence failure", "a-b-child" , ((TestRule) list.get(1)).getIdentifier());
364         
365         list = digester.getRules().match(null, "a/b/d");
366         assertEquals("Simple ancester matches (3)", 2, list.size());
367         assertEquals("Univeral ancester mismatch (3)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
368         assertEquals("Ancester mismatch (1)", "a-b-child" , ((TestRule) list.get(1)).getIdentifier());
369
370         list = digester.getRules().match(null, "a/b/d/e/f");
371         assertEquals("Simple ancester matches (4)", 2, list.size());
372         assertEquals("Univeral ancester mismatch (4)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
373         assertEquals("Ancester mismatch (2)", "a-b-star" , ((TestRule) list.get(1)).getIdentifier());
374         
375         // test wild root ancester
376
digester.getRules().clear();
377
378         digester.addRule("!*/a/b/*", new TestRule("uni-star-a-b-star"));
379         digester.addRule("*/b/c/*", new TestRule("star-b-c-star"));
380         digester.addRule("*/b/c/d", new TestRule("star-b-c-d"));
381         digester.addRule("a/b/c", new TestRule("a-b-c"));
382         
383         list = digester.getRules().match(null, "a/b/c");
384         assertEquals("Wild ancester match (1)", 2, list.size());
385         assertEquals(
386                     "Univeral ancester mismatch (5)",
387                     "uni-star-a-b-star" ,
388                     ((TestRule) list.get(0)).getIdentifier());
389         assertEquals("Match missed (1)", "a-b-c" , ((TestRule) list.get(1)).getIdentifier());
390         
391         list = digester.getRules().match(null, "b/c");
392         assertEquals("Wild ancester match (2)", 1, list.size());
393         assertEquals("Match missed (2)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier());
394         
395         list = digester.getRules().match(null, "a/b/c/d");
396         assertEquals("Wild ancester match (3)", 2, list.size());
397         assertEquals("Match missed (3)", "uni-star-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
398         assertEquals("Match missed (4)", "star-b-c-d" , ((TestRule) list.get(1)).getIdentifier());
399         
400         list = digester.getRules().match(null, "b/b/c/e/d");
401         assertEquals("Wild ancester match (2)", 1, list.size());
402         assertEquals("Match missed (5)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier());
403     }
404     
405     public void testLongMatch() {
406         
407         digester.getRules().clear();
408
409         digester.addRule("a/b/c/d/*", new TestRule("a-b-c-d-star"));
410         
411         List JavaDoc list = digester.getRules().match(null, "a/b/c/d/e");
412         assertEquals("Long match (1)", 1, list.size());
413         assertEquals("Match missed (1)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier());
414         
415         list = digester.getRules().match(null, "a/b/c/d/e/f");
416         assertEquals("Long match (2)", 1, list.size());
417         assertEquals("Match missed (2)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier());
418         
419         list = digester.getRules().match(null, "a/b/c/d/e/f/g");
420         assertEquals("Long match (3)", 1, list.size());
421         assertEquals("Match missed (3)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier());
422         
423         list = digester.getRules().match(null, "a/b/c/d");
424         assertEquals("Long match (4)", 0, list.size());
425     }
426     
427     public void testInstructors() {
428         digester.getRules().clear();
429         
430         digester.addRule("!instructors/*", new TestRule("instructors"));
431         digester.addRule("!instructor/*", new TestRule("instructor"));
432         
433         List JavaDoc list = digester.getRules().match(null, "instructors");
434         assertEquals("Only expect to match instructors", 1, list.size());
435         assertEquals("Instructors expected", "instructors" , ((TestRule) list.get(0)).getIdentifier());
436
437     }
438     
439     public void testMiddleInstructors() {
440         digester.getRules().clear();
441         
442         digester.addRule("!instructors/*", new TestRule("instructors"));
443         
444         List JavaDoc list = digester.getRules().match(null, "/tosh/instructors/fiddlesticks");
445         assertEquals("No matches expected", 0, list.size());
446
447     }
448 }
449
Popular Tags