GH-2569: Add ChainFileListFilter constructors
* Adding ChainFileListFilter constructors to behave like CompositeFileListFilter Fixes https://github.com/spring-projects/spring-integration/issues/2569 Adding constructors in `ChainFileListFilter` matching super, so this class behave like `CompositeFileListFilter` when using XML flow configuration. * Fix import error * Fix checkstyle * Fix checkstyle * Fix checkstyle * Fix author name case Remove unnecessary javadoc * Fix code smell squid:S1155 * Add a simple test case for initializing filters with constructor Fix code smell squid:S1192 * Add a simple test case for initializing filters with constructor Fix code smell squid:S1192 * Fix missing import
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.integration.file.filters;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
@@ -31,18 +32,27 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
* @author Cengis Kocaurlu
|
||||
*
|
||||
* @since 4.3.7
|
||||
*
|
||||
*/
|
||||
public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
|
||||
|
||||
public ChainFileListFilter() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ChainFileListFilter(Collection<? extends FileListFilter<F>> fileFilters) {
|
||||
super(fileFilters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<F> filterFiles(F[] files) {
|
||||
Assert.notNull(files, "'files' should not be null");
|
||||
List<F> leftOver = Arrays.asList(files);
|
||||
for (FileListFilter<F> fileFilter : this.fileFilters) {
|
||||
if (leftOver.size() == 0) {
|
||||
if (leftOver.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -20,20 +20,24 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Aaron Grant
|
||||
* @author Cengis Kocaurlu
|
||||
*
|
||||
* @since 4.3.8
|
||||
*/
|
||||
public class ChainFileListFilterIntegrationTests {
|
||||
|
||||
private static final String PATTERN_ANY_TEXT_FILES = "*.txt";
|
||||
|
||||
private File[] noFiles = new File[0];
|
||||
|
||||
private File[] oneFile = new File[] { new MockOldFile("file.txt") };
|
||||
private File[] oneFile = new File[]{new MockOldFile("file.txt")};
|
||||
|
||||
@Test
|
||||
public void singleModifiedFilterNoFiles() throws IOException {
|
||||
@@ -47,7 +51,7 @@ public class ChainFileListFilterIntegrationTests {
|
||||
@Test
|
||||
public void singlePatternFilter() throws IOException {
|
||||
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
|
||||
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
|
||||
chain.addFilter(new SimplePatternFileListFilter(PATTERN_ANY_TEXT_FILES));
|
||||
List<File> result = chain.filterFiles(oneFile);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
@@ -65,7 +69,7 @@ public class ChainFileListFilterIntegrationTests {
|
||||
@Test
|
||||
public void patternThenModifiedFilters() throws IOException {
|
||||
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
|
||||
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
|
||||
chain.addFilter(new SimplePatternFileListFilter(PATTERN_ANY_TEXT_FILES));
|
||||
chain.addFilter(new LastModifiedFileListFilter());
|
||||
List<File> result = chain.filterFiles(oneFile);
|
||||
assertEquals(1, result.size());
|
||||
@@ -76,7 +80,16 @@ public class ChainFileListFilterIntegrationTests {
|
||||
public void modifiedThenPatternFilters() throws IOException {
|
||||
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
|
||||
chain.addFilter(new LastModifiedFileListFilter());
|
||||
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
|
||||
chain.addFilter(new SimplePatternFileListFilter(PATTERN_ANY_TEXT_FILES));
|
||||
List<File> result = chain.filterFiles(oneFile);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
}
|
||||
|
||||
//https://github.com/spring-projects/spring-integration/issues/2569
|
||||
@Test
|
||||
public void initializeFilterByConstructor() throws IOException {
|
||||
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>(Arrays.asList(new SimplePatternFileListFilter(PATTERN_ANY_TEXT_FILES)))) {
|
||||
List<File> result = chain.filterFiles(oneFile);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user