View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.CatchParameterNameCheck;
33  
34  public class XpathRegressionCatchParameterNameTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = CatchParameterNameCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      public String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/naming/catchparametername";
46      }
47  
48      @Test
49      public void testSimple() throws Exception {
50          final String pattern = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(CatchParameterNameCheck.class);
54  
55          final File fileToProcess =
56                  new File(getPath("InputXpathCatchParameterNameSimple.java"));
57  
58          final String[] expectedViolation = {
59              "6:28: " + getCheckMessage(CatchParameterNameCheck.class,
60                      MSG_INVALID_PATTERN, "e1", pattern),
61          };
62  
63          final List<String> expectedXpathQueries = Collections.singletonList(
64                  "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathCatchParameterNameSimple']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
67                  + "/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF/IDENT[@text='e1']"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
71      }
72  
73      @Test
74      public void testNested() throws Exception {
75          final String pattern = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
76  
77          final DefaultConfiguration moduleConfig =
78                  createModuleConfig(CatchParameterNameCheck.class);
79  
80          final File fileToProcess =
81                  new File(getPath("InputXpathCatchParameterNameNested.java"));
82  
83          final String[] expectedViolation = {
84              "9:40: " + getCheckMessage(CatchParameterNameCheck.class,
85                      MSG_INVALID_PATTERN, "i", pattern),
86          };
87  
88          final List<String> expectedXpathQueries = Collections.singletonList(
89                  "/COMPILATION_UNIT/CLASS_DEF"
90                  + "[./IDENT[@text='InputXpathCatchParameterNameNested']]"
91                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='NestedClass']]"
92                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
93                  + "/SLIST/LITERAL_IF/SLIST"
94                  + "/LITERAL_TRY/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF/IDENT[@text='i']"
95          );
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
98      }
99  
100     @Test
101     public void testStaticInit() throws Exception {
102         final String pattern = "^[a-z][a-zA-Z0-9]+$";
103 
104         final DefaultConfiguration moduleConfig =
105                 createModuleConfig(CatchParameterNameCheck.class);
106         moduleConfig.addProperty("format", pattern);
107 
108         final File fileToProcess =
109                 new File(getPath("InputXpathCatchParameterNameStaticInit.java"));
110 
111         final String[] expectedViolation = {
112             "7:32: " + getCheckMessage(CatchParameterNameCheck.class,
113                     MSG_INVALID_PATTERN, "Ex", pattern),
114         };
115 
116         final List<String> expectedXpathQueries = Collections.singletonList(
117                 "/COMPILATION_UNIT/CLASS_DEF"
118                 + "[./IDENT[@text='InputXpathCatchParameterNameStaticInit']]"
119                 + "/OBJBLOCK/STATIC_INIT/SLIST"
120                 + "/LITERAL_DO/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF/IDENT[@text='Ex']"
121         );
122 
123         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
124     }
125 
126     @Test
127     public void testAnonymous() throws Exception {
128         final String pattern = "^[a-z][a-zA-Z0-9]+$";
129 
130         final DefaultConfiguration moduleConfig =
131                 createModuleConfig(CatchParameterNameCheck.class);
132         moduleConfig.addProperty("format", pattern);
133 
134         final File fileToProcess =
135                 new File(getPath("InputXpathCatchParameterNameAnonymous.java"));
136 
137         final String[] expectedViolation = {
138             "12:40: " + getCheckMessage(CatchParameterNameCheck.class,
139                     MSG_INVALID_PATTERN, "E1", pattern),
140         };
141 
142         final List<String> expectedXpathQueries = Collections.singletonList(
143                 "/COMPILATION_UNIT/CLASS_DEF"
144                 + "[./IDENT[@text='InputXpathCatchParameterNameAnonymous']]"
145                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]"
146                 + "/OBJBLOCK/CTOR_DEF[./IDENT[@text='InnerClass']]"
147                 + "/SLIST/EXPR/LITERAL_NEW[./IDENT[@text='Runnable']]"
148                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]"
149                 + "/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF/IDENT[@text='E1']"
150         );
151 
152         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
153     }
154 
155     @Test
156     public void testLambda() throws Exception {
157         final String pattern = "^[A-Z][a-z]+$";
158 
159         final DefaultConfiguration moduleConfig =
160                 createModuleConfig(CatchParameterNameCheck.class);
161         moduleConfig.addProperty("format", pattern);
162 
163         final File fileToProcess =
164                 new File(getPath("InputXpathCatchParameterNameLambda.java"));
165 
166         final String[] expectedViolation = {
167             "12:32: " + getCheckMessage(CatchParameterNameCheck.class,
168                     MSG_INVALID_PATTERN, "e", pattern),
169         };
170 
171         final List<String> expectedXpathQueries = Collections.singletonList(
172                 "/COMPILATION_UNIT/CLASS_DEF"
173                 + "[./IDENT[@text='InputXpathCatchParameterNameLambda']]"
174                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='lambdaFunction']]"
175                 + "/ASSIGN/LAMBDA[./IDENT[@text='a']]"
176                 + "/SLIST/LITERAL_FOR/SLIST/LITERAL_TRY/LITERAL_CATCH"
177                 + "/PARAMETER_DEF/IDENT[@text='e']"
178         );
179 
180         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
181     }
182 
183     @Test
184     public void testEnum() throws Exception {
185         final String pattern = "^[A-Z][a-z]+$";
186 
187         final DefaultConfiguration moduleConfig =
188                 createModuleConfig(CatchParameterNameCheck.class);
189         moduleConfig.addProperty("format", pattern);
190 
191         final File fileToProcess =
192                 new File(getPath("InputXpathCatchParameterNameEnum.java"));
193 
194         final String[] expectedViolation = {
195             "10:40: " + getCheckMessage(CatchParameterNameCheck.class,
196                     MSG_INVALID_PATTERN, "eX", pattern),
197         };
198 
199         final List<String> expectedXpathQueries = Collections.singletonList(
200                 "/COMPILATION_UNIT/ENUM_DEF"
201                 + "[./IDENT[@text='InputXpathCatchParameterNameEnum']]"
202                 + "/OBJBLOCK/ENUM_CONSTANT_DEF[./IDENT[@text='VALUE']]"
203                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
204                 + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST/LITERAL_TRY/LITERAL_CATCH/"
205                 + "PARAMETER_DEF/IDENT[@text='eX']"
206         );
207 
208         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
209     }
210 
211     @Test
212     public void testInterface() throws Exception {
213         final String pattern = "^[A-Z][a-z]+$";
214 
215         final DefaultConfiguration moduleConfig =
216                 createModuleConfig(CatchParameterNameCheck.class);
217         moduleConfig.addProperty("format", pattern);
218 
219         final File fileToProcess =
220                 new File(getPath("InputXpathCatchParameterNameInterface.java"));
221 
222         final String[] expectedViolation = {
223             "7:32: " + getCheckMessage(CatchParameterNameCheck.class,
224                     MSG_INVALID_PATTERN, "EX", pattern),
225         };
226 
227         final List<String> expectedXpathQueries = Collections.singletonList(
228                 "/COMPILATION_UNIT/INTERFACE_DEF"
229                 + "[./IDENT[@text='InputXpathCatchParameterNameInterface']]"
230                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='InnerInterface']]"
231                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
232                 + "/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF/IDENT[@text='EX']"
233         );
234 
235         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
236     }
237 
238 }