KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: WDRulesWrapperTestCase.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 package org.apache.commons.digester;
19
20 import java.util.List JavaDoc;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Test case for WithDefaultsRulesWrapper
26  *
27  * @author Robert Burrell Donkin
28  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
29  */

30
31 public class WDRulesWrapperTestCase extends TestCase {
32     
33     /** Base constructor */
34     public WDRulesWrapperTestCase(String JavaDoc name) {
35         super(name);
36     }
37     
38     public void testClear() {
39         // test clear wrapped
40
WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
41         rules.add("alpha", new TestRule("Tom"));
42         rules.add("alpha", new TestRule("Dick"));
43         rules.add("alpha", new TestRule("Harry"));
44         
45         assertNotNull("Rules should not be null", rules.rules());
46         assertEquals("Wrong number of rules registered (1)", 3 , rules.rules().size());
47         rules.clear();
48         assertEquals("Clear Failed (1)", 0 , rules.rules().size());
49         
50         // mixed
51
rules.add("alpha", new TestRule("Tom"));
52         rules.add("alpha", new TestRule("Dick"));
53         rules.add("alpha", new TestRule("Harry"));
54         rules.addDefault(new TestRule("Roger"));
55         assertEquals("Wrong number of rules registered (2)", 4 , rules.rules().size());
56         rules.clear();
57         assertEquals("Clear Failed (2)", 0 , rules.rules().size());
58         
59         rules.addDefault(new TestRule("Roger"));
60         assertEquals("Wrong number of rules registered (3)", 1 , rules.rules().size());
61         rules.clear();
62         assertEquals("Clear Failed (3)", 0 , rules.rules().size());
63     }
64     
65     public void testRules() {
66         // test rules
67
WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
68         rules.add("alpha", new TestRule("Tom"));
69         rules.add("alpha", new TestRule("Dick"));
70         rules.addDefault(new TestRule("Roger"));
71         rules.add("alpha", new TestRule("Harry"));
72         
73         assertNotNull("Rules should not be null", rules.rules());
74         assertEquals("Wrong order (1)", "Tom" , ((TestRule) rules.rules().get(0)).getIdentifier());
75         assertEquals("Wrong order (2)", "Dick" , ((TestRule) rules.rules().get(1)).getIdentifier());
76         assertEquals("Wrong order (3)", "Roger" , ((TestRule) rules.rules().get(2)).getIdentifier());
77         assertEquals("Wrong order (4)", "Harry" , ((TestRule) rules.rules().get(3)).getIdentifier());
78     }
79     
80     public void testMatch() {
81         // test no defaults
82
WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new RulesBase());
83         rules.add("alpha", new TestRule("Tom"));
84         rules.add("alpha", new TestRule("Dick"));
85         rules.add("alpha", new TestRule("Harry"));
86         rules.addDefault(new TestRule("Roger"));
87         rules.addDefault(new TestRule("Rabbit"));
88         
89         List JavaDoc matches = rules.match("", "alpha");
90         assertEquals("Wrong size (1)", 3 , matches.size());
91         assertEquals("Wrong order (1)", "Tom" , ((TestRule) matches.get(0)).getIdentifier());
92         assertEquals("Wrong order (2)", "Dick" , ((TestRule) matches.get(1)).getIdentifier());
93         assertEquals("Wrong order (3)", "Harry" , ((TestRule) matches.get(2)).getIdentifier());
94         
95         matches = rules.match("", "not-alpha");
96         assertEquals("Wrong size (2)", 2 , matches.size());
97         assertEquals("Wrong order (4)", "Roger" , ((TestRule) matches.get(0)).getIdentifier());
98         assertEquals("Wrong order (5)", "Rabbit" , ((TestRule) matches.get(1)).getIdentifier());
99     }
100 }
101
Popular Tags