Search in sources :

Example 1 with PropertyProvider

use of cn.taketoday.test.context.support.PropertyProvider in project today-framework by TAKETODAY.

the class ApplicationExtension method supportsParameter.

/**
 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
 * should be autowired from the test's {@link ApplicationContext}.
 * <p>A parameter is considered to be autowirable if one of the following
 * conditions is {@code true}.
 * <ol>
 * <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
 * executable} is a {@link Constructor} and
 * {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
 * returns {@code true}. Note that {@code isAutowirableConstructor()} will be
 * invoked with a fallback {@link PropertyProvider} that delegates its lookup
 * to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
 * <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
 * <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
 * <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
 * </ol>
 * <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
 * with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
 * Spring will assume the responsibility for resolving all parameters in the
 * constructor. Consequently, no other registered {@link ParameterResolver}
 * will be able to resolve parameters.
 *
 * @see #resolveParameter
 * @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
 * @see ParameterResolutionDelegate#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Parameter parameter = parameterContext.getParameter();
    Executable executable = parameter.getDeclaringExecutable();
    Class<?> testClass = extensionContext.getRequiredTestClass();
    PropertyProvider junitPropertyProvider = propertyName -> extensionContext.getConfigurationParameter(propertyName).orElse(null);
    return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) || ApplicationContext.class.isAssignableFrom(parameter.getType()) || supportsApplicationEvents(parameterContext) || ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) PropertyProvider(cn.taketoday.test.context.support.PropertyProvider) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Constructor(java.lang.reflect.Constructor) AfterAll(org.junit.jupiter.api.AfterAll) ParameterResolutionDelegate(cn.taketoday.beans.factory.annotation.ParameterResolutionDelegate) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) MergedAnnotations(cn.taketoday.core.annotation.MergedAnnotations) BeforeAll(org.junit.jupiter.api.BeforeAll) Parameter(java.lang.reflect.Parameter) SearchStrategy(cn.taketoday.core.annotation.MergedAnnotations.SearchStrategy) Executable(java.lang.reflect.Executable) Method(java.lang.reflect.Method) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) ApplicationContext(cn.taketoday.context.ApplicationContext) TestConstructorUtils(cn.taketoday.test.context.support.TestConstructorUtils) Autowired(cn.taketoday.beans.factory.annotation.Autowired) ReflectionUtils(cn.taketoday.util.ReflectionUtils) TestContext(cn.taketoday.test.context.TestContext) JUnitWebConfig(cn.taketoday.test.context.junit.jupiter.web.JUnitWebConfig) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) BeforeTestExecutionCallback(org.junit.jupiter.api.extension.BeforeTestExecutionCallback) Objects(java.util.Objects) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) RepeatableContainers(cn.taketoday.core.annotation.RepeatableContainers) TestContextManager(cn.taketoday.test.context.TestContextManager) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Modifier(java.lang.reflect.Modifier) ApplicationEvents(cn.taketoday.test.context.event.ApplicationEvents) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Annotation(java.lang.annotation.Annotation) Testable(org.junit.platform.commons.annotation.Testable) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) TestInstancePostProcessor(org.junit.jupiter.api.extension.TestInstancePostProcessor) TestConstructor(cn.taketoday.test.context.TestConstructor) PropertyProvider(cn.taketoday.test.context.support.PropertyProvider) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Example 2 with PropertyProvider

use of cn.taketoday.test.context.support.PropertyProvider in project today-infrastructure by TAKETODAY.

the class ApplicationExtension method supportsParameter.

/**
 * Determine if the value for the {@link Parameter} in the supplied {@link ParameterContext}
 * should be autowired from the test's {@link ApplicationContext}.
 * <p>A parameter is considered to be autowirable if one of the following
 * conditions is {@code true}.
 * <ol>
 * <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
 * executable} is a {@link Constructor} and
 * {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
 * returns {@code true}. Note that {@code isAutowirableConstructor()} will be
 * invoked with a fallback {@link PropertyProvider} that delegates its lookup
 * to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
 * <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
 * <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
 * <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
 * </ol>
 * <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
 * with {@code @Autowired} or automatically autowirable (see {@link TestConstructor}),
 * Spring will assume the responsibility for resolving all parameters in the
 * constructor. Consequently, no other registered {@link ParameterResolver}
 * will be able to resolve parameters.
 *
 * @see #resolveParameter
 * @see TestConstructorUtils#isAutowirableConstructor(Constructor, Class)
 * @see ParameterResolutionDelegate#isAutowirable
 */
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Parameter parameter = parameterContext.getParameter();
    Executable executable = parameter.getDeclaringExecutable();
    Class<?> testClass = extensionContext.getRequiredTestClass();
    PropertyProvider junitPropertyProvider = propertyName -> extensionContext.getConfigurationParameter(propertyName).orElse(null);
    return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) || ApplicationContext.class.isAssignableFrom(parameter.getType()) || supportsApplicationEvents(parameterContext) || ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) PropertyProvider(cn.taketoday.test.context.support.PropertyProvider) MethodFilter(cn.taketoday.util.ReflectionUtils.MethodFilter) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Constructor(java.lang.reflect.Constructor) AfterAll(org.junit.jupiter.api.AfterAll) ParameterResolutionDelegate(cn.taketoday.beans.factory.annotation.ParameterResolutionDelegate) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) MergedAnnotations(cn.taketoday.core.annotation.MergedAnnotations) BeforeAll(org.junit.jupiter.api.BeforeAll) Parameter(java.lang.reflect.Parameter) SearchStrategy(cn.taketoday.core.annotation.MergedAnnotations.SearchStrategy) Executable(java.lang.reflect.Executable) Method(java.lang.reflect.Method) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) ApplicationContext(cn.taketoday.context.ApplicationContext) TestConstructorUtils(cn.taketoday.test.context.support.TestConstructorUtils) Autowired(cn.taketoday.beans.factory.annotation.Autowired) ReflectionUtils(cn.taketoday.util.ReflectionUtils) TestContext(cn.taketoday.test.context.TestContext) JUnitWebConfig(cn.taketoday.test.context.junit.jupiter.web.JUnitWebConfig) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) BeforeTestExecutionCallback(org.junit.jupiter.api.extension.BeforeTestExecutionCallback) Objects(java.util.Objects) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) RepeatableContainers(cn.taketoday.core.annotation.RepeatableContainers) TestContextManager(cn.taketoday.test.context.TestContextManager) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Modifier(java.lang.reflect.Modifier) ApplicationEvents(cn.taketoday.test.context.event.ApplicationEvents) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) Annotation(java.lang.annotation.Annotation) Testable(org.junit.platform.commons.annotation.Testable) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) TestInstancePostProcessor(org.junit.jupiter.api.extension.TestInstancePostProcessor) TestConstructor(cn.taketoday.test.context.TestConstructor) PropertyProvider(cn.taketoday.test.context.support.PropertyProvider) Parameter(java.lang.reflect.Parameter) Executable(java.lang.reflect.Executable)

Aggregations

Autowired (cn.taketoday.beans.factory.annotation.Autowired)2 ParameterResolutionDelegate (cn.taketoday.beans.factory.annotation.ParameterResolutionDelegate)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2 MergedAnnotations (cn.taketoday.core.annotation.MergedAnnotations)2 SearchStrategy (cn.taketoday.core.annotation.MergedAnnotations.SearchStrategy)2 RepeatableContainers (cn.taketoday.core.annotation.RepeatableContainers)2 Assert (cn.taketoday.lang.Assert)2 Nullable (cn.taketoday.lang.Nullable)2 TestConstructor (cn.taketoday.test.context.TestConstructor)2 TestContext (cn.taketoday.test.context.TestContext)2 TestContextManager (cn.taketoday.test.context.TestContextManager)2 ApplicationEvents (cn.taketoday.test.context.event.ApplicationEvents)2 JUnitWebConfig (cn.taketoday.test.context.junit.jupiter.web.JUnitWebConfig)2 PropertyProvider (cn.taketoday.test.context.support.PropertyProvider)2 TestConstructorUtils (cn.taketoday.test.context.support.TestConstructorUtils)2 ReflectionUtils (cn.taketoday.util.ReflectionUtils)2 MethodFilter (cn.taketoday.util.ReflectionUtils.MethodFilter)2 Annotation (java.lang.annotation.Annotation)2 Constructor (java.lang.reflect.Constructor)2 Executable (java.lang.reflect.Executable)2