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 java.io.File;
23  import java.util.Collections;
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.naming.AbstractNameCheck;
31  import com.puppycrawl.tools.checkstyle.checks.naming.PatternVariableNameCheck;
32  
33  public class XpathRegressionPatternVariableNameTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = PatternVariableNameCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/naming/patternvariablename";
45      }
46  
47      @Test
48      public void testOne() throws Exception {
49          final File fileToProcess =
50                  new File(getPath(
51                          "InputXpathPatternVariableNameOne.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(PatternVariableNameCheck.class);
55          final String defaultPattern = "^([a-z][a-zA-Z0-9]*|_)$";
56  
57          final String[] expectedViolation = {
58              "6:33: " + getCheckMessage(PatternVariableNameCheck.class,
59                      AbstractNameCheck.MSG_INVALID_PATTERN,
60                      "STRING1", defaultPattern),
61          };
62  
63          final List<String> expectedXpathQueries = Collections.singletonList(
64              "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathPatternVariableNameOne']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyClass']]/SLIST/LITERAL_IF/EXPR/"
67                  + "LITERAL_INSTANCEOF[./IDENT[@text='o1']]/PATTERN_VARIABLE_DEF/"
68                  + "IDENT[@text='STRING1']"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testTwo() throws Exception {
77          final File fileToProcess =
78                  new File(getPath(
79                          "InputXpathPatternVariableNameTwo.java"));
80  
81          final String nonDefaultPattern = "^_[a-zA-Z0-9]*$";
82  
83          final DefaultConfiguration moduleConfig =
84                  createModuleConfig(PatternVariableNameCheck.class);
85          moduleConfig.addProperty("format", nonDefaultPattern);
86  
87          final String[] expectedViolation = {
88              "6:34: " + getCheckMessage(PatternVariableNameCheck.class,
89                      AbstractNameCheck.MSG_INVALID_PATTERN, "s", nonDefaultPattern),
90          };
91  
92          final List<String> expectedXpathQueries = Collections.singletonList(
93              "/COMPILATION_UNIT/CLASS_DEF"
94                  + "[./IDENT[@text='InputXpathPatternVariableNameTwo']]"
95                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyClass']]/SLIST/LITERAL_IF/EXPR/"
96                  + "LITERAL_INSTANCEOF[./IDENT[@text='o1']]/"
97                  + "PATTERN_VARIABLE_DEF/IDENT[@text='s']"
98          );
99  
100         runVerifications(moduleConfig, fileToProcess, expectedViolation,
101                 expectedXpathQueries);
102     }
103 
104     @Test
105     public void testThree() throws Exception {
106         final File fileToProcess =
107                 new File(getPath(
108                         "InputXpathPatternVariableNameThree.java"));
109 
110         final String nonDefaultPattern = "^[a-z](_?[a-zA-Z0-9]+)*$";
111 
112         final DefaultConfiguration moduleConfig =
113                 createModuleConfig(PatternVariableNameCheck.class);
114         moduleConfig.addProperty("format", nonDefaultPattern);
115 
116         final String[] expectedViolation = {
117             "6:34: " + getCheckMessage(PatternVariableNameCheck.class,
118                 AbstractNameCheck.MSG_INVALID_PATTERN, "STR", nonDefaultPattern),
119         };
120 
121         final List<String> expectedXpathQueries = Collections.singletonList(
122                 "/COMPILATION_UNIT/CLASS_DEF"
123                     + "[./IDENT[@text='InputXpathPatternVariableNameThree']]"
124                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyClass']]/SLIST/LITERAL_IF/"
125                     + "EXPR/LITERAL_INSTANCEOF[./IDENT[@text='o1']]/"
126                     + "PATTERN_VARIABLE_DEF/IDENT[@text='STR']"
127         );
128 
129         runVerifications(moduleConfig, fileToProcess, expectedViolation,
130                 expectedXpathQueries);
131     }
132 
133     @Test
134     public void testFour() throws Exception {
135         final File fileToProcess =
136                 new File(getPath(
137                         "InputXpathPatternVariableNameFour.java"));
138 
139         final String nonDefaultPattern = "^[a-z][_a-zA-Z0-9]{2,}$";
140 
141         final DefaultConfiguration moduleConfig =
142                 createModuleConfig(PatternVariableNameCheck.class);
143         moduleConfig.addProperty("format", nonDefaultPattern);
144 
145         final String[] expectedViolation = {
146             "6:34: " + getCheckMessage(PatternVariableNameCheck.class,
147                 AbstractNameCheck.MSG_INVALID_PATTERN, "st", nonDefaultPattern),
148         };
149 
150         final List<String> expectedXpathQueries = Collections.singletonList(
151                 "/COMPILATION_UNIT/CLASS_DEF"
152                     + "[./IDENT[@text='InputXpathPatternVariableNameFour']]"
153                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyClass']]/SLIST/LITERAL_IF/EXPR/"
154                     + "LITERAL_INSTANCEOF[./IDENT[@text='o1']]/"
155                     + "PATTERN_VARIABLE_DEF/IDENT[@text='st']"
156         );
157 
158         runVerifications(moduleConfig, fileToProcess, expectedViolation,
159                 expectedXpathQueries);
160     }
161 }