public @Bean CustomizableTraceInterceptor interceptor() { CustomizableTraceInterceptor interceptor = new CustomizableTraceInterceptor(); interceptor.setEnterMessage("Entering $[methodName]($[arguments])."); interceptor.setExitMessage("Leaving $[methodName](..) with return value $[returnValue], took $[invocationTime]ms."); return interceptor; }
@Test(expected = IllegalArgumentException.class) public void testSetEnterMessageWithInvocationTimePlaceholder() { // Must not be able to set enter message with invocation time placeholder new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME); }
@Test(expected = IllegalArgumentException.class) public void testSetEmptyEnterMessage() { // Must not be able to set empty enter message new CustomizableTraceInterceptor().setEnterMessage(""); }
@Test(expected = IllegalArgumentException.class) public void testSetEnterMessageWithReturnValuePlaceholder() { // Must not be able to set enter message with return value placeholder new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE); }
@Test(expected = IllegalArgumentException.class) public void testSetEnterMessageWithExceptionPlaceholder() { // Must not be able to set enter message with exception placeholder new CustomizableTraceInterceptor().setEnterMessage(CustomizableTraceInterceptor.PLACEHOLDER_EXCEPTION); }
@Test public void testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[0])); given(methodInvocation.getThis()).willReturn(this); given(methodInvocation.getArguments()).willReturn(new Object[]{"$ One \\$", new Long(2)}); given(methodInvocation.proceed()).willReturn("Hello!"); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log); interceptor.setEnterMessage(new StringBuffer() .append("Entering the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME) .append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_NAME) .append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS) .append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES) .append(").").toString()); interceptor.setExitMessage(new StringBuffer() .append("Exiting the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME) .append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_SHORT_NAME) .append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS) .append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES) .append("), returning '").append(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE) .append("' and taking '").append(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME) .append("' this long.").toString()); interceptor.invoke(methodInvocation); verify(log, times(2)).trace(anyString()); }
@Bean public CustomizableTraceInterceptor interceptor() { CustomizableTraceInterceptor interceptor = new CustomizableTraceInterceptor(); interceptor.setEnterMessage("Entering $[methodName]($[arguments])."); interceptor.setExitMessage("Leaving $[methodName](..) with return value $[returnValue], took $[invocationTime]ms."); return interceptor; }
@Bean @Role(ROLE_INFRASTRUCTURE) public Advice traceInterceptor() { CustomizableTraceInterceptor traceInterceptor = new CustomizableTraceInterceptor(); traceInterceptor.setUseDynamicLogger(true); traceInterceptor.setEnterMessage("$[methodName]($[arguments])"); traceInterceptor.setExitMessage("~$[methodName]=$[returnValue] ($[invocationTime])"); traceInterceptor.setExceptionMessage("!$[methodName]"); return traceInterceptor; }