KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: RegexRulesTestCase.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 RegexRules
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 RegexRulesTestCase extends TestCase {
32     
33     /** Base constructor */
34     public RegexRulesTestCase(String JavaDoc name) {
35         super(name);
36     }
37     
38     /** Test regex that matches everything */
39     public void testMatchAll() {
40         // set up which should match every rule
41
RegexRules rules = new RegexRules(
42             new RegexMatcher() {
43                 public boolean match(String JavaDoc pathPattern, String JavaDoc rulePattern) {
44                     return true;
45                 }
46             });
47         
48         rules.add("/a/b/b", new TestRule("alpha"));
49         rules.add("/a/d", new TestRule("beta"));
50         rules.add("/b", new TestRule("gamma"));
51         
52         // now test a few patterns
53
// check that all are return in the order which they were added
54
List JavaDoc matches = rules.match("", "x/g/e");
55         assertEquals("Wrong number of rules returned (1)", 3, matches.size());
56         assertEquals("Rule Out Of Order (1)", "alpha", ((TestRule) matches.get(0)).getIdentifier());
57         assertEquals("Rule Out Of Order (2)", "beta", ((TestRule) matches.get(1)).getIdentifier());
58         assertEquals("Rule Out Of Order (3)", "gamma", ((TestRule) matches.get(2)).getIdentifier());
59         
60         matches = rules.match("", "/a");
61         assertEquals("Wrong number of rules returned (2)", 3, matches.size());
62         assertEquals("Rule Out Of Order (4)", "alpha", ((TestRule) matches.get(0)).getIdentifier());
63         assertEquals("Rule Out Of Order (5)", "beta", ((TestRule) matches.get(1)).getIdentifier());
64         assertEquals("Rule Out Of Order (6)", "gamma", ((TestRule) matches.get(2)).getIdentifier());
65     }
66     
67     /** Test regex matcher that matches nothing */
68     public void testMatchNothing() {
69         // set up which should match every rule
70
RegexRules rules = new RegexRules(
71             new RegexMatcher() {
72                 public boolean match(String JavaDoc pathPattern, String JavaDoc rulePattern) {
73                     return false;
74                 }
75             });
76         
77         rules.add("/b/c/f", new TestRule("alpha"));
78         rules.add("/c/f", new TestRule("beta"));
79         rules.add("/b", new TestRule("gamma"));
80         
81         // now test a few patterns
82
// check that all are return in the order which they were added
83
List JavaDoc matches = rules.match("", "/b/c");
84         assertEquals("Wrong number of rules returned (1)", 0, matches.size());
85         
86         matches = rules.match("", "/b/c/f");
87         assertEquals("Wrong number of rules returned (2)", 0, matches.size());
88     }
89
90     /** Test a mixed regex - in other words, one that sometimes returns true and sometimes false */
91     public void testMatchMixed() {
92         // set up which should match every rule
93
RegexRules rules = new RegexRules(
94             new RegexMatcher() {
95                 public boolean match(String JavaDoc pathPattern, String JavaDoc rulePattern) {
96                     return (rulePattern.equals("/match/me"));
97                 }
98             });
99         
100         rules.add("/match", new TestRule("alpha"));
101         rules.add("/match/me", new TestRule("beta"));
102         rules.add("/match", new TestRule("gamma"));
103         
104         // now test a few patterns
105
// check that all are return in the order which they were added
106
List JavaDoc matches = rules.match("", "/match");
107         assertEquals("Wrong number of rules returned (1)", 1, matches.size());
108         assertEquals("Wrong Rule (1)", "beta", ((TestRule) matches.get(0)).getIdentifier());
109         
110         matches = rules.match("", "/a/match");
111         assertEquals("Wrong Rule (2)", "beta", ((TestRule) matches.get(0)).getIdentifier());
112     }
113         
114     /** Test rules and clear methods */
115     public void testClear() {
116         // set up which should match every rule
117
RegexRules rules = new RegexRules(
118             new RegexMatcher() {
119                 public boolean match(String JavaDoc pathPattern, String JavaDoc rulePattern) {
120                     return true;
121                 }
122             });
123         
124         rules.add("/abba", new TestRule("alpha"));
125         rules.add("/ad/ma", new TestRule("beta"));
126         rules.add("/gamma", new TestRule("gamma"));
127         
128         // check that rules returns all rules in the order which they were added
129
List JavaDoc matches = rules.rules();
130         assertEquals("Wrong number of rules returned (1)", 3, matches.size());
131         assertEquals("Rule Out Of Order (1)", "alpha", ((TestRule) matches.get(0)).getIdentifier());
132         assertEquals("Rule Out Of Order (2)", "beta", ((TestRule) matches.get(1)).getIdentifier());
133         assertEquals("Rule Out Of Order (3)", "gamma", ((TestRule) matches.get(2)).getIdentifier());
134         
135         matches = rules.match("", "/eggs");
136         assertEquals("Wrong number of rules returned (2)", 3, matches.size());
137         assertEquals("Rule Out Of Order (4)", "alpha", ((TestRule) matches.get(0)).getIdentifier());
138         assertEquals("Rule Out Of Order (5)", "beta", ((TestRule) matches.get(1)).getIdentifier());
139         assertEquals("Rule Out Of Order (6)", "gamma", ((TestRule) matches.get(2)).getIdentifier());
140         
141         rules.clear();
142         matches = rules.rules();
143         assertEquals("Wrong number of rules returned (3)", 0, matches.size());
144         
145         matches = rules.match("", "/eggs");
146         assertEquals("Wrong number of rules returned (4)", 0, matches.size());
147     }
148     
149     public void testSimpleRegexMatch() {
150         
151         SimpleRegexMatcher matcher = new SimpleRegexMatcher();
152         
153 // SimpleLog log = new SimpleLog("{testSimpleRegexMatch:SimpleRegexMatcher]");
154
// log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
155

156         
157         assertEquals(
158                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma' ",
159                 true,
160                 matcher.match("/alpha/beta/gamma", "/alpha/beta/gamma"));
161         assertEquals(
162                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma/epsilon' ",
163                 false,
164                 matcher.match("/alpha/beta/gamma", "/alpha/beta/gamma/epsilon"));
165         assertEquals(
166                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*' ",
167                 true,
168                 matcher.match("/alpha/beta/gamma", "/alpha/*"));
169         assertEquals(
170                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*/gamma' ",
171                 true,
172                 matcher.match("/alpha/beta/gamma", "/alpha/*/gamma"));
173         assertEquals(
174                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*me' ",
175                 false,
176                 matcher.match("/alpha/beta/gamma", "/alpha/*me"));
177         assertEquals(
178                 "Simple Regex Match '/alpha/beta/gamma' to '*/beta/gamma' ",
179                 true,
180                 matcher.match("/alpha/beta/gamma", "*/beta/gamma"));
181         assertEquals(
182                 "Simple Regex Match '/alpha/beta/gamma' to '*/alpha/beta/gamma' ",
183                 true,
184                 matcher.match("/alpha/beta/gamma", "*/alpha/beta/gamma"));
185         assertEquals(
186                 "Simple Regex Match '/alpha/beta/gamma' to '*/bet/gamma' ",
187                 false,
188                 matcher.match("/alpha/beta/gamma", "*/bet/gamma"));
189         assertEquals(
190                 "Simple Regex Match '/alpha/beta/gamma' to 'alph?/beta/gamma' ",
191                 true,
192                 matcher.match("/alpha/beta/gamma", "/alph?/beta/gamma"));
193         assertEquals(
194                 "Simple Regex Match '/alpha/beta/gamma' to '/?lpha/beta/gamma' ",
195                 true,
196                 matcher.match("/alpha/beta/gamma", "/?lpha/beta/gamma"));
197         assertEquals(
198                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?beta/gamma' ",
199                 false,
200                 matcher.match("/alpha/beta/gamma", "/alpha/?beta/gamma"));
201         assertEquals(
202                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*' ",
203                 true,
204                 matcher.match("/alpha/beta/gamma", "/alpha/?eta/*"));
205         assertEquals(
206                 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*e' ",
207                 false,
208                 matcher.match("/alpha/beta/gamma", "/alpha/?eta/*e"));
209         assertEquals(
210                 "Simple Regex Match '/alpha/beta/gamma' to '*/?et?/?amma' ",
211                 true,
212                 matcher.match("/alpha/beta/gamma", "*/?et?/?amma"));
213         assertEquals(
214                 "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
215                 + " '*/beta/gamma/?p*n' ",
216                 true,
217                 matcher.match("/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon", "*/beta/gamma/?p*n"));
218         assertEquals(
219                 "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to "
220                 + " '*/beta/gamma/?p*no' ",
221                 false,
222                 matcher.match("/alpha/beta/gamma", "*/beta/gamma/?p*no"));
223     }
224 }
225
Popular Tags