View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY_EXT;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class VariableDeclarationUsageDistanceCheckTest extends
33          AbstractModuleTestSupport {
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 DefaultConfiguration checkConfig =
42              createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
43          checkConfig.addAttribute("allowedDistance", "1");
44          checkConfig.addAttribute("ignoreVariablePattern", "");
45          checkConfig.addAttribute("validateBetweenScopes", "true");
46          checkConfig.addAttribute("ignoreFinal", "false");
47          final String[] expected = {
48              "30: " + getCheckMessage(MSG_KEY, "a", 2, 1),
49              "38: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
50              "44: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
51              "57: " + getCheckMessage(MSG_KEY, "count", 2, 1),
52              "71: " + getCheckMessage(MSG_KEY, "count", 4, 1),
53              "96: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
54              "144: " + getCheckMessage(MSG_KEY, "m", 3, 1),
55              "145: " + getCheckMessage(MSG_KEY, "n", 2, 1),
56              "184: " + getCheckMessage(MSG_KEY, "result", 2, 1),
57              "219: " + getCheckMessage(MSG_KEY, "t", 5, 1),
58              "222: " + getCheckMessage(MSG_KEY, "c", 3, 1),
59              "223: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
60              "260: " + getCheckMessage(MSG_KEY, "selected", 2, 1),
61              "261: " + getCheckMessage(MSG_KEY, "model", 2, 1),
62              "287: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
63              "300: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
64              "343: " + getCheckMessage(MSG_KEY, "green", 2, 1),
65              "344: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
66              "367: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
67              "454: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
68              "455: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
69              "471: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
70              "471: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
71              "479: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
72              "491: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
73              "504: " + getCheckMessage(MSG_KEY, "count", 4, 1),
74              "505: " + getCheckMessage(MSG_KEY, "files", 2, 1),
75              "540: " + getCheckMessage(MSG_KEY, "id", 2, 1),
76              "542: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
77              "891: " + getCheckMessage(MSG_KEY, "a", 4, 1),
78              "901: " + getCheckMessage(MSG_KEY, "a", 4, 1),
79              "967: " + getCheckMessage(MSG_KEY, "a", 4, 1),
80              "978: " + getCheckMessage(MSG_KEY, "a", 2, 1),
81          };
82          verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
83      }
84  
85      @Test
86      public void testDistance() throws Exception {
87          final DefaultConfiguration checkConfig =
88              createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
89          checkConfig.addAttribute("allowedDistance", "3");
90          checkConfig.addAttribute("ignoreVariablePattern", "");
91          checkConfig.addAttribute("validateBetweenScopes", "true");
92          checkConfig.addAttribute("ignoreFinal", "false");
93          final String[] expected = {
94              "71: " + getCheckMessage(MSG_KEY, "count", 4, 3),
95              "219: " + getCheckMessage(MSG_KEY, "t", 5, 3),
96              "479: " + getCheckMessage(MSG_KEY, "myOption", 7, 3),
97              "491: " + getCheckMessage(MSG_KEY, "myOption", 6, 3),
98              "504: " + getCheckMessage(MSG_KEY, "count", 4, 3),
99              "891: " + getCheckMessage(MSG_KEY, "a", 4, 3),
100             "901: " + getCheckMessage(MSG_KEY, "a", 4, 3),
101             "967: " + getCheckMessage(MSG_KEY, "a", 4, 3),
102         };
103         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
104     }
105 
106     @Test
107     public void testVariableRegExp() throws Exception {
108         final DefaultConfiguration checkConfig =
109             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
110         checkConfig.addAttribute("allowedDistance", "1");
111         checkConfig.addAttribute("ignoreVariablePattern",
112                 "a|b|c|d|block|dist|t|m");
113         checkConfig.addAttribute("validateBetweenScopes", "true");
114         checkConfig.addAttribute("ignoreFinal", "false");
115         final String[] expected = {
116             "38: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
117             "44: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
118             "57: " + getCheckMessage(MSG_KEY, "count", 2, 1),
119             "71: " + getCheckMessage(MSG_KEY, "count", 4, 1),
120             "96: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
121             "145: " + getCheckMessage(MSG_KEY, "n", 2, 1),
122             "184: " + getCheckMessage(MSG_KEY, "result", 2, 1),
123             "223: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
124             "260: " + getCheckMessage(MSG_KEY, "selected", 2, 1),
125             "261: " + getCheckMessage(MSG_KEY, "model", 2, 1),
126             "287: " + getCheckMessage(MSG_KEY, "sw", 2, 1),
127             "300: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
128             "343: " + getCheckMessage(MSG_KEY, "green", 2, 1),
129             "344: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
130             "367: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
131             "454: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
132             "455: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
133             "471: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
134             "471: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
135             "479: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
136             "491: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
137             "504: " + getCheckMessage(MSG_KEY, "count", 4, 1),
138             "505: " + getCheckMessage(MSG_KEY, "files", 2, 1),
139             "540: " + getCheckMessage(MSG_KEY, "id", 2, 1),
140             "542: " + getCheckMessage(MSG_KEY, "parentId", 3, 1),
141         };
142         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
143     }
144 
145     @Test
146     public void testValidateBetweenScopesOption() throws Exception {
147         final DefaultConfiguration checkConfig =
148             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
149         checkConfig.addAttribute("allowedDistance", "1");
150         checkConfig.addAttribute("ignoreVariablePattern", "");
151         checkConfig.addAttribute("validateBetweenScopes", "false");
152         checkConfig.addAttribute("ignoreFinal", "false");
153         final String[] expected = {
154             "30: " + getCheckMessage(MSG_KEY, "a", 2, 1),
155             "38: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
156             "44: " + getCheckMessage(MSG_KEY, "temp", 2, 1),
157             "71: " + getCheckMessage(MSG_KEY, "count", 4, 1),
158             "96: " + getCheckMessage(MSG_KEY, "arg", 2, 1),
159             "219: " + getCheckMessage(MSG_KEY, "t", 5, 1),
160             "222: " + getCheckMessage(MSG_KEY, "c", 3, 1),
161             "223: " + getCheckMessage(MSG_KEY, "d2", 3, 1),
162             "300: " + getCheckMessage(MSG_KEY, "wh", 2, 1),
163             "343: " + getCheckMessage(MSG_KEY, "green", 2, 1),
164             "344: " + getCheckMessage(MSG_KEY, "blue", 3, 1),
165             "367: " + getCheckMessage(MSG_KEY, "intervalMs", 2, 1),
166             "454: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1),
167             "455: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1),
168             "471: " + getCheckMessage(MSG_KEY, "l1", 3, 1),
169             "471: " + getCheckMessage(MSG_KEY, "l2", 2, 1),
170             "479: " + getCheckMessage(MSG_KEY, "myOption", 7, 1),
171             "491: " + getCheckMessage(MSG_KEY, "myOption", 6, 1),
172             "505: " + getCheckMessage(MSG_KEY, "files", 2, 1),
173             "540: " + getCheckMessage(MSG_KEY, "id", 2, 1),
174             "542: " + getCheckMessage(MSG_KEY, "parentId", 4, 1),
175             "978: " + getCheckMessage(MSG_KEY, "a", 2, 1),
176         };
177         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
178     }
179 
180     @Test
181     public void testIgnoreFinalOption() throws Exception {
182         final DefaultConfiguration checkConfig =
183             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
184         checkConfig.addAttribute("allowedDistance", "1");
185         checkConfig.addAttribute("ignoreVariablePattern", "");
186         checkConfig.addAttribute("validateBetweenScopes", "true");
187         checkConfig.addAttribute("ignoreFinal", "true");
188         final String[] expected = {
189             "30: " + getCheckMessage(MSG_KEY_EXT, "a", 2, 1),
190             "38: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
191             "44: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1),
192             "57: " + getCheckMessage(MSG_KEY_EXT, "count", 2, 1),
193             "71: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
194             "96: " + getCheckMessage(MSG_KEY_EXT, "arg", 2, 1),
195             "144: " + getCheckMessage(MSG_KEY_EXT, "m", 3, 1),
196             "145: " + getCheckMessage(MSG_KEY_EXT, "n", 2, 1),
197             "184: " + getCheckMessage(MSG_KEY_EXT, "result", 2, 1),
198             "219: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 1),
199             "222: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1),
200             "223: " + getCheckMessage(MSG_KEY_EXT, "d2", 3, 1),
201             "260: " + getCheckMessage(MSG_KEY_EXT, "selected", 2, 1),
202             "261: " + getCheckMessage(MSG_KEY_EXT, "model", 2, 1),
203             "287: " + getCheckMessage(MSG_KEY_EXT, "sw", 2, 1),
204             "300: " + getCheckMessage(MSG_KEY_EXT, "wh", 2, 1),
205             "343: " + getCheckMessage(MSG_KEY_EXT, "green", 2, 1),
206             "344: " + getCheckMessage(MSG_KEY_EXT, "blue", 3, 1),
207             "454: " + getCheckMessage(MSG_KEY_EXT, "aOpt", 3, 1),
208             "455: " + getCheckMessage(MSG_KEY_EXT, "bOpt", 2, 1),
209             "471: " + getCheckMessage(MSG_KEY_EXT, "l1", 3, 1),
210             "471: " + getCheckMessage(MSG_KEY_EXT, "l2", 2, 1),
211             "479: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 1),
212             "491: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 1),
213             "504: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1),
214             "505: " + getCheckMessage(MSG_KEY_EXT, "files", 2, 1),
215             "540: " + getCheckMessage(MSG_KEY_EXT, "id", 2, 1),
216             "542: " + getCheckMessage(MSG_KEY_EXT, "parentId", 3, 1),
217             "891: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
218             "901: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
219             "967: " + getCheckMessage(MSG_KEY_EXT, "a", 4, 1),
220         };
221         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
222     }
223 
224     @Test
225     public void testTokensNotNull() {
226         final VariableDeclarationUsageDistanceCheck check =
227             new VariableDeclarationUsageDistanceCheck();
228         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
229         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
230         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
231     }
232 
233     @Test
234     public void testDefaultConfiguration() throws Exception {
235         final DefaultConfiguration checkConfig =
236             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
237         final String[] expected = {
238             "71: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 3),
239             "219: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 3),
240             "479: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 3),
241             "491: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 3),
242             "542: " + getCheckMessage(MSG_KEY_EXT, "parentId", 4, 3),
243         };
244 
245         verify(checkConfig, getPath("InputVariableDeclarationUsageDistance.java"), expected);
246     }
247 
248     @Test
249     public void testAnonymousClass() throws Exception {
250         final DefaultConfiguration checkConfig =
251             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
252         final String[] expected = {
253             "9: " + getCheckMessage(MSG_KEY_EXT, "prefs", 4, 3),
254         };
255 
256         verify(checkConfig, getPath("InputVariableDeclarationUsageDistanceAnonymous.java"),
257                 expected);
258     }
259 
260     @Test
261     public void testLabels() throws Exception {
262         final DefaultConfiguration checkConfig =
263             createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
264         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
265 
266         verify(checkConfig, getPath("InputVariableDeclarationUsageDistanceLabels.java"), expected);
267     }
268 }