diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/ChainFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/ChainFileListFilter.java index aba8233529..6fbfdc2168 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/ChainFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/ChainFileListFilter.java @@ -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 The type that will be filtered. * * @author Artem Bilan @@ -44,7 +46,7 @@ public class ChainFileListFilter extends CompositeFileListFilter { 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; diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/filters/ChainFileListFilterIntegrationTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/ChainFileListFilterIntegrationTests.java new file mode 100644 index 0000000000..0d3d6ef16f --- /dev/null +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/ChainFileListFilterIntegrationTests.java @@ -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 chain = new ChainFileListFilter<>()) { + chain.addFilter(new LastModifiedFileListFilter()); + List result = chain.filterFiles(noFiles); + assertEquals(0, result.size()); + } + } + + @Test + public void singlePatternFilter() throws IOException { + try (ChainFileListFilter chain = new ChainFileListFilter<>()) { + chain.addFilter(new SimplePatternFileListFilter("*.txt")); + List result = chain.filterFiles(oneFile); + assertEquals(1, result.size()); + } + } + + @Test + public void singleModifiedFilter() throws IOException { + try (ChainFileListFilter chain = new ChainFileListFilter<>()) { + chain.addFilter(new LastModifiedFileListFilter()); + List result = chain.filterFiles(oneFile); + assertEquals(1, result.size()); + } + } + + @Test + public void patternThenModifiedFilters() throws IOException { + try (ChainFileListFilter chain = new ChainFileListFilter<>()) { + chain.addFilter(new SimplePatternFileListFilter("*.txt")); + chain.addFilter(new LastModifiedFileListFilter()); + List result = chain.filterFiles(oneFile); + assertEquals(1, result.size()); + } + } + + @Test + public void modifiedThenPatternFilters() throws IOException { + try (ChainFileListFilter chain = new ChainFileListFilter<>()) { + chain.addFilter(new LastModifiedFileListFilter()); + chain.addFilter(new SimplePatternFileListFilter("*.txt")); + List 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; + } + + } + +} diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/CompositeFileListFilterTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/CompositeFileListFilterTests.java similarity index 94% rename from spring-integration-file/src/test/java/org/springframework/integration/file/CompositeFileListFilterTests.java rename to spring-integration-file/src/test/java/org/springframework/integration/file/filters/CompositeFileListFilterTests.java index 57a0fc645d..4e6dd01188 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/CompositeFileListFilterTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/CompositeFileListFilterTests.java @@ -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