Search in sources :

Example 86 with FTPFile

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

the class FtpServerOutboundTests method testMgetPartial.

@Test
public void testMgetPartial() throws Exception {
    Session<FTPFile> session = spyOnSession();
    doAnswer(invocation -> {
        FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
        // add an extra file where the get will fail
        files = Arrays.copyOf(files, files.length + 1);
        FTPFile bogusFile = new FTPFile();
        bogusFile.setName("bogus.txt");
        files[files.length - 1] = bogusFile;
        return files;
    }).when(session).list("ftpSource/subFtpSource/*");
    String dir = "ftpSource/subFtpSource/";
    try {
        this.inboundMGet.send(new GenericMessage<Object>(dir + "*"));
        fail("expected exception");
    } catch (PartialSuccessException e) {
        assertEquals(2, e.getDerivedInput().size());
        assertEquals(1, e.getPartialResults().size());
        assertThat(e.getCause().getMessage(), containsString("/ftpSource/subFtpSource/bogus.txt: No such file or directory."));
    }
}
Also used : PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 87 with FTPFile

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

the class FtpServerOutboundTests method testMgetRecursivePartial.

@Test
public void testMgetRecursivePartial() throws Exception {
    Session<FTPFile> session = spyOnSession();
    doAnswer(invocation -> {
        FTPFile[] files = (FTPFile[]) invocation.callRealMethod();
        // add an extra file where the get will fail
        files = Arrays.copyOf(files, files.length + 1);
        FTPFile bogusFile = new FTPFile();
        bogusFile.setName("bogus.txt");
        bogusFile.setTimestamp(Calendar.getInstance());
        files[files.length - 1] = bogusFile;
        return files;
    }).when(session).list("ftpSource/subFtpSource/");
    String dir = "ftpSource/";
    try {
        this.inboundMGetRecursive.send(new GenericMessage<Object>(dir + "*"));
        fail("expected exception");
    } catch (PartialSuccessException e) {
        assertEquals(4, e.getDerivedInput().size());
        assertEquals(2, e.getPartialResults().size());
        assertThat(e.getCause().getMessage(), containsString("/ftpSource/subFtpSource/bogus.txt: No such file or directory."));
    }
}
Also used : PartialSuccessException(org.springframework.integration.support.PartialSuccessException) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 88 with FTPFile

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

the class FtpServerOutboundTests method testRawGETWithTemplate.

@Test
public void testRawGETWithTemplate() throws Exception {
    RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<FTPFile>(this.ftpSessionFactory);
    template.setFileNameExpression(new SpelExpressionParser().parseExpression("payload"));
    template.setBeanFactory(mock(BeanFactory.class));
    template.afterPropertiesSet();
    final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    assertTrue(template.get(new GenericMessage<String>("ftpSource/ ftpSource1.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos1)));
    assertEquals("source1", new String(baos1.toByteArray()));
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    assertTrue(template.get(new GenericMessage<String>("ftpSource/ftpSource2.txt"), (InputStreamCallback) stream -> FileCopyUtils.copy(stream, baos2)));
    assertEquals("source2", new String(baos2.toByteArray()));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) InputStreamCallback(org.springframework.integration.file.remote.InputStreamCallback) FTPFile(org.apache.commons.net.ftp.FTPFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 89 with FTPFile

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

the class FtpServerOutboundTests method testMGETOnNullDir.

@Test
@SuppressWarnings("unchecked")
public void testMGETOnNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();
    this.inboundMGet.send(new GenericMessage<Object>(""));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);
    List<File> localFiles = (List<File>) result.getPayload();
    assertThat(localFiles.size(), Matchers.greaterThan(0));
    for (File file : localFiles) {
        assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt"));
        assertThat(file.getName(), not(containsString("null")));
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) List(java.util.List) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 90 with FTPFile

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

the class FtpServerOutboundTests method testLsForNullDir.

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();
    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));
    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) FTPClient(org.apache.commons.net.ftp.FTPClient) 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