-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
AnnotationUtilTest.java
346 lines (312 loc) · 12.9 KB
/
AnnotationUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.utils;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
import java.util.Set;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.DetailAstImpl;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class AnnotationUtilTest {
@Test
public void testIsProperUtilsClass() {
try {
isUtilsClassHasPrivateConstructor(AnnotationUtil.class);
assertWithMessage("Exception is expected").fail();
}
catch (ReflectiveOperationException ex) {
assertWithMessage("Invalid exception message")
.that(ex)
.hasCauseThat()
.hasMessageThat()
.isEqualTo("do not instantiate.");
}
}
@Test
public void testContainsAnnotationNull() {
try {
AnnotationUtil.containsAnnotation(null);
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testContainsAnnotationNull2() {
try {
AnnotationUtil.containsAnnotation(null, "");
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testContainsAnnotationFalse() {
final DetailAstImpl ast = new DetailAstImpl();
ast.setType(1);
assertWithMessage("AnnotationUtil should not contain " + ast)
.that(AnnotationUtil.containsAnnotation(ast))
.isFalse();
}
@Test
public void testContainsAnnotationFalse2() {
final DetailAstImpl ast = new DetailAstImpl();
ast.setType(1);
final DetailAstImpl ast2 = new DetailAstImpl();
ast2.setType(TokenTypes.MODIFIERS);
ast.addChild(ast2);
assertWithMessage("AnnotationUtil should not contain " + ast)
.that(AnnotationUtil.containsAnnotation(ast))
.isFalse();
}
@Test
public void testContainsAnnotationTrue() {
final DetailAstImpl ast = new DetailAstImpl();
ast.setType(1);
final DetailAstImpl ast2 = new DetailAstImpl();
ast2.setType(TokenTypes.MODIFIERS);
ast.addChild(ast2);
final DetailAstImpl ast3 = new DetailAstImpl();
ast3.setType(TokenTypes.ANNOTATION);
ast2.addChild(ast3);
assertWithMessage("AnnotationUtil should contain " + ast)
.that(AnnotationUtil.containsAnnotation(ast))
.isTrue();
}
@Test
public void testAnnotationHolderNull() {
try {
AnnotationUtil.getAnnotationHolder(null);
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testAnnotationNull() {
try {
AnnotationUtil.getAnnotation(null, null);
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testAnnotationNull2() {
try {
AnnotationUtil.getAnnotation(new DetailAstImpl(), null);
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the annotation is null");
}
}
@Test
public void testAnnotationEmpty() {
try {
AnnotationUtil.getAnnotation(new DetailAstImpl(), "");
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the annotation is empty or spaces");
}
}
@Test
public void testContainsAnnotationWithNull() {
try {
AnnotationUtil.getAnnotation(null, "");
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testContainsAnnotationListWithNullAst() {
try {
AnnotationUtil.containsAnnotation(null, Set.of("Override"));
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("the ast is null");
}
}
@Test
public void testContainsAnnotationListWithNullList() {
final DetailAST ast = new DetailAstImpl();
final Set<String> annotations = null;
try {
AnnotationUtil.containsAnnotation(ast, annotations);
assertWithMessage("IllegalArgumentException is expected").fail();
}
catch (IllegalArgumentException ex) {
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("annotations cannot be null");
}
}
@Test
public void testContainsAnnotationListWithEmptyList() {
final DetailAST ast = new DetailAstImpl();
final Set<String> annotations = Set.of();
final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
assertWithMessage("An empty set should lead to a false result")
.that(result)
.isFalse();
}
@Test
public void testContainsAnnotationListWithNoAnnotationNode() {
final DetailAstImpl ast = new DetailAstImpl();
final DetailAstImpl modifiersAst = new DetailAstImpl();
modifiersAst.setType(TokenTypes.MODIFIERS);
ast.addChild(modifiersAst);
final Set<String> annotations = Set.of("Override");
final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
assertWithMessage("An empty ast should lead to a false result")
.that(result)
.isFalse();
}
@Test
public void testContainsAnnotationListWithNoMatchingAnnotation() {
final DetailAstImpl ast = new DetailAstImpl();
final DetailAstImpl modifiersAst = create(
TokenTypes.MODIFIERS,
create(
TokenTypes.ANNOTATION,
create(
TokenTypes.DOT,
create(
TokenTypes.IDENT,
"Override")
)
)
);
ast.addChild(modifiersAst);
final Set<String> annotations = Set.of("Deprecated");
final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
assertWithMessage("No matching annotation found")
.that(result)
.isFalse();
}
@Test
public void testContainsAnnotation() {
final DetailAstImpl astForTest = new DetailAstImpl();
astForTest.setType(TokenTypes.PACKAGE_DEF);
final DetailAstImpl child = new DetailAstImpl();
final DetailAstImpl annotations = new DetailAstImpl();
final DetailAstImpl annotation = new DetailAstImpl();
final DetailAstImpl annotationNameHolder = new DetailAstImpl();
final DetailAstImpl annotationName = new DetailAstImpl();
annotations.setType(TokenTypes.ANNOTATIONS);
annotation.setType(TokenTypes.ANNOTATION);
annotationNameHolder.setType(TokenTypes.AT);
annotationName.setText("Annotation");
annotationNameHolder.setNextSibling(annotationName);
annotation.setFirstChild(annotationNameHolder);
annotations.setFirstChild(annotation);
child.setNextSibling(annotations);
astForTest.setFirstChild(child);
assertWithMessage("Annotation should contain " + astForTest)
.that(AnnotationUtil.containsAnnotation(astForTest, "Annotation"))
.isTrue();
}
@Test
public void testContainsAnnotationWithStringFalse() {
final DetailAstImpl astForTest = new DetailAstImpl();
astForTest.setType(TokenTypes.PACKAGE_DEF);
final DetailAstImpl child = new DetailAstImpl();
final DetailAstImpl annotations = new DetailAstImpl();
final DetailAstImpl annotation = new DetailAstImpl();
final DetailAstImpl annotationNameHolder = new DetailAstImpl();
final DetailAstImpl annotationName = new DetailAstImpl();
annotations.setType(TokenTypes.ANNOTATIONS);
annotation.setType(TokenTypes.ANNOTATION);
annotationNameHolder.setType(TokenTypes.AT);
annotationName.setText("Annotation");
annotationNameHolder.setNextSibling(annotationName);
annotation.setFirstChild(annotationNameHolder);
annotations.setFirstChild(annotation);
child.setNextSibling(annotations);
astForTest.setFirstChild(child);
assertWithMessage("Annotation should not contain " + astForTest)
.that(AnnotationUtil.containsAnnotation(astForTest, "AnnotationBad"))
.isFalse();
}
@Test
public void testContainsAnnotationWithComment() {
final DetailAstImpl astForTest = new DetailAstImpl();
astForTest.setType(TokenTypes.PACKAGE_DEF);
final DetailAstImpl child = new DetailAstImpl();
final DetailAstImpl annotations = new DetailAstImpl();
final DetailAstImpl annotation = new DetailAstImpl();
final DetailAstImpl annotationNameHolder = new DetailAstImpl();
final DetailAstImpl annotationName = new DetailAstImpl();
final DetailAstImpl comment = new DetailAstImpl();
annotations.setType(TokenTypes.ANNOTATIONS);
annotation.setType(TokenTypes.ANNOTATION);
annotationNameHolder.setType(TokenTypes.AT);
comment.setType(TokenTypes.BLOCK_COMMENT_BEGIN);
annotationName.setText("Annotation");
annotationNameHolder.setNextSibling(annotationName);
annotation.setFirstChild(comment);
comment.setNextSibling(annotationNameHolder);
annotations.setFirstChild(annotation);
child.setNextSibling(annotations);
astForTest.setFirstChild(child);
assertWithMessage("Annotation should contain " + astForTest)
.that(AnnotationUtil.containsAnnotation(astForTest, "Annotation"))
.isTrue();
}
private static DetailAstImpl create(int tokenType) {
final DetailAstImpl ast = new DetailAstImpl();
ast.setType(tokenType);
return ast;
}
private static DetailAstImpl create(int tokenType, String text) {
final DetailAstImpl ast = create(tokenType);
ast.setText(text);
return ast;
}
private static DetailAstImpl create(int tokenType, DetailAstImpl child) {
final DetailAstImpl ast = create(tokenType);
ast.addChild(child);
return ast;
}
}