use of org.springframework.core.log.LogAccessor 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) {
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, "pollingTask", Callable.class);
assertThat(AopUtils.isAopProxy(pollingTask)).isTrue();
LogAccessor logger = spy(TestUtils.getPropertyValue(advice, "logger", LogAccessor.class));
when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
final AtomicReference<String> logMessage = new AtomicReference<>();
doAnswer(invocation -> {
logMessage.set(invocation.getArgument(0));
return null;
}).when(logger).warn(anyString());
DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
accessor.setPropertyValue("logger", logger);
pollingTask.call();
assertThat(called.get()).isFalse();
assertThat(logMessage.get()).isNotNull();
assertThat(logMessage.get()).contains("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in " + "'org.springframework.integration.endpoint.AbstractPollingEndpoint");
consumer.stop();
exec.shutdownNow();
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class LoggingHandlerTests method testDontEvaluateIfNotEnabled.
@Test
public void testDontEvaluateIfNotEnabled() {
LoggingHandler loggingHandler = new LoggingHandler("INFO");
loggingHandler.setLoggerName("test.logging.handler");
loggingHandler.setBeanFactory(mock(BeanFactory.class));
loggingHandler.afterPropertiesSet();
LogAccessor logAccessor = TestUtils.getPropertyValue(loggingHandler, "messageLogger", LogAccessor.class);
Log log = spy(logAccessor.getLog());
when(log.isInfoEnabled()).thenReturn(false, true);
new DirectFieldAccessor(logAccessor).setPropertyValue("log", log);
Expression expression = spy(TestUtils.getPropertyValue(loggingHandler, "expression", Expression.class));
loggingHandler.setLogExpression(expression);
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(expression, never()).getValue(any(EvaluationContext.class), any(Message.class));
loggingHandler.handleMessage(new GenericMessage<>("foo"));
verify(expression, times(1)).getValue(any(EvaluationContext.class), any(Message.class));
}
use of org.springframework.core.log.LogAccessor 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);
LogAccessor log = (LogAccessor) 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(ArgumentMatchers.<Supplier<? extends CharSequence>>argThat(logMessage -> logMessage.get().equals(testPayload)));
}
use of org.springframework.core.log.LogAccessor in project spring-integration by spring-projects.
the class FileWritingMessageHandlerTests method noFlushAppend.
@Test
public void noFlushAppend() throws Exception {
File tempFolder = new File(tempDir, UUID.randomUUID().toString());
FileWritingMessageHandler handler = new FileWritingMessageHandler(tempFolder);
handler.setFileExistsMode(FileExistsMode.APPEND_NO_FLUSH);
handler.setFileNameGenerator(message -> "foo.txt");
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
handler.setTaskScheduler(taskScheduler);
handler.setOutputChannel(new NullChannel());
handler.setBeanFactory(mock(BeanFactory.class));
handler.setFlushInterval(30000);
handler.afterPropertiesSet();
handler.start();
File file = new File(tempFolder, "foo.txt");
handler.handleMessage(new GenericMessage<>("foo"));
handler.handleMessage(new GenericMessage<>("bar"));
handler.handleMessage(new GenericMessage<>("baz"));
// change of payload type forces flush
handler.handleMessage(new GenericMessage<>("qux".getBytes()));
assertThat(file.length()).isGreaterThanOrEqualTo(9L);
// forces flush
handler.stop();
assertThat(file.length()).isEqualTo(12L);
handler.setFlushInterval(100);
handler.start();
handler.handleMessage(new GenericMessage<InputStream>(new ByteArrayInputStream("fiz".getBytes())));
int n = 0;
while (n++ < 100 && file.length() < 15) {
Thread.sleep(100);
}
assertThat(file.length()).isEqualTo(15L);
handler.handleMessage(new GenericMessage<InputStream>(new ByteArrayInputStream("buz".getBytes())));
handler.trigger(new GenericMessage<>(Matcher.quoteReplacement(file.getAbsolutePath())));
assertThat(file.length()).isEqualTo(18L);
assertThat(TestUtils.getPropertyValue(handler, "fileStates", Map.class).size()).isEqualTo(0);
handler.setFlushInterval(30000);
final AtomicBoolean called = new AtomicBoolean();
handler.setFlushPredicate((fileAbsolutePath, firstWrite, lastWrite, triggerMessage) -> {
called.set(true);
return true;
});
handler.handleMessage(new GenericMessage<InputStream>(new ByteArrayInputStream("box".getBytes())));
handler.trigger(new GenericMessage<>("foo"));
assertThat(file.length()).isEqualTo(21L);
assertThat(called.get()).isTrue();
handler.handleMessage(new GenericMessage<InputStream>(new ByteArrayInputStream("bux".getBytes())));
called.set(false);
handler.flushIfNeeded((fileAbsolutePath, firstWrite, lastWrite) -> {
called.set(true);
return true;
});
assertThat(file.length()).isEqualTo(24L);
assertThat(called.get()).isTrue();
handler.stop();
LogAccessor logger = spy(TestUtils.getPropertyValue(handler, "logger", LogAccessor.class));
new DirectFieldAccessor(handler).setPropertyValue("logger", logger);
when(logger.isDebugEnabled()).thenReturn(true);
final AtomicInteger flushes = new AtomicInteger();
doAnswer(i -> {
flushes.incrementAndGet();
return null;
}).when(logger).debug(startsWith("Flushed:"));
handler.setFlushInterval(50);
handler.setFlushWhenIdle(false);
handler.start();
for (int i = 0; i < 40; i++) {
handler.handleMessage(new GenericMessage<>("foo"));
Thread.sleep(5);
}
assertThat(flushes.get()).isGreaterThanOrEqualTo(2);
handler.stop();
}
use of org.springframework.core.log.LogAccessor 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);
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(suspensions.size()).as("suspension not removed").isEqualTo(0);
LogAccessor logger = spy(TestUtils.getPropertyValue(handler, "logger", LogAccessor.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()).contains("Suspending thread timed out or did not arrive within timeout for:").contains("payload=bar");
assertThat(suspensions.size()).isEqualTo(0);
Message<?> discard = discardChannel.receive(0);
assertThat(triggerMessage).isSameAs(discard);
handler.handleMessage(MessageBuilder.withPayload("foo").setCorrelationId("foo").build());
assertThat(suspensions.size()).isEqualTo(0);
exec.shutdownNow();
}
Aggregations