Skip to content

Commit eaaddc7

Browse files
DavidBwilkinsona
DavidB
authored andcommitted
Simplify annotation handling in SpringJUnit5Check
Checkstyle's AnnotationUtil.containsAnnotation automatically handles simple and fully qualified names. For example, Override matches both java.lang.Override and Override. This means that SpringJUnit5Check need only care about the simple names of the annotations that it wants to detect. See gh-157
1 parent 7b76036 commit eaaddc7

File tree

1 file changed

+15
-30
lines changed
  • spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check

1 file changed

+15
-30
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringJUnit5Check.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,23 @@
3737
* @author Phillip Webb
3838
*/
3939
public class SpringJUnit5Check extends AbstractSpringCheck {
40-
4140
private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test";
4241

43-
private static final List<String> TEST_ANNOTATIONS;
44-
static {
45-
Set<String> annotations = new LinkedHashSet<>();
46-
addAnnotation(annotations, JUNIT4_TEST_ANNOTATION);
47-
addAnnotation(annotations, "org.junit.jupiter.api.RepeatedTest");
48-
addAnnotation(annotations, "org.junit.jupiter.api.Test");
49-
addAnnotation(annotations, "org.junit.jupiter.api.TestFactory");
50-
addAnnotation(annotations, "org.junit.jupiter.api.TestTemplate");
51-
addAnnotation(annotations, "org.junit.jupiter.params.ParameterizedTest");
52-
TEST_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
53-
}
54-
55-
private static final List<String> LIFECYCLE_ANNOTATIONS;
56-
static {
57-
Set<String> annotations = new LinkedHashSet<>();
58-
addAnnotation(annotations, "org.junit.jupiter.api.BeforeAll");
59-
addAnnotation(annotations, "org.junit.jupiter.api.BeforeEach");
60-
addAnnotation(annotations, "org.junit.jupiter.api.AfterAll");
61-
addAnnotation(annotations, "org.junit.jupiter.api.AfterEach");
62-
LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(new ArrayList<>(annotations));
63-
}
42+
private static final List<String> TEST_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
43+
"RepeatedTest",
44+
"Test",
45+
"TestFactory",
46+
"TestTemplate",
47+
"ParameterizedTest"
48+
));
49+
50+
private static final List<String> LIFECYCLE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
51+
"BeforeAll",
52+
"BeforeEach",
53+
"AfterAll",
54+
"AfterEach"
55+
)
56+
);
6457

6558
private static final Set<String> BANNED_IMPORTS;
6659
static {
@@ -75,14 +68,6 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
7568
BANNED_IMPORTS = Collections.unmodifiableSet(bannedImports);
7669
}
7770

78-
private static void addAnnotation(Set<String> annotations, String annotation) {
79-
annotations.add(annotation);
80-
int lastDot = annotation.lastIndexOf(".");
81-
if (lastDot != -1) {
82-
annotations.add(annotation.substring(lastDot + 1));
83-
}
84-
}
85-
8671
private List<String> unlessImports = new ArrayList<>();
8772

8873
private final List<DetailAST> testMethods = new ArrayList<>();

0 commit comments

Comments
 (0)