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