View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck;
30  
31  public class XpathRegressionUnusedLocalVariableTest extends AbstractXpathTestSupport {
32      private final String checkName = UnusedLocalVariableCheck.class.getSimpleName();
33  
34      @Override
35      protected String getCheckName() {
36          return checkName;
37      }
38  
39      @Test
40      public void testOne() throws Exception {
41          final File fileToProcess = new File(getPath(
42                  "InputXpathUnusedLocalVariableOne.java"));
43  
44          final DefaultConfiguration moduleConfig =
45                  createModuleConfig(UnusedLocalVariableCheck.class);
46  
47          final String[] expectedViolation = {
48              "6:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
49                      UnusedLocalVariableCheck.MSG_UNUSED_NAMED_LOCAL_VARIABLE, "a"),
50          };
51  
52          final List<String> expectedXpathQueries = Arrays.asList(
53                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
54                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
55                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='a']]",
56                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
57                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
58                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
59                          + "./IDENT[@text='a']]/MODIFIERS",
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
62                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
63                          + "./IDENT[@text='a']]/TYPE",
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
65                          + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
66                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
67                          + "./IDENT[@text='a']]/TYPE/LITERAL_INT"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation,
71                  expectedXpathQueries);
72      }
73  
74      @Test
75      public void testTwo() throws Exception {
76          final File fileToProcess = new File(getPath(
77                  "InputXpathUnusedLocalVariableTwo.java"));
78  
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(UnusedLocalVariableCheck.class);
81          moduleConfig.addProperty("allowUnnamedVariables", "false");
82  
83          final String[] expectedViolation = {
84              "10:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
85                      UnusedLocalVariableCheck.MSG_UNUSED_LOCAL_VARIABLE, "b"),
86          };
87  
88          final List<String> expectedXpathQueries = Arrays.asList(
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
90                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
91                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='b']]",
92                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
93                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
94                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
95                          + "./IDENT[@text='b']]/MODIFIERS",
96                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
97                          + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
98                          + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
99                          + "./IDENT[@text='b']]/TYPE", "/COMPILATION_UNIT/CLASS_DEF["
100                         + "./IDENT[@text='InputXpathUnusedLocalVariableTwo']]/"
101                         + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
102                         + "./IDENT[@text='b']]/TYPE/LITERAL_INT"
103         );
104 
105         runVerifications(moduleConfig, fileToProcess, expectedViolation,
106                 expectedXpathQueries);
107 
108     }
109 }