1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY;
24 import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY_EXT;
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 VariableDeclarationUsageDistanceCheckTest extends
32 AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance";
37 }
38
39 @Test
40 public void testGeneralLogic() throws Exception {
41 final String[] expected = {
42 "42:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
43 "50:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
44 "56:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
45 "69:9: " + getCheckMessage(MSG_KEY, "count", 2, 1),
46 "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
47 "108:9: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
48 "156:9: " + getCheckMessage(MSG_KEY, "m", 3, 1),
49 "157:9: " + getCheckMessage(MSG_KEY, "n", 2, 1),
50 "196:9: " + getCheckMessage(MSG_KEY, "result", 2, 1),
51 "231:9: " + getCheckMessage(MSG_KEY, "t", 5, 1),
52 "234:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
53 "235:9: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
54 "272:9: " + getCheckMessage(MSG_KEY, "sel", 2, 1),
55 "273:9: " + getCheckMessage(MSG_KEY, "model", 2, 1),
56 "299:9: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
57 "312:9: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
58 "355:9: " + getCheckMessage(MSG_KEY, "green", 2, 1),
59 "356:9: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
60 "379:9: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
61 "466:9: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
62 "467:9: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
63 "483:9: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
64 "483:9: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
65 "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
66 "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
67 "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
68 "517:9: " + getCheckMessage(MSG_KEY, "files", 2, 1),
69 "552:13: " + getCheckMessage(MSG_KEY, "id", 2, 1),
70 "554:13: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
71 "903:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
72 "913:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
73 "979:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
74 "990:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
75 "1001:9: " + getCheckMessage(MSG_KEY, "a", 3, 1),
76 "1036:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
77 "1066:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputVariableDeclarationUsageDistanceGeneral.java"), expected);
81 }
82
83 @Test
84 public void testGeneralLogic2() throws Exception {
85 final String[] expected = {
86 "17:9: " + getCheckMessage(MSG_KEY, "first", 5, 1),
87 "29:9: " + getCheckMessage(MSG_KEY, "allInvariants", 2, 1),
88 };
89 verifyWithInlineConfigParser(
90 getPath("InputVariableDeclarationUsageDistanceGeneral2.java"), expected);
91 }
92
93 @Test
94 public void testIfStatements() throws Exception {
95 final String[] expected = {
96 "18:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
97 "28:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
98 "32:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
99 "38:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
100 "49:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
101 "50:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
102 "51:9: " + getCheckMessage(MSG_KEY, "d", 4, 1),
103 "63:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
104
105 };
106 verifyWithInlineConfigParser(
107 getPath("InputVariableDeclarationUsageDistanceIfStatements.java"), expected);
108 }
109
110 @Test
111 public void testDistance() throws Exception {
112 final String[] expected = {
113 "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 3),
114 "231:9: " + getCheckMessage(MSG_KEY, "t", 5, 3),
115 "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 3),
116 "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 3),
117 "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 3),
118 "903:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
119 "913:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
120 "979:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
121 "1066:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
122 };
123 verifyWithInlineConfigParser(
124 getPath("InputVariableDeclarationUsageDistance.java"), expected);
125 }
126
127 @Test
128 public void testVariableRegExp() throws Exception {
129 final String[] expected = {
130 "50:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
131 "56:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
132 "69:9: " + getCheckMessage(MSG_KEY, "count", 2, 1),
133 "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
134 "108:9: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
135 "157:9: " + getCheckMessage(MSG_KEY, "n", 2, 1),
136 "196:9: " + getCheckMessage(MSG_KEY, "result", 2, 1),
137 "235:9: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
138 "272:9: " + getCheckMessage(MSG_KEY, "sel", 2, 1),
139 "273:9: " + getCheckMessage(MSG_KEY, "model", 2, 1),
140 "299:9: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
141 "312:9: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
142 "355:9: " + getCheckMessage(MSG_KEY, "green", 2, 1),
143 "356:9: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
144 "379:9: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
145 "466:9: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
146 "467:9: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
147 "483:9: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
148 "483:9: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
149 "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
150 "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
151 "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
152 "517:9: " + getCheckMessage(MSG_KEY, "files", 2, 1),
153 "552:13: " + getCheckMessage(MSG_KEY, "id", 2, 1),
154 "554:13: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
155 };
156 verifyWithInlineConfigParser(
157 getPath("InputVariableDeclarationUsageDistanceRegExp.java"), expected);
158 }
159
160 @Test
161 public void testValidateBetweenScopesOption() throws Exception {
162 final String[] expected = {
163 "42:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
164 "50:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
165 "56:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
166 "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
167 "108:9: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
168 "231:9: " + getCheckMessage(MSG_KEY, "t", 5, 1),
169 "234:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
170 "235:9: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
171 "272:9: " + getCheckMessage(MSG_KEY, "sel", 2, 1),
172 "273:9: " + getCheckMessage(MSG_KEY, "model", 2, 1),
173 "312:9: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
174 "355:9: " + getCheckMessage(MSG_KEY, "green", 2, 1),
175 "356:9: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
176 "379:9: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
177 "466:9: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
178 "467:9: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
179 "483:9: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
180 "483:9: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
181 "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
182 "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
183 "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
184 "517:9: " + getCheckMessage(MSG_KEY, "files", 2, 1),
185 "552:13: " + getCheckMessage(MSG_KEY, "id", 2, 1),
186 "554:13: " + getCheckMessage(MSG_KEY, "parentId", 4, 1),
187 "855:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
188 "867:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
189 "879:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
190 "891:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
191 "903:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
192 "913:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
193 "924:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
194 "945:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
195 "990:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
196 "1001:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
197 "1036:9: " + getCheckMessage(MSG_KEY, "c", 4, 1),
198 "1066:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
199 };
200 verifyWithInlineConfigParser(
201 getPath("InputVariableDeclarationUsageDistanceScopes.java"), expected);
202 }
203
204 @Test
205 public void testIgnoreFinalOption() throws Exception {
206 final String[] expected = {
207 "42:9: " + getCheckMessage(MSG_KEY_EXT, "a", 2, 1),
208 "50:9: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
209 "56:9: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
210 "69:9: " + getCheckMessage(MSG_KEY_EXT, "count", 2, 1),
211 "83:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
212 "108:9: " + getCheckMessage(MSG_KEY_EXT, "arg", 2, 1),
213 "156:9: " + getCheckMessage(MSG_KEY_EXT, "m", 3, 1),
214 "157:9: " + getCheckMessage(MSG_KEY_EXT, "n", 2, 1),
215 "196:9: " + getCheckMessage(MSG_KEY_EXT, "result", 2, 1),
216 "231:9: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 1),
217 "234:9: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
218 "235:9: " + getCheckMessage(MSG_KEY_EXT, "d2", 3, 1),
219 "272:9: " + getCheckMessage(MSG_KEY_EXT, "sel", 2, 1),
220 "273:9: " + getCheckMessage(MSG_KEY_EXT, "model", 2, 1),
221 "299:9: " + getCheckMessage(MSG_KEY_EXT, "sw", 2, 1),
222 "312:9: " + getCheckMessage(MSG_KEY_EXT, "wh", 2, 1),
223 "355:9: " + getCheckMessage(MSG_KEY_EXT, "green", 2, 1),
224 "356:9: " + getCheckMessage(MSG_KEY_EXT, "blue", 3, 1),
225 "466:9: " + getCheckMessage(MSG_KEY_EXT, "aOpt", 3, 1),
226 "467:9: " + getCheckMessage(MSG_KEY_EXT, "bOpt", 2, 1),
227 "483:9: " + getCheckMessage(MSG_KEY_EXT, "l1", 3, 1),
228 "483:9: " + getCheckMessage(MSG_KEY_EXT, "l2", 2, 1),
229 "491:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 1),
230 "503:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 1),
231 "516:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
232 "517:9: " + getCheckMessage(MSG_KEY_EXT, "files", 2, 1),
233 "552:13: " + getCheckMessage(MSG_KEY_EXT, "id", 2, 1),
234 "554:13: " + getCheckMessage(MSG_KEY_EXT, "parentId", 3, 1),
235 "903:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
236 "913:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
237 "979:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
238 "1001:9: " + getCheckMessage(MSG_KEY_EXT, "a", 3, 1),
239 "1036:9: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
240 "1066:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
241 };
242 verifyWithInlineConfigParser(
243 getPath("InputVariableDeclarationUsageDistanceFinal.java"), expected);
244 }
245
246 @Test
247 public void testTokensNotNull() {
248 final VariableDeclarationUsageDistanceCheck check =
249 new VariableDeclarationUsageDistanceCheck();
250 assertWithMessage("Acceptable tokens should not be null")
251 .that(check.getAcceptableTokens())
252 .isNotNull();
253 assertWithMessage("Default tokens should not be null")
254 .that(check.getDefaultTokens())
255 .isNotNull();
256 assertWithMessage("Required tokens should not be null")
257 .that(check.getRequiredTokens())
258 .isNotNull();
259 }
260
261 @Test
262 public void testDefaultConfiguration() throws Exception {
263 final String[] expected = {
264 "83:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 3),
265 "231:9: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 3),
266 "491:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 3),
267 "503:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 3),
268 "516:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 3),
269 "554:13: " + getCheckMessage(MSG_KEY_EXT, "parentId", 4, 3),
270 "855:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
271 "867:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
272 "879:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
273 "891:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
274 "903:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
275 "913:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
276 "945:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
277 "1036:9: " + getCheckMessage(MSG_KEY_EXT, "c", 4, 3),
278 "1001:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
279 "1066:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
280 };
281 verifyWithInlineConfigParser(
282 getPath("InputVariableDeclarationUsageDistanceDefault.java"), expected);
283 }
284
285 @Test
286 public void testDefaultConfiguration2() throws Exception {
287 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
288
289 verifyWithInlineConfigParser(
290 getPath("InputVariableDeclarationUsageDistanceDefault2.java"), expected);
291 }
292
293 @Test
294 public void testAnonymousClass() throws Exception {
295 final String[] expected = {
296 "19:9: " + getCheckMessage(MSG_KEY_EXT, "prefs", 4, 3),
297 };
298
299 verifyWithInlineConfigParser(
300 getPath("InputVariableDeclarationUsageDistanceAnonymous.java"),
301 expected);
302 }
303
304 @Test
305 public void testLabels() throws Exception {
306 final String[] expected = {
307 "15:9: " + getCheckMessage(MSG_KEY_EXT, "eol", 5, 3),
308 };
309 verifyWithInlineConfigParser(
310 getPath("InputVariableDeclarationUsageDistanceLabels.java"), expected);
311 }
312
313 @Test
314 public void testVariableDeclarationUsageDistanceSwitchExpressions() throws Exception {
315
316 final int maxDistance = 1;
317 final String[] expected = {
318 "35:17: " + getCheckMessage(MSG_KEY, "arg", 2, maxDistance),
319 "80:17: " + getCheckMessage(MSG_KEY, "m", 3, maxDistance),
320 "81:17: " + getCheckMessage(MSG_KEY, "n", 2, maxDistance),
321 "113:17: " + getCheckMessage(MSG_KEY, "arg", 2, maxDistance),
322 "155:17: " + getCheckMessage(MSG_KEY, "m", 3, maxDistance),
323 "156:17: " + getCheckMessage(MSG_KEY, "n", 2, maxDistance),
324 "177:17: " + getCheckMessage(MSG_KEY, "count", 3, maxDistance),
325 "197:17: " + getCheckMessage(MSG_KEY, "count", 3, maxDistance),
326 };
327
328 final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions.java";
329 verifyWithInlineConfigParser(getNonCompilablePath(filename), expected);
330 }
331
332 @Test
333 public void testVariableDeclarationUsageDistanceSwitchExpressions2() throws Exception {
334 final int maxDistance = 1;
335 final String[] expected = {
336 "16:9: " + getCheckMessage(MSG_KEY, "i", 2, maxDistance),
337 };
338
339 final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions2.java";
340 verifyWithInlineConfigParser(getNonCompilablePath(filename), expected);
341 }
342
343 @Test
344 public void testGeneralClass3() throws Exception {
345 final String[] expected = {
346 "46:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
347 };
348
349 verifyWithInlineConfigParser(
350 getPath("InputVariableDeclarationUsageDistanceGeneral3.java"), expected);
351 }
352
353 @Test
354 public void testGeneralClass4() throws Exception {
355 final String[] expected = {
356 "26:9: " + getCheckMessage(MSG_KEY, "z", 3, 1),
357 };
358
359 verifyWithInlineConfigParser(
360 getPath("InputVariableDeclarationUsageDistanceGeneral4.java"), expected);
361 }
362
363 @Test
364 public void testVariableDeclarationUsageDistanceTryResources() throws Exception {
365 final String[] expected = {
366 "19:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
367 "20:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
368 };
369
370 verifyWithInlineConfigParser(
371 getPath("InputVariableDeclarationUsageDistanceTryResources.java"), expected);
372 }
373
374 @Test
375 public void testVariableDeclarationUsageDistance4() throws Exception {
376 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
377
378 verifyWithInlineConfigParser(
379 getPath("InputVariableDeclarationUsageDistance3.java"), expected);
380 }
381
382 @Test
383 public void testVariableDeclarationUsageDistanceScope2() throws Exception {
384 final String[] expected = {
385 "16:9: " + getCheckMessage(MSG_KEY, "i", 5, 1),
386 };
387
388 verifyWithInlineConfigParser(
389 getPath("InputVariableDeclarationUsageDistanceScope2.java"), expected);
390 }
391
392 @Test
393 public void testVariableDeclarationUsageDistance1() throws Exception {
394 final String[] expected = {
395 "15:9: " + getCheckMessage(MSG_KEY, "i", 2, 1),
396 };
397
398 verifyWithInlineConfigParser(
399 getPath("InputVariableDeclarationUsageDistance1.java"), expected);
400 }
401
402 @Test
403 public void testVariableDeclarationUsageDistanceCloseToBlock() throws Exception {
404 final String[] expected = {
405 "15:9: " + getCheckMessage(MSG_KEY, "a", 13, 1),
406 "16:9: " + getCheckMessage(MSG_KEY, "b", 13, 1),
407 };
408 verifyWithInlineConfigParser(
409 getPath("InputVariableDeclarationUsageDistanceCloseToBlock.java"), expected);
410 }
411
412 @Test
413 public void testVariableDeclarationUsageDistancePatternVariables() throws Exception {
414 final String[] expected = {
415 "35:9: " + getCheckMessage(MSG_KEY, "b", 5, 1),
416 };
417 verifyWithInlineConfigParser(
418 getNonCompilablePath("InputVariableDeclarationUsageDistancePatternVariables.java"),
419 expected);
420 }
421 }