Search in sources :

Example 1 with RepositoryInterface

use of cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface 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 2 with RepositoryInterface

use of cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface in project today-infrastructure 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)

Aggregations

AnnotationAwareAspectJAutoProxyCreator (cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)2 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)2 RepositoryInterface (cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface)2 RepositoryInterfaceImpl (cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterfaceImpl)2 StereotypedRepositoryInterfaceImpl (cn.taketoday.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl)2 Test (org.junit.jupiter.api.Test)2