forked from spring-projects/spring-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteFileOutboundGatewayTests.java
725 lines (673 loc) · 23.8 KB
/
RemoteFileOutboundGatewayTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
* Copyright 2002-2012 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.file.remote.gateway;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.filters.AbstractSimplePatternFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.remote.AbstractFileInfo;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.message.GenericMessage;
/**
* @author Gary Russell
* @since 2.1
*
*/
@SuppressWarnings("rawtypes")
public class RemoteFileOutboundGatewayTests {
private String tmpDir = System.getProperty("java.io.tmpdir");
@Test(expected=IllegalArgumentException.class)
public void testBad() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "bad", "payload");
gw.afterPropertiesSet();
}
@Test
public void testBadFilterGet() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "get", "payload");
gw.setFilter(new TestPatternFilter(""));
try {
gw.onInit();
fail("Exception expected");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Filters are not supported"));
}
}
@Test
public void testBadFilterMGet() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "mget", "payload");
gw.setFilter(new TestPatternFilter(""));
try {
gw.onInit();
fail("Exception expected");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Filters are not supported"));
}
}
@Test
public void testBadFilterRm() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "rm", "payload");
gw.setFilter(new TestPatternFilter(""));
try {
gw.onInit();
fail("Exception expected");
}
catch (IllegalArgumentException e) {
assertTrue(e.getMessage().startsWith("Filters are not supported"));
}
}
@Test
public void testLs() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/x/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<TestLsEntry>> out = (Message<List<TestLsEntry>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote/x"));
assertEquals(2, out.getPayload().size());
assertSame(files[1], out.getPayload().get(0)); // sort by default
assertSame(files[0], out.getPayload().get(1));
assertEquals("testremote/x/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
@Test
public void testMGetWild() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "mget", "payload");
gw.setLocalDirectory(new File(this.tmpDir ));
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
new File(this.tmpDir + "/f2").delete();
when(sessionFactory.getSession()).thenReturn(new Session() {
public boolean remove(String path) throws IOException {
return false;
}
public Object[] list(String path) throws IOException {
return null;
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testData".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return false;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
}
public boolean isOpen() {
return false;
}
public boolean exists(String path) throws IOException {
return false;
}
public String[] listNames(String path) throws IOException {
return new String[] {"f1", "f2"};
}
});
@SuppressWarnings("unchecked")
Message<List<File>> out = (Message<List<File>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote/*"));
assertEquals(2, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0).getName());
assertEquals("f2", out.getPayload().get(1).getName());
assertEquals("testremote/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
@Test
public void testMGetSingle() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "mget", "payload");
gw.setLocalDirectory(new File(this.tmpDir ));
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
when(sessionFactory.getSession()).thenReturn(new Session() {
public boolean remove(String path) throws IOException {
return false;
}
public Object[] list(String path) throws IOException {
return null;
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testData".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return false;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
}
public boolean isOpen() {
return false;
}
public boolean exists(String path) throws IOException {
return false;
}
public String[] listNames(String path) throws IOException {
return new String[] {"f1"};
}
});
@SuppressWarnings("unchecked")
Message<List<File>> out = (Message<List<File>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote/f1"));
assertEquals(1, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0).getName());
assertEquals("testremote/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
@Test(expected=MessagingException.class)
public void testMGetEmpty() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "mget", "payload");
gw.setLocalDirectory(new File(this.tmpDir ));
gw.setOptions(" -x ");
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
new File(this.tmpDir + "/f2").delete();
when(sessionFactory.getSession()).thenReturn(new Session() {
public boolean remove(String path) throws IOException {
return false;
}
public Object[] list(String path) throws IOException {
return null;
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testData".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return false;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
}
public boolean isOpen() {
return false;
}
public boolean exists(String path) throws IOException {
return false;
}
public String[] listNames(String path) throws IOException {
return new String[0];
}
});
gw.handleRequestMessage(new GenericMessage<String>("testremote/*"));
}
/**
* @return
*/
public TestLsEntry[] fileList() {
TestLsEntry[] files = new TestLsEntry[6];
files[0] = new TestLsEntry("f2", 123, false, false, 1234, "-r--r--r--");
files[1] = new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--");
files[2] = new TestLsEntry("f3", 12345, true, false, 123456, "drw-r--r--");
files[3] = new TestLsEntry("f4", 12346, false, true, 1234567, "lrw-r--r--");
files[4] = new TestLsEntry(".f5", 12347, false, false, 12345678, "-rw-r--r--");
files[5] = new TestLsEntry(".f6", 12347, true, false, 123456789, "drw-r--r--");
return files;
}
@Test
public void testLs_f() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-f");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/x/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<TestLsEntry>> out = (Message<List<TestLsEntry>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote/x"));
assertEquals(2, out.getPayload().size());
assertSame(files[0], out.getPayload().get(0));
assertSame(files[1], out.getPayload().get(1));
assertEquals("testremote/x/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
@Test
public void testLs_None() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = new TestLsEntry[0];
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<TestLsEntry>> out = (Message<List<TestLsEntry>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(0, out.getPayload().size());
}
@Test
public void testLs_1() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(2, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0));
assertEquals("f2", out.getPayload().get(1));
}
@Test
public void testLs_1_f() throws Exception { //no sort
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1 -f");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(2, out.getPayload().size());
assertEquals("f2", out.getPayload().get(0));
assertEquals("f1", out.getPayload().get(1));
}
@Test
public void testLs_1_dirs() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1 -dirs");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(3, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0));
assertEquals("f2", out.getPayload().get(1));
assertEquals("f3", out.getPayload().get(2));
}
@Test
public void testLs_1_dirs_links() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1 -dirs -links");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(4, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0));
assertEquals("f2", out.getPayload().get(1));
assertEquals("f3", out.getPayload().get(2));
assertEquals("f4", out.getPayload().get(3));
}
@Test
public void testLs_1_a_f_dirs_links() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1 -a -f -dirs -links");
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(6, out.getPayload().size());
assertEquals("f2", out.getPayload().get(0));
assertEquals("f1", out.getPayload().get(1));
assertEquals("f3", out.getPayload().get(2));
assertEquals("f4", out.getPayload().get(3));
assertEquals(".f5", out.getPayload().get(4));
assertEquals(".f6", out.getPayload().get(5));
}
@Test
public void testLs_1_a_f_dirs_links_filtered() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "ls", "payload");
gw.setOptions("-1 -a -f -dirs -links");
gw.setFilter(new TestPatternFilter("*4"));
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] files = fileList();
when(session.list("testremote/")).thenReturn(files);
@SuppressWarnings("unchecked")
Message<List<String>> out = (Message<List<String>>) gw
.handleRequestMessage(new GenericMessage<String>("testremote"));
assertEquals(1, out.getPayload().size());
assertEquals("f4", out.getPayload().get(0));
}
@Test
public void testGet() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "get", "payload");
gw.setLocalDirectory(new File(this.tmpDir ));
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
when(sessionFactory.getSession()).thenReturn(new Session(){
private boolean open = true;
public boolean remove(String path) throws IOException {
return false;
}
public TestLsEntry[] list(String path) throws IOException {
return new TestLsEntry[] {
new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--")
};
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testfile".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return true;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
open = false;
}
public boolean isOpen() {
return open;
}
public boolean exists(String path) throws IOException {
return true;
}
public String[] listNames(String path) throws IOException {
return null;
}
});
@SuppressWarnings("unchecked")
Message<File> out = (Message<File>) gw.handleRequestMessage(new GenericMessage<String>("f1"));
File outFile = new File(this.tmpDir + "/f1");
assertEquals(outFile, out.getPayload());
assertTrue(outFile.exists());
outFile.delete();
assertEquals("/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
assertEquals("f1",
out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
@Test
public void testGet_P() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "get", "payload");
gw.setLocalDirectory(new File(this.tmpDir));
gw.setOptions("-P");
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
final Date modified = new Date(cal.getTime().getTime() / 1000 * 1000);
when(sessionFactory.getSession()).thenReturn(new Session(){
private boolean open = true;
public boolean remove(String path) throws IOException {
return false;
}
public TestLsEntry[] list(String path) throws IOException {
return new TestLsEntry[] {
new TestLsEntry("f1", 1234, false, false, modified.getTime(), "-rw-r--r--")
};
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testfile".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return true;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
open = false;
}
public boolean isOpen() {
return open;
}
public boolean exists(String path) throws IOException {
return true;
}
public String[] listNames(String path) throws IOException {
return null;
}
});
@SuppressWarnings("unchecked")
Message<File> out = (Message<File>) gw.handleRequestMessage(new GenericMessage<String>("x/f1"));
File outFile = new File(this.tmpDir + "/f1");
assertEquals(outFile, out.getPayload());
assertTrue(outFile.exists());
assertEquals(modified.getTime(), outFile.lastModified());
outFile.delete();
assertEquals("x/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
assertEquals("f1",
out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
@Test
public void testGet_create_dir() throws Exception {
new File(this.tmpDir + "/x/f1").delete();
new File(this.tmpDir + "/x").delete();
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "get", "payload");
gw.setLocalDirectory(new File(this.tmpDir + "/x"));
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(new Session(){
private boolean open = true;
public boolean remove(String path) throws IOException {
return false;
}
public TestLsEntry[] list(String path) throws IOException {
return new TestLsEntry[] {
new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--")
};
}
public void read(String source, OutputStream outputStream)
throws IOException {
outputStream.write("testfile".getBytes());
}
public void write(InputStream inputStream, String destination)
throws IOException {
}
public boolean mkdir(String directory) throws IOException {
return true;
}
public void rename(String pathFrom, String pathTo)
throws IOException {
}
public void close() {
open = false;
}
public boolean isOpen() {
return open;
}
public boolean exists(String path) throws IOException {
return true;
}
public String[] listNames(String path) throws IOException {
return null;
}
});
gw.handleRequestMessage(new GenericMessage<String>("f1"));
File out = new File(this.tmpDir + "/x/f1");
assertTrue(out.exists());
out.delete();
}
@Test
public void testRm() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway
(sessionFactory, "rm", "payload");
when(sessionFactory.getSession()).thenReturn(session);
when(session.remove("testremote/x/f1")).thenReturn(Boolean.TRUE);
@SuppressWarnings("unchecked")
Message<Boolean> out = (Message<Boolean>) gw
.handleRequestMessage(new GenericMessage<String>("testremote/x/f1"));
assertEquals(Boolean.TRUE, out.getPayload());
verify(session).remove("testremote/x/f1");
assertEquals("testremote/x/",
out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
assertEquals("f1",
out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
}
class TestRemoteFileOutboundGateway extends AbstractRemoteFileOutboundGateway<TestLsEntry> {
@SuppressWarnings({ "rawtypes", "unchecked" })
public TestRemoteFileOutboundGateway(SessionFactory sessionFactory,
String command, String expression) {
super(sessionFactory, command, expression);
}
@Override
protected boolean isDirectory(TestLsEntry file) {
return file.isDirectory();
}
@Override
protected boolean isLink(TestLsEntry file) {
return file.isLink();
}
@Override
protected String getFilename(TestLsEntry file) {
return file.getFilename();
}
@Override
protected long getModified(TestLsEntry file) {
return file.getModified();
}
@Override
protected List<AbstractFileInfo<TestLsEntry>> asFileInfoList(
Collection<TestLsEntry> files) {
return new ArrayList<AbstractFileInfo<TestLsEntry>>(files);
}
}
class TestLsEntry extends AbstractFileInfo<TestLsEntry> {
private String filename;
private int size;
private boolean dir;
private boolean link;
private long modified;
private String permissions;
public TestLsEntry(String filename, int size, boolean dir, boolean link,
long modified, String permissions) {
this.filename = filename;
this.size = size;
this.dir = dir;
this.link = link;
this.modified = modified;
this.permissions = permissions;
}
public boolean isDirectory() {
return this.dir;
}
public long getModified() {
return this.modified;
}
public String getFilename() {
return this.filename;
}
public boolean isLink() {
return this.link;
}
public long getSize() {
return this.size;
}
public String getPermissions() {
return this.permissions;
}
public TestLsEntry getFileInfo() {
return this;
}
}
class TestPatternFilter extends AbstractSimplePatternFileListFilter<TestLsEntry>{
public TestPatternFilter(String path) {
super(path);
}
@Override
protected String getFilename(TestLsEntry file) {
return file.getFilename();
}
}