1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.coding;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck;
32
33 public class XpathRegressionUnnecessaryParenthesesTest extends AbstractXpathTestSupport {
34
35 @Override
36 protected String getCheckName() {
37 return UnnecessaryParenthesesCheck.class.getSimpleName();
38 }
39
40 @Override
41 protected String getPackageLocation() {
42 return "org/checkstyle/suppressionxpathfilter/coding/unnecessaryparentheses";
43 }
44
45 @Test
46 public void testClassFields() throws Exception {
47 final File fileToProcess = new File(
48 getPath("InputXpathUnnecessaryParenthesesClassFields.java")
49 );
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(UnnecessaryParenthesesCheck.class);
53
54 final String[] expectedViolation = {
55 "4:13: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
56 UnnecessaryParenthesesCheck.MSG_ASSIGN),
57 };
58
59 final List<String> expectedXpathQueries = Arrays.asList(
60 "/COMPILATION_UNIT/CLASS_DEF"
61 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesClassFields']]"
62 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
63 + "/ASSIGN/EXPR",
64
65 "/COMPILATION_UNIT/CLASS_DEF"
66 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesClassFields']]"
67 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
68 + "/ASSIGN/EXPR/LPAREN"
69 );
70
71 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
72 }
73
74 @Test
75 public void testConditionals() throws Exception {
76 final File fileToProcess = new File(
77 getPath("InputXpathUnnecessaryParenthesesConditionals.java")
78 );
79
80 final DefaultConfiguration moduleConfig =
81 createModuleConfig(UnnecessaryParenthesesCheck.class);
82
83 final String[] expectedViolation = {
84 "5:13: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
85 UnnecessaryParenthesesCheck.MSG_EXPR),
86 };
87
88 final List<String> expectedXpathQueries = Arrays.asList(
89 "/COMPILATION_UNIT/CLASS_DEF"
90 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesConditionals']]"
91 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
92 + "/SLIST/LITERAL_IF/EXPR",
93
94 "/COMPILATION_UNIT/CLASS_DEF"
95 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesConditionals']]"
96 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
97 + "/SLIST/LITERAL_IF/EXPR/LPAREN"
98 );
99
100 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
101 }
102
103 @Test
104 public void testLambdas() throws Exception {
105 final File fileToProcess = new File(
106 getPath("InputXpathUnnecessaryParenthesesLambdas.java")
107 );
108
109 final DefaultConfiguration moduleConfig =
110 createModuleConfig(UnnecessaryParenthesesCheck.class);
111
112 final String[] expectedViolation = {
113 "7:35: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
114 UnnecessaryParenthesesCheck.MSG_LAMBDA),
115 };
116
117 final List<String> expectedXpathQueries = Collections.singletonList(
118 "/COMPILATION_UNIT/CLASS_DEF"
119 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesLambdas']]"
120 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='predicate']]"
121 + "/ASSIGN/LAMBDA"
122 );
123
124 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
125 }
126
127 @Test
128 public void testLocalVariables() throws Exception {
129 final File fileToProcess = new File(
130 getPath("InputXpathUnnecessaryParenthesesLocalVariables.java")
131 );
132
133 final DefaultConfiguration moduleConfig =
134 createModuleConfig(UnnecessaryParenthesesCheck.class);
135
136 final String[] expectedViolation = {
137 "5:18: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
138 UnnecessaryParenthesesCheck.MSG_IDENT, "a"),
139 };
140
141 final List<String> expectedXpathQueries = Collections.singletonList(
142 "/COMPILATION_UNIT/CLASS_DEF"
143 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesLocalVariables']]"
144 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
145 + "/SLIST/VARIABLE_DEF[./IDENT[@text='b']]"
146 + "/ASSIGN/EXPR/PLUS/IDENT[@text='a']"
147 );
148
149 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
150 }
151
152 @Test
153 public void testStringLiteral() throws Exception {
154 final File fileToProcess = new File(
155 getPath("InputXpathUnnecessaryParenthesesStringLiteral.java")
156 );
157
158 final DefaultConfiguration moduleConfig =
159 createModuleConfig(UnnecessaryParenthesesCheck.class);
160
161 final String[] expectedViolation = {
162 "5:23: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
163 UnnecessaryParenthesesCheck.MSG_STRING, "\"Checkstyle\""),
164 };
165
166 final List<String> expectedXpathQueries = Collections.singletonList(
167 "/COMPILATION_UNIT/CLASS_DEF"
168 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesStringLiteral']]"
169 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
170 + "/SLIST/VARIABLE_DEF[./IDENT[@text='str']]"
171 + "/ASSIGN/EXPR/PLUS/STRING_LITERAL[@text='Checkstyle']"
172 );
173
174 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
175 }
176
177 @Test
178 public void testMethodDef() throws Exception {
179 final File fileToProcess = new File(
180 getPath("InputXpathUnnecessaryParenthesesMethodDef.java")
181 );
182
183 final DefaultConfiguration moduleConfig =
184 createModuleConfig(UnnecessaryParenthesesCheck.class);
185
186 final String[] expectedViolation = {
187 "5:18: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
188 UnnecessaryParenthesesCheck.MSG_LITERAL, "10"),
189 };
190
191 final List<String> expectedXpathQueries = Collections.singletonList(
192 "/COMPILATION_UNIT/CLASS_DEF"
193 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesMethodDef']]"
194 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
195 + "/SLIST/VARIABLE_DEF[./IDENT[@text='a']]"
196 + "/ASSIGN/EXPR/PLUS/NUM_INT[@text='10']"
197 );
198
199 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
200 }
201
202 @Test
203 public void testReturnExpr() throws Exception {
204 final File fileToProcess = new File(
205 getPath("InputXpathUnnecessaryParenthesesReturnExpr.java")
206 );
207
208 final DefaultConfiguration moduleConfig =
209 createModuleConfig(UnnecessaryParenthesesCheck.class);
210
211 final String[] expectedViolation = {
212 "5:16: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
213 UnnecessaryParenthesesCheck.MSG_RETURN),
214 };
215
216 final List<String> expectedXpathQueries = Arrays.asList(
217 "/COMPILATION_UNIT/CLASS_DEF"
218 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesReturnExpr']]"
219 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
220 + "/SLIST/LITERAL_RETURN/EXPR",
221
222 "/COMPILATION_UNIT/CLASS_DEF"
223 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesReturnExpr']]"
224 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
225 + "/SLIST/LITERAL_RETURN/EXPR/LPAREN"
226 );
227
228 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
229 }
230
231 @Test
232 public void testExprWithMethodParam() throws Exception {
233 final File fileToProcess = new File(
234 getPath("InputXpathUnnecessaryParenthesesExprWithMethodParam.java")
235 );
236
237 final DefaultConfiguration moduleConfig =
238 createModuleConfig(UnnecessaryParenthesesCheck.class);
239
240 final String[] expectedViolation = {
241 "5:17: " + getCheckMessage(UnnecessaryParenthesesCheck.class,
242 UnnecessaryParenthesesCheck.MSG_ASSIGN),
243 };
244
245 final List<String> expectedXpathQueries = Arrays.asList(
246 "/COMPILATION_UNIT/CLASS_DEF"
247 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesExprWithMethodParam']]"
248 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
249 + "/SLIST/VARIABLE_DEF[./IDENT[@text='c']]"
250 + "/ASSIGN/EXPR",
251
252 "/COMPILATION_UNIT/CLASS_DEF"
253 + "[./IDENT[@text='InputXpathUnnecessaryParenthesesExprWithMethodParam']]"
254 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]"
255 + "/SLIST/VARIABLE_DEF[./IDENT[@text='c']]"
256 + "/ASSIGN/EXPR/LPAREN"
257 );
258
259 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
260 }
261 }