use of org.springframework.integration.file.filters.AcceptOnceFileListFilter 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();
}
use of org.springframework.integration.file.filters.AcceptOnceFileListFilter in project spring-integration by spring-projects.
the class FileInboundChannelAdapterWithPreventDuplicatesFlagTests method defaultAndNull.
@Test
public void defaultAndNull() throws Exception {
FileListFilter<File> filter = this.extractFilter("defaultAndNull");
assertNotNull(filter);
assertFalse(filter instanceof CompositeFileListFilter);
assertTrue(filter instanceof AcceptOnceFileListFilter);
File testFile = new File("test");
File[] files = new File[] { testFile, testFile, testFile };
List<File> result = filter.filterFiles(files);
assertEquals(1, result.size());
}
use of org.springframework.integration.file.filters.AcceptOnceFileListFilter in project spring-integration by spring-projects.
the class FileInboundChannelAdapterWithPreventDuplicatesFlagTests method defaultAndFalse.
@Test
public void defaultAndFalse() throws Exception {
FileListFilter<File> filter = this.extractFilter("defaultAndFalse");
assertNotNull(filter);
assertFalse(filter instanceof CompositeFileListFilter);
assertFalse(filter instanceof AcceptOnceFileListFilter);
File testFile = new File("test");
File[] files = new File[] { testFile, testFile, testFile };
List<File> result = filter.filterFiles(files);
assertEquals(3, result.size());
}
use of org.springframework.integration.file.filters.AcceptOnceFileListFilter 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();
}
use of org.springframework.integration.file.filters.AcceptOnceFileListFilter in project spring-integration by spring-projects.
the class FileListFilterFactoryBean method initializeFileListFilter.
private void initializeFileListFilter() {
if (this.result != null) {
return;
}
FileListFilter<File> createdFilter = null;
if ((this.filter != null) && (this.filenamePattern != null || this.filenameRegex != null)) {
throw new IllegalArgumentException("The 'filter' reference is mutually exclusive with " + "either the 'filename-pattern' or 'filename-regex' attribute.");
}
if (this.filenamePattern != null && this.filenameRegex != null) {
throw new IllegalArgumentException("The 'filename-pattern' and 'filename-regex' attributes are mutually exclusive.");
}
final List<FileListFilter<File>> filtersNeeded = new ArrayList<FileListFilter<File>>();
if (!Boolean.FALSE.equals(this.ignoreHidden)) {
filtersNeeded.add(new IgnoreHiddenFileListFilter());
}
// 'filter' is set
if (this.filter != null) {
if (Boolean.TRUE.equals(this.preventDuplicates)) {
filtersNeeded.add(new AcceptOnceFileListFilter<File>());
filtersNeeded.add(this.filter);
} else {
// preventDuplicates is either FALSE or NULL
filtersNeeded.add(this.filter);
}
} else // 'file-pattern' or 'file-regex' is set
if (this.filenamePattern != null || this.filenameRegex != null) {
if (!Boolean.FALSE.equals(this.preventDuplicates)) {
// preventDuplicates is either null or true
filtersNeeded.add(new AcceptOnceFileListFilter<File>());
}
if (this.filenamePattern != null) {
SimplePatternFileListFilter patternFilter = new SimplePatternFileListFilter(this.filenamePattern);
if (this.alwaysAcceptDirectories != null) {
patternFilter.setAlwaysAcceptDirectories(this.alwaysAcceptDirectories);
}
filtersNeeded.add(patternFilter);
}
if (this.filenameRegex != null) {
RegexPatternFileListFilter regexFilter = new RegexPatternFileListFilter(this.filenameRegex);
if (this.alwaysAcceptDirectories != null) {
regexFilter.setAlwaysAcceptDirectories(this.alwaysAcceptDirectories);
}
filtersNeeded.add(regexFilter);
}
} else // no filters are provided
if (Boolean.FALSE.equals(this.preventDuplicates)) {
filtersNeeded.add(new AcceptAllFileListFilter<File>());
} else {
// preventDuplicates is either TRUE or NULL
filtersNeeded.add(new AcceptOnceFileListFilter<File>());
}
if (filtersNeeded.size() == 1) {
createdFilter = filtersNeeded.get(0);
} else {
createdFilter = new CompositeFileListFilter<File>(filtersNeeded);
}
this.result = createdFilter;
}
Aggregations