Search in sources :

Example 1 with InputStreamCallback

use of org.springframework.integration.file.remote.InputStreamCallback in project spring-integration by spring-projects.

the class CachingSessionFactoryTests method testDirtySession.

@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();
}
Also used : OutputStream(java.io.OutputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TestUtils(org.springframework.integration.test.util.TestUtils) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamCallback(org.springframework.integration.file.remote.InputStreamCallback) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BeanFactory(org.springframework.beans.factory.BeanFactory) Assert.fail(org.junit.Assert.fail) GenericMessage(org.springframework.messaging.support.GenericMessage) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) InputStream(java.io.InputStream) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) LiteralExpression(org.springframework.expression.common.LiteralExpression) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 2 with InputStreamCallback

use of org.springframework.integration.file.remote.InputStreamCallback in project spring-integration by spring-projects.

the class FtpServerOutboundTests method testRawGETWithTemplate.

@Test
public void testRawGETWithTemplate() throws Exception {
    RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<FTPFile>(this.ftpSessionFactory);
    template.setFileNameExpression(new SpelExpressionParser().parseExpression("payload"));
    template.setBeanFactory(mock(BeanFactory.class));
    template.afterPropertiesSet();
    final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    assertTrue(template.get(new GenericMessage<String>("ftpSource/ ftpSource1.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos1)));
    assertEquals("source1", new String(baos1.toByteArray()));
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    assertTrue(template.get(new GenericMessage<String>("ftpSource/ftpSource2.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos2)));
    assertEquals("source2", new String(baos2.toByteArray()));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) InputStreamCallback(org.springframework.integration.file.remote.InputStreamCallback) FTPFile(org.apache.commons.net.ftp.FTPFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 InputStreamCallback (org.springframework.integration.file.remote.InputStreamCallback)2 RemoteFileTemplate (org.springframework.integration.file.remote.RemoteFileTemplate)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FTPFile (org.apache.commons.net.ftp.FTPFile)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 Mockito.mock (org.mockito.Mockito.mock)1