Search in sources :

Example 36 with Log

use of org.apache.commons.logging.Log in project spring-integration by spring-projects.

the class BarrierMessageHandlerTests method testLateReply.

@Test
public void testLateReply() throws Exception {
    final BarrierMessageHandler handler = new BarrierMessageHandler(0);
    QueueChannel outputChannel = new QueueChannel();
    QueueChannel discardChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.setDiscardChannelName("discards");
    handler.setChannelResolver(s -> discardChannel);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final CountDownLatch latch = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
        latch.countDown();
    });
    Map<?, ?> suspensions = TestUtils.getPropertyValue(handler, "suspensions", Map.class);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals("suspension not removed", 0, suspensions.size());
    Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class));
    new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
    final Message<String> triggerMessage = MessageBuilder.withPayload("bar").setCorrelationId("foo").build();
    handler.trigger(triggerMessage);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).error(captor.capture());
    assertThat(captor.getValue(), allOf(containsString("Suspending thread timed out or did not arrive within timeout for:"), containsString("payload=bar")));
    assertEquals(0, suspensions.size());
    Message<?> discard = discardChannel.receive(0);
    assertSame(discard, triggerMessage);
    handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
    assertEquals(0, suspensions.size());
    exec.shutdownNow();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 37 with Log

use of org.apache.commons.logging.Log in project spring-integration by spring-projects.

the class AsyncHandlerTests method setup.

@Before
public void setup() {
    this.executor = Executors.newSingleThreadExecutor();
    this.handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            final SettableListenableFuture<String> future = new SettableListenableFuture<String>();
            AsyncHandlerTests.this.executor.execute(() -> {
                try {
                    latch.await(10, TimeUnit.SECONDS);
                    switch(whichTest) {
                        case 0:
                            future.set("reply");
                            break;
                        case 1:
                            future.setException(new RuntimeException("foo"));
                            break;
                        case 2:
                            future.setException(new MessagingException(requestMessage));
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
            return future;
        }
    };
    this.handler.setAsync(true);
    this.handler.setOutputChannel(this.output);
    this.handler.setBeanFactory(mock(BeanFactory.class));
    this.latch = new CountDownLatch(1);
    Log logger = spy(TestUtils.getPropertyValue(this.handler, "logger", Log.class));
    new DirectFieldAccessor(this.handler).setPropertyValue("logger", logger);
    doAnswer(invocation -> {
        failedCallbackMessage = invocation.getArgument(0);
        failedCallbackException = invocation.getArgument(1);
        exceptionLatch.countDown();
        return null;
    }).when(logger).error(anyString(), any(Throwable.class));
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) MessagingException(org.springframework.messaging.MessagingException) Log(org.apache.commons.logging.Log) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) Before(org.junit.Before)

Example 38 with Log

use of org.apache.commons.logging.Log in project spring-integration by spring-projects.

the class LoggingHandlerTests method testDontEvaluateIfNotEnabled.

@Test
public void testDontEvaluateIfNotEnabled() {
    LoggingHandler loggingHandler = new LoggingHandler("INFO");
    loggingHandler.setBeanFactory(mock(BeanFactory.class));
    loggingHandler.afterPropertiesSet();
    DirectFieldAccessor accessor = new DirectFieldAccessor(loggingHandler);
    Log log = (Log) accessor.getPropertyValue("messageLogger");
    log = spy(log);
    accessor.setPropertyValue("messageLogger", log);
    Expression expression = (Expression) accessor.getPropertyValue("expression");
    expression = spy(expression);
    accessor.setPropertyValue("expression", expression);
    when(log.isInfoEnabled()).thenReturn(false);
    loggingHandler.handleMessage(new GenericMessage<>("foo"));
    verify(expression, never()).getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class));
    when(log.isInfoEnabled()).thenReturn(true);
    loggingHandler.handleMessage(new GenericMessage<>("foo"));
    verify(expression, times(1)).getValue(Mockito.any(EvaluationContext.class), Mockito.any(Message.class));
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Log(org.apache.commons.logging.Log) Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 39 with Log

use of org.apache.commons.logging.Log in project spring-integration by spring-projects.

the class LoggingHandlerTests method testUsageWithoutSpringInitialization.

@Test
public void testUsageWithoutSpringInitialization() {
    LoggingHandler loggingHandler = new LoggingHandler("ERROR");
    DirectFieldAccessor accessor = new DirectFieldAccessor(loggingHandler);
    Log log = (Log) accessor.getPropertyValue("messageLogger");
    log = spy(log);
    accessor.setPropertyValue("messageLogger", log);
    String testPayload = "TEST_PAYLOAD";
    Message<String> message = MessageBuilder.withPayload(testPayload).build();
    loggingHandler.handleMessage(message);
    verify(log).error(testPayload);
}
Also used : Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 40 with Log

use of org.apache.commons.logging.Log in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method testInappropriateAdvice.

@Test
public void testInappropriateAdvice() throws Exception {
    final AtomicBoolean called = new AtomicBoolean(false);
    Advice advice = new AbstractRequestHandlerAdvice() {

        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
            called.set(true);
            return callback.execute();
        }
    };
    PollableChannel inputChannel = new QueueChannel();
    PollingConsumer consumer = new PollingConsumer(inputChannel, message -> {
    });
    consumer.setAdviceChain(Collections.singletonList(advice));
    ExecutorService exec = Executors.newSingleThreadExecutor();
    consumer.setTaskExecutor(new ErrorHandlingTaskExecutor(exec, t -> {
    }));
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.setTaskScheduler(mock(TaskScheduler.class));
    consumer.start();
    Callable<?> pollingTask = TestUtils.getPropertyValue(consumer, "poller.pollingTask", Callable.class);
    assertTrue(AopUtils.isAopProxy(pollingTask));
    Log logger = TestUtils.getPropertyValue(advice, "logger", Log.class);
    logger = spy(logger);
    when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
    final AtomicReference<String> logMessage = new AtomicReference<String>();
    doAnswer(invocation -> {
        logMessage.set(invocation.getArgument(0));
        return null;
    }).when(logger).warn(Mockito.anyString());
    DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
    accessor.setPropertyValue("logger", logger);
    pollingTask.call();
    assertFalse(called.get());
    assertNotNull(logMessage.get());
    assertThat(logMessage.get(), Matchers.containsString("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint"));
    consumer.stop();
    exec.shutdownNow();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) AopUtils(org.springframework.aop.support.AopUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) Method(java.lang.reflect.Method) AdviceMessage(org.springframework.integration.message.AdviceMessage) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) 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) Assert.assertSame(org.junit.Assert.assertSame) MessageFilter(org.springframework.integration.filter.MessageFilter) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ExecutorService(java.util.concurrent.ExecutorService) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) RetryContext(org.springframework.retry.RetryContext) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) Log(org.apache.commons.logging.Log) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskScheduler(org.springframework.scheduling.TaskScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollableChannel(org.springframework.messaging.PollableChannel) ExecutorService(java.util.concurrent.ExecutorService) BeanFactory(org.springframework.beans.factory.BeanFactory) Advice(org.aopalliance.aop.Advice) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Test(org.junit.Test)

Aggregations

Log (org.apache.commons.logging.Log)188 Test (org.junit.Test)51 Test (org.junit.jupiter.api.Test)40 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)35 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)19 BeanFactory (org.springframework.beans.factory.BeanFactory)17 CountDownLatch (java.util.concurrent.CountDownLatch)15 LogConfigurationException (org.apache.commons.logging.LogConfigurationException)15 ArrayList (java.util.ArrayList)12 File (java.io.File)11 QueueChannel (org.springframework.integration.channel.QueueChannel)11 MethodInvocation (org.aopalliance.intercept.MethodInvocation)10 IOException (java.io.IOException)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)9 Message (org.springframework.messaging.Message)8 List (java.util.List)7 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)7 InputStream (java.io.InputStream)6 LogFactory (org.apache.commons.logging.LogFactory)6