KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > RegexpCheckTest


1 package com.puppycrawl.tools.checkstyle.checks;
2
3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
5
6 public class RegexpCheckTest extends BaseCheckTestCase
7 {
8     public void testRequiredPass()
9             throws Exception JavaDoc
10     {
11         final String JavaDoc required = "Test case file";
12         final DefaultConfiguration checkConfig =
13             createCheckConfig(RegexpCheck.class);
14         checkConfig.addAttribute("format", required);
15         final String JavaDoc[] expected = {
16         };
17         verify(checkConfig, getPath("InputSemantic.java"), expected);
18     }
19
20     public void testRequiredFail()
21             throws Exception JavaDoc
22     {
23         final String JavaDoc required = "This text is not in the file";
24         final DefaultConfiguration checkConfig =
25             createCheckConfig(RegexpCheck.class);
26         checkConfig.addAttribute("format", required);
27         final String JavaDoc[] expected = {
28             "0: Required pattern '" + required + "' missing in file."
29         };
30         verify(checkConfig, getPath("InputSemantic.java"), expected);
31     }
32
33     public void testRequiredNoDuplicatesPass()
34             throws Exception JavaDoc
35     {
36         final String JavaDoc required = "Test case file";
37         final DefaultConfiguration checkConfig =
38             createCheckConfig(RegexpCheck.class);
39         checkConfig.addAttribute("format", required);
40         checkConfig.addAttribute("duplicateLimit", "0");
41         final String JavaDoc[] expected = {
42         };
43         verify(checkConfig, getPath("InputSemantic.java"), expected);
44     }
45
46     public void testRequiredNoDuplicatesFail()
47             throws Exception JavaDoc
48     {
49         final String JavaDoc required = "Boolean x = new Boolean";
50         final DefaultConfiguration checkConfig =
51             createCheckConfig(RegexpCheck.class);
52         checkConfig.addAttribute("format", required);
53         checkConfig.addAttribute("duplicateLimit", "0");
54         final String JavaDoc[] expected = {
55             "24: Found duplicate pattern '" + required + "'."
56         };
57         verify(checkConfig, getPath("InputSemantic.java"), expected);
58     }
59
60     public void testIllegalPass()
61             throws Exception JavaDoc
62     {
63         final String JavaDoc illegal = "This text is not in the file";
64         final DefaultConfiguration checkConfig =
65             createCheckConfig(RegexpCheck.class);
66         checkConfig.addAttribute("format", illegal);
67         checkConfig.addAttribute("illegalPattern", "true");
68         final String JavaDoc[] expected = {
69         };
70         verify(checkConfig, getPath("InputSemantic.java"), expected);
71     }
72
73     public void testIllegalFailBelowErrorLimit()
74             throws Exception JavaDoc
75     {
76         final String JavaDoc illegal = "^import";
77         final DefaultConfiguration checkConfig =
78             createCheckConfig(RegexpCheck.class);
79         checkConfig.addAttribute("format", illegal);
80         checkConfig.addAttribute("illegalPattern", "true");
81         checkConfig.addAttribute("errorLimit", "4");
82         final String JavaDoc[] expected = {
83             "7: Line matches the illegal pattern '" + illegal + "'.",
84             "8: Line matches the illegal pattern '" + illegal + "'.",
85             "9: Line matches the illegal pattern '" + illegal + "'."
86         };
87         verify(checkConfig, getPath("InputSemantic.java"), expected);
88     }
89
90     public void testIllegalFailAboveErrorLimit()
91             throws Exception JavaDoc
92     {
93         final String JavaDoc illegal = "^import";
94         final String JavaDoc error = "The error limit has been exceeded, "
95         + "the check is aborting, there may be more unreported errors.";
96         final DefaultConfiguration checkConfig =
97             createCheckConfig(RegexpCheck.class);
98         checkConfig.addAttribute("format", illegal);
99         checkConfig.addAttribute("illegalPattern", "true");
100         checkConfig.addAttribute("errorLimit", "3");
101         final String JavaDoc[] expected = {
102             "7: Line matches the illegal pattern '" + illegal + "'.",
103             "8: Line matches the illegal pattern '" + illegal + "'.",
104             "9: Line matches the illegal pattern '" + error + illegal + "'."
105         };
106         verify(checkConfig, getPath("InputSemantic.java"), expected);
107     }
108
109     public void testMessagePropertyGood()
110         throws Exception JavaDoc
111     {
112         final String JavaDoc illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
113         final String JavaDoc message = "Bad line :(";
114         final DefaultConfiguration checkConfig =
115             createCheckConfig(RegexpCheck.class);
116         checkConfig.addAttribute("format", illegal);
117         checkConfig.addAttribute("illegalPattern", "true");
118         checkConfig.addAttribute("message", message);
119         final String JavaDoc[] expected = {
120             "69: Line matches the illegal pattern '" + message + "'."
121         };
122         verify(checkConfig, getPath("InputSemantic.java"), expected);
123     }
124
125     public void testMessagePropertyBad()
126         throws Exception JavaDoc
127     {
128         final String JavaDoc illegal = "System\\.(out)|(err)\\.print(ln)?\\(";
129         final DefaultConfiguration checkConfig =
130             createCheckConfig(RegexpCheck.class);
131         checkConfig.addAttribute("format", illegal);
132         checkConfig.addAttribute("illegalPattern", "true");
133         checkConfig.addAttribute("message", null);
134         final String JavaDoc[] expected = {
135             "69: Line matches the illegal pattern '" + illegal + "'."
136         };
137         verify(checkConfig, getPath("InputSemantic.java"), expected);
138     }
139     
140     public void testIgnoreCaseTrue()
141             throws Exception JavaDoc
142     {
143         final String JavaDoc illegal = "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
144         final DefaultConfiguration checkConfig =
145             createCheckConfig(RegexpCheck.class);
146         checkConfig.addAttribute("format", illegal);
147         checkConfig.addAttribute("illegalPattern", "true");
148         final String JavaDoc[] expected = {
149             "69: Line matches the illegal pattern '" + illegal + "'."
150         };
151         verify(checkConfig, getPath("InputSemantic.java"), expected);
152     }
153
154     public void testIgnoreCaseFalse()
155             throws Exception JavaDoc
156     {
157         final String JavaDoc illegalTrue = "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
158         final DefaultConfiguration checkConfigTrue =
159             createCheckConfig(RegexpCheck.class);
160         checkConfigTrue.addAttribute("format", illegalTrue);
161         checkConfigTrue.addAttribute("illegalPattern", "true");
162         final String JavaDoc[] expectedTrue = {
163             "69: Line matches the illegal pattern '" + illegalTrue + "'."};
164         verify(checkConfigTrue, getPath("InputSemantic.java"), expectedTrue);
165         
166         final String JavaDoc illegalFalse = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(";
167         final DefaultConfiguration checkConfigFalse =
168             createCheckConfig(RegexpCheck.class);
169         checkConfigFalse.addAttribute("format", illegalFalse);
170         checkConfigFalse.addAttribute("illegalPattern", "true");
171         final String JavaDoc[] expectedFalse = {};
172         verify(checkConfigFalse, getPath("InputSemantic.java"), expectedFalse);
173     }
174
175     public void testIgnoreCommentsCppStyle()
176             throws Exception JavaDoc
177     {
178         // See if the comment is removed properly
179
final String JavaDoc illegal = "don't use trailing comments";
180         final DefaultConfiguration checkConfig =
181             createCheckConfig(RegexpCheck.class);
182         checkConfig.addAttribute("format", illegal);
183         checkConfig.addAttribute("illegalPattern", "true");
184         checkConfig.addAttribute("ignoreComments", "true");
185         final String JavaDoc[] expected = {
186         };
187         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
188     }
189
190     public void testIgnoreCommentsFalseCppStyle()
191             throws Exception JavaDoc
192     {
193         // See if the comment is removed properly
194
final String JavaDoc illegal = "don't use trailing comments";
195         final DefaultConfiguration checkConfig =
196             createCheckConfig(RegexpCheck.class);
197         checkConfig.addAttribute("format", illegal);
198         checkConfig.addAttribute("illegalPattern", "true");
199         checkConfig.addAttribute("ignoreComments", "false");
200         final String JavaDoc[] expected = {
201             "2: Line matches the illegal pattern '" + illegal + "'."
202         };
203         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
204     }
205
206     public void testIgnoreCommentsCStyle()
207             throws Exception JavaDoc
208     {
209         // See if the comment is removed properly
210
final String JavaDoc illegal = "c-style 1";
211         final DefaultConfiguration checkConfig =
212             createCheckConfig(RegexpCheck.class);
213         checkConfig.addAttribute("format", illegal);
214         checkConfig.addAttribute("illegalPattern", "true");
215         checkConfig.addAttribute("ignoreComments", "true");
216         final String JavaDoc[] expected = {
217         };
218         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
219     }
220
221     public void testIgnoreCommentsFalseCStyle()
222             throws Exception JavaDoc
223     {
224         final String JavaDoc illegal = "c-style 1";
225         final DefaultConfiguration checkConfig =
226             createCheckConfig(RegexpCheck.class);
227         checkConfig.addAttribute("format", illegal);
228         checkConfig.addAttribute("illegalPattern", "true");
229         checkConfig.addAttribute("ignoreComments", "false");
230         final String JavaDoc[] expected = {
231             "17: Line matches the illegal pattern '" + illegal + "'."
232         };
233         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
234     }
235
236     public void testIgnoreCommentsMultipleCStyle()
237             throws Exception JavaDoc
238     {
239         // See if a second comment on the same line is removed properly
240
final String JavaDoc illegal = "c-style 2";
241         final DefaultConfiguration checkConfig =
242             createCheckConfig(RegexpCheck.class);
243         checkConfig.addAttribute("format", illegal);
244         checkConfig.addAttribute("illegalPattern", "true");
245         checkConfig.addAttribute("ignoreComments", "true");
246         final String JavaDoc[] expected = {
247         };
248         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
249     }
250
251     public void testIgnoreCommentsMultiLine()
252             throws Exception JavaDoc
253     {
254         final String JavaDoc illegal = "Let's check multi-line comments";
255         final DefaultConfiguration checkConfig =
256             createCheckConfig(RegexpCheck.class);
257         checkConfig.addAttribute("format", illegal);
258         checkConfig.addAttribute("illegalPattern", "true");
259         checkConfig.addAttribute("ignoreComments", "true");
260         final String JavaDoc[] expected = {
261         };
262         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
263     }
264
265     public void testIgnoreCommentsInlineStart()
266             throws Exception JavaDoc
267     {
268         final String JavaDoc illegal = "long ms /";
269         final DefaultConfiguration checkConfig =
270             createCheckConfig(RegexpCheck.class);
271         checkConfig.addAttribute("format", illegal);
272         checkConfig.addAttribute("illegalPattern", "true");
273         checkConfig.addAttribute("ignoreComments", "true");
274         final String JavaDoc[] expected = {
275         };
276         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
277     }
278
279     public void testIgnoreCommentsInlineEnd()
280             throws Exception JavaDoc
281     {
282         final String JavaDoc illegal = "int z";
283         final DefaultConfiguration checkConfig =
284             createCheckConfig(RegexpCheck.class);
285         checkConfig.addAttribute("format", illegal);
286         checkConfig.addAttribute("illegalPattern", "true");
287         checkConfig.addAttribute("ignoreComments", "true");
288         final String JavaDoc[] expected = {
289             "20: Line matches the illegal pattern '" + illegal + "'."
290         };
291         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
292     }
293     
294     public void testIgnoreCommentsInlineMiddle()
295             throws Exception JavaDoc
296     {
297         final String JavaDoc illegal = "int y";
298         final DefaultConfiguration checkConfig =
299             createCheckConfig(RegexpCheck.class);
300         checkConfig.addAttribute("format", illegal);
301         checkConfig.addAttribute("illegalPattern", "true");
302         checkConfig.addAttribute("ignoreComments", "true");
303         final String JavaDoc[] expected = {
304             "21: Line matches the illegal pattern '" + illegal + "'."
305         };
306         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
307     }
308
309     public void testIgnoreCommentsNoSpaces()
310             throws Exception JavaDoc
311     {
312         // make sure the comment is not turned into spaces
313
final String JavaDoc illegal = "long ms ";
314         final DefaultConfiguration checkConfig =
315             createCheckConfig(RegexpCheck.class);
316         checkConfig.addAttribute("format", illegal);
317         checkConfig.addAttribute("illegalPattern", "true");
318         checkConfig.addAttribute("ignoreComments", "true");
319         final String JavaDoc[] expected = {
320         };
321         verify(checkConfig, getPath("InputTrailingComment.java"), expected);
322     }
323 }
324
Popular Tags