From 1849cda1c1417e15fbf25e44321510016b822fab Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Sat, 7 Nov 2015 11:06:19 -0500 Subject: [PATCH] Extract LogAdjustingTestSupport Also add test debugging to JmsOutboundGatewayTests. --- .../configuration/EnableIntegrationTests.java | 34 +--------- .../test/util/LogAdjustingTestSupport.java | 61 +++++++++++++++++ .../ip/tcp/TcpOutboundGatewayTests.java | 29 +-------- .../jms/JmsOutboundGatewayTests.java | 5 +- .../test/support/LogAdjustingTestSupport.java | 65 +++++++++++++++++++ 5 files changed, 134 insertions(+), 60 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/test/util/LogAdjustingTestSupport.java create mode 100644 spring-integration-test/src/main/java/org/springframework/integration/test/support/LogAdjustingTestSupport.java diff --git a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java index cdca9f8af3..b1aa3456e6 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java @@ -43,15 +43,8 @@ import java.util.concurrent.atomic.AtomicReference; import org.aopalliance.intercept.MethodInterceptor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; import org.hamcrest.Matchers; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.springframework.beans.factory.FactoryBean; @@ -66,8 +59,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.convert.converter.Converter; import org.springframework.core.serializer.support.SerializingConverter; import org.springframework.integration.annotation.Aggregator; @@ -106,6 +97,7 @@ import org.springframework.integration.scheduling.PollerMetadata; import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.support.MutableMessageBuilder; import org.springframework.integration.support.SmartLifecycleRoleController; +import org.springframework.integration.test.util.LogAdjustingTestSupport; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @@ -143,7 +135,7 @@ import reactor.spring.context.config.EnableReactor; classes = {EnableIntegrationTests.ContextConfiguration.class, EnableIntegrationTests.ContextConfiguration2.class}) @RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) -public class EnableIntegrationTests { +public class EnableIntegrationTests extends LogAdjustingTestSupport { @Autowired private ApplicationContext context; @@ -270,28 +262,6 @@ public class EnableIntegrationTests { @Qualifier("enableIntegrationTests.ContextConfiguration2.sendAsyncHandler.serviceActivator") private AbstractEndpoint sendAsyncHandler; - @Rule - public TestName testName = new TestName(); - - private final Log logger = LogFactory.getLog(this.getClass()); - - private final Logger loggerToAdjust = LogManager.getLogger("org.springframework.integration"); - - private Level oldCategory; - - @Before - public void beforeTest() { - this.oldCategory = loggerToAdjust.getEffectiveLevel(); - this.loggerToAdjust.setLevel(Level.TRACE); - this.logger.debug("!!!! Starting the test: " + this.testName.getMethodName() + " !!!!"); - } - - @After - public void afterTest() { - logger.debug("!!!! Finish the test: " + this.testName.getMethodName() + " !!!!"); - this.loggerToAdjust.setLevel(this.oldCategory); - } - @Test public void testAnnotatedServiceActivator() { assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll")); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/test/util/LogAdjustingTestSupport.java b/spring-integration-core/src/test/java/org/springframework/integration/test/util/LogAdjustingTestSupport.java new file mode 100644 index 0000000000..9a7641e2b2 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/test/util/LogAdjustingTestSupport.java @@ -0,0 +1,61 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.test.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TestName; + +/** + * Base class for module tests where logging is set to TRACE for the duration + * of the test and reverted to the previous value. Also logs a start/end + * message. Duplicated from s-i-test to avoid circular dep. + * @author Artem Bilan + * @author Gary Russell + * @since 4.2.2 + * + */ +public class LogAdjustingTestSupport { + + @Rule + public TestName testName = new TestName(); + + protected final Log logger = LogFactory.getLog(this.getClass()); + + private final Logger loggerToAdjust = LogManager.getLogger("org.springframework.integration"); + + private Level oldCategory; + + @Before + public void beforeTest() { + this.oldCategory = loggerToAdjust.getEffectiveLevel(); + this.loggerToAdjust.setLevel(Level.TRACE); + this.logger.debug("!!!! Starting test: " + this.testName.getMethodName() + " !!!!"); + } + + @After + public void afterTest() { + logger.debug("!!!! Finished test: " + this.testName.getMethodName() + " !!!!"); + this.loggerToAdjust.setLevel(this.oldCategory); + } + +} diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java index be0919d4c1..8af58f98c1 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java @@ -52,14 +52,7 @@ import java.util.concurrent.atomic.AtomicReference; import javax.net.ServerSocketFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.log4j.Level; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.TestName; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactory; @@ -77,7 +70,7 @@ import org.springframework.integration.ip.tcp.connection.TcpConnectionSupport; import org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory; import org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory; import org.springframework.integration.support.MessageBuilder; -import org.springframework.integration.test.rule.Log4jLevelAdjuster; +import org.springframework.integration.test.support.LogAdjustingTestSupport; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; @@ -88,25 +81,7 @@ import org.springframework.messaging.support.GenericMessage; * @author Artem Bilan * @since 2.0 */ -public class TcpOutboundGatewayTests { - - @Rule - public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, "org.springframework.integration"); - - @Rule - public TestName testName = new TestName(); - - private final Log logger = LogFactory.getLog(this.getClass()); - - @Before - public void beforeTest() { - logger.debug("!!!! Starting the test: " + this.testName.getMethodName() + " !!!!"); - } - - @After - public void afterTest() { - logger.debug("!!!! Finish the test: " + this.testName.getMethodName() + " !!!!"); - } +public class TcpOutboundGatewayTests extends LogAdjustingTestSupport { @Test public void testGoodNetSingle() throws Exception { diff --git a/spring-integration-jms/src/test/java/org/springframework/integration/jms/JmsOutboundGatewayTests.java b/spring-integration-jms/src/test/java/org/springframework/integration/jms/JmsOutboundGatewayTests.java index 74f11d07d9..4488cd6a70 100644 --- a/spring-integration-jms/src/test/java/org/springframework/integration/jms/JmsOutboundGatewayTests.java +++ b/spring-integration-jms/src/test/java/org/springframework/integration/jms/JmsOutboundGatewayTests.java @@ -52,6 +52,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties; +import org.springframework.integration.test.support.LogAdjustingTestSupport; import org.springframework.integration.test.util.TestUtils; import org.springframework.integration.util.ErrorHandlingTaskExecutor; import org.springframework.jms.JmsException; @@ -68,7 +69,7 @@ import org.springframework.util.ObjectUtils; * @author Artem Bilan * @since 2.2.4 */ -public class JmsOutboundGatewayTests { +public class JmsOutboundGatewayTests extends LogAdjustingTestSupport { final Log logger = LogFactory.getLog(this.getClass()); @@ -212,6 +213,7 @@ public class JmsOutboundGatewayTests { } }; template.send(replyQ, reply); + logger.debug("Sent reply: " + reply); org.springframework.messaging.Message received = queueChannel.receive(20000); assertNotNull(received); assertEquals("bar", received.getPayload()); @@ -262,6 +264,7 @@ public class JmsOutboundGatewayTests { } }; template.send(replyQ, reply); + logger.debug("Sent reply to: " + replyQ); org.springframework.messaging.Message received = queueChannel.receive(20000); assertNotNull(received); assertEquals("bar", received.getPayload()); diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/support/LogAdjustingTestSupport.java b/spring-integration-test/src/main/java/org/springframework/integration/test/support/LogAdjustingTestSupport.java new file mode 100644 index 0000000000..1735667caa --- /dev/null +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/support/LogAdjustingTestSupport.java @@ -0,0 +1,65 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.test.support; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TestName; + +/** + * Base class for module tests where logging is set to TRACE for the duration + * of the test and reverted to the previous value. Also logs a start/end + * message. Duplicated in s-i-core/src/test for use there, to avoid circular dep. + * @author Artem Bilan + * @author Gary Russell + * @since 4.2.2 + * + */ +public class LogAdjustingTestSupport { + + /* + * If you make changes here, consider doing the same in the core version. + */ + + @Rule + public TestName testName = new TestName(); + + protected final Log logger = LogFactory.getLog(this.getClass()); + + private final Logger loggerToAdjust = LogManager.getLogger("org.springframework.integration"); + + private Level oldCategory; + + @Before + public void beforeTest() { + this.oldCategory = loggerToAdjust.getEffectiveLevel(); + this.loggerToAdjust.setLevel(Level.TRACE); + this.logger.debug("!!!! Starting test: " + this.testName.getMethodName() + " !!!!"); + } + + @After + public void afterTest() { + logger.debug("!!!! Finished test: " + this.testName.getMethodName() + " !!!!"); + this.loggerToAdjust.setLevel(this.oldCategory); + } + +}