1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter;
21
22 import java.io.File;
23 import java.util.List;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28 import com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck;
29
30 public class XpathRegressionFinalParametersTest extends AbstractXpathTestSupport {
31
32 private final String checkName = FinalParametersCheck.class.getSimpleName();
33
34 @Override
35 protected String getCheckName() {
36 return checkName;
37 }
38
39 @Test
40 public void testMethod() throws Exception {
41 final File fileToProcess =
42 new File(getPath("InputXpathFinalParametersMethod.java"));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(FinalParametersCheck.class);
46
47 final String[] expectedViolation = {
48 "5:24: " + getCheckMessage(FinalParametersCheck.class,
49 FinalParametersCheck.MSG_KEY, "argOne"),
50 };
51
52 final List<String> expectedXpathQueries = List.of(
53 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
54 + "[@text='InputXpathFinalParametersMethod']]"
55 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
56 + "/PARAMETERS",
57
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59 + "[@text='InputXpathFinalParametersMethod']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
61 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]",
62
63 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64 + "[@text='InputXpathFinalParametersMethod']]"
65 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
66 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",
67
68 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
69 + "[@text='InputXpathFinalParametersMethod']]"
70 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
71 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE",
72
73 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
74 + "[@text='InputXpathFinalParametersMethod']]"
75 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
76 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT"
77 );
78
79 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
80 }
81
82 @Test
83 public void testCtor() throws Exception {
84 final File fileToProcess =
85 new File(getPath("InputXpathFinalParametersCtor.java"));
86
87 final DefaultConfiguration moduleConfig =
88 createModuleConfig(FinalParametersCheck.class);
89
90 moduleConfig.addProperty("tokens", "CTOR_DEF");
91
92 final String[] expectedViolation = {
93 "5:42: " + getCheckMessage(FinalParametersCheck.class,
94 FinalParametersCheck.MSG_KEY, "argOne"),
95 };
96
97 final List<String> expectedXpathQueries = List.of(
98 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
99 + "[@text='InputXpathFinalParametersCtor']]"
100 + "/OBJBLOCK/CTOR_DEF[./IDENT["
101 + "@text='InputXpathFinalParametersCtor']]"
102 + "/PARAMETERS",
103
104 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
105 + "[@text='InputXpathFinalParametersCtor']]"
106 + "/OBJBLOCK/CTOR_DEF[./IDENT["
107 + "@text='InputXpathFinalParametersCtor']]"
108 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]",
109
110 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
111 + "[@text='InputXpathFinalParametersCtor']]"
112 + "/OBJBLOCK/CTOR_DEF[./IDENT["
113 + "@text='InputXpathFinalParametersCtor']]"
114 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",
115
116 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
117 + "[@text='InputXpathFinalParametersCtor']]"
118 + "/OBJBLOCK/CTOR_DEF[./IDENT["
119 + "@text='InputXpathFinalParametersCtor']]"
120 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE",
121
122 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
123 + "[@text='InputXpathFinalParametersCtor']]"
124 + "/OBJBLOCK/CTOR_DEF[./IDENT["
125 + "@text='InputXpathFinalParametersCtor']]"
126 + "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT"
127 );
128
129 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
130 }
131
132 @Test
133 public void testAnonymous() throws Exception {
134 final File fileToProcess =
135 new File(getPath("InputXpathFinalParametersAnonymous.java"));
136
137 final DefaultConfiguration moduleConfig =
138 createModuleConfig(FinalParametersCheck.class);
139
140 moduleConfig.addProperty("ignorePrimitiveTypes", "true");
141
142 final String[] expectedViolation = {
143 "11:32: " + getCheckMessage(FinalParametersCheck.class,
144 FinalParametersCheck.MSG_KEY, "argOne"),
145 };
146
147 final List<String> expectedXpathQueries = List.of(
148 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
149 + "[@text='InputXpathFinalParametersAnonymous']]"
150 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
151 + "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
152 + "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
153 + "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS",
154
155 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
156 + "[@text='InputXpathFinalParametersAnonymous']]"
157 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
158 + "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
159 + "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
160 + "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
161 + "/PARAMETER_DEF[./IDENT[@text='argOne']]",
162
163 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
164 + "[@text='InputXpathFinalParametersAnonymous']]"
165 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
166 + "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
167 + "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
168 + "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
169 + "/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS",
170
171 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
172 + "[@text='InputXpathFinalParametersAnonymous']]"
173 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
174 + "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
175 + "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
176 + "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
177 + "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE[./IDENT[@text='String']]",
178
179 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
180 + "[@text='InputXpathFinalParametersAnonymous']]"
181 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/"
182 + "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR"
183 + "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK"
184 + "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS"
185 + "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/IDENT[@text='String']"
186 );
187
188 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
189 }
190 }