INT-4216: Fix ClassCast in ChainFileListFilter

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

Add test for running empty array through `ChainFileListFilter`

Assure no exceptions are throw when filter input is empty.

INT-4216: Add fix for ChainFileListFilter edge case

INT-4216: Fix checkstyle error on test class

Add copyright header to test file

* Polishing for `ChainFileListFilter` JavaDocs
* Move `CompositeFileListFilterTests` and `ChainFileListFilterIntegrationTests` to the `filters` package instead of `file` core
This commit is contained in:
Aaron Grant
2017-01-25 20:50:56 -05:00
committed by Artem Bilan
parent 1674cd0f85
commit 1f9d07ae24
3 changed files with 104 additions and 6 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.file.filters;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
@@ -25,6 +26,7 @@ import org.springframework.util.Assert;
* The {@link CompositeFileListFilter} extension which chains the result
* 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
@@ -44,7 +46,7 @@ public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {
break;
}
@SuppressWarnings("unchecked")
F[] fileArray = (F[]) leftOver.toArray();
F[] fileArray = leftOver.toArray((F[]) Array.newInstance(leftOver.get(0).getClass(), leftOver.size()));
leftOver = fileFilter.filterFiles(fileArray);
}
return leftOver;

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.file.filters;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
/**
* @author Aaron Grant
*
* @since 4.3.8
*/
public class ChainFileListFilterIntegrationTests {
private File[] noFiles = new File[0];
private File[] oneFile = new File[] { new MockOldFile("file.txt") };
@Test
public void singleModifiedFilterNoFiles() throws IOException {
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
chain.addFilter(new LastModifiedFileListFilter());
List<File> result = chain.filterFiles(noFiles);
assertEquals(0, result.size());
}
}
@Test
public void singlePatternFilter() throws IOException {
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
List<File> result = chain.filterFiles(oneFile);
assertEquals(1, result.size());
}
}
@Test
public void singleModifiedFilter() throws IOException {
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
chain.addFilter(new LastModifiedFileListFilter());
List<File> result = chain.filterFiles(oneFile);
assertEquals(1, result.size());
}
}
@Test
public void patternThenModifiedFilters() throws IOException {
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
chain.addFilter(new LastModifiedFileListFilter());
List<File> result = chain.filterFiles(oneFile);
assertEquals(1, result.size());
}
}
@Test
public void modifiedThenPatternFilters() throws IOException {
try (ChainFileListFilter<File> chain = new ChainFileListFilter<>()) {
chain.addFilter(new LastModifiedFileListFilter());
chain.addFilter(new SimplePatternFileListFilter("*.txt"));
List<File> result = chain.filterFiles(oneFile);
assertEquals(1, result.size());
}
}
private static class MockOldFile extends File {
private static final long serialVersionUID = 1L;
MockOldFile(String pathname) {
super(pathname);
}
@Override
public long lastModified() {
return 1;
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.file;
package org.springframework.integration.file.filters;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.junit.Assert.assertEquals;
@@ -33,10 +33,6 @@ import java.util.List;
import org.junit.Test;
import org.springframework.integration.file.filters.ChainFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
/**
* @author Iwein Fuld
* @author Gary Russell