use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.
the class WatchServiceDirectoryScannerTests method testWatchServiceDirectoryScanner.
@Test
public void testWatchServiceDirectoryScanner() throws Exception {
FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
fileReadingMessageSource.setDirectory(folder.getRoot());
fileReadingMessageSource.setUseWatchService(true);
fileReadingMessageSource.setWatchEvents(FileReadingMessageSource.WatchEventType.CREATE, FileReadingMessageSource.WatchEventType.MODIFY, FileReadingMessageSource.WatchEventType.DELETE);
fileReadingMessageSource.setBeanFactory(mock(BeanFactory.class));
final CountDownLatch removeFileLatch = new CountDownLatch(1);
FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "test") {
@Override
public boolean remove(File fileToRemove) {
removeFileLatch.countDown();
return super.remove(fileToRemove);
}
};
fileReadingMessageSource.setFilter(filter);
fileReadingMessageSource.afterPropertiesSet();
fileReadingMessageSource.start();
DirectoryScanner scanner = fileReadingMessageSource.getScanner();
assertThat(scanner.getClass().getName(), containsString("FileReadingMessageSource$WatchServiceDirectoryScanner"));
List<File> files = scanner.listFiles(folder.getRoot());
assertEquals(3, files.size());
assertTrue(files.contains(top1));
assertTrue(files.contains(foo1));
assertTrue(files.contains(bar1));
fileReadingMessageSource.start();
File top2 = this.folder.newFile();
File foo2 = File.createTempFile("foo", ".txt", this.foo);
File bar2 = File.createTempFile("bar", ".txt", this.bar);
File baz = new File(this.foo, "baz");
baz.mkdir();
File baz1 = File.createTempFile("baz", ".txt", baz);
files = scanner.listFiles(folder.getRoot());
int n = 0;
Set<File> accum = new HashSet<File>(files);
while (n++ < 300 && accum.size() != 4) {
Thread.sleep(100);
files = scanner.listFiles(folder.getRoot());
accum.addAll(files);
}
assertEquals(4, accum.size());
assertTrue(accum.contains(top2));
assertTrue(accum.contains(foo2));
assertTrue(accum.contains(bar2));
assertTrue(accum.contains(baz1));
/*See AbstractWatchKey#signalEvent source code:
if(var5 >= 512) {
var1 = StandardWatchEventKinds.OVERFLOW;
}
*/
fileReadingMessageSource.start();
List<File> filesForOverflow = new ArrayList<File>(600);
for (int i = 0; i < 600; i++) {
filesForOverflow.add(this.folder.newFile("" + i));
}
n = 0;
while (n++ < 300 && accum.size() < 604) {
Thread.sleep(100);
files = scanner.listFiles(folder.getRoot());
accum.addAll(files);
}
assertEquals(604, accum.size());
for (File fileForOverFlow : filesForOverflow) {
accum.contains(fileForOverFlow);
}
File baz2 = File.createTempFile("baz2", ".txt", baz);
n = 0;
while (n++ < 300 && accum.size() < 605) {
Thread.sleep(100);
files = scanner.listFiles(folder.getRoot());
accum.addAll(files);
}
assertTrue(accum.contains(baz2));
File baz2Copy = new File(baz2.getAbsolutePath());
baz2Copy.setLastModified(baz2.lastModified() + 100000);
n = 0;
files.clear();
while (n++ < 300 && files.size() < 1) {
Thread.sleep(100);
files = scanner.listFiles(folder.getRoot());
accum.addAll(files);
}
assertEquals(1, files.size());
assertTrue(files.contains(baz2));
baz2.delete();
n = 0;
while (n++ < 300 && removeFileLatch.getCount() > 0) {
Thread.sleep(100);
scanner.listFiles(folder.getRoot());
}
assertTrue(removeFileLatch.await(10, TimeUnit.SECONDS));
File baz3 = File.createTempFile("baz3", ".txt", baz);
n = 0;
Message<File> fileMessage = null;
while (n++ < 300 && (fileMessage = fileReadingMessageSource.receive()) == null) {
Thread.sleep(100);
}
assertNotNull(fileMessage);
assertEquals(baz3, fileMessage.getPayload());
assertThat(fileMessage.getHeaders().get(FileHeaders.RELATIVE_PATH, String.class), startsWith(TestUtils.applySystemFileSeparator("foo/baz/")));
fileReadingMessageSource.stop();
}
use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.
the class FtpInboundChannelAdapterSpec method composeFilters.
@SuppressWarnings("unchecked")
private CompositeFileListFilter<FTPFile> composeFilters(FileListFilter<FTPFile> fileListFilter) {
CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
compositeFileListFilter.addFilters(fileListFilter, new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "ftpMessageSource"));
return compositeFileListFilter;
}
use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.
the class SftpPersistentAcceptOnceFileListFilterTests method testKeyUsingFileName.
@Test
public void testKeyUsingFileName() throws Exception {
SftpPersistentAcceptOnceFileListFilter filter = new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
ChannelSftp channel = new ChannelSftp();
SftpATTRS attrs = mock(SftpATTRS.class);
@SuppressWarnings("unchecked") Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
ctor.setAccessible(true);
LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "same", attrs);
LsEntry sftpFile2 = ctor.newInstance(channel, "bar", "same", attrs);
LsEntry[] files = new LsEntry[] { sftpFile1, sftpFile2 };
List<LsEntry> now = filter.filterFiles(files);
assertEquals(2, now.size());
assertEquals("foo", now.get(0).getFilename());
assertEquals("bar", now.get(1).getFilename());
filter.close();
}
use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.
the class SftpPersistentAcceptOnceFileListFilterTests method testRollback.
@Test
public void testRollback() throws Exception {
SftpPersistentAcceptOnceFileListFilter filter = new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "rollback:");
ChannelSftp channel = new ChannelSftp();
SftpATTRS attrs = mock(SftpATTRS.class);
@SuppressWarnings("unchecked") Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
ctor.setAccessible(true);
LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "foo", attrs);
LsEntry sftpFile2 = ctor.newInstance(channel, "bar", "bar", attrs);
LsEntry ftpFile3 = ctor.newInstance(channel, "baz", "baz", attrs);
LsEntry[] files = new LsEntry[] { sftpFile1, sftpFile2, ftpFile3 };
List<LsEntry> passed = filter.filterFiles(files);
assertTrue(Arrays.equals(files, passed.toArray()));
List<LsEntry> 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).getFilename());
assertEquals("baz", now.get(1).getFilename());
now = filter.filterFiles(files);
assertEquals(0, now.size());
filter.close();
}
use of org.springframework.integration.metadata.SimpleMetadataStore in project spring-integration by spring-projects.
the class SftpStreamingInboundChannelAdapterSpec method composeFilters.
@SuppressWarnings("unchecked")
private CompositeFileListFilter<LsEntry> composeFilters(FileListFilter<LsEntry> fileListFilter) {
CompositeFileListFilter<LsEntry> compositeFileListFilter = new CompositeFileListFilter<>();
compositeFileListFilter.addFilters(fileListFilter, new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "sftpStreamingMessageSource"));
return compositeFileListFilter;
}
Aggregations