Search in sources :

Example 1 with ClientCallbackWithoutResult

use of org.springframework.integration.file.remote.ClientCallbackWithoutResult 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 2 with ClientCallbackWithoutResult

use of org.springframework.integration.file.remote.ClientCallbackWithoutResult 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)

Aggregations

Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Bean (org.springframework.context.annotation.Bean)2 Configuration (org.springframework.context.annotation.Configuration)2 LiteralExpression (org.springframework.expression.common.LiteralExpression)2 DefaultFileNameGenerator (org.springframework.integration.file.DefaultFileNameGenerator)2 ClientCallbackWithoutResult (org.springframework.integration.file.remote.ClientCallbackWithoutResult)2 SessionCallbackWithoutResult (org.springframework.integration.file.remote.SessionCallbackWithoutResult)2 SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 ContextConfiguration (org.springframework.test.context.ContextConfiguration)2 SpringJUnit4ClassRunner (org.springframework.test.context.junit4.SpringJUnit4ClassRunner)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)1 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 SftpATTRS (com.jcraft.jsch.SftpATTRS)1 SftpException (com.jcraft.jsch.SftpException)1