~ubuntu-branches/ubuntu/maverick/libcommons-digester-java/maverick

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/digester/EBRTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2004-08-13 01:59:24 UTC
  • Revision ID: james.westby@ubuntu.com-20040813015924-kxkbfi0a1u5cbxbr
Tags: 1.5.0.1-3
rebuild with J2SDK1.3 comparible mode.(closes: #265253)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.7 $
 
4
 * $Date: 2003/03/12 22:10:51 $
 
5
 *
 
6
 * ====================================================================
 
7
 *
 
8
 * The Apache Software License, Version 1.1
 
9
 *
 
10
 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
 
11
 * reserved.
 
12
 *
 
13
 * Redistribution and use in source and binary forms, with or without
 
14
 * modification, are permitted provided that the following conditions
 
15
 * are met:
 
16
 *
 
17
 * 1. Redistributions of source code must retain the above copyright
 
18
 *    notice, this list of conditions and the following disclaimer.
 
19
 *
 
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
 
23
 *    distribution.
 
24
 *
 
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.
 
31
 *
 
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.
 
36
 *
 
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.
 
40
 *
 
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
 
52
 * SUCH DAMAGE.
 
53
 * ====================================================================
 
54
 *
 
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/>.
 
59
 *
 
60
 */
 
61
 
 
62
 
 
63
package org.apache.commons.digester;
 
64
 
 
65
 
 
66
import java.util.Iterator;
 
67
import java.util.List;
 
68
 
 
69
import junit.framework.Test;
 
70
import junit.framework.TestSuite;
 
71
 
 
72
 
 
73
/**
 
74
 * <p> Runs standard tests for RulesBase as well as tests of extensions.
 
75
 *
 
76
 * @author Robert Burrell Donkin <robertdonkin@mac.com>
 
77
 * @version $Revision: 1.7 $ $Date: 2003/03/12 22:10:51 $
 
78
 */
 
79
 
 
80
 
 
81
public class EBRTestCase extends RulesBaseTestCase {
 
82
 
 
83
 
 
84
    // ----------------------------------------------------------- Constructors
 
85
 
 
86
    /**
 
87
     * Construct a new instance of this test case.
 
88
     *
 
89
     * @param name Name of the test case
 
90
     */
 
91
    public EBRTestCase(String name) {
 
92
 
 
93
        super(name);
 
94
    }
 
95
 
 
96
 
 
97
    // -------------------------------------------------- Overall Test Methods
 
98
 
 
99
    /**
 
100
     * <p> This should be overriden by subclasses.
 
101
     *
 
102
     * @return the matching rules to be tested.
 
103
     */
 
104
    protected Rules createMatchingRulesForTest() {
 
105
 
 
106
        return new ExtendedBaseRules();
 
107
    }
 
108
 
 
109
 
 
110
    /**
 
111
     * Return the tests included in this test suite.
 
112
     */
 
113
    public static Test suite() {
 
114
 
 
115
        return (new TestSuite(EBRTestCase.class));
 
116
 
 
117
    }
 
118
 
 
119
 
 
120
    /**
 
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.
 
125
     */
 
126
    public void testBasicParentMatch() {
 
127
 
 
128
        // clear any existing rules
 
129
        digester.getRules().clear();
 
130
 
 
131
        assertEquals("Initial rules list is empty",
 
132
                0, digester.getRules().rules().size());
 
133
 
 
134
        // Set up rules
 
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"));
 
141
 
 
142
 
 
143
        List list = null;
 
144
        Iterator it = null;
 
145
 
 
146
        // this should match just the exact since this has presidence
 
147
        list = digester.getRules().match(null, "alpha/beta/gamma/delta");
 
148
 
 
149
        // all three rules should match
 
150
        assertEquals("Testing basic parent mismatch (A)", 1, list.size());
 
151
 
 
152
        it = list.iterator();
 
153
        assertEquals("Testing basic parent mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier());
 
154
 
 
155
 
 
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");
 
158
 
 
159
        // all three rules should match
 
160
        assertEquals("Testing basic parent mismatch (C)", 1, list.size());
 
161
 
 
162
        it = list.iterator();
 
163
        assertEquals("Testing basic parent mismatch (D)", "exact_parent", ((TestRule) it.next()).getIdentifier());
 
164
 
 
165
 
 
166
        // wild child overrides wild parent
 
167
        list = digester.getRules().match(null, "alpha/omega/beta/gamma/epsilon");
 
168
 
 
169
        // all three rules should match
 
170
        assertEquals("Testing basic parent mismatch (E)", 1, list.size());
 
171
 
 
172
        it = list.iterator();
 
173
        assertEquals("Testing basic parent mismatch (F)", "wild_child", ((TestRule) it.next()).getIdentifier());
 
174
 
 
175
 
 
176
        // nothing else matches so return wild parent
 
177
        list = digester.getRules().match(null, "alpha/omega/beta/gamma/zeta");
 
178
 
 
179
        // all three rules should match
 
180
        assertEquals("Testing basic parent mismatch (G)", 1, list.size());
 
181
 
 
182
        it = list.iterator();
 
183
        assertEquals("Testing basic parent mismatch (H)", "wild_parent", ((TestRule) it.next()).getIdentifier());
 
184
 
 
185
 
 
186
        // clean up
 
187
        digester.getRules().clear();
 
188
 
 
189
    }
 
190
 
 
191
 
 
192
    /**
 
193
     * Basic test of universal matching rules.
 
194
     * Universal rules act independent.
 
195
     */
 
196
    public void testBasicUniversal() {
 
197
 
 
198
        // clear any existing rules
 
199
        digester.getRules().clear();
 
200
 
 
201
        assertEquals("Initial rules list is empty",
 
202
                0, digester.getRules().rules().size());
 
203
 
 
204
        // Set up rules
 
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"));
 
215
 
 
216
 
 
217
        List list = null;
 
218
        Iterator it = null;
 
219
 
 
220
        // test universal wild head
 
221
        list = digester.getRules().match(null, "alpha/beta/gamma");
 
222
 
 
223
        assertEquals("Testing universal wildcard mismatch (A)", 2, list.size());
 
224
 
 
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());
 
228
 
 
229
 
 
230
        // test universal parent
 
231
        list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
 
232
 
 
233
        assertEquals("Testing universal wildcard mismatch (D)", 2, list.size());
 
234
 
 
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());
 
238
 
 
239
        // test universal parent
 
240
        list = digester.getRules().match(null, "alpha/beta/gamma/zeta");
 
241
 
 
242
        assertEquals("Testing universal wildcard mismatch (G)", 2, list.size());
 
243
 
 
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());
 
247
 
 
248
 
 
249
        // test wildcard universal parent
 
250
        list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/alpha");
 
251
 
 
252
        assertEquals("Testing universal wildcard mismatch (J)", 2, list.size());
 
253
 
 
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());
 
257
 
 
258
        // test wildcard universal parent
 
259
        list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/zeta");
 
260
 
 
261
        assertEquals("Testing universal wildcard mismatch (M)", 2, list.size());
 
262
 
 
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());
 
266
 
 
267
 
 
268
        // clean up
 
269
        digester.getRules().clear();
 
270
 
 
271
    }
 
272
 
 
273
 
 
274
    /**
 
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.
 
279
     */
 
280
    public void testWildMatch() {
 
281
 
 
282
        // clear any existing rules
 
283
        digester.getRules().clear();
 
284
 
 
285
        assertEquals("Initial rules list is empty",
 
286
                0, digester.getRules().rules().size());
 
287
 
 
288
        // Set up rules
 
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"));
 
294
 
 
295
 
 
296
        List list = null;
 
297
        Iterator it = null;
 
298
 
 
299
        // The universal wild will always match whatever else does
 
300
        list = digester.getRules().match(null, "alpha/beta/gamma/delta");
 
301
 
 
302
        // all three rules should match
 
303
        assertEquals("Testing wild mismatch (A)", 2, list.size());
 
304
 
 
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());
 
308
 
 
309
 
 
310
        // The universal wild will always match whatever else does
 
311
        list = digester.getRules().match(null, "alpha/beta/gamma/epsilon");
 
312
 
 
313
        assertEquals("Testing wild mismatch (D)", 2, list.size());
 
314
 
 
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());
 
318
 
 
319
 
 
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");
 
323
 
 
324
        assertEquals("Testing wild mismatch (G)", 2, list.size());
 
325
 
 
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());
 
329
 
 
330
 
 
331
        // clean up
 
332
        digester.getRules().clear();
 
333
 
 
334
    }
 
335
    
 
336
    
 
337
 
 
338
    /**
 
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.
 
343
     */
 
344
    public void testRootTailMatch() {
 
345
 
 
346
        // clear any existing rules
 
347
        digester.getRules().clear();
 
348
 
 
349
        assertEquals("Initial rules list is empty",
 
350
                0, digester.getRules().rules().size());
 
351
 
 
352
        // Set up rules
 
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"));
 
355
 
 
356
 
 
357
        List list = null;
 
358
        Iterator it = null;
 
359
 
 
360
        list = digester.getRules().match(null, "a");
 
361
 
 
362
        assertEquals("Testing tail wrong size (A)", 1, list.size());
 
363
        assertEquals("Testing tail mismatch (B)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
 
364
 
 
365
 
 
366
        list = digester.getRules().match(null, "beta/a");
 
367
 
 
368
        assertEquals("Testing tail wrong size (C)", 1, list.size());
 
369
        assertEquals("Testing tail mismatch (D)", "a_tail", ((TestRule) list.get(0)).getIdentifier());
 
370
 
 
371
        list = digester.getRules().match(null, "be/aaa");
 
372
 
 
373
        assertEquals("Testing tail no matches (E)", 0, list.size());
 
374
        
 
375
        list = digester.getRules().match(null, "aaa");
 
376
 
 
377
        assertEquals("Testing tail no matches (F)", 0, list.size());
 
378
        
 
379
        list = digester.getRules().match(null, "a/beta");
 
380
 
 
381
        assertEquals("Testing tail no matches (G)", 0, list.size());
 
382
 
 
383
        // clean up
 
384
        digester.getRules().clear();
 
385
 
 
386
    }
 
387
    
 
388
    public void testAncesterMatch() throws Exception {
 
389
        System.out.println("Starting ancester match...");
 
390
        // test fixed root ancester
 
391
        digester.getRules().clear();
 
392
        
 
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"));
 
397
        
 
398
        List
 
399
        list = digester.getRules().match(null, "a/b/c");
 
400
        
 
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());
 
404
        
 
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());
 
409
        
 
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());
 
414
 
 
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());
 
419
        
 
420
        // test wild root ancester
 
421
        digester.getRules().clear();
 
422
 
 
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"));
 
427
        
 
428
        list = digester.getRules().match(null, "a/b/c");  
 
429
        assertEquals("Wild ancester match (1)", 2, list.size());
 
430
        assertEquals(
 
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());
 
435
        
 
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());    
 
439
        
 
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());
 
444
        
 
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.");
 
449
    }
 
450
}