More test clean up to minimize memory

Probably `@DirtiesContext` would remove application contexts for the JUnit cache
This commit is contained in:
Artem Bilan
2024-07-24 16:45:48 -04:00
parent 201acf5405
commit 6054e8bc9f
12 changed files with 98 additions and 117 deletions

View File

@@ -16,33 +16,34 @@
package org.springframework.integration.config.xml;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.handler.BridgeHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.predicate.MessagePredicate;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Mark Fisher
* @author Iwein Fuld
* @author Artem Bilan
*/
@ContextConfiguration
public class BridgeParserTests extends AbstractJUnit4SpringContextTests {
@SpringJUnitConfig
@DirtiesContext
public class BridgeParserTests {
@Autowired
@Qualifier("pollableChannel")
@@ -95,19 +96,17 @@ public class BridgeParserTests extends AbstractJUnit4SpringContextTests {
assertThat(message).matches(new MessagePredicate(reply));
}
@Test(expected = MessagingException.class)
@Test
public void stopperWithoutReplyHeader() {
Message<?> message = MessageBuilder.withPayload("test3").build();
this.stopperChannel.send(message);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> this.stopperChannel.send(message));
}
@Test
public void bridgeWithSendTimeout() {
BridgeHandler handler =
(BridgeHandler) new DirectFieldAccessor(bridgeWithSendTimeout).getPropertyValue("handler");
MessagingTemplate template =
(MessagingTemplate) new DirectFieldAccessor(handler).getPropertyValue("messagingTemplate");
assertThat(new DirectFieldAccessor(template).getPropertyValue("sendTimeout")).isEqualTo(1234L);
assertThat(TestUtils.getPropertyValue(bridgeWithSendTimeout, "handler.messagingTemplate.sendTimeout"))
.isEqualTo(1234L);
}
}

View File

@@ -18,8 +18,7 @@ package org.springframework.integration.config.xml;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,8 +32,8 @@ import org.springframework.integration.transformer.ClaimCheckOutTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,8 +45,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ClaimCheckParserTests {
@Autowired

View File

@@ -17,36 +17,52 @@
package org.springframework.integration.config.xml;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Jim Moore
* @author Mark Fisher
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ConstructorAutowireTests {
@Test // INT-568
public void testApplicationContextCreation() {
@Autowired
TestService service;
@Autowired
TestEndpoint testEndpoint;
@Test
public void testApplicationContextCreation() throws InterruptedException {
assertThat(this.testEndpoint.consumerLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(this.testEndpoint.result).isEqualTo(this.service.getVal());
}
public static class TestService {
public String getVal() {
return "fooble";
return "test data";
}
}
public static class TestEndpoint {
private CountDownLatch consumerLatch = new CountDownLatch(1);
private String result;
private TestService service;
@Autowired
@@ -59,7 +75,8 @@ public class ConstructorAutowireTests {
}
public void aConsumer(String str) {
// ignore
this.result = str;
this.consumerLatch.countDown();
}
public List<String> aSplitter(List<String> strs) {

View File

@@ -16,34 +16,27 @@
package org.springframework.integration.config.xml;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Mark Fisher
* @author Artem Bilan
*/
public class ContextHierarchyTests {
private ApplicationContext parentContext;
private ApplicationContext childContext;
@Before
public void setupContext() {
String prefix = "/org/springframework/integration/config/xml/ContextHierarchyTests-";
this.parentContext = new ClassPathXmlApplicationContext(prefix + "parent.xml");
this.childContext = new ClassPathXmlApplicationContext(
new String[] {prefix + "child.xml"}, parentContext);
}
@Test // INT-646
@Test
public void inputChannelInParentContext() {
String prefix = "/org/springframework/integration/config/xml/ContextHierarchyTests-";
ConfigurableApplicationContext parentContext = new ClassPathXmlApplicationContext(prefix + "parent.xml");
ConfigurableApplicationContext childContext = new ClassPathXmlApplicationContext(
new String[] {prefix + "child.xml"}, parentContext);
Object parentInput = parentContext.getBean("input");
Object childInput = childContext.getBean("input");
Object endpoint = childContext.getBean("chain");
@@ -51,6 +44,9 @@ public class ContextHierarchyTests {
Object endpointInput = accessor.getPropertyValue("inputChannel");
assertThat(childInput).isEqualTo(parentInput);
assertThat(endpointInput).isEqualTo(parentInput);
parentContext.close();
childContext.close();
}
}

View File

@@ -18,8 +18,7 @@ package org.springframework.integration.config.xml;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
@@ -28,8 +27,8 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,8 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Fisher
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ControlBusChainTests {
@Autowired

View File

@@ -16,8 +16,7 @@
package org.springframework.integration.config.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -27,8 +26,8 @@ import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,8 +35,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Oleg Zhurakousky
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ConverterParserTests {
@Autowired

View File

@@ -16,8 +16,7 @@
package org.springframework.integration.config.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -28,8 +27,8 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Artem Bilan
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class ConverterParserWithExistingConversionServiceTests {
@Autowired
@@ -82,24 +81,11 @@ public class ConverterParserWithExistingConversionServiceTests {
parentContext.close();
}
private static class TestBean1 {
private final String text;
@SuppressWarnings("unused")
TestBean1(String text) {
this.text = text;
}
private record TestBean1(String text) {
}
private static class TestBean2 {
private final String text;
TestBean2(String text) {
this.text = text;
}
private record TestBean2(String text) {
// called by router for channel name
@Override
@@ -109,13 +95,7 @@ public class ConverterParserWithExistingConversionServiceTests {
}
private static class TestBean3 {
private final String text;
TestBean3(String text) {
this.text = text;
}
private record TestBean3(String text) {
// called by router for channel name
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@@ -16,8 +16,7 @@
package org.springframework.integration.config.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
@@ -25,16 +24,17 @@ import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Mark Fisher
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class CronTriggerParserTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 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.
@@ -41,6 +41,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -55,6 +56,7 @@ import static org.mockito.Mockito.mock;
*
*/
@SpringJUnitConfig
@DirtiesContext
public class DelegatingConsumerParserTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 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.
@@ -16,8 +16,7 @@
package org.springframework.integration.config.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.endpoint.EventDrivenConsumer;
@@ -25,8 +24,8 @@ import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.leader.event.OnGrantedEvent;
import org.springframework.integration.leader.event.OnRevokedEvent;
import org.springframework.integration.support.SmartLifecycleRoleController;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,8 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 4.2
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class EndpointRoleParserTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 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,8 +40,6 @@ import org.junit.jupiter.api.BeforeEach;
*/
public class ZookeeperTestSupport {
private static final Log logger = LogFactory.getLog(ZookeeperTestSupport.class);
protected final Log log = LogFactory.getLog(this.getClass());
protected static TestingServer testingServer;
@@ -50,18 +48,12 @@ public class ZookeeperTestSupport {
@BeforeAll
public static void setUpClass() throws Exception {
testingServer = new TestingServer(true);
testingServer = new TestingServer();
}
@AfterAll
public static void tearDownClass() {
try {
testingServer.stop();
}
catch (IOException e) {
logger.warn("Exception thrown while shutting down ZooKeeper: ", e);
}
testingServer.getTempDirectory().delete();
public static void tearDownClass() throws IOException {
testingServer.close();
}
@BeforeEach

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2024 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.
@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.Participant;
import org.apache.curator.utils.CloseableUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -72,9 +73,7 @@ public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
@AfterAll
public static void closeClient() {
if (client != null) {
client.close();
}
CloseableUtils.closeQuietly(client);
}
@Test