View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.regexp;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_DUPLICATE_REGEXP;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLEGAL_REGEXP;
24  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_REQUIRED_REGEXP;
25  import static org.junit.Assert.assertArrayEquals;
26  
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class RegexpCheckTest extends AbstractModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexp";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final RegexpCheck checkObj = new RegexpCheck();
42          assertArrayEquals(
43              "RegexpCheck#getRequiredTockens should return empty array by default",
44              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
45      }
46  
47      @Test
48      public void testRequiredPass() throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(RegexpCheck.class);
51          checkConfig.addAttribute("format", "Test case file");
52          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
53          verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
54      }
55  
56      @Test
57      public void testRequiredFail() throws Exception {
58          final DefaultConfiguration checkConfig =
59              createModuleConfig(RegexpCheck.class);
60          checkConfig.addAttribute("format", "This text is not in the file");
61          final String[] expected = {
62              "0: " + getCheckMessage(MSG_REQUIRED_REGEXP, "This text is not in the file"),
63          };
64          verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
65      }
66  
67      @Test
68      public void testRequiredNoDuplicatesPass() throws Exception {
69          final DefaultConfiguration checkConfig =
70              createModuleConfig(RegexpCheck.class);
71          checkConfig.addAttribute("format", "Test case file");
72          checkConfig.addAttribute("duplicateLimit", "0");
73          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
74          verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
75      }
76  
77      @Test
78      public void testSetDuplicatesTrue() throws Exception {
79          final DefaultConfiguration checkConfig = createModuleConfig(RegexpCheck.class);
80          checkConfig.addAttribute("format", "Test case file");
81          checkConfig.addAttribute("duplicateLimit", "-1");
82          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
83          verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
84      }
85  
86      @Test
87      public void testRequiredNoDuplicatesFail() throws Exception {
88          final DefaultConfiguration checkConfig =
89              createModuleConfig(RegexpCheck.class);
90          checkConfig.addAttribute("format", "Boolean x = new Boolean");
91          checkConfig.addAttribute("duplicateLimit", "0");
92          final String[] expected = {
93              "24: " + getCheckMessage(MSG_DUPLICATE_REGEXP, "Boolean x = new Boolean"),
94          };
95          verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
96      }
97  
98      @Test
99      public void testIllegalPass() throws Exception {
100         final DefaultConfiguration checkConfig =
101             createModuleConfig(RegexpCheck.class);
102         checkConfig.addAttribute("format", "This text is not in the file");
103         checkConfig.addAttribute("illegalPattern", "true");
104         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
105         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
106     }
107 
108     @Test
109     public void testIllegalFailBelowErrorLimit() throws Exception {
110         final DefaultConfiguration checkConfig =
111             createModuleConfig(RegexpCheck.class);
112         checkConfig.addAttribute("format", "^import");
113         checkConfig.addAttribute("illegalPattern", "true");
114         checkConfig.addAttribute("errorLimit", "4");
115         final String[] expected = {
116             "7: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
117             "8: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
118             "9: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
119         };
120         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
121     }
122 
123     @Test
124     public void testIllegalFailAboveErrorLimit() throws Exception {
125         final DefaultConfiguration checkConfig =
126             createModuleConfig(RegexpCheck.class);
127         checkConfig.addAttribute("format", "^import");
128         checkConfig.addAttribute("illegalPattern", "true");
129         checkConfig.addAttribute("errorLimit", "2");
130         final String error = "The error limit has been exceeded, "
131                 + "the check is aborting, there may be more unreported errors.";
132         final String[] expected = {
133             "7: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
134             "8: " + getCheckMessage(MSG_ILLEGAL_REGEXP, error + "^import"),
135         };
136         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
137     }
138 
139     @Test
140     public void testMessagePropertyGood()
141             throws Exception {
142         final DefaultConfiguration checkConfig =
143             createModuleConfig(RegexpCheck.class);
144         checkConfig.addAttribute("format", "System\\.(out)|(err)\\.print(ln)?\\(");
145         checkConfig.addAttribute("illegalPattern", "true");
146         checkConfig.addAttribute("message", "Bad line :(");
147         final String[] expected = {
148             "69: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Bad line :("),
149         };
150         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
151     }
152 
153     @Test
154     public void testMessagePropertyBad()
155             throws Exception {
156         final DefaultConfiguration checkConfig =
157             createModuleConfig(RegexpCheck.class);
158         checkConfig.addAttribute("format", "System\\.(out)|(err)\\.print(ln)?\\(");
159         checkConfig.addAttribute("illegalPattern", "true");
160         checkConfig.addAttribute("message", null);
161         final String[] expected = {
162             "69: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
163         };
164         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
165     }
166 
167     @Test
168     public void testIgnoreCaseTrue() throws Exception {
169         final DefaultConfiguration checkConfig =
170             createModuleConfig(RegexpCheck.class);
171         checkConfig.addAttribute("format", "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(");
172         checkConfig.addAttribute("illegalPattern", "true");
173         final String[] expected = {
174             "69: " + getCheckMessage(MSG_ILLEGAL_REGEXP,
175                     "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
176         };
177         verify(checkConfig, getPath("InputRegexpSemantic.java"), expected);
178     }
179 
180     @Test
181     public void testIgnoreCaseFalse() throws Exception {
182         final DefaultConfiguration checkConfigTrue =
183             createModuleConfig(RegexpCheck.class);
184         checkConfigTrue.addAttribute("format", "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(");
185         checkConfigTrue.addAttribute("illegalPattern", "true");
186         final String[] expectedTrue = {
187             "69: " + getCheckMessage(MSG_ILLEGAL_REGEXP,
188                     "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
189         };
190         verify(checkConfigTrue, getPath("InputRegexpSemantic.java"), expectedTrue);
191 
192         final DefaultConfiguration checkConfigFalse =
193             createModuleConfig(RegexpCheck.class);
194         checkConfigFalse.addAttribute("format", "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(");
195         checkConfigFalse.addAttribute("illegalPattern", "true");
196         final String[] expectedFalse = CommonUtils.EMPTY_STRING_ARRAY;
197         verify(checkConfigFalse, getPath("InputRegexpSemantic.java"), expectedFalse);
198     }
199 
200     @Test
201     public void testIgnoreCommentsCppStyle() throws Exception {
202         // See if the comment is removed properly
203         final DefaultConfiguration checkConfig =
204             createModuleConfig(RegexpCheck.class);
205         checkConfig.addAttribute("format", "don't use trailing comments");
206         checkConfig.addAttribute("illegalPattern", "true");
207         checkConfig.addAttribute("ignoreComments", "true");
208         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
209         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
210     }
211 
212     @Test
213     public void testIgnoreCommentsFalseCppStyle() throws Exception {
214         // See if the comment is removed properly
215         final DefaultConfiguration checkConfig =
216             createModuleConfig(RegexpCheck.class);
217         checkConfig.addAttribute("format", "don't use trailing comments");
218         checkConfig.addAttribute("illegalPattern", "true");
219         checkConfig.addAttribute("ignoreComments", "false");
220         final String[] expected = {
221             "4: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "don't use trailing comments"),
222         };
223         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
224     }
225 
226     @Test
227     public void testIgnoreCommentsBlockStyle() throws Exception {
228         // See if the comment is removed properly
229         final DefaultConfiguration checkConfig =
230             createModuleConfig(RegexpCheck.class);
231         checkConfig.addAttribute("format", "c-style 1");
232         checkConfig.addAttribute("illegalPattern", "true");
233         checkConfig.addAttribute("ignoreComments", "true");
234         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
235         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
236     }
237 
238     @Test
239     public void testIgnoreCommentsFalseBlockStyle() throws Exception {
240         final DefaultConfiguration checkConfig =
241             createModuleConfig(RegexpCheck.class);
242         checkConfig.addAttribute("format", "c-style 1");
243         checkConfig.addAttribute("illegalPattern", "true");
244         checkConfig.addAttribute("ignoreComments", "false");
245         final String[] expected = {
246             "19: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "c-style 1"),
247         };
248         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
249     }
250 
251     @Test
252     public void testIgnoreCommentsMultipleBlockStyle() throws Exception {
253         // See if a second comment on the same line is removed properly
254         final DefaultConfiguration checkConfig =
255             createModuleConfig(RegexpCheck.class);
256         checkConfig.addAttribute("format", "c-style 2");
257         checkConfig.addAttribute("illegalPattern", "true");
258         checkConfig.addAttribute("ignoreComments", "true");
259         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
260         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
261     }
262 
263     @Test
264     public void testIgnoreCommentsMultiLine() throws Exception {
265         final DefaultConfiguration checkConfig =
266             createModuleConfig(RegexpCheck.class);
267         checkConfig.addAttribute("format", "Let's check multi-line comments");
268         checkConfig.addAttribute("illegalPattern", "true");
269         checkConfig.addAttribute("ignoreComments", "true");
270         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
271         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
272     }
273 
274     @Test
275     public void testIgnoreCommentsInlineStart() throws Exception {
276         final DefaultConfiguration checkConfig =
277             createModuleConfig(RegexpCheck.class);
278         checkConfig.addAttribute("format", "long ms /");
279         checkConfig.addAttribute("illegalPattern", "true");
280         checkConfig.addAttribute("ignoreComments", "true");
281         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
282         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
283     }
284 
285     @Test
286     public void testIgnoreCommentsInlineEnd() throws Exception {
287         final DefaultConfiguration checkConfig =
288             createModuleConfig(RegexpCheck.class);
289         checkConfig.addAttribute("format", "int z");
290         checkConfig.addAttribute("illegalPattern", "true");
291         checkConfig.addAttribute("ignoreComments", "true");
292         final String[] expected = {
293             "22: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "int z"),
294         };
295         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
296     }
297 
298     @Test
299     public void testIgnoreCommentsInlineMiddle() throws Exception {
300         final DefaultConfiguration checkConfig =
301             createModuleConfig(RegexpCheck.class);
302         checkConfig.addAttribute("format", "int y");
303         checkConfig.addAttribute("illegalPattern", "true");
304         checkConfig.addAttribute("ignoreComments", "true");
305         final String[] expected = {
306             "23: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "int y"),
307         };
308         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
309     }
310 
311     @Test
312     public void testIgnoreCommentsNoSpaces() throws Exception {
313         // make sure the comment is not turned into spaces
314         final DefaultConfiguration checkConfig =
315             createModuleConfig(RegexpCheck.class);
316         checkConfig.addAttribute("format", "long ms  ");
317         checkConfig.addAttribute("illegalPattern", "true");
318         checkConfig.addAttribute("ignoreComments", "true");
319         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
320         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
321     }
322 
323     @Test
324     public void testOnFileStartingWithEmptyLine() throws Exception {
325         final DefaultConfiguration checkConfig = createModuleConfig(RegexpCheck.class);
326         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
327         verify(checkConfig, getPath("InputRegexpStartingWithEmptyLine.java"), expected);
328     }
329 
330     @Test
331     public void testIgnoreCommentsCppStyleWithIllegalPatternFalse() throws Exception {
332         // See if the comment is removed properly
333         final DefaultConfiguration checkConfig =
334                 createModuleConfig(RegexpCheck.class);
335         checkConfig.addAttribute("format", "don't use trailing comments");
336         checkConfig.addAttribute("illegalPattern", "false");
337         checkConfig.addAttribute("ignoreComments", "true");
338         final String[] expected = {
339             "0: " + getCheckMessage(MSG_REQUIRED_REGEXP, "don't use trailing comments"),
340         };
341         verify(checkConfig, getPath("InputRegexpTrailingComment.java"), expected);
342     }
343 }