INT-1541: modify default pattern style to AntPath instead of Regex
- update all patterns in test contexts to comply with ant style - change configurators to create SimplePatternFileListFilter instead of regex style PatternMatchingFileListFilter
This commit is contained in:
@@ -16,13 +16,11 @@
|
||||
package org.springframework.integration.file.config;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
import org.springframework.integration.file.entries.*;
|
||||
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
/**
|
||||
@@ -32,7 +30,7 @@ import java.util.regex.Pattern;
|
||||
public class FileListFilterFactoryBean implements FactoryBean<EntryListFilter<File>> {
|
||||
private volatile EntryListFilter<File> fileListFilter;
|
||||
private volatile EntryListFilter<File> filterReference;
|
||||
private volatile Pattern filenamePattern;
|
||||
private volatile String filenamePattern;
|
||||
private volatile Boolean preventDuplicates;
|
||||
private final Object monitor = new Object();
|
||||
private volatile Collection<EntryListFilter<File>> filterReferences;
|
||||
@@ -46,7 +44,7 @@ public class FileListFilterFactoryBean implements FactoryBean<EntryListFilter<Fi
|
||||
this.filterReference = filterReference;
|
||||
}
|
||||
|
||||
public void setFilenamePattern(Pattern filenamePattern) {
|
||||
public void setFilenamePattern(String filenamePattern) {
|
||||
this.filenamePattern = filenamePattern;
|
||||
}
|
||||
|
||||
@@ -90,7 +88,7 @@ public class FileListFilterFactoryBean implements FactoryBean<EntryListFilter<Fi
|
||||
flf = this.filterReference;
|
||||
}
|
||||
} else if (this.filenamePattern != null) {
|
||||
PatternMatchingEntryListFilter<File> patternFilter = new PatternMatchingEntryListFilter<File>(fileNamer, this.filenamePattern);
|
||||
SimplePatternFileListFilter patternFilter = new SimplePatternFileListFilter(this.filenamePattern);
|
||||
|
||||
if (Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
flf = patternFilter;
|
||||
|
||||
@@ -9,23 +9,28 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Filter that supports ant style path expressions, which are less powerful but more readable than regular expressions.
|
||||
* This filter only filters on the name of the file, the rest of the path is ignored.
|
||||
*
|
||||
* @author Iwein Fuld
|
||||
* @see org.springframework.util.AntPathMatcher
|
||||
* @see org.springframework.integration.file.filters.PatternMatchingFileListFilter
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class AntPathFileListFilter extends AbstractEntryListFilter<File> implements FileListFilter {
|
||||
public class SimplePatternFileListFilter extends AbstractEntryListFilter<File> implements FileListFilter {
|
||||
|
||||
private final AntPathMatcher matcher = new AntPathMatcher();
|
||||
private final String path;
|
||||
|
||||
public AntPathFileListFilter(String path) {
|
||||
this.path = path;
|
||||
public SimplePatternFileListFilter(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept the given file its name matches the pattern,
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(File file) {
|
||||
return matcher.match(path, file.getPath());
|
||||
return matcher.match(path, file.getName());
|
||||
}
|
||||
|
||||
public List<File> filterFiles(File[] files) {
|
||||
@@ -13,22 +13,20 @@
|
||||
<bean class="org.springframework.integration.file.entries.FileEntryNamer" id="entryNamer"/>
|
||||
|
||||
<!-- customized filter -->
|
||||
<!--
|
||||
<bean id="compositeFilter" class="org.springframework.integration.file.entries.CompositeEntryListFilter">
|
||||
<bean id="legacyCompositeFilter" class="org.springframework.integration.file.entries.CompositeEntryListFilter">
|
||||
<constructor-arg>
|
||||
<list>
|
||||
<bean class="org.springframework.integration.file.entries.AcceptOnceEntryFileListFilter"/>
|
||||
<bean class="org.springframework.integration.file.TestFileListFilter"/>
|
||||
<bean class="org.springframework.integration.file.entries.PatternMatchingEntryListFilter">
|
||||
<constructor-arg ref="entryNamer"/>
|
||||
<constructor-arg value="^test.*$"/>
|
||||
<constructor-arg value="test*"/>
|
||||
</bean>
|
||||
</list>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
-->
|
||||
|
||||
|
||||
<!-- is equivalent to previous filter -->
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" id="compositeFilter">
|
||||
<property name="filterReferences">
|
||||
|
||||
@@ -36,7 +34,7 @@
|
||||
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:preventDuplicates="true"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:preventDuplicates="false"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:filenamePattern="^test.*$"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:filenamePattern="test*"/>
|
||||
|
||||
</util:list>
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:preventDuplicates="true"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:preventDuplicates="false"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:filenamePattern="^test.*$"/>
|
||||
<bean class="org.springframework.integration.file.config.FileListFilterFactoryBean" p:filenamePattern="test*"/>
|
||||
|
||||
</util:list>
|
||||
|
||||
|
||||
@@ -18,15 +18,13 @@
|
||||
<integration:poller fixed-rate="5000"/>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<beans:bean id="filter" class="org.springframework.integration.file.config.FileListFilterFactoryBean">
|
||||
<beans:bean id="filter" class="org.springframework.integration.file.config.FileListFilterFactoryBean"/>
|
||||
|
||||
</beans:bean>
|
||||
<!--
|
||||
<beans:bean id="filter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
|
||||
<beans:bean id="compositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
|
||||
<beans:constructor-arg>
|
||||
<beans:list></beans:list>
|
||||
<beans:list/>
|
||||
</beans:constructor-arg>
|
||||
</beans:bean>-->
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ 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.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.file.DefaultDirectoryScanner;
|
||||
@@ -48,12 +47,7 @@ public class FileInboundChannelAdapterParserTests {
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
// @Qualifier("inputDirPoller")
|
||||
private FileReadingMessageSource source;
|
||||
|
||||
// @Autowired
|
||||
// @Qualifier("inputDirPollerWithChannel")
|
||||
// private FileReadingMessageSource sourceWithChannel;
|
||||
|
||||
private DirectFieldAccessor accessor;
|
||||
|
||||
@@ -62,7 +56,6 @@ public class FileInboundChannelAdapterParserTests {
|
||||
accessor = new DirectFieldAccessor(source);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void channelName() throws Exception {
|
||||
Object adapter = context.getBean("inputDirPoller");
|
||||
@@ -102,6 +95,5 @@ public class FileInboundChannelAdapterParserTests {
|
||||
public int compare(File f1, File f2) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
<inbound-channel-adapter id="adapterWithPattern"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filename-pattern=".*\.txt" auto-startup="false">
|
||||
filename-pattern="*.txt" auto-startup="false">
|
||||
<integration:poller fixed-rate="1000"/>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
|
||||
@@ -28,13 +28,12 @@ import org.springframework.integration.file.FileReadingMessageSource;
|
||||
import org.springframework.integration.file.entries.AcceptOnceEntryFileListFilter;
|
||||
import org.springframework.integration.file.entries.CompositeEntryListFilter;
|
||||
import org.springframework.integration.file.entries.EntryListFilter;
|
||||
import org.springframework.integration.file.entries.PatternMatchingEntryListFilter;
|
||||
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -120,16 +119,14 @@ public class FileInboundChannelAdapterWithPatternParserTests {
|
||||
|
||||
Set<EntryListFilter> filters = (Set<EntryListFilter>) new DirectFieldAccessor(
|
||||
scannerAccessor.getPropertyValue("filter")).getPropertyValue("fileFilters");
|
||||
Pattern pattern = null;
|
||||
String pattern = null;
|
||||
for (EntryListFilter filter : filters) {
|
||||
if (filter instanceof PatternMatchingEntryListFilter) {
|
||||
pattern = (Pattern) new DirectFieldAccessor(filter).getPropertyValue("pattern");
|
||||
if (filter instanceof SimplePatternFileListFilter) {
|
||||
pattern = (String) new DirectFieldAccessor(filter).getPropertyValue("path");
|
||||
}
|
||||
}
|
||||
assertNotNull("expected PatternMatchingFileListFilter", pattern);
|
||||
assertEquals(".*\\.txt", pattern.toString());
|
||||
assertFalse(pattern.matcher("foo").matches());
|
||||
assertTrue(pattern.matcher("foo.txt").matches());
|
||||
assertNotNull("expected SimplePatternFileListFilterTest", pattern);
|
||||
assertEquals("*.txt", pattern.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,33 +15,28 @@
|
||||
*/
|
||||
package org.springframework.integration.file.config;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
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.TestFileListFilter;
|
||||
import org.springframework.integration.file.entries.AcceptOnceEntryFileListFilter;
|
||||
import org.springframework.integration.file.entries.CompositeEntryListFilter;
|
||||
import org.springframework.integration.file.entries.EntryListFilter;
|
||||
import org.springframework.integration.file.entries.PatternMatchingEntryListFilter;
|
||||
|
||||
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@@ -88,7 +83,7 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
|
||||
Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters");
|
||||
Iterator<EntryListFilter<File>> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceEntryFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(iterator.next(), is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,14 +95,14 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
|
||||
Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters");
|
||||
Iterator<EntryListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceEntryFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(iterator.next(), is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void patternAndFalse() throws Exception {
|
||||
EntryListFilter<File> filter = this.extractFilter("patternAndFalse");
|
||||
assertFalse(filter instanceof CompositeEntryListFilter);
|
||||
assertTrue(filter instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(filter, is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,7 +147,12 @@ public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private EntryListFilter<File> extractFilter(String beanName) {
|
||||
return (EntryListFilter<File>) new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(context.getBean(beanName)).getPropertyValue("source")).getPropertyValue("scanner")).getPropertyValue(
|
||||
"filter");
|
||||
return (EntryListFilter<File>)
|
||||
new DirectFieldAccessor(
|
||||
new DirectFieldAccessor(
|
||||
new DirectFieldAccessor(context.getBean(beanName))
|
||||
.getPropertyValue("source"))
|
||||
.getPropertyValue("scanner"))
|
||||
.getPropertyValue("filter");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,16 +19,18 @@ package org.springframework.integration.file.config;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.integration.file.entries.*;
|
||||
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Iwein Fuld
|
||||
*/
|
||||
public class FileListFilterFactoryBeanTests {
|
||||
|
||||
@@ -36,7 +38,7 @@ public class FileListFilterFactoryBeanTests {
|
||||
public void customFilterAndFilenamePatternAreMutuallyExclusive() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilterReference(new TestFilter());
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setFilenamePattern("foo");
|
||||
factory.getObject();
|
||||
}
|
||||
|
||||
@@ -79,37 +81,37 @@ public class FileListFilterFactoryBeanTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void filenamePatternAndPreventDuplicatesNull() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setFilenamePattern("foo");
|
||||
EntryListFilter<File> result = factory.getObject();
|
||||
assertTrue(result instanceof CompositeEntryListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(result).getPropertyValue("fileFilters");
|
||||
Iterator<EntryListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceEntryFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(iterator.next(), is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void filenamePatternAndPreventDuplicatesTrue() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setFilenamePattern(("foo"));
|
||||
factory.setPreventDuplicates(Boolean.TRUE);
|
||||
EntryListFilter<File> result = factory.getObject();
|
||||
assertTrue(result instanceof CompositeEntryListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(result).getPropertyValue("fileFilters");
|
||||
Iterator<EntryListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceEntryFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(iterator.next(), is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filenamePatternAndPreventDuplicatesFalse() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setFilenamePattern(("foo"));
|
||||
factory.setPreventDuplicates(Boolean.FALSE);
|
||||
EntryListFilter<File> result = factory.getObject();
|
||||
assertFalse(result instanceof CompositeEntryListFilter);
|
||||
assertTrue(result instanceof PatternMatchingEntryListFilter);
|
||||
assertThat(result, is(SimplePatternFileListFilter.class));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,21 +12,21 @@ import static org.junit.Assert.assertThat;
|
||||
*
|
||||
* Minimal test set to ensure AntPathMatcher is used correctly.
|
||||
*/
|
||||
public class AntPathFileListFilterTest {
|
||||
public class SimplePatternFileListFilterTest {
|
||||
|
||||
@Test
|
||||
public void shouldMatchExactly() {
|
||||
assertThat(new AntPathFileListFilter("foo/bar").accept(new File("foo/bar")), is(true));
|
||||
assertThat(new SimplePatternFileListFilter("bar").accept(new File("bar")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMatchQuestionMark() {
|
||||
assertThat(new AntPathFileListFilter("*/bar").accept(new File("foo/bar")), is(true));
|
||||
assertThat(new SimplePatternFileListFilter("*bar").accept(new File("bar")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMatchWildcard() {
|
||||
assertThat(new AntPathFileListFilter("foo/ba?").accept(new File("foo/bar")), is(true));
|
||||
assertThat(new SimplePatternFileListFilter("ba?").accept(new File("bar")), is(true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,10 +91,10 @@
|
||||
|
||||
<file:inbound-channel-adapter id="filesIn"
|
||||
directory="file:${input.directory}"
|
||||
filename-pattern="^test.*$" /> ]]></programlisting>
|
||||
filename-pattern="test*" /> ]]></programlisting>
|
||||
The first channel adapter is relying on the default filter that just prevents
|
||||
duplication, the second is using a custom filter, and the third is using the
|
||||
<emphasis>filename-pattern</emphasis> attribute to add a <classname>Pattern</classname>
|
||||
<emphasis>filename-pattern</emphasis> attribute to add a <classname>AntPathMatcher</classname>
|
||||
based filter to the <classname>FileReadingMessageSource</classname>.
|
||||
The <emphasis>file-name-pattern</emphasis> and <emphasis>filter</emphasis> attributes are mutually exclusive, but
|
||||
you can use a <classname>CompositeFileListFilter</classname> to use any combination of filters, including a
|
||||
|
||||
Reference in New Issue
Block a user