Search in sources :

Example 21 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 22 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)

Example 23 with LogAccessor

use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.

the class P2pChannelTests method testPubSubChannelLoggingWithMoreThenOneSubscriber.

@Test
public void testPubSubChannelLoggingWithMoreThenOneSubscriber() {
    final PublishSubscribeChannel channel = new PublishSubscribeChannel();
    channel.setBeanName("pubSubChannel");
    final LogAccessor logger = mock(LogAccessor.class);
    when(logger.isInfoEnabled()).thenReturn(true);
    ReflectionUtils.doWithFields(AbstractMessageChannel.class, field -> {
        if ("logger".equals(field.getName())) {
            field.setAccessible(true);
            field.set(channel, logger);
        }
    });
    channel.subscribe(mock(MessageHandler.class));
    channel.subscribe(mock(MessageHandler.class));
    verify(logger, times(2)).info(Mockito.anyString());
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Example 24 with LogAccessor

use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.

the class P2pChannelTests method testExecutorChannelLoggingWithMoreThenOneSubscriber.

@Test
public void testExecutorChannelLoggingWithMoreThenOneSubscriber() {
    final ExecutorChannel channel = new ExecutorChannel(mock(Executor.class));
    channel.setBeanName("executorChannel");
    final LogAccessor logger = mock(LogAccessor.class);
    when(logger.isInfoEnabled()).thenReturn(true);
    ReflectionUtils.doWithFields(AbstractMessageChannel.class, field -> {
        if ("logger".equals(field.getName())) {
            field.setAccessible(true);
            field.set(channel, logger);
        }
    });
    channel.subscribe(mock(MessageHandler.class));
    channel.subscribe(mock(MessageHandler.class));
    verify(logger, times(2)).info(Mockito.anyString());
}
Also used : Executor(java.util.concurrent.Executor) MessageHandler(org.springframework.messaging.MessageHandler) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Example 25 with LogAccessor

use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.

the class GatewayParserTests method testCustomCompletableNoAsyncAttemptAsync.

@Test
public void testCustomCompletableNoAsyncAttemptAsync() throws Exception {
    Object gateway = context.getBean("&customCompletableAttemptAsync");
    LogAccessor logger = spy(TestUtils.getPropertyValue(gateway, "logger", LogAccessor.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    new DirectFieldAccessor(gateway).setPropertyValue("logger", logger);
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptor() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return message;
        }
    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("customCompletableAttemptAsync", TestService.class);
    MyCompletableFuture result = service.customCompletable("flowCustomCompletable");
    String reply = result.get(10, TimeUnit.SECONDS);
    assertThat(reply).isEqualTo("SYNC_CUSTOM_COMPLETABLE");
    assertThat(thread.get()).isEqualTo(Thread.currentThread());
    assertThat(TestUtils.getPropertyValue(gateway, "asyncExecutor")).isNotNull();
    verify(logger).debug(ArgumentMatchers.<Supplier<String>>argThat(logMessage -> logMessage.get().equals("AsyncTaskExecutor submit*() return types are incompatible " + "with the method return type; " + "running on calling thread; the downstream flow must return the required Future: " + "MyCompletableFuture")));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Sinks(reactor.core.publisher.Sinks) QueueChannel(org.springframework.integration.channel.QueueChannel) ArgumentMatchers(org.mockito.ArgumentMatchers) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) GatewayMethodMetadata(org.springframework.integration.gateway.GatewayMethodMetadata) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) MessageBuilder(org.springframework.integration.support.MessageBuilder) SpringJUnitConfig(org.springframework.test.context.junit.jupiter.SpringJUnitConfig) Future(java.util.concurrent.Future) FactoryBean(org.springframework.beans.factory.FactoryBean) Duration(java.time.Duration) Map(java.util.Map) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) BeanNameAware(org.springframework.beans.factory.BeanNameAware) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) MyCompletableMessageFuture(org.springframework.integration.gateway.TestService.MyCompletableMessageFuture) TestService(org.springframework.integration.gateway.TestService) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) LogAccessor(org.springframework.core.log.LogAccessor) MyCompletableFuture(org.springframework.integration.gateway.TestService.MyCompletableFuture) MessageChannel(org.springframework.messaging.MessageChannel) ApplicationContext(org.springframework.context.ApplicationContext) Executors(java.util.concurrent.Executors) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) GatewayProxyFactoryBean(org.springframework.integration.gateway.GatewayProxyFactoryBean) Expression(org.springframework.expression.Expression) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TestService(org.springframework.integration.gateway.TestService) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) MyCompletableFuture(org.springframework.integration.gateway.TestService.MyCompletableFuture) MessageChannel(org.springframework.messaging.MessageChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

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