Search in sources :

Example 6 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class CustomizableTraceInterceptorTests method testSunnyDayPathLogsCorrectly.

@Test
public void testSunnyDayPathLogsCorrectly() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString"));
    given(methodInvocation.getThis()).willReturn(this);
    Logger log = mock(Logger.class);
    given(log.isTraceEnabled()).willReturn(true);
    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    interceptor.invoke(methodInvocation);
    verify(log, times(2)).trace(anyString());
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Example 7 with Logger

use of cn.taketoday.logging.Logger in project today-infrastructure by TAKETODAY.

the class LambdaSafeTests method callbackWithLoggerShouldUseLogger.

@Test
@SuppressWarnings("unchecked")
void callbackWithLoggerShouldUseLogger() {
    Logger logger = mock(Logger.class);
    given(logger.isDebugEnabled()).willReturn(true);
    GenericCallback<StringBuilder> callbackInstance = (s) -> fail("Should not get here");
    String argument = "foo";
    LambdaSafe.callback(GenericCallback.class, callbackInstance, argument).withLogger(logger).invoke((c) -> c.handle(argument));
    verify(logger).debug(contains("Non-matching CharSequence type for callback LambdaSafeTests.GenericCallback"), any(Throwable.class));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Filter.allowAll(cn.taketoday.util.LambdaSafe.Filter.allowAll) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Test(org.junit.jupiter.api.Test) List(java.util.List) InvocationResult(cn.taketoday.util.LambdaSafe.InvocationResult) Stream(java.util.stream.Stream) BDDMockito.given(org.mockito.BDDMockito.given) Logger(cn.taketoday.logging.Logger) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Example 8 with Logger

use of cn.taketoday.logging.Logger in project today-framework by TAKETODAY.

the class CustomizableTraceInterceptorTests method testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching.

@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 \\$", 2L });
    given(methodInvocation.proceed()).willReturn("Hello!");
    Logger log = mock(Logger.class);
    given(log.isTraceEnabled()).willReturn(true);
    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    interceptor.setEnterMessage(new StringBuilder().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 StringBuilder().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());
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Example 9 with Logger

use of cn.taketoday.logging.Logger in project today-framework by TAKETODAY.

the class CustomizableTraceInterceptorTests method testExceptionPathLogsCorrectly.

@Test
public void testExceptionPathLogsCorrectly() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    IllegalArgumentException exception = new IllegalArgumentException();
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString"));
    given(methodInvocation.getThis()).willReturn(this);
    given(methodInvocation.proceed()).willThrow(exception);
    Logger log = mock(Logger.class);
    given(log.isTraceEnabled()).willReturn(true);
    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    assertThatIllegalArgumentException().isThrownBy(() -> interceptor.invoke(methodInvocation));
    verify(log).trace(anyString());
    verify(log).trace(anyString(), eq(exception));
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(cn.taketoday.logging.Logger) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 10 with Logger

use of cn.taketoday.logging.Logger in project today-framework by TAKETODAY.

the class DebugInterceptorTests method testSunnyDayPathLogsCorrectly.

@Test
public void testSunnyDayPathLogsCorrectly() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    Logger log = mock(Logger.class);
    given(log.isTraceEnabled()).willReturn(true);
    DebugInterceptor interceptor = new StubDebugInterceptor(log);
    interceptor.invoke(methodInvocation);
    checkCallCountTotal(interceptor);
    verify(log, times(2)).trace(anyString());
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) Logger(cn.taketoday.logging.Logger) Test(org.junit.jupiter.api.Test)

Aggregations

Logger (cn.taketoday.logging.Logger)34 Test (org.junit.jupiter.api.Test)32 MethodInvocation (org.aopalliance.intercept.MethodInvocation)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14 NoOpCache (cn.taketoday.cache.support.NoOpCache)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 List (java.util.List)4 ResolvableType (cn.taketoday.core.ResolvableType)2 Hints (cn.taketoday.core.codec.Hints)2 ResourceDecoder (cn.taketoday.core.codec.ResourceDecoder)2 ResourceEncoder (cn.taketoday.core.codec.ResourceEncoder)2 ResourceRegionEncoder (cn.taketoday.core.codec.ResourceRegionEncoder)2 DescriptiveResource (cn.taketoday.core.io.DescriptiveResource)2 InputStreamResource (cn.taketoday.core.io.InputStreamResource)2 Resource (cn.taketoday.core.io.Resource)2 ResourceRegion (cn.taketoday.core.io.ResourceRegion)2 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)2 DataBufferFactory (cn.taketoday.core.io.buffer.DataBufferFactory)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpLogging (cn.taketoday.http.HttpLogging)2