Search in sources :

Example 1 with GenericApplicationContext

use of cn.taketoday.context.support.GenericApplicationContext in project today-framework by TAKETODAY.

the class AbstractMBeanServerTests method loadContext.

protected final ConfigurableApplicationContext loadContext(String configLocation) {
    GenericApplicationContext ctx = new GenericApplicationContext();
    new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(configLocation);
    ctx.getBeanFactory().registerSingleton("server", getServer());
    ctx.refresh();
    return ctx;
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) XmlBeanDefinitionReader(cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader)

Example 2 with GenericApplicationContext

use of cn.taketoday.context.support.GenericApplicationContext in project today-framework by TAKETODAY.

the class ScriptFactoryPostProcessorTests method testForRefreshedScriptHavingErrorPickedUpOnFirstCall.

@Test
void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
    BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
    BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
    BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
    collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
    ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
    final String collaboratorBeanName = "collaborator";
    ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
    ctx.refresh();
    Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    assertThat(messenger.getMessage()).isEqualTo(MESSAGE_TEXT);
    // cool; now let's change the script and check the refresh behaviour...
    pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
    StaticScriptSource source = getScriptSource(ctx);
    // needs The Sundays compiler; must NOT throw any exception here...
    source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
    Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    assertThatExceptionOfType(FatalBeanException.class).isThrownBy(refreshedMessenger::getMessage).matches(ex -> ex.contains(ScriptCompilationException.class));
}
Also used : BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) Messenger(cn.taketoday.scripting.Messenger) ScriptCompilationException(cn.taketoday.scripting.ScriptCompilationException) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with GenericApplicationContext

use of cn.taketoday.context.support.GenericApplicationContext in project today-framework by TAKETODAY.

the class PersistenceExceptionTranslationPostProcessorTests method proxiesCorrectly.

@Test
@SuppressWarnings("resource")
public void proxiesCorrectly() {
    GenericApplicationContext gac = new GenericApplicationContext();
    gac.registerBeanDefinition("translator", new RootBeanDefinition(PersistenceExceptionTranslationPostProcessor.class));
    gac.registerBeanDefinition("notProxied", new RootBeanDefinition(RepositoryInterfaceImpl.class));
    gac.registerBeanDefinition("proxied", new RootBeanDefinition(StereotypedRepositoryInterfaceImpl.class));
    gac.registerBeanDefinition("classProxied", new RootBeanDefinition(RepositoryWithoutInterface.class));
    gac.registerBeanDefinition("classProxiedAndAdvised", new RootBeanDefinition(RepositoryWithoutInterfaceAndOtherwiseAdvised.class));
    gac.registerBeanDefinition("myTranslator", new RootBeanDefinition(MyPersistenceExceptionTranslator.class));
    gac.registerBeanDefinition("proxyCreator", BeanDefinitionBuilder.rootBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class).addPropertyValue("order", 50).getBeanDefinition());
    gac.registerBeanDefinition("logger", new RootBeanDefinition(LogAllAspect.class));
    gac.refresh();
    RepositoryInterface shouldNotBeProxied = (RepositoryInterface) gac.getBean("notProxied");
    assertThat(AopUtils.isAopProxy(shouldNotBeProxied)).isFalse();
    RepositoryInterface shouldBeProxied = (RepositoryInterface) gac.getBean("proxied");
    assertThat(AopUtils.isAopProxy(shouldBeProxied)).isTrue();
    RepositoryWithoutInterface rwi = (RepositoryWithoutInterface) gac.getBean("classProxied");
    assertThat(AopUtils.isAopProxy(rwi)).isTrue();
    checkWillTranslateExceptions(rwi);
    Additional rwi2 = (Additional) gac.getBean("classProxiedAndAdvised");
    assertThat(AopUtils.isAopProxy(rwi2)).isTrue();
    rwi2.additionalMethod(false);
    checkWillTranslateExceptions(rwi2);
    assertThatExceptionOfType(DataAccessResourceFailureException.class).isThrownBy(() -> rwi2.additionalMethod(true)).withMessage("my failure");
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) StereotypedRepositoryInterfaceImpl(cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl) RepositoryInterfaceImpl(cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterfaceImpl) AnnotationAwareAspectJAutoProxyCreator(cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator) StereotypedRepositoryInterfaceImpl(cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) RepositoryInterface(cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface) Test(org.junit.jupiter.api.Test)

Example 4 with GenericApplicationContext

use of cn.taketoday.context.support.GenericApplicationContext in project today-framework by TAKETODAY.

the class AsyncExecutionTests method asyncInterfaceWithPostProcessor.

@Test
public void asyncInterfaceWithPostProcessor() throws Exception {
    originalThreadName = Thread.currentThread().getName();
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("asyncTest", new BeanDefinition(AsyncInterfaceBean.class));
    context.registerBeanDefinition("asyncProcessor", new BeanDefinition(AsyncAnnotationBeanPostProcessor.class));
    context.refresh();
    AsyncInterface asyncTest = context.getBean("asyncTest", AsyncInterface.class);
    asyncTest.doSomething(10);
    Future<String> future = asyncTest.returnSomething(20);
    assertThat(future.get()).isEqualTo("20");
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with GenericApplicationContext

use of cn.taketoday.context.support.GenericApplicationContext in project today-framework by TAKETODAY.

the class AsyncExecutionTests method asyncMethodListener.

@Test
public void asyncMethodListener() throws Exception {
    // Arrange
    originalThreadName = Thread.currentThread().getName();
    listenerCalled = 0;
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("asyncTest", new BeanDefinition(AsyncMethodListener.class));
    context.registerBeanDefinition("autoProxyCreator", new BeanDefinition(DefaultAdvisorAutoProxyCreator.class));
    context.registerBeanDefinition("asyncAdvisor", new BeanDefinition(AsyncAnnotationAdvisor.class));
    // Act
    context.refresh();
    // Assert
    Awaitility.await().atMost(1, TimeUnit.SECONDS).pollInterval(10, TimeUnit.MILLISECONDS).until(() -> listenerCalled == 1);
    context.close();
}
Also used : DefaultAdvisorAutoProxyCreator(cn.taketoday.aop.proxy.DefaultAdvisorAutoProxyCreator) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)147 Test (org.junit.jupiter.api.Test)141 ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)47 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)33 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)25 XmlBeanDefinitionReader (cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader)14 DefaultAdvisorAutoProxyCreator (cn.taketoday.aop.proxy.DefaultAdvisorAutoProxyCreator)13 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)7 AnnotationTypeFilter (cn.taketoday.core.type.filter.AnnotationTypeFilter)5 Messenger (cn.taketoday.scripting.Messenger)5 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 CommonAnnotationBeanPostProcessor (cn.taketoday.context.annotation.CommonAnnotationBeanPostProcessor)4 BootstrapContext (cn.taketoday.context.loader.BootstrapContext)4 FooServiceImpl (example.scannable.FooServiceImpl)4 EntityManager (jakarta.persistence.EntityManager)4 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)3 Qualifier (cn.taketoday.beans.factory.annotation.Qualifier)3 ConfigurationClassPostProcessor (cn.taketoday.context.annotation.ConfigurationClassPostProcessor)3