View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 org.checkstyle.suppressionxpathfilter.coding;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck;
30  
31  public class XpathRegressionFinalLocalVariableTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = FinalLocalVariableCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/finallocalvariable";
43      }
44  
45      @Test
46      public void testMethodDef() throws Exception {
47          final File fileToProcess = new
48                  File(getPath("InputXpathFinalLocalVariableMethodDef.java"));
49  
50          final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
51  
52          final String[] expectedViolation = {
53              "5:13: " + getCheckMessage(FinalLocalVariableCheck.class,
54                      FinalLocalVariableCheck.MSG_KEY, "x"),
55          };
56  
57          final List<String> expectedXpathQueries = List.of(
58                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59                          + "[@text='InputXpathFinalLocalVariableMethodDef']]"
60                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod']]"
61                          + "/SLIST/VARIABLE_DEF/IDENT[@text='x']"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65      }
66  
67      @Test
68      public void testForLoop() throws Exception {
69          final File fileToProcess = new
70                  File(getPath("InputXpathFinalLocalVariableForLoop.java"));
71  
72          final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
73  
74          final String[] expectedViolation = {
75              "6:17: " + getCheckMessage(FinalLocalVariableCheck.class,
76                      FinalLocalVariableCheck.MSG_KEY, "x"),
77          };
78  
79          final List<String> expectedXpathQueries = List.of(
80              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
81                      + "[@text='InputXpathFinalLocalVariableForLoop']]"
82                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method2']]/SLIST/"
83                      + "LITERAL_FOR/SLIST/VARIABLE_DEF/IDENT[@text='x']"
84          );
85  
86          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
87      }
88  
89      @Test
90      public void testSwitchCase() throws Exception {
91          final File fileToProcess = new
92                  File(getPath("InputXpathFinalLocalVariableSwitchCase.java"));
93  
94          final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
95  
96          final String[] expectedViolation = {
97              "8:25: " + getCheckMessage(FinalLocalVariableCheck.class,
98                      FinalLocalVariableCheck.MSG_KEY, "foo"),
99          };
100 
101         final List<String> expectedXpathQueries = List.of(
102             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
103                     + "[@text='InputXpathFinalLocalVariableSwitchCase']]"
104                     + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]"
105                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/"
106                     + "LITERAL_SWITCH/CASE_GROUP/SLIST/VARIABLE_DEF/IDENT[@text='foo']"
107         );
108 
109         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
110     }
111 
112     @Test
113     public void testInnerClass() throws Exception {
114         final File fileToProcess = new
115                 File(getPath("InputXpathFinalLocalVariableInnerClass.java"));
116 
117         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
118 
119         final String[] expectedViolation = {
120             "7:17: " + getCheckMessage(FinalLocalVariableCheck.class,
121                     FinalLocalVariableCheck.MSG_KEY, "shouldBeFinal"),
122         };
123 
124         final List<String> expectedXpathQueries = List.of(
125             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
126                     + "[@text='InputXpathFinalLocalVariableInnerClass']]"
127                     + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]"
128                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test1']]"
129                     + "/SLIST/VARIABLE_DEF/IDENT[@text='shouldBeFinal']"
130         );
131 
132         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
133     }
134 
135     @Test
136     public void testParameterDef() throws Exception {
137         final File fileToProcess = new
138                 File(getPath("InputXpathFinalLocalVariableParameterDef.java"));
139 
140         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
141         moduleConfig.addProperty("tokens", "PARAMETER_DEF");
142 
143         final String[] expectedViolation = {
144             "4:28: " + getCheckMessage(FinalLocalVariableCheck.class,
145                     FinalLocalVariableCheck.MSG_KEY, "aArg"),
146         };
147 
148         final List<String> expectedXpathQueries = List.of(
149             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
150                     + "[@text='InputXpathFinalLocalVariableParameterDef']]"
151                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
152                     + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='aArg']"
153         );
154 
155         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
156     }
157 
158     @Test
159     public void testEnhancedFor() throws Exception {
160         final File fileToProcess = new
161                 File(getPath("InputXpathFinalLocalVariableEnhancedFor.java"));
162 
163         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
164         moduleConfig.addProperty("validateEnhancedForLoopVariable", "true");
165 
166         final String[] expectedViolation = {
167             "8:20: " + getCheckMessage(FinalLocalVariableCheck.class,
168                     FinalLocalVariableCheck.MSG_KEY, "a"),
169         };
170 
171         final List<String> expectedXpathQueries = List.of(
172             "/COMPILATION_UNIT/CLASS_DEF"
173                     + "[./IDENT[@text='InputXpathFinalLocalVariableEnhancedFor']]"
174                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method1']]"
175                     + "/SLIST/LITERAL_FOR/FOR_EACH_CLAUSE/VARIABLE_DEF/IDENT[@text='a']"
176         );
177 
178         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
179     }
180 
181     @Test
182     public void testCtor() throws Exception {
183         final File fileToProcess = new
184                 File(getPath("InputXpathFinalLocalVariableCtor.java"));
185 
186         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
187         moduleConfig.addProperty("tokens", "PARAMETER_DEF");
188 
189         final String[] expectedViolation = {
190             "4:42: " + getCheckMessage(FinalLocalVariableCheck.class,
191                     FinalLocalVariableCheck.MSG_KEY, "a"),
192         };
193 
194         final List<String> expectedXpathQueries = List.of(
195             "/COMPILATION_UNIT/CLASS_DEF"
196                     + "[./IDENT[@text='InputXpathFinalLocalVariableCtor']]"
197                     + "/OBJBLOCK/CTOR_DEF[./IDENT"
198                     + "[@text='InputXpathFinalLocalVariableCtor']]"
199                     + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='a']"
200         );
201 
202         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
203     }
204 
205     @Test
206     public void testTryBlock() throws Exception {
207         final File fileToProcess = new
208                 File(getPath("InputXpathFinalLocalVariableTryBlock.java"));
209 
210         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
211 
212         final String[] expectedViolation = {
213             "6:17: " + getCheckMessage(FinalLocalVariableCheck.class,
214                     FinalLocalVariableCheck.MSG_KEY, "start"),
215         };
216 
217         final List<String> expectedXpathQueries = List.of(
218             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
219                     + "[@text='InputXpathFinalLocalVariableTryBlock']]"
220                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='checkCodeBlock']]"
221                     + "/SLIST/LITERAL_TRY/SLIST/VARIABLE_DEF/IDENT[@text='start']"
222         );
223 
224         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
225     }
226 
227     @Test
228     public void testConditionals() throws Exception {
229         final File fileToProcess = new
230                 File(getPath("InputXpathFinalLocalVariableConditionals.java"));
231 
232         final DefaultConfiguration moduleConfig = createModuleConfig(FinalLocalVariableCheck.class);
233 
234         final String[] expectedViolation = {
235             "11:25: " + getCheckMessage(FinalLocalVariableCheck.class,
236                     FinalLocalVariableCheck.MSG_KEY, "body"),
237         };
238 
239         final List<String> expectedXpathQueries = List.of(
240             "/COMPILATION_UNIT/CLASS_DEF"
241                     + "[./IDENT[@text='InputXpathFinalLocalVariableConditionals']]"
242                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='checkCodeBlock']]/SLIST/LITERAL_TRY"
243                     + "/SLIST/LITERAL_IF/LITERAL_ELSE/LITERAL_IF"
244                     + "/SLIST/VARIABLE_DEF/IDENT[@text='body']"
245         );
246 
247         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
248     }
249 }