Search in sources :

Example 1 with MergedContextConfiguration

use of org.springframework.test.context.MergedContextConfiguration in project spring-boot by spring-projects.

the class SpringBootContextLoaderTests method getEnvironmentProperties.

private Map<String, Object> getEnvironmentProperties(Class<?> testClass) throws Exception {
    TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
    MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration");
    return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) TestContext(org.springframework.test.context.TestContext)

Example 2 with MergedContextConfiguration

use of org.springframework.test.context.MergedContextConfiguration in project spring-framework by spring-projects.

the class AbstractTestContextBootstrapper method buildMergedContextConfiguration.

/**
	 * Build the {@link MergedContextConfiguration merged context configuration}
	 * for the supplied {@link Class testClass}, context configuration attributes,
	 * and parent context configuration.
	 * @param testClass the test class for which the {@code MergedContextConfiguration}
	 * should be built (must not be {@code null})
	 * @param configAttributesList the list of context configuration attributes for the
	 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
	 * traversing up the class hierarchy); never {@code null} or empty
	 * @param parentConfig the merged context configuration for the parent application
	 * context in a context hierarchy, or {@code null} if there is no parent
	 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
	 * be passed to the {@code MergedContextConfiguration} constructor
	 * @param requireLocationsClassesOrInitializers whether locations, classes, or
	 * initializers are required; typically {@code true} but may be set to {@code false}
	 * if the configured loader supports empty configuration
	 * @return the merged context configuration
	 * @see #resolveContextLoader
	 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
	 * @see SmartContextLoader#processContextConfiguration
	 * @see ContextLoader#processLocations
	 * @see ActiveProfilesUtils#resolveActiveProfiles
	 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
	 * @see MergedContextConfiguration
	 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList, MergedContextConfiguration parentConfig, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate, boolean requireLocationsClassesOrInitializers) {
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");
    ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
    List<String> locations = new ArrayList<>();
    List<Class<?>> classes = new ArrayList<>();
    List<Class<?>> initializers = new ArrayList<>();
    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Processing locations and classes for context configuration attributes %s", configAttributes));
        }
        if (contextLoader instanceof SmartContextLoader) {
            SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
            smartContextLoader.processContextConfiguration(configAttributes);
            locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
            classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
        } else {
            String[] processedLocations = contextLoader.processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations());
            locations.addAll(0, Arrays.asList(processedLocations));
        // Legacy ContextLoaders don't know how to process classes
        }
        initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
        if (!configAttributes.isInheritLocations()) {
            break;
        }
    }
    Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass, Collections.unmodifiableList(configAttributesList));
    Assert.state(!(requireLocationsClassesOrInitializers && areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> String.format("%s was unable to detect defaults, and no ApplicationContextInitializers " + "or ContextCustomizers were declared for context configuration attributes %s", contextLoader.getClass().getSimpleName(), configAttributesList));
    MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass);
    MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes), ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList), ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(), mergedTestPropertySources.getProperties(), contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig);
    return processMergedContextConfiguration(mergedConfig);
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) ContextConfigurationAttributes(org.springframework.test.context.ContextConfigurationAttributes) ArrayList(java.util.ArrayList) SmartContextLoader(org.springframework.test.context.SmartContextLoader) SmartContextLoader(org.springframework.test.context.SmartContextLoader) ContextLoader(org.springframework.test.context.ContextLoader) ContextCustomizer(org.springframework.test.context.ContextCustomizer)

Example 3 with MergedContextConfiguration

use of org.springframework.test.context.MergedContextConfiguration in project spring-framework by spring-projects.

the class AbstractTestContextBootstrapper method buildMergedContextConfiguration.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public final MergedContextConfiguration buildMergedContextConfiguration() {
    Class<?> testClass = getBootstrapContext().getTestClass();
    CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = getCacheAwareContextLoaderDelegate();
    if (MetaAnnotationUtils.findAnnotationDescriptorForTypes(testClass, ContextConfiguration.class, ContextHierarchy.class) == null) {
        return buildDefaultMergedContextConfiguration(testClass, cacheAwareContextLoaderDelegate);
    }
    if (AnnotationUtils.findAnnotation(testClass, ContextHierarchy.class) != null) {
        Map<String, List<ContextConfigurationAttributes>> hierarchyMap = ContextLoaderUtils.buildContextHierarchyMap(testClass);
        MergedContextConfiguration parentConfig = null;
        MergedContextConfiguration mergedConfig = null;
        for (List<ContextConfigurationAttributes> list : hierarchyMap.values()) {
            List<ContextConfigurationAttributes> reversedList = new ArrayList<>(list);
            Collections.reverse(reversedList);
            // Don't use the supplied testClass; instead ensure that we are
            // building the MCC for the actual test class that declared the
            // configuration for the current level in the context hierarchy.
            Assert.notEmpty(reversedList, "ContextConfigurationAttributes list must not be empty");
            Class<?> declaringClass = reversedList.get(0).getDeclaringClass();
            mergedConfig = buildMergedContextConfiguration(declaringClass, reversedList, parentConfig, cacheAwareContextLoaderDelegate, true);
            parentConfig = mergedConfig;
        }
        // Return the last level in the context hierarchy
        return mergedConfig;
    } else {
        return buildMergedContextConfiguration(testClass, ContextLoaderUtils.resolveContextConfigurationAttributes(testClass), null, cacheAwareContextLoaderDelegate, true);
    }
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) ContextConfigurationAttributes(org.springframework.test.context.ContextConfigurationAttributes) CacheAwareContextLoaderDelegate(org.springframework.test.context.CacheAwareContextLoaderDelegate) ContextHierarchy(org.springframework.test.context.ContextHierarchy) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration)

Example 4 with MergedContextConfiguration

use of org.springframework.test.context.MergedContextConfiguration in project spring-framework by spring-projects.

the class DelegatingSmartContextLoaderTests method loadContextWithNullConfig.

// --- SmartContextLoader - loadContext() ----------------------------------
@Test(expected = IllegalArgumentException.class)
public void loadContextWithNullConfig() throws Exception {
    MergedContextConfiguration mergedConfig = null;
    loader.loadContext(mergedConfig);
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) Test(org.junit.Test)

Example 5 with MergedContextConfiguration

use of org.springframework.test.context.MergedContextConfiguration in project spring-framework by spring-projects.

the class DelegatingSmartContextLoaderTests method loadContextWithConfigurationClass.

@Test
public void loadContextWithConfigurationClass() throws Exception {
    MergedContextConfiguration mergedConfig = new MergedContextConfiguration(ConfigClassTestCase.class, EMPTY_STRING_ARRAY, new Class<?>[] { ConfigClassTestCase.Config.class }, EMPTY_STRING_ARRAY, loader);
    assertApplicationContextLoadsAndContainsFooString(mergedConfig);
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) Test(org.junit.Test)

Aggregations

MergedContextConfiguration (org.springframework.test.context.MergedContextConfiguration)32 Test (org.junit.Test)25 WebMergedContextConfiguration (org.springframework.test.context.web.WebMergedContextConfiguration)13 ArrayList (java.util.ArrayList)3 ContextConfigurationAttributes (org.springframework.test.context.ContextConfigurationAttributes)2 List (java.util.List)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 CacheAwareContextLoaderDelegate (org.springframework.test.context.CacheAwareContextLoaderDelegate)1 ContextConfiguration (org.springframework.test.context.ContextConfiguration)1 ContextCustomizer (org.springframework.test.context.ContextCustomizer)1 ContextHierarchy (org.springframework.test.context.ContextHierarchy)1 ContextLoader (org.springframework.test.context.ContextLoader)1 SmartContextLoader (org.springframework.test.context.SmartContextLoader)1 TestContext (org.springframework.test.context.TestContext)1