View Javadoc

1   /* $Id: RulesBaseTestCase.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.Digester;
27  import org.apache.commons.digester3.Rule;
28  import org.apache.commons.digester3.Rules;
29  import org.apache.commons.digester3.RulesBase;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  /**
35   * <p>
36   * Test Case for the RulesBase matching rules. Most of this material was original contained in the digester test case
37   * but was moved into this class so that extensions of the basic matching rules behaviour can extend this test case.
38   * </p>
39   *
40   * @author Craig R. McClanahan
41   */
42  public class RulesBaseTestCase
43  {
44  
45      // ----------------------------------------------------- Instance Variables
46  
47      /**
48       * The digester instance we will be processing.
49       */
50      protected Digester digester = null;
51  
52      // -------------------------------------------------- Overall Test Methods
53  
54      /**
55       * Set up instance variables required by this test case.
56       */
57      @Before
58      public void setUp()
59      {
60  
61          digester = new Digester();
62          digester.setRules( createMatchingRulesForTest() );
63  
64      }
65  
66      /**
67       * <p>
68       * This should be overriden by subclasses.
69       *
70       * @return the matching rules to be tested.
71       */
72      protected Rules createMatchingRulesForTest()
73      {
74          return new RulesBase();
75      }
76  
77      /**
78       * Tear down instance variables required by this test case.
79       */
80      @After
81      public void tearDown()
82      {
83  
84          digester = null;
85  
86      }
87  
88      // ------------------------------------------------ Individual Test Methods
89  
90      /**
91       * Basic test for rule creation and matching.
92       */
93      @Test
94      public void testRules()
95      {
96  
97          // clear any existing rules
98          digester.getRules().clear();
99  
100         // perform tests
101 
102         assertEquals( "Initial rules list is empty", 0, digester.getRules().match( null, "a", null, null ).size() );
103         digester.addSetProperties( "a" );
104         assertEquals( "Add a matching rule", 1, digester.getRules().match( null, "a", null, null ).size() );
105         digester.addSetProperties( "b" );
106         assertEquals( "Add a non-matching rule", 1, digester.getRules().match( null, "a", null, null ).size() );
107         digester.addSetProperties( "a/b" );
108         assertEquals( "Add a non-matching nested rule", 1, digester.getRules().match( null, "a", null, null ).size() );
109         digester.addSetProperties( "a/b" );
110         assertEquals( "Add a second matching rule", 2, digester.getRules().match( null, "a/b", null, null ).size() );
111 
112         // clean up
113         digester.getRules().clear();
114 
115     }
116 
117     /**
118      * <p>
119      * Test matching rules in {@link RulesBase}.
120      * </p>
121      * <p>
122      * Tests:
123      * </p>
124      * <ul>
125      * <li>exact match</li>
126      * <li>tail match</li>
127      * <li>longest pattern rule</li>
128      * </ul>
129      */
130     @Test
131     public void testRulesBase()
132     {
133 
134         // clear any existing rules
135         digester.getRules().clear();
136 
137         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
138 
139         // We're going to set up
140         digester.addRule( "a/b/c/d", new TestRule( "a/b/c/d" ) );
141         digester.addRule( "*/d", new TestRule( "*/d" ) );
142         digester.addRule( "*/c/d", new TestRule( "*/c/d" ) );
143 
144         // Test exact match
145         assertEquals( "Exact match takes precedence 1", 1, digester.getRules().match( null, "a/b/c/d", null, null ).size() );
146         assertEquals( "Exact match takes precedence 2", "a/b/c/d",
147                       ( (TestRule) digester.getRules().match( null, "a/b/c/d", null, null ).iterator().next() ).getIdentifier() );
148 
149         // Test wildcard tail matching
150         assertEquals( "Wildcard tail matching rule 1", 1, digester.getRules().match( null, "a/b/d", null, null ).size() );
151         assertEquals( "Wildcard tail matching rule 2", "*/d",
152                       ( (TestRule) digester.getRules().match( null, "a/b/d", null, null ).iterator().next() ).getIdentifier() );
153 
154         // Test the longest matching pattern rule
155         assertEquals( "Longest tail rule 1", 1, digester.getRules().match( null, "x/c/d", null, null ).size() );
156         assertEquals( "Longest tail rule 2", "*/c/d",
157                       ( (TestRule) digester.getRules().match( null, "x/c/d", null, null ).iterator().next() ).getIdentifier() );
158 
159         // Test wildcard tail matching at the top level,
160         // i.e. the wildcard is nothing
161         digester.addRule( "*/a", new TestRule( "*/a" ) );
162         assertEquals( "Wildcard tail matching rule 3", 1, digester.getRules().match( null, "a", null, null ).size() );
163 
164         assertEquals( "Wildcard tail matching rule 3 (match too much)", 0,
165                       digester.getRules().match( null, "aa", null, null ).size() );
166         // clean up
167         digester.getRules().clear();
168 
169     }
170 
171     /**
172      * Test basic matchings involving namespaces.
173      */
174     @Test
175     public void testBasicNamespaceMatching()
176     {
177 
178         List<Rule> list = null;
179         Iterator<Rule> it = null;
180 
181         // clear any existing rules
182         digester.getRules().clear();
183 
184         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
185 
186         // Set up rules
187         digester.addRule( "alpha/beta/gamma", new TestRule( "No-Namespace" ) );
188         digester.addRule( "alpha/beta/gamma", new TestRule( "Euclidean-Namespace", "euclidean" ) );
189 
190         list = digester.getRules().rules();
191 
192         // test that matching null namespace brings back namespace and non-namespace rules
193         list = digester.getRules().match( null, "alpha/beta/gamma", null, null );
194 
195         assertEquals( "Null namespace match (A)", 2, list.size() );
196 
197         it = list.iterator();
198         assertEquals( "Null namespace match (B)", "No-Namespace", ( (TestRule) it.next() ).getIdentifier() );
199         assertEquals( "Null namespace match (C)", "Euclidean-Namespace", ( (TestRule) it.next() ).getIdentifier() );
200 
201         // test that matching euclid namespace brings back namespace and non-namespace rules
202         list = digester.getRules().match( "euclidean", "alpha/beta/gamma", null, null );
203 
204         assertEquals( "Matching namespace match (A)", 2, list.size() );
205 
206         it = list.iterator();
207         assertEquals( "Matching namespace match (B)", "No-Namespace", ( (TestRule) it.next() ).getIdentifier() );
208         assertEquals( "Matching namespace match (C)", "Euclidean-Namespace", ( (TestRule) it.next() ).getIdentifier() );
209 
210         // test that matching another namespace brings back only non-namespace rule
211         list = digester.getRules().match( "hyperbolic", "alpha/beta/gamma", null, null );
212 
213         assertEquals( "Non matching namespace match (A)", 1, list.size() );
214 
215         it = list.iterator();
216         assertEquals( "Non matching namespace match (B)", "No-Namespace", ( (TestRule) it.next() ).getIdentifier() );
217 
218         // clean up
219         digester.getRules().clear();
220 
221     }
222 
223     /**
224      * Rules must always be returned in the correct order.
225      */
226     @Test
227     public void testOrdering()
228     {
229 
230         // clear any existing rules
231         digester.getRules().clear();
232 
233         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
234 
235         // Set up rules
236         digester.addRule( "alpha/beta/gamma", new TestRule( "one" ) );
237         digester.addRule( "alpha/beta/gamma", new TestRule( "two" ) );
238         digester.addRule( "alpha/beta/gamma", new TestRule( "three" ) );
239 
240         // test that rules are returned in set order
241         List<Rule> list = digester.getRules().match( null, "alpha/beta/gamma", null, null );
242 
243         assertEquals( "Testing ordering mismatch (A)", 3, list.size() );
244 
245         Iterator<Rule> it = list.iterator();
246         assertEquals( "Testing ordering mismatch (B)", "one", ( (TestRule) it.next() ).getIdentifier() );
247         assertEquals( "Testing ordering mismatch (C)", "two", ( (TestRule) it.next() ).getIdentifier() );
248         assertEquals( "Testing ordering mismatch (D)", "three", ( (TestRule) it.next() ).getIdentifier() );
249 
250         // clean up
251         digester.getRules().clear();
252 
253     }
254 
255     /** Tests the behaviour when a rule is added with a trailing slash */
256     @Test
257     public void testTrailingSlash()
258     {
259         // clear any existing rules
260         digester.getRules().clear();
261 
262         assertEquals( "Initial rules list is empty", 0, digester.getRules().rules().size() );
263 
264         // Set up rules
265         digester.addRule( "alpha/beta/gamma/", new TestRule( "one" ) );
266         digester.addRule( "alpha/beta/", new TestRule( "two" ) );
267         digester.addRule( "beta/gamma/alpha", new TestRule( "three" ) );
268 
269         // test that rules are returned in set order
270         List<Rule> list = digester.getRules().match( null, "alpha/beta/gamma", null, null );
271 
272         assertEquals( "Testing number of matches", 1, list.size() );
273 
274         Iterator<Rule> it = list.iterator();
275         assertEquals( "Testing ordering (A)", "one", ( (TestRule) it.next() ).getIdentifier() );
276 
277         // clean up
278         digester.getRules().clear();
279     }
280 }