Search in sources :

Example 1 with ConfigurableApplicationContext

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

the class EnableAsyncTests method withAsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder.

@Test
public void withAsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder() throws Exception {
    System.setProperty("myExecutor", "myExecutor1");
    System.setProperty("my.app.myExecutor", "myExecutor2");
    Class<?> configClass = AsyncWithExecutorQualifiedByExpressionConfig.class;
    try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(configClass)) {
        AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder asyncBean = context.getBean(AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder.class);
        Future<Thread> workerThread1 = asyncBean.myWork1();
        assertThat(workerThread1.get().getName()).startsWith("myExecutor1-");
        Future<Thread> workerThread2 = asyncBean.myWork2();
        assertThat(workerThread2.get().getName()).startsWith("myExecutor2-");
    } finally {
        System.clearProperty("myExecutor");
        System.clearProperty("my.app.myExecutor");
    }
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurableApplicationContext

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

the class AsyncAnnotationBeanPostProcessorTests method invokedAsynchronously.

@Test
public void invokedAsynchronously() {
    ConfigurableApplicationContext context = initContext(new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class));
    ITestBean testBean = context.getBean("target", ITestBean.class);
    testBean.test();
    Thread mainThread = Thread.currentThread();
    testBean.await(3000);
    Thread asyncThread = testBean.getThread();
    assertThat(asyncThread).isNotSameAs(mainThread);
    context.close();
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurableApplicationContext

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

the class AsyncAnnotationBeanPostProcessorTests method handleExceptionWithFuture.

@Test
@SuppressWarnings("resource")
public void handleExceptionWithFuture() {
    ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithExceptionHandler.class);
    ITestBean testBean = context.getBean("target", ITestBean.class);
    TestableAsyncUncaughtExceptionHandler exceptionHandler = context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class);
    assertThat(exceptionHandler.isCalled()).as("handler should not have been called yet").isFalse();
    Future<Object> result = testBean.failWithFuture();
    assertFutureWithException(result, exceptionHandler);
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurableApplicationContext

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

the class AsyncAnnotationBeanPostProcessorTests method handleExceptionWithCustomExceptionHandler.

@Test
public void handleExceptionWithCustomExceptionHandler() {
    Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
    TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler();
    BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
    processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler);
    ConfigurableApplicationContext context = initContext(processorDefinition);
    ITestBean testBean = context.getBean("target", ITestBean.class);
    assertThat(exceptionHandler.isCalled()).as("Handler should not have been called").isFalse();
    testBean.failWithVoid();
    exceptionHandler.await(3000);
    exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Method(java.lang.reflect.Method) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurableApplicationContext

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

the class ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests method bootstrapsApplicationContext.

@Test
void bootstrapsApplicationContext() {
    String args = "--context.config.import=classpath:application-bootstrap-registry-integration-tests.properties";
    try (ConfigurableApplicationContext context = application.run(args)) {
        LoaderHelper bean = context.getBean(TestConfigDataBootstrap.LoaderHelper.class);
        assertThat(bean).isNotNull();
        assertThat(bean.getBound()).isEqualTo("igotbound");
        assertThat(bean.getProfileBound()).isEqualTo("igotprofilebound");
        assertThat(bean.getLocation().getResolverHelper().getLocation()).isEqualTo(ConfigDataLocation.valueOf("testbootstrap:test"));
    }
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) LoaderHelper(cn.taketoday.framework.context.config.TestConfigDataBootstrap.LoaderHelper) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)274 Test (org.junit.jupiter.api.Test)252 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)28 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)24 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)20 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)12 MBeanServer (javax.management.MBeanServer)12 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)10 MergedContextConfiguration (cn.taketoday.test.context.MergedContextConfiguration)10 CacheInterceptor (cn.taketoday.cache.interceptor.CacheInterceptor)8 DefaultJCacheOperationSource (cn.taketoday.cache.jcache.interceptor.DefaultJCacheOperationSource)8 ApplicationContext (cn.taketoday.context.ApplicationContext)8 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)8 GenericXmlApplicationContext (cn.taketoday.context.support.GenericXmlApplicationContext)8 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)8 ObjectName (javax.management.ObjectName)8 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)6 ArrayList (java.util.ArrayList)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 CacheManager (cn.taketoday.cache.CacheManager)4