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 OperationInvokingChannelAdapterParserTests method testOutboundAdapterWithNonNullReturn.
@Test
public void testOutboundAdapterWithNonNullReturn() {
LogAccessor logger = spy(TestUtils.getPropertyValue(this.operationWithNonNullReturnHandler, "logger", LogAccessor.class));
willReturn(true).given(logger).isWarnEnabled();
new DirectFieldAccessor(this.operationWithNonNullReturnHandler).setPropertyValue("logger", logger);
this.operationWithNonNullReturn.send(new GenericMessage<>("test1"));
verify(logger).warn("This component doesn't expect a reply. " + "The MBean operation 'testWithReturn' result '[test1]' for " + "'org.springframework.integration.jmx.config:type=TestBean,name=testBeanAdapter' is ignored.");
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class OperationInvokingChannelAdapterParserTests method testOperationWithinChainWithNonNullReturn.
@Test
public void testOperationWithinChainWithNonNullReturn() {
LogAccessor logger = spy(TestUtils.getPropertyValue(this.operationWithinChainWithNonNullReturnHandler, "logger", LogAccessor.class));
willReturn(true).given(logger).isWarnEnabled();
new DirectFieldAccessor(this.operationWithinChainWithNonNullReturnHandler).setPropertyValue("logger", logger);
this.operationWithinChainWithNonNullReturn.send(new GenericMessage<>("test1"));
verify(logger).warn("This component doesn't expect a reply. " + "The MBean operation 'testWithReturn' result '[test1]' for " + "'org.springframework.integration.jmx.config:type=TestBean,name=testBeanAdapter' is ignored.");
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class AmqpOutboundChannelAdapterParserTests method testInt3430FailForNotLazyConnect.
@Test
public void testInt3430FailForNotLazyConnect() {
RabbitTemplate amqpTemplate = spy(new RabbitTemplate());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
RuntimeException toBeThrown = new RuntimeException("Test Connection Exception");
doThrow(toBeThrown).when(connectionFactory).createConnection();
when(amqpTemplate.getConnectionFactory()).thenReturn(connectionFactory);
AmqpOutboundEndpoint handler = new AmqpOutboundEndpoint(amqpTemplate);
LogAccessor logger = spy(TestUtils.getPropertyValue(handler, "logger", LogAccessor.class));
new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
doNothing().when(logger).error(toBeThrown, "Failed to eagerly establish the connection.");
ApplicationContext context = mock(ApplicationContext.class);
handler.setApplicationContext(context);
handler.setBeanFactory(context);
handler.afterPropertiesSet();
handler.start();
handler.stop();
verify(logger, never()).error(any(RuntimeException.class), anyString());
handler.setLazyConnect(false);
handler.start();
verify(logger).error(toBeThrown, "Failed to eagerly establish the connection.");
handler.stop();
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class DirectChannelTests method testSend.
@Test
void testSend() {
DirectChannel channel = new DirectChannel();
LogAccessor logger = spy(TestUtils.getPropertyValue(channel, "logger", LogAccessor.class));
when(logger.isDebugEnabled()).thenReturn(true);
new DirectFieldAccessor(channel).setPropertyValue("logger", logger);
ThreadNameExtractingTestTarget target = new ThreadNameExtractingTestTarget();
channel.subscribe(target);
GenericMessage<String> message = new GenericMessage<>("test");
assertThat(channel.send(message)).isTrue();
assertThat(target.threadName).isEqualTo(Thread.currentThread().getName());
DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel);
UnicastingDispatcher dispatcher = (UnicastingDispatcher) channelAccessor.getPropertyValue("dispatcher");
DirectFieldAccessor dispatcherAccessor = new DirectFieldAccessor(dispatcher);
Object loadBalancingStrategy = dispatcherAccessor.getPropertyValue("loadBalancingStrategy");
assertThat(loadBalancingStrategy instanceof RoundRobinLoadBalancingStrategy).isTrue();
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(logger, times(2)).debug(captor.capture());
List<String> logs = captor.getAllValues();
assertThat(logs.size()).isEqualTo(2);
assertThat(logs.get(0)).startsWith("preSend");
assertThat(logs.get(1)).startsWith("postSend");
}
Aggregations