@Test public void testCorrectHandlerUsed() throws Throwable { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); FileNotFoundException ex = new FileNotFoundException(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode")); given(mi.getThis()).willReturn(new Object()); given(mi.proceed()).willThrow(ex); try { ti.invoke(mi); fail(); } catch (Exception caught) { assertEquals(ex, caught); } assertEquals(1, th.getCalls()); assertEquals(1, th.getCalls("ioException")); }
@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); try { ti.invoke(mi); fail(); } catch (Throwable caught) { assertEquals(t, caught); } assertEquals(1, th.getCalls()); assertEquals(1, th.getCalls("remoteException")); }
@Test public void testNoHandlerMethodForThrowable() throws Throwable { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); assertEquals(2, ti.getHandlerMethodCount()); Exception ex = new Exception(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willThrow(ex); try { ti.invoke(mi); fail(); } catch (Exception caught) { assertEquals(ex, caught); } assertEquals(0, th.getCalls()); }
@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); try { ti.invoke(mi); fail(); } catch (Exception caught) { assertEquals(ex, caught); } assertEquals(1, th.getCalls()); assertEquals(1, th.getCalls("remoteException")); }
public void afterThrowing(RemoteException ex) throws Throwable { count("remoteException"); }
public void afterThrowing(Method m, Object[] args, Object target, IOException ex) { count("ioException"); }
@Override public void afterThrowing(RemoteException ex) throws Throwable { super.afterThrowing(ex); throw t; } };
@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); assertEquals(ret, ti.invoke(mi)); assertEquals(0, th.getCalls()); }