Search in sources :

Example 1 with WebMergedContextConfiguration

use of cn.taketoday.test.context.web.WebMergedContextConfiguration in project today-infrastructure by TAKETODAY.

the class ApplicationTestContextBootstrapper method processMergedContextConfiguration.

@Override
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
    Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
    List<String> propertySourceProperties = getAndProcessPropertySourceProperties(mergedConfig);
    mergedConfig = createModifiedConfig(mergedConfig, classes, StringUtils.toStringArray(propertySourceProperties));
    WebEnvironment webEnvironment = getWebEnvironment(mergedConfig.getTestClass());
    if (webEnvironment != null && isWebEnvironmentSupported(mergedConfig)) {
        ApplicationType webApplicationType = getApplicationType(mergedConfig);
        if (webApplicationType == ApplicationType.SERVLET_WEB && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
        } else if (webApplicationType == ApplicationType.REACTIVE_WEB && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            return new ReactiveWebMergedContextConfiguration(mergedConfig);
        }
    }
    return mergedConfig;
}
Also used : WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) ApplicationType(cn.taketoday.framework.ApplicationType) WebEnvironment(cn.taketoday.framework.test.context.ApplicationTest.WebEnvironment)

Example 2 with WebMergedContextConfiguration

use of cn.taketoday.test.context.web.WebMergedContextConfiguration in project today-infrastructure by TAKETODAY.

the class BootstrapTestUtilsMergedConfigTests method buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass.

/**
 * Introduced to investigate claims made in a discussion on
 * <a href="https://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
    Class<?> webTestClass = WebClassesFoo.class;
    Class<?> standardTestClass = ClassesFoo.class;
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
    MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);
    assertThat(webMergedConfig).isEqualTo(webMergedConfig);
    assertThat(standardMergedConfig).isEqualTo(standardMergedConfig);
    assertThat(webMergedConfig).isNotEqualTo(standardMergedConfig);
    assertThat(standardMergedConfig).isNotEqualTo(webMergedConfig);
    assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), WebDelegatingSmartContextLoader.class);
    assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), DelegatingSmartContextLoader.class);
}
Also used : WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) MergedContextConfiguration(cn.taketoday.test.context.MergedContextConfiguration) WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with WebMergedContextConfiguration

use of cn.taketoday.test.context.web.WebMergedContextConfiguration in project today-framework by TAKETODAY.

the class BootstrapTestUtilsMergedConfigTests method buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass.

/**
 * Introduced to investigate claims made in a discussion on
 * <a href="https://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
    Class<?> webTestClass = WebClassesFoo.class;
    Class<?> standardTestClass = ClassesFoo.class;
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
    MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);
    assertThat(webMergedConfig).isEqualTo(webMergedConfig);
    assertThat(standardMergedConfig).isEqualTo(standardMergedConfig);
    assertThat(webMergedConfig).isNotEqualTo(standardMergedConfig);
    assertThat(standardMergedConfig).isNotEqualTo(webMergedConfig);
    assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), WebDelegatingSmartContextLoader.class);
    assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class), DelegatingSmartContextLoader.class);
}
Also used : WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) MergedContextConfiguration(cn.taketoday.test.context.MergedContextConfiguration) WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with WebMergedContextConfiguration

use of cn.taketoday.test.context.web.WebMergedContextConfiguration in project today-framework by TAKETODAY.

the class ApplicationContextLoader method loadContext.

@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
    Class<?>[] configClasses = config.getClasses();
    String[] configLocations = config.getLocations();
    Assert.state(ObjectUtils.isNotEmpty(configClasses) || ObjectUtils.isNotEmpty(configLocations), () -> "No configuration classes or locations found in @SpringApplicationConfiguration. " + "For default configuration detection to work you need Spring 4.0.3 or better (found " + cn.taketoday.lang.Version.get() + ").");
    Application application = getSpringApplication();
    application.setMainApplicationClass(config.getTestClass());
    application.addPrimarySources(Arrays.asList(configClasses));
    application.getSources().addAll(Arrays.asList(configLocations));
    List<ApplicationContextInitializer> initializers = getInitializers(config, application);
    if (config instanceof WebMergedContextConfiguration) {
        application.setApplicationType(ApplicationType.SERVLET_WEB);
        if (!isEmbeddedWebEnvironment(config)) {
            new WebConfigurer().configure(config, application, initializers);
        }
    } else if (config instanceof ReactiveWebMergedContextConfiguration) {
        application.setApplicationType(ApplicationType.REACTIVE_WEB);
        if (!isEmbeddedWebEnvironment(config)) {
            application.setApplicationContextFactory(ApplicationContextFactory.from(GenericReactiveWebApplicationContext::new));
        }
    } else {
        application.setApplicationType(ApplicationType.NONE_WEB);
    }
    application.setInitializers(initializers);
    ConfigurableEnvironment environment = getEnvironment();
    if (environment != null) {
        prepareEnvironment(config, application, environment, false);
        application.setEnvironment(environment);
    } else {
        application.addListeners(new PrepareEnvironmentListener(config));
    }
    String[] args = ApplicationTestArgs.get(config.getContextCustomizers());
    return application.run(args);
}
Also used : GenericReactiveWebApplicationContext(cn.taketoday.framework.web.reactive.context.GenericReactiveWebApplicationContext) WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) ServletContextApplicationContextInitializer(cn.taketoday.framework.web.servlet.support.ServletContextApplicationContextInitializer) ApplicationContextInitializer(cn.taketoday.context.ApplicationContextInitializer) Application(cn.taketoday.framework.Application)

Example 5 with WebMergedContextConfiguration

use of cn.taketoday.test.context.web.WebMergedContextConfiguration in project today-framework by TAKETODAY.

the class ApplicationTestContextBootstrapper method processMergedContextConfiguration.

@Override
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
    Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
    List<String> propertySourceProperties = getAndProcessPropertySourceProperties(mergedConfig);
    mergedConfig = createModifiedConfig(mergedConfig, classes, StringUtils.toStringArray(propertySourceProperties));
    WebEnvironment webEnvironment = getWebEnvironment(mergedConfig.getTestClass());
    if (webEnvironment != null && isWebEnvironmentSupported(mergedConfig)) {
        ApplicationType webApplicationType = getApplicationType(mergedConfig);
        if (webApplicationType == ApplicationType.SERVLET_WEB && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            mergedConfig = new WebMergedContextConfiguration(mergedConfig, determineResourceBasePath(mergedConfig));
        } else if (webApplicationType == ApplicationType.REACTIVE_WEB && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) {
            return new ReactiveWebMergedContextConfiguration(mergedConfig);
        }
    }
    return mergedConfig;
}
Also used : WebMergedContextConfiguration(cn.taketoday.test.context.web.WebMergedContextConfiguration) ApplicationType(cn.taketoday.framework.ApplicationType) WebEnvironment(cn.taketoday.framework.test.context.ApplicationTest.WebEnvironment)

Aggregations

WebMergedContextConfiguration (cn.taketoday.test.context.web.WebMergedContextConfiguration)6 ApplicationContextInitializer (cn.taketoday.context.ApplicationContextInitializer)2 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)2 Application (cn.taketoday.framework.Application)2 ApplicationType (cn.taketoday.framework.ApplicationType)2 WebEnvironment (cn.taketoday.framework.test.context.ApplicationTest.WebEnvironment)2 GenericReactiveWebApplicationContext (cn.taketoday.framework.web.reactive.context.GenericReactiveWebApplicationContext)2 ServletContextApplicationContextInitializer (cn.taketoday.framework.web.servlet.support.ServletContextApplicationContextInitializer)2 MergedContextConfiguration (cn.taketoday.test.context.MergedContextConfiguration)2 Test (org.junit.jupiter.api.Test)2