Search in sources :

Example 6 with DefaultFileNameGenerator

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

the class FileWritingMessageHandlerSpec method fileNameExpression.

/**
 * Set the {@link DefaultFileNameGenerator} based on the provided SpEL expression.
 * @param fileNameExpression the SpEL expression for file names generation.
 * @return the current Spec
 */
public FileWritingMessageHandlerSpec fileNameExpression(String fileNameExpression) {
    Assert.isNull(this.fileNameGenerator, "'fileNameGenerator' and 'fileNameGeneratorExpression' are mutually exclusive.");
    this.defaultFileNameGenerator = new DefaultFileNameGenerator();
    this.defaultFileNameGenerator.setExpression(fileNameExpression);
    return fileNameGenerator(this.defaultFileNameGenerator);
}
Also used : DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator)

Example 7 with DefaultFileNameGenerator

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

the class FileOutboundChannelAdapterParserTests method simpleAdapter.

@Test
public void simpleAdapter() {
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(simpleAdapter);
    FileWritingMessageHandler handler = (FileWritingMessageHandler) adapterAccessor.getPropertyValue("handler");
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
    File expected = new File(System.getProperty("java.io.tmpdir"));
    Expression destinationDirectoryExpression = (Expression) handlerAccessor.getPropertyValue("destinationDirectoryExpression");
    File actual = new File(destinationDirectoryExpression.getExpressionString());
    assertEquals(".foo", TestUtils.getPropertyValue(handler, "temporaryFileSuffix", String.class));
    assertThat(actual, is(expected));
    DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
    assertNotNull(fileNameGenerator);
    Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class);
    assertNotNull(expression);
    assertEquals("'foo.txt'", expression.getExpressionString());
    assertEquals(Boolean.FALSE, handlerAccessor.getPropertyValue("deleteSourceFiles"));
    assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("flushWhenIdle"));
    if (FileUtils.IS_POSIX) {
        assertThat(TestUtils.getPropertyValue(handler, "permissions", Set.class).size(), equalTo(9));
    }
    assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("preserveTimestamp"));
}
Also used : Expression(org.springframework.expression.Expression) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) FileWritingMessageHandler(org.springframework.integration.file.FileWritingMessageHandler) File(java.io.File) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 8 with DefaultFileNameGenerator

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

the class SftpOutboundTests method testHandleFileMessage.

@Test
public void testHandleFileMessage() throws Exception {
    File targetDir = new File("remote-target-dir");
    assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists());
    SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
    DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
    fGenerator.setBeanFactory(mock(BeanFactory.class));
    fGenerator.setExpression("payload + '.test'");
    handler.setFileNameGenerator(fGenerator);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    File srcFile = File.createTempFile("testHandleFileMessage", ".tmp", new File("."));
    srcFile.deleteOnExit();
    File destFile = new File(targetDir, srcFile.getName() + ".test");
    destFile.deleteOnExit();
    handler.handleMessage(new GenericMessage<>(srcFile));
    assertTrue("destination file was not created", destFile.exists());
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 9 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator 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 10 with DefaultFileNameGenerator

use of org.springframework.integration.file.DefaultFileNameGenerator 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

DefaultFileNameGenerator (org.springframework.integration.file.DefaultFileNameGenerator)11 Test (org.junit.Test)9 File (java.io.File)6 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 FileTransferringMessageHandler (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler)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 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Bean (org.springframework.context.annotation.Bean)2 Configuration (org.springframework.context.annotation.Configuration)2 Expression (org.springframework.expression.Expression)2 FileWritingMessageHandler (org.springframework.integration.file.FileWritingMessageHandler)2 ClientCallbackWithoutResult (org.springframework.integration.file.remote.ClientCallbackWithoutResult)2 SessionCallbackWithoutResult (org.springframework.integration.file.remote.SessionCallbackWithoutResult)2