View Javadoc

1   /* $Id: ExtendedBaseRulesTestCase.java 1212599 2011-12-09 19:46:42Z simonetripodi $
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.digester3;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.digester3.ExtendedBaseRules;
27  import org.apache.commons.digester3.Rule;
28  import org.apache.commons.digester3.Rules;
29  import org.junit.Test;
30  
31  /**
32   * <p>
33   * Runs standard tests for RulesBase as well as tests of extensions.
34   *
35   * @author Robert Burrell Donkin <robertdonkin@mac.com>
36   */
37  public class ExtendedBaseRulesTestCase
38      extends RulesBaseTestCase
39  {
40  
41      // -------------------------------------------------- Overall Test Methods
42  
43      /**
44       * <p>
45       * This should be overriden by subclasses.
46       *
47       * @return the matching rules to be tested.
48       */
49      @Override
50      protected Rules createMatchingRulesForTest()
51      {
52  
53          return new ExtendedBaseRules();
54      }
55  
56      /**
57       * Basic test of parent matching rules. A parent match matches any child of a particular kind of parent. A wild
58       * parent has a wildcard prefix. This method tests non-universal wildcards.
59       */
60      @Test
61      public void testBasicParentMatch()
62      {
63  
64          // clear any existing rules
65          digester.getRules().clear();
66  
67          assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
68  
69          // Set up rules
70          // since these are all NON-UNIVERSAL matches
71          // only expect one match at each stage
72          digester.addRule( "alpha/beta/gamma/delta", new TestRule( "exact" ) );
73          digester.addRule( "*/beta/gamma/epsilon", new TestRule( "wild_child" ) );
74          digester.addRule( "alpha/beta/gamma/?", new TestRule( "exact_parent" ) );
75          digester.addRule( "*/beta/gamma/?", new TestRule( "wild_parent" ) );
76  
77          List<Rule> list = null;
78          Iterator<Rule> it = null;
79  
80          // this should match just the exact since this has presidence
81          list = digester.getRules().match( null, "alpha/beta/gamma/delta", null, null );
82  
83          // all three rules should match
84          assertEquals( "Testing basic parent mismatch (A)", 1, list.size() );
85  
86          it = list.iterator();
87          assertEquals( "Testing basic parent mismatch (B)", "exact", ( (TestRule) it.next() ).getIdentifier() );
88  
89          // we don't have an exact match for this child so we should get the exact parent
90          list = digester.getRules().match( null, "alpha/beta/gamma/epsilon", null, null );
91  
92          // all three rules should match
93          assertEquals( "Testing basic parent mismatch (C)", 1, list.size() );
94  
95          it = list.iterator();
96          assertEquals( "Testing basic parent mismatch (D)", "exact_parent", ( (TestRule) it.next() ).getIdentifier() );
97  
98          // wild child overrides wild parent
99          list = digester.getRules().match( null, "alpha/omega/beta/gamma/epsilon", null, null );
100 
101         // all three rules should match
102         assertEquals( "Testing basic parent mismatch (E)", 1, list.size() );
103 
104         it = list.iterator();
105         assertEquals( "Testing basic parent mismatch (F)", "wild_child", ( (TestRule) it.next() ).getIdentifier() );
106 
107         // nothing else matches so return wild parent
108         list = digester.getRules().match( null, "alpha/omega/beta/gamma/zeta", null, null );
109 
110         // all three rules should match
111         assertEquals( "Testing basic parent mismatch (G)", 1, list.size() );
112 
113         it = list.iterator();
114         assertEquals( "Testing basic parent mismatch (H)", "wild_parent", ( (TestRule) it.next() ).getIdentifier() );
115 
116         // clean up
117         digester.getRules().clear();
118 
119     }
120 
121     /**
122      * Basic test of universal matching rules. Universal rules act independent.
123      */
124     @Test
125     public void testBasicUniversal()
126     {
127 
128         // clear any existing rules
129         digester.getRules().clear();
130 
131         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
132 
133         // Set up rules
134         // set up universal matches against non-universal ones
135         digester.addRule( "alpha/beta/gamma", new TestRule( "exact" ) );
136         digester.addRule( "*/beta/gamma", new TestRule( "non_wild_head" ) );
137         digester.addRule( "!*/beta/gamma", new TestRule( "universal_wild_head" ) );
138         digester.addRule( "!alpha/beta/gamma/?", new TestRule( "universal_wild_child" ) );
139         digester.addRule( "alpha/beta/gamma/?", new TestRule( "non_wild_child" ) );
140         digester.addRule( "alpha/beta/gamma/epsilon", new TestRule( "exact2" ) );
141         digester.addRule( "alpha/epsilon/beta/gamma/zeta", new TestRule( "exact3" ) );
142         digester.addRule( "*/gamma/?", new TestRule( "non_wildhead_child" ) );
143         digester.addRule( "!*/epsilon/beta/gamma/?", new TestRule( "universal_wildhead_child" ) );
144 
145         List<Rule> list = null;
146         Iterator<Rule> it = null;
147 
148         // test universal wild head
149         list = digester.getRules().match( null, "alpha/beta/gamma", null, null );
150 
151         assertEquals( "Testing universal wildcard mismatch (A)", 2, list.size() );
152 
153         it = list.iterator();
154         assertEquals( "Testing universal wildcard mismatch (B)", "exact", ( (TestRule) it.next() ).getIdentifier() );
155         assertEquals( "Testing universal wildcard mismatch (C)", "universal_wild_head",
156                       ( (TestRule) it.next() ).getIdentifier() );
157 
158         // test universal parent
159         list = digester.getRules().match( null, "alpha/beta/gamma/epsilon", null, null );
160 
161         assertEquals( "Testing universal wildcard mismatch (D)", 2, list.size() );
162 
163         it = list.iterator();
164         assertEquals( "Testing universal wildcard mismatch (E)", "universal_wild_child",
165                       ( (TestRule) it.next() ).getIdentifier() );
166         assertEquals( "Testing universal wildcard mismatch (F)", "exact2", ( (TestRule) it.next() ).getIdentifier() );
167 
168         // test universal parent
169         list = digester.getRules().match( null, "alpha/beta/gamma/zeta", null, null );
170 
171         assertEquals( "Testing universal wildcard mismatch (G)", 2, list.size() );
172 
173         it = list.iterator();
174         assertEquals( "Testing universal wildcard mismatch (H)", "universal_wild_child",
175                       ( (TestRule) it.next() ).getIdentifier() );
176         assertEquals( "Testing universal wildcard mismatch (I)", "non_wild_child",
177                       ( (TestRule) it.next() ).getIdentifier() );
178 
179         // test wildcard universal parent
180         list = digester.getRules().match( null, "alpha/epsilon/beta/gamma/alpha", null, null );
181 
182         assertEquals( "Testing universal wildcard mismatch (J)", 2, list.size() );
183 
184         it = list.iterator();
185         assertEquals( "Testing universal wildcard mismatch (K)", "non_wildhead_child",
186                       ( (TestRule) it.next() ).getIdentifier() );
187         assertEquals( "Testing universal wildcard mismatch (L)", "universal_wildhead_child",
188                       ( (TestRule) it.next() ).getIdentifier() );
189 
190         // test wildcard universal parent
191         list = digester.getRules().match( null, "alpha/epsilon/beta/gamma/zeta", null, null );
192 
193         assertEquals( "Testing universal wildcard mismatch (M)", 2, list.size() );
194 
195         it = list.iterator();
196         assertEquals( "Testing universal wildcard mismatch (M)", "exact3", ( (TestRule) it.next() ).getIdentifier() );
197         assertEquals( "Testing universal wildcard mismatch (O)", "universal_wildhead_child",
198                       ( (TestRule) it.next() ).getIdentifier() );
199 
200         // clean up
201         digester.getRules().clear();
202 
203     }
204 
205     /**
206      * Basic test of wild matches. A universal will match matches anything! A non-universal will match matches anything
207      * not matched by something else. This method tests non-universal and universal wild matches.
208      */
209     @Test
210     public void testWildMatch()
211     {
212 
213         // clear any existing rules
214         digester.getRules().clear();
215 
216         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
217 
218         // Set up rules
219         // The combinations a little large to test everything but we'll pick a couple and try them.
220         digester.addRule( "*", new TestRule( "basic_wild" ) );
221         digester.addRule( "!*", new TestRule( "universal_wild" ) );
222         digester.addRule( "alpha/beta/gamma/delta", new TestRule( "exact" ) );
223         digester.addRule( "*/beta/gamma/?", new TestRule( "wild_parent" ) );
224 
225         List<Rule> list = null;
226         Iterator<Rule> it = null;
227 
228         // The universal wild will always match whatever else does
229         list = digester.getRules().match( null, "alpha/beta/gamma/delta", null, null );
230 
231         // all three rules should match
232         assertEquals( "Testing wild mismatch (A)", 2, list.size() );
233 
234         it = list.iterator();
235         assertEquals( "Testing wild mismatch (B)", "universal_wild", ( (TestRule) it.next() ).getIdentifier() );
236         assertEquals( "Testing wild mismatch (C)", "exact", ( (TestRule) it.next() ).getIdentifier() );
237 
238         // The universal wild will always match whatever else does
239         list = digester.getRules().match( null, "alpha/beta/gamma/epsilon", null, null );
240 
241         assertEquals( "Testing wild mismatch (D)", 2, list.size() );
242 
243         it = list.iterator();
244         assertEquals( "Testing wild mismatch (E)", "universal_wild", ( (TestRule) it.next() ).getIdentifier() );
245         assertEquals( "Testing wild mismatch (F)", "wild_parent", ( (TestRule) it.next() ).getIdentifier() );
246 
247         // The universal wild will always match whatever else does
248         // we have no other non-universal matching so this will match the non-universal wild as well
249         list = digester.getRules().match( null, "alpha/gamma", null, null );
250 
251         assertEquals( "Testing wild mismatch (G)", 2, list.size() );
252 
253         it = list.iterator();
254         assertEquals( "Testing wild mismatch (H)", "basic_wild", ( (TestRule) it.next() ).getIdentifier() );
255         assertEquals( "Testing wild mismatch (I)", "universal_wild", ( (TestRule) it.next() ).getIdentifier() );
256 
257         // clean up
258         digester.getRules().clear();
259 
260     }
261 
262     /**
263      * Basic test of wild matches. A universal will match matches anything! A non-universal will match matches anything
264      * not matched by something else. This method tests non-universal and universal wild matches.
265      */
266     @Test
267     public void testRootTailMatch()
268     {
269 
270         // clear any existing rules
271         digester.getRules().clear();
272 
273         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
274 
275         // Set up rules
276         // The combinations a little large to test everything but we'll pick a couple and try them.
277         digester.addRule( "*/a", new TestRule( "a_tail" ) );
278 
279         List<Rule> list = null;
280 
281         list = digester.getRules().match( null, "a", null, null );
282 
283         assertEquals( "Testing tail wrong size (A)", 1, list.size() );
284         assertEquals( "Testing tail mismatch (B)", "a_tail", ( (TestRule) list.get( 0 ) ).getIdentifier() );
285 
286         list = digester.getRules().match( null, "beta/a", null, null );
287 
288         assertEquals( "Testing tail wrong size (C)", 1, list.size() );
289         assertEquals( "Testing tail mismatch (D)", "a_tail", ( (TestRule) list.get( 0 ) ).getIdentifier() );
290 
291         list = digester.getRules().match( null, "be/aaa", null, null );
292 
293         assertEquals( "Testing tail no matches (E)", 0, list.size() );
294 
295         list = digester.getRules().match( null, "aaa", null, null );
296 
297         assertEquals( "Testing tail no matches (F)", 0, list.size() );
298 
299         list = digester.getRules().match( null, "a/beta", null, null );
300 
301         assertEquals( "Testing tail no matches (G)", 0, list.size() );
302 
303         // clean up
304         digester.getRules().clear();
305 
306     }
307 
308     @Test
309     public void testAncesterMatch()
310         throws Exception
311     {
312         // test fixed root ancester
313         digester.getRules().clear();
314 
315         digester.addRule( "!a/b/*", new TestRule( "uni-a-b-star" ) );
316         digester.addRule( "a/b/*", new TestRule( "a-b-star" ) );
317         digester.addRule( "a/b/c", new TestRule( "a-b-c" ) );
318         digester.addRule( "a/b/?", new TestRule( "a-b-child" ) );
319 
320         List<Rule> list = digester.getRules().match( null, "a/b/c", null, null );
321 
322         assertEquals( "Simple ancester matches (1)", 2, list.size() );
323         assertEquals( "Univeral ancester mismatch (1)", "uni-a-b-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
324         assertEquals( "Parent precedence failure", "a-b-c", ( (TestRule) list.get( 1 ) ).getIdentifier() );
325 
326         list = digester.getRules().match( null, "a/b/b", null, null );
327         assertEquals( "Simple ancester matches (2)", 2, list.size() );
328         assertEquals( "Univeral ancester mismatch (2)", "uni-a-b-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
329         assertEquals( "Child precedence failure", "a-b-child", ( (TestRule) list.get( 1 ) ).getIdentifier() );
330 
331         list = digester.getRules().match( null, "a/b/d", null, null );
332         assertEquals( "Simple ancester matches (3)", 2, list.size() );
333         assertEquals( "Univeral ancester mismatch (3)", "uni-a-b-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
334         assertEquals( "Ancester mismatch (1)", "a-b-child", ( (TestRule) list.get( 1 ) ).getIdentifier() );
335 
336         list = digester.getRules().match( null, "a/b/d/e/f", null, null );
337         assertEquals( "Simple ancester matches (4)", 2, list.size() );
338         assertEquals( "Univeral ancester mismatch (4)", "uni-a-b-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
339         assertEquals( "Ancester mismatch (2)", "a-b-star", ( (TestRule) list.get( 1 ) ).getIdentifier() );
340 
341         // test wild root ancester
342         digester.getRules().clear();
343 
344         digester.addRule( "!*/a/b/*", new TestRule( "uni-star-a-b-star" ) );
345         digester.addRule( "*/b/c/*", new TestRule( "star-b-c-star" ) );
346         digester.addRule( "*/b/c/d", new TestRule( "star-b-c-d" ) );
347         digester.addRule( "a/b/c", new TestRule( "a-b-c" ) );
348 
349         list = digester.getRules().match( null, "a/b/c", null, null );
350         assertEquals( "Wild ancester match (1)", 2, list.size() );
351         assertEquals( "Univeral ancester mismatch (5)", "uni-star-a-b-star",
352                       ( (TestRule) list.get( 0 ) ).getIdentifier() );
353         assertEquals( "Match missed (1)", "a-b-c", ( (TestRule) list.get( 1 ) ).getIdentifier() );
354 
355         list = digester.getRules().match( null, "b/c", null, null );
356         assertEquals( "Wild ancester match (2)", 1, list.size() );
357         assertEquals( "Match missed (2)", "star-b-c-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
358 
359         list = digester.getRules().match( null, "a/b/c/d", null, null );
360         assertEquals( "Wild ancester match (3)", 2, list.size() );
361         assertEquals( "Match missed (3)", "uni-star-a-b-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
362         assertEquals( "Match missed (4)", "star-b-c-d", ( (TestRule) list.get( 1 ) ).getIdentifier() );
363 
364         list = digester.getRules().match( null, "b/b/c/e/d", null, null );
365         assertEquals( "Wild ancester match (2)", 1, list.size() );
366         assertEquals( "Match missed (5)", "star-b-c-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
367     }
368 
369     @Test
370     public void testLongMatch()
371     {
372 
373         digester.getRules().clear();
374 
375         digester.addRule( "a/b/c/d/*", new TestRule( "a-b-c-d-star" ) );
376 
377         List<Rule> list = digester.getRules().match( null, "a/b/c/d/e", null, null );
378         assertEquals( "Long match (1)", 1, list.size() );
379         assertEquals( "Match missed (1)", "a-b-c-d-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
380 
381         list = digester.getRules().match( null, "a/b/c/d/e/f", null, null );
382         assertEquals( "Long match (2)", 1, list.size() );
383         assertEquals( "Match missed (2)", "a-b-c-d-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
384 
385         list = digester.getRules().match( null, "a/b/c/d/e/f/g", null, null );
386         assertEquals( "Long match (3)", 1, list.size() );
387         assertEquals( "Match missed (3)", "a-b-c-d-star", ( (TestRule) list.get( 0 ) ).getIdentifier() );
388 
389         list = digester.getRules().match( null, "a/b/c/d", null, null );
390         assertEquals( "Long match (4)", 0, list.size() );
391     }
392 
393     @Test
394     public void testInstructors()
395     {
396         digester.getRules().clear();
397 
398         digester.addRule( "!instructors/*", new TestRule( "instructors" ) );
399         digester.addRule( "!instructor/*", new TestRule( "instructor" ) );
400 
401         List<Rule> list = digester.getRules().match( null, "instructors", null, null );
402         assertEquals( "Only expect to match instructors", 1, list.size() );
403         assertEquals( "Instructors expected", "instructors", ( (TestRule) list.get( 0 ) ).getIdentifier() );
404 
405     }
406 
407     @Test
408     public void testMiddleInstructors()
409     {
410         digester.getRules().clear();
411 
412         digester.addRule( "!instructors/*", new TestRule( "instructors" ) );
413 
414         List<Rule> list = digester.getRules().match( null, "/tosh/instructors/fiddlesticks", null, null );
415         assertEquals( "No matches expected", 0, list.size() );
416 
417     }
418 }