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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck.MSG_WS_NOT_FOLLOWED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck.MSG_WS_TYPECAST;
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 WhitespaceAfterCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/whitespace/whitespaceafter";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final WhitespaceAfterCheck checkObj = new WhitespaceAfterCheck();
42          assertWithMessage(
43                  "WhitespaceAfterCheck#getRequiredTokens should return empty array by default")
44                          .that(checkObj.getRequiredTokens())
45                          .isEmpty();
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50          final String[] expected = {
51              "45:39: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
52              "74:29: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
53          };
54          verifyWithInlineConfigParser(
55                  getPath("InputWhitespaceAfterDefaultConfig.java"),
56                  expected);
57      }
58  
59      @Test
60      public void testCast() throws Exception {
61          final String[] expected = {
62              "91:20: " + getCheckMessage(MSG_WS_TYPECAST),
63          };
64          verifyWithInlineConfigParser(
65                  getPath("InputWhitespaceAfterTypeCast.java"),
66                  expected);
67      }
68  
69      @Test
70      public void testMultilineCast() throws Exception {
71          final String[] expected = {
72              "14:23: " + getCheckMessage(MSG_WS_TYPECAST),
73          };
74          verifyWithInlineConfigParser(
75                  getPath("InputWhitespaceAfterMultilineCast.java"),
76                  expected);
77      }
78  
79      @Test
80      public void testSemi() throws Exception {
81          final String[] expected = {
82              "57:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
83              "57:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
84              "106:18: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
85          };
86          verifyWithInlineConfigParser(
87                  getPath("InputWhitespaceAfterBraces.java"),
88                  expected);
89      }
90  
91      @Test
92      public void testLiteralWhile() throws Exception {
93          final String[] expected = {
94              "46:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "while"),
95          };
96          verifyWithInlineConfigParser(
97                  getPath("InputWhitespaceAfterLiteralWhile.java"),
98                  expected);
99      }
100 
101     @Test
102     public void testLiteralIf() throws Exception {
103         final String[] expected = {
104             "25:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "if"),
105         };
106         verifyWithInlineConfigParser(
107                 getPath("InputWhitespaceAfterLiteralIf.java"),
108                 expected);
109     }
110 
111     @Test
112     public void testLiteralElse() throws Exception {
113         final String[] expected = {
114             "34:11: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "else"),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputWhitespaceAfterLiteralElse.java"),
118                 expected);
119     }
120 
121     @Test
122     public void testLiteralFor() throws Exception {
123         final String[] expected = {
124             "58:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "for"),
125         };
126         verifyWithInlineConfigParser(
127                 getPath("InputWhitespaceAfterLiteralFor.java"),
128                 expected);
129     }
130 
131     @Test
132     public void testLiteralFinally() throws Exception {
133         final String[] expected = {
134             "14:13: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "finally"),
135             "17:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "finally"),
136         };
137         verifyWithInlineConfigParser(
138             getPath("InputWhitespaceAfterLiteralFinally.java"),
139             expected);
140     }
141 
142     @Test
143     public void testLiteralReturn() throws Exception {
144         final String[] expected = {
145             "17:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "return"),
146             "21:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "return"),
147             "25:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "return"),
148             "29:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "return"),
149         };
150         verifyWithInlineConfigParser(
151             getPath("InputWhitespaceAfterLiteralReturn.java"),
152             expected);
153     }
154 
155     @Test
156     public void testLiteralDo() throws Exception {
157         final String[] expected = {
158             "70:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "do"),
159         };
160         verifyWithInlineConfigParser(
161                 getPath("InputWhitespaceAfterLiteralDo.java"),
162                 expected);
163     }
164 
165     @Test
166     public void testLiteralYield() throws Exception {
167         final String[] expected = {
168             "17:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "yield"),
169         };
170         verifyWithInlineConfigParser(
171                 getNonCompilablePath("InputWhitespaceAfterLiteralYield.java"),
172                 expected);
173     }
174 
175     @Test
176     public void testLiteralSynchronized() throws Exception {
177         final String[] expected = {
178             "13:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "synchronized"),
179             "31:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "synchronized"),
180         };
181 
182         verifyWithInlineConfigParser(
183                 getPath("InputWhitespaceAfterLiteralSynchronized.java"),
184                 expected);
185     }
186 
187     @Test
188     public void testDoWhile() throws Exception {
189         final String[] expected = {
190             "25:11: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "while"),
191         };
192         verifyWithInlineConfigParser(
193                 getPath("InputWhitespaceAfterDoWhile.java"),
194                 expected);
195     }
196 
197     @Test
198     public void testLiteralTry() throws Exception {
199         final String[] expected = {
200             "20:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "try"),
201             "24:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "try"),
202         };
203         verifyWithInlineConfigParser(
204             getPath("InputWhitespaceAfterLiteralTry.java"),
205             expected);
206     }
207 
208     @Test
209     public void testLiteralCatch() throws Exception {
210         final String[] expected = {
211             "14:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "catch"),
212         };
213         verifyWithInlineConfigParser(
214             getPath("InputWhitespaceAfterLiteralCatch.java"),
215             expected);
216     }
217 
218     @Test
219     public void testLiteralCase() throws Exception {
220         final String[] expected = {
221             "15:13: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "case"),
222         };
223         verifyWithInlineConfigParser(
224             getPath("InputWhitespaceAfterLiteralCase.java"),
225             expected);
226     }
227 
228     @Test
229     public void testLiteralCase2() throws Exception {
230         final String[] expected = {
231             "13:13: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "case"),
232         };
233         verifyWithInlineConfigParser(
234             getPath("InputWhitespaceAfterLiteralCase2.java"),
235             expected);
236     }
237 
238     @Test
239     public void testEmptyForIterator() throws Exception {
240         final String[] expected = {
241             "18:30: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
242             "21:30: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
243         };
244         verifyWithInlineConfigParser(
245                 getPath("InputWhitespaceAfterFor.java"),
246                 expected);
247     }
248 
249     @Test
250     public void testTypeArgumentAndParameterCommas() throws Exception {
251         final String[] expected = {
252             "20:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
253             "20:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
254             "20:40: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
255         };
256         verifyWithInlineConfigParser(
257                 getPath("InputWhitespaceAfterGenerics.java"),
258                 expected);
259     }
260 
261     @Test
262     public void test1322879() throws Exception {
263         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
264         verifyWithInlineConfigParser(
265                 getPath("InputWhitespaceAfterAround.java"),
266                expected);
267     }
268 
269     @Test
270     public void testCountUnicodeCorrectly() throws Exception {
271         final String[] expected = {
272             "14:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
273         };
274         verifyWithInlineConfigParser(
275                 getPath("InputWhitespaceAfterCountUnicodeCorrectly.java"), expected);
276     }
277 
278     @Test
279     public void testVarargs() throws Exception {
280         final String[] expected = {
281             "14:27: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "..."),
282             "18:25: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "..."),
283             "21:36: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "..."),
284             "28:40: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "..."),
285             "37:19: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "..."),
286         };
287         verifyWithInlineConfigParser(getPath("InputWhitespaceAfterVarargs.java"), expected);
288     }
289 
290     @Test
291     public void testSwitchStatements() throws Exception {
292         final String[] expected = {
293             "18:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "switch"),
294             "31:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "switch"),
295             "33:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
296             "40:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "switch"),
297             "41:27: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
298             "42:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
299             "49:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "switch"),
300         };
301 
302         verifyWithInlineConfigParser(
303                 getNonCompilablePath("InputWhitespaceAfterSwitchStatements.java"),
304                 expected);
305     }
306 
307     @Test
308     public void testLambdaExpressions() throws Exception {
309         final String[] expected = {
310             "17:29: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
311             "19:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
312             "28:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
313         };
314 
315         verifyWithInlineConfigParser(getPath("InputWhitespaceAfterLambdaExpressions.java"),
316                 expected);
317     }
318 
319     @Test
320     public void testWhitespaceAfterWithEmoji() throws Exception {
321         final String[] expected = {
322             "13:48: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
323             "13:52: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
324             "29:32: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
325             "38:23: " + getCheckMessage(MSG_WS_TYPECAST, ";"),
326             "48:23: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
327             "48:53: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
328         };
329         verifyWithInlineConfigParser(
330             getPath("InputWhitespaceAfterWithEmoji.java"), expected);
331     }
332 
333     @Test
334     public void testLiteralWhen() throws Exception {
335         final String[] expected = {
336             "14:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
337             "16:27: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
338             "18:39: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
339             "20:39: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
340             "35:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
341             "45:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "when"),
342         };
343         verifyWithInlineConfigParser(
344             getNonCompilablePath("InputWhitespaceAfterLiteralWhen.java"),
345             expected);
346     }
347 
348     @Test
349     public void testUnnamedPattern() throws Exception {
350         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
351         verifyWithInlineConfigParser(
352             getNonCompilablePath("InputWhitespaceAfterUnnamedPattern.java"),
353             expected);
354 
355     }
356 }