Search in sources :

Example 81 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.

the class FtpFileListFilterTests method testMarkerFile.

@Test
public void testMarkerFile() throws Exception {
    FtpSystemMarkerFilePresentFileListFilter filter = new FtpSystemMarkerFilePresentFileListFilter(new FtpSimplePatternFileListFilter("*.txt"));
    FTPFile[] files = template.list("ftpSource");
    assertThat(files.length, greaterThan(0));
    List<FTPFile> filtered = filter.filterFiles(files);
    assertThat(filtered.size(), equalTo(0));
    File remoteDir = getSourceRemoteDirectory();
    File marker = new File(remoteDir, "ftpSource2.txt.complete");
    marker.createNewFile();
    files = template.list("ftpSource");
    filtered = filter.filterFiles(files);
    assertThat(filtered.size(), equalTo(1));
    assertThat(filtered.get(0).getName(), equalTo("ftpSource2.txt"));
    marker.delete();
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 82 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.

the class FtpOutboundTests method testHandleFileContentMessage.

@Test
public void testHandleFileContentMessage() 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<String>("String data"));
    assertTrue(file.exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("String 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 83 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project spring-integration by spring-projects.

the class FtpOutboundTests method testHandleMissingFileMessage.

@Test
public void testHandleMissingFileMessage() 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 = new File(UUID.randomUUID() + ".txt");
    Log logger = spy(TestUtils.getPropertyValue(handler, "remoteFileTemplate.logger", Log.class));
    when(logger.isWarnEnabled()).thenReturn(true);
    final AtomicReference<String> logged = new AtomicReference<>();
    doAnswer(invocation -> {
        logged.set(invocation.getArgument(0));
        invocation.callRealMethod();
        return null;
    }).when(logger).warn(Mockito.anyString());
    RemoteFileTemplate<?> template = TestUtils.getPropertyValue(handler, "remoteFileTemplate", RemoteFileTemplate.class);
    new DirectFieldAccessor(template).setPropertyValue("logger", logger);
    handler.handleMessage(new GenericMessage<File>(srcFile));
    assertNotNull(logged.get());
    assertEquals("File " + srcFile.toString() + " does not exist", logged.get());
}
Also used : Log(org.apache.commons.logging.Log) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) FTPFile(org.apache.commons.net.ftp.FTPFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 84 with FTPFile

use of org.apache.commons.net.ftp.FTPFile 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 85 with FTPFile

use of org.apache.commons.net.ftp.FTPFile 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

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4