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.naming;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck;
33  
34  public class XpathRegressionLocalVariableNameTest extends AbstractXpathTestSupport {
35  
36      @Override
37      protected String getCheckName() {
38          return LocalVariableNameCheck.class.getSimpleName();
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/naming/localvariablename";
44      }
45  
46      @Test
47      public void testMethod() throws Exception {
48          final File fileToProcess = new File(getPath("InputXpathLocalVariableNameMethod.java"));
49  
50          final DefaultConfiguration moduleConfig =
51              createModuleConfig(LocalVariableNameCheck.class);
52  
53          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
54          final String[] expectedViolations = {
55              "5:9: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
56                  "VAR", pattern),
57          };
58  
59          final List<String> expectedXpathQueries = Collections.singletonList(
60              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61                + "@text='InputXpathLocalVariableNameMethod']]"
62                + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]"
63                + "/SLIST/VARIABLE_DEF/IDENT[@text='VAR']"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
67  
68      }
69  
70      @Test
71      public void testIteration() throws Exception {
72          final File fileToProcess = new File(getPath("InputXpathLocalVariableNameIteration.java"));
73  
74          final DefaultConfiguration moduleConfig = createModuleConfig(LocalVariableNameCheck.class);
75  
76          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
77          final String[] expectedViolations = {
78              "7:14: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
79                  "var_1", pattern),
80          };
81  
82          final List<String> expectedXpathQueries = Collections.singletonList(
83              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
84                + "@text='InputXpathLocalVariableNameIteration']]"
85                + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/"
86                + "SLIST/LITERAL_FOR/FOR_INIT/VARIABLE_DEF/IDENT[@text='var_1']"
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
90  
91      }
92  
93      @Test
94      public void testInnerClass() throws Exception {
95          final File fileToProcess = new File(getPath("InputXpathLocalVariableNameInnerClass.java"));
96  
97          final DefaultConfiguration moduleConfig = createModuleConfig(LocalVariableNameCheck.class);
98  
99          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
100         final String[] expectedViolations = {
101             "6:11: " + getCheckMessage(LocalVariableNameCheck.class, MSG_INVALID_PATTERN,
102                 "VAR", pattern),
103         };
104 
105         final List<String> expectedXpathQueries = Collections.singletonList(
106             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
107               + "@text='InputXpathLocalVariableNameInnerClass']]"
108               + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
109               + "METHOD_DEF[./IDENT[@text='myMethod']]/SLIST/VARIABLE_DEF/IDENT[@text='VAR']"
110         );
111 
112         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
113 
114     }
115 
116 }