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.ParameterNameCheck;
32  
33  public class XpathRegressionParameterNameTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = ParameterNameCheck.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/parametername";
45      }
46  
47      @Test
48      public void testDefaultPattern() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathParameterNameDefaultPattern.java"));
51  
52          final String pattern = "^[a-z][a-zA-Z0-9]*$";
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(ParameterNameCheck.class);
55  
56          final String[] expectedViolation = {
57              "5:22: " + getCheckMessage(ParameterNameCheck.class,
58                          AbstractNameCheck.MSG_INVALID_PATTERN, "v_1", pattern),
59          };
60  
61          final List<String> expectedXpathQueries = Collections.singletonList(
62                  "/COMPILATION_UNIT"
63                          + "/CLASS_DEF[./IDENT[@text"
64                          + "='InputXpathParameterNameDefaultPattern']]"
65                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method1']]"
66                          + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='v_1']"
67          );
68          runVerifications(moduleConfig, fileToProcess, expectedViolation,
69                  expectedXpathQueries);
70      }
71  
72      @Test
73      public void testDifferentPattern() throws Exception {
74          final File fileToProcess =
75                  new File(getPath("InputXpathParameterNameDifferentPattern.java"));
76  
77          final String pattern = "^[a-z][_a-zA-Z0-9]+$";
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(ParameterNameCheck.class);
80          moduleConfig.addProperty("format", "^[a-z][_a-zA-Z0-9]+$");
81  
82          final String[] expectedViolation = {
83              "5:22: " + getCheckMessage(ParameterNameCheck.class,
84                          AbstractNameCheck.MSG_INVALID_PATTERN, "V2", pattern),
85          };
86  
87          final List<String> expectedXpathQueries = Collections.singletonList(
88                  "/COMPILATION_UNIT"
89                          + "/CLASS_DEF[./IDENT[@text"
90                          + "='InputXpathParameterNameDifferentPattern']]"
91                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method2']]"
92                          + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='V2']"
93          );
94          runVerifications(moduleConfig, fileToProcess, expectedViolation,
95                  expectedXpathQueries);
96      }
97  
98      @Test
99      public void testIgnoreOverridden() throws Exception {
100         final File fileToProcess =
101                 new File(getPath("InputXpathParameterNameIgnoreOverridden.java"));
102 
103         final String pattern = "^[a-z][a-zA-Z0-9]*$";
104         final DefaultConfiguration moduleConfig =
105                 createModuleConfig(ParameterNameCheck.class);
106         moduleConfig.addProperty("ignoreOverridden", "true");
107 
108         final String[] expectedViolation = {
109             "5:22: " + getCheckMessage(ParameterNameCheck.class,
110                         AbstractNameCheck.MSG_INVALID_PATTERN, "V2", pattern),
111         };
112 
113         final List<String> expectedXpathQueries = Collections.singletonList(
114                 "/COMPILATION_UNIT"
115                         + "/CLASS_DEF[./IDENT[@text"
116                         + "='InputXpathParameterNameIgnoreOverridden']]"
117                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method2']]"
118                         + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='V2']"
119         );
120         runVerifications(moduleConfig, fileToProcess, expectedViolation,
121                 expectedXpathQueries);
122     }
123 
124     @Test
125     public void testAccessModifiers() throws Exception {
126         final File fileToProcess =
127                 new File(getPath("InputXpathParameterNameAccessModifier.java"));
128 
129         final String pattern = "^[a-z][a-z0-9][a-zA-Z0-9]*$";
130         final DefaultConfiguration moduleConfig =
131                 createModuleConfig(ParameterNameCheck.class);
132         moduleConfig.addProperty("format", "^[a-z][a-z0-9][a-zA-Z0-9]*$");
133         moduleConfig.addProperty("accessModifiers", "public");
134 
135         final String[] expectedViolation = {
136             "8:29: " + getCheckMessage(ParameterNameCheck.class,
137                         AbstractNameCheck.MSG_INVALID_PATTERN, "b", pattern),
138         };
139 
140         final List<String> expectedXpathQueries = Collections.singletonList(
141                 "/COMPILATION_UNIT"
142                         + "/CLASS_DEF[./IDENT[@text"
143                         + "='InputXpathParameterNameAccessModifier']]"
144                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method2']]"
145                         + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='b']"
146         );
147         runVerifications(moduleConfig, fileToProcess, expectedViolation,
148                 expectedXpathQueries);
149     }
150 }