ExecuteMethodInterceptor implements Interceptor { public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { try { Method reflectionMethod = method.getMethod(); Object[] parameters = this.info.getParameters(); log.debug("Invoking {}", Stringnifier.simpleNameFor(reflectionMethod)); Object result = reflectionMethod.invoke(resourceInstance, parameters); if (validator.hasErrors()) { if (log.isDebugEnabled()) { try { validator.onErrorUse(nothing()); } catch (ValidationException e) { log.debug("Some validation errors occured: {}", e.getErrors()); } } throw new InterceptionException( "There are validation errors and you forgot to specify where to go. Please add in your method " + "something like:\n" + "validator.onErrorUse(page()).of(AnyController.class).anyMethod();\n" + "or any view that you like.\n" + "If you didn't add any validation error, it is possible that a conversion error had happened."); } if (reflectionMethod.getReturnType().equals(Void.TYPE)) { this.info.setResult("ok"); } else { this.info.setResult(result); } stack.next(method, resourceInstance); } catch (IllegalArgumentException e) { throw new InterceptionException(e); } catch (IllegalAccessException e) { throw new InterceptionException(e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof ValidationException) { log.trace("swallowing {}", cause); } else { throw new ApplicationLogicException("your controller raised an exception", cause); } } } ExecuteMethodInterceptor(MethodInfo info, Validator validator); void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance); boolean accepts(ResourceMethod method);  }
@Test public void shouldInvokeTheMethodAndNotProceedWithInterceptorStack() throws SecurityException, NoSuchMethodException, IOException, InterceptionException { ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark")); DogAlike auau = mock(DogAlike.class); when(info.getParameters()).thenReturn(new Object[0]); interceptor.intercept(stack, method, auau); verify(auau).bark(); verify(stack).next(method, auau); verify(info).setResult("ok"); }
@Test public void shouldThrowMethodExceptionIfThereIsAnInvocationException() throws IOException, SecurityException, NoSuchMethodException { ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark")); final DogAlike auau = mock(DogAlike.class); final RuntimeException exception = new RuntimeException(); doThrow(exception).when(auau).bark(); when(info.getParameters()).thenReturn(new Object[0]); try { interceptor.intercept(stack, method, auau); Assert.fail(); } catch (InterceptionException e) { assertThat((RuntimeException) e.getCause(), is(equalTo(exception))); } }
@Test public void shouldUseTheProvidedArguments() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { ResourceMethod method = new DefaultResourceMethod(null, DogAlike.class.getMethod("bark", int.class)); DogAlike auau = mock(DogAlike.class); when(info.getParameters()).thenReturn(new Object[] { 3 }); interceptor.intercept(stack, method, auau); verify(auau).bark(3); verify(stack).next(method, auau); verify(info).setResult("ok"); }
@Test public void shouldSetResultReturnedValueFromInvokedMethod() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method", Object.class)); final XController x = new XController(); when(info.getParameters()).thenReturn(new Object[] { "string" }); interceptor.intercept(stack, method, x); verify(stack).next(method, x); verify(info).setResult("string"); }
@Test public void shouldSetNullWhenNullReturnedFromInvokedMethod() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method", Object.class)); final XController x = new XController(); when(info.getParameters()).thenReturn(new Object[] { null }); interceptor.intercept(stack, method, x); verify(stack).next(method, x); verify(info).setResult(null); }
@Test public void shouldSetOkWhenVoidReturnedFromInvokedMethod() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { ResourceMethod method = new DefaultResourceMethod(null, XController.class.getMethod("method")); XController x = new XController(); when(info.getParameters()).thenReturn(new Object[] {}); interceptor.intercept(stack, method, x); verify(stack).next(method, x); verify(info).setResult("ok"); }
@Test public void shouldBeOkIfThereIsValidationErrorsAndYouSpecifiedWhereToGo() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { Method specifiedWhereToGo = AnyController.class.getMethod("specifiedWhereToGo"); ResourceMethod method = DefaultResourceMethod.instanceFor(AnyController.class, specifiedWhereToGo); AnyController controller = new AnyController(validator); when(info.getParameters()).thenReturn(new Object[0]); doThrow(new ValidationException(Collections.<Message> emptyList())).when(validator).onErrorUse(nothing()); when(validator.hasErrors()).thenReturn(true); interceptor.intercept(stack, method, controller); }
@Test public void shouldThrowExceptionIfYouHaventSpecifiedWhereToGoOnValidationError() throws SecurityException, NoSuchMethodException, InterceptionException, IOException { Method didntSpecifyWhereToGo = AnyController.class.getMethod("didntSpecifyWhereToGo"); final ResourceMethod method = DefaultResourceMethod.instanceFor(AnyController.class, didntSpecifyWhereToGo); final AnyController controller = new AnyController(validator); when(info.getParameters()).thenReturn(new Object[0]); when(validator.hasErrors()).thenReturn(true); try { interceptor.intercept(stack, method, controller); Assert.fail(); } catch (InterceptionException e) { } }