Search in sources :

Example 1 with RepositoryInterface

use of org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface in project spring-framework by spring-projects.

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");
    assertFalse(AopUtils.isAopProxy(shouldNotBeProxied));
    RepositoryInterface shouldBeProxied = (RepositoryInterface) gac.getBean("proxied");
    assertTrue(AopUtils.isAopProxy(shouldBeProxied));
    RepositoryWithoutInterface rwi = (RepositoryWithoutInterface) gac.getBean("classProxied");
    assertTrue(AopUtils.isAopProxy(rwi));
    checkWillTranslateExceptions(rwi);
    Additional rwi2 = (Additional) gac.getBean("classProxiedAndAdvised");
    assertTrue(AopUtils.isAopProxy(rwi2));
    rwi2.additionalMethod(false);
    checkWillTranslateExceptions(rwi2);
    try {
        rwi2.additionalMethod(true);
        fail("Should have thrown DataAccessResourceFailureException");
    } catch (DataAccessResourceFailureException ex) {
        assertEquals("my failure", ex.getMessage());
    }
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RepositoryInterfaceImpl(org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterfaceImpl) StereotypedRepositoryInterfaceImpl(org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl) AnnotationAwareAspectJAutoProxyCreator(org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator) StereotypedRepositoryInterfaceImpl(org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RepositoryInterface(org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 AnnotationAwareAspectJAutoProxyCreator (org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)1 RepositoryInterface (org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface)1 RepositoryInterfaceImpl (org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterfaceImpl)1 StereotypedRepositoryInterfaceImpl (org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl)1