20
20
import java .util .Arrays ;
21
21
import java .util .Collections ;
22
22
import java .util .LinkedHashMap ;
23
+ import java .util .LinkedHashSet ;
23
24
import java .util .List ;
24
25
import java .util .Map ;
26
+ import java .util .Set ;
25
27
import java .util .stream .Collectors ;
26
28
27
29
import com .puppycrawl .tools .checkstyle .api .DetailAST ;
@@ -38,29 +40,44 @@ public class SpringJUnit5Check extends AbstractSpringCheck {
38
40
39
41
private static final String JUNIT4_TEST_ANNOTATION = "org.junit.Test" ;
40
42
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
+ }
48
51
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
+ }
52
61
53
- private static final List <String > BANNED_IMPORTS ;
62
+ private static final Set <String > BANNED_IMPORTS ;
54
63
static {
55
- List <String > bannedImports = new ArrayList <>();
64
+ Set <String > bannedImports = new LinkedHashSet <>();
56
65
bannedImports .add (JUNIT4_TEST_ANNOTATION );
57
66
bannedImports .add ("org.junit.After" );
58
67
bannedImports .add ("org.junit.AfterClass" );
59
68
bannedImports .add ("org.junit.Before" );
60
69
bannedImports .add ("org.junit.BeforeClass" );
61
70
bannedImports .add ("org.junit.Rule" );
62
71
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
+ }
64
81
}
65
82
66
83
private List <String > unlessImports = new ArrayList <>();
0 commit comments