SpringExtension.getApplicationContext declared as public

Issue: SPR-15340
This commit is contained in:
Juergen Hoeller
2017-03-14 15:18:44 +01:00
parent e999da0eb0
commit e4741b8e42

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -61,10 +61,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
ParameterResolver { ParameterResolver {
/** /**
* {@link Namespace} in which {@code TestContextManagers} are stored, keyed * {@link Namespace} in which {@code TestContextManagers} are stored,
* by test class. * keyed by test class.
*/ */
private static final Namespace namespace = Namespace.create(SpringExtension.class); private static final Namespace NAMESPACE = Namespace.create(SpringExtension.class);
/** /**
@@ -84,7 +84,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
getTestContextManager(context).afterTestClass(); getTestContextManager(context).afterTestClass();
} }
finally { finally {
context.getStore(namespace).remove(context.getTestClass().get()); context.getStore(NAMESPACE).remove(context.getTestClass().get());
} }
} }
@@ -139,17 +139,15 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
} }
/** /**
* Determine if the value for the {@link Parameter} in the supplied * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
* {@link ParameterContext} should be autowired from the test's * should be autowired from the test's {@link ApplicationContext}.
* {@link ApplicationContext}.
* <p>Returns {@code true} if the parameter is declared in a {@link Constructor} * <p>Returns {@code true} if the parameter is declared in a {@link Constructor}
* that is annotated with {@link Autowired @Autowired} and otherwise delegates * that is annotated with {@link Autowired @Autowired} and otherwise delegates to
* to {@link ParameterAutowireUtils#isAutowirable}. * {@link ParameterAutowireUtils#isAutowirable}.
* <p><strong>WARNING</strong>: if the parameter is declared in a {@code Constructor} * <p><strong>WARNING</strong>: If the parameter is declared in a {@code Constructor}
* that is annotated with {@code @Autowired}, Spring will assume the responsibility * that is annotated with {@code @Autowired}, Spring will assume the responsibility
* for resolving all parameters in the constructor. Consequently, no other * for resolving all parameters in the constructor. Consequently, no other registered
* registered {@link ParameterResolver} will be able to resolve parameters. * {@link ParameterResolver} will be able to resolve parameters.
*
* @see #resolve * @see #resolve
* @see ParameterAutowireUtils#isAutowirable * @see ParameterAutowireUtils#isAutowirable
*/ */
@@ -157,14 +155,14 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
Parameter parameter = parameterContext.getParameter(); Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable(); Executable executable = parameter.getDeclaringExecutable();
return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) return (executable instanceof Constructor &&
|| ParameterAutowireUtils.isAutowirable(parameter); AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) ||
ParameterAutowireUtils.isAutowirable(parameter);
} }
/** /**
* Resolve a value for the {@link Parameter} in the supplied * Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by
* {@link ParameterContext} by retrieving the corresponding dependency * retrieving the corresponding dependency from the test's {@link ApplicationContext}.
* from the test's {@link ApplicationContext}.
* <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}. * <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}.
* @see #supports * @see #supports
* @see ParameterAutowireUtils#resolveDependency * @see ParameterAutowireUtils#resolveDependency
@@ -177,28 +175,26 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext); return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
} }
/** /**
* Get the {@link ApplicationContext} associated with the supplied * Get the {@link ApplicationContext} associated with the supplied {@code ExtensionContext}.
* {@code ExtensionContext}. * @param context the current {@code ExtensionContext} (never {@code null})
* @param context the current {@code ExtensionContext}; never {@code null}
* @return the application context * @return the application context
* @throws IllegalStateException if an error occurs while retrieving the * @throws IllegalStateException if an error occurs while retrieving the application context
* application context
* @see org.springframework.test.context.TestContext#getApplicationContext() * @see org.springframework.test.context.TestContext#getApplicationContext()
*/ */
static ApplicationContext getApplicationContext(ExtensionContext context) { public static ApplicationContext getApplicationContext(ExtensionContext context) {
return getTestContextManager(context).getTestContext().getApplicationContext(); return getTestContextManager(context).getTestContext().getApplicationContext();
} }
/** /**
* Get the {@link TestContextManager} associated with the supplied * Get the {@link TestContextManager} associated with the supplied {@code ExtensionContext}.
* {@code ExtensionContext}. * @return the {@code TestContextManager} (never {@code null})
* @return the {@code TestContextManager}; never {@code null}
*/ */
private static TestContextManager getTestContextManager(ExtensionContext context) { private static TestContextManager getTestContextManager(ExtensionContext context) {
Assert.notNull(context, "ExtensionContext must not be null"); Assert.notNull(context, "ExtensionContext must not be null");
Class<?> testClass = context.getTestClass().get(); Class<?> testClass = context.getTestClass().get();
Store store = context.getStore(namespace); Store store = context.getStore(NAMESPACE);
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class); return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
} }