LoggingInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { List<String> argStrings = Lists.newArrayList(); for (Object arg : invocation.getArguments()) { if (arg == null) { argStrings.add("null"); } else { Function<Object, String> printFunction = printFunctions.get(arg.getClass()); argStrings.add((printFunction == null) ? arg.toString() : printFunction.apply(arg)); } } String methodName = invocation.getMethod().getName(); String message = String.format("%s(%s)", methodName, String.join(", ", argStrings)); LOG.info(message); try { return invocation.proceed(); } catch (Storage.TransientStorageException e) { LOG.warn("Uncaught transient exception while handling " + message, e); return Responses.addMessage(Responses.empty(), ResponseCode.ERROR_TRANSIENT, e); } catch (RuntimeException e) { Throwables.propagateIfInstanceOf(e, ShiroException.class); LOG.warn("Uncaught exception while handling " + message, e); return Responses.addMessage(Responses.empty(), ResponseCode.ERROR, e); } }  @Override Object invoke(MethodInvocation invocation);  }
@Test public void testInvokeTransientStorageException() throws Throwable { expect(methodInvocation.getMethod()) .andReturn(InterceptedClass.class.getDeclaredMethod("respond")); expect(methodInvocation.getArguments()).andReturn(new Object[]{}); expect(methodInvocation.proceed()).andThrow(new TransientStorageException("try again")); control.replay(); assertEquals( ResponseCode.ERROR_TRANSIENT, ((Response) loggingInterceptor.invoke(methodInvocation)).getResponseCode()); }
@Test public void testInvokeRuntimeException() throws Throwable { expect(methodInvocation.getMethod()) .andReturn(InterceptedClass.class.getDeclaredMethod("respond")); expect(methodInvocation.getArguments()).andReturn(new Object[] {}); expect(methodInvocation.proceed()).andThrow(new RuntimeException()); control.replay(); assertEquals( ResponseCode.ERROR, ((Response) loggingInterceptor.invoke(methodInvocation)).getResponseCode()); }
@Test public void testInvokePrintsArgs() throws Throwable { Response response = new Response(); expect(methodInvocation.getMethod()) .andReturn(InterceptedClass.class.getDeclaredMethod("respond", Object.class, Object.class)); final AtomicBoolean calledAtLeastOnce = new AtomicBoolean(false); Object arg1 = new Object() { @Override public String toString() { calledAtLeastOnce.set(true); return "arg1"; } }; expect(methodInvocation.getArguments()).andReturn(new Object[] {arg1, null}); expect(methodInvocation.proceed()).andReturn(response); control.replay(); assertSame(response, loggingInterceptor.invoke(methodInvocation)); assertTrue(calledAtLeastOnce.get()); }
@Test public void testInvokePrintsBlankedJobConfiguration() throws Throwable { Response response = new Response(); expect(methodInvocation.getMethod()) .andReturn(InterceptedClass.class.getDeclaredMethod("respond", JobConfiguration.class)); expect(methodInvocation.getArguments()) .andReturn(new Object[] {new JobConfiguration().setTaskConfig(new TaskConfig())}); expect(methodInvocation.proceed()).andReturn(response); control.replay(); assertSame(response, loggingInterceptor.invoke(methodInvocation)); }
@Test public void testInvokePrintsJobConfiguration() throws Throwable { Response response = new Response(); expect(methodInvocation.getMethod()) .andReturn(InterceptedClass.class.getDeclaredMethod("respond", JobConfiguration.class)); expect(methodInvocation.getArguments()).andReturn(new Object[] {new JobConfiguration()}); expect(methodInvocation.proceed()).andReturn(response); control.replay(); assertSame(response, loggingInterceptor.invoke(methodInvocation)); }