View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
34      private final String checkName = UnusedLocalVariableCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      public String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/unusedlocalvariable";
44      }
45  
46      @Test
47      public void testOne() throws Exception {
48          final File fileToProcess = new File(getPath(
49                  "InputXpathUnusedLocalVariableOne.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(UnusedLocalVariableCheck.class);
53  
54          final String[] expectedViolation = {
55              "6:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
56                      UnusedLocalVariableCheck.MSG_UNUSED_NAMED_LOCAL_VARIABLE, "a"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
62                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='a']]",
63                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
64                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
65                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
66                          + "./IDENT[@text='a']]/MODIFIERS",
67                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
68                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
69                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
70                          + "./IDENT[@text='a']]/TYPE",
71                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
72                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
73                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
74                          + "./IDENT[@text='a']]/TYPE/LITERAL_INT"
75          );
76  
77          runVerifications(moduleConfig, fileToProcess, expectedViolation,
78                  expectedXpathQueries);
79      }
80  
81      @Test
82      public void testTwo() throws Exception {
83          final File fileToProcess = new File(getPath(
84                  "InputXpathUnusedLocalVariableTwo.java"));
85  
86          final DefaultConfiguration moduleConfig =
87                  createModuleConfig(UnusedLocalVariableCheck.class);
88          moduleConfig.addProperty("allowUnnamedVariables", "false");
89  
90          final String[] expectedViolation = {
91              "10:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
92                      UnusedLocalVariableCheck.MSG_UNUSED_LOCAL_VARIABLE, "b"),
93          };
94  
95          final List<String> expectedXpathQueries = Arrays.asList(
96                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
97                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
98                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='b']]",
99                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
100                         + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
101                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
102                         + "./IDENT[@text='b']]/MODIFIERS",
103                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
104                         + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
105                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
106                         + "./IDENT[@text='b']]/TYPE", "/COMPILATION_UNIT/CLASS_DEF["
107                         + "./IDENT[@text='InputXpathUnusedLocalVariableTwo']]/"
108                         + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
109                         + "./IDENT[@text='b']]/TYPE/LITERAL_INT"
110         );
111 
112         runVerifications(moduleConfig, fileToProcess, expectedViolation,
113                 expectedXpathQueries);
114 
115     }
116 
117     @Test
118     public void testThree() throws Exception {
119         final File fileToProcess = new File(getPath(
120                 "InputXpathUnusedLocalVariableThree.java"));
121 
122         final DefaultConfiguration moduleConfig =
123             createModuleConfig(UnusedLocalVariableCheck.class);
124 
125         final String[] expectedViolation = {
126             "8:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
127                    UnusedLocalVariableCheck.MSG_UNUSED_NAMED_LOCAL_VARIABLE, "c"),
128         };
129 
130         final List<String> expectedXpathQueries = Arrays.asList(
131                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
132                         + "@text='InputXpathUnusedLocalVariableThree']]/OBJBLOCK/"
133                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_DO/SLIST/"
134                         + "VARIABLE_DEF[./IDENT[@text='c']]",
135                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
136                         + "@text='InputXpathUnusedLocalVariableThree']]/OBJBLOCK/"
137                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_DO/SLIST/"
138                         + "VARIABLE_DEF[./IDENT[@text='c']]/MODIFIERS",
139                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
140                         + "@text='InputXpathUnusedLocalVariableThree']]/OBJBLOCK/"
141                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_DO/SLIST/"
142                         + "VARIABLE_DEF[./IDENT[@text='c']]/TYPE",
143                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
144                         + "@text='InputXpathUnusedLocalVariableThree']]/OBJBLOCK/"
145                         + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/LITERAL_DO/SLIST/"
146                         + "VARIABLE_DEF[./IDENT[@text='c']]/TYPE/LITERAL_INT"
147         );
148 
149         runVerifications(moduleConfig, fileToProcess, expectedViolation,
150                 expectedXpathQueries);
151 
152     }
153 
154 }