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.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck;
31  
32  public class XpathRegressionUnusedLocalVariableTest extends AbstractXpathTestSupport {
33      private final String checkName = UnusedLocalVariableCheck.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/unusedlocalvariable";
43      }
44  
45      @Test
46      public void testOne() throws Exception {
47          final File fileToProcess = new File(getPath(
48                  "InputXpathUnusedLocalVariableOne.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(UnusedLocalVariableCheck.class);
52  
53          final String[] expectedViolation = {
54              "6:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
55                      UnusedLocalVariableCheck.MSG_UNUSED_NAMED_LOCAL_VARIABLE, "a"),
56          };
57  
58          final List<String> expectedXpathQueries = Arrays.asList(
59                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
60                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
61                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='a']]",
62                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
63                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
64                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
65                          + "./IDENT[@text='a']]/MODIFIERS",
66                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
67                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
68                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
69                          + "./IDENT[@text='a']]/TYPE",
70                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
71                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
72                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
73                          + "./IDENT[@text='a']]/TYPE/LITERAL_INT"
74          );
75  
76          runVerifications(moduleConfig, fileToProcess, expectedViolation,
77                  expectedXpathQueries);
78      }
79  
80      @Test
81      public void testTwo() throws Exception {
82          final File fileToProcess = new File(getPath(
83                  "InputXpathUnusedLocalVariableTwo.java"));
84  
85          final DefaultConfiguration moduleConfig =
86                  createModuleConfig(UnusedLocalVariableCheck.class);
87          moduleConfig.addProperty("allowUnnamedVariables", "false");
88  
89          final String[] expectedViolation = {
90              "10:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
91                      UnusedLocalVariableCheck.MSG_UNUSED_LOCAL_VARIABLE, "b"),
92          };
93  
94          final List<String> expectedXpathQueries = Arrays.asList(
95                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
96                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
97                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='b']]",
98                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
99                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
100                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
101                         + "./IDENT[@text='b']]/MODIFIERS",
102                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
103                         + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
104                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
105                         + "./IDENT[@text='b']]/TYPE", "/COMPILATION_UNIT/CLASS_DEF["
106                         + "./IDENT[@text='InputXpathUnusedLocalVariableTwo']]/"
107                         + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
108                         + "./IDENT[@text='b']]/TYPE/LITERAL_INT"
109         );
110 
111         runVerifications(moduleConfig, fileToProcess, expectedViolation,
112                 expectedXpathQueries);
113 
114     }
115 }