Search in sources :

Example 1 with AutowireMode

use of cn.taketoday.test.context.TestConstructor.AutowireMode in project today-infrastructure by TAKETODAY.

the class TestConstructorUtils method isAutowirableConstructor.

/**
 * Determine if the supplied constructor for the given test class is
 * autowirable.
 *
 * <p>A constructor is considered to be autowirable if one of the following
 * conditions is {@code true}.
 *
 * <ol>
 * <li>The constructor is annotated with {@link Autowired @Autowired}.</li>
 * <li>{@link TestConstructor @TestConstructor} is <em>present</em> or
 * <em>meta-present</em> on the test class with
 * {@link TestConstructor#autowireMode() autowireMode} set to
 * {@link AutowireMode#ALL ALL}.</li>
 * <li>The default <em>test constructor autowire mode</em> has been set to
 * {@code ALL} in {@link TodayStrategies} or in the supplied fallback
 * {@link PropertyProvider} (see
 * {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
 * </ol>
 *
 * @param constructor a constructor for the test class
 * @param testClass the test class
 * @param fallbackPropertyProvider fallback property provider used to look up
 * the value for the default <em>test constructor autowire mode</em> if no
 * such value is found in {@link TodayStrategies}
 * @return {@code true} if the constructor is autowirable
 * @since 4.0
 */
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass, @Nullable PropertyProvider fallbackPropertyProvider) {
    // Is the constructor annotated with @Autowired?
    if (AnnotatedElementUtils.hasAnnotation(constructor, Autowired.class)) {
        return true;
    }
    AutowireMode autowireMode = null;
    // Is the test class annotated with @TestConstructor?
    TestConstructor testConstructor = TestContextAnnotationUtils.findMergedAnnotation(testClass, TestConstructor.class);
    if (testConstructor != null) {
        autowireMode = testConstructor.autowireMode();
    } else {
        // Custom global default from TodayStrategies?
        String value = TodayStrategies.getProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
        autowireMode = AutowireMode.from(value);
        // Use fallback provider?
        if (autowireMode == null && fallbackPropertyProvider != null) {
            value = fallbackPropertyProvider.get(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
            autowireMode = AutowireMode.from(value);
        }
    }
    return (autowireMode == AutowireMode.ALL);
}
Also used : AutowireMode(cn.taketoday.test.context.TestConstructor.AutowireMode) TestConstructor(cn.taketoday.test.context.TestConstructor)

Example 2 with AutowireMode

use of cn.taketoday.test.context.TestConstructor.AutowireMode in project today-framework by TAKETODAY.

the class TestConstructorUtils method isAutowirableConstructor.

/**
 * Determine if the supplied constructor for the given test class is
 * autowirable.
 *
 * <p>A constructor is considered to be autowirable if one of the following
 * conditions is {@code true}.
 *
 * <ol>
 * <li>The constructor is annotated with {@link Autowired @Autowired}.</li>
 * <li>{@link TestConstructor @TestConstructor} is <em>present</em> or
 * <em>meta-present</em> on the test class with
 * {@link TestConstructor#autowireMode() autowireMode} set to
 * {@link AutowireMode#ALL ALL}.</li>
 * <li>The default <em>test constructor autowire mode</em> has been set to
 * {@code ALL} in {@link TodayStrategies} or in the supplied fallback
 * {@link PropertyProvider} (see
 * {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
 * </ol>
 *
 * @param constructor a constructor for the test class
 * @param testClass the test class
 * @param fallbackPropertyProvider fallback property provider used to look up
 * the value for the default <em>test constructor autowire mode</em> if no
 * such value is found in {@link TodayStrategies}
 * @return {@code true} if the constructor is autowirable
 * @since 4.0
 */
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass, @Nullable PropertyProvider fallbackPropertyProvider) {
    // Is the constructor annotated with @Autowired?
    if (AnnotatedElementUtils.hasAnnotation(constructor, Autowired.class)) {
        return true;
    }
    AutowireMode autowireMode = null;
    // Is the test class annotated with @TestConstructor?
    TestConstructor testConstructor = TestContextAnnotationUtils.findMergedAnnotation(testClass, TestConstructor.class);
    if (testConstructor != null) {
        autowireMode = testConstructor.autowireMode();
    } else {
        // Custom global default from TodayStrategies?
        String value = TodayStrategies.getProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
        autowireMode = AutowireMode.from(value);
        // Use fallback provider?
        if (autowireMode == null && fallbackPropertyProvider != null) {
            value = fallbackPropertyProvider.get(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
            autowireMode = AutowireMode.from(value);
        }
    }
    return (autowireMode == AutowireMode.ALL);
}
Also used : AutowireMode(cn.taketoday.test.context.TestConstructor.AutowireMode) TestConstructor(cn.taketoday.test.context.TestConstructor)

Aggregations

TestConstructor (cn.taketoday.test.context.TestConstructor)2 AutowireMode (cn.taketoday.test.context.TestConstructor.AutowireMode)2