From 448f9a52d21b1fca27e034013581d5b16a2573cd Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 29 Jul 2015 18:01:22 -0400 Subject: [PATCH] Debugging for TCP FailOver Tests Add LogLevelAdjuster; enable TRACE debugging for this test class. --- build.gradle | 1 + .../OutboundGatewayIntegrationTests.java | 5 +- .../FailoverClientConnectionFactoryTests.java | 7 ++ .../test/rule/Log4jLevelAdjuster.java | 97 +++++++++++++++++++ .../integration/test/rule/package-info.java | 4 + 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100755 spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jLevelAdjuster.java create mode 100644 spring-integration-test/src/main/java/org/springframework/integration/test/rule/package-info.java diff --git a/build.gradle b/build.gradle index 911eaffabc..f8bf7c44ee 100644 --- a/build.gradle +++ b/build.gradle @@ -266,6 +266,7 @@ project('spring-integration-test') { exclude group: 'org.hamcrest', module: 'hamcrest-core' } compile "org.springframework:spring-test:$springVersion" + compile "log4j:log4j:$log4jVersion" // TEMPORARY - DO NOT RELEASE } } diff --git a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/config/OutboundGatewayIntegrationTests.java b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/config/OutboundGatewayIntegrationTests.java index 6792f8dd5c..c052ba81df 100644 --- a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/config/OutboundGatewayIntegrationTests.java +++ b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/config/OutboundGatewayIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-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. @@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Mark Fisher * @author Artem Bilan + * @author Gary Russell * @since 2.1 */ @ContextConfiguration @@ -54,7 +55,7 @@ public class OutboundGatewayIntegrationTests { public void testOutboundInboundGateways() throws Exception { String payload = "foo"; this.toRabbit.send(new GenericMessage(payload)); - Message receive = this.fromRabbit.receive(1000); + Message receive = this.fromRabbit.receive(10000); assertNotNull(receive); assertEquals(payload.toUpperCase(), receive.getPayload()); } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java index 5688d12902..9cae76387a 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java @@ -36,6 +36,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import org.apache.log4j.Level; +import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; @@ -50,6 +52,7 @@ import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.ip.tcp.TcpInboundGateway; import org.springframework.integration.ip.tcp.TcpOutboundGateway; import org.springframework.integration.ip.util.TestingUtilities; +import org.springframework.integration.test.rule.Log4jLevelAdjuster; import org.springframework.integration.test.util.SocketUtils; import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.Message; @@ -66,6 +69,10 @@ import org.springframework.messaging.support.GenericMessage; */ public class FailoverClientConnectionFactoryTests { + @Rule + public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, + "org.springframework.integration.ip.tcp"); + @Test public void testFailoverGood() throws Exception { AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class); diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jLevelAdjuster.java b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jLevelAdjuster.java new file mode 100755 index 0000000000..a03a95dbd2 --- /dev/null +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jLevelAdjuster.java @@ -0,0 +1,97 @@ +/* + * 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.rule; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +/** + * A JUnit method @Rule that changes the logger level for a set of classes + * or packages + * while a test method is running. Useful for performance or scalability tests + * where we don't want to generate a large log in a tight inner loop, or + * enabling debug logging for a test case. + * + * @author Dave Syer + * @author Gary Russell + * + */ +public class Log4jLevelAdjuster implements MethodRule { + + private static final Log logger = LogFactory.getLog(Log4jLevelAdjuster.class); + + private final Class[] classes; + + private final Level level; + + private final String[] categories; + + public Log4jLevelAdjuster(Level level, Class... classes) { + this.level = level; + this.classes = classes; + this.categories = new String[0]; + } + + public Log4jLevelAdjuster(Level level, String... categories) { + this.level = level; + this.classes = new Class[0]; + this.categories = categories; + } + + @Override + public Statement apply(final Statement base, FrameworkMethod method, Object target) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + logger.debug("Overriding log level setting for: " + Arrays.asList(classes)); + Map, Level> oldLevels = new HashMap, Level>(); + for (Class cls : classes) { + oldLevels.put(cls, LogManager.getLogger(cls).getEffectiveLevel()); + LogManager.getLogger(cls).setLevel(level); + } + Map oldCatLevels = new HashMap(); + for (String category : categories) { + oldCatLevels.put(category, LogManager.getLogger(category).getEffectiveLevel()); + LogManager.getLogger(category).setLevel(level); + } + try { + base.evaluate(); + } + finally { + logger.debug("Restoring log level setting for: " + Arrays.asList(classes) + " and " + + Arrays.asList(categories)); + // raw Class type used to avoid http://bugs.sun.com/view_bug.do?bug_id=6682380 + for (@SuppressWarnings("rawtypes") Class cls : classes) { + LogManager.getLogger(cls).setLevel(oldLevels.get(cls)); + } + for (String category : categories) { + LogManager.getLogger(category).setLevel(oldCatLevels.get(category)); + } + } + } + }; + } + +} diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/rule/package-info.java b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/package-info.java new file mode 100644 index 0000000000..f42e7946cb --- /dev/null +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/package-info.java @@ -0,0 +1,4 @@ +/** + * Provides various test rules. + */ +package org.springframework.integration.test.rule;