Search in sources :

Example 1 with SftpRegexPatternFileListFilter

use of org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter in project spring-integration by spring-projects.

the class SftpInboundRemoteFileSystemSynchronizerTests method testCopyFileToLocalDir.

@Test
public void testCopyFileToLocalDir() throws Exception {
    File localDirectory = new File("test");
    assertFalse(localDirectory.exists());
    TestSftpSessionFactory ftpSessionFactory = new TestSftpSessionFactory();
    ftpSessionFactory.setUser("kermit");
    ftpSessionFactory.setPassword("frog");
    ftpSessionFactory.setHost("foo.com");
    SftpInboundFileSynchronizer synchronizer = spy(new SftpInboundFileSynchronizer(ftpSessionFactory));
    synchronizer.setDeleteRemoteFiles(true);
    synchronizer.setPreserveTimestamp(true);
    synchronizer.setRemoteDirectory("remote-test-dir");
    SftpRegexPatternFileListFilter patternFilter = new SftpRegexPatternFileListFilter(".*\\.test$");
    PropertiesPersistingMetadataStore store = spy(new PropertiesPersistingMetadataStore());
    store.setBaseDirectory("test");
    store.afterPropertiesSet();
    SftpPersistentAcceptOnceFileListFilter persistFilter = new SftpPersistentAcceptOnceFileListFilter(store, "foo");
    List<FileListFilter<LsEntry>> filters = new ArrayList<FileListFilter<LsEntry>>();
    filters.add(persistFilter);
    filters.add(patternFilter);
    CompositeFileListFilter<LsEntry> filter = new CompositeFileListFilter<LsEntry>(filters);
    synchronizer.setFilter(filter);
    synchronizer.setBeanFactory(mock(BeanFactory.class));
    synchronizer.afterPropertiesSet();
    SftpInboundFileSynchronizingMessageSource ms = new SftpInboundFileSynchronizingMessageSource(synchronizer);
    ms.setAutoCreateLocalDirectory(true);
    ms.setLocalDirectory(localDirectory);
    ms.setBeanFactory(mock(BeanFactory.class));
    CompositeFileListFilter<File> localFileListFilter = new CompositeFileListFilter<File>();
    localFileListFilter.addFilter(new RegexPatternFileListFilter(".*\\.test$"));
    AcceptOnceFileListFilter<File> localAcceptOnceFilter = new AcceptOnceFileListFilter<File>();
    localFileListFilter.addFilter(localAcceptOnceFilter);
    ms.setLocalFilter(localFileListFilter);
    ms.afterPropertiesSet();
    ms.start();
    Message<File> atestFile = ms.receive();
    assertNotNull(atestFile);
    assertEquals("a.test", atestFile.getPayload().getName());
    // The test remote files are created with the current timestamp + 1 day.
    assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
    Message<File> btestFile = ms.receive();
    assertNotNull(btestFile);
    assertEquals("b.test", btestFile.getPayload().getName());
    // The test remote files are created with the current timestamp + 1 day.
    assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
    Message<File> nothing = ms.receive();
    assertNull(nothing);
    // two times because on the third receive (above) the internal queue will be empty, so it will attempt
    verify(synchronizer, times(2)).synchronizeToLocalDirectory(localDirectory, Integer.MIN_VALUE);
    assertTrue(new File("test/a.test").exists());
    assertTrue(new File("test/b.test").exists());
    TestUtils.getPropertyValue(localAcceptOnceFilter, "seenSet", Collection.class).clear();
    new File("test/a.test").delete();
    new File("test/b.test").delete();
    // the remote filter should prevent a re-fetch
    nothing = ms.receive();
    assertNull(nothing);
    ms.stop();
    verify(synchronizer).close();
    verify(store).close();
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) ArrayList(java.util.ArrayList) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) BeanFactory(org.springframework.beans.factory.BeanFactory) Collection(java.util.Collection) SftpRegexPatternFileListFilter(org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter) File(java.io.File) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter) Test(org.junit.Test)

Aggregations

LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Test (org.junit.Test)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 AcceptOnceFileListFilter (org.springframework.integration.file.filters.AcceptOnceFileListFilter)1 CompositeFileListFilter (org.springframework.integration.file.filters.CompositeFileListFilter)1 FileListFilter (org.springframework.integration.file.filters.FileListFilter)1 RegexPatternFileListFilter (org.springframework.integration.file.filters.RegexPatternFileListFilter)1 PropertiesPersistingMetadataStore (org.springframework.integration.metadata.PropertiesPersistingMetadataStore)1 SftpPersistentAcceptOnceFileListFilter (org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter)1 SftpRegexPatternFileListFilter (org.springframework.integration.sftp.filters.SftpRegexPatternFileListFilter)1