2020import java .util .Arrays ;
2121import java .util .Collections ;
2222import java .util .LinkedHashMap ;
23+ import java .util .LinkedHashSet ;
2324import java .util .List ;
2425import java .util .Map ;
26+ import java .util .Set ;
2527import java .util .stream .Collectors ;
2628
2729import com .puppycrawl .tools .checkstyle .api .DetailAST ;
@@ -38,29 +40,44 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
3840
3941 private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test" ;
4042
41- private static final String JUNIT5_TEST_ANNOTATION = "org.junit.jupiter.api.Test" ;
42-
43- private static final String JUNIT5_TEST_TEMPLATE_ANNOTATION = "org.junit.jupiter.api.TestTemplate" ;
44-
45- private static final List <String > TEST_ANNOTATIONS = Collections
46- .unmodifiableList (Arrays .asList ("Test" , "TestTemplate" , JUNIT4_TEST_ANNOTATION , JUNIT5_TEST_ANNOTATION ,
47- JUNIT5_TEST_TEMPLATE_ANNOTATION ));
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.Test" );
48+ addAnnotation (annotations , "org.junit.jupiter.api.TestTemplate" );
49+ TEST_ANNOTATIONS = Collections .unmodifiableList (new ArrayList <>(annotations ));
50+ }
4851
49- private static final List <String > LIFECYCLE_ANNOTATIONS = Collections .unmodifiableList (Arrays .asList ("BeforeAll" ,
50- "org.junit.jupiter.api.BeforeAll" , "BeforeEach" , "org.junit.jupiter.api.BeforeEach" , "AfterAll" ,
51- "org.junit.jupiter.api.AfterAll" , "AfterEach" , "org.junit.jupiter.api.AfterEach" ));
52+ private static final List <String > LIFECYCLE_ANNOTATIONS ;
53+ static {
54+ Set <String > annotations = new LinkedHashSet <>();
55+ addAnnotation (annotations , "org.junit.jupiter.api.BeforeAll" );
56+ addAnnotation (annotations , "org.junit.jupiter.api.BeforeEach" );
57+ addAnnotation (annotations , "org.junit.jupiter.api.AfterAll" );
58+ addAnnotation (annotations , "org.junit.jupiter.api.AfterEach" );
59+ LIFECYCLE_ANNOTATIONS = Collections .unmodifiableList (new ArrayList <>(annotations ));
60+ }
5261
53- private static final List <String > BANNED_IMPORTS ;
62+ private static final Set <String > BANNED_IMPORTS ;
5463 static {
55- List <String > bannedImports = new ArrayList <>();
64+ Set <String > bannedImports = new LinkedHashSet <>();
5665 bannedImports .add (JUNIT4_TEST_ANNOTATION );
5766 bannedImports .add ("org.junit.After" );
5867 bannedImports .add ("org.junit.AfterClass" );
5968 bannedImports .add ("org.junit.Before" );
6069 bannedImports .add ("org.junit.BeforeClass" );
6170 bannedImports .add ("org.junit.Rule" );
6271 bannedImports .add ("org.junit.ClassRule" );
63- BANNED_IMPORTS = Collections .unmodifiableList (bannedImports );
72+ BANNED_IMPORTS = Collections .unmodifiableSet (bannedImports );
73+ }
74+
75+ private static void addAnnotation (Set <String > annotations , String annotation ) {
76+ annotations .add (annotation );
77+ int lastDot = annotation .lastIndexOf ("." );
78+ if (lastDot != -1 ) {
79+ annotations .add (annotation .substring (lastDot + 1 ));
80+ }
6481 }
6582
6683 private List <String > unlessImports = new ArrayList <>();
0 commit comments