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