Skip to content

Commit 5ceb151

Browse files
committed
* Fix FileSplitterTests
1 parent e066b8c commit 5ceb151

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

spring-integration-file/src/test/java/org/springframework/integration/file/splitter/FileSplitterTests.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.integration.file.splitter;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

2222
import java.io.ByteArrayInputStream;
2323
import java.io.File;
@@ -29,15 +29,13 @@
2929
import java.io.Reader;
3030
import java.nio.charset.Charset;
3131
import java.nio.charset.StandardCharsets;
32+
import java.time.Duration;
3233
import java.util.Date;
33-
import java.util.List;
3434

35-
import org.junit.AfterClass;
36-
import org.junit.BeforeClass;
37-
import org.junit.Test;
38-
import org.junit.runner.RunWith;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.io.TempDir;
3938
import org.mockito.Mockito;
40-
import org.reactivestreams.Subscriber;
4139

4240
import org.springframework.beans.factory.annotation.Autowired;
4341
import org.springframework.context.annotation.Bean;
@@ -53,15 +51,15 @@
5351
import org.springframework.integration.file.splitter.FileSplitter.FileMarker;
5452
import org.springframework.integration.support.json.JsonObjectMapper;
5553
import org.springframework.integration.support.json.JsonObjectMapperProvider;
56-
import org.springframework.integration.test.util.TestUtils;
5754
import org.springframework.messaging.Message;
5855
import org.springframework.messaging.MessageChannel;
5956
import org.springframework.messaging.MessageHandler;
57+
import org.springframework.messaging.MessagingException;
6058
import org.springframework.messaging.PollableChannel;
6159
import org.springframework.messaging.support.GenericMessage;
6260
import org.springframework.test.annotation.DirtiesContext;
6361
import org.springframework.test.context.ContextConfiguration;
64-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
62+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
6563
import org.springframework.test.context.support.AnnotationConfigContextLoader;
6664
import org.springframework.util.FileCopyUtils;
6765

@@ -75,7 +73,7 @@
7573
* @since 4.1.2
7674
*/
7775
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
78-
@RunWith(SpringJUnit4ClassRunner.class)
76+
@SpringJUnitConfig
7977
@DirtiesContext
8078
public class FileSplitterTests {
8179

@@ -95,20 +93,15 @@ public class FileSplitterTests {
9593
@Autowired
9694
private PollableChannel output;
9795

98-
@BeforeClass
99-
public static void setup() throws IOException {
100-
file = File.createTempFile("foo", ".txt");
96+
@BeforeAll
97+
static void setup(@TempDir File tempDir) throws IOException {
98+
file = new File(tempDir, "foo.txt");
10199
FileCopyUtils.copy(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8),
102100
new FileOutputStream(file, false));
103101
}
104102

105-
@AfterClass
106-
public static void tearDown() {
107-
file.delete();
108-
}
109-
110103
@Test
111-
public void testFileSplitter() throws Exception {
104+
void testFileSplitter() throws Exception {
112105
this.input1.send(new GenericMessage<>(file));
113106
Message<?> receive = this.output.receive(10000);
114107
assertThat(receive).isNotNull(); //HelloWorld
@@ -156,14 +149,11 @@ public void testFileSplitter() throws Exception {
156149
assertThat(receive).isNotNull(); //äöüß
157150
assertThat(this.output.receive(1)).isNull();
158151

159-
try {
160-
this.input2.send(new GenericMessage<>("bar"));
161-
fail("FileNotFoundException expected");
162-
}
163-
catch (Exception e) {
164-
assertThat(e.getCause()).isInstanceOf(FileNotFoundException.class);
165-
assertThat(e.getMessage()).contains("Failed to read file [bar]");
166-
}
152+
assertThatExceptionOfType(MessagingException.class)
153+
.isThrownBy(() -> this.input2.send(new GenericMessage<>("bar")))
154+
.withCauseInstanceOf(FileNotFoundException.class)
155+
.withMessageContaining("Failed to read file [bar]");
156+
167157
this.input2.send(new GenericMessage<>(new Date()));
168158
receive = this.output.receive(10000);
169159
assertThat(receive).isNotNull();
@@ -190,7 +180,7 @@ public void testFileSplitter() throws Exception {
190180
}
191181

192182
@Test
193-
public void testMarkers() {
183+
void testMarkers() {
194184
QueueChannel outputChannel = new QueueChannel();
195185
FileSplitter splitter = new FileSplitter(true, true);
196186
splitter.setOutputChannel(outputChannel);
@@ -216,7 +206,7 @@ public void testMarkers() {
216206
}
217207

218208
@Test
219-
public void testMarkersEmptyFile() throws IOException {
209+
void testMarkersEmptyFile() throws IOException {
220210
QueueChannel outputChannel = new QueueChannel();
221211
FileSplitter splitter = new FileSplitter(true, true);
222212
splitter.setOutputChannel(outputChannel);
@@ -244,7 +234,7 @@ public void testMarkersEmptyFile() throws IOException {
244234
}
245235

246236
@Test
247-
public void testMarkersJson() throws Exception {
237+
void testMarkersJson() throws Exception {
248238
JsonObjectMapper<?, ?> objectMapper = JsonObjectMapperProvider.newInstance();
249239
QueueChannel outputChannel = new QueueChannel();
250240
FileSplitter splitter = new FileSplitter(true, true, true);
@@ -273,7 +263,7 @@ public void testMarkersJson() throws Exception {
273263
}
274264

275265
@Test
276-
public void testFileSplitterReactive() {
266+
void testFileSplitterReactive() {
277267
FluxMessageChannel outputChannel = new FluxMessageChannel();
278268
FileSplitter splitter = new FileSplitter(true, true);
279269
splitter.setApplySequence(true);
@@ -300,14 +290,13 @@ public void testFileSplitterReactive() {
300290
assertThat(fileMarker.getFilePath()).isEqualTo(file.getAbsolutePath());
301291
assertThat(fileMarker.getLineCount()).isEqualTo(2);
302292
})
303-
.then(() ->
304-
((Subscriber<?>) TestUtils.getPropertyValue(outputChannel, "subscribers", List.class).get(0))
305-
.onComplete())
306-
.verifyComplete();
293+
.expectNoEvent(Duration.ofMillis(100))
294+
.thenCancel()
295+
.verify(Duration.ofSeconds(1));
307296
}
308297

309298
@Test
310-
public void testFirstLineAsHeader() {
299+
void testFirstLineAsHeader() {
311300
QueueChannel outputChannel = new QueueChannel();
312301
FileSplitter splitter = new FileSplitter(true, true);
313302
splitter.setFirstLineAsHeader("firstLine");
@@ -337,7 +326,7 @@ public void testFirstLineAsHeader() {
337326
}
338327

339328
@Test
340-
public void testFirstLineAsHeaderOnlyHeader() throws IOException {
329+
void testFirstLineAsHeaderOnlyHeader() throws IOException {
341330
QueueChannel outputChannel = new QueueChannel();
342331
FileSplitter splitter = new FileSplitter(true, true);
343332
splitter.setFirstLineAsHeader("firstLine");
@@ -365,7 +354,7 @@ public void testFirstLineAsHeaderOnlyHeader() throws IOException {
365354
}
366355

367356
@Test
368-
public void testFileReaderClosedOnException() throws Exception {
357+
void testFileReaderClosedOnException() throws Exception {
369358
DirectChannel outputChannel = new DirectChannel();
370359
outputChannel.subscribe(message -> {
371360
throw new RuntimeException();

0 commit comments

Comments
 (0)