Search in sources :

Example 1 with SimpleMetadataStore

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

the class SearchReceivingMessageSourceTests method testSearchReceivingMessageSourceInit.

/**
 * Unit Test ensuring some basic initialization properties being set.
 */
@Test
public void testSearchReceivingMessageSourceInit() {
    final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(new TwitterTemplate("test"), "foo");
    messageSource.setComponentName("twitterSearchMessageSource");
    final Object metadataStore = TestUtils.getPropertyValue(messageSource, "metadataStore");
    final Object metadataKey = TestUtils.getPropertyValue(messageSource, "metadataKey");
    assertNull(metadataStore);
    assertNotNull(metadataKey);
    messageSource.setBeanFactory(mock(BeanFactory.class));
    messageSource.afterPropertiesSet();
    final Object metadataStoreInitialized = TestUtils.getPropertyValue(messageSource, "metadataStore");
    final Object metadataKeyInitialized = TestUtils.getPropertyValue(messageSource, "metadataKey");
    assertNotNull(metadataStoreInitialized);
    assertTrue(metadataStoreInitialized instanceof SimpleMetadataStore);
    assertNotNull(metadataKeyInitialized);
    assertEquals("foo", metadataKeyInitialized);
    final Twitter twitter = TestUtils.getPropertyValue(messageSource, "twitter", Twitter.class);
    assertFalse(twitter.isAuthorized());
    assertNotNull(twitter.userOperations());
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) Twitter(org.springframework.social.twitter.api.Twitter) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Test(org.junit.Test)

Example 2 with SimpleMetadataStore

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

the class PersistentAcceptOnceFileListFilterTests method testFileSystem.

@Test
public void testFileSystem() throws Exception {
    final AtomicBoolean suspend = new AtomicBoolean();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ConcurrentMetadataStore store = new SimpleMetadataStore() {

        @Override
        public boolean replace(String key, String oldValue, String newValue) {
            if (suspend.get()) {
                latch2.countDown();
                try {
                    latch1.await(10, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
            return super.replace(key, oldValue, newValue);
        }
    };
    final FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(store, "foo:");
    final File file = File.createTempFile("foo", ".txt");
    assertEquals(1, filter.filterFiles(new File[] { file }).size());
    String ts = store.get("foo:" + file.getAbsolutePath());
    assertEquals(String.valueOf(file.lastModified()), ts);
    assertEquals(0, filter.filterFiles(new File[] { file }).size());
    file.setLastModified(file.lastModified() + 5000L);
    assertEquals(1, filter.filterFiles(new File[] { file }).size());
    ts = store.get("foo:" + file.getAbsolutePath());
    assertEquals(String.valueOf(file.lastModified()), ts);
    assertEquals(0, filter.filterFiles(new File[] { file }).size());
    suspend.set(true);
    file.setLastModified(file.lastModified() + 5000L);
    Future<Integer> result = Executors.newSingleThreadExecutor().submit(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            return filter.filterFiles(new File[] { file }).size();
        }
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    store.put("foo:" + file.getAbsolutePath(), "43");
    latch1.countDown();
    Integer theResult = result.get(10, TimeUnit.SECONDS);
    // lost the race, key changed
    assertEquals(Integer.valueOf(0), theResult);
    file.delete();
    filter.close();
}
Also used : SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) Test(org.junit.Test)

Example 3 with SimpleMetadataStore

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

the class PersistentAcceptOnceFileListFilterTests method testFlush.

@Test
public /*
	 * INT-3721: Test all operations that can cause the metadata to be flushed.
	 */
void testFlush() throws Exception {
    final AtomicInteger flushes = new AtomicInteger();
    final AtomicBoolean replaced = new AtomicBoolean();
    class MS extends SimpleMetadataStore implements Flushable, Closeable {

        @Override
        public void flush() throws IOException {
            flushes.incrementAndGet();
        }

        @Override
        public void close() throws IOException {
            flush();
        }

        @Override
        public boolean replace(String key, String oldValue, String newValue) {
            replaced.set(true);
            return super.replace(key, oldValue, newValue);
        }
    }
    MS store = new MS();
    String prefix = "flush:";
    FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(store, prefix);
    final File file = File.createTempFile("foo", ".txt");
    File[] files = new File[] { file };
    List<File> passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    filter.rollback(passed.get(0), passed);
    assertEquals(0, flushes.get());
    filter.setFlushOnUpdate(true);
    passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    assertEquals(1, flushes.get());
    filter.rollback(passed.get(0), passed);
    assertEquals(2, flushes.get());
    passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    assertEquals(3, flushes.get());
    passed = filter.filterFiles(files);
    assertEquals(0, passed.size());
    assertEquals(3, flushes.get());
    assertFalse(replaced.get());
    store.put(prefix + file.getAbsolutePath(), "1");
    passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    assertEquals(4, flushes.get());
    assertTrue(replaced.get());
    file.delete();
    filter.close();
    assertEquals(5, flushes.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(java.io.Closeable) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Flushable(java.io.Flushable) File(java.io.File) Test(org.junit.Test)

Example 4 with SimpleMetadataStore

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

the class PersistentAcceptOnceFileListFilterTests method testRollbackFileSystem.

@Test
public void testRollbackFileSystem() throws Exception {
    FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
    File[] files = new File[] { new File("foo"), new File("bar"), new File("baz") };
    List<File> passed = filter.filterFiles(files);
    assertTrue(Arrays.equals(files, passed.toArray()));
    List<File> now = filter.filterFiles(files);
    assertEquals(0, now.size());
    filter.rollback(passed.get(1), passed);
    now = filter.filterFiles(files);
    assertEquals(2, now.size());
    assertEquals("bar", now.get(0).getName());
    assertEquals("baz", now.get(1).getName());
    now = filter.filterFiles(files);
    assertEquals(0, now.size());
    filter.close();
}
Also used : SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) File(java.io.File) Test(org.junit.Test)

Example 5 with SimpleMetadataStore

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

the class SftpInboundChannelAdapterSpec method composeFilters.

@SuppressWarnings("unchecked")
private CompositeFileListFilter<ChannelSftp.LsEntry> composeFilters(FileListFilter<ChannelSftp.LsEntry> fileListFilter) {
    CompositeFileListFilter<ChannelSftp.LsEntry> compositeFileListFilter = new CompositeFileListFilter<>();
    compositeFileListFilter.addFilters(fileListFilter, new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "sftpMessageSource"));
    return compositeFileListFilter;
}
Also used : CompositeFileListFilter(org.springframework.integration.file.filters.CompositeFileListFilter) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) SftpPersistentAcceptOnceFileListFilter(org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter)

Aggregations

SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)16 Test (org.junit.Test)10 File (java.io.File)5 BeanFactory (org.springframework.beans.factory.BeanFactory)4 CompositeFileListFilter (org.springframework.integration.file.filters.CompositeFileListFilter)4 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 FTPFile (org.apache.commons.net.ftp.FTPFile)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 SftpATTRS (com.jcraft.jsch.SftpATTRS)2 IOException (java.io.IOException)2 Constructor (java.lang.reflect.Constructor)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 FileSystemPersistentAcceptOnceFileListFilter (org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter)2 FtpPersistentAcceptOnceFileListFilter (org.springframework.integration.ftp.filters.FtpPersistentAcceptOnceFileListFilter)2 ConcurrentMetadataStore (org.springframework.integration.metadata.ConcurrentMetadataStore)2 SftpPersistentAcceptOnceFileListFilter (org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter)2 Closeable (java.io.Closeable)1 FileNotFoundException (java.io.FileNotFoundException)1