use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class ChainParserTests method chainWithLoggingChannelAdapter.
@Test
@SuppressWarnings("unchecked")
public void chainWithLoggingChannelAdapter() {
LogAccessor logger = mock(LogAccessor.class);
final AtomicReference<Supplier<? extends CharSequence>> log = new AtomicReference<>();
when(logger.isWarnEnabled()).thenReturn(true);
doAnswer(invocation -> {
log.set(invocation.getArgument(0));
return null;
}).when(logger).warn(any(Supplier.class));
@SuppressWarnings("unchecked") List<MessageHandler> handlers = TestUtils.getPropertyValue(this.logChain, "handlers", List.class);
MessageHandler handler = handlers.get(2);
assertThat(handler instanceof LoggingHandler).isTrue();
DirectFieldAccessor dfa = new DirectFieldAccessor(handler);
dfa.setPropertyValue("messageLogger", logger);
this.loggingChannelAdapterChannel.send(MessageBuilder.withPayload(new byte[] { 116, 101, 115, 116 }).build());
assertThat(log.get()).isNotNull();
assertThat(log.get().get()).isEqualTo("TEST");
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class ConnectionFactoryTests method testEarlyClose.
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();
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);
when(logger.isInfoEnabled()).thenReturn(true);
when(logger.isDebugEnabled()).thenReturn(true);
doAnswer(invocation -> {
latch1.countDown();
// wait until the stop nulls the channel
latch2.await(10, TimeUnit.SECONDS);
return null;
}).when(logger).info(argThat(logMessage -> logMessage.toString().contains("Listening")));
doAnswer(invocation -> {
latch3.countDown();
return null;
}).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
new SimpleAsyncTaskExecutor("testEarlyClose-").execute(factory::stop);
int n = 0;
DirectFieldAccessor accessor = new DirectFieldAccessor(factory);
await("Stop was not invoked in time").atMost(Duration.ofSeconds(20)).until(() -> accessor.getPropertyValue(property) == null);
latch2.countDown();
assertThat(latch3.await(10, TimeUnit.SECONDS)).as("missing debug log").isTrue();
String expected = "bean 'foo', port=" + factory.getPort() + message;
ArgumentCaptor<LogMessage> captor = ArgumentCaptor.forClass(LogMessage.class);
verify(logger, atLeast(1)).debug(captor.capture());
assertThat(captor.getAllValues().stream().map(Object::toString).collect(Collectors.toList())).contains(expected);
factory.stop();
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class SyslogReceivingChannelAdapterTests method testTcp.
@Test
public void testTcp() throws Exception {
SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
factory.setPort(0);
PollableChannel outputChannel = new QueueChannel();
factory.setOutputChannel(outputChannel);
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
final CountDownLatch latch = new CountDownLatch(2);
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(publisher).publishEvent(any(ApplicationEvent.class));
factory.setApplicationEventPublisher(publisher);
factory.setBeanFactory(mock(BeanFactory.class));
factory.afterPropertiesSet();
factory.start();
AbstractServerConnectionFactory server = TestUtils.getPropertyValue(factory, "syslogAdapter.connectionFactory", AbstractServerConnectionFactory.class);
TestingUtilities.waitListening(server, null);
TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
LogAccessor logger = spy(TestUtils.getPropertyValue(adapter, "logger", LogAccessor.class));
doReturn(true).when(logger).isDebugEnabled();
final CountDownLatch sawLog = new CountDownLatch(1);
doAnswer(invocation -> {
if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
sawLog.countDown();
}
invocation.callRealMethod();
return null;
}).when(logger).debug(anyString());
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes(StandardCharsets.UTF_8);
Socket socket = SocketFactory.getDefault().createSocket("localhost", server.getPort());
socket.getOutputStream().write(buf);
socket.close();
assertThat(sawLog.await(10, TimeUnit.SECONDS)).isTrue();
Message<?> message = outputChannel.receive(10000);
assertThat(message).isNotNull();
assertThat(message.getHeaders().get("syslog_HOST")).isEqualTo("WEBERN");
assertThat(message.getHeaders().get(IpHeaders.IP_ADDRESS)).isNotNull();
adapter.stop();
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
}
Aggregations