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"));
}
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"));
}
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"));
}
Aggregations