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