16
16
17
17
package org .springframework .test .context .junit .jupiter ;
18
18
19
- import java .lang .reflect .AnnotatedElement ;
20
19
import java .lang .reflect .Constructor ;
21
20
import java .lang .reflect .Executable ;
22
21
import java .lang .reflect .Method ;
23
22
import java .lang .reflect .Parameter ;
24
- import java .util .Optional ;
25
23
26
- import org .apache .commons .logging .Log ;
27
- import org .apache .commons .logging .LogFactory ;
28
24
import org .junit .jupiter .api .extension .AfterAllCallback ;
29
25
import org .junit .jupiter .api .extension .AfterEachCallback ;
30
26
import org .junit .jupiter .api .extension .AfterTestExecutionCallback ;
31
27
import org .junit .jupiter .api .extension .BeforeAllCallback ;
32
28
import org .junit .jupiter .api .extension .BeforeEachCallback ;
33
29
import org .junit .jupiter .api .extension .BeforeTestExecutionCallback ;
34
- import org .junit .jupiter .api .extension .ConditionEvaluationResult ;
35
- import org .junit .jupiter .api .extension .ContainerExecutionCondition ;
36
30
import org .junit .jupiter .api .extension .ContainerExtensionContext ;
37
31
import org .junit .jupiter .api .extension .ExtensionContext ;
38
32
import org .junit .jupiter .api .extension .ExtensionContext .Namespace ;
39
33
import org .junit .jupiter .api .extension .ExtensionContext .Store ;
40
34
import org .junit .jupiter .api .extension .ParameterContext ;
41
35
import org .junit .jupiter .api .extension .ParameterResolver ;
42
- import org .junit .jupiter .api .extension .TestExecutionCondition ;
43
36
import org .junit .jupiter .api .extension .TestExtensionContext ;
44
37
import org .junit .jupiter .api .extension .TestInstancePostProcessor ;
45
38
46
39
import org .springframework .beans .factory .annotation .Autowired ;
47
- import org .springframework .beans .factory .config .BeanExpressionContext ;
48
- import org .springframework .beans .factory .config .BeanExpressionResolver ;
49
- import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
50
40
import org .springframework .context .ApplicationContext ;
51
- import org .springframework .context .ConfigurableApplicationContext ;
52
41
import org .springframework .core .annotation .AnnotatedElementUtils ;
53
42
import org .springframework .test .context .TestContextManager ;
54
43
import org .springframework .util .Assert ;
61
50
* {@code @ExtendWith(SpringExtension.class)}.
62
51
*
63
52
* @author Sam Brannen
64
- * @author Tadaya Tsuyukubo
65
53
* @since 5.0
54
+ * @see org.springframework.test.context.junit.jupiter.DisabledIf
66
55
* @see org.springframework.test.context.junit.jupiter.SpringJUnitConfig
67
56
* @see org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
68
57
* @see org.springframework.test.context.TestContextManager
69
- * @see DisabledIf
70
58
*/
71
59
public class SpringExtension implements BeforeAllCallback , AfterAllCallback , TestInstancePostProcessor ,
72
60
BeforeEachCallback , AfterEachCallback , BeforeTestExecutionCallback , AfterTestExecutionCallback ,
73
- ParameterResolver ,
74
- ContainerExecutionCondition , TestExecutionCondition {
61
+ ParameterResolver {
75
62
76
63
/**
77
64
* {@link Namespace} in which {@code TestContextManagers} are stored, keyed
78
65
* by test class.
79
66
*/
80
67
private static final Namespace namespace = Namespace .create (SpringExtension .class );
81
68
82
- private static final ConditionEvaluationResult TEST_ENABLED = ConditionEvaluationResult .enabled (
83
- "@DisabledIf condition didn't match" );
84
-
85
- private static final Log logger = LogFactory .getLog (SpringExtension .class );
86
-
87
69
88
70
/**
89
71
* Delegates to {@link TestContextManager#beforeTestClass}.
@@ -195,61 +177,6 @@ public Object resolve(ParameterContext parameterContext, ExtensionContext extens
195
177
return ParameterAutowireUtils .resolveDependency (parameter , testClass , applicationContext );
196
178
}
197
179
198
- @ Override
199
- public ConditionEvaluationResult evaluate (ContainerExtensionContext context ) {
200
- return evaluateDisabledIf (context );
201
- }
202
-
203
- @ Override
204
- public ConditionEvaluationResult evaluate (TestExtensionContext context ) {
205
- return evaluateDisabledIf (context );
206
- }
207
-
208
- private ConditionEvaluationResult evaluateDisabledIf (ExtensionContext extensionContext ) {
209
- Optional <AnnotatedElement > element = extensionContext .getElement ();
210
- if (!element .isPresent ()) {
211
- return TEST_ENABLED ;
212
- }
213
-
214
- DisabledIf disabledIf = AnnotatedElementUtils .findMergedAnnotation (element .get (), DisabledIf .class );
215
- if (disabledIf == null ) {
216
- return TEST_ENABLED ;
217
- }
218
-
219
- String condition = disabledIf .condition ();
220
- if (condition .trim ().length () == 0 ) {
221
- return TEST_ENABLED ;
222
- }
223
-
224
- ApplicationContext applicationContext = getApplicationContext (extensionContext );
225
- if (!(applicationContext instanceof ConfigurableApplicationContext )) {
226
- return TEST_ENABLED ;
227
- }
228
-
229
- ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext ) applicationContext )
230
- .getBeanFactory ();
231
- BeanExpressionResolver expressionResolver = configurableBeanFactory .getBeanExpressionResolver ();
232
- BeanExpressionContext beanExpressionContext = new BeanExpressionContext (configurableBeanFactory , null );
233
-
234
- Object result = expressionResolver
235
- .evaluate (configurableBeanFactory .resolveEmbeddedValue (condition ), beanExpressionContext );
236
-
237
- if (result == null || !Boolean .valueOf (result .toString ())) {
238
- return TEST_ENABLED ;
239
- }
240
-
241
- String reason = disabledIf .reason ();
242
- if (reason .trim ().length () == 0 ) {
243
- String testTarget = extensionContext .getTestMethod ().map (Method ::getName )
244
- .orElseGet (() -> extensionContext .getTestClass ().get ().getSimpleName ());
245
- reason = String .format ("%s is disabled. condition=%s" , testTarget , condition );
246
- }
247
-
248
- logger .info (String .format ("%s is disabled. reason=%s" , element .get (), reason ));
249
- return ConditionEvaluationResult .disabled (reason );
250
-
251
- }
252
-
253
180
/**
254
181
* Get the {@link ApplicationContext} associated with the supplied
255
182
* {@code ExtensionContext}.
@@ -259,7 +186,7 @@ private ConditionEvaluationResult evaluateDisabledIf(ExtensionContext extensionC
259
186
* application context
260
187
* @see org.springframework.test.context.TestContext#getApplicationContext()
261
188
*/
262
- private ApplicationContext getApplicationContext (ExtensionContext context ) {
189
+ static ApplicationContext getApplicationContext (ExtensionContext context ) {
263
190
return getTestContextManager (context ).getTestContext ().getApplicationContext ();
264
191
}
265
192
@@ -268,7 +195,7 @@ private ApplicationContext getApplicationContext(ExtensionContext context) {
268
195
* {@code ExtensionContext}.
269
196
* @return the {@code TestContextManager}; never {@code null}
270
197
*/
271
- private TestContextManager getTestContextManager (ExtensionContext context ) {
198
+ private static TestContextManager getTestContextManager (ExtensionContext context ) {
272
199
Assert .notNull (context , "ExtensionContext must not be null" );
273
200
Class <?> testClass = context .getTestClass ().get ();
274
201
Store store = context .getStore (namespace );
0 commit comments