INT-2939 LoggingHandler Improvements
- Don't evaluate expression if the log level is not enabled - Add mutually exclusive assertions (already protected by the parser but not for programmatic configuration). INT-2944 Document <logging-channel-adapter/> Add reference sub-chapter. INT-2939 Polishing - PR Comments - remove unnecessary cast. - add setter for level changes. Polishing * Change setter to use enum. * Add getter for current level. * Rename method to 'createLogMessage'.
This commit is contained in:
committed by
Gunnar Hillert
parent
93211942cf
commit
d54a88f528
@@ -22,14 +22,12 @@ import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.expression.spel.SpelEvaluationException;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.handler.LoggingHandler;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
@@ -39,6 +37,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
* @since 2.1
|
||||
*/
|
||||
@ContextConfiguration
|
||||
|
||||
@@ -16,10 +16,25 @@
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.handler.LoggingHandler.Level;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -41,11 +56,71 @@ public class LoggingHandlerTests {
|
||||
input.send(MessageBuilder.withPayload(bean).setHeader("foo", "bar").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertMutuallyExclusive() {
|
||||
LoggingHandler loggingHandler = new LoggingHandler("INFO");
|
||||
loggingHandler.setExpression("'foo'");
|
||||
try {
|
||||
loggingHandler.setShouldLogFullMessage(true);
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertEquals("Cannot set both 'expression' AND 'shouldLogFullMessage' properties", e.getMessage());
|
||||
}
|
||||
|
||||
loggingHandler = new LoggingHandler("INFO");
|
||||
loggingHandler.setShouldLogFullMessage(true);
|
||||
try {
|
||||
loggingHandler.setExpression("'foo'");
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertEquals("Cannot set both 'expression' AND 'shouldLogFullMessage' properties", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDontEvaluateIfNotEnabled() {
|
||||
LoggingHandler loggingHandler = new LoggingHandler("INFO");
|
||||
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<String>("foo"));
|
||||
verify(expression, never()).getValue(Mockito.any(EvaluationContext.class), Mockito.any());
|
||||
|
||||
when(log.isInfoEnabled()).thenReturn(true);
|
||||
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
|
||||
verify(expression, times(1)).getValue(Mockito.any(EvaluationContext.class), Mockito.any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeLevel() {
|
||||
LoggingHandler loggingHandler = new LoggingHandler("INFO");
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(loggingHandler);
|
||||
Log log = (Log) accessor.getPropertyValue("messageLogger");
|
||||
log = spy(log);
|
||||
accessor.setPropertyValue("messageLogger", log);
|
||||
when(log.isInfoEnabled()).thenReturn(true);
|
||||
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
|
||||
verify(log, times(1)).info(Mockito.anyString());
|
||||
verify(log, never()).warn(Mockito.anyString());
|
||||
|
||||
loggingHandler.setLevel(Level.WARN);
|
||||
loggingHandler.handleMessage(new GenericMessage<String>("foo"));
|
||||
verify(log, times(1)).info(Mockito.anyString());
|
||||
verify(log, times(1)).warn(Mockito.anyString());
|
||||
}
|
||||
|
||||
public static class TestBean {
|
||||
|
||||
private final String name;
|
||||
|
||||
private int age;
|
||||
private final int age;
|
||||
|
||||
public TestBean(String name, int age) {
|
||||
this.name = name;
|
||||
|
||||
Reference in New Issue
Block a user