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.MultilineDetector.MSG_REGEXP_EXCEEDED;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.MSG_REGEXP_MINIMUM;
24  import static org.junit.Assert.assertArrayEquals;
25  
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class RegexpSinglelineJavaCheckTest extends AbstractModuleTestSupport {
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexpsinglelinejava";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final RegexpSinglelineJavaCheck checkObj = new RegexpSinglelineJavaCheck();
41          assertArrayEquals("Default required tokens are invalid",
42              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
43      }
44  
45      @Test
46      public void testIt() throws Exception {
47          final DefaultConfiguration checkConfig =
48                  createModuleConfig(RegexpSinglelineJavaCheck.class);
49          checkConfig.addAttribute("format", "System\\.(out)|(err)\\.print(ln)?\\(");
50          final String[] expected = {
51              "69: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "System\\.(out)|(err)\\.print(ln)?\\("),
52          };
53          verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
54      }
55  
56      @Test
57      public void testMessageProperty()
58              throws Exception {
59          final DefaultConfiguration checkConfig =
60                  createModuleConfig(RegexpSinglelineJavaCheck.class);
61          checkConfig.addAttribute("format", "System\\.(out)|(err)\\.print(ln)?\\(");
62          checkConfig.addAttribute("message", "Bad line :(");
63          final String[] expected = {
64              "69: " + "Bad line :(",
65          };
66          verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
67      }
68  
69      @Test
70      public void testIgnoreCaseTrue() throws Exception {
71          final DefaultConfiguration checkConfig =
72                  createModuleConfig(RegexpSinglelineJavaCheck.class);
73          checkConfig.addAttribute("format", "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(");
74          checkConfig.addAttribute("ignoreCase", "true");
75          final String[] expected = {
76              "69: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
77          };
78          verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
79      }
80  
81      @Test
82      public void testIgnoreCaseFalse() throws Exception {
83          final DefaultConfiguration checkConfig =
84                  createModuleConfig(RegexpSinglelineJavaCheck.class);
85          checkConfig.addAttribute("format", "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\(");
86          checkConfig.addAttribute("ignoreCase", "false");
87          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
88          verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
89      }
90  
91      @Test
92      public void testIgnoreCommentsCppStyle() throws Exception {
93          final DefaultConfiguration checkConfig =
94                  createModuleConfig(RegexpSinglelineJavaCheck.class);
95          // See if the comment is removed properly
96          checkConfig.addAttribute("format", "don't use trailing comments");
97          checkConfig.addAttribute("ignoreComments", "true");
98          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
99          verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
100     }
101 
102     @Test
103     public void testIgnoreCommentsFalseCppStyle() throws Exception {
104         final DefaultConfiguration checkConfig =
105                 createModuleConfig(RegexpSinglelineJavaCheck.class);
106         // See if the comment is removed properly
107         checkConfig.addAttribute("format", "don't use trailing comments");
108         checkConfig.addAttribute("ignoreComments", "false");
109         final String[] expected = {
110             "4: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "don't use trailing comments"),
111         };
112         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
113     }
114 
115     @Test
116     public void testIgnoreCommentsBlockStyle() throws Exception {
117         final DefaultConfiguration checkConfig =
118                 createModuleConfig(RegexpSinglelineJavaCheck.class);
119         // See if the comment is removed properly
120         checkConfig.addAttribute("format", "c-style 1");
121         checkConfig.addAttribute("ignoreComments", "true");
122         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
123         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
124     }
125 
126     @Test
127     public void testIgnoreCommentsFalseBlockStyle() throws Exception {
128         final DefaultConfiguration checkConfig =
129                 createModuleConfig(RegexpSinglelineJavaCheck.class);
130         checkConfig.addAttribute("format", "c-style 1");
131         checkConfig.addAttribute("ignoreComments", "false");
132         final String[] expected = {
133             "19: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "c-style 1"),
134         };
135         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
136     }
137 
138     @Test
139     public void testIgnoreCommentsMultipleBlockStyle() throws Exception {
140         final DefaultConfiguration checkConfig =
141                 createModuleConfig(RegexpSinglelineJavaCheck.class);
142         // See if a second comment on the same line is removed properly
143         checkConfig.addAttribute("format", "c-style 2");
144         checkConfig.addAttribute("ignoreComments", "true");
145         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
146         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
147     }
148 
149     @Test
150     public void testIgnoreCommentsMultiLine() throws Exception {
151         final DefaultConfiguration checkConfig =
152                 createModuleConfig(RegexpSinglelineJavaCheck.class);
153         checkConfig.addAttribute("format", "Let's check multi-line comments");
154         checkConfig.addAttribute("ignoreComments", "true");
155         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
156         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
157     }
158 
159     @Test
160     public void testIgnoreCommentsInlineStart() throws Exception {
161         final DefaultConfiguration checkConfig =
162                 createModuleConfig(RegexpSinglelineJavaCheck.class);
163         checkConfig.addAttribute("format", "long ms /");
164         checkConfig.addAttribute("ignoreComments", "true");
165         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
166         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
167     }
168 
169     @Test
170     public void testIgnoreCommentsInlineEnd() throws Exception {
171         final DefaultConfiguration checkConfig =
172                 createModuleConfig(RegexpSinglelineJavaCheck.class);
173         checkConfig.addAttribute("format", "int z");
174         checkConfig.addAttribute("ignoreComments", "true");
175         final String[] expected = {
176             "22: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "int z"),
177         };
178         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
179     }
180 
181     @Test
182     public void testIgnoreCommentsInlineMiddle() throws Exception {
183         final DefaultConfiguration checkConfig =
184                 createModuleConfig(RegexpSinglelineJavaCheck.class);
185         checkConfig.addAttribute("format", "int y");
186         checkConfig.addAttribute("ignoreComments", "true");
187         final String[] expected = {
188             "23: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "int y"),
189         };
190         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
191     }
192 
193     @Test
194     public void testIgnoreCommentsNoSpaces() throws Exception {
195         final DefaultConfiguration checkConfig =
196                 createModuleConfig(RegexpSinglelineJavaCheck.class);
197         // make sure the comment is not turned into spaces
198         checkConfig.addAttribute("format", "long ms  ");
199         checkConfig.addAttribute("ignoreComments", "true");
200         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
201         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
202     }
203 
204     @Test
205     public void test1371588() throws Exception {
206         final DefaultConfiguration checkConfig =
207                 createModuleConfig(RegexpSinglelineJavaCheck.class);
208         // StackOverflowError with trailing space and ignoreComments
209         checkConfig.addAttribute("format", "\\s+$");
210         checkConfig.addAttribute("ignoreComments", "true");
211         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
212         verify(checkConfig, getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
213     }
214 
215     @Test
216     public void testExistingInDoc() throws Exception {
217         final DefaultConfiguration checkConfig =
218                 createModuleConfig(RegexpSinglelineJavaCheck.class);
219         checkConfig.addAttribute("format", "Test case file");
220         checkConfig.addAttribute("minimum", "1");
221         checkConfig.addAttribute("maximum", "1000");
222         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
223         verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
224     }
225 
226     @Test
227     public void testExistingInCode() throws Exception {
228         final DefaultConfiguration checkConfig =
229                 createModuleConfig(RegexpSinglelineJavaCheck.class);
230         checkConfig.addAttribute("format", "package");
231         checkConfig.addAttribute("minimum", "1");
232         checkConfig.addAttribute("maximum", "1000");
233         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
234         verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
235     }
236 
237     @Test
238     public void testMissing() throws Exception {
239         final DefaultConfiguration checkConfig =
240                 createModuleConfig(RegexpSinglelineJavaCheck.class);
241         checkConfig.addAttribute("format", "This text is not in the file");
242         checkConfig.addAttribute("minimum", "1");
243         checkConfig.addAttribute("maximum", "1000");
244         final String[] expected = {
245             "0: " + getCheckMessage(MSG_REGEXP_MINIMUM, 1, "This text is not in the file"),
246         };
247         verify(checkConfig, getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
248     }
249 }