@Override public boolean get(final String remotePath, final InputStreamCallback callback) { Assert.notNull(remotePath, "'remotePath' cannot be null"); return execute(session -> { try (InputStream inputStream = session.readRaw(remotePath)) { callback.doWithInputStream(inputStream); return session.finalizeRaw(); } }); }
@Test public void testInt3100RawGET() throws Exception { Session<?> session = this.ftpSessionFactory.getSession(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ ftpSource1.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source1", new String(baos.toByteArray())); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("ftpSource/ftpSource2.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source2", new String(baos.toByteArray())); session.close(); }
/** * Only runs with a real server (see class javadocs). */ @Test public void testInt3100RawGET() throws Exception { Session<?> session = this.sessionFactory.getSession(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("sftpSource/ sftpSource1.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source1", new String(baos.toByteArray())); baos = new ByteArrayOutputStream(); FileCopyUtils.copy(session.readRaw("sftpSource/sftpSource2.txt"), baos); assertTrue(session.finalizeRaw()); assertEquals("source2", new String(baos.toByteArray())); session.close(); }
@Test public void testDirtySession() throws Exception { @SuppressWarnings("unchecked") SessionFactory<Object> factory = mock(SessionFactory.class); @SuppressWarnings("unchecked") Session<Object> session = mock(Session.class); when(factory.getSession()).thenReturn(session); when(session.readRaw("foo")).thenReturn(new ByteArrayInputStream("".getBytes())); when(session.finalizeRaw()).thenReturn(true); CachingSessionFactory<Object> ccf = new CachingSessionFactory<Object>(factory); RemoteFileTemplate<Object> template = new RemoteFileTemplate<Object>(ccf); template.setFileNameExpression(new LiteralExpression("foo")); template.setBeanFactory(mock(BeanFactory.class)); template.afterPropertiesSet(); try { template.get(new GenericMessage<String>("foo"), (InputStreamCallback) stream -> { throw new RuntimeException("bar"); }); fail("Expected exception"); } catch (Exception e) { assertThat(e.getCause(), instanceOf(RuntimeException.class)); assertThat(e.getCause().getMessage(), equalTo("bar")); } verify(session).close(); }
@SuppressWarnings("unchecked") @Override public Session<String> getSession() { if (this.session != null) { return this.session; } try { Session<String> session = mock(Session.class); willReturn(new String[] { "/foo/foo", "/foo/bar" }).given(session).list("/foo"); ByteArrayInputStream foo = new ByteArrayInputStream("foo\nbar".getBytes()); ByteArrayInputStream bar = new ByteArrayInputStream("baz\nqux".getBytes()); willReturn(foo).given(session).readRaw("/foo/foo"); willReturn(bar).given(session).readRaw("/foo/bar"); willReturn(new String[] { "/bar/foo", "/bar/bar" }).given(session).list("/bar"); ByteArrayInputStream foo2 = new ByteArrayInputStream("foo\r\nbar".getBytes()); ByteArrayInputStream bar2 = new ByteArrayInputStream("baz\r\nqux".getBytes()); willReturn(foo2).given(session).readRaw("/bar/foo"); willReturn(bar2).given(session).readRaw("/bar/bar"); willReturn(new String[] { "/bad/file" }).given(session).list("/bad"); willThrow(new IOException("No file")).given(session).readRaw("/bad/file"); given(session.finalizeRaw()).willReturn(true); this.session = session; return session; } catch (Exception e) { throw new RuntimeException("failed to mock session", e); } }