View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck.MSG_WS_NOT_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck.MSG_WS_NOT_PRECEDED;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
28  
29  public class WhitespaceAroundExamplesTest extends AbstractExamplesModuleTestSupport {
30      @Override
31      public String getPackageLocation() {
32          return "com/puppycrawl/tools/checkstyle/checks/whitespace/whitespacearound";
33      }
34  
35      @Test
36      public void testExample1() throws Exception {
37          final String[] expected = {
38              "15:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
39              "15:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
40              "15:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
41              "21:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
42              "21:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
43              "21:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
44              "21:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
45              "24:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
46              "24:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
47              "24:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
48              "28:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
49              "28:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
50              "28:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
51              "30:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
52              "30:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
53              "34:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
54              "34:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
55              "37:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
56              "37:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
57              "41:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
58              "41:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
59          };
60  
61          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
62      }
63  
64      @Test
65      public void testExample2() throws Exception {
66          final String[] expected = {
67              "21:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
68              "21:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
69              "24:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "+="),
70              "24:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "+="),
71              "27:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "*="),
72              "27:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "*="),
73              "30:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "-="),
74              "30:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "-="),
75              "33:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "/="),
76              "33:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "/="),
77              "36:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "%="),
78              "36:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "%="),
79              "39:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ">>="),
80              "39:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ">>="),
81              "42:6: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ">>>="),
82              "42:6: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ">>>="),
83          };
84  
85          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
86      }
87  
88      @Test
89      public void testExample3() throws Exception {
90          final String[] expected = {
91              "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
92              "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
93              "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
94              "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
95              "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
96              "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
97              "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
98          };
99  
100         verifyWithInlineConfigParser(getPath("Example3.java"), expected);
101     }
102 
103     @Test
104     public void testExample4() throws Exception {
105         final String[] expected = {
106             "17:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
107             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
108             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
109             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
110             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
111             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
112             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
113             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
114             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
115             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
116             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
117             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
118             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
119             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
120             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
121             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
122             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
123             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
124             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
125         };
126 
127         verifyWithInlineConfigParser(getPath("Example4.java"), expected);
128     }
129 
130     @Test
131     public void testExample5() throws Exception {
132         final String[] expected = {
133             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
134             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
135             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
136             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
137             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
138             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
139             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
140             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
141             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
142             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
143             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
144             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
145             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
146             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
147             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
148             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
149             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
150             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
151             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
152         };
153 
154         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
155     }
156 
157     @Test
158     public void testExample6() throws Exception {
159         final String[] expected = {
160             "17:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
161             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
162             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
163             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
164             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
165             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
166             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
167             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
168             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
169             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
170             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
171             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
172             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
173             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
174             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
175             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
176             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
177             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
178             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
179             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
180             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
181         };
182 
183         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
184     }
185 
186     @Test
187     public void testExample7() throws Exception {
188         final String[] expected = {
189             "17:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
190             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
191             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
192             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
193             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
194             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
195             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
196             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
197             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
198             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
199             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
200             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
201             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
202             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
203             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
204             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
205         };
206 
207         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
208     }
209 
210     @Test
211     public void testExample8() throws Exception {
212         final String[] expected = {
213             "17:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
214             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
215             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
216             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
217             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
218             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
219             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
220             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
221             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
222             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
223             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
224             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
225             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
226             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
227             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
228             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
229             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
230             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
231         };
232 
233         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
234     }
235 
236     @Test
237     public void testExample9() throws Exception {
238         final String[] expected = {
239             "17:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
240             "17:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
241             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
242             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
243             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
244             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
245             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
246             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
247             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
248             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
249             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
250             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
251             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
252             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
253             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
254             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
255             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
256             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
257         };
258 
259         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
260     }
261 
262     @Test
263     public void testExample10() throws Exception {
264         final String[] expected = {
265             "17:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
266             "17:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
267             "17:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
268             "23:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
269             "23:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
270             "23:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
271             "23:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
272             "26:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
273             "26:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
274             "26:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
275             "29:19: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ":"),
276             "30:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
277             "30:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
278             "30:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
279             "32:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
280             "32:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
281             "36:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
282             "36:16: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
283             "39:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
284             "39:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
285             "43:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
286             "43:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
287         };
288 
289         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
290     }
291 
292     @Test
293     public void testExample11() throws Exception {
294         final String[] expected = {
295             "15:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
296             "15:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
297             "15:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
298             "21:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "->"),
299             "21:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
300             "21:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
301             "21:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
302             "24:24: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
303             "24:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
304             "24:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
305             "28:34: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
306             "28:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
307             "28:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
308             "30:8: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
309             "30:9: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
310             "37:10: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "="),
311             "37:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "="),
312             "41:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "{"),
313             "41:22: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "}"),
314         };
315 
316         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
317     }
318 
319 }