1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .integration .sftp .outbound ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
21
+ import static org .mockito .AdditionalMatchers .and ;
22
+ import static org .mockito .AdditionalMatchers .not ;
20
23
import static org .mockito .ArgumentMatchers .anyString ;
24
+ import static org .mockito .ArgumentMatchers .eq ;
21
25
import static org .mockito .BDDMockito .willAnswer ;
26
+ import static org .mockito .BDDMockito .willReturn ;
27
+ import static org .mockito .BDDMockito .willThrow ;
22
28
import static org .mockito .Mockito .doAnswer ;
23
29
import static org .mockito .Mockito .doNothing ;
24
30
import static org .mockito .Mockito .doReturn ;
30
36
31
37
import java .io .File ;
32
38
import java .io .FileOutputStream ;
39
+ import java .io .IOException ;
33
40
import java .io .InputStream ;
34
41
import java .lang .reflect .Constructor ;
35
42
import java .util .ArrayList ;
38
45
import java .util .Vector ;
39
46
import java .util .concurrent .atomic .AtomicInteger ;
40
47
41
- import org .junit .Test ;
48
+ import org .junit .jupiter . api . Test ;
42
49
import org .mockito .Mockito ;
43
50
44
51
import org .springframework .beans .DirectFieldAccessor ;
45
52
import org .springframework .beans .factory .BeanFactory ;
46
53
import org .springframework .context .support .ClassPathXmlApplicationContext ;
54
+ import org .springframework .core .NestedIOException ;
47
55
import org .springframework .expression .common .LiteralExpression ;
48
56
import org .springframework .integration .file .DefaultFileNameGenerator ;
49
57
import org .springframework .integration .file .remote .FileInfo ;
67
75
import com .jcraft .jsch .JSch ;
68
76
import com .jcraft .jsch .JSchException ;
69
77
import com .jcraft .jsch .SftpATTRS ;
78
+ import com .jcraft .jsch .SftpException ;
70
79
71
80
/**
72
81
* @author Oleg Zhurakousky
76
85
*/
77
86
public class SftpOutboundTests {
78
87
79
- private static com .jcraft .jsch .Session jschSession = mock (com .jcraft .jsch .Session .class );
88
+ private static final com .jcraft .jsch .Session jschSession = mock (com .jcraft .jsch .Session .class );
80
89
81
90
@ Test
82
91
public void testHandleFileMessage () throws Exception {
83
92
File targetDir = new File ("remote-target-dir" );
84
93
assertThat (targetDir .exists ()).as ("target directory does not exist: " + targetDir .getName ()).isTrue ();
85
94
86
95
SessionFactory <LsEntry > sessionFactory = new TestSftpSessionFactory ();
87
- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
96
+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
88
97
handler .setRemoteDirectoryExpression (new LiteralExpression (targetDir .getName ()));
89
98
DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator ();
90
99
fGenerator .setBeanFactory (mock (BeanFactory .class ));
@@ -133,7 +142,7 @@ public void testHandleBytesMessage() throws Exception {
133
142
file .delete ();
134
143
}
135
144
SessionFactory <LsEntry > sessionFactory = new TestSftpSessionFactory ();
136
- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
145
+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
137
146
DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator ();
138
147
fGenerator .setBeanFactory (mock (BeanFactory .class ));
139
148
fGenerator .setExpression ("'foo.txt'" );
@@ -142,7 +151,7 @@ public void testHandleBytesMessage() throws Exception {
142
151
handler .setBeanFactory (mock (BeanFactory .class ));
143
152
handler .afterPropertiesSet ();
144
153
145
- handler .handleMessage (new GenericMessage <byte [] >("byte[] data" .getBytes ()));
154
+ handler .handleMessage (new GenericMessage <>("byte[] data" .getBytes ()));
146
155
assertThat (new File ("remote-target-dir" , "foo.txt" ).exists ()).isTrue ();
147
156
byte [] inFile = FileCopyUtils .copyToByteArray (file );
148
157
assertThat (new String (inFile )).isEqualTo ("byte[] data" );
@@ -171,7 +180,7 @@ public void testSftpOutboundChannelAdapterInsideChain() throws Exception {
171
180
}
172
181
173
182
@ Test //INT-2275
174
- public void testFtpOutboundGatewayInsideChain () throws Exception {
183
+ public void testFtpOutboundGatewayInsideChain () {
175
184
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
176
185
"SftpOutboundInsideChainTests-context.xml" , getClass ());
177
186
@@ -202,17 +211,17 @@ public void testMkDir() throws Exception {
202
211
@ SuppressWarnings ("unchecked" )
203
212
SessionFactory <LsEntry > sessionFactory = mock (SessionFactory .class );
204
213
when (sessionFactory .getSession ()).thenReturn (session );
205
- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
214
+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
206
215
handler .setAutoCreateDirectory (true );
207
216
handler .setRemoteDirectoryExpression (new LiteralExpression ("/foo/bar/baz" ));
208
217
handler .setBeanFactory (mock (BeanFactory .class ));
209
218
handler .afterPropertiesSet ();
210
- final List <String > madeDirs = new ArrayList <String >();
219
+ final List <String > madeDirs = new ArrayList <>();
211
220
doAnswer (invocation -> {
212
221
madeDirs .add (invocation .getArgument (0 ));
213
222
return null ;
214
223
}).when (session ).mkdir (anyString ());
215
- handler .handleMessage (new GenericMessage <String >("qux" ));
224
+ handler .handleMessage (new GenericMessage <>("qux" ));
216
225
assertThat (madeDirs .size ()).isEqualTo (3 );
217
226
assertThat (madeDirs .get (0 )).isEqualTo ("/foo" );
218
227
assertThat (madeDirs .get (1 )).isEqualTo ("/foo/bar" );
@@ -380,6 +389,34 @@ public void testSharedSessionCachedReset() throws Exception {
380
389
verify (jschSession2 ).disconnect ();
381
390
}
382
391
392
+ @ Test
393
+ public void testExists () throws SftpException , IOException {
394
+ ChannelSftp channelSftp = mock (ChannelSftp .class );
395
+
396
+ willReturn (mock (SftpATTRS .class ))
397
+ .given (channelSftp )
398
+ .lstat (eq ("exist" ));
399
+
400
+ willThrow (new SftpException (ChannelSftp .SSH_FX_NO_SUCH_FILE , "Path does not exist." ))
401
+ .given (channelSftp )
402
+ .lstat (eq ("notExist" ));
403
+
404
+ willThrow (new SftpException (ChannelSftp .SSH_FX_CONNECTION_LOST , "Connection lost." ))
405
+ .given (channelSftp )
406
+ .lstat (and (not (eq ("exist" )), not (eq ("notExist" ))));
407
+
408
+ SftpSession sftpSession = new SftpSession (mock (com .jcraft .jsch .Session .class ));
409
+ DirectFieldAccessor fieldAccessor = new DirectFieldAccessor (sftpSession );
410
+ fieldAccessor .setPropertyValue ("channel" , channelSftp );
411
+
412
+ assertThat (sftpSession .exists ("exist" )).isTrue ();
413
+
414
+ assertThat (sftpSession .exists ("notExist" )).isFalse ();
415
+
416
+ assertThatExceptionOfType (NestedIOException .class ).
417
+ isThrownBy (() -> sftpSession .exists ("foo" ));
418
+ }
419
+
383
420
private void noopConnect (ChannelSftp channel1 ) throws JSchException {
384
421
doNothing ().when (channel1 ).connect (5000 );
385
422
}
0 commit comments