2
* $Header: /home/cvspublic/jakarta-commons/digester/src/test/org/apache/commons/digester/EBRTestCase.java,v 1.7 2003/03/12 22:10:51 rdonkin Exp $
4
* $Date: 2003/03/12 22:10:51 $
6
* ====================================================================
8
* The Apache Software License, Version 1.1
10
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
13
* Redistribution and use in source and binary forms, with or without
14
* modification, are permitted provided that the following conditions
17
* 1. Redistributions of source code must retain the above copyright
18
* notice, this list of conditions and the following disclaimer.
20
* 2. Redistributions in binary form must reproduce the above copyright
21
* notice, this list of conditions and the following disclaimer in
22
* the documentation and/or other materials provided with the
25
* 3. The end-user documentation included with the redistribution, if
26
* any, must include the following acknowlegement:
27
* "This product includes software developed by the
28
* Apache Software Foundation (http://www.apache.org/)."
29
* Alternately, this acknowlegement may appear in the software itself,
30
* if and wherever such third-party acknowlegements normally appear.
32
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
33
* Foundation" must not be used to endorse or promote products derived
34
* from this software without prior written permission. For written
35
* permission, please contact apache@apache.org.
37
* 5. Products derived from this software may not be called "Apache"
38
* nor may "Apache" appear in their names without prior written
39
* permission of the Apache Group.
41
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53
* ====================================================================
55
* This software consists of voluntary contributions made by many
56
* individuals on behalf of the Apache Software Foundation. For more
57
* information on the Apache Software Foundation, please see
58
* <http://www.apache.org/>.
63
package org.apache.commons.digester;
66
import java.util.Iterator;
67
import java.util.List;
69
import junit.framework.Test;
70
import junit.framework.TestSuite;
74
* <p> Runs standard tests for RulesBase as well as tests of extensions.
76
* @author Robert Burrell Donkin <robertdonkin@mac.com>
77
* @version $Revision: 1.7 $ $Date: 2003/03/12 22:10:51 $
81
public class EBRTestCase extends RulesBaseTestCase {
84
// ----------------------------------------------------------- Constructors
87
* Construct a new instance of this test case.
89
* @param name Name of the test case
91
public EBRTestCase(String name) {
97
// -------------------------------------------------- Overall Test Methods
100
* <p> This should be overriden by subclasses.
102
* @return the matching rules to be tested.
104
protected Rules createMatchingRulesForTest() {
106
return new ExtendedBaseRules();
111
* Return the tests included in this test suite.
113
public static Test suite() {
115
return (new TestSuite(EBRTestCase.class));
121
* Basic test of parent matching rules.
122
* A parent match matches any child of a particular kind of parent.
123
* A wild parent has a wildcard prefix.
124
* This method tests non-universal wildcards.
126
public void testBasicParentMatch() {
128
// clear any existing rules
129
digester.getRules().clear();
131
assertEquals("Initial rules list is empty",
132
0, digester.getRules().rules().size());
135
// since these are all NON-UNIVERSAL matches
136
// only expect one match at each stage
137
digester.addRule("alpha/beta/gamma/delta", new TestRule("exact"));
138
digester.addRule("*/beta/gamma/epsilon", new TestRule("wild_child"));
139
digester.addRule("alpha/beta/gamma/?", new TestRule("exact_parent"));
140
digester.addRule("*/beta/gamma/?", new TestRule("wild_parent"));
146
// this should match just the exact since this has presidence
147
list = digester.getRules().match(null, "alpha/beta/gamma/delta");
149
// all three rules should match
150
assertEquals("Testing basic parent mismatch (A)", 1, list.size());
152
it = list.iterator();
153
assertEquals("Testing basic parent mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier());
156
// we don't have an exact match for this child so we should get the exact parent
157
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
159
// all three rules should match
160
assertEquals("Testing basic parent mismatch (C)", 1, list.size());
162
it = list.iterator();
163
assertEquals("Testing basic parent mismatch (D)", "exact_parent", ((TestRule) it.next()).getIdentifier());
166
// wild child overrides wild parent
167
list = digester.getRules().match(null, "alpha/omega/beta/gamma/epsilon");
169
// all three rules should match
170
assertEquals("Testing basic parent mismatch (E)", 1, list.size());
172
it = list.iterator();
173
assertEquals("Testing basic parent mismatch (F)", "wild_child", ((TestRule) it.next()).getIdentifier());
176
// nothing else matches so return wild parent
177
list = digester.getRules().match(null, "alpha/omega/beta/gamma/zeta");
179
// all three rules should match
180
assertEquals("Testing basic parent mismatch (G)", 1, list.size());
182
it = list.iterator();
183
assertEquals("Testing basic parent mismatch (H)", "wild_parent", ((TestRule) it.next()).getIdentifier());
187
digester.getRules().clear();
193
* Basic test of universal matching rules.
194
* Universal rules act independent.
196
public void testBasicUniversal() {
198
// clear any existing rules
199
digester.getRules().clear();
201
assertEquals("Initial rules list is empty",
202
0, digester.getRules().rules().size());
205
// set up universal matches against non-universal ones
206
digester.addRule("alpha/beta/gamma", new TestRule("exact"));
207
digester.addRule("*/beta/gamma", new TestRule("non_wild_head"));
208
digester.addRule("!*/beta/gamma", new TestRule("universal_wild_head"));
209
digester.addRule("!alpha/beta/gamma/?", new TestRule("universal_wild_child"));
210
digester.addRule("alpha/beta/gamma/?", new TestRule("non_wild_child"));
211
digester.addRule("alpha/beta/gamma/epsilon", new TestRule("exact2"));
212
digester.addRule("alpha/epsilon/beta/gamma/zeta", new TestRule("exact3"));
213
digester.addRule("*/gamma/?", new TestRule("non_wildhead_child"));
214
digester.addRule("!*/epsilon/beta/gamma/?", new TestRule("universal_wildhead_child"));
220
// test universal wild head
221
list = digester.getRules().match(null, "alpha/beta/gamma");
223
assertEquals("Testing universal wildcard mismatch (A)", 2, list.size());
225
it = list.iterator();
226
assertEquals("Testing universal wildcard mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier());
227
assertEquals("Testing universal wildcard mismatch (C)", "universal_wild_head", ((TestRule) it.next()).getIdentifier());
230
// test universal parent
231
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
233
assertEquals("Testing universal wildcard mismatch (D)", 2, list.size());
235
it = list.iterator();
236
assertEquals("Testing universal wildcard mismatch (E)", "universal_wild_child", ((TestRule) it.next()).getIdentifier());
237
assertEquals("Testing universal wildcard mismatch (F)", "exact2", ((TestRule) it.next()).getIdentifier());
239
// test universal parent
240
list = digester.getRules().match(null, "alpha/beta/gamma/zeta");
242
assertEquals("Testing universal wildcard mismatch (G)", 2, list.size());
244
it = list.iterator();
245
assertEquals("Testing universal wildcard mismatch (H)", "universal_wild_child", ((TestRule) it.next()).getIdentifier());
246
assertEquals("Testing universal wildcard mismatch (I)", "non_wild_child", ((TestRule) it.next()).getIdentifier());
249
// test wildcard universal parent
250
list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/alpha");
252
assertEquals("Testing universal wildcard mismatch (J)", 2, list.size());
254
it = list.iterator();
255
assertEquals("Testing universal wildcard mismatch (K)", "non_wildhead_child", ((TestRule) it.next()).getIdentifier());
256
assertEquals("Testing universal wildcard mismatch (L)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier());
258
// test wildcard universal parent
259
list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/zeta");
261
assertEquals("Testing universal wildcard mismatch (M)", 2, list.size());
263
it = list.iterator();
264
assertEquals("Testing universal wildcard mismatch (M)", "exact3", ((TestRule) it.next()).getIdentifier());
265
assertEquals("Testing universal wildcard mismatch (O)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier());
269
digester.getRules().clear();
275
* Basic test of wild matches.
276
* A universal will match matches anything!
277
* A non-universal will match matches anything not matched by something else.
278
* This method tests non-universal and universal wild matches.
280
public void testWildMatch() {
282
// clear any existing rules
283
digester.getRules().clear();
285
assertEquals("Initial rules list is empty",
286
0, digester.getRules().rules().size());
289
// The combinations a little large to test everything but we'll pick a couple and try them.
290
digester.addRule("*", new TestRule("basic_wild"));
291
digester.addRule("!*", new TestRule("universal_wild"));
292
digester.addRule("alpha/beta/gamma/delta", new TestRule("exact"));
293
digester.addRule("*/beta/gamma/?", new TestRule("wild_parent"));
299
// The universal wild will always match whatever else does
300
list = digester.getRules().match(null, "alpha/beta/gamma/delta");
302
// all three rules should match
303
assertEquals("Testing wild mismatch (A)", 2, list.size());
305
it = list.iterator();
306
assertEquals("Testing wild mismatch (B)", "universal_wild", ((TestRule) it.next()).getIdentifier());
307
assertEquals("Testing wild mismatch (C)", "exact", ((TestRule) it.next()).getIdentifier());
310
// The universal wild will always match whatever else does
311
list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
313
assertEquals("Testing wild mismatch (D)", 2, list.size());
315
it = list.iterator();
316
assertEquals("Testing wild mismatch (E)", "universal_wild", ((TestRule) it.next()).getIdentifier());
317
assertEquals("Testing wild mismatch (F)", "wild_parent", ((TestRule) it.next()).getIdentifier());
320
// The universal wild will always match whatever else does
321
// we have no other non-universal matching so this will match the non-universal wild as well
322
list = digester.getRules().match(null, "alpha/gamma");
324
assertEquals("Testing wild mismatch (G)", 2, list.size());
326
it = list.iterator();
327
assertEquals("Testing wild mismatch (H)", "basic_wild", ((TestRule) it.next()).getIdentifier());
328
assertEquals("Testing wild mismatch (I)", "universal_wild", ((TestRule) it.next()).getIdentifier());
332
digester.getRules().clear();
339
* Basic test of wild matches.
340
* A universal will match matches anything!
341
* A non-universal will match matches anything not matched by something else.
342
* This method tests non-universal and universal wild matches.
344
public void testRootTailMatch() {
346
// clear any existing rules
347
digester.getRules().clear();
349
assertEquals("Initial rules list is empty",
350
0, digester.getRules().rules().size());
353
// The combinations a little large to test everything but we'll pick a couple and try them.
354
digester.addRule("*/a", new TestRule("a_tail"));
360
list = digester.getRules().match(null, "a");
362
assertEquals("Testing tail wrong size (A)", 1, list.size());
363
assertEquals("Testing tail mismatch (B)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
366
list = digester.getRules().match(null, "beta/a");
368
assertEquals("Testing tail wrong size (C)", 1, list.size());
369
assertEquals("Testing tail mismatch (D)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
371
list = digester.getRules().match(null, "be/aaa");
373
assertEquals("Testing tail no matches (E)", 0, list.size());
375
list = digester.getRules().match(null, "aaa");
377
assertEquals("Testing tail no matches (F)", 0, list.size());
379
list = digester.getRules().match(null, "a/beta");
381
assertEquals("Testing tail no matches (G)", 0, list.size());
384
digester.getRules().clear();
388
public void testAncesterMatch() throws Exception {
389
System.out.println("Starting ancester match...");
390
// test fixed root ancester
391
digester.getRules().clear();
393
digester.addRule("!a/b/*", new TestRule("uni-a-b-star"));
394
digester.addRule("a/b/*", new TestRule("a-b-star"));
395
digester.addRule("a/b/c", new TestRule("a-b-c"));
396
digester.addRule("a/b/?", new TestRule("a-b-child"));
399
list = digester.getRules().match(null, "a/b/c");
401
assertEquals("Simple ancester matches (1)", 2, list.size());
402
assertEquals("Univeral ancester mismatch (1)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
403
assertEquals("Parent precedence failure", "a-b-c" , ((TestRule) list.get(1)).getIdentifier());
405
list = digester.getRules().match(null, "a/b/b");
406
assertEquals("Simple ancester matches (2)", 2, list.size());
407
assertEquals("Univeral ancester mismatch (2)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
408
assertEquals("Child precedence failure", "a-b-child" , ((TestRule) list.get(1)).getIdentifier());
410
list = digester.getRules().match(null, "a/b/d");
411
assertEquals("Simple ancester matches (3)", 2, list.size());
412
assertEquals("Univeral ancester mismatch (3)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
413
assertEquals("Ancester mismatch (1)", "a-b-child" , ((TestRule) list.get(1)).getIdentifier());
415
list = digester.getRules().match(null, "a/b/d/e/f");
416
assertEquals("Simple ancester matches (4)", 2, list.size());
417
assertEquals("Univeral ancester mismatch (4)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
418
assertEquals("Ancester mismatch (2)", "a-b-star" , ((TestRule) list.get(1)).getIdentifier());
420
// test wild root ancester
421
digester.getRules().clear();
423
digester.addRule("!*/a/b/*", new TestRule("uni-star-a-b-star"));
424
digester.addRule("*/b/c/*", new TestRule("star-b-c-star"));
425
digester.addRule("*/b/c/d", new TestRule("star-b-c-d"));
426
digester.addRule("a/b/c", new TestRule("a-b-c"));
428
list = digester.getRules().match(null, "a/b/c");
429
assertEquals("Wild ancester match (1)", 2, list.size());
431
"Univeral ancester mismatch (5)",
432
"uni-star-a-b-star" ,
433
((TestRule) list.get(0)).getIdentifier());
434
assertEquals("Match missed (1)", "a-b-c" , ((TestRule) list.get(1)).getIdentifier());
436
list = digester.getRules().match(null, "b/c");
437
assertEquals("Wild ancester match (2)", 1, list.size());
438
assertEquals("Match missed (2)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier());
440
list = digester.getRules().match(null, "a/b/c/d");
441
assertEquals("Wild ancester match (3)", 2, list.size());
442
assertEquals("Match missed (3)", "uni-star-a-b-star" , ((TestRule) list.get(0)).getIdentifier());
443
assertEquals("Match missed (4)", "star-b-c-d" , ((TestRule) list.get(1)).getIdentifier());
445
list = digester.getRules().match(null, "b/b/c/e/d");
446
assertEquals("Wild ancester match (2)", 1, list.size());
447
assertEquals("Match missed (5)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier());
448
System.out.println("Finished ancester match.");