Example usage for org.springframework.aop.interceptor CustomizableTraceInterceptor invoke

List of usage examples for org.springframework.aop.interceptor CustomizableTraceInterceptor invoke

Introduction

In this page you can find the example usage for org.springframework.aop.interceptor CustomizableTraceInterceptor invoke.

Prototype

@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable 

Source Link

Document

Determines whether or not logging is enabled for the particular MethodInvocation .

Usage

From source file:org.springframework.aop.interceptor.CustomizableTraceInterceptorTests.java

@Test
public void testSunnyDayPathLogsCorrectly() throws Throwable {

    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[] {}));
    given(methodInvocation.getThis()).willReturn(this);

    Log log = mock(Log.class);
    given(log.isTraceEnabled()).willReturn(true);

    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    interceptor.invoke(methodInvocation);

    verify(log, times(2)).trace(anyString());
}

From source file:org.springframework.aop.interceptor.CustomizableTraceInterceptorTests.java

@Test
public void testExceptionPathLogsCorrectly() throws Throwable {

    MethodInvocation methodInvocation = mock(MethodInvocation.class);

    IllegalArgumentException exception = new IllegalArgumentException();
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[] {}));
    given(methodInvocation.getThis()).willReturn(this);
    given(methodInvocation.proceed()).willThrow(exception);

    Log log = mock(Log.class);
    given(log.isTraceEnabled()).willReturn(true);

    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    try {//from w  w w. jav a2 s.c  o m
        interceptor.invoke(methodInvocation);
        fail("Must have propagated the IllegalArgumentException.");
    } catch (IllegalArgumentException expected) {
    }

    verify(log).trace(anyString());
    verify(log).trace(anyString(), eq(exception));
}

From source file:org.springframework.aop.interceptor.CustomizableTraceInterceptorTests.java

@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());//from  ww w .j a  v  a  2 s .  c  o m
    interceptor.invoke(methodInvocation);

    verify(log, times(2)).trace(anyString());
}