@Test public void testInvoke() throws NoSuchMethodException, SecurityException, Throwable { Container container = mock(Container.class); Map<String, Interceptor> interceptors = new LinkedHashMap<>(); interceptors.put("interceptor-1", new Interceptor1().result("result-1").applies(true)); interceptors.put("interceptor-2", new Interceptor1().result("result-2").applies(true)); doReturn(interceptors).when(container).interceptors(); Prodigy.container(container); ClientInvocationHandler handler = new ClientInvocationHandler(new Object()); Object result = handler.invoke(null, Object.class.getMethod("toString"), null); assertThat(result, is("result-1")); assertTrue(((Interceptor1) interceptors.get("interceptor-1")).invoked()); assertFalse(((Interceptor1) interceptors.get("interceptor-2")).invoked()); container = mock(Container.class); interceptors = new LinkedHashMap<>(); interceptors.put("interceptor-3", new Interceptor1().throwable(new IllegalStateException()).applies(true)); interceptors.put("interceptor-4", new Interceptor1().throwable(new IllegalArgumentException()).applies(true)); doReturn(interceptors).when(container).interceptors(); Prodigy.container(container); handler = new ClientInvocationHandler(new Object()); try { handler.invoke(null, Object.class.getMethod("toString"), null); fail(); } catch (IllegalStateException e) {} assertTrue(((Interceptor1) interceptors.get("interceptor-3")).invoked()); assertFalse(((Interceptor1) interceptors.get("interceptor-4")).invoked()); container = mock(Container.class); interceptors = new LinkedHashMap<>(); interceptors.put("interceptor-5", new Interceptor1().result("result-5").applies(false)); interceptors.put("interceptor-6", new Interceptor1().result("result-6").applies(false)); doReturn(interceptors).when(container).interceptors(); Prodigy.container(container); handler = new ClientInvocationHandler("hello"); result = handler.invoke(null, String.class.getMethod("toString"), null); assertThat(result, is("hello")); assertTrue(((Interceptor1) interceptors.get("interceptor-5")).invoked()); assertTrue(((Interceptor1) interceptors.get("interceptor-6")).invoked()); container = mock(Container.class); interceptors = new LinkedHashMap<>(); interceptors.put("interceptor-7", new Interceptor1().throwable(new IllegalStateException()).applies(false).proceeds(true)); interceptors.put("interceptor-8", new Interceptor1().throwable(new IllegalArgumentException()).applies(false)); doReturn(interceptors).when(container).interceptors(); Prodigy.container(container); handler = new ClientInvocationHandler(new Object()); try { handler.invoke(null, Object.class.getMethod("toString"), null); fail(); } catch (IllegalArgumentException e) {} assertTrue(((Interceptor1) interceptors.get("interceptor-7")).invoked()); assertTrue(((Interceptor1) interceptors.get("interceptor-8")).invoked()); container = mock(Container.class); interceptors = new LinkedHashMap<>(); interceptors.put("interceptor-9", new Interceptor1().result("result-9").applies(false).proceeds(true)); interceptors.put("interceptor-10", new Interceptor1().result("result-10").applies(false)); doReturn(interceptors).when(container).interceptors(); Prodigy.container(container); handler = new ClientInvocationHandler(new Object()); result = handler.invoke(null, Object.class.getMethod("toString"), null); assertThat(result, is("result-10")); assertTrue(((Interceptor1) interceptors.get("interceptor-9")).invoked()); assertTrue(((Interceptor1) interceptors.get("interceptor-10")).invoked()); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Invocation invocation = new Invocation(target, method).args(args); for (Interceptor interceptor : Prodigy.container().interceptors().values()) { if (interceptor.apply(invocation)) { return throwOrResult(invocation); } } if (!invocation.proceeded()) { invocation.proceed(); } return throwOrResult(invocation); }
ClientInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Invocation invocation = new Invocation(target, method).args(args); for (Interceptor interceptor : Prodigy.container().interceptors().values()) { if (interceptor.apply(invocation)) { return throwOrResult(invocation); } } if (!invocation.proceeded()) { invocation.proceed(); } return throwOrResult(invocation); } }
ClientInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Invocation invocation = new Invocation(target, method).args(args); for (Interceptor interceptor : Prodigy.container().interceptors().values()) { if (interceptor.apply(invocation)) { return throwOrResult(invocation); } } if (!invocation.proceeded()) { invocation.proceed(); } return throwOrResult(invocation); } ClientInvocationHandler(Object target); }
ClientInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Invocation invocation = new Invocation(target, method).args(args); for (Interceptor interceptor : Prodigy.container().interceptors().values()) { if (interceptor.apply(invocation)) { return throwOrResult(invocation); } } if (!invocation.proceeded()) { invocation.proceed(); } return throwOrResult(invocation); } ClientInvocationHandler(Object target); @Override Object invoke(Object proxy, Method method, Object[] args); }
ClientInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Invocation invocation = new Invocation(target, method).args(args); for (Interceptor interceptor : Prodigy.container().interceptors().values()) { if (interceptor.apply(invocation)) { return throwOrResult(invocation); } } if (!invocation.proceeded()) { invocation.proceed(); } return throwOrResult(invocation); } ClientInvocationHandler(Object target); @Override Object invoke(Object proxy, Method method, Object[] args);  }