Extract LogAdjustingTestSupport

Also add test debugging to JmsOutboundGatewayTests.
This commit is contained in:
Gary Russell
2015-11-07 11:06:19 -05:00
parent b041fe9992
commit 1849cda1c1
5 changed files with 134 additions and 60 deletions

View File

@@ -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"));

View File

@@ -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);
}
}

View File

@@ -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 {

View File

@@ -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());

View File

@@ -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);
}
}