View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.MSG_REGEXP_EXCEEDED;
24  import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.MSG_REGEXP_MINIMUM;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class RegexpSinglelineJavaCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexpsinglelinejava";
36      }
37  
38      @Test
39      public void testGetAcceptableTokens() {
40          final RegexpSinglelineJavaCheck regexpSinglelineJavaCheck =
41              new RegexpSinglelineJavaCheck();
42          assertWithMessage("Default acceptable tokens are invalid")
43                  .that(regexpSinglelineJavaCheck.getAcceptableTokens())
44                  .isEmpty();
45      }
46  
47      @Test
48      public void testGetRequiredTokens() {
49          final RegexpSinglelineJavaCheck checkObj = new RegexpSinglelineJavaCheck();
50          assertWithMessage("Default required tokens are invalid")
51                  .that(checkObj.getRequiredTokens())
52                  .isEmpty();
53      }
54  
55      @Test
56      public void testIt() throws Exception {
57          final String[] expected = {
58              "77: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "System\\.(out)|(err)\\.print(ln)?\\("),
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputRegexpSinglelineJavaSemantic.java"), expected);
62      }
63  
64      @Test
65      public void testMessageProperty()
66              throws Exception {
67          final String[] expected = {
68              "78: " + "Bad line :(",
69          };
70          verifyWithInlineConfigParser(
71                  getPath("InputRegexpSinglelineJavaSemantic2.java"), expected);
72      }
73  
74      @Test
75      public void testIgnoreCaseTrue() throws Exception {
76          final String[] expected = {
77              "78: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
78          };
79          verifyWithInlineConfigParser(
80                  getPath("InputRegexpSinglelineJavaSemantic3.java"), expected);
81      }
82  
83      @Test
84      public void testIgnoreCaseFalse() throws Exception {
85          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
86          verifyWithInlineConfigParser(
87                  getPath("InputRegexpSinglelineJavaSemantic4.java"), expected);
88      }
89  
90      @Test
91      public void testIgnoreCommentsCppStyle() throws Exception {
92          // See if the comment is removed properly
93          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
94          verifyWithInlineConfigParser(
95                  getPath("InputRegexpSinglelineJavaTrailingComment.java"), expected);
96      }
97  
98      @Test
99      public void testIgnoreCommentsFalseCppStyle() throws Exception {
100         // See if the comment is removed properly
101         final String[] expected = {
102             "16: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "don't\\suse trailing comments"),
103         };
104         verifyWithInlineConfigParser(
105                 getPath("InputRegexpSinglelineJavaTrailingComment2.java"), expected);
106     }
107 
108     @Test
109     public void testIgnoreCommentsBlockStyle() throws Exception {
110         // See if the comment is removed properly
111         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
112         verifyWithInlineConfigParser(
113                 getPath("InputRegexpSinglelineJavaTrailingComment3.java"), expected);
114     }
115 
116     @Test
117     public void testIgnoreCommentsFalseBlockStyle() throws Exception {
118         final String[] expected = {
119             "31: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "c-style\\s1"),
120         };
121         verifyWithInlineConfigParser(
122                 getPath("InputRegexpSinglelineJavaTrailingComment4.java"), expected);
123     }
124 
125     @Test
126     public void testIgnoreCommentsMultipleBlockStyle() throws Exception {
127         // See if a second comment on the same line is removed properly
128         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
129         verifyWithInlineConfigParser(
130                 getPath("InputRegexpSinglelineJavaTrailingComment5.java"), expected);
131     }
132 
133     @Test
134     public void testIgnoreCommentsMultiLine() throws Exception {
135         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136         verifyWithInlineConfigParser(
137                 getPath("InputRegexpSinglelineJavaTrailingComment6.java"), expected);
138     }
139 
140     @Test
141     public void testIgnoreCommentsInlineStart() throws Exception {
142         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
143         verifyWithInlineConfigParser(
144                 getPath("InputRegexpSinglelineJavaTrailingComment7.java"), expected);
145     }
146 
147     @Test
148     public void testIgnoreCommentsInlineEnd() throws Exception {
149         final String[] expected = {
150             "34: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "int z"),
151         };
152         verifyWithInlineConfigParser(
153                 getPath("InputRegexpSinglelineJavaTrailingComment8.java"), expected);
154     }
155 
156     @Test
157     public void testIgnoreCommentsInlineMiddle() throws Exception {
158         final String[] expected = {
159             "35: " + getCheckMessage(MSG_REGEXP_EXCEEDED, "int y"),
160         };
161         verifyWithInlineConfigParser(
162                 getPath("InputRegexpSinglelineJavaTrailingComment9.java"), expected);
163     }
164 
165     @Test
166     public void testIgnoreCommentsNoSpaces() throws Exception {
167         // make sure the comment is not turned into spaces
168         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
169         verifyWithInlineConfigParser(
170                 getPath("InputRegexpSinglelineJavaTrailingComment10.java"), expected);
171     }
172 
173     @Test
174     public void test1371588() throws Exception {
175         // StackOverflowError with trailing space and ignoreComments
176         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
177         verifyWithInlineConfigParser(
178                 getPath("InputRegexpSinglelineJavaTrailingComment11.java"), expected);
179     }
180 
181     @Test
182     public void testExistingInDoc() throws Exception {
183         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
184         verifyWithInlineConfigParser(
185                 getPath("InputRegexpSinglelineJavaSemantic5.java"), expected);
186     }
187 
188     @Test
189     public void testExistingInCode() throws Exception {
190         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
191         verifyWithInlineConfigParser(
192                 getPath("InputRegexpSinglelineJavaSemantic6.java"), expected);
193     }
194 
195     @Test
196     public void testMissing() throws Exception {
197         final String[] expected = {
198             "1: " + getCheckMessage(MSG_REGEXP_MINIMUM, 1, "This\\stext is not in the file"),
199         };
200         verifyWithInlineConfigParser(
201                 getPath("InputRegexpSinglelineJavaSemantic7.java"), expected);
202     }
203 
204     @Test
205     public void testDefault() throws Exception {
206         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
207         verifyWithInlineConfigParser(
208                 getPath("InputRegexpSinglelineJavaSemantic8.java"), expected);
209     }
210 }