Use LogAccessor from SF

* Change main classes to use a `LogAccessor` API to simplify code flow
* Fix tests according `LogAccessor` property
* Fix some Sonar smells
This commit is contained in:
Artem Bilan
2020-10-06 13:56:50 -04:00
parent a66e82b0aa
commit c7ff99a4e8
95 changed files with 1165 additions and 1432 deletions

View File

@@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.apache.commons.logging.Log;
import org.junit.jupiter.api.Test;
@@ -62,6 +63,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.log.LogAccessor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.ip.IpHeaders;
@@ -725,7 +727,7 @@ public class CachingClientConnectionFactoryTests {
gate.setOutputChannel(outputChannel);
gate.setBeanFactory(mock(BeanFactory.class));
gate.afterPropertiesSet();
Log logger = spy(TestUtils.getPropertyValue(gate, "logger", Log.class));
LogAccessor logger = spy(TestUtils.getPropertyValue(gate, "logger", LogAccessor.class));
new DirectFieldAccessor(gate).setPropertyValue("logger", logger);
when(logger.isDebugEnabled()).thenReturn(true);
doAnswer(new Answer<Void>() {
@@ -735,7 +737,7 @@ public class CachingClientConnectionFactoryTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
invocation.callRealMethod();
String log = invocation.getArgument(0);
String log = ((Supplier<String>) invocation.getArgument(0)).get();
if (log.startsWith("Response")) {
new SimpleAsyncTaskExecutor()
.execute(() -> gate.handleMessage(new GenericMessage<>("bar")));
@@ -747,7 +749,7 @@ public class CachingClientConnectionFactoryTests {
}
return null;
}
}).when(logger).debug(anyString());
}).when(logger).debug(any(Supplier.class));
gate.start();
gate.handleMessage(new GenericMessage<>("foo"));
Message<byte[]> result = (Message<byte[]>) outputChannel.receive(10000);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,14 +40,14 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.net.ServerSocketFactory;
import org.apache.commons.logging.Log;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.log.LogAccessor;
import org.springframework.core.serializer.Serializer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.IpHeaders;
@@ -74,7 +74,7 @@ public class ConnectionEventTests {
@Test
public void testConnectionEvents() throws Exception {
Socket socket = mock(Socket.class);
final List<TcpConnectionEvent> theEvent = new ArrayList<TcpConnectionEvent>();
final List<TcpConnectionEvent> theEvent = new ArrayList<>();
TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {
@Override
@@ -99,7 +99,7 @@ public class ConnectionEventTests {
conn.setMapper(new TcpMessageMapper());
conn.setSerializer(serializer);
try {
conn.send(new GenericMessage<String>("bar"));
conn.send(new GenericMessage<>("bar"));
fail("Expected exception");
}
catch (Exception e) {
@@ -142,7 +142,7 @@ public class ConnectionEventTests {
}
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<>();
scf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
@@ -182,7 +182,7 @@ public class ConnectionEventTests {
public void run() {
}
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<>();
scf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
@@ -218,7 +218,7 @@ public class ConnectionEventTests {
AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<>();
ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
@@ -246,7 +246,7 @@ public class ConnectionEventTests {
assertThat(messagingException.getFailedMessage()).isSameAs(message);
assertThat(messagingException.getMessage()).isEqualTo("Cannot correlate response - no pending reply for bar");
message = new GenericMessage<String>("foo");
message = new GenericMessage<>("foo");
gw.onMessage(message);
assertThat(theEvent.get()).isNotNull();
event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
@@ -261,8 +261,7 @@ public class ConnectionEventTests {
private void testServerExceptionGuts(AbstractServerConnectionFactory factory) throws Exception {
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(0);
factory.setPort(ss.getLocalPort());
final AtomicReference<TcpConnectionServerExceptionEvent> theEvent =
new AtomicReference<TcpConnectionServerExceptionEvent>();
final AtomicReference<TcpConnectionServerExceptionEvent> theEvent = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
factory.setApplicationEventPublisher(new ApplicationEventPublisher() {
@@ -280,8 +279,8 @@ public class ConnectionEventTests {
});
factory.setBeanName("sf");
factory.registerListener(message -> false);
Log logger = spy(TestUtils.getPropertyValue(factory, "logger", Log.class));
doNothing().when(logger).error(anyString(), any(Throwable.class));
LogAccessor logger = spy(TestUtils.getPropertyValue(factory, "logger", LogAccessor.class));
doNothing().when(logger).error(any(Throwable.class), anyString());
new DirectFieldAccessor(factory).setPropertyValue("logger", logger);
factory.start();
@@ -293,7 +292,7 @@ public class ConnectionEventTests {
ArgumentCaptor<String> reasonCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(logger).error(reasonCaptor.capture(), throwableCaptor.capture());
verify(logger).error(throwableCaptor.capture(), reasonCaptor.capture());
assertThat(reasonCaptor.getValue()).startsWith("Error on Server");
assertThat(reasonCaptor.getValue()).endsWith("; port = " + factory.getPort());
assertThat(throwableCaptor.getValue()).isInstanceOf(BindException.class);
@@ -316,7 +315,7 @@ public class ConnectionEventTests {
};
final AtomicReference<ApplicationEvent> failEvent = new AtomicReference<ApplicationEvent>();
final AtomicReference<ApplicationEvent> failEvent = new AtomicReference<>();
ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override

View File

@@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
@@ -41,6 +41,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -50,6 +51,8 @@ import org.mockito.ArgumentCaptor;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.log.LogAccessor;
import org.springframework.core.log.LogMessage;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -199,12 +202,14 @@ public class ConnectionFactoryTests {
private void testEarlyClose(final AbstractServerConnectionFactory factory, String property,
String message) throws Exception {
factory.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
factory.setBeanName("foo");
factory.registerListener(mock(TcpListener.class));
factory.afterPropertiesSet();
Log logger = spy(TestUtils.getPropertyValue(factory, "logger", Log.class));
new DirectFieldAccessor(factory).setPropertyValue("logger", logger);
LogAccessor logAccessor = TestUtils.getPropertyValue(factory, "logger", LogAccessor.class);
Log logger = spy(logAccessor.getLog());
new DirectFieldAccessor(logAccessor).setPropertyValue("log", logger);
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
@@ -215,11 +220,11 @@ public class ConnectionFactoryTests {
// wait until the stop nulls the channel
latch2.await(10, TimeUnit.SECONDS);
return null;
}).when(logger).info(contains("Listening"));
}).when(logger).info(argThat(logMessage -> logMessage.toString().contains("Listening")));
doAnswer(invocation -> {
latch3.countDown();
return null;
}).when(logger).debug(contains(message));
}).when(logger).debug(argThat(logMessage -> logMessage.toString().contains(message)));
factory.start();
assertThat(latch1.await(10, TimeUnit.SECONDS)).as("missing info log").isTrue();
// stop on a different thread because it waits for the executor
@@ -232,9 +237,9 @@ public class ConnectionFactoryTests {
latch2.countDown();
assertThat(latch3.await(10, TimeUnit.SECONDS)).as("missing debug log").isTrue();
String expected = "bean 'foo', port=" + factory.getPort() + message;
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<LogMessage> captor = ArgumentCaptor.forClass(LogMessage.class);
verify(logger, atLeast(1)).debug(captor.capture());
assertThat(captor.getAllValues()).contains(expected);
assertThat(captor.getAllValues().stream().map(Object::toString).collect(Collectors.toList())).contains(expected);
factory.stop();
}
@@ -315,7 +320,7 @@ public class ConnectionFactoryTests {
gateway.start();
if (fail) {
assertThatExceptionOfType(MessagingException.class).isThrownBy(() ->
gateway.handleMessage(new GenericMessage<>("test1")))
gateway.handleMessage(new GenericMessage<>("test1")))
.withMessageContaining("Connection test failed for");
}
else {