SystemCommandHandlerProxy { public void invokeCommand(final SystemCommand systemCommand, final UUID commandId) throws SystemCommandInvocationException { handlerMethodValidator.checkHandlerMethodIsValid(method, instance); try { method.invoke(instance, systemCommand, commandId); } catch (final IllegalAccessException e) { final String message = format("Failed to call method '%s()' on %s. Is the method public?", method.getName(), instance.getClass().getName()); throw new SystemCommandInvocationException( message, e ); } catch (final InvocationTargetException e) { final Throwable targetException = e.getTargetException(); final String message = format("%s thrown when calling method '%s()' on %s", targetException.getClass().getSimpleName(), method.getName(), instance.getClass().getName()); throw new SystemCommandInvocationException( message, targetException); } } SystemCommandHandlerProxy(final String commandName, final Method method, final Object instance, final HandlerMethodValidator handlerMethodValidator); String getCommandName(); Object getInstance(); void invokeCommand(final SystemCommand systemCommand, final UUID commandId);  }
@Test public void shouldFailIfTheMethodIsInaccessible() throws Exception { final UUID commandId = UUID.randomUUID(); final TestCommand testCommand = new TestCommand(); final DummyHandler dummyHandler = new DummyHandler(); final Method method = getMethod("aPrivateMethod", dummyHandler); final HandlerMethodValidator handlerMethodValidator = mock(HandlerMethodValidator.class); final SystemCommandHandlerProxy systemCommandHandlerProxy = new SystemCommandHandlerProxy( testCommand.getName(), method, dummyHandler, handlerMethodValidator); try { systemCommandHandlerProxy.invokeCommand(testCommand, commandId); fail(); } catch (final SystemCommandInvocationException expected) { assertThat(expected.getMessage(), is("Failed to call method 'aPrivateMethod()' on " + dummyHandler.getClass().getName() + ". Is the method public?")); assertThat(expected.getCause(), is(instanceOf(IllegalAccessException.class))); } verify(handlerMethodValidator).checkHandlerMethodIsValid(method, dummyHandler); }
@Test public void shouldFailIfTheInvokedMethodThrowsAnException() throws Exception { final UUID commandId = UUID.randomUUID(); final TestCommand testCommand = new TestCommand(); final DummyHandler dummyHandler = new DummyHandler(); final Method method = getMethod("anExceptionThrowingMethod", dummyHandler); final HandlerMethodValidator handlerMethodValidator = mock(HandlerMethodValidator.class); final SystemCommandHandlerProxy systemCommandHandlerProxy = new SystemCommandHandlerProxy( testCommand.getName(), method, dummyHandler, handlerMethodValidator); try { systemCommandHandlerProxy.invokeCommand(testCommand, commandId); fail(); } catch (final SystemCommandInvocationException expected) { assertThat(expected.getMessage(), is("IOException thrown when calling method 'anExceptionThrowingMethod()' on " + dummyHandler.getClass().getName())); assertThat(expected.getCause(), is(instanceOf(IOException.class))); } verify(handlerMethodValidator).checkHandlerMethodIsValid(method, dummyHandler); }