17
17
package org .springframework .integration .file .splitter ;
18
18
19
19
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 ;
21
21
22
22
import java .io .ByteArrayInputStream ;
23
23
import java .io .File ;
29
29
import java .io .Reader ;
30
30
import java .nio .charset .Charset ;
31
31
import java .nio .charset .StandardCharsets ;
32
+ import java .time .Duration ;
32
33
import java .util .Date ;
33
- import java .util .List ;
34
34
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 ;
39
38
import org .mockito .Mockito ;
40
- import org .reactivestreams .Subscriber ;
41
39
42
40
import org .springframework .beans .factory .annotation .Autowired ;
43
41
import org .springframework .context .annotation .Bean ;
53
51
import org .springframework .integration .file .splitter .FileSplitter .FileMarker ;
54
52
import org .springframework .integration .support .json .JsonObjectMapper ;
55
53
import org .springframework .integration .support .json .JsonObjectMapperProvider ;
56
- import org .springframework .integration .test .util .TestUtils ;
57
54
import org .springframework .messaging .Message ;
58
55
import org .springframework .messaging .MessageChannel ;
59
56
import org .springframework .messaging .MessageHandler ;
57
+ import org .springframework .messaging .MessagingException ;
60
58
import org .springframework .messaging .PollableChannel ;
61
59
import org .springframework .messaging .support .GenericMessage ;
62
60
import org .springframework .test .annotation .DirtiesContext ;
63
61
import org .springframework .test .context .ContextConfiguration ;
64
- import org .springframework .test .context .junit4 . SpringJUnit4ClassRunner ;
62
+ import org .springframework .test .context .junit . jupiter . SpringJUnitConfig ;
65
63
import org .springframework .test .context .support .AnnotationConfigContextLoader ;
66
64
import org .springframework .util .FileCopyUtils ;
67
65
75
73
* @since 4.1.2
76
74
*/
77
75
@ ContextConfiguration (loader = AnnotationConfigContextLoader .class )
78
- @ RunWith ( SpringJUnit4ClassRunner . class )
76
+ @ SpringJUnitConfig
79
77
@ DirtiesContext
80
78
public class FileSplitterTests {
81
79
@@ -95,20 +93,15 @@ public class FileSplitterTests {
95
93
@ Autowired
96
94
private PollableChannel output ;
97
95
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" );
101
99
FileCopyUtils .copy (SAMPLE_CONTENT .getBytes (StandardCharsets .UTF_8 ),
102
100
new FileOutputStream (file , false ));
103
101
}
104
102
105
- @ AfterClass
106
- public static void tearDown () {
107
- file .delete ();
108
- }
109
-
110
103
@ Test
111
- public void testFileSplitter () throws Exception {
104
+ void testFileSplitter () throws Exception {
112
105
this .input1 .send (new GenericMessage <>(file ));
113
106
Message <?> receive = this .output .receive (10000 );
114
107
assertThat (receive ).isNotNull (); //HelloWorld
@@ -156,14 +149,11 @@ public void testFileSplitter() throws Exception {
156
149
assertThat (receive ).isNotNull (); //äöüß
157
150
assertThat (this .output .receive (1 )).isNull ();
158
151
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
+
167
157
this .input2 .send (new GenericMessage <>(new Date ()));
168
158
receive = this .output .receive (10000 );
169
159
assertThat (receive ).isNotNull ();
@@ -190,7 +180,7 @@ public void testFileSplitter() throws Exception {
190
180
}
191
181
192
182
@ Test
193
- public void testMarkers () {
183
+ void testMarkers () {
194
184
QueueChannel outputChannel = new QueueChannel ();
195
185
FileSplitter splitter = new FileSplitter (true , true );
196
186
splitter .setOutputChannel (outputChannel );
@@ -216,7 +206,7 @@ public void testMarkers() {
216
206
}
217
207
218
208
@ Test
219
- public void testMarkersEmptyFile () throws IOException {
209
+ void testMarkersEmptyFile () throws IOException {
220
210
QueueChannel outputChannel = new QueueChannel ();
221
211
FileSplitter splitter = new FileSplitter (true , true );
222
212
splitter .setOutputChannel (outputChannel );
@@ -244,7 +234,7 @@ public void testMarkersEmptyFile() throws IOException {
244
234
}
245
235
246
236
@ Test
247
- public void testMarkersJson () throws Exception {
237
+ void testMarkersJson () throws Exception {
248
238
JsonObjectMapper <?, ?> objectMapper = JsonObjectMapperProvider .newInstance ();
249
239
QueueChannel outputChannel = new QueueChannel ();
250
240
FileSplitter splitter = new FileSplitter (true , true , true );
@@ -273,7 +263,7 @@ public void testMarkersJson() throws Exception {
273
263
}
274
264
275
265
@ Test
276
- public void testFileSplitterReactive () {
266
+ void testFileSplitterReactive () {
277
267
FluxMessageChannel outputChannel = new FluxMessageChannel ();
278
268
FileSplitter splitter = new FileSplitter (true , true );
279
269
splitter .setApplySequence (true );
@@ -300,14 +290,13 @@ public void testFileSplitterReactive() {
300
290
assertThat (fileMarker .getFilePath ()).isEqualTo (file .getAbsolutePath ());
301
291
assertThat (fileMarker .getLineCount ()).isEqualTo (2 );
302
292
})
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 ));
307
296
}
308
297
309
298
@ Test
310
- public void testFirstLineAsHeader () {
299
+ void testFirstLineAsHeader () {
311
300
QueueChannel outputChannel = new QueueChannel ();
312
301
FileSplitter splitter = new FileSplitter (true , true );
313
302
splitter .setFirstLineAsHeader ("firstLine" );
@@ -337,7 +326,7 @@ public void testFirstLineAsHeader() {
337
326
}
338
327
339
328
@ Test
340
- public void testFirstLineAsHeaderOnlyHeader () throws IOException {
329
+ void testFirstLineAsHeaderOnlyHeader () throws IOException {
341
330
QueueChannel outputChannel = new QueueChannel ();
342
331
FileSplitter splitter = new FileSplitter (true , true );
343
332
splitter .setFirstLineAsHeader ("firstLine" );
@@ -365,7 +354,7 @@ public void testFirstLineAsHeaderOnlyHeader() throws IOException {
365
354
}
366
355
367
356
@ Test
368
- public void testFileReaderClosedOnException () throws Exception {
357
+ void testFileReaderClosedOnException () throws Exception {
369
358
DirectChannel outputChannel = new DirectChannel ();
370
359
outputChannel .subscribe (message -> {
371
360
throw new RuntimeException ();
0 commit comments