1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.annotation;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck;
31
32 public class XpathRegressionAnnotationLocationTest extends AbstractXpathTestSupport {
33
34 private final String checkName = AnnotationLocationCheck.class.getSimpleName();
35
36 @Override
37 protected String getPackageLocation() {
38 return "org/checkstyle/suppressionxpathfilter/annotation/annotationlocation";
39 }
40
41 @Override
42 protected String getCheckName() {
43 return checkName;
44 }
45
46 @Test
47 public void testClass() throws Exception {
48 final File fileToProcess = new File(getPath(
49 "InputXpathAnnotationLocationClass.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(AnnotationLocationCheck.class);
53
54 final String[] expectedViolation = {
55 "6:1: " + getCheckMessage(AnnotationLocationCheck.class,
56 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE, "ClassAnnotation"),
57 };
58
59 final List<String> expectedXpathQueries = Arrays.asList(
60 "/COMPILATION_UNIT/CLASS_DEF"
61 + "[./IDENT[@text='InputXpathAnnotationLocationClass']]",
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathAnnotationLocationClass']]"
64 + "/MODIFIERS",
65 "/COMPILATION_UNIT/CLASS_DEF"
66 + "[./IDENT[@text='InputXpathAnnotationLocationClass']]/"
67 + "MODIFIERS/ANNOTATION[./IDENT[@text='ClassAnnotation']]",
68 "/COMPILATION_UNIT/CLASS_DEF"
69 + "[./IDENT[@text='InputXpathAnnotationLocationClass']]/"
70 + "MODIFIERS/ANNOTATION[./IDENT[@text='ClassAnnotation']]/AT"
71 );
72
73 runVerifications(moduleConfig, fileToProcess, expectedViolation,
74 expectedXpathQueries);
75 }
76
77 @Test
78 public void testInterface() throws Exception {
79 final File fileToProcess = new File(getPath(
80 "InputXpathAnnotationLocationInterface.java"));
81
82 final DefaultConfiguration moduleConfig =
83 createModuleConfig(AnnotationLocationCheck.class);
84
85 final String[] expectedViolation = {
86 "7:1: " + getCheckMessage(AnnotationLocationCheck.class,
87 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE,
88 "InterfaceAnnotation"),
89 };
90
91 final List<String> expectedXpathQueries = Arrays.asList(
92 "/COMPILATION_UNIT/INTERFACE_DEF"
93 + "[./IDENT[@text='"
94 + "InputXpathAnnotationLocationInterface']]",
95 "/COMPILATION_UNIT/INTERFACE_DEF"
96 + "[./IDENT[@text='InputXpathAnnotationLocationInterface'"
97 + "]]/MODIFIERS",
98 "/COMPILATION_UNIT/INTERFACE_DEF"
99 + "[./IDENT[@text='InputXpathAnnotationLocationInterface']]"
100 + "/MODIFIERS/ANNOTATION[./IDENT[@text='InterfaceAnnotation']]",
101 "/COMPILATION_UNIT/INTERFACE_DEF"
102 + "[./IDENT[@text='InputXpathAnnotationLocationInterface']]"
103 + "/MODIFIERS/ANNOTATION[./IDENT[@text='InterfaceAnnotation']]/AT"
104 );
105
106 runVerifications(moduleConfig, fileToProcess, expectedViolation,
107 expectedXpathQueries);
108 }
109
110 @Test
111 public void testEnum() throws Exception {
112 final File fileToProcess = new File(getPath(
113 "InputXpathAnnotationLocationEnum.java"));
114
115 final DefaultConfiguration moduleConfig =
116 createModuleConfig(AnnotationLocationCheck.class);
117
118 final String[] expectedViolation = {
119 "6:1: " + getCheckMessage(AnnotationLocationCheck.class,
120 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE,
121 "EnumAnnotation"),
122 };
123
124 final List<String> expectedXpathQueries = Arrays.asList(
125 "/COMPILATION_UNIT/ENUM_DEF[./IDENT[@text='"
126 + "InputXpathAnnotationLocationEnum']]",
127 "/COMPILATION_UNIT/ENUM_DEF"
128 + "[./IDENT[@text='InputXpathAnnotationLocationEnum']]"
129 + "/MODIFIERS",
130 "/COMPILATION_UNIT/ENUM_DEF"
131 + "[./IDENT[@text='InputXpathAnnotationLocationEnum']]"
132 + "/MODIFIERS/ANNOTATION[./IDENT[@text='EnumAnnotation']]",
133 "/COMPILATION_UNIT/ENUM_DEF"
134 + "[./IDENT[@text='InputXpathAnnotationLocationEnum']]"
135 + "/MODIFIERS/ANNOTATION[./IDENT[@text='EnumAnnotation']]/AT"
136 );
137
138 runVerifications(moduleConfig, fileToProcess, expectedViolation,
139 expectedXpathQueries);
140
141 }
142
143 @Test
144 public void testMethod() throws Exception {
145 final File fileToProcess = new File(getPath(
146 "InputXpathAnnotationLocationMethod.java"));
147
148 final DefaultConfiguration moduleConfig =
149 createModuleConfig(AnnotationLocationCheck.class);
150 moduleConfig.addProperty("tokens", "METHOD_DEF");
151
152 final String[] expectedViolation = {
153 "4:6: " + getCheckMessage(AnnotationLocationCheck.class,
154 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE,
155 "MethodAnnotation"),
156 };
157
158 final List<String> expectedXpathQueries = Arrays.asList(
159 "/COMPILATION_UNIT/CLASS_DEF"
160 + "[./IDENT[@text='InputXpathAnnotationLocationMethod']]/"
161 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]",
162 "/COMPILATION_UNIT/CLASS_DEF"
163 + "[./IDENT[@text='InputXpathAnnotationLocationMethod']]/"
164 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]/MODIFIERS",
165 "/COMPILATION_UNIT/CLASS_DEF"
166 + "[./IDENT[@text='InputXpathAnnotationLocationMethod']]/"
167 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]/MODIFIERS/"
168 + "ANNOTATION[./IDENT[@text='MethodAnnotation']]",
169 "/COMPILATION_UNIT/CLASS_DEF"
170 + "[./IDENT[@text='InputXpathAnnotationLocationMethod']]/"
171 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]/MODIFIERS/"
172 + "ANNOTATION[./IDENT[@text='MethodAnnotation']]/AT"
173 );
174
175 runVerifications(moduleConfig, fileToProcess, expectedViolation,
176 expectedXpathQueries);
177
178 }
179
180 @Test
181 public void testVariable() throws Exception {
182 final File fileToProcess = new File(getPath(
183 "InputXpathAnnotationLocationVariable.java"));
184
185 final DefaultConfiguration moduleConfig =
186 createModuleConfig(AnnotationLocationCheck.class);
187 moduleConfig.addProperty("tokens", "VARIABLE_DEF");
188
189 final String[] expectedViolation = {
190 "4:5: " + getCheckMessage(AnnotationLocationCheck.class,
191 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE,
192 "VariableAnnotation"),
193 };
194
195 final List<String> expectedXpathQueries = Arrays.asList(
196 "/COMPILATION_UNIT/CLASS_DEF"
197 + "[./IDENT[@text='InputXpathAnnotationLocationVariable']]/"
198 + "OBJBLOCK/VARIABLE_DEF[./IDENT[@text='b']]",
199 "/COMPILATION_UNIT/CLASS_DEF"
200 + "[./IDENT[@text='InputXpathAnnotationLocationVariable']]/"
201 + "OBJBLOCK/VARIABLE_DEF[./IDENT[@text='b']]/MODIFIERS",
202 "/COMPILATION_UNIT/CLASS_DEF"
203 + "[./IDENT[@text='InputXpathAnnotationLocationVariable']]/"
204 + "OBJBLOCK/VARIABLE_DEF[./IDENT[@text='b']]/MODIFIERS/"
205 + "ANNOTATION[./IDENT[@text='VariableAnnotation']]",
206 "/COMPILATION_UNIT/CLASS_DEF"
207 + "[./IDENT[@text='InputXpathAnnotationLocationVariable']]/"
208 + "OBJBLOCK/VARIABLE_DEF[./IDENT[@text='b']]/MODIFIERS/"
209 + "ANNOTATION[./IDENT[@text='VariableAnnotation']]/AT"
210 );
211
212 runVerifications(moduleConfig, fileToProcess, expectedViolation,
213 expectedXpathQueries);
214
215 }
216
217 @Test
218 public void testConstructor() throws Exception {
219 final File fileToProcess = new File(getPath(
220 "InputXpathAnnotationLocationCTOR.java"));
221
222 final DefaultConfiguration moduleConfig =
223 createModuleConfig(AnnotationLocationCheck.class);
224 moduleConfig.addProperty("tokens", "CTOR_DEF");
225
226 final String[] expectedViolation = {
227 "4:5: " + getCheckMessage(AnnotationLocationCheck.class,
228 AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE,
229 "CTORAnnotation"),
230 };
231
232 final List<String> expectedXpathQueries = Arrays.asList(
233 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
234 + "InputXpathAnnotationLocationCTOR']]/OBJBLOCK/CTOR_DEF"
235 + "[./IDENT[@text='"
236 + "InputXpathAnnotationLocationCTOR']]",
237 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
238 + "InputXpathAnnotationLocationCTOR']]/OBJBLOCK/CTOR_DEF"
239 + "[./IDENT[@text='"
240 + "InputXpathAnnotationLocationCTOR']]/MODIFIERS",
241 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
242 + "InputXpathAnnotationLocationCTOR']]/OBJBLOCK/CTOR_DEF"
243 + "[./IDENT[@text='"
244 + "InputXpathAnnotationLocationCTOR']]/"
245 + "MODIFIERS/ANNOTATION[./IDENT[@text='CTORAnnotation']]",
246 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
247 + "InputXpathAnnotationLocationCTOR']]/OBJBLOCK/CTOR_DEF"
248 + "[./IDENT[@text='"
249 + "InputXpathAnnotationLocationCTOR']]/"
250 + "MODIFIERS/ANNOTATION[./IDENT[@text='CTORAnnotation']]/AT"
251 );
252
253 runVerifications(moduleConfig, fileToProcess, expectedViolation,
254 expectedXpathQueries);
255
256 }
257
258 }