From cad80a15b06f13b7d9e8db4b213c942b87b02fb8 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 30 Jun 2009 16:56:26 +0000 Subject: [PATCH] INT-664 Added a 'prevent-duplicates' flag to the file namespace's inbound-channel-adapter element. --- .../FileInboundChannelAdapterParser.java | 38 ++--- .../config/spring-integration-file-1.0.xsd | 12 ++ ...WithPreventDuplicatesFlagTests-context.xml | 114 +++++++++++++ ...AdapterWithPreventDuplicatesFlagTests.java | 155 ++++++++++++++++++ 4 files changed, 297 insertions(+), 22 deletions(-) create mode 100644 org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests-context.xml create mode 100644 org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests.java diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java index 7782c969a8..dbf63f4b62 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java @@ -19,10 +19,8 @@ package org.springframework.integration.file.config; import org.w3c.dom.Element; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; -import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser; import org.springframework.util.StringUtils; @@ -39,7 +37,6 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann @Override - @SuppressWarnings("unchecked") protected String parseSource(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( PACKAGE_NAME + ".FileReadingMessageSource"); @@ -54,9 +51,18 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann } builder.addPropertyValue("inputDirectory", directory); } + String filterBeanName = this.registerFileListFilter(element, parserContext); + builder.addPropertyReference("filter", filterBeanName); + return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry()); + } + + private String registerFileListFilter(Element element, ParserContext parserContext) { + BeanDefinitionBuilder factoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition( + PACKAGE_NAME + ".config.FileListFilterFactoryBean"); + factoryBeanBuilder.setRole(BeanDefinition.ROLE_SUPPORT); String filter = element.getAttribute("filter"); - if (StringUtils.hasText(filter)){ - builder.addPropertyReference("filter", filter); + if (StringUtils.hasText(filter)) { + factoryBeanBuilder.addPropertyReference("filterReference", filter); } String filenamePattern = element.getAttribute("filename-pattern"); if (StringUtils.hasText(filenamePattern)) { @@ -64,26 +70,14 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann parserContext.getReaderContext().error( "At most one of 'filter' and 'filename-pattern' may be provided.", element); } - String acceptOnceFilterBeanName = this.parseFilter("AcceptOnceFileListFilter", null, parserContext); - String patternFilterBeanName = this.parseFilter("PatternMatchingFileListFilter", filenamePattern, parserContext); - ManagedList filters = new ManagedList(); - filters.add(new RuntimeBeanReference(acceptOnceFilterBeanName)); - filters.add(new RuntimeBeanReference(patternFilterBeanName)); - String compositeFilterBeanName = this.parseFilter("CompositeFileListFilter", filters, parserContext); - builder.addPropertyReference("filter", compositeFilterBeanName); + factoryBeanBuilder.addPropertyValue("filenamePattern", filenamePattern); } - return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry()); - } - - private String parseFilter(String shortClassName, Object constructorArgValue, ParserContext parserContext) { - BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.genericBeanDefinition( - PACKAGE_NAME + "." + shortClassName); - filterBuilder.getBeanDefinition().setRole(BeanDefinition.ROLE_SUPPORT); - if (constructorArgValue != null) { - filterBuilder.addConstructorArgValue(constructorArgValue); + String preventDuplicates = element.getAttribute("prevent-duplicates"); + if (StringUtils.hasText(preventDuplicates)) { + factoryBeanBuilder.addPropertyValue("preventDuplicates", preventDuplicates); } return BeanDefinitionReaderUtils.registerWithGeneratedName( - filterBuilder.getBeanDefinition(), parserContext.getRegistry()); + factoryBeanBuilder.getBeanDefinition(), parserContext.getRegistry()); } } diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd index 0df1483d47..8a2b5b3452 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd @@ -58,6 +58,18 @@ + + + + + diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests-context.xml b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests-context.xml new file mode 100644 index 0000000000..62639337bc --- /dev/null +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests-context.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests.java b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests.java new file mode 100644 index 0000000000..cf4f0c621a --- /dev/null +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterWithPreventDuplicatesFlagTests.java @@ -0,0 +1,155 @@ +/* + * Copyright 2002-2009 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.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.ApplicationContext; +import org.springframework.integration.file.AcceptOnceFileListFilter; +import org.springframework.integration.file.CompositeFileListFilter; +import org.springframework.integration.file.FileListFilter; +import org.springframework.integration.file.PatternMatchingFileListFilter; +import org.springframework.integration.file.TestFileListFilter; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests { + + @Autowired + private ApplicationContext context; + + @Autowired + @Qualifier("testFilter") + private TestFileListFilter testFilter; + + + @Test + public void filterAndNull() { + FileListFilter filter = this.extractFilter("filterAndNull"); + assertFalse(filter instanceof CompositeFileListFilter); + assertSame(testFilter, filter); + } + + @Test + @SuppressWarnings("unchecked") + public void filterAndTrue() { + FileListFilter filter = this.extractFilter("filterAndTrue"); + assertTrue(filter instanceof CompositeFileListFilter); + Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters"); + assertTrue(filters.iterator().next() instanceof AcceptOnceFileListFilter); + assertTrue(filters.contains(testFilter)); + } + + @Test + public void filterAndFalse() throws Exception { + FileListFilter filter = this.extractFilter("filterAndFalse"); + assertFalse(filter instanceof CompositeFileListFilter); + assertSame(testFilter, filter); + } + + @Test + @SuppressWarnings("unchecked") + public void patternAndNull() throws Exception { + FileListFilter filter = this.extractFilter("patternAndNull"); + assertTrue(filter instanceof CompositeFileListFilter); + Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters"); + Iterator iterator = filters.iterator(); + assertTrue(iterator.next() instanceof AcceptOnceFileListFilter); + assertTrue(iterator.next() instanceof PatternMatchingFileListFilter); + } + + @Test + @SuppressWarnings("unchecked") + public void patternAndTrue() throws Exception { + FileListFilter filter = this.extractFilter("patternAndTrue"); + assertTrue(filter instanceof CompositeFileListFilter); + Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters"); + Iterator iterator = filters.iterator(); + assertTrue(iterator.next() instanceof AcceptOnceFileListFilter); + assertTrue(iterator.next() instanceof PatternMatchingFileListFilter); + } + + @Test + public void patternAndFalse() throws Exception { + FileListFilter filter = this.extractFilter("patternAndFalse"); + assertFalse(filter instanceof CompositeFileListFilter); + assertTrue(filter instanceof PatternMatchingFileListFilter); + } + + @Test + public void defaultAndNull() throws Exception { + FileListFilter 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 result = filter.filterFiles(files); + assertEquals(1, result.size()); + } + + @Test + public void defaultAndTrue() throws Exception { + FileListFilter filter = this.extractFilter("defaultAndTrue"); + assertFalse(filter instanceof CompositeFileListFilter); + assertTrue(filter instanceof AcceptOnceFileListFilter); + File testFile = new File("test"); + File[] files = new File[] { testFile, testFile, testFile }; + List result = filter.filterFiles(files); + assertEquals(1, result.size()); + } + + @Test + public void defaultAndFalse() throws Exception { + FileListFilter 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 result = filter.filterFiles(files); + assertEquals(3, result.size()); + } + + + private FileListFilter extractFilter(String beanName) { + return (FileListFilter) new DirectFieldAccessor( + new DirectFieldAccessor(context.getBean(beanName)).getPropertyValue("source")) + .getPropertyValue("filter"); + } + +}