2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.io.filefilter;
20
import java.util.regex.Pattern;
22
import junit.framework.TestSuite;
23
import junit.textui.TestRunner;
25
import org.apache.commons.io.FileUtils;
26
import org.apache.commons.io.IOCase;
27
import org.apache.commons.io.testtools.FileBasedTestCase;
30
* Used to test RegexFileFilterUtils.
32
public class RegexFileFilterTestCase extends FileBasedTestCase {
34
public RegexFileFilterTestCase(String name) {
38
public static void main(String[] args) {
39
TestRunner.run(suite());
42
public static TestSuite suite() {
43
return new TestSuite(RegexFileFilterTestCase.class);
47
getTestDirectory().mkdirs();
50
public void tearDown() throws Exception {
51
FileUtils.deleteDirectory(getTestDirectory());
54
public void assertFiltering(IOFileFilter filter, File file, boolean expected) throws Exception {
55
// Note. This only tests the (File, String) version if the parent of
56
// the File passed in is not null
58
"Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
59
(filter.accept(file) == expected));
61
if (file != null && file.getParentFile() != null) {
63
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
64
(filter.accept(file.getParentFile(), file.getName()) == expected));
65
} else if (file == null) {
67
"Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
68
filter.accept(file) == expected);
72
public void testRegex() throws Exception {
73
IOFileFilter filter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
74
assertFiltering(filter, new File("Test.java"), true);
75
assertFiltering(filter, new File("test-10.java"), true);
76
assertFiltering(filter, new File("test-.java"), false);
78
filter = new RegexFileFilter("^[Tt]est.java$");
79
assertFiltering(filter, new File("Test.java"), true);
80
assertFiltering(filter, new File("test.java"), true);
81
assertFiltering(filter, new File("tEST.java"), false);
83
filter = new RegexFileFilter(Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE));
84
assertFiltering(filter, new File("Test.java"), true);
85
assertFiltering(filter, new File("test.java"), true);
86
assertFiltering(filter, new File("tEST.java"), true);
88
filter = new RegexFileFilter("^test.java$", Pattern.CASE_INSENSITIVE);
89
assertFiltering(filter, new File("Test.java"), true);
90
assertFiltering(filter, new File("test.java"), true);
91
assertFiltering(filter, new File("tEST.java"), true);
93
filter = new RegexFileFilter("^test.java$", IOCase.INSENSITIVE);
94
assertFiltering(filter, new File("Test.java"), true);
95
assertFiltering(filter, new File("test.java"), true);
96
assertFiltering(filter, new File("tEST.java"), true);
99
new RegexFileFilter((String)null);
101
} catch (IllegalArgumentException ex) {
106
new RegexFileFilter((String)null, Pattern.CASE_INSENSITIVE);
108
} catch (IllegalArgumentException ex) {
113
new RegexFileFilter((String)null, IOCase.INSENSITIVE);
115
} catch (IllegalArgumentException ex) {
120
new RegexFileFilter((java.util.regex.Pattern)null);
122
} catch (IllegalArgumentException ex) {