From f3d13ebf8fa8def8df4b5b6fcfd2e490311ca399 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 9 Jun 2008 18:11:00 +0000 Subject: [PATCH] Added RegexPatternFilenameFilter (INT-252). --- .../file/RegexPatternFilenameFilter.java | 48 ++++++++++ .../file/RegexPatternFilenameFilterTests.java | 87 +++++++++++++++++++ ...invalidRegexPatternFilenameFilterTests.xml | 11 +++ .../file/regexPatternFilenameFilterTests.xml | 11 +++ 4 files changed, 157 insertions(+) create mode 100644 org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java create mode 100644 org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilterTests.java create mode 100644 org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/invalidRegexPatternFilenameFilterTests.xml create mode 100644 org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/regexPatternFilenameFilterTests.xml diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java new file mode 100644 index 0000000000..48ba33dd58 --- /dev/null +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2008 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.adapter.file; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.regex.Pattern; + +import org.springframework.integration.ConfigurationException; + +/** + * A {@link FilenameFilter} implementation for matching against + * a regular expression {@link Pattern}. + * + * @author Mark Fisher + */ +public class RegexPatternFilenameFilter implements FilenameFilter { + + private volatile Pattern pattern; + + + public void setPattern(Pattern pattern) { + this.pattern = pattern; + } + + + public boolean accept(File dir, String name) { + if (this.pattern == null) { + throw new ConfigurationException("no pattern available"); + } + return this.pattern.matcher(name).matches(); + } + +} diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilterTests.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilterTests.java new file mode 100644 index 0000000000..28368f9a0f --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilterTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2002-2008 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.adapter.file; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +import org.junit.Test; + +import org.springframework.beans.TypeMismatchException; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.ConfigurationException; + +/** + * @author Mark Fisher + */ +public class RegexPatternFilenameFilterTests { + + @Test + public void testMatch() { + File file = new File("/some/path/test.txt"); + RegexPatternFilenameFilter filter = new RegexPatternFilenameFilter(); + filter.setPattern(Pattern.compile("[a-z]+\\.txt")); + assertTrue(filter.accept(file.getParentFile(), file.getName())); + } + + @Test + public void testNoMatch() { + File file = new File("/some/path/Test.txt"); + RegexPatternFilenameFilter filter = new RegexPatternFilenameFilter(); + filter.setPattern(Pattern.compile("[a-z]+\\.txt")); + assertFalse(filter.accept(file.getParentFile(), file.getName())); + } + + @Test(expected=ConfigurationException.class) + public void testPatternNotSet() { + File file = new File("/some/path/test.txt"); + RegexPatternFilenameFilter filter = new RegexPatternFilenameFilter(); + filter.accept(file.getParentFile(), file.getName()); + } + + @Test + public void testPatternEditorInContext() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "regexPatternFilenameFilterTests.xml", this.getClass()); + FilenameFilter filter = (FilenameFilter) context.getBean("filter"); + File file = new File("/some/path/foo.txt"); + assertTrue(filter.accept(file.getParentFile(), file.getName())); + } + + @Test + public void testInvalidPatternSyntax() { + try { + new ClassPathXmlApplicationContext("invalidRegexPatternFilenameFilterTests.xml", this.getClass()); + throw new IllegalStateException("context creation should have failed"); + } + catch (Exception e) { + assertEquals(BeanCreationException.class, e.getClass()); + Throwable cause1 = e.getCause(); + assertEquals(TypeMismatchException.class, cause1.getClass()); + Throwable cause2 = cause1.getCause(); + assertEquals(PatternSyntaxException.class, cause2.getClass()); + } + } + +} diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/invalidRegexPatternFilenameFilterTests.xml b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/invalidRegexPatternFilenameFilterTests.xml new file mode 100644 index 0000000000..e1d08e0313 --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/invalidRegexPatternFilenameFilterTests.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/regexPatternFilenameFilterTests.xml b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/regexPatternFilenameFilterTests.xml new file mode 100644 index 0000000000..d4025bb9b2 --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/regexPatternFilenameFilterTests.xml @@ -0,0 +1,11 @@ + + + + + + + +