|
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.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
20 | 21 | import static org.junit.Assert.assertNotSame; |
21 | 22 | import static org.junit.Assert.assertSame; |
22 | 23 | import static org.junit.Assert.assertTrue; |
| 24 | +import static org.junit.Assert.fail; |
23 | 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | +import static org.mockito.ArgumentMatchers.eq; |
24 | 27 | import static org.mockito.BDDMockito.willAnswer; |
| 28 | +import static org.mockito.BDDMockito.willReturn; |
| 29 | +import static org.mockito.BDDMockito.willThrow; |
25 | 30 | import static org.mockito.Mockito.doAnswer; |
26 | 31 | import static org.mockito.Mockito.doReturn; |
27 | 32 | import static org.mockito.Mockito.mock; |
|
32 | 37 |
|
33 | 38 | import java.io.File; |
34 | 39 | import java.io.FileOutputStream; |
| 40 | +import java.io.IOException; |
35 | 41 | import java.io.InputStream; |
| 42 | +import java.io.UncheckedIOException; |
36 | 43 | import java.lang.reflect.Constructor; |
37 | 44 | import java.util.ArrayList; |
38 | 45 | import java.util.Arrays; |
39 | 46 | import java.util.List; |
40 | 47 | import java.util.Vector; |
41 | 48 | import java.util.concurrent.atomic.AtomicInteger; |
42 | 49 |
|
43 | | -import org.junit.Test; |
| 50 | +import org.junit.jupiter.api.Test; |
44 | 51 | import org.mockito.Mockito; |
45 | 52 |
|
46 | 53 | import org.springframework.beans.DirectFieldAccessor; |
|
69 | 76 | import com.jcraft.jsch.JSch; |
70 | 77 | import com.jcraft.jsch.JSchException; |
71 | 78 | import com.jcraft.jsch.SftpATTRS; |
| 79 | +import com.jcraft.jsch.SftpException; |
72 | 80 |
|
73 | 81 | /** |
74 | 82 | * @author Oleg Zhurakousky |
|
78 | 86 | */ |
79 | 87 | public class SftpOutboundTests { |
80 | 88 |
|
81 | | - private static com.jcraft.jsch.Session jschSession = mock(com.jcraft.jsch.Session.class); |
| 89 | + private static final com.jcraft.jsch.Session jschSession = mock(com.jcraft.jsch.Session.class); |
82 | 90 |
|
83 | 91 | @Test |
84 | 92 | public void testHandleFileMessage() throws Exception { |
85 | 93 | File targetDir = new File("remote-target-dir"); |
86 | 94 | assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists()); |
87 | 95 |
|
88 | 96 | SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory(); |
89 | | - FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory); |
| 97 | + FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<>(sessionFactory); |
90 | 98 | handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName())); |
91 | 99 | DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator(); |
92 | 100 | fGenerator.setBeanFactory(mock(BeanFactory.class)); |
@@ -135,7 +143,7 @@ public void testHandleBytesMessage() throws Exception { |
135 | 143 | file.delete(); |
136 | 144 | } |
137 | 145 | SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory(); |
138 | | - FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory); |
| 146 | + FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<>(sessionFactory); |
139 | 147 | DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator(); |
140 | 148 | fGenerator.setBeanFactory(mock(BeanFactory.class)); |
141 | 149 | fGenerator.setExpression("'foo.txt'"); |
@@ -173,7 +181,7 @@ public void testSftpOutboundChannelAdapterInsideChain() throws Exception { |
173 | 181 | } |
174 | 182 |
|
175 | 183 | @Test //INT-2275 |
176 | | - public void testFtpOutboundGatewayInsideChain() throws Exception { |
| 184 | + public void testFtpOutboundGatewayInsideChain() { |
177 | 185 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
178 | 186 | "SftpOutboundInsideChainTests-context.xml", getClass()); |
179 | 187 |
|
@@ -204,12 +212,12 @@ public void testMkDir() throws Exception { |
204 | 212 | @SuppressWarnings("unchecked") |
205 | 213 | SessionFactory<LsEntry> sessionFactory = mock(SessionFactory.class); |
206 | 214 | when(sessionFactory.getSession()).thenReturn(session); |
207 | | - FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory); |
| 215 | + FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<>(sessionFactory); |
208 | 216 | handler.setAutoCreateDirectory(true); |
209 | 217 | handler.setRemoteDirectoryExpression(new LiteralExpression("/foo/bar/baz")); |
210 | 218 | handler.setBeanFactory(mock(BeanFactory.class)); |
211 | 219 | handler.afterPropertiesSet(); |
212 | | - final List<String> madeDirs = new ArrayList<String>(); |
| 220 | + final List<String> madeDirs = new ArrayList<>(); |
213 | 221 | doAnswer(invocation -> { |
214 | 222 | madeDirs.add(invocation.getArgument(0)); |
215 | 223 | return null; |
@@ -374,6 +382,38 @@ public void testSharedSessionCachedReset() throws Exception { |
374 | 382 | verify(jschSession2).disconnect(); |
375 | 383 | } |
376 | 384 |
|
| 385 | + @Test |
| 386 | + public void testExists() throws SftpException, IOException { |
| 387 | + ChannelSftp channelSftp = mock(ChannelSftp.class); |
| 388 | + |
| 389 | + willReturn(mock(SftpATTRS.class)) |
| 390 | + .given(channelSftp) |
| 391 | + .lstat(eq("exist")); |
| 392 | + |
| 393 | + willThrow(new SftpException(ChannelSftp.SSH_FX_NO_SUCH_FILE, "Path does not exist.")) |
| 394 | + .given(channelSftp) |
| 395 | + .lstat(eq("notExist")); |
| 396 | + |
| 397 | + willThrow(new SftpException(ChannelSftp.SSH_FX_CONNECTION_LOST, "Connection lost.")) |
| 398 | + .given(channelSftp) |
| 399 | + .lstat(eq("foo")); |
| 400 | + |
| 401 | + SftpSession sftpSession = new SftpSession(mock(com.jcraft.jsch.Session.class)); |
| 402 | + DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(sftpSession); |
| 403 | + fieldAccessor.setPropertyValue("channel", channelSftp); |
| 404 | + |
| 405 | + assertTrue(sftpSession.exists("exist")); |
| 406 | + |
| 407 | + assertFalse(sftpSession.exists("notExist")); |
| 408 | + |
| 409 | + try { |
| 410 | + sftpSession.exists("foo"); |
| 411 | + fail("Expected exception"); |
| 412 | + } |
| 413 | + catch (UncheckedIOException e) { |
| 414 | + } |
| 415 | + } |
| 416 | + |
377 | 417 | private void noopConnect(ChannelSftp channel1) throws JSchException { |
378 | 418 | doAnswer(invocation -> null).when(channel1).connect(); |
379 | 419 | } |
|
0 commit comments