~ubuntu-branches/ubuntu/quantal/commons-io/quantal

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/io/filefilter/RegexFileFilterTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-02-21 13:26:43 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080221132643-p4c8f8lhb9rnqnlo
Tags: 1.4-1
* New upstream release
* Bump Standards-Version to 3.7.3
* Bump up debhelper compat to 6
* Replace XS-Vcs headers with Vcs
* debian/patches:
  - remove 01_no_ext_links.dpatch - not required
  - remove 02_no_mkdir_in_homedir.dpatch - not required
* Remove dpatch from Build-Depends
* Update debian/rules and debian/libcommons-io-java-doc.install
  with new target dirs
* debian/copyright: add copyright notice

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
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.
 
16
 */
 
17
package org.apache.commons.io.filefilter;
 
18
 
 
19
import java.io.File;
 
20
import java.util.regex.Pattern;
 
21
 
 
22
import junit.framework.TestSuite;
 
23
import junit.textui.TestRunner;
 
24
 
 
25
import org.apache.commons.io.FileUtils;
 
26
import org.apache.commons.io.IOCase;
 
27
import org.apache.commons.io.testtools.FileBasedTestCase;
 
28
 
 
29
/**
 
30
 * Used to test RegexFileFilterUtils.
 
31
 */
 
32
public class RegexFileFilterTestCase extends FileBasedTestCase {
 
33
 
 
34
    public RegexFileFilterTestCase(String name) {
 
35
        super(name);
 
36
    }
 
37
 
 
38
    public static void main(String[] args) {
 
39
        TestRunner.run(suite());
 
40
    }
 
41
 
 
42
    public static TestSuite suite() {
 
43
        return new TestSuite(RegexFileFilterTestCase.class);
 
44
    }
 
45
 
 
46
    public void setUp() {
 
47
        getTestDirectory().mkdirs();
 
48
    }
 
49
 
 
50
    public void tearDown() throws Exception {
 
51
        FileUtils.deleteDirectory(getTestDirectory());
 
52
    }
 
53
 
 
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
 
57
        assertTrue(
 
58
            "Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
 
59
            (filter.accept(file) == expected));
 
60
 
 
61
        if (file != null && file.getParentFile() != null) {
 
62
            assertTrue(
 
63
                "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
 
64
                (filter.accept(file.getParentFile(), file.getName()) == expected));
 
65
        } else if (file == null) {
 
66
            assertTrue(
 
67
                "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
 
68
                filter.accept(file) == expected);
 
69
        }
 
70
    }
 
71
 
 
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);
 
77
 
 
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);
 
82
 
 
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);
 
87
 
 
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);
 
92
 
 
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);
 
97
 
 
98
        try {
 
99
            new RegexFileFilter((String)null);
 
100
            fail();
 
101
        } catch (IllegalArgumentException ex) {
 
102
            // expected
 
103
        }
 
104
 
 
105
        try {
 
106
            new RegexFileFilter((String)null, Pattern.CASE_INSENSITIVE);
 
107
            fail();
 
108
        } catch (IllegalArgumentException ex) {
 
109
            // expected
 
110
        }
 
111
 
 
112
        try {
 
113
            new RegexFileFilter((String)null, IOCase.INSENSITIVE);
 
114
            fail();
 
115
        } catch (IllegalArgumentException ex) {
 
116
            // expected
 
117
        }
 
118
 
 
119
        try {
 
120
            new RegexFileFilter((java.util.regex.Pattern)null);
 
121
            fail();
 
122
        } catch (IllegalArgumentException ex) {
 
123
            // expected
 
124
        }
 
125
    }
 
126
         
 
127
}