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