KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > MackerRegexTest


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.rule;
22
23 import net.innig.macker.structure.*;
24
25 import junit.framework.TestCase;
26
27 public final class MackerRegexTest
28     extends TestCase
29     {
30     public MackerRegexTest(String JavaDoc name)
31         { super(name); }
32     
33     protected void setUp()
34         throws Exception JavaDoc
35         {
36         context = new EvaluationContext(new ClassManager(), new RuleSet(RuleSet.getMackerDefaults()));
37         }
38
39     protected void tearDown()
40         throws Exception JavaDoc
41         {
42         }
43     
44     private EvaluationContext context;
45     
46     private void testMatches(MackerRegex re, String JavaDoc[] positive, String JavaDoc[] negative)
47         throws Exception JavaDoc
48         {
49         for(int n = 0; n < positive.length; n++)
50             assertTrue(
51                 "\"" + re + "\" should match \"" + positive[n] + "\"",
52                 re.matches(context, positive[n]));
53         
54         for(int n = 0; n < negative.length; n++)
55             assertTrue(
56                 "\"" + re + "\" shouldn't match \"" + negative[n] + "\"",
57                 !re.matches(context, negative[n]));
58         }
59     
60     public void test_basic()
61         throws Exception JavaDoc
62         {
63         testMatches(
64             new MackerRegex("java.lang.*"),
65             new String JavaDoc[] { "java.lang.String" },
66             new String JavaDoc[] { "java.lang.reflect.Method",
67                            "java.language.MadeUpClass" });
68
69         testMatches(
70             new MackerRegex("java.lang.**"),
71             new String JavaDoc[] { "java.lang.String",
72                            "java.lang.reflect.Method" },
73             new String JavaDoc[] { "java.language.MadeUpClass" });
74
75         testMatches(
76             new MackerRegex("java.lang**"),
77             new String JavaDoc[] { "java.lang.String",
78                            "java.lang.reflect.Method",
79                            "java.language.MadeUpClass" },
80             new String JavaDoc[0]);
81
82         testMatches(
83             new MackerRegex("String"),
84             new String JavaDoc[] { "String" },
85             new String JavaDoc[] { "java.lang.String" });
86
87         testMatches(
88             new MackerRegex("java.lang.String"),
89             new String JavaDoc[] { "java.lang.String" },
90             new String JavaDoc[] { "String" });
91
92         testMatches(
93             new MackerRegex("String"),
94             new String JavaDoc[] { "String" },
95             new String JavaDoc[] { "java.lang.String",
96                            "java.lang.StringBuffer",
97                            "java.text.AttributedString" });
98         testMatches(
99             new MackerRegex("**String*"),
100             new String JavaDoc[] { "String",
101                            "java.lang.String",
102                            "java.lang.StringBuffer",
103                            "java.text.AttributedString" },
104             new String JavaDoc[0]);
105
106         testMatches(
107             new MackerRegex("**String"),
108             new String JavaDoc[] { "String",
109                            "java.lang.String",
110                            "java.text.AttributedString" },
111             new String JavaDoc[] { "java.lang.StringBuffer" });
112
113         testMatches(
114             new MackerRegex("**.String*"),
115             new String JavaDoc[] { "String",
116                            "java.lang.String",
117                            "java.lang.StringBuffer" },
118             new String JavaDoc[] { "java.text.AttributedString" });
119
120         testMatches(
121             new MackerRegex("a.b"),
122             new String JavaDoc[] { "a.b",
123                            "a$b" },
124             new String JavaDoc[] { "" });
125         }
126         
127     public void test_innerClass()
128         throws Exception JavaDoc
129         {
130         testMatches(
131             new MackerRegex("a.b"),
132             new String JavaDoc[] { "a$b", "a.b" },
133             new String JavaDoc[0]);
134
135         testMatches(
136             new MackerRegex("a$b"),
137             new String JavaDoc[] { "a$b" },
138             new String JavaDoc[] { "a.b" });
139
140         testMatches(
141             new MackerRegex("a/b"),
142             new String JavaDoc[] { "a.b" },
143             new String JavaDoc[] { "a$b" });
144
145         testMatches(
146             new MackerRegex("**a*b**"),
147             new String JavaDoc[] { "x.axb",
148                            "x$axb",
149                            "a$b" }, //! is this really right?
150
new String JavaDoc[] { "a.b" });
151         }
152     
153     public void test_variable()
154         throws Exception JavaDoc
155         {
156         MackerRegex re = new MackerRegex("x${var}y");
157         
158         try {
159             re.matches(context, "");
160             fail("expected exception");
161             }
162         catch(UndeclaredVariableException uve)
163             { /* correct */ }
164         
165         context.setVariableValue("var", "");
166         testMatches(
167             re,
168             new String JavaDoc[] { "xy" },
169             new String JavaDoc[] { "xay" });
170
171         context.setVariableValue("var", ".a");
172         testMatches(
173             re,
174             new String JavaDoc[] { "x.ay" },
175             new String JavaDoc[] { "xy", "x.y", "ax.y" });
176
177         context.setVariableValue("var", ".*");
178         testMatches(
179             re,
180             new String JavaDoc[] { "x.y", "x.ay" },
181             new String JavaDoc[] { "x.a.y" });
182
183         context.setVariableValue("rav", "**");
184         context.setVariableValue("var", ".${rav}");
185         testMatches(
186             re,
187             new String JavaDoc[] { "x.y", "x.ay", "x.a.y" },
188             new String JavaDoc[] { "xy" });
189
190         context.setVariableValue("rav", "*A*");
191         testMatches(
192             new MackerRegex("*${rav}*"),
193             new String JavaDoc[] { "Apple" },
194             new String JavaDoc[] { "granny.smith.Apple", "Apple.computer" });
195         }
196     
197     public void test_illegal()
198         throws Exception JavaDoc
199         {
200         String JavaDoc[] illegal = new String JavaDoc[]
201             {
202             "#", "@", "^", "|", "!",
203 //! "()()", "(())", "(a)(b)", "((a))"
204
"\n", "\r", "a\nb", "a\rb",
205             "a.", ".", "" // and ".a" ... except we need that to grab top-level parts, as in .(*).**
206
};
207         for(int n = 0; n < illegal.length; n++)
208             try {
209                 new MackerRegex(illegal[n]).matches(context, "");
210                 fail("expected exception: \"" + illegal[n] + "\" shouldn't be a legal regex");
211                 }
212             catch(MackerRegexSyntaxException mrse)
213                 { /* good */ }
214         }
215
216     public void test_noParts()
217         throws Exception JavaDoc
218         {
219         String JavaDoc[] illegal = new String JavaDoc[]
220             {
221             "a.b", "a$b", "a/b",
222             ".a", "a.", ".", ""
223             };
224         for(int n = 0; n < illegal.length; n++)
225             try {
226                 new MackerRegex(illegal[n], false).matches(context, "");
227                 fail("expected exception: \"" + illegal[n] + "\" shouldn't be a legal regex in no-parts mode");
228                 }
229             catch(MackerRegexSyntaxException mrse)
230                 { /* good */ }
231         
232         assertTrue(new MackerRegex("a*z").matches(context, "abblefrabazz"));
233         }
234     }
235
236
237
238
Popular Tags