use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ThrowsAdviceInterceptorTests method testNoHandlerMethodForThrowable.
@Test
public void testNoHandlerMethodForThrowable() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
assertThat(ti.getHandlerMethodCount()).isEqualTo(2);
Exception ex = new Exception();
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
assertThat(th.getCalls()).isEqualTo(0);
}
use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ThrowsAdviceInterceptorTests method testHandlerMethodThrowsException.
@Test
public void testHandlerMethodThrowsException() throws Throwable {
final Throwable t = new Throwable();
@SuppressWarnings("serial") MyThrowsHandler th = new MyThrowsHandler() {
@Override
public void afterThrowing(RemoteException ex) throws Throwable {
super.afterThrowing(ex);
throw t;
}
};
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
// Extends RemoteException
ConnectException ex = new ConnectException("");
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(t);
assertThat(th.getCalls()).isEqualTo(1);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
use of org.springframework.aop.testfixture.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, @Nullable 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);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesEchoInvocations);
IEcho proxied = (IEcho) createProxy(pf);
assertThat(th.getCalls()).isEqualTo(0);
assertThat(proxied.getA()).isEqualTo(target.getA());
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
FileNotFoundException fex = new FileNotFoundException();
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> proxied.echoException(1, fex)).matches(fex::equals);
assertThat(th.getCalls("ioException")).isEqualTo(1);
}
use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ThrowsAdviceInterceptorTests method testCorrectHandlerUsedForSubclass.
@Test
public void testCorrectHandlerUsedForSubclass() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
// Extends RemoteException
ConnectException ex = new ConnectException("");
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
assertThatExceptionOfType(ConnectException.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
assertThat(th.getCalls()).isEqualTo(1);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ThrowsAdviceInterceptorTests method testNotInvoked.
@Test
public void testNotInvoked() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
Object ret = new Object();
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willReturn(ret);
assertThat(ti.invoke(mi)).isEqualTo(ret);
assertThat(th.getCalls()).isEqualTo(0);
}
Aggregations