11/*
2- * Copyright 2002-2019 the original author or authors.
2+ * Copyright 2002-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .integration .sftp .outbound ;
1818
1919import 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 ;
2023import static org .mockito .ArgumentMatchers .anyString ;
24+ import static org .mockito .ArgumentMatchers .eq ;
2125import static org .mockito .BDDMockito .willAnswer ;
26+ import static org .mockito .BDDMockito .willReturn ;
27+ import static org .mockito .BDDMockito .willThrow ;
2228import static org .mockito .Mockito .doAnswer ;
2329import static org .mockito .Mockito .doNothing ;
2430import static org .mockito .Mockito .doReturn ;
3036
3137import java .io .File ;
3238import java .io .FileOutputStream ;
39+ import java .io .IOException ;
3340import java .io .InputStream ;
3441import java .lang .reflect .Constructor ;
3542import java .util .ArrayList ;
3845import java .util .Vector ;
3946import java .util .concurrent .atomic .AtomicInteger ;
4047
41- import org .junit .Test ;
48+ import org .junit .jupiter . api . Test ;
4249import org .mockito .Mockito ;
4350
4451import org .springframework .beans .DirectFieldAccessor ;
4552import org .springframework .beans .factory .BeanFactory ;
4653import org .springframework .context .support .ClassPathXmlApplicationContext ;
54+ import org .springframework .core .NestedIOException ;
4755import org .springframework .expression .common .LiteralExpression ;
4856import org .springframework .integration .file .DefaultFileNameGenerator ;
4957import org .springframework .integration .file .remote .FileInfo ;
6775import com .jcraft .jsch .JSch ;
6876import com .jcraft .jsch .JSchException ;
6977import com .jcraft .jsch .SftpATTRS ;
78+ import com .jcraft .jsch .SftpException ;
7079
7180/**
7281 * @author Oleg Zhurakousky
7685 */
7786public class SftpOutboundTests {
7887
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 );
8089
8190 @ Test
8291 public void testHandleFileMessage () throws Exception {
8392 File targetDir = new File ("remote-target-dir" );
8493 assertThat (targetDir .exists ()).as ("target directory does not exist: " + targetDir .getName ()).isTrue ();
8594
8695 SessionFactory <LsEntry > sessionFactory = new TestSftpSessionFactory ();
87- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
96+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
8897 handler .setRemoteDirectoryExpression (new LiteralExpression (targetDir .getName ()));
8998 DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator ();
9099 fGenerator .setBeanFactory (mock (BeanFactory .class ));
@@ -133,7 +142,7 @@ public void testHandleBytesMessage() throws Exception {
133142 file .delete ();
134143 }
135144 SessionFactory <LsEntry > sessionFactory = new TestSftpSessionFactory ();
136- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
145+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
137146 DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator ();
138147 fGenerator .setBeanFactory (mock (BeanFactory .class ));
139148 fGenerator .setExpression ("'foo.txt'" );
@@ -142,7 +151,7 @@ public void testHandleBytesMessage() throws Exception {
142151 handler .setBeanFactory (mock (BeanFactory .class ));
143152 handler .afterPropertiesSet ();
144153
145- handler .handleMessage (new GenericMessage <byte [] >("byte[] data" .getBytes ()));
154+ handler .handleMessage (new GenericMessage <>("byte[] data" .getBytes ()));
146155 assertThat (new File ("remote-target-dir" , "foo.txt" ).exists ()).isTrue ();
147156 byte [] inFile = FileCopyUtils .copyToByteArray (file );
148157 assertThat (new String (inFile )).isEqualTo ("byte[] data" );
@@ -171,7 +180,7 @@ public void testSftpOutboundChannelAdapterInsideChain() throws Exception {
171180 }
172181
173182 @ Test //INT-2275
174- public void testFtpOutboundGatewayInsideChain () throws Exception {
183+ public void testFtpOutboundGatewayInsideChain () {
175184 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (
176185 "SftpOutboundInsideChainTests-context.xml" , getClass ());
177186
@@ -202,17 +211,17 @@ public void testMkDir() throws Exception {
202211 @ SuppressWarnings ("unchecked" )
203212 SessionFactory <LsEntry > sessionFactory = mock (SessionFactory .class );
204213 when (sessionFactory .getSession ()).thenReturn (session );
205- FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <LsEntry >(sessionFactory );
214+ FileTransferringMessageHandler <LsEntry > handler = new FileTransferringMessageHandler <>(sessionFactory );
206215 handler .setAutoCreateDirectory (true );
207216 handler .setRemoteDirectoryExpression (new LiteralExpression ("/foo/bar/baz" ));
208217 handler .setBeanFactory (mock (BeanFactory .class ));
209218 handler .afterPropertiesSet ();
210- final List <String > madeDirs = new ArrayList <String >();
219+ final List <String > madeDirs = new ArrayList <>();
211220 doAnswer (invocation -> {
212221 madeDirs .add (invocation .getArgument (0 ));
213222 return null ;
214223 }).when (session ).mkdir (anyString ());
215- handler .handleMessage (new GenericMessage <String >("qux" ));
224+ handler .handleMessage (new GenericMessage <>("qux" ));
216225 assertThat (madeDirs .size ()).isEqualTo (3 );
217226 assertThat (madeDirs .get (0 )).isEqualTo ("/foo" );
218227 assertThat (madeDirs .get (1 )).isEqualTo ("/foo/bar" );
@@ -380,6 +389,34 @@ public void testSharedSessionCachedReset() throws Exception {
380389 verify (jschSession2 ).disconnect ();
381390 }
382391
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+
383420 private void noopConnect (ChannelSftp channel1 ) throws JSchException {
384421 doNothing ().when (channel1 ).connect (5000 );
385422 }
0 commit comments