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.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      public 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              "58:9: " + getCheckMessage(MSG_KEY, "b", 3, 1),
89          };
90          verifyWithInlineConfigParser(
91                  getPath("InputVariableDeclarationUsageDistanceGeneral2.java"), expected);
92      }
93  
94      @Test
95      public void testIfStatements() throws Exception {
96          final String[] expected = {
97              "18:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
98              "28:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
99              "32:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
100             "38:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
101             "49:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
102             "50:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
103             "51:9: " + getCheckMessage(MSG_KEY, "d", 4, 1),
104             "63:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
105 
106         };
107         verifyWithInlineConfigParser(
108             getPath("InputVariableDeclarationUsageDistanceIfStatements.java"), expected);
109     }
110 
111     @Test
112     public void testDistance() throws Exception {
113         final String[] expected = {
114             "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 3),
115             "231:9: " + getCheckMessage(MSG_KEY, "t", 5, 3),
116             "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 3),
117             "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 3),
118             "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 3),
119             "903:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
120             "913:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
121             "979:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
122             "1066:9: " + getCheckMessage(MSG_KEY, "a", 4, 3),
123         };
124         verifyWithInlineConfigParser(
125                 getPath("InputVariableDeclarationUsageDistance.java"), expected);
126     }
127 
128     @Test
129     public void testVariableRegExp() throws Exception {
130         final String[] expected = {
131             "50:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
132             "56:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
133             "69:9: " + getCheckMessage(MSG_KEY, "count", 2, 1),
134             "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
135             "108:9: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
136             "157:9: " + getCheckMessage(MSG_KEY, "n", 2, 1),
137             "196:9: " + getCheckMessage(MSG_KEY, "result", 2, 1),
138             "235:9: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
139             "272:9: " + getCheckMessage(MSG_KEY, "sel", 2, 1),
140             "273:9: " + getCheckMessage(MSG_KEY, "model", 2, 1),
141             "299:9: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
142             "312:9: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
143             "355:9: " + getCheckMessage(MSG_KEY, "green", 2, 1),
144             "356:9: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
145             "379:9: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
146             "466:9: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
147             "467:9: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
148             "483:9: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
149             "483:9: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
150             "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
151             "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
152             "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
153             "517:9: " + getCheckMessage(MSG_KEY, "files", 2, 1),
154             "552:13: " + getCheckMessage(MSG_KEY, "id", 2, 1),
155             "554:13: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
156         };
157         verifyWithInlineConfigParser(
158                 getPath("InputVariableDeclarationUsageDistanceRegExp.java"), expected);
159     }
160 
161     @Test
162     public void testValidateBetweenScopesOption() throws Exception {
163         final String[] expected = {
164             "42:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
165             "50:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
166             "56:9: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
167             "83:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
168             "108:9: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
169             "231:9: " + getCheckMessage(MSG_KEY, "t", 5, 1),
170             "234:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
171             "235:9: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
172             "272:9: " + getCheckMessage(MSG_KEY, "sel", 2, 1),
173             "273:9: " + getCheckMessage(MSG_KEY, "model", 2, 1),
174             "312:9: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
175             "355:9: " + getCheckMessage(MSG_KEY, "green", 2, 1),
176             "356:9: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
177             "379:9: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
178             "466:9: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
179             "467:9: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
180             "483:9: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
181             "483:9: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
182             "491:9: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
183             "503:9: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
184             "516:9: " + getCheckMessage(MSG_KEY, "count", 4, 1),
185             "517:9: " + getCheckMessage(MSG_KEY, "files", 2, 1),
186             "552:13: " + getCheckMessage(MSG_KEY, "id", 2, 1),
187             "554:13: " + getCheckMessage(MSG_KEY, "parentId", 4, 1),
188             "855:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
189             "867:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
190             "879:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
191             "891:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
192             "903:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
193             "913:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
194             "924:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
195             "945:9: " + getCheckMessage(MSG_KEY, "a", 5, 1),
196             "979:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
197             "990:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
198             "1001:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
199             "1036:9: " + getCheckMessage(MSG_KEY, "c", 4, 1),
200             "1066:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
201         };
202         verifyWithInlineConfigParser(
203                 getPath("InputVariableDeclarationUsageDistanceScopes.java"), expected);
204     }
205 
206     @Test
207     public void testIgnoreFinalOption() throws Exception {
208         final String[] expected = {
209             "42:9: " + getCheckMessage(MSG_KEY_EXT, "a", 2, 1),
210             "50:9: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
211             "56:9: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
212             "69:9: " + getCheckMessage(MSG_KEY_EXT, "count", 2, 1),
213             "83:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
214             "108:9: " + getCheckMessage(MSG_KEY_EXT, "arg", 2, 1),
215             "156:9: " + getCheckMessage(MSG_KEY_EXT, "m", 3, 1),
216             "157:9: " + getCheckMessage(MSG_KEY_EXT, "n", 2, 1),
217             "196:9: " + getCheckMessage(MSG_KEY_EXT, "result", 2, 1),
218             "231:9: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 1),
219             "234:9: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
220             "235:9: " + getCheckMessage(MSG_KEY_EXT, "d2", 3, 1),
221             "272:9: " + getCheckMessage(MSG_KEY_EXT, "sel", 2, 1),
222             "273:9: " + getCheckMessage(MSG_KEY_EXT, "model", 2, 1),
223             "299:9: " + getCheckMessage(MSG_KEY_EXT, "sw", 2, 1),
224             "312:9: " + getCheckMessage(MSG_KEY_EXT, "wh", 2, 1),
225             "355:9: " + getCheckMessage(MSG_KEY_EXT, "green", 2, 1),
226             "356:9: " + getCheckMessage(MSG_KEY_EXT, "blue", 3, 1),
227             "466:9: " + getCheckMessage(MSG_KEY_EXT, "aOpt", 3, 1),
228             "467:9: " + getCheckMessage(MSG_KEY_EXT, "bOpt", 2, 1),
229             "483:9: " + getCheckMessage(MSG_KEY_EXT, "l1", 3, 1),
230             "483:9: " + getCheckMessage(MSG_KEY_EXT, "l2", 2, 1),
231             "491:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 1),
232             "503:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 1),
233             "516:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
234             "517:9: " + getCheckMessage(MSG_KEY_EXT, "files", 2, 1),
235             "552:13: " + getCheckMessage(MSG_KEY_EXT, "id", 2, 1),
236             "554:13: " + getCheckMessage(MSG_KEY_EXT, "parentId", 3, 1),
237             "903:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
238             "913:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
239             "979:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
240             "1001:9: " + getCheckMessage(MSG_KEY_EXT, "a", 3, 1),
241             "1036:9: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
242             "1066:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
243         };
244         verifyWithInlineConfigParser(
245                 getPath("InputVariableDeclarationUsageDistanceFinal.java"), expected);
246     }
247 
248     @Test
249     public void testTokensNotNull() {
250         final VariableDeclarationUsageDistanceCheck check =
251             new VariableDeclarationUsageDistanceCheck();
252         assertWithMessage("Acceptable tokens should not be null")
253             .that(check.getAcceptableTokens())
254             .isNotNull();
255         assertWithMessage("Default tokens should not be null")
256             .that(check.getDefaultTokens())
257             .isNotNull();
258         assertWithMessage("Required tokens should not be null")
259             .that(check.getRequiredTokens())
260             .isNotNull();
261     }
262 
263     @Test
264     public void testDefaultConfiguration() throws Exception {
265         final String[] expected = {
266             "83:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 3),
267             "231:9: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 3),
268             "491:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 3),
269             "503:9: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 3),
270             "516:9: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 3),
271             "554:13: " + getCheckMessage(MSG_KEY_EXT, "parentId", 4, 3),
272             "855:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
273             "867:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
274             "879:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
275             "891:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
276             "903:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
277             "913:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
278             "945:9: " + getCheckMessage(MSG_KEY_EXT, "a", 5, 3),
279             "1036:9: " + getCheckMessage(MSG_KEY_EXT, "c", 4, 3),
280             "979:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
281             "1001:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
282             "1066:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
283         };
284         verifyWithInlineConfigParser(
285                 getPath("InputVariableDeclarationUsageDistanceDefault.java"), expected);
286     }
287 
288     @Test
289     public void testDefaultConfiguration2() throws Exception {
290         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
291 
292         verifyWithInlineConfigParser(
293             getPath("InputVariableDeclarationUsageDistanceDefault2.java"), expected);
294     }
295 
296     @Test
297     public void testAnonymousClass() throws Exception {
298         final String[] expected = {
299             "19:9: " + getCheckMessage(MSG_KEY_EXT, "prefs", 4, 3),
300         };
301 
302         verifyWithInlineConfigParser(
303                 getPath("InputVariableDeclarationUsageDistanceAnonymous.java"),
304                 expected);
305     }
306 
307     @Test
308     public void testLabels() throws Exception {
309         final String[] expected = {
310             "15:9: " + getCheckMessage(MSG_KEY_EXT, "eol", 5, 3),
311         };
312         verifyWithInlineConfigParser(
313                 getPath("InputVariableDeclarationUsageDistanceLabels.java"), expected);
314     }
315 
316     @Test
317     public void testVariableDeclarationUsageDistanceSwitchExpressions() throws Exception {
318 
319         final int maxDistance = 1;
320         final String[] expected = {
321             "33:17: " + getCheckMessage(MSG_KEY, "arg", 2, maxDistance),
322             "50:17: " + getCheckMessage(MSG_KEY, "m", 3, maxDistance),
323             "51:17: " + getCheckMessage(MSG_KEY, "n", 2, maxDistance),
324             "83:17: " + getCheckMessage(MSG_KEY, "arg", 2, maxDistance),
325             "100:17: " + getCheckMessage(MSG_KEY, "m", 3, maxDistance),
326             "101:17: " + getCheckMessage(MSG_KEY, "n", 2, maxDistance),
327         };
328 
329         final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions.java";
330         verifyWithInlineConfigParser(getPath(filename), expected);
331     }
332 
333     @Test
334     public void testVariableDeclarationUsageDistanceSwitchExpressions2() throws Exception {
335         final int maxDistance = 1;
336         final String[] expected = {
337             "16:9: " + getCheckMessage(MSG_KEY, "i", 2, maxDistance),
338         };
339 
340         final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions2.java";
341         verifyWithInlineConfigParser(getPath(filename), expected);
342     }
343 
344     @Test
345     public void testGeneralClass3() throws Exception {
346         final String[] expected = {
347             "46:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
348             "92:9: " + getCheckMessage(MSG_KEY, "t", 3, 1),
349         };
350 
351         verifyWithInlineConfigParser(
352                 getPath("InputVariableDeclarationUsageDistanceGeneral3.java"), expected);
353     }
354 
355     @Test
356     public void testGeneralClass4() throws Exception {
357         final String[] expected = {
358             "25:9: " + getCheckMessage(MSG_KEY, "z", 2, 1),
359         };
360 
361         verifyWithInlineConfigParser(
362                 getPath("InputVariableDeclarationUsageDistanceGeneral4.java"), expected);
363     }
364 
365     @Test
366     public void testVariableDeclarationUsageDistanceTryResources() throws Exception {
367         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
368 
369         verifyWithInlineConfigParser(
370                 getPath("InputVariableDeclarationUsageDistanceTryResources.java"), expected);
371     }
372 
373     @Test
374     public void testVariableDeclarationUsageDistanceTryBlock() throws Exception {
375         final String[] expected = {
376             "16:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
377             "28:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
378             "40:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
379             "76:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
380             "88:9: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 3),
381         };
382 
383         verifyWithInlineConfigParser(
384                 getPath("InputVariableDeclarationUsageDistanceTryBlock.java"), expected);
385     }
386 
387     @Test
388     public void testVariableDeclarationUsageDistance4() throws Exception {
389         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
390 
391         verifyWithInlineConfigParser(
392                 getPath("InputVariableDeclarationUsageDistance3.java"), expected);
393     }
394 
395     @Test
396     public void testVariableDeclarationUsageDistanceScope2() throws Exception {
397         final String[] expected = {
398             "16:9: " + getCheckMessage(MSG_KEY, "i", 5, 1),
399         };
400 
401         verifyWithInlineConfigParser(
402                 getPath("InputVariableDeclarationUsageDistanceScope2.java"), expected);
403     }
404 
405     @Test
406     public void testVariableDeclarationUsageDistance1() throws Exception {
407         final String[] expected = {
408             "15:9: " + getCheckMessage(MSG_KEY, "i", 2, 1),
409         };
410 
411         verifyWithInlineConfigParser(
412                 getPath("InputVariableDeclarationUsageDistance1.java"), expected);
413     }
414 
415     @Test
416     public void testVariableDeclarationUsageDistanceCloseToBlock() throws Exception {
417         final String[] expected = {
418             "15:9: " + getCheckMessage(MSG_KEY, "a", 13, 1),
419             "16:9: " + getCheckMessage(MSG_KEY, "b", 13, 1),
420         };
421         verifyWithInlineConfigParser(
422                 getPath("InputVariableDeclarationUsageDistanceCloseToBlock.java"), expected);
423     }
424 
425     @Test
426     public void testVariableDeclarationUsageDistancePatternVariables() throws Exception {
427         final String[] expected = {
428             "35:9: " + getCheckMessage(MSG_KEY, "b", 5, 1),
429         };
430         verifyWithInlineConfigParser(
431                 getNonCompilablePath("InputVariableDeclarationUsageDistancePatternVariables.java"),
432                 expected);
433     }
434 
435     @Test
436     public void testVariableDeclarationUsageDistanceMethodDef() throws Exception {
437         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
438         verifyWithInlineConfigParser(
439                 getPath("InputVariableDeclarationUsageDistanceMethodDef.java"), expected);
440     }
441 
442     @Test
443     public void testInitializationSequence1() throws Exception {
444         final String[] expected = {
445             "25:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
446             "40:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
447             "49:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
448             "55:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
449             "69:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
450             "83:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
451             "91:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
452             "97:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
453         };
454 
455         verifyWithInlineConfigParser(
456             getPath(
457                 "InputVariableDeclarationUsageDistanceInitializationSequence1.java"),
458             expected);
459     }
460 
461     @Test
462     public void testInitializationSequence2() throws Exception {
463         final String[] expected = {
464             "25:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
465             "33:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
466             "40:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
467             "53:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
468             "61:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
469             "68:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
470             "82:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
471             "90:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
472             "94:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
473             "101:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
474             "105:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
475         };
476 
477         verifyWithInlineConfigParser(
478             getPath(
479                 "InputVariableDeclarationUsageDistanceInitializationSequence2.java"),
480             expected);
481     }
482 
483     @Test
484     public void testInitializationSequence3() throws Exception {
485         final String[] expected = {
486             "24:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
487             "38:9: " + getCheckMessage(MSG_KEY, "list2", 3, 1),
488             "51:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
489             "70:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
490         };
491 
492         verifyWithInlineConfigParser(
493             getPath(
494                 "InputVariableDeclarationUsageDistanceInitializationSequence3.java"),
495             expected);
496     }
497 
498     @Test
499     public void testInitializationSequence4() throws Exception {
500         final String[] expected = {
501             "25:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
502             "40:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
503             "57:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
504             "67:9: " + getCheckMessage(MSG_KEY, "list", 2, 1),
505             "74:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
506             "93:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
507         };
508 
509         verifyWithInlineConfigParser(
510             getPath(
511                 "InputVariableDeclarationUsageDistanceInitializationSequence4.java"),
512             expected);
513     }
514 
515     @Test
516     public void testInitializationSequence5() throws Exception {
517         final String[] expected = {
518             "27:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
519             "44:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
520             "60:9: " + getCheckMessage(MSG_KEY, "list2", 3, 1),
521             "80:9: " + getCheckMessage(MSG_KEY, "list2", 2, 1),
522         };
523 
524         verifyWithInlineConfigParser(
525             getPath(
526                 "InputVariableDeclarationUsageDistanceInitializationSequence5.java"),
527             expected);
528     }
529 
530     @Test
531     public void testFalsePositive1() throws Exception {
532         final String[] expected = {
533             "33:17: " + getCheckMessage(MSG_KEY, "localMap", 5, 3),
534             "35:17: " + getCheckMessage(MSG_KEY, "nl", 5, 3),
535         };
536 
537         verifyWithInlineConfigParser(
538             getPath(
539                 "InputVariableDeclarationUsageDistanceInitializationFalsePositive1.java"),
540             expected);
541     }
542 
543     @Test
544     public void testReturn() throws Exception {
545         final String[] expected = {
546             "47:9: " + getCheckMessage(MSG_KEY, "function", 5, 3),
547         };
548 
549         verifyWithInlineConfigParser(
550             getPath(
551                 "InputVariableDeclarationUsageDistanceInitializationReturn.java"),
552             expected);
553     }
554 
555 }