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.FinalLocalVariableCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.DetailAST;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class FinalLocalVariableCheckTest
34      extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/coding/finallocalvariable";
38      }
39  
40      @Test
41      public void testDefault() throws Exception {
42          final DefaultConfiguration checkConfig =
43              createModuleConfig(FinalLocalVariableCheck.class);
44  
45          final String[] expected = {
46              "9:13: " + getCheckMessage(MSG_KEY, "i"),
47              "9:16: " + getCheckMessage(MSG_KEY, "j"),
48              "10:18: " + getCheckMessage(MSG_KEY, "runnable"),
49              "20:13: " + getCheckMessage(MSG_KEY, "i"),
50              "24:13: " + getCheckMessage(MSG_KEY, "z"),
51              "26:16: " + getCheckMessage(MSG_KEY, "obj"),
52              "30:16: " + getCheckMessage(MSG_KEY, "x"),
53              "36:18: " + getCheckMessage(MSG_KEY, "runnable"),
54              "40:21: " + getCheckMessage(MSG_KEY, "q"),
55              "56:13: " + getCheckMessage(MSG_KEY, "i"),
56              "60:13: " + getCheckMessage(MSG_KEY, "z"),
57              "62:16: " + getCheckMessage(MSG_KEY, "obj"),
58              "66:16: " + getCheckMessage(MSG_KEY, "x"),
59              "74:21: " + getCheckMessage(MSG_KEY, "w"),
60              "75:26: " + getCheckMessage(MSG_KEY, "runnable"),
61              "96:17: " + getCheckMessage(MSG_KEY, "weird"),
62              "97:17: " + getCheckMessage(MSG_KEY, "j"),
63              "98:17: " + getCheckMessage(MSG_KEY, "k"),
64              "185:13: " + getCheckMessage(MSG_KEY, "x"),
65              "190:17: " + getCheckMessage(MSG_KEY, "x"),
66              "210:17: " + getCheckMessage(MSG_KEY, "n"),
67              "216:13: " + getCheckMessage(MSG_KEY, "q"),
68              "217:13: " + getCheckMessage(MSG_KEY, "w"),
69              "226:21: " + getCheckMessage(MSG_KEY, "w"),
70              "227:21: " + getCheckMessage(MSG_KEY, "e"),
71              "247:17: " + getCheckMessage(MSG_KEY, "n"),
72              "259:17: " + getCheckMessage(MSG_KEY, "t"),
73              "269:21: " + getCheckMessage(MSG_KEY, "foo"),
74              "288:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
75              "300:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
76              "344:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
77              "357:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
78              "360:21: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
79              "375:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
80              "386:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
81              "418:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
82              "421:21: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
83          };
84          verify(checkConfig, getPath("InputFinalLocalVariable.java"), expected);
85      }
86  
87      @Test
88      public void testParameter() throws Exception {
89          final DefaultConfiguration checkConfig =
90              createModuleConfig(FinalLocalVariableCheck.class);
91          checkConfig.addAttribute("tokens", "PARAMETER_DEF");
92  
93          final String[] expected = {
94              "45:28: " + getCheckMessage(MSG_KEY, "aArg"),
95              "149:36: " + getCheckMessage(MSG_KEY, "_o"),
96              "154:37: " + getCheckMessage(MSG_KEY, "_o1"),
97          };
98          verify(checkConfig, getPath("InputFinalLocalVariable.java"), expected);
99      }
100 
101     @Test
102     public void testNativeMethods() throws Exception {
103         final DefaultConfiguration checkConfig =
104             createModuleConfig(FinalLocalVariableCheck.class);
105         checkConfig.addAttribute("tokens", "PARAMETER_DEF");
106 
107         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
108         verify(checkConfig, getPath("InputFinalLocalVariableNativeMethods.java"), expected);
109     }
110 
111     @Test
112     public void testFalsePositive() throws Exception {
113         final DefaultConfiguration checkConfig =
114             createModuleConfig(FinalLocalVariableCheck.class);
115         checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");
116 
117         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
118         verify(checkConfig, getPath("InputFinalLocalVariableFalsePositive.java"), expected);
119     }
120 
121     @Test
122     public void testEnhancedForLoopVariableTrue() throws Exception {
123         final DefaultConfiguration checkConfig =
124                 createModuleConfig(FinalLocalVariableCheck.class);
125         checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");
126         checkConfig.addAttribute("validateEnhancedForLoopVariable", "true");
127         final String[] expected = {
128             "8:20: " + getCheckMessage(MSG_KEY, "a"),
129             "15:13: " + getCheckMessage(MSG_KEY, "x"),
130         };
131         verify(checkConfig, getPath("InputFinalLocalVariableEnhancedForLoopVariable.java"),
132             expected);
133     }
134 
135     @Test
136     public void testEnhancedForLoopVariableFalse() throws Exception {
137         final DefaultConfiguration checkConfig =
138                 createModuleConfig(FinalLocalVariableCheck.class);
139         checkConfig.addAttribute("tokens", "VARIABLE_DEF, PARAMETER_DEF");
140         final String[] expected = {
141             "15:13: " + getCheckMessage(MSG_KEY, "x"),
142         };
143         verify(checkConfig, getPath("InputFinalLocalVariableEnhancedForLoopVariable.java"),
144             expected);
145     }
146 
147     @Test
148     public void testLambda()
149             throws Exception {
150         final DefaultConfiguration checkConfig =
151             createModuleConfig(FinalLocalVariableCheck.class);
152         checkConfig.addAttribute("tokens", "PARAMETER_DEF,VARIABLE_DEF");
153         final String[] expected = {
154             "32:16: " + getCheckMessage(MSG_KEY, "result"),
155         };
156         verify(checkConfig, getPath("InputFinalLocalVariableNameLambda.java"),
157             expected);
158     }
159 
160     @Test
161     public void testVariableNameShadowing()
162             throws Exception {
163         final DefaultConfiguration checkConfig =
164             createModuleConfig(FinalLocalVariableCheck.class);
165         checkConfig.addAttribute("tokens", "PARAMETER_DEF,VARIABLE_DEF");
166 
167         final String[] expected = {
168             "4:28: " + getCheckMessage(MSG_KEY, "text"),
169             "17:13: " + getCheckMessage(MSG_KEY, "x"),
170         };
171         verify(checkConfig, getPath("InputFinalLocalVariableNameShadowing.java"), expected);
172     }
173 
174     @Test
175     public void testImproperToken() {
176         final FinalLocalVariableCheck check = new FinalLocalVariableCheck();
177 
178         final DetailAST lambdaAst = new DetailAST();
179         lambdaAst.setType(TokenTypes.LAMBDA);
180 
181         try {
182             check.visitToken(lambdaAst);
183             Assert.fail("IllegalStateException is expected");
184         }
185         catch (IllegalStateException ex) {
186             // it is OK
187         }
188     }
189 
190     @Test
191     public void testVariableWhichIsAssignedMultipleTimes() throws Exception {
192         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
193 
194         final String[] expected = {
195             "49:13: " + getCheckMessage(MSG_KEY, "i"),
196             "122:16: " + getCheckMessage(MSG_KEY, "path"),
197             "126:20: " + getCheckMessage(MSG_KEY, "relativePath"),
198             "202:17: " + getCheckMessage(MSG_KEY, "kind"),
199             "207:24: " + getCheckMessage(MSG_KEY, "m"),
200             "409:17: " + getCheckMessage(MSG_KEY, "increment"),
201         };
202         verify(checkConfig, getPath("InputFinalLocalVariableAssignedMultipleTimes.java"), expected);
203     }
204 
205     @Test
206     public void testVariableIsAssignedInsideAndOutsideSwitchBlock() throws Exception {
207         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
208         final String[] expected = {
209             "31:13: " + getCheckMessage(MSG_KEY, "b"),
210         };
211         verify(checkConfig, getPath("InputFinalLocalVariableAssignedInsideAndOutsideSwitch.java"),
212             expected);
213     }
214 
215     @Test
216     public void testFinalLocalVariableFalsePositives() throws Exception {
217         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
218         final String[] expected = {
219             "344:16: " + getCheckMessage(MSG_KEY, "c2"),
220             "2187:16: " + getCheckMessage(MSG_KEY, "b"),
221         };
222         verify(checkConfig, getPath("InputFinalLocalVariableFalsePositives.java"), expected);
223     }
224 
225     @Test
226     public void testMultipleAndNestedConditions() throws Exception {
227         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
228         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
229         verify(checkConfig, getPath("InputFinalLocalVariableMultipleAndNestedConditions.java"),
230             expected);
231     }
232 
233     @Test
234     public void testMultiTypeCatch() throws Exception {
235         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
236         checkConfig.addAttribute("tokens", "PARAMETER_DEF,VARIABLE_DEF");
237         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
238         verify(checkConfig, getPath("InputFinalLocalVariableMultiCatch.java"),
239                 expected);
240     }
241 
242     @Test
243     public void testLeavingSlistToken() throws Exception {
244         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
245         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
246         verify(checkConfig, getPath("InputFinalLocalVariableLeavingSlistToken.java"), expected);
247     }
248 
249     @Test
250     public void testBreakOrReturn() throws Exception {
251         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
252         final String[] expected = {
253             "7:19: " + getCheckMessage(MSG_KEY, "e"),
254         };
255         verify(checkConfig, getPath("InputFinalLocalVariableBreak.java"), expected);
256     }
257 
258     @Test
259     public void testAnonymousClass() throws Exception {
260         final DefaultConfiguration checkConfig = createModuleConfig(FinalLocalVariableCheck.class);
261         final String[] expected = {
262             "5:16: " + getCheckMessage(MSG_KEY, "testSupport"),
263         };
264         verify(checkConfig, getPath("InputFinalLocalVariableAnonymousClass.java"), expected);
265     }
266 }