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