Search in sources :

Example 1 with MyThrowsHandler

use of org.springframework.tests.aop.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testThrowsAdvisorIsInvoked.

@Test
public void testThrowsAdvisorIsInvoked() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    @SuppressWarnings("serial") Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {

        @Override
        public boolean matches(Method m, Class<?> targetClass) {
            return m.getName().startsWith("echo");
        }
    };
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvisor(matchesEchoInvocations);
    assertEquals("Advisor was added", matchesEchoInvocations, pf.getAdvisors()[1]);
    IEcho proxied = (IEcho) createProxy(pf);
    assertEquals(0, th.getCalls());
    assertEquals(target.getA(), proxied.getA());
    assertEquals(0, th.getCalls());
    Exception ex = new Exception();
    // Will be advised but doesn't match
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (Exception caught) {
        assertEquals(ex, caught);
    }
    ex = new FileNotFoundException();
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (FileNotFoundException caught) {
        assertEquals(ex, caught);
    }
    assertEquals(1, th.getCalls("ioException"));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) FileNotFoundException(java.io.FileNotFoundException) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Method(java.lang.reflect.Method) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 2 with MyThrowsHandler

use of org.springframework.tests.aop.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.

@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvice(th);
    IEcho proxied = (IEcho) createProxy(pf);
    assertEquals(0, th.getCalls());
    assertEquals(target.getA(), proxied.getA());
    assertEquals(0, th.getCalls());
    Exception ex = new Exception();
    // Will be advised but doesn't match
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (Exception caught) {
        assertEquals(ex, caught);
    }
    // Subclass of RemoteException
    ex = new MarshalException("");
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (MarshalException caught) {
        assertEquals(ex, caught);
    }
    assertEquals(1, th.getCalls("remoteException"));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MarshalException(java.rmi.MarshalException) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 3 with MyThrowsHandler

use of org.springframework.tests.aop.advice.MyThrowsHandler in project spring-framework by spring-projects.

the class ProxyFactoryBeanTests method testCanAddThrowsAdviceWithoutAdvisor.

@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
    DefaultListableBeanFactory f = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
    MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
    CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
    assertEquals(0, cba.getCalls());
    assertEquals(0, th.getCalls());
    IEcho echo = (IEcho) f.getBean("throwsAdvised");
    int i = 12;
    echo.setA(i);
    assertEquals(i, echo.getA());
    assertEquals(2, cba.getCalls());
    assertEquals(0, th.getCalls());
    Exception expected = new Exception();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (Exception ex) {
        assertEquals(expected, ex);
    }
    // No throws handler method: count should still be 0
    assertEquals(0, th.getCalls());
    // Handler knows how to handle this exception
    expected = new FileNotFoundException();
    try {
        echo.echoException(1, expected);
        fail();
    } catch (IOException ex) {
        assertEquals(expected, ex);
    }
    // One match
    assertEquals(1, th.getCalls("ioException"));
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LockedException(test.mixin.LockedException) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 MyThrowsHandler (org.springframework.tests.aop.advice.MyThrowsHandler)3 LockedException (test.mixin.LockedException)3 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 MarshalException (java.rmi.MarshalException)2 SQLException (java.sql.SQLException)2 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)2 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)2 Method (java.lang.reflect.Method)1 Advisor (org.springframework.aop.Advisor)1 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)1 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)1 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)1