INT-4376: Upgage to Log4J 2 (#2321)

* INT-4376: Upgage to Log4J 2

JIRA: https://jira.spring.io/browse/INT-4376

* Deprecate Log4J 1.x components in favor of newly added
a `Log4j2LevelAdjuster` JUnit `@Rule`
* Update all the logging configuration to Log4J 2

* Polishing after testing
This commit is contained in:
Artem Bilan
2018-01-11 10:59:55 -05:00
committed by Gary Russell
parent 422f651113
commit 30450c48be
97 changed files with 981 additions and 539 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,60 +25,27 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.support.LongRunningIntegrationTest;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
*/
public class AggregatorWithCustomReleaseStrategyTests {
@Rule
public TestWatcher longTests = new TestWatcher() {
private static final String RUN_LONG_PROP = "RUN_LONG_INTEGRATION_TESTS";
private boolean shouldRun;
{
for (String value: new String[]{System.getenv(RUN_LONG_PROP), System.getProperty(RUN_LONG_PROP)}) {
if ("true".equalsIgnoreCase(value)) {
this.shouldRun = true;
break;
}
}
}
@Override
public Statement apply(Statement base, Description description) {
if (!this.shouldRun) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Assume.assumeTrue(false);
}
};
}
else {
return super.apply(base, description);
}
}
};
@ClassRule
public static LongRunningIntegrationTest longTests = new LongRunningIntegrationTest();
private static ExecutorService executor = Executors.newCachedThreadPool();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.1
*/
@ContextConfiguration
@@ -56,7 +57,8 @@ public class LoggingChannelAdapterParserTests {
@Test
public void verifyConfig() {
LoggingHandler loggingHandler = TestUtils.getPropertyValue(loggerConsumer, "handler", LoggingHandler.class);
assertEquals("org.springframework.integration.test.logger", TestUtils.getPropertyValue(loggingHandler, "messageLogger.name"));
assertEquals("org.springframework.integration.test.logger",
TestUtils.getPropertyValue(loggingHandler, "messageLogger.logger.name"));
assertEquals(1, TestUtils.getPropertyValue(loggingHandler, "order"));
assertEquals("WARN", TestUtils.getPropertyValue(loggingHandler, "level").toString());
assertEquals("#root", TestUtils.getPropertyValue(loggingHandler, "expression.expression"));
@@ -65,7 +67,8 @@ public class LoggingChannelAdapterParserTests {
@Test
public void verifyExpressionAndOtherDefaultConfig() {
LoggingHandler loggingHandler = TestUtils.getPropertyValue(loggerWithExpression, "handler", LoggingHandler.class);
assertEquals("org.springframework.integration.handler.LoggingHandler", TestUtils.getPropertyValue(loggingHandler, "messageLogger.name"));
assertEquals("org.springframework.integration.handler.LoggingHandler",
TestUtils.getPropertyValue(loggingHandler, "messageLogger.logger.name"));
assertEquals(Ordered.LOWEST_PRECEDENCE, TestUtils.getPropertyValue(loggingHandler, "order"));
assertEquals("INFO", TestUtils.getPropertyValue(loggingHandler, "level").toString());
assertEquals("payload.foo", TestUtils.getPropertyValue(loggingHandler, "expression.expression"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,7 +42,6 @@ import java.util.concurrent.atomic.AtomicReference;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.Joinpoint;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.log4j.Level;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -67,7 +66,7 @@ import org.springframework.integration.config.ExpressionControlBusFactoryBean;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.scheduling.PollSkipAdvice;
import org.springframework.integration.scheduling.SimplePollSkipStrategy;
import org.springframework.integration.test.rule.Log4jLevelAdjuster;
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
import org.springframework.integration.test.util.OnlyOnceTrigger;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.util.CompoundTrigger;
@@ -85,6 +84,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.1
*
*/
@@ -94,7 +95,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class PollerAdviceTests {
@Rule
public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, "org.springframework.integration");
public Log4j2LevelAdjuster adjuster = Log4j2LevelAdjuster.trace();
@Autowired
private MessageChannel control;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -41,7 +42,6 @@ import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.config.IntegrationEvaluationContextFactoryBean;
import org.springframework.integration.context.IntegrationContextUtils;
@@ -56,121 +56,122 @@ import org.springframework.messaging.support.GenericMessage;
* @author Gunnar Hillert
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class ExpressionEvaluatingMessageProcessorTests {
private static final Log logger = LogFactory.getLog(ExpressionEvaluatingMessageProcessorTests.class);
private static final ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
private static final ExpressionParser expressionParser = new SpelExpressionParser();
@Rule
public ExpectedException expected = ExpectedException.none();
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessage() {
Expression expression = expressionParser.parseExpression("payload");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertEquals("foo", processor.processMessage(new GenericMessage<String>("foo")));
assertEquals("foo", processor.processMessage(new GenericMessage<>("foo")));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithParameterCoercion() throws Exception {
@SuppressWarnings("unused")
class TestTarget {
public String stringify(int number) {
return number + "";
}
}
Expression expression = expressionParser.parseExpression("#target.stringify(payload)");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
processor.afterPropertiesSet();
EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
EvaluationContext evaluationContext =
TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
evaluationContext.setVariable("target", new TestTarget());
assertEquals("2", processor.processMessage(new GenericMessage<String>("2")));
assertEquals("2", processor.processMessage(new GenericMessage<>("2")));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithVoidResult() throws Exception {
@SuppressWarnings("unused")
class TestTarget {
public void ping(String input) {
}
}
Expression expression = expressionParser.parseExpression("#target.ping(payload)");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
processor.afterPropertiesSet();
EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
EvaluationContext evaluationContext =
TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
evaluationContext.setVariable("target", new TestTarget());
assertEquals(null, processor.processMessage(new GenericMessage<String>("2")));
assertEquals(null, processor.processMessage(new GenericMessage<>("2")));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithParameterCoercionToNonPrimitive() throws Exception {
class TestTarget {
@SuppressWarnings("unused")
public String find(Resource[] resources) {
return Arrays.asList(resources).toString();
return Arrays.toString(resources);
}
}
Expression expression = expressionParser.parseExpression("#target.find(payload)");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
AbstractApplicationContext applicationContext = new GenericApplicationContext();
processor.setBeanFactory(applicationContext);
IntegrationEvaluationContextFactoryBean factoryBean = new IntegrationEvaluationContextFactoryBean();
factoryBean.setApplicationContext(applicationContext);
applicationContext.getBeanFactory().registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
factoryBean.getObject());
applicationContext.getBeanFactory()
.registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
factoryBean.getObject());
applicationContext.refresh();
processor.afterPropertiesSet();
EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
EvaluationContext evaluationContext =
TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class);
evaluationContext.setVariable("target", new TestTarget());
String result = (String) processor.processMessage(new GenericMessage<String>("classpath*:*.properties"));
assertTrue("Wrong result: " + result, result.contains("log4j.properties"));
String result = processor.processMessage(new GenericMessage<>("classpath*:*-test.xml"));
assertTrue("Wrong result: " + result, result.contains("log4j2-test.xml"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithDollarInBrackets() {
Expression expression = expressionParser.parseExpression("headers['$foo_id']");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
Message<String> message = MessageBuilder.withPayload("foo").setHeader("$foo_id", "abc").build();
assertEquals("abc", processor.processMessage(message));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithDollarPropertyAccess() {
Expression expression = expressionParser.parseExpression("headers.$foo_id");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
Message<String> message = MessageBuilder.withPayload("foo").setHeader("$foo_id", "xyz").build();
assertEquals("xyz", processor.processMessage(message));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithStaticKey() {
Expression expression = expressionParser.parseExpression("headers[headers.ID]");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<UUID> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
GenericMessage<String> message = new GenericMessage<String>("foo");
GenericMessage<String> message = new GenericMessage<>("foo");
assertEquals(message.getHeaders().getId(), processor.processMessage(message));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithBeanAsMethodArgument() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
@@ -182,14 +183,13 @@ public class ExpressionEvaluatingMessageProcessorTests {
context.refresh();
Expression expression = expressionParser.parseExpression("payload.concat(@testString)");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(context);
processor.afterPropertiesSet();
GenericMessage<String> message = new GenericMessage<String>("foo");
GenericMessage<String> message = new GenericMessage<>("foo");
assertEquals("foobar", processor.processMessage(message));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageWithMethodCallOnBean() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
@@ -201,76 +201,83 @@ public class ExpressionEvaluatingMessageProcessorTests {
context.refresh();
Expression expression = expressionParser.parseExpression("@testString.concat(payload)");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(context);
processor.afterPropertiesSet();
GenericMessage<String> message = new GenericMessage<String>("foo");
GenericMessage<String> message = new GenericMessage<>("foo");
assertEquals("barfoo", processor.processMessage(message));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageBadExpression() {
expected.expect(new TypeSafeMatcher<Exception>(Exception.class) {
private Throwable cause;
@Override
public boolean matchesSafely(Exception item) {
logger.debug(item);
cause = item.getCause();
return cause instanceof EvaluationException;
}
@Override
public void describeTo(Description description) {
description.appendText("cause to be EvaluationException but was ").appendValue(cause);
}
});
Expression expression = expressionParser.parseExpression("payload.fixMe()");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertEquals("foo", processor.processMessage(new GenericMessage<String>("foo")));
assertEquals("foo", processor.processMessage(new GenericMessage<>("foo")));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageExpressionThrowsRuntimeException() {
expected.expect(new TypeSafeMatcher<Exception>(Exception.class) {
private Throwable cause;
@Override
public boolean matchesSafely(Exception item) {
logger.debug(item);
cause = item.getCause();
return cause instanceof UnsupportedOperationException;
}
@Override
public void describeTo(Description description) {
description.appendText("cause to be UnsupportedOperationException but was ").appendValue(cause);
}
});
Expression expression = expressionParser.parseExpression("payload.throwRuntimeException()");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertEquals("foo", processor.processMessage(new GenericMessage<TestPayload>(new TestPayload())));
assertEquals("foo", processor.processMessage(new GenericMessage<>(new TestPayload())));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testProcessMessageExpressionThrowsCheckedException() {
expected.expect(new TypeSafeMatcher<Exception>(Exception.class) {
private Throwable cause;
@Override
public boolean matchesSafely(Exception item) {
logger.debug(item);
cause = item.getCause();
return cause instanceof CheckedException;
}
@Override
public void describeTo(Description description) {
description.appendText("cause to be CheckedException but was ").appendValue(cause);
}
});
Expression expression = expressionParser.parseExpression("payload.throwCheckedException()");
ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertEquals("foo", processor.processMessage(new GenericMessage<TestPayload>(new TestPayload())));
assertEquals("foo", processor.processMessage(new GenericMessage<>(new TestPayload())));
}
@@ -288,6 +295,7 @@ public class ExpressionEvaluatingMessageProcessorTests {
public String throwCheckedException() throws Exception {
throw new CheckedException("Expected test exception");
}
}
@SuppressWarnings("serial")

View File

@@ -1,7 +0,0 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %c{1} [%t] : %m%n
log4j.category.org.springframework.integration=WARN

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%t] [%c] - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.integration" level="warn"/>
<Root level="warn">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>