From 48edc94a1ee6e684d8abd5d29786cb4b639645a9 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 13 Jun 2017 16:51:28 -0400 Subject: [PATCH] INT-4292: More Diagnostics https://jira.spring.io/browse/INT-4292 Add class-level log adjuster. --- .../client/StompServerIntegrationTests.java | 17 +-- ...annelAdapterWebSocketIntegrationTests.java | 17 +-- .../test/rule/Log4jClassLevelAdjuster.java | 103 ++++++++++++++++++ 3 files changed, 122 insertions(+), 15 deletions(-) create mode 100644 spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jClassLevelAdjuster.java diff --git a/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java b/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java index 10e2ecf87c..5a55250a6c 100644 --- a/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java +++ b/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 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. @@ -26,8 +26,10 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import org.apache.activemq.broker.BrokerService; +import org.apache.log4j.Level; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; import org.springframework.context.ApplicationEvent; @@ -50,7 +52,7 @@ import org.springframework.integration.stomp.event.StompSessionConnectedEvent; import org.springframework.integration.stomp.inbound.StompInboundChannelAdapter; import org.springframework.integration.stomp.outbound.StompMessageHandler; import org.springframework.integration.support.converter.PassThruMessageConverter; -import org.springframework.integration.test.support.LogAdjustingTestSupport; +import org.springframework.integration.test.rule.Log4jClassLevelAdjuster; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageDeliveryException; @@ -67,18 +69,17 @@ import org.springframework.util.SocketUtils; * @author Gary Russell * @since 4.2 */ -public class StompServerIntegrationTests extends LogAdjustingTestSupport { +public class StompServerIntegrationTests { + + @ClassRule + public static Log4jClassLevelAdjuster adjuster = new Log4jClassLevelAdjuster(Level.TRACE, "org.springframework", "org.springframework.integration.stomp", + "org.apache.activemq.broker", "reactor.io", "io.netty"); private static BrokerService activeMQBroker; private static Reactor2TcpStompClient stompClient; - public StompServerIntegrationTests() { - super("org.springframework", "org.springframework.integration.stomp", - "org.apache.activemq.broker", "reactor.io", "io.netty"); - } - @BeforeClass public static void setup() throws Exception { int port = SocketUtils.findAvailableTcpPort(61613); diff --git a/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java b/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java index 07cfa6c1b6..369378e0e9 100644 --- a/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java +++ b/spring-integration-stomp/src/test/java/org/springframework/integration/stomp/inbound/StompInboundChannelAdapterWebSocketIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 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. @@ -27,6 +27,8 @@ import static org.junit.Assert.assertTrue; import java.util.Collections; import java.util.Map; +import org.apache.log4j.Level; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; @@ -50,7 +52,7 @@ import org.springframework.integration.stomp.event.StompConnectionFailedEvent; import org.springframework.integration.stomp.event.StompIntegrationEvent; import org.springframework.integration.stomp.event.StompReceiptEvent; import org.springframework.integration.stomp.event.StompSessionConnectedEvent; -import org.springframework.integration.test.support.LogAdjustingTestSupport; +import org.springframework.integration.test.rule.Log4jClassLevelAdjuster; import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHandlingException; @@ -90,12 +92,17 @@ import org.springframework.web.socket.sockjs.client.WebSocketTransport; /** * @author Artem Bilan + * @author Gary Russell * @since 4.2 */ @ContextConfiguration(classes = StompInboundChannelAdapterWebSocketIntegrationTests.ContextConfiguration.class) @RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext -public class StompInboundChannelAdapterWebSocketIntegrationTests extends LogAdjustingTestSupport { +public class StompInboundChannelAdapterWebSocketIntegrationTests { + + @ClassRule + public static Log4jClassLevelAdjuster adjuster = new Log4jClassLevelAdjuster(Level.TRACE, "org.springframework", + "org.springframework.integration.stomp"); @Value("#{server.serverContext}") private ConfigurableApplicationContext serverContext; @@ -115,10 +122,6 @@ public class StompInboundChannelAdapterWebSocketIntegrationTests extends LogAdju @Autowired private StompInboundChannelAdapter stompInboundChannelAdapter; - public StompInboundChannelAdapterWebSocketIntegrationTests() { - super("org.springframework", "org.springframework.integration.stomp"); - } - @Test public void testWebSocketStompClient() throws Exception { Message eventMessage = this.stompEvents.receive(10000); diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jClassLevelAdjuster.java b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jClassLevelAdjuster.java new file mode 100644 index 0000000000..0ecc8a2c1b --- /dev/null +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/rule/Log4jClassLevelAdjuster.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017 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.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +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.TestWatcher; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Similar to {@code Log4jLevelAdjuster} that can be applied as a @ClassRule but you + * will not get a log message indicating breaks between tests. + * + * @author Dave Syer + * @author Gary Russell + * + */ +public class Log4jClassLevelAdjuster extends TestWatcher { + + private static final Log logger = LogFactory.getLog(Log4jClassLevelAdjuster.class); + + private final Class[] classes; + + private final Level level; + + private final String[] categories; + + public Log4jClassLevelAdjuster(Level level, Class... classes) { + this.level = level; + this.classes = classes; + this.categories = new String[0]; + } + + public Log4jClassLevelAdjuster(Level level, String... categories) { + this.level = level; + this.classes = new Class[0]; + Set cats = new LinkedHashSet(Arrays.asList(categories)); + cats.add(getClass().getPackage().getName()); + this.categories = new ArrayList(cats).toArray(new String[cats.size()]); + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + 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); + } + logger.debug("++++++++++++++++++++++++++++ " + + "Overridden log level setting for: " + Arrays.asList(classes) + " and " + + Arrays.asList(categories) + " for test " + description.getDisplayName()); + try { + base.evaluate(); + } + finally { + logger.debug("++++++++++++++++++++++++++++ " + + "Restoring log level setting for: " + Arrays.asList(classes) + " and " + + Arrays.asList(categories) + " for test " + description.getDisplayName()); + // 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)); + } + } + } + }; + } + +}