Search in sources :

Example 31 with LogAccessor

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();
}
Also used : Arrays(java.util.Arrays) SocketAddress(java.net.SocketAddress) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TcpConnectionFactoryFactoryBean(org.springframework.integration.ip.config.TcpConnectionFactoryFactoryBean) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) Mockito.atLeast(org.mockito.Mockito.atLeast) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Awaitility.await(org.awaitility.Awaitility.await) TaskScheduler(org.springframework.scheduling.TaskScheduler) LogAccessor(org.springframework.core.log.LogAccessor) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) SocketFactory(javax.net.SocketFactory) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) LogFactory(org.apache.commons.logging.LogFactory) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) Socket(java.net.Socket) IpIntegrationEvent(org.springframework.integration.ip.event.IpIntegrationEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LogMessage(org.springframework.core.log.LogMessage) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Collections(java.util.Collections) Log(org.apache.commons.logging.Log) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) LogMessage(org.springframework.core.log.LogMessage) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LogAccessor(org.springframework.core.log.LogAccessor)

Example 32 with LogAccessor

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.");
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Example 33 with LogAccessor

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.");
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Example 34 with LogAccessor

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();
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Example 35 with LogAccessor

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");
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

LogAccessor (org.springframework.core.log.LogAccessor)53 Test (org.junit.jupiter.api.Test)48 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)37 Supplier (java.util.function.Supplier)26 BeanFactory (org.springframework.beans.factory.BeanFactory)19 CountDownLatch (java.util.concurrent.CountDownLatch)17 QueueChannel (org.springframework.integration.channel.QueueChannel)15 Message (org.springframework.messaging.Message)11 GenericMessage (org.springframework.messaging.support.GenericMessage)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Mockito.spy (org.mockito.Mockito.spy)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.verify (org.mockito.Mockito.verify)8 TestUtils (org.springframework.integration.test.util.TestUtils)8 TimeUnit (java.util.concurrent.TimeUnit)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7