Search in sources :

Example 1 with PropertiesPersistingMetadataStore

use of org.springframework.integration.metadata.PropertiesPersistingMetadataStore in project spring-integration by spring-projects.

the class FtpInboundRemoteFileSystemSynchronizerTests method testCopyFileToLocalDir.

@Test
public void testCopyFileToLocalDir() throws Exception {
    File localDirectory = new File("test");
    assertFalse(localDirectory.exists());
    TestFtpSessionFactory ftpSessionFactory = new TestFtpSessionFactory();
    ftpSessionFactory.setUsername("kermit");
    ftpSessionFactory.setPassword("frog");
    ftpSessionFactory.setHost("foo.com");
    FtpInboundFileSynchronizer synchronizer = spy(new FtpInboundFileSynchronizer(ftpSessionFactory));
    synchronizer.setDeleteRemoteFiles(true);
    synchronizer.setPreserveTimestamp(true);
    synchronizer.setRemoteDirectory("remote-test-dir");
    FtpRegexPatternFileListFilter patternFilter = new FtpRegexPatternFileListFilter(".*\\.test$");
    PropertiesPersistingMetadataStore store = spy(new PropertiesPersistingMetadataStore());
    store.setBaseDirectory("test");
    store.afterPropertiesSet();
    FtpPersistentAcceptOnceFileListFilter persistFilter = new FtpPersistentAcceptOnceFileListFilter(store, "foo");
    List<FileListFilter<FTPFile>> filters = new ArrayList<FileListFilter<FTPFile>>();
    filters.add(persistFilter);
    filters.add(patternFilter);
    CompositeFileListFilter<FTPFile> filter = new CompositeFileListFilter<FTPFile>(filters);
    synchronizer.setFilter(filter);
    ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = expressionParser.parseExpression("'subdir/' + #this.toUpperCase() + '.a'");
    synchronizer.setLocalFilenameGeneratorExpression(expression);
    synchronizer.setBeanFactory(mock(BeanFactory.class));
    synchronizer.afterPropertiesSet();
    FtpInboundFileSynchronizingMessageSource ms = new FtpInboundFileSynchronizingMessageSource(synchronizer);
    ms.setAutoCreateLocalDirectory(true);
    ms.setLocalDirectory(localDirectory);
    ms.setBeanFactory(mock(BeanFactory.class));
    CompositeFileListFilter<File> localFileListFilter = new CompositeFileListFilter<File>();
    localFileListFilter.addFilter(new RegexPatternFileListFilter(".*\\.TEST\\.a$"));
    AcceptOnceFileListFilter<File> localAcceptOnceFilter = new AcceptOnceFileListFilter<File>();
    localFileListFilter.addFilter(localAcceptOnceFilter);
    RecursiveDirectoryScanner scanner = new RecursiveDirectoryScanner();
    ms.setScanner(scanner);
    ms.setLocalFilter(localFileListFilter);
    ms.afterPropertiesSet();
    ms.start();
    Message<File> atestFile = ms.receive();
    assertNotNull(atestFile);
    assertEquals("A.TEST.a", atestFile.getPayload().getName());
    // The test remote files are created with the current timestamp + 1 day.
    assertThat(atestFile.getPayload().lastModified(), Matchers.greaterThan(System.currentTimeMillis()));
    assertEquals("A.TEST.a", atestFile.getHeaders().get(FileHeaders.FILENAME));
    Message<File> btestFile = ms.receive();
    assertNotNull(btestFile);
    assertEquals("B.TEST.a", 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/subdir/A.TEST.a").exists());
    assertTrue(new File("test/subdir/B.TEST.a").exists());
    TestUtils.getPropertyValue(localAcceptOnceFilter, "seenSet", Collection.class).clear();
    new File("test/subdir/A.TEST.a").delete();
    new File("test/subdir/B.TEST.a").delete();
    // the remote filter should prevent a re-fetch
    nothing = ms.receive();
    assertNull(nothing);
    ms.stop();
    verify(synchronizer).close();
    verify(store).close();
}
Also used : FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) RegexPatternFileListFilter(org.springframework.integration.file.filters.RegexPatternFileListFilter) FileListFilter(org.springframework.integration.file.filters.FileListFilter) CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) FtpRegexPatternFileListFilter(org.springframework.integration.ftp.filters.FtpRegexPatternFileListFilter) AcceptOnceFileListFilter(org.springframework.integration.file.filters.AcceptOnceFileListFilter) FtpPersistentAcceptOnceFileListFilter(org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter) Expression(org.springframework.expression.Expression) Collection(java.util.Collection) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) RecursiveDirectoryScanner(org.springframework.integration.file.RecursiveDirectoryScanner) Test(org.junit.Test)

Example 2 with PropertiesPersistingMetadataStore

use of org.springframework.integration.metadata.PropertiesPersistingMetadataStore in project spring-integration by spring-projects.

the class FeedEntryMessageSourceTests method testEntryHavingBeenUpdatedAfterPublishAndRepeat.

// verifies that when entry has been updated since publish, that is taken into
// account when determining if the feed entry has been seen before
@Test
public void testEntryHavingBeenUpdatedAfterPublishAndRepeat() throws Exception {
    ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/atom.xml");
    FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(resource, "foo");
    feedEntrySource.setBeanName("feedReader");
    PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
    metadataStore.afterPropertiesSet();
    feedEntrySource.setMetadataStore(metadataStore);
    feedEntrySource.setBeanFactory(mock(BeanFactory.class));
    feedEntrySource.afterPropertiesSet();
    SyndEntry entry1 = feedEntrySource.receive().getPayload();
    // only 1 entries in the test feed
    assertNull(feedEntrySource.receive());
    assertEquals("Atom draft-07 snapshot", entry1.getTitle().trim());
    assertEquals(1071318569000L, entry1.getPublishedDate().getTime());
    assertEquals(1122812969000L, entry1.getUpdatedDate().getTime());
    metadataStore.destroy();
    metadataStore.afterPropertiesSet();
    // now test that what's been read is no longer retrieved
    feedEntrySource = new FeedEntryMessageSource(resource, "foo");
    feedEntrySource.setBeanName("feedReader");
    metadataStore = new PropertiesPersistingMetadataStore();
    metadataStore.afterPropertiesSet();
    feedEntrySource.setMetadataStore(metadataStore);
    feedEntrySource.setBeanFactory(mock(BeanFactory.class));
    feedEntrySource.afterPropertiesSet();
    assertNull(feedEntrySource.receive());
}
Also used : SyndEntry(com.rometools.rome.feed.synd.SyndEntry) BeanFactory(org.springframework.beans.factory.BeanFactory) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 3 with PropertiesPersistingMetadataStore

use of org.springframework.integration.metadata.PropertiesPersistingMetadataStore 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)

Example 4 with PropertiesPersistingMetadataStore

use of org.springframework.integration.metadata.PropertiesPersistingMetadataStore in project spring-integration by spring-projects.

the class FeedEntryMessageSourceTests method testReceiveFeedWithRealEntriesAndRepeatWithPersistentMetadataStore.

// will test that last feed entry is remembered between the sessions
// and no duplicate entries are retrieved
@Test
public void testReceiveFeedWithRealEntriesAndRepeatWithPersistentMetadataStore() throws Exception {
    ClassPathResource resource = new ClassPathResource("org/springframework/integration/feed/sample.rss");
    FeedEntryMessageSource feedEntrySource = new FeedEntryMessageSource(resource, "foo");
    feedEntrySource.setBeanName("feedReader");
    PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
    metadataStore.afterPropertiesSet();
    feedEntrySource.setMetadataStore(metadataStore);
    feedEntrySource.setBeanFactory(mock(BeanFactory.class));
    feedEntrySource.afterPropertiesSet();
    SyndEntry entry1 = feedEntrySource.receive().getPayload();
    SyndEntry entry2 = feedEntrySource.receive().getPayload();
    SyndEntry entry3 = feedEntrySource.receive().getPayload();
    // only 3 entries in the test feed
    assertNull(feedEntrySource.receive());
    assertEquals("Spring Integration download", entry1.getTitle().trim());
    assertEquals(1266088337000L, entry1.getPublishedDate().getTime());
    assertEquals("Check out Spring Integration forums", entry2.getTitle().trim());
    assertEquals(1268469501000L, entry2.getPublishedDate().getTime());
    assertEquals("Spring Integration adapters", entry3.getTitle().trim());
    assertEquals(1272044098000L, entry3.getPublishedDate().getTime());
    metadataStore.destroy();
    metadataStore.afterPropertiesSet();
    // now test that what's been read is no longer retrieved
    feedEntrySource = new FeedEntryMessageSource(resource, "foo");
    feedEntrySource.setBeanName("feedReader");
    metadataStore = new PropertiesPersistingMetadataStore();
    metadataStore.afterPropertiesSet();
    feedEntrySource.setMetadataStore(metadataStore);
    feedEntrySource.setBeanFactory(mock(BeanFactory.class));
    feedEntrySource.afterPropertiesSet();
    assertNull(feedEntrySource.receive());
    assertNull(feedEntrySource.receive());
    assertNull(feedEntrySource.receive());
}
Also used : SyndEntry(com.rometools.rome.feed.synd.SyndEntry) BeanFactory(org.springframework.beans.factory.BeanFactory) PropertiesPersistingMetadataStore(org.springframework.integration.metadata.PropertiesPersistingMetadataStore) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 PropertiesPersistingMetadataStore (org.springframework.integration.metadata.PropertiesPersistingMetadataStore)4 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 AcceptOnceFileListFilter (org.springframework.integration.file.filters.AcceptOnceFileListFilter)2 CompositeFileListFilter (org.springframework.integration.file.filters.CompositeFileListFilter)2 FileListFilter (org.springframework.integration.file.filters.FileListFilter)2 RegexPatternFileListFilter (org.springframework.integration.file.filters.RegexPatternFileListFilter)2 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 FTPFile (org.apache.commons.net.ftp.FTPFile)1 Expression (org.springframework.expression.Expression)1 ExpressionParser (org.springframework.expression.ExpressionParser)1 SpelParserConfiguration (org.springframework.expression.spel.SpelParserConfiguration)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 RecursiveDirectoryScanner (org.springframework.integration.file.RecursiveDirectoryScanner)1 FtpPersistentAcceptOnceFileListFilter (org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)1