INT-4215: ChainFileListFilter Exit When Empty

JIRA: https://jira.spring.io/browse/INT-4215

Due to type erasure we can't create an empty generic array from an empty list.
Don't attempt to call remaining filters when the list is empty.
This commit is contained in:
Gary Russell
2017-01-25 15:32:07 -05:00
parent d47bca48cc
commit 1674cd0f85
3 changed files with 10 additions and 3 deletions

View File

@@ -23,13 +23,15 @@ import org.springframework.util.Assert;
/**
* The {@link CompositeFileListFilter} extension which chains the result
* of the previous filter to the next one.
* of the previous filter to the next one. If a filter in the chain returns
* an empty list, the remaining filters are not invoked.
* @param <F> The type that will be filtered.
*
* @author Artem Bilan
* @author Gary Russell
*
* @since 4.3.7
*
* @param <F> The type that will be filtered.
*/
public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
@@ -38,6 +40,9 @@ public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
Assert.notNull(files, "'files' should not be null");
List<F> leftOver = Arrays.asList(files);
for (FileListFilter<F> fileFilter : this.fileFilters) {
if (leftOver.size() == 0) {
break;
}
@SuppressWarnings("unchecked")
F[] fileArray = (F[]) leftOver.toArray();
leftOver = fileFilter.filterFiles(fileArray);

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
@@ -102,7 +103,7 @@ public class CompositeFileListFilterTests {
assertEquals(noFiles, compositeFileFilter.filterFiles(new File[] { this.fileMock }));
verify(fileFilterMock1).filterFiles(argThat(arrayWithSize(1)));
verify(fileFilterMock2).filterFiles(argThat(arrayWithSize(0)));
verify(fileFilterMock2, never()).filterFiles(isA(File[].class));
compositeFileFilter.close();
}

View File

@@ -118,6 +118,7 @@ Say we have files `a.txt`, `a.done`, and `b.txt`.
The pattern filter only passes `a.txt` and `b.txt`, the "done" filter will see all three files and only pass `a.txt`.
The final result of the composite filter is only `a.txt` is released.
NOTE: With the `ChainFileListFilter`, if any filter in the chain returns an empty list, the remaining filters are not invoked.
Starting with _version 5.0_ an `ExpressionFileListFilter` has been introduced to allow to execute SpEL expression against file as a context evaluation root object.
For this purpose all the XML components for file handling (local and remote), alongside with an existing `filter` attribute, have been supplied with the `filter-expression` option: