More @DirtiesContext in tests to mitigate memory

* Some other cleanups and modernizations for affected test class and their configurations
This commit is contained in:
Artem Bilan
2024-08-28 14:25:38 -04:00
parent eb07fda61b
commit 730e49adec
22 changed files with 185 additions and 303 deletions

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.
@@ -35,6 +35,7 @@ import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.util.ReflectionTestUtils;
@@ -55,6 +56,7 @@ import static org.mockito.Mockito.when;
* @since 2.1
*/
@SpringJUnitConfig
@DirtiesContext
public class OutboundGatewayTests {
private static final ExpressionParser PARSER = new SpelExpressionParser();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2023 the original author or authors.
* Copyright 2018-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.
@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.integration.amqp.dsl.Amqp;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -55,6 +56,7 @@ import static org.mockito.Mockito.verify;
*
*/
@SpringJUnitConfig
@DirtiesContext
public class BoundRabbitChannelAdviceTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-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.
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.cassandra.outbound.CassandraMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 6.0
*/
@SpringJUnitConfig
@DirtiesContext
class CassandraOutboundAdapterParserTests {
@Autowired

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.
@@ -38,7 +38,7 @@ import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.event.ApplicationEvents;
import org.springframework.test.context.event.RecordApplicationEvents;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -55,7 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@SpringJUnitConfig
@RecordApplicationEvents
@ContextConfiguration
@DirtiesContext
public class EventOutboundChannelAdapterParserTests {
@Autowired

View File

@@ -76,6 +76,7 @@ public class ApplicationEventListeningMessageProducerTests {
Message<?> message3 = channel.receive(20);
assertThat(message3).isNotNull();
assertThat(((ApplicationEvent) message3.getPayload()).getSource()).isEqualTo("event2");
adapter.stop();
}
@Test
@@ -106,6 +107,7 @@ public class ApplicationEventListeningMessageProducerTests {
adapter.setEventTypes(null, null);
assertThat(adapter.supportsEventType(ResolvableType.forClass(TestApplicationEvent1.class))).isTrue();
assertThat(adapter.supportsEventType(ResolvableType.forClass(TestApplicationEvent2.class))).isTrue();
adapter.stop();
}
@Test
@@ -178,6 +180,7 @@ public class ApplicationEventListeningMessageProducerTests {
Message<?> message2 = channel.receive(20);
assertThat(message2).isNotNull();
assertThat(message2.getPayload()).isEqualTo("test");
adapter.stop();
}
@Test
@@ -192,6 +195,7 @@ public class ApplicationEventListeningMessageProducerTests {
Message<?> message2 = channel.receive(20);
assertThat(message2).isNotNull();
assertThat(message2.getPayload()).isEqualTo("test");
adapter.stop();
}
@Test
@@ -216,6 +220,7 @@ public class ApplicationEventListeningMessageProducerTests {
adapter.setErrorChannel(null);
assertThatExceptionOfType(MessageHandlingException.class)
.isThrownBy(() -> adapter.onApplicationEvent(new TestApplicationEvent1()));
adapter.stop();
}
@Test

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.
@@ -22,6 +22,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import com.rometools.rome.feed.synd.SyndEntry;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -46,15 +47,24 @@ import static org.mockito.Mockito.mock;
*/
public class FeedEntryMessageSourceTests {
static File metadataStoreFile;
@BeforeEach
public void prepare() {
File metadataStoreFile = new File(System.getProperty("java.io.tmpdir") + "/spring-integration/",
metadataStoreFile = new File(System.getProperty("java.io.tmpdir") + "/spring-integration/",
"metadata-store.properties");
if (metadataStoreFile.exists()) {
metadataStoreFile.delete();
}
}
@AfterAll
static void tearDown() {
if (metadataStoreFile.exists()) {
metadataStoreFile.delete();
}
}
@Test
public void testFailureWhenNotInitialized() throws Exception {
URL url = new ClassPathResource("org/springframework/integration/feed/sample.rss").getURL();

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.
@@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.FileCopyUtils;
@@ -38,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Gary Russell
*/
@SpringJUnitConfig
@DirtiesContext
public class FileOutboundChannelAdapterInsideChainTests {
static final String TEST_FILE_NAME = FileOutboundChannelAdapterInsideChainTests.class.getSimpleName();

View File

@@ -20,12 +20,10 @@ import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,8 +32,8 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
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 org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -45,8 +43,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Fisher
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileOutboundGatewayIntegrationTests {
@Qualifier("copyInput")
@@ -76,26 +74,10 @@ public class FileOutboundGatewayIntegrationTests {
File sourceFile;
@TempDir
static File workDir;
@BeforeClass
public static void setupClass() {
workDir = new File(System.getProperty("java.io.tmpdir"), "anyDir");
workDir.mkdir();
workDir.deleteOnExit();
}
@AfterClass
public static void cleanUp() {
if (workDir != null && workDir.exists()) {
for (File file : workDir.listFiles()) {
file.delete();
}
}
workDir.delete();
}
@Before
@BeforeEach
public void setUp() throws Exception {
sourceFile = File.createTempFile("anyFile", ".txt");
sourceFile.deleteOnExit();
@@ -104,7 +86,7 @@ public class FileOutboundGatewayIntegrationTests {
message = MessageBuilder.withPayload(sourceFile).build();
}
@After
@AfterEach
public void tearDown() {
sourceFile.delete();
}

View File

@@ -7,7 +7,7 @@
<!-- under test -->
<bean id="pollableFileSource" class="org.springframework.integration.file.FileReadingMessageSource"
p:directory="file:${java.io.tmpdir}/FileReadingMessageSourceIntegrationTests"
p:directory="#{T(org.springframework.integration.file.FileReadingMessageSourceIntegrationTests).inputDir}"
p:filter-ref="compositeFilter"/>
<!-- customized filter -->

View File

@@ -17,21 +17,20 @@
package org.springframework.integration.file;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,71 +39,39 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Gary Russell
* @author Artem Bilan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringJUnitConfig
@DirtiesContext
public class FileReadingMessageSourceIntegrationTests {
@Autowired
FileReadingMessageSource pollableFileSource;
private static File inputDir;
@TempDir
public static File inputDir;
@AfterClass
public static void cleanUp() throws Throwable {
if (inputDir.exists()) {
inputDir.delete();
}
}
@BeforeClass
public static void setupInputDir() {
inputDir = new File(System.getProperty("java.io.tmpdir") + "/"
+ FileReadingMessageSourceIntegrationTests.class.getSimpleName());
inputDir.mkdir();
clean();
}
@AfterClass
public static void tearDown() {
clean();
inputDir.delete();
}
private static void clean() {
File[] files = inputDir.listFiles();
for (File file : files) {
file.delete();
}
}
@Before
public void generateTestFiles() throws Exception {
@BeforeEach
public void generateTestFiles() throws IOException {
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
}
@After
public void cleanupInputDir() throws Exception {
@AfterEach
public void cleanupInputDir() {
File[] listFiles = inputDir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
listFiles[i].delete();
for (File listFile : listFiles) {
listFile.delete();
}
}
@AfterClass
public static void removeInputDir() throws Exception {
inputDir.delete();
}
@Test
public void configured() throws Exception {
public void configured() {
DirectFieldAccessor accessor = new DirectFieldAccessor(pollableFileSource);
assertThat(accessor.getPropertyValue("directory")).isEqualTo(inputDir);
}
@Test
public void getFiles() throws Exception {
public void getFiles() {
Message<File> received1 = pollableFileSource.receive();
assertThat(received1).as("This should return the first message").isNotNull();
Message<File> received2 = pollableFileSource.receive();
@@ -117,7 +84,7 @@ public class FileReadingMessageSourceIntegrationTests {
}
@Test
public void parallelRetrieval() throws Exception {
public void parallelRetrieval() {
Message<File> received1 = pollableFileSource.receive();
Message<File> received2 = pollableFileSource.receive();
Message<File> received3 = pollableFileSource.receive();
@@ -127,7 +94,7 @@ public class FileReadingMessageSourceIntegrationTests {
}
@Test
public void inputDirExhausted() throws Exception {
public void inputDirExhausted() {
assertThat(pollableFileSource.receive()).isNotNull();
assertThat(pollableFileSource.receive()).isNotNull();
Message<File> receive = pollableFileSource.receive();
@@ -141,7 +108,7 @@ public class FileReadingMessageSourceIntegrationTests {
@Test
@Repeat(5)
public void concurrentProcessing() throws Exception {
public void concurrentProcessing() {
CountDownLatch go = new CountDownLatch(1);
Runnable successfulConsumer = () -> {
Message<File> received = pollableFileSource.receive();

View File

@@ -7,7 +7,7 @@
<!-- under test -->
<bean id="pollableFileSource" class="org.springframework.integration.file.FileReadingMessageSource"
p:directory="file:${java.io.tmpdir}/FileReadingMessageSourcePersistentFilterIntegrationTests"
p:directory="#{T(org.springframework.integration.file.FileReadingMessageSourcePersistentFilterIntegrationTests).inputDir}"
p:filter-ref="persistentFilter"/>
<!-- persistent filter -->
@@ -18,7 +18,7 @@
<bean id="ppms" class="org.springframework.integration.metadata.PropertiesPersistingMetadataStore">
<property name="baseDirectory"
value="#{T(System).getProperty('java.io.tmpdir') + T(java.io.File).separator + 'FileReadingMessageSourcePersistentFilterIntegrationTests.meta'}"/>
value="#{T(org.springframework.integration.file.FileReadingMessageSourcePersistentFilterIntegrationTests).inputDir.path}"/>
</bean>
</beans>

View File

@@ -18,86 +18,57 @@ package org.springframework.integration.file;
import java.io.File;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Iwein Fuld
* @author Gary Russell
* @author Artem Bilan
*/
@SpringJUnitConfig
@DirtiesContext
public class FileReadingMessageSourcePersistentFilterIntegrationTests {
AbstractApplicationContext context;
@Autowired
FileReadingMessageSource pollableFileSource;
private static File inputDir;
@TempDir
public static File inputDir;
@AfterClass
public static void cleanUp() throws Throwable {
if (inputDir.exists()) {
inputDir.delete();
}
}
@BeforeClass
public static void setupInputDir() {
inputDir = new File(System.getProperty("java.io.tmpdir") + "/"
+ FileReadingMessageSourcePersistentFilterIntegrationTests.class.getSimpleName());
inputDir.mkdir();
}
@Before
@BeforeEach
public void generateTestFiles() throws Exception {
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
this.loadContextAndGetMessageSource();
}
private void loadContextAndGetMessageSource() {
this.context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-context.xml",
this.getClass());
this.pollableFileSource = context.getBean(FileReadingMessageSource.class);
}
@After
public void cleanoutInputDir() throws Exception {
@AfterEach
public void cleanupInputDir() {
File[] listFiles = inputDir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
listFiles[i].delete();
for (File listFile : listFiles) {
listFile.delete();
}
}
@AfterClass
public static void removeInputDir() throws Exception {
inputDir.delete();
File persistDir = new File(System.getProperty("java.io.tmpdir") + "/"
+ FileReadingMessageSourcePersistentFilterIntegrationTests.class.getSimpleName()
+ ".meta");
File persist = new File(persistDir, "metadata-store.properties");
persist.delete();
persistDir.delete();
}
@Test
public void configured() throws Exception {
public void configured() {
DirectFieldAccessor accessor = new DirectFieldAccessor(this.pollableFileSource);
assertThat(accessor.getPropertyValue("directory")).isEqualTo(inputDir);
}
@Test
public void getFiles() throws Exception {
public void getFiles() {
Message<File> received1 = this.pollableFileSource.receive();
assertThat(received1).as("This should return the first message").isNotNull();
Message<File> received2 = this.pollableFileSource.receive();
@@ -107,12 +78,9 @@ public class FileReadingMessageSourcePersistentFilterIntegrationTests {
assertThat(received2.getPayload()).as(received1 + " == " + received2).isNotSameAs(received1.getPayload());
assertThat(received3.getPayload()).as(received1 + " == " + received3).isNotSameAs(received1.getPayload());
assertThat(received3.getPayload()).as(received2 + " == " + received3).isNotSameAs(received2.getPayload());
this.context.close();
loadContextAndGetMessageSource();
Message<File> received4 = this.pollableFileSource.receive();
assertThat(received4).isNull();
this.context.close();
}
}

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.
@@ -20,9 +20,8 @@ import java.io.File;
import java.util.Iterator;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;
@@ -33,8 +32,8 @@ import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.ExpressionFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Iwein Fuld
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterWithClasspathInPropertiesTests {
@Autowired
@@ -54,7 +53,7 @@ public class FileInboundChannelAdapterWithClasspathInPropertiesTests {
private DirectFieldAccessor accessor;
@Before
@BeforeEach
public void init() {
accessor = new DirectFieldAccessor(source);
}

View File

@@ -19,8 +19,7 @@ package org.springframework.integration.file.config;
import java.io.File;
import java.util.Set;
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,17 +32,19 @@ import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.integration.test.util.TestUtils;
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 Iwein Fuld
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterWithPatternParserTests {
@Autowired
@@ -53,12 +54,8 @@ public class FileInboundChannelAdapterWithPatternParserTests {
@Qualifier("adapterWithPattern.adapter")
private AbstractEndpoint endpoint;
private DirectFieldAccessor accessor;
@Autowired
public void setSource(FileReadingMessageSource source) {
this.accessor = new DirectFieldAccessor(source);
}
FileReadingMessageSource source;
@Test
public void channelName() {
@@ -75,35 +72,30 @@ public class FileInboundChannelAdapterWithPatternParserTests {
@Test
public void inputDirectory() {
File expected = new File(System.getProperty("java.io.tmpdir"));
File actual = (File) accessor.getPropertyValue("directory");
File actual = TestUtils.getPropertyValue(this.source, "directory", File.class);
assertThat(actual).isEqualTo(expected);
}
@Test
public void compositeFilterType() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
assertThat(scannerAccessor.getPropertyValue("filter") instanceof CompositeFileListFilter).isTrue();
FileListFilter<?> filter = TestUtils.getPropertyValue(this.source, "scanner.filter", FileListFilter.class);
assertThat(filter).isInstanceOf(CompositeFileListFilter.class);
}
@Test
@SuppressWarnings("unchecked")
public void compositeFilterSetSize() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
Set<FileListFilter<File>> filters = (Set<FileListFilter<File>>) new DirectFieldAccessor(
scannerAccessor.getPropertyValue("filter")).getPropertyValue("fileFilters");
assertThat(filters.size()).isEqualTo(2);
assertThat((Set<?>) TestUtils.getPropertyValue(this.source, "scanner.filter.fileFilters")).hasSize(2);
}
@Test
@SuppressWarnings("unchecked")
public void acceptOnceFilter() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
Set<FileListFilter<File>> filters = (Set<FileListFilter<File>>) new DirectFieldAccessor(
scannerAccessor.getPropertyValue("filter")).getPropertyValue("fileFilters");
var filters = (Set<FileListFilter<File>>) TestUtils.getPropertyValue(this.source, "scanner.filter.fileFilters");
boolean hasAcceptOnceFilter = false;
for (FileListFilter<File> filter : filters) {
if (filter instanceof AcceptOnceFileListFilter) {
hasAcceptOnceFilter = true;
break;
}
}
assertThat(hasAcceptOnceFilter).as("expected AcceptOnceFileListFilter").isTrue();
@@ -112,9 +104,7 @@ public class FileInboundChannelAdapterWithPatternParserTests {
@Test
@SuppressWarnings("unchecked")
public void patternFilter() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
Set<FileListFilter<?>> filters = (Set<FileListFilter<?>>) new DirectFieldAccessor(
scannerAccessor.getPropertyValue("filter")).getPropertyValue("fileFilters");
var filters = (Set<FileListFilter<File>>) TestUtils.getPropertyValue(this.source, "scanner.filter.fileFilters");
String pattern = null;
for (FileListFilter<?> filter : filters) {
if (filter instanceof SimplePatternFileListFilter) {
@@ -122,7 +112,7 @@ public class FileInboundChannelAdapterWithPatternParserTests {
}
}
assertThat(pattern).as("expected SimplePatternFileListFilterTest").isNotNull();
assertThat(pattern.toString()).isEqualTo("*.txt");
assertThat(pattern).isEqualTo("*.txt");
}
}

View File

@@ -21,8 +21,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.List;
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,9 @@ import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,8 +42,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Fisher
* @author Gunnar Hillert
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
@Autowired
@@ -70,7 +70,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
}
@Test
public void filterAndFalse() throws Exception {
public void filterAndFalse() {
FileListFilter<?> filter = this.extractFilter("filterAndFalse");
assertThat(filter instanceof CompositeFileListFilter).isFalse();
assertThat(filter).isSameAs(testFilter);
@@ -78,7 +78,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
@Test
@SuppressWarnings("unchecked")
public void patternAndNull() throws Exception {
public void patternAndNull() {
FileListFilter<?> filter = this.extractFilter("patternAndNull");
assertThat(filter instanceof CompositeFileListFilter).isTrue();
Collection<FileListFilter<File>> filters = (Collection<FileListFilter<File>>)
@@ -90,7 +90,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
@Test
@SuppressWarnings("unchecked")
public void patternAndTrue() throws Exception {
public void patternAndTrue() {
FileListFilter<?> filter = this.extractFilter("patternAndTrue");
assertThat(filter instanceof CompositeFileListFilter).isTrue();
Collection<FileListFilter<File>> filters = (Collection<FileListFilter<File>>)
@@ -101,14 +101,14 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
}
@Test
public void patternAndFalse() throws Exception {
public void patternAndFalse() {
FileListFilter<File> filter = this.extractFilter("patternAndFalse");
assertThat(filter instanceof CompositeFileListFilter).isFalse();
assertThat(filter).isInstanceOf(SimplePatternFileListFilter.class);
}
@Test
public void defaultAndNull() throws Exception {
public void defaultAndNull() {
FileListFilter<File> filter = this.extractFilter("defaultAndNull");
assertThat(filter).isNotNull();
assertThat(filter instanceof CompositeFileListFilter).isFalse();
@@ -121,7 +121,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
}
@Test
public void defaultAndTrue() throws Exception {
public void defaultAndTrue() {
FileListFilter<File> filter = this.extractFilter("defaultAndTrue");
assertThat(filter instanceof CompositeFileListFilter).isFalse();
assertThat(filter instanceof AcceptOnceFileListFilter).isTrue();
@@ -132,7 +132,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
}
@Test
public void defaultAndFalse() throws Exception {
public void defaultAndFalse() {
FileListFilter<File> filter = this.extractFilter("defaultAndFalse");
assertThat(filter).isNotNull();
assertThat(filter instanceof CompositeFileListFilter).isFalse();
@@ -145,13 +145,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
@SuppressWarnings("unchecked")
private FileListFilter<File> extractFilter(String beanName) {
return (FileListFilter<File>)
new DirectFieldAccessor(
new DirectFieldAccessor(
new DirectFieldAccessor(context.getBean(beanName))
.getPropertyValue("source"))
.getPropertyValue("scanner"))
.getPropertyValue("filter");
return (FileListFilter<File>) TestUtils.getPropertyValue(context.getBean(beanName), "source.scanner.filter");
}
}

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.
@@ -19,33 +19,30 @@ package org.springframework.integration.file.config;
import java.io.File;
import java.util.List;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.HeadDirectoryScanner;
import org.springframework.integration.test.util.TestUtils;
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 Gunnar Hillert
* @author Gary Russell
* @author Artem Bilan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterWithQueueSizeTests {
private static final String PATHNAME = System.getProperty("java.io.tmpdir") + "/"
+ FileInboundChannelAdapterWithQueueSizeTests.class.getSimpleName();
@TempDir
private static File inputDir;
@Autowired
@@ -56,27 +53,7 @@ public class FileInboundChannelAdapterWithQueueSizeTests {
@Qualifier("inputDirPollerSimpleFilter.adapter.source")
FileReadingMessageSource source2;
@BeforeClass
public static void setupInputDir() {
inputDir = new File(PATHNAME);
inputDir.mkdir();
clean();
}
@AfterClass
public static void tearDown() {
clean();
inputDir.delete();
}
private static void clean() {
File[] files = inputDir.listFiles();
for (File file : files) {
file.delete();
}
}
@Before
@BeforeEach
public void generateTestFiles() throws Exception {
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
File.createTempFile("test", null, inputDir).setLastModified(System.currentTimeMillis() - 1000);
@@ -87,15 +64,15 @@ public class FileInboundChannelAdapterWithQueueSizeTests {
public void queueSize() {
HeadDirectoryScanner scanner1 = TestUtils.getPropertyValue(source1, "scanner", HeadDirectoryScanner.class);
HeadDirectoryScanner scanner2 = TestUtils.getPropertyValue(source2, "scanner", HeadDirectoryScanner.class);
List<File> files = scanner1.listFiles(new File(PATHNAME));
List<File> files = scanner1.listFiles(inputDir);
assertThat(files.size()).isEqualTo(2);
files = scanner2.listFiles(new File(PATHNAME));
files = scanner2.listFiles(inputDir);
assertThat(files.size()).isEqualTo(2);
files.get(0).delete();
files.get(1).delete();
files = scanner1.listFiles(new File(PATHNAME));
files = scanner1.listFiles(inputDir);
assertThat(files.size()).isEqualTo(1);
files = scanner2.listFiles(new File(PATHNAME));
files = scanner2.listFiles(inputDir);
assertThat(files.size()).isEqualTo(1);
}

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.
@@ -19,17 +19,15 @@ package org.springframework.integration.file.config;
import java.util.Set;
import java.util.regex.Pattern;
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;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.RegexPatternFileListFilter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,32 +35,26 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Fisher
* @author Iwein Fuld
* @author Gunnar Hillert
* @author Artem Bilan
*
* @see org.springframework.integration.file.config.FileInboundChannelAdapterWithPatternParserTests
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileInboundChannelAdapterWithRegexPatternParserTests {
private DirectFieldAccessor accessor;
@Autowired(required = true)
public void setSource(FileReadingMessageSource source) {
this.accessor = new DirectFieldAccessor(source);
}
@Autowired
FileReadingMessageSource source;
@Test
@SuppressWarnings("unchecked")
public void regexFilter() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
Object extractedFilter = scannerAccessor.getPropertyValue("filter");
assertThat(extractedFilter).isInstanceOf(CompositeFileListFilter.class);
Set<FileListFilter<?>> filters = (Set<FileListFilter<?>>) new DirectFieldAccessor(
extractedFilter).getPropertyValue("fileFilters");
var filters = (Set<FileListFilter<?>>) TestUtils.getPropertyValue(this.source, "scanner.filter.fileFilters");
Pattern pattern = null;
for (FileListFilter<?> filter : filters) {
if (filter instanceof RegexPatternFileListFilter) {
pattern = (Pattern) new DirectFieldAccessor(filter).getPropertyValue("pattern");
pattern = TestUtils.getPropertyValue(filter, "pattern", Pattern.class);
break;
}
}
assertThat(pattern).as("expected PatternMatchingFileListFilter").isNotNull();

View File

@@ -18,17 +18,17 @@ package org.springframework.integration.file.config;
import java.io.File;
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;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.expression.Expression;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageHandler;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,11 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Iwein Fuld
* @author Mark Fisher
* @author Gunnar Hillert
* @author Artem Bilan
*
* @since 1.0.3
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileOutboundAdaptersWithClasspathInPropertiesTests {
@Autowired
@@ -53,11 +54,11 @@ public class FileOutboundAdaptersWithClasspathInPropertiesTests {
@Test
public void outboundChannelAdapter() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(
new DirectFieldAccessor(adapter).getPropertyValue("handler"));
MessageHandler handler = adapter.getHandler();
File expected = new ClassPathResource("").getFile();
Expression destinationDirectoryExpression = (Expression) accessor.getPropertyValue("destinationDirectoryExpression");
var destinationDirectoryExpression =
TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class);
File actual = new File(destinationDirectoryExpression.getExpressionString());
assertThat(actual).as("'destinationDirectory' should be set").isEqualTo(expected);
@@ -65,11 +66,11 @@ public class FileOutboundAdaptersWithClasspathInPropertiesTests {
@Test
public void outboundGateway() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(
new DirectFieldAccessor(gateway).getPropertyValue("handler"));
MessageHandler handler = gateway.getHandler();
File expected = new ClassPathResource("").getFile();
Expression destinationDirectoryExpression = (Expression) accessor.getPropertyValue("destinationDirectoryExpression");
var destinationDirectoryExpression =
TestUtils.getPropertyValue(handler, "destinationDirectoryExpression", Expression.class);
File actual = new File(destinationDirectoryExpression.getExpressionString());
assertThat(actual).as("'destinationDirectory' should be set").isEqualTo(expected);

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,10 +16,9 @@
package org.springframework.integration.file.config;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
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;
@@ -27,8 +26,7 @@ import org.springframework.integration.file.splitter.FileSplitter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,8 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 4.2
*
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileSplitterParserTests {
@@ -63,7 +60,7 @@ public class FileSplitterParserTests {
assertThat(TestUtils.getPropertyValue(this.splitter, "markersJson", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.splitter, "requiresReply", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.splitter, "applySequence", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.splitter, "charset")).isEqualTo(Charset.forName("UTF-8"));
assertThat(TestUtils.getPropertyValue(this.splitter, "charset")).isEqualTo(StandardCharsets.UTF_8);
assertThat(TestUtils.getPropertyValue(this.splitter, "messagingTemplate.sendTimeout")).isEqualTo(5L);
assertThat(TestUtils.getPropertyValue(this.splitter, "firstLineHeaderName")).isEqualTo("foo");
assertThat(TestUtils.getPropertyValue(this.splitter, "discardChannelName")).isEqualTo("nullChannel");

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,18 +16,14 @@
package org.springframework.integration.file.config;
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;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.file.transformer.FileToStringTransformer;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,8 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Gary Russell
* @author Artem Bilan
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class FileToStringTransformerParserTests {
@@ -47,14 +42,7 @@ public class FileToStringTransformerParserTests {
@Test
public void checkDeleteFilesValue() {
DirectFieldAccessor endpointAccessor = new DirectFieldAccessor(endpoint);
MessageTransformingHandler handler = (MessageTransformingHandler)
endpointAccessor.getPropertyValue("handler");
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
FileToStringTransformer transformer = (FileToStringTransformer)
handlerAccessor.getPropertyValue("transformer");
DirectFieldAccessor transformerAccessor = new DirectFieldAccessor(transformer);
assertThat(transformerAccessor.getPropertyValue("deleteFiles")).isEqualTo(Boolean.TRUE);
assertThat(TestUtils.getPropertyValue(this.endpoint, "handler.transformer.deleteFiles", Boolean.class)).isTrue();
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.file.locking.NioFileLocker;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
*/
@SpringJUnitConfig
@DirtiesContext
public class InboundAdapterWithLockersTests {
@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.
@@ -38,6 +38,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -54,6 +55,7 @@ import static org.mockito.Mockito.verify;
*
*/
@SpringJUnitConfig
@DirtiesContext
public class DelegatingSessionFactoryTests {
@Autowired