KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: RulesBaseTestCase.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.TestCase;
27 import junit.framework.TestSuite;
28
29
30 /**
31  * <p>Test Case for the RulesBase matching rules.
32  * Most of this material was original contained in the digester test case
33  * but was moved into this class so that extensions of the basic matching rules
34  * behaviour can extend this test case.
35  * </p>
36  *
37  * @author Craig R. McClanahan
38  * @version $Revision$ $Date: 2005-02-26 04:58:36 -0800 (Sat, 26 Feb 2005) $
39  */

40
41 public class RulesBaseTestCase extends TestCase {
42
43
44     // ----------------------------------------------------- Instance Variables
45

46
47     /**
48      * The digester instance we will be processing.
49      */

50     protected Digester digester = null;
51
52     // ----------------------------------------------------------- Constructors
53

54
55     /**
56      * Construct a new instance of this test case.
57      *
58      * @param name Name of the test case
59      */

60     public RulesBaseTestCase(String JavaDoc name) {
61
62         super(name);
63
64     }
65
66
67     // -------------------------------------------------- Overall Test Methods
68

69
70     /**
71      * Set up instance variables required by this test case.
72      */

73     public void setUp() {
74
75         digester = new Digester();
76         digester.setRules(createMatchingRulesForTest());
77
78     }
79
80     /**
81      * <p> This should be overriden by subclasses.
82      *
83      * @return the matching rules to be tested.
84      */

85     protected Rules createMatchingRulesForTest() {
86         return new RulesBase();
87     }
88
89     /**
90      * Return the tests included in this test suite.
91      */

92     public static Test suite() {
93
94         return (new TestSuite(RulesBaseTestCase.class));
95
96     }
97
98
99     /**
100      * Tear down instance variables required by this test case.
101      */

102     public void tearDown() {
103
104         digester = null;
105
106     }
107
108
109
110     // ------------------------------------------------ Individual Test Methods
111

112     /**
113      * Basic test for rule creation and matching.
114      */

115     public void testRules() {
116
117         // clear any existing rules
118
digester.getRules().clear();
119
120         // perform tests
121
List JavaDoc list = null;
122
123         assertEquals("Initial rules list is empty",
124                 0, digester.getRules().match("a").size());
125         digester.addSetProperties("a");
126         assertEquals("Add a matching rule",
127                 1, digester.getRules().match(null, "a").size());
128         digester.addSetProperties("b");
129         assertEquals("Add a non-matching rule",
130                 1, digester.getRules().match(null, "a").size());
131         digester.addSetProperties("a/b");
132         assertEquals("Add a non-matching nested rule",
133                 1, digester.getRules().match(null, "a").size());
134         digester.addSetProperties("a/b");
135         assertEquals("Add a second matching rule",
136                 2, digester.getRules().match(null, "a/b").size());
137
138         // clean up
139
digester.getRules().clear();
140
141     }
142
143     /**
144      * <p>Test matching rules in {@link RulesBase}.</p>
145      *
146      * <p>Tests:</p>
147      * <ul>
148      * <li>exact match</li>
149      * <li>tail match</li>
150      * <li>longest pattern rule</li>
151      * </ul>
152      */

153     public void testRulesBase() {
154
155         // clear any existing rules
156
digester.getRules().clear();
157
158         assertEquals("Initial rules list is empty",
159                 0, digester.getRules().rules().size());
160
161         // We're going to set up
162
digester.addRule("a/b/c/d", new TestRule("a/b/c/d"));
163         digester.addRule("*/d", new TestRule("*/d"));
164         digester.addRule("*/c/d", new TestRule("*/c/d"));
165
166         // Test exact match
167
assertEquals("Exact match takes precedence 1",
168                 1, digester.getRules().match(null, "a/b/c/d").size());
169         assertEquals("Exact match takes precedence 2",
170                 "a/b/c/d",
171                 ((TestRule) digester.getRules().match(null, "a/b/c/d").iterator().next()).getIdentifier());
172
173         // Test wildcard tail matching
174
assertEquals("Wildcard tail matching rule 1",
175                 1, digester.getRules().match(null, "a/b/d").size());
176         assertEquals("Wildcard tail matching rule 2",
177                 "*/d",
178                 ((TestRule) digester.getRules().match(null, "a/b/d").iterator().next()).getIdentifier());
179
180         // Test the longest matching pattern rule
181
assertEquals("Longest tail rule 1",
182                 1, digester.getRules().match(null, "x/c/d").size());
183         assertEquals("Longest tail rule 2",
184                 "*/c/d",
185                 ((TestRule) digester.getRules().match(null, "x/c/d").iterator().next()).getIdentifier());
186
187         // Test wildcard tail matching at the top level,
188
// i.e. the wildcard is nothing
189
digester.addRule("*/a", new TestRule("*/a"));
190         assertEquals("Wildcard tail matching rule 3",
191                      1,
192                      digester.getRules().match(null,"a").size());
193
194         assertEquals("Wildcard tail matching rule 3 (match too much)",
195                      0,
196                      digester.getRules().match(null,"aa").size());
197         // clean up
198
digester.getRules().clear();
199
200     }
201
202     /**
203      * Test basic matchings involving namespaces.
204      */

205     public void testBasicNamespaceMatching() {
206
207         List JavaDoc list = null;
208         Iterator JavaDoc it = null;
209
210         // clear any existing rules
211
digester.getRules().clear();
212
213         assertEquals("Initial rules list is empty",
214                 0, digester.getRules().rules().size());
215
216         // Set up rules
217
digester.addRule("alpha/beta/gamma", new TestRule("No-Namespace"));
218         digester.addRule("alpha/beta/gamma", new TestRule("Euclidean-Namespace", "euclidean"));
219
220
221         list = digester.getRules().rules();
222
223         // test that matching null namespace brings back namespace and non-namespace rules
224
list = digester.getRules().match(null, "alpha/beta/gamma");
225
226         assertEquals("Null namespace match (A)", 2, list.size());
227
228         it = list.iterator();
229         assertEquals("Null namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier());
230         assertEquals("Null namespace match (C)", "Euclidean-Namespace", ((TestRule) it.next()).getIdentifier());
231
232
233
234         // test that matching euclid namespace brings back namespace and non-namespace rules
235
list = digester.getRules().match("euclidean", "alpha/beta/gamma");
236
237         assertEquals("Matching namespace match (A)", 2, list.size());
238
239         it = list.iterator();
240         assertEquals("Matching namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier());
241         assertEquals("Matching namespace match (C)", "Euclidean-Namespace", ((TestRule) it.next()).getIdentifier());
242
243
244
245         // test that matching another namespace brings back only non-namespace rule
246
list = digester.getRules().match("hyperbolic", "alpha/beta/gamma");
247
248         assertEquals("Non matching namespace match (A)", 1, list.size());
249
250         it = list.iterator();
251         assertEquals("Non matching namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier());
252
253         // clean up
254
digester.getRules().clear();
255
256     }
257
258     /**
259      * Rules must always be returned in the correct order.
260      */

261     public void testOrdering() {
262
263         // clear any existing rules
264
digester.getRules().clear();
265
266         assertEquals("Initial rules list is empty",
267                 0, digester.getRules().rules().size());
268
269         // Set up rules
270
digester.addRule("alpha/beta/gamma", new TestRule("one"));
271         digester.addRule("alpha/beta/gamma", new TestRule("two"));
272         digester.addRule("alpha/beta/gamma", new TestRule("three"));
273
274         // test that rules are returned in set order
275
List JavaDoc list = digester.getRules().match(null, "alpha/beta/gamma");
276
277         assertEquals("Testing ordering mismatch (A)", 3, list.size());
278
279         Iterator JavaDoc it = list.iterator();
280         assertEquals("Testing ordering mismatch (B)", "one", ((TestRule) it.next()).getIdentifier());
281         assertEquals("Testing ordering mismatch (C)", "two", ((TestRule) it.next()).getIdentifier());
282         assertEquals("Testing ordering mismatch (D)", "three", ((TestRule) it.next()).getIdentifier());
283
284         // clean up
285
digester.getRules().clear();
286
287     }
288     
289     /** Tests the behaviour when a rule is added with a trailing slash*/
290     public void testTrailingSlash() {
291         // clear any existing rules
292
digester.getRules().clear();
293
294         assertEquals("Initial rules list is empty",
295                 0, digester.getRules().rules().size());
296
297         // Set up rules
298
digester.addRule("alpha/beta/gamma/", new TestRule("one"));
299         digester.addRule("alpha/beta/", new TestRule("two"));
300         digester.addRule("beta/gamma/alpha", new TestRule("three"));
301
302         // test that rules are returned in set order
303
List JavaDoc list = digester.getRules().match(null, "alpha/beta/gamma");
304
305         assertEquals("Testing number of matches", 1, list.size());
306
307         Iterator JavaDoc it = list.iterator();
308         assertEquals("Testing ordering (A)", "one", ((TestRule) it.next()).getIdentifier());
309
310         // clean up
311
digester.getRules().clear();
312     }
313 }
314
Popular Tags