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