View Javadoc

1   /* $Id: RegexRulesTestCase.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.List;
24  
25  import org.apache.commons.digester3.RegexMatcher;
26  import org.apache.commons.digester3.RegexRules;
27  import org.apache.commons.digester3.Rule;
28  import org.apache.commons.digester3.SimpleRegexMatcher;
29  import org.junit.Test;
30  
31  /**
32   * Test case for RegexRules
33   *
34   * @author Robert Burrell Donkin
35   */
36  public class RegexRulesTestCase
37  {
38  
39      /** Test regex that matches everything */
40      @Test
41      public void testMatchAll()
42      {
43          // set up which should match every rule
44          RegexRules rules = new RegexRules( new RegexMatcher()
45          {
46              @Override
47              public boolean match( String pathPattern, String rulePattern )
48              {
49                  return true;
50              }
51          } );
52  
53          rules.add( "/a/b/b", new TestRule( "alpha" ) );
54          rules.add( "/a/d", new TestRule( "beta" ) );
55          rules.add( "/b", new TestRule( "gamma" ) );
56  
57          // now test a few patterns
58          // check that all are return in the order which they were added
59          List<Rule> matches = rules.match( "", "x/g/e", null, null );
60          assertEquals( "Wrong number of rules returned (1)", 3, matches.size() );
61          assertEquals( "Rule Out Of Order (1)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
62          assertEquals( "Rule Out Of Order (2)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
63          assertEquals( "Rule Out Of Order (3)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
64  
65          matches = rules.match( "", "/a", null, null );
66          assertEquals( "Wrong number of rules returned (2)", 3, matches.size() );
67          assertEquals( "Rule Out Of Order (4)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
68          assertEquals( "Rule Out Of Order (5)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
69          assertEquals( "Rule Out Of Order (6)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
70      }
71  
72      /** Test regex matcher that matches nothing */
73      @Test
74      public void testMatchNothing()
75      {
76          // set up which should match every rule
77          RegexRules rules = new RegexRules( new RegexMatcher()
78          {
79              @Override
80              public boolean match( String pathPattern, String rulePattern )
81              {
82                  return false;
83              }
84          } );
85  
86          rules.add( "/b/c/f", new TestRule( "alpha" ) );
87          rules.add( "/c/f", new TestRule( "beta" ) );
88          rules.add( "/b", new TestRule( "gamma" ) );
89  
90          // now test a few patterns
91          // check that all are return in the order which they were added
92          List<Rule> matches = rules.match( "", "/b/c", null, null );
93          assertEquals( "Wrong number of rules returned (1)", 0, matches.size() );
94  
95          matches = rules.match( "", "/b/c/f", null, null );
96          assertEquals( "Wrong number of rules returned (2)", 0, matches.size() );
97      }
98  
99      /** Test a mixed regex - in other words, one that sometimes returns true and sometimes false */
100     @Test
101     public void testMatchMixed()
102     {
103         // set up which should match every rule
104         RegexRules rules = new RegexRules( new RegexMatcher()
105         {
106             @Override
107             public boolean match( String pathPattern, String rulePattern )
108             {
109                 return ( rulePattern.equals( "/match/me" ) );
110             }
111         } );
112 
113         rules.add( "/match", new TestRule( "alpha" ) );
114         rules.add( "/match/me", new TestRule( "beta" ) );
115         rules.add( "/match", new TestRule( "gamma" ) );
116 
117         // now test a few patterns
118         // check that all are return in the order which they were added
119         List<Rule> matches = rules.match( "", "/match", null, null );
120         assertEquals( "Wrong number of rules returned (1)", 1, matches.size() );
121         assertEquals( "Wrong Rule (1)", "beta", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
122 
123         matches = rules.match( "", "/a/match", null, null );
124         assertEquals( "Wrong Rule (2)", "beta", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
125     }
126 
127     /** Test rules and clear methods */
128     @Test
129     public void testClear()
130     {
131         // set up which should match every rule
132         RegexRules rules = new RegexRules( new RegexMatcher()
133         {
134             @Override
135             public boolean match( String pathPattern, String rulePattern )
136             {
137                 return true;
138             }
139         } );
140 
141         rules.add( "/abba", new TestRule( "alpha" ) );
142         rules.add( "/ad/ma", new TestRule( "beta" ) );
143         rules.add( "/gamma", new TestRule( "gamma" ) );
144 
145         // check that rules returns all rules in the order which they were added
146         List<Rule> matches = rules.rules();
147         assertEquals( "Wrong number of rules returned (1)", 3, matches.size() );
148         assertEquals( "Rule Out Of Order (1)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
149         assertEquals( "Rule Out Of Order (2)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
150         assertEquals( "Rule Out Of Order (3)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
151 
152         matches = rules.match( "", "/eggs", null, null );
153         assertEquals( "Wrong number of rules returned (2)", 3, matches.size() );
154         assertEquals( "Rule Out Of Order (4)", "alpha", ( (TestRule) matches.get( 0 ) ).getIdentifier() );
155         assertEquals( "Rule Out Of Order (5)", "beta", ( (TestRule) matches.get( 1 ) ).getIdentifier() );
156         assertEquals( "Rule Out Of Order (6)", "gamma", ( (TestRule) matches.get( 2 ) ).getIdentifier() );
157 
158         rules.clear();
159         matches = rules.rules();
160         assertEquals( "Wrong number of rules returned (3)", 0, matches.size() );
161 
162         matches = rules.match( "", "/eggs", null, null );
163         assertEquals( "Wrong number of rules returned (4)", 0, matches.size() );
164     }
165 
166     @Test
167     public void testSimpleRegexMatch()
168     {
169 
170         SimpleRegexMatcher matcher = new SimpleRegexMatcher();
171 
172         // SimpleLog log = new SimpleLog("{testSimpleRegexMatch:SimpleRegexMatcher]");
173         // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
174 
175         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma' ", true,
176                       matcher.match( "/alpha/beta/gamma", "/alpha/beta/gamma" ) );
177         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma/epsilon' ", false,
178                       matcher.match( "/alpha/beta/gamma", "/alpha/beta/gamma/epsilon" ) );
179         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*' ", true,
180                       matcher.match( "/alpha/beta/gamma", "/alpha/*" ) );
181         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*/gamma' ", true,
182                       matcher.match( "/alpha/beta/gamma", "/alpha/*/gamma" ) );
183         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*me' ", false,
184                       matcher.match( "/alpha/beta/gamma", "/alpha/*me" ) );
185         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/beta/gamma' ", true,
186                       matcher.match( "/alpha/beta/gamma", "*/beta/gamma" ) );
187         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/alpha/beta/gamma' ", true,
188                       matcher.match( "/alpha/beta/gamma", "*/alpha/beta/gamma" ) );
189         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/bet/gamma' ", false,
190                       matcher.match( "/alpha/beta/gamma", "*/bet/gamma" ) );
191         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to 'alph?/beta/gamma' ", true,
192                       matcher.match( "/alpha/beta/gamma", "/alph?/beta/gamma" ) );
193         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/?lpha/beta/gamma' ", true,
194                       matcher.match( "/alpha/beta/gamma", "/?lpha/beta/gamma" ) );
195         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?beta/gamma' ", false,
196                       matcher.match( "/alpha/beta/gamma", "/alpha/?beta/gamma" ) );
197         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*' ", true,
198                       matcher.match( "/alpha/beta/gamma", "/alpha/?eta/*" ) );
199         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*e' ", false,
200                       matcher.match( "/alpha/beta/gamma", "/alpha/?eta/*e" ) );
201         assertEquals( "Simple Regex Match '/alpha/beta/gamma' to '*/?et?/?amma' ", true,
202                       matcher.match( "/alpha/beta/gamma", "*/?et?/?amma" ) );
203         assertEquals( "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
204                           + " '*/beta/gamma/?p*n' ", true,
205                       matcher.match( "/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon", "*/beta/gamma/?p*n" ) );
206         assertEquals( "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
207             + " '*/beta/gamma/?p*no' ", false, matcher.match( "/alpha/beta/gamma", "*/beta/gamma/?p*no" ) );
208     }
209 }