Search in sources :

Example 11 with FileTransferringMessageHandler

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

the class FtpOutboundTests method testHandleFileAsByte.

@Test
public void testHandleFileAsByte() throws Exception {
    File file = new File("remote-target-dir/handlerContent.test");
    if (file.exists()) {
        file.delete();
    }
    assertFalse(file.exists());
    FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
    handler.setFileNameGenerator(message -> "handlerContent.test");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(new GenericMessage<byte[]>("byte[] data".getBytes()));
    assertTrue(file.exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("byte[] data", new String(inFile));
    file.delete();
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 12 with FileTransferringMessageHandler

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

the class FtpOutboundTests 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());
    FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
    handler.setFileNameGenerator(message -> ((File) message.getPayload()).getName() + ".test");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    File srcFile = File.createTempFile("testHandleFileMessage", ".tmp");
    srcFile.deleteOnExit();
    File destFile = new File(targetDir, srcFile.getName() + ".test");
    destFile.deleteOnExit();
    handler.handleMessage(new GenericMessage<File>(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) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 FileTransferringMessageHandler (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler)12 LiteralExpression (org.springframework.expression.common.LiteralExpression)11 BeanFactory (org.springframework.beans.factory.BeanFactory)10 File (java.io.File)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)4 FTPFile (org.apache.commons.net.ftp.FTPFile)4 DefaultFileNameGenerator (org.springframework.integration.file.DefaultFileNameGenerator)3 InputStream (java.io.InputStream)2 Set (java.util.Set)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 RemoteFileTemplate (org.springframework.integration.file.remote.RemoteFileTemplate)2 MessageHandler (org.springframework.messaging.MessageHandler)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Log (org.apache.commons.logging.Log)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1