12
12
13
13
import static org .junit .platform .commons .support .AnnotationSupport .findAnnotatedMethods ;
14
14
import static org .junit .platform .commons .util .CollectionUtils .toUnmodifiableList ;
15
+ import static org .junit .platform .engine .support .discovery .DiscoveryIssueReporter .Condition .allOf ;
15
16
16
17
import java .lang .annotation .Annotation ;
17
18
import java .lang .reflect .Method ;
18
19
import java .util .List ;
19
- import java .util .function .Consumer ;
20
- import java .util .function .Function ;
21
- import java .util .function .Predicate ;
22
20
23
21
import org .junit .jupiter .api .AfterAll ;
24
22
import org .junit .jupiter .api .AfterEach ;
25
23
import org .junit .jupiter .api .BeforeAll ;
26
24
import org .junit .jupiter .api .BeforeEach ;
27
25
import org .junit .platform .commons .support .HierarchyTraversalMode ;
28
26
import org .junit .platform .commons .support .ModifierSupport ;
29
- import org .junit .platform .commons .util .Preconditions ;
30
27
import org .junit .platform .commons .util .ReflectionUtils ;
31
28
import org .junit .platform .engine .DiscoveryIssue ;
32
29
import org .junit .platform .engine .DiscoveryIssue .Severity ;
33
30
import org .junit .platform .engine .support .descriptor .MethodSource ;
34
31
import org .junit .platform .engine .support .discovery .DiscoveryIssueReporter ;
32
+ import org .junit .platform .engine .support .discovery .DiscoveryIssueReporter .Condition ;
35
33
36
34
/**
37
35
* Collection of utilities for working with test lifecycle methods.
@@ -70,7 +68,7 @@ private static List<Method> findMethodsAndCheckStatic(Class<?> testClass, boolea
70
68
Class <? extends Annotation > annotationType , HierarchyTraversalMode traversalMode ,
71
69
DiscoveryIssueReporter issueReporter ) {
72
70
73
- Predicate <Method > additionalCondition = requireStatic ? isStatic (annotationType , issueReporter ) : __ -> true ;
71
+ Condition <Method > additionalCondition = requireStatic ? isStatic (annotationType , issueReporter ) : __ -> true ;
74
72
return findMethodsAndCheckVoidReturnType (testClass , annotationType , traversalMode , issueReporter ,
75
73
additionalCondition );
76
74
}
@@ -85,46 +83,46 @@ private static List<Method> findMethodsAndCheckNonStatic(Class<?> testClass,
85
83
86
84
private static List <Method > findMethodsAndCheckVoidReturnType (Class <?> testClass ,
87
85
Class <? extends Annotation > annotationType , HierarchyTraversalMode traversalMode ,
88
- DiscoveryIssueReporter issueReporter , Predicate <? super Method > additionalCondition ) {
86
+ DiscoveryIssueReporter issueReporter , Condition <? super Method > additionalCondition ) {
89
87
90
88
return findAnnotatedMethods (testClass , annotationType , traversalMode ).stream () //
91
89
.peek (isNotPrivate (annotationType , issueReporter )) //
92
90
.filter (allOf (returnsPrimitiveVoid (annotationType , issueReporter ), additionalCondition )) //
93
91
.collect (toUnmodifiableList ());
94
92
}
95
93
96
- private static Predicate <Method > isStatic (Class <? extends Annotation > annotationType ,
94
+ private static Condition <Method > isStatic (Class <? extends Annotation > annotationType ,
97
95
DiscoveryIssueReporter issueReporter ) {
98
- return DiscoveryIssueReportingPredicate . from (ModifierSupport ::isStatic , issueReporter , method -> {
96
+ return issueReporter . createReportingCondition (ModifierSupport ::isStatic , method -> {
99
97
String message = String .format (
100
98
"@%s method '%s' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS)." ,
101
99
annotationType .getSimpleName (), method .toGenericString ());
102
100
return createIssue (Severity .ERROR , message , method );
103
101
});
104
102
}
105
103
106
- private static Predicate <Method > isNotStatic (Class <? extends Annotation > annotationType ,
104
+ private static Condition <Method > isNotStatic (Class <? extends Annotation > annotationType ,
107
105
DiscoveryIssueReporter issueReporter ) {
108
- return DiscoveryIssueReportingPredicate . from (ModifierSupport ::isNotStatic , issueReporter , method -> {
106
+ return issueReporter . createReportingCondition (ModifierSupport ::isNotStatic , method -> {
109
107
String message = String .format ("@%s method '%s' must not be static." , annotationType .getSimpleName (),
110
108
method .toGenericString ());
111
109
return createIssue (Severity .ERROR , message , method );
112
110
});
113
111
}
114
112
115
- private static Consumer <Method > isNotPrivate (Class <? extends Annotation > annotationType ,
113
+ private static Condition <Method > isNotPrivate (Class <? extends Annotation > annotationType ,
116
114
DiscoveryIssueReporter issueReporter ) {
117
- return DiscoveryIssueReportingPredicate . from (ModifierSupport ::isNotPrivate , issueReporter , method -> {
115
+ return issueReporter . createReportingCondition (ModifierSupport ::isNotPrivate , method -> {
118
116
String message = String .format (
119
117
"@%s method '%s' should not be private. This will be disallowed in a future release." ,
120
118
annotationType .getSimpleName (), method .toGenericString ());
121
119
return createIssue (Severity .DEPRECATION , message , method );
122
120
});
123
121
}
124
122
125
- private static Predicate <Method > returnsPrimitiveVoid (Class <? extends Annotation > annotationType ,
123
+ private static Condition <Method > returnsPrimitiveVoid (Class <? extends Annotation > annotationType ,
126
124
DiscoveryIssueReporter issueReporter ) {
127
- return DiscoveryIssueReportingPredicate . from (ReflectionUtils ::returnsPrimitiveVoid , issueReporter , method -> {
125
+ return issueReporter . createReportingCondition (ReflectionUtils ::returnsPrimitiveVoid , method -> {
128
126
String message = String .format ("@%s method '%s' must not return a value." , annotationType .getSimpleName (),
129
127
method .toGenericString ());
130
128
return createIssue (Severity .ERROR , message , method );
@@ -135,61 +133,4 @@ private static DiscoveryIssue createIssue(Severity severity, String message, Met
135
133
return DiscoveryIssue .builder (severity , message ).source (MethodSource .from (method )).build ();
136
134
}
137
135
138
- @ SafeVarargs
139
- @ SuppressWarnings ("varargs" )
140
- private static <T > Predicate <T > allOf (Predicate <? super T >... predicates ) {
141
- Preconditions .notNull (predicates , "predicates must not be null" );
142
- Preconditions .notEmpty (predicates , "predicates must not be empty" );
143
- Preconditions .containsNoNullElements (predicates , "predicates must not contain null elements" );
144
- return value -> {
145
- boolean result = true ;
146
- for (Predicate <? super T > predicate : predicates ) {
147
- result &= predicate .test (value );
148
- }
149
- return result ;
150
- };
151
- }
152
-
153
- private abstract static class DiscoveryIssueReportingPredicate <T > implements Predicate <T >, Consumer <T > {
154
-
155
- static <T > DiscoveryIssueReportingPredicate <T > from (Predicate <T > predicate , DiscoveryIssueReporter reporter ,
156
- Function <T , DiscoveryIssue > issueBuilder ) {
157
- return new DiscoveryIssueReportingPredicate <T >(reporter ) {
158
- @ Override
159
- protected boolean checkCondition (T value ) {
160
- return predicate .test (value );
161
- }
162
-
163
- @ Override
164
- protected DiscoveryIssue createIssue (T value ) {
165
- return issueBuilder .apply (value );
166
- }
167
- };
168
- }
169
-
170
- private final DiscoveryIssueReporter reporter ;
171
-
172
- protected DiscoveryIssueReportingPredicate (DiscoveryIssueReporter reporter ) {
173
- this .reporter = reporter ;
174
- }
175
-
176
- @ Override
177
- public final boolean test (T value ) {
178
- if (checkCondition (value )) {
179
- return true ;
180
- }
181
- reporter .reportIssue (createIssue (value ));
182
- return false ;
183
- }
184
-
185
- @ Override
186
- public void accept (T value ) {
187
- test (value );
188
- }
189
-
190
- protected abstract boolean checkCondition (T value );
191
-
192
- protected abstract DiscoveryIssue createIssue (T value );
193
- }
194
-
195
136
}
0 commit comments