Search in sources :

Example 1 with ApplicationContext

use of cn.taketoday.context.ApplicationContext in project today-infrastructure by TAKETODAY.

the class ConversionServiceDeducerTests method getConversionServiceWhenHasQualifiedConverterBeansContainsCustomizedApplicationService.

@Test
void getConversionServiceWhenHasQualifiedConverterBeansContainsCustomizedApplicationService() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CustomConverterConfiguration.class);
    ConversionServiceDeducer deducer = new ConversionServiceDeducer(applicationContext);
    List<ConversionService> conversionServices = deducer.getConversionServices();
    assertThat(conversionServices).hasSize(1);
    assertThat(conversionServices.get(0)).isNotSameAs(ApplicationConversionService.getSharedInstance());
    assertThat(conversionServices.get(0).canConvert(InputStream.class, OutputStream.class)).isTrue();
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ConversionService(cn.taketoday.core.conversion.ConversionService) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with ApplicationContext

use of cn.taketoday.context.ApplicationContext in project today-infrastructure by TAKETODAY.

the class ConversionServiceDeducerTests method getConversionServicesWhenHasConversionServiceBeanContainsOnlyBean.

@Test
void getConversionServicesWhenHasConversionServiceBeanContainsOnlyBean() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CustomConverterServiceConfiguration.class);
    ConversionServiceDeducer deducer = new ConversionServiceDeducer(applicationContext);
    TestApplicationConversionService expected = applicationContext.getBean(TestApplicationConversionService.class);
    assertThat(deducer.getConversionServices()).containsExactly(expected);
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test)

Example 3 with ApplicationContext

use of cn.taketoday.context.ApplicationContext in project today-infrastructure by TAKETODAY.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method requestScopeWithProxiedTargetClass.

@Test
void requestScopeWithProxiedTargetClass() {
    RequestContextHolder.set(oldRequestAttributes);
    ApplicationContext context = createContext(TARGET_CLASS);
    IScopedTestBean bean = (IScopedTestBean) context.getBean("request");
    // should be a class-based proxy
    assertThat(AopUtils.isCglibProxy(bean)).isTrue();
    boolean condition = bean instanceof RequestScopedTestBean;
    assertThat(condition).isTrue();
    assertThat(bean.getName()).isEqualTo(DEFAULT_NAME);
    bean.setName(MODIFIED_NAME);
    RequestContextHolder.set(newRequestAttributes);
    // this is a proxy so it should be reset to default
    assertThat(bean.getName()).isEqualTo(DEFAULT_NAME);
    RequestContextHolder.set(oldRequestAttributes);
    assertThat(bean.getName()).isEqualTo(MODIFIED_NAME);
}
Also used : GenericWebServletApplicationContext(cn.taketoday.web.context.support.GenericWebServletApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with ApplicationContext

use of cn.taketoday.context.ApplicationContext in project today-infrastructure by TAKETODAY.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method sessionScopeWithNoProxy.

@Test
void sessionScopeWithNoProxy() {
    RequestContextHolder.set(oldRequestAttributesWithSession);
    ApplicationContext context = createContext(NO);
    ScopedTestBean bean = (ScopedTestBean) context.getBean("session");
    // should not be a proxy
    assertThat(AopUtils.isAopProxy(bean)).isFalse();
    assertThat(bean.getName()).isEqualTo(DEFAULT_NAME);
    bean.setName(MODIFIED_NAME);
    RequestContextHolder.set(newRequestAttributesWithSession);
    // not a proxy so this should not have changed
    assertThat(bean.getName()).isEqualTo(MODIFIED_NAME);
    // but a newly retrieved bean should have the default name
    ScopedTestBean bean2 = (ScopedTestBean) context.getBean("session");
    assertThat(bean2.getName()).isEqualTo(DEFAULT_NAME);
}
Also used : GenericWebServletApplicationContext(cn.taketoday.web.context.support.GenericWebServletApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with ApplicationContext

use of cn.taketoday.context.ApplicationContext in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertiesBindExceptionTests method createFromBeanHasDetails.

@Test
void createFromBeanHasDetails() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example.class);
    ConfigurationPropertiesBean bean = ConfigurationPropertiesBean.get(applicationContext, applicationContext.getBean(Example.class), "example");
    ConfigurationPropertiesBindException exception = new ConfigurationPropertiesBindException(bean, new IllegalStateException());
    assertThat(exception.getNestedMessage()).isEqualTo("Error creating bean with name 'example': " + "Could not bind properties to 'ConfigurationPropertiesBindExceptionTests.Example' : " + "prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true; " + "Nested exception is java.lang.IllegalStateException");
    assertThat(exception.getBeanType()).isEqualTo(Example.class);
    assertThat(exception.getBeanName()).isEqualTo("example");
    assertThat(exception.getAnnotation()).isInstanceOf(ConfigurationProperties.class);
    assertThat(exception.getCause()).isInstanceOf(IllegalStateException.class);
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationContext (cn.taketoday.context.ApplicationContext)200 Test (org.junit.jupiter.api.Test)158 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)44 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)29 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)24 GenericWebServletApplicationContext (cn.taketoday.web.context.support.GenericWebServletApplicationContext)22 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)14 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)12 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)10 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)8 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)6 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)6 PropertySources (cn.taketoday.core.env.PropertySources)6 Resource (cn.taketoday.core.io.Resource)6 Nullable (cn.taketoday.lang.Nullable)6 FooService (example.scannable.FooService)6 ServletContext (jakarta.servlet.ServletContext)6 WebApplicationContext (cn.taketoday.web.WebApplicationContext)5 AutowireCapableBeanFactory (cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)4 Advised (cn.taketoday.aop.framework.Advised)3