|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.Arrays;
|
21 | 21 | import java.util.Collections;
|
| 22 | +import java.util.HashSet; |
22 | 23 | import java.util.LinkedHashMap;
|
23 | 24 | import java.util.LinkedHashSet;
|
24 | 25 | import java.util.List;
|
25 | 26 | import java.util.Map;
|
26 | 27 | import java.util.Set;
|
27 | 28 | import java.util.stream.Collectors;
|
| 29 | +import java.util.stream.Stream; |
28 | 30 |
|
29 | 31 | import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
30 | 32 | import com.puppycrawl.tools.checkstyle.api.FullIdent;
|
|
38 | 40 | */
|
39 | 41 | public class SpringJUnit5Check extends AbstractSpringCheck {
|
40 | 42 |
|
41 |
| - private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test"; |
| 43 | + private static final String JUNIT4_TEST_ANNOTATION_NAME = "org.junit.Test"; |
42 | 44 |
|
43 |
| - private static final List<String> TEST_ANNOTATIONS; |
| 45 | + private static final List<Annotation> TEST_ANNOTATIONS; |
44 | 46 | static {
|
45 |
| - Set<String> annotations = new LinkedHashSet<>(); |
46 |
| - annotations.add("RepeatedTest"); |
47 |
| - annotations.add("Test"); |
48 |
| - annotations.add("TestFactory"); |
49 |
| - annotations.add("TestTemplate"); |
50 |
| - annotations.add("ParameterizedTest"); |
| 47 | + Set<Annotation> annotations = new LinkedHashSet<>(); |
| 48 | + annotations.add(new Annotation("org.junit.jupiter.api", "RepeatedTest")); |
| 49 | + annotations.add(new Annotation("org.junit.jupiter.api", "Test")); |
| 50 | + annotations.add(new Annotation("org.junit.jupiter.api", "TestFactory")); |
| 51 | + annotations.add(new Annotation("org.junit.jupiter.api", "TestTemplate")); |
| 52 | + annotations.add(new Annotation("org.junit.jupiter.api", "ParameterizedTest")); |
51 | 53 | TEST_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
|
52 | 54 | }
|
53 | 55 |
|
54 |
| - private static final List<String> LIFECYCLE_ANNOTATIONS; |
| 56 | + private static final List<Annotation> LIFECYCLE_ANNOTATIONS; |
55 | 57 | static {
|
56 |
| - Set<String> annotations = new LinkedHashSet<>(); |
57 |
| - annotations.add("BeforeAll"); |
58 |
| - annotations.add("BeforeEach"); |
59 |
| - annotations.add("AfterAll"); |
60 |
| - annotations.add("AfterEach"); |
| 58 | + Set<Annotation> annotations = new LinkedHashSet<>(); |
| 59 | + annotations.add(new Annotation("org.junit.jupiter.api", "BeforeAll")); |
| 60 | + annotations.add(new Annotation("org.junit.jupiter.api", "BeforeEach")); |
| 61 | + annotations.add(new Annotation("org.junit.jupiter.api", "AfterAll")); |
| 62 | + annotations.add(new Annotation("org.junit.jupiter.api", "AfterEach")); |
61 | 63 | LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
|
62 | 64 | }
|
63 | 65 |
|
64 | 66 | private static final Set<String> BANNED_IMPORTS;
|
65 | 67 | static {
|
66 | 68 | Set<String> bannedImports = new LinkedHashSet<>();
|
67 |
| - bannedImports.add(JUNIT4_TEST_ANNOTATION); |
| 69 | + bannedImports.add(JUNIT4_TEST_ANNOTATION_NAME); |
68 | 70 | bannedImports.add("org.junit.After");
|
69 | 71 | bannedImports.add("org.junit.AfterClass");
|
70 | 72 | bannedImports.add("org.junit.Before");
|
@@ -106,14 +108,33 @@ public void visitToken(DetailAST ast) {
|
106 | 108 | }
|
107 | 109 |
|
108 | 110 | private void visitMethodDef(DetailAST ast) {
|
109 |
| - if (AnnotationUtil.containsAnnotation(ast, TEST_ANNOTATIONS)) { |
| 111 | + if (containsAnnotation(ast, TEST_ANNOTATIONS)) { |
110 | 112 | this.testMethods.add(ast);
|
111 | 113 | }
|
112 |
| - if (AnnotationUtil.containsAnnotation(ast, LIFECYCLE_ANNOTATIONS)) { |
| 114 | + if (containsAnnotation(ast, LIFECYCLE_ANNOTATIONS)) { |
113 | 115 | this.lifecycleMethods.add(ast);
|
114 | 116 | }
|
115 | 117 | }
|
116 | 118 |
|
| 119 | + private boolean containsAnnotation(DetailAST ast, List<Annotation> annotations) { |
| 120 | + List<String> annotationNames = annotations.stream().flatMap((annotation) -> |
| 121 | + Stream.of(annotation.simpleName, annotation.fullyQualifiedName())).collect(Collectors.toList()); |
| 122 | + try { |
| 123 | + return AnnotationUtil.containsAnnotation(ast, annotationNames); |
| 124 | + } |
| 125 | + catch (NoSuchMethodError ex) { |
| 126 | + // Checkstyle >= 10.3 (https://github.com/checkstyle/checkstyle/issues/14134) |
| 127 | + Set<String> annotationNamesSet = new HashSet<>(annotationNames); |
| 128 | + try { |
| 129 | + return (boolean) AnnotationUtil.class.getMethod("containsAnnotation", DetailAST.class, Set.class) |
| 130 | + .invoke(null, ast, annotationNamesSet); |
| 131 | + } |
| 132 | + catch (Exception ex2) { |
| 133 | + throw new RuntimeException("containsAnnotation failed", ex2); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
117 | 138 | private void visitImport(DetailAST ast) {
|
118 | 139 | FullIdent ident = FullIdent.createFullIdentBelow(ast);
|
119 | 140 | this.imports.put(ident.getText(), ident);
|
@@ -146,7 +167,7 @@ private void check() {
|
146 | 167 | }
|
147 | 168 | }
|
148 | 169 | for (DetailAST testMethod : this.testMethods) {
|
149 |
| - if (AnnotationUtil.containsAnnotation(testMethod, JUNIT4_TEST_ANNOTATION)) { |
| 170 | + if (AnnotationUtil.containsAnnotation(testMethod, JUNIT4_TEST_ANNOTATION_NAME)) { |
150 | 171 | log(testMethod, "junit5.bannedTestAnnotation");
|
151 | 172 | }
|
152 | 173 | }
|
@@ -176,4 +197,21 @@ public void setUnlessImports(String unlessImports) {
|
176 | 197 | .unmodifiableList(Arrays.stream(unlessImports.split(",")).map(String::trim).collect(Collectors.toList()));
|
177 | 198 | }
|
178 | 199 |
|
| 200 | + private static final class Annotation { |
| 201 | + |
| 202 | + private final String packageName; |
| 203 | + |
| 204 | + private final String simpleName; |
| 205 | + |
| 206 | + private Annotation(String packageName, String simpleName) { |
| 207 | + this.packageName = packageName; |
| 208 | + this.simpleName = simpleName; |
| 209 | + } |
| 210 | + |
| 211 | + private String fullyQualifiedName() { |
| 212 | + return this.packageName + "." + this.simpleName; |
| 213 | + } |
| 214 | + |
| 215 | + } |
| 216 | + |
179 | 217 | }
|
0 commit comments