Search in sources :

Example 26 with SessionFactory

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

the class RemoteFileOutboundGatewayTests method testGetExists.

@SuppressWarnings("unchecked")
@Test
public void testGetExists() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "get", "payload");
    gw.setLocalDirectory(new File(this.tmpDir));
    gw.afterPropertiesSet();
    File outFile = new File(this.tmpDir + "/f1");
    FileOutputStream fos = new FileOutputStream(outFile);
    fos.write("foo".getBytes());
    fos.close();
    when(sessionFactory.getSession()).thenReturn(new TestSession() {

        @Override
        public TestLsEntry[] list(String path) throws IOException {
            return new TestLsEntry[] { new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--") };
        }

        @Override
        public void read(String source, OutputStream outputStream) throws IOException {
            outputStream.write("testfile".getBytes());
        }
    });
    // default (null)
    MessageBuilder<File> out;
    try {
        out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
        fail("Exception expected");
    } catch (MessageHandlingException e) {
        assertThat(e.getMessage(), containsString("already exists"));
    }
    gw.setFileExistsMode(FileExistsMode.FAIL);
    try {
        out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
        fail("Exception expected");
    } catch (MessageHandlingException e) {
        assertThat(e.getMessage(), containsString("already exists"));
    }
    gw.setFileExistsMode(FileExistsMode.IGNORE);
    out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
    assertEquals(outFile, out.getPayload());
    assertContents("foo", outFile);
    gw.setFileExistsMode(FileExistsMode.APPEND);
    out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
    assertEquals(outFile, out.getPayload());
    assertContents("footestfile", outFile);
    gw.setFileExistsMode(FileExistsMode.REPLACE);
    out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
    assertEquals(outFile, out.getPayload());
    assertContents("testfile", outFile);
    outFile.delete();
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

Example 27 with SessionFactory

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

the class FtpRemoteFileTemplateTests method testINT3412AppendStatRmdir.

@Test
public void testINT3412AppendStatRmdir() throws IOException {
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setExpression("'foobar.txt'");
    template.setFileNameGenerator(fileNameGenerator);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setUseTemporaryFileName(false);
    template.execute(session -> {
        session.mkdir("foo/");
        return session.mkdir("foo/bar/");
    });
    template.append(new GenericMessage<String>("foo"));
    template.append(new GenericMessage<String>("bar"));
    assertTrue(template.exists("foo/foobar.txt"));
    template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
        try {
            FTPFile[] files = client.listFiles("foo/foobar.txt");
            assertEquals(6, files[0].getSize());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
        assertTrue(session.remove("foo/foobar.txt"));
        assertTrue(session.rmdir("foo/bar/"));
        FTPFile[] files = session.list("foo/");
        assertEquals(0, files.length);
        assertTrue(session.rmdir("foo/"));
    });
    assertFalse(template.getSession().exists("foo"));
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RunWith(org.junit.runner.RunWith) LiteralExpression(org.springframework.expression.common.LiteralExpression) Autowired(org.springframework.beans.factory.annotation.Autowired) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) SessionCallbackWithoutResult(org.springframework.integration.file.remote.SessionCallbackWithoutResult) Assert.fail(org.junit.Assert.fail) FTPClient(org.apache.commons.net.ftp.FTPClient) ClientCallbackWithoutResult(org.springframework.integration.file.remote.ClientCallbackWithoutResult) Assert.assertTrue(org.junit.Assert.assertTrue) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) File(java.io.File) Configuration(org.springframework.context.annotation.Configuration) Assert.assertFalse(org.junit.Assert.assertFalse) ContextConfiguration(org.springframework.test.context.ContextConfiguration) FTPFile(org.apache.commons.net.ftp.FTPFile) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) LiteralExpression(org.springframework.expression.common.LiteralExpression) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 28 with SessionFactory

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

the class SftpRemoteFileTemplateTests method testINT3412AppendStatRmdir.

@Test
public void testINT3412AppendStatRmdir() {
    SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory);
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setExpression("'foobar.txt'");
    template.setFileNameGenerator(fileNameGenerator);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setUseTemporaryFileName(false);
    template.execute(session -> {
        session.mkdir("foo/");
        return session.mkdir("foo/bar/");
    });
    template.append(new GenericMessage<String>("foo"));
    template.append(new GenericMessage<String>("bar"));
    assertTrue(template.exists("foo/foobar.txt"));
    template.executeWithClient((ClientCallbackWithoutResult<ChannelSftp>) client -> {
        try {
            SftpATTRS file = client.lstat("foo/foobar.txt");
            assertEquals(6, file.getSize());
        } catch (SftpException e) {
            throw new RuntimeException(e);
        }
    });
    template.execute((SessionCallbackWithoutResult<LsEntry>) session -> {
        LsEntry[] files = session.list("foo/");
        assertEquals(4, files.length);
        assertTrue(session.remove("foo/foobar.txt"));
        assertTrue(session.rmdir("foo/bar/"));
        files = session.list("foo/");
        assertEquals(2, files.length);
        List<LsEntry> list = Arrays.asList(files);
        assertThat(list.stream().map(l -> l.getFilename()).collect(Collectors.toList()), containsInAnyOrder(".", ".."));
        assertTrue(session.rmdir("foo/"));
    });
    assertFalse(template.exists("foo"));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RunWith(org.junit.runner.RunWith) LiteralExpression(org.springframework.expression.common.LiteralExpression) Autowired(org.springframework.beans.factory.annotation.Autowired) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) Assert.assertThat(org.junit.Assert.assertThat) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) SessionCallbackWithoutResult(org.springframework.integration.file.remote.SessionCallbackWithoutResult) SftpException(com.jcraft.jsch.SftpException) ChannelSftp(com.jcraft.jsch.ChannelSftp) ClientCallbackWithoutResult(org.springframework.integration.file.remote.ClientCallbackWithoutResult) SftpATTRS(com.jcraft.jsch.SftpATTRS) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Collectors(java.util.stream.Collectors) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) ChannelSftp(com.jcraft.jsch.ChannelSftp) LiteralExpression(org.springframework.expression.common.LiteralExpression) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) List(java.util.List) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 29 with SessionFactory

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

the class SessionFactoryTests method testSessionWaitExpire.

// timeout expire
@Test(expected = MessagingException.class)
public void testSessionWaitExpire() throws Exception {
    SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
    Session session = Mockito.mock(Session.class);
    Mockito.when(sessionFactory.getSession()).thenReturn(session);
    CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2);
    cachingFactory.setSessionWaitTimeout(3000);
    cachingFactory.getSession();
    cachingFactory.getSession();
    cachingFactory.getSession();
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 30 with SessionFactory

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

the class SessionFactoryTests method testStaleConnection.

@Test
public void testStaleConnection() throws Exception {
    SessionFactory sessionFactory = Mockito.mock(SessionFactory.class);
    Session sessionA = Mockito.mock(Session.class);
    Session sessionB = Mockito.mock(Session.class);
    Mockito.when(sessionA.isOpen()).thenReturn(true);
    Mockito.when(sessionB.isOpen()).thenReturn(false);
    Mockito.when(sessionFactory.getSession()).thenReturn(sessionA);
    Mockito.when(sessionFactory.getSession()).thenReturn(sessionB);
    CachingSessionFactory cachingFactory = new CachingSessionFactory(sessionFactory, 2);
    Session firstSession = cachingFactory.getSession();
    Session secondSession = cachingFactory.getSession();
    secondSession.close();
    Session nonStaleSession = cachingFactory.getSession();
    assertEquals(TestUtils.getPropertyValue(firstSession, "targetSession"), TestUtils.getPropertyValue(nonStaleSession, "targetSession"));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) CachingSessionFactory(org.springframework.integration.file.remote.session.CachingSessionFactory) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Aggregations

SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)31 Test (org.junit.Test)30 MessageBuilder (org.springframework.integration.support.MessageBuilder)19 GenericMessage (org.springframework.messaging.support.GenericMessage)19 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)17 Session (org.springframework.integration.file.remote.session.Session)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)8 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 RunWith (org.junit.runner.RunWith)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2