public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); template.execute((SessionCallback<LsEntry, Void>) session -> { try { session.mkdir("si.sftp.sample"); } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to create")); } for (int i = 0; i < fileNames.length; i++) { stream.reset(); session.write(stream, "si.sftp.sample/" + fileNames[i]); } return null; }); } }
verify(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); if (!fail) { verify(this.session).write(any(InputStream.class), eq("foo/006.txt.writing")); verify(this.session).write(any(InputStream.class), eq("foo/009.txt.writing")); verify(this.session).rename("foo/002.txt.writing", "foo/002.txt"); verify(this.session).rename("foo/006.txt.writing", "foo/006.txt");
@Test public void testFailure() throws Exception { willThrow(new RuntimeException("fail test exception")) .given(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); String message = runTest(true); assertThat(message).contains("File split and transfer failed"); assertThat(message).contains("fail test exception"); assertThat(message).contains(TestUtils.applySystemFileSeparator("/tmp/out/002.txt")); }
private <F> Session<F> newSession() throws IOException { @SuppressWarnings("unchecked") Session<F> session = mock(Session.class); doThrow(new IOException("test")).when(session).write(any(InputStream.class), anyString()); when(session.isOpen()).thenReturn(false); return session; }
@Test public void testIgnoreExists() throws Exception { when(session.exists(Mockito.anyString())).thenReturn(true); this.template.send(new GenericMessage<File>(this.file), FileExistsMode.IGNORE); verify(this.session, never()).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@Test public void testFailNotExists() throws Exception { when(session.exists(Mockito.anyString())).thenReturn(false); this.template.send(new GenericMessage<File>(this.file), FileExistsMode.FAIL); verify(this.session).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@Test public void testIgnoreNotExists() throws Exception { when(session.exists(Mockito.anyString())).thenReturn(false); this.template.send(new GenericMessage<File>(this.file), FileExistsMode.IGNORE); verify(this.session).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@Test public void testReplace() throws Exception { this.template.send(new GenericMessage<File>(this.file), FileExistsMode.REPLACE); verify(this.session).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@Test public void testFailExists() throws Exception { when(session.exists(Mockito.anyString())).thenReturn(true); try { this.template.send(new GenericMessage<File>(this.file), FileExistsMode.FAIL); fail("Expected exception"); } catch (MessagingException e) { assertThat(e.getMessage(), Matchers.containsString("The destination file already exists")); } verify(this.session, never()).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@SuppressWarnings("unchecked") @Test public <F> void testRemoteDirWithEmptyString() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { String path = invocation.getArgument(1); assertFalse(path.startsWith("/")); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("''")); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage<String>("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@SuppressWarnings("unchecked") @Test public <F> void testTemporaryRemoteDir() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); final AtomicReference<String> temporaryPath = new AtomicReference<String>(); final AtomicReference<String> finalPath = new AtomicReference<String>(); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { temporaryPath.set(invocation.getArgument(0)); finalPath.set(invocation.getArgument(1)); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(new LiteralExpression("foo")); handler.setTemporaryRemoteDirectoryExpression(new LiteralExpression("bar")); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage<String>("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); assertEquals("bar", temporaryPath.get().substring(0, 3)); assertEquals("foo", finalPath.get().substring(0, 3)); }
@SuppressWarnings("unchecked") @Test public <F> void testUseTemporaryFileNameFalse() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); handler.setUseTemporaryFileName(false); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("hello").setHeader("path", null).build(); handler.handleMessage(message); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session, times(0)).rename(Mockito.anyString(), Mockito.anyString()); }
@SuppressWarnings("unchecked") @Test public <F> void testRemoteDirWithNull() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); Session<F> session = mock(Session.class); when(sf.getSession()).thenReturn(session); doAnswer(invocation -> { String path = invocation.getArgument(1); assertFalse(path.startsWith("/")); return null; }).when(session).rename(Mockito.anyString(), Mockito.anyString()); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("hello").setHeader("path", null).build(); handler.handleMessage(message); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); }
@BeforeClass public static void setup() { FtpRemoteFileTemplate rft = new FtpRemoteFileTemplate(sessionFactory()); rft.execute(s -> { s.mkdir("foo"); s.mkdir("bar"); s.mkdir("baz"); s.mkdir("qux"); s.mkdir("fiz"); s.mkdir("buz"); ByteArrayInputStream bais = new ByteArrayInputStream("foo".getBytes()); s.write(bais, "foo/f1"); s.write(bais, "baz/f2"); s.write(bais, "fiz/f3"); return null; }); }
@SuppressWarnings("unchecked") @Test public <F> void testServerException() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); CachingSessionFactory<F> csf = new CachingSessionFactory<F>(sf, 2); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(csf); handler.setBeanFactory(mock(BeanFactory.class)); Session<F> session1 = newSession(); Session<F> session2 = newSession(); Session<F> session3 = newSession(); when(sf.getSession()).thenReturn(session1, session2, session3); handler.setRemoteDirectoryExpressionString("'foo'"); handler.afterPropertiesSet(); for (int i = 0; i < 3; i++) { try { handler.handleMessage(new GenericMessage<String>("hello")); } catch (Exception e) { assertEquals("test", e.getCause().getCause().getMessage()); } } verify(session1, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session2, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session3, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); SimplePool<?> pool = TestUtils.getPropertyValue(csf, "pool", SimplePool.class); assertEquals(1, pool.getAllocatedCount()); assertEquals(1, pool.getIdleCount()); assertSame(session3, TestUtils.getPropertyValue(pool, "allocated", Set.class).iterator().next()); }
protected void doTest(SshServer server, Session<LsEntry> session) throws IOException { assertEquals(1, server.getActiveSessions().size()); LsEntry[] list = session.list("."); if (list.length > 0) { session.remove("*"); } session.write(new ByteArrayInputStream("foo".getBytes()), "bar"); list = session.list("."); assertEquals("bar", list[1].getFilename()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); session.read("bar", outputStream); assertEquals("foo", new String(outputStream.toByteArray())); session.remove("bar"); session.close(); }
@Test public void testMputPartial() throws Exception { Session<FTPFile> session = spyOnSession(); doAnswer(invocation -> { throw new IOException("Failed to send localSource2"); }).when(session).write(Mockito.any(InputStream.class), Mockito.contains("localSource2")); try { this.inboundMPut.send(new GenericMessage<File>(getSourceLocalDirectory())); fail("expected exception"); } catch (PartialSuccessException e) { assertEquals(3, e.getDerivedInput().size()); assertEquals(1, e.getPartialResults().size()); assertEquals("ftpTarget/localSource1.txt", e.getPartialResults().iterator().next()); assertThat(e.getCause().getMessage(), containsString("Failed to send localSource2")); } }
written.set(invocation.getArgument(1)); return null; }).when(session).write(any(InputStream.class), anyString()); tempFolder.newFile("baz.txt"); tempFolder.newFile("qux.txt");
@Test public void testMputRecursivePartial() throws Exception { Session<FTPFile> session = spyOnSession(); File sourceLocalSubDirectory = new File(getSourceLocalDirectory(), "subLocalSource"); assertTrue(sourceLocalSubDirectory.isDirectory()); File extra = new File(sourceLocalSubDirectory, "subLocalSource2.txt"); FileOutputStream writer = new FileOutputStream(extra); writer.write("foo".getBytes()); writer.close(); doAnswer(invocation -> { throw new IOException("Failed to send subLocalSource2"); }).when(session).write(Mockito.any(InputStream.class), Mockito.contains("subLocalSource2")); try { this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory())); fail("expected exception"); } catch (PartialSuccessException e) { assertEquals(3, e.getDerivedInput().size()); assertEquals(2, e.getPartialResults().size()); assertThat(e.getCause(), Matchers.instanceOf(PartialSuccessException.class)); PartialSuccessException cause = (PartialSuccessException) e.getCause(); assertEquals(2, cause.getDerivedInput().size()); assertEquals(1, cause.getPartialResults().size()); assertThat(cause.getCause().getMessage(), containsString("Failed to send subLocalSource2")); } extra.delete(); }
assertEquals("foo/bar.txt", path); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(session).write(any(InputStream.class), captor.capture()); assertEquals("foo/bar.txt.writing", captor.getValue()); verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");