KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > filefilter > FileFilterTestCase


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io.filefilter;
17
18 import java.io.File JavaDoc;
19
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import junit.textui.TestRunner;
23
24 /**
25  * Used to test an unknown FileFilter
26  */

27 public class FileFilterTestCase extends TestCase {
28
29     public FileFilterTestCase(String JavaDoc name) {
30         super(name);
31     }
32     
33     public static void main(String JavaDoc[] args) {
34         TestRunner.run(suite());
35     }
36     
37     public static TestSuite suite() {
38         return new TestSuite(FileFilterTestCase.class);
39     }
40
41     public void setUp() {
42     }
43
44     public void tearDown() {
45     }
46
47     public void assertFiltering(IOFileFilter filter, File JavaDoc file, boolean expected) throws Exception JavaDoc {
48         // Note. This only tests the (File, String) version if the parent of
49
// the File passed in is not null
50
assertTrue(
51             "Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
52             (filter.accept(file) == expected));
53
54         if (file != null && file.getParentFile() != null) {
55             assertTrue(
56                 "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
57                 (filter.accept(file.getParentFile(), file.getName()) == expected));
58         } else if (file == null) {
59             assertTrue(
60                 "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
61                 filter.accept(file) == expected);
62         }
63     }
64
65     public void testSuffix() throws Exception JavaDoc {
66         IOFileFilter filter = new SuffixFileFilter(new String JavaDoc[] { "tes", "est" });
67         assertFiltering(filter, new File JavaDoc("fred.tes"), true);
68         assertFiltering(filter, new File JavaDoc("fred.est"), true);
69         assertFiltering(filter, new File JavaDoc("fred.EST"), false); //case-sensitive
70
assertFiltering(filter, new File JavaDoc("fred.exe"), false);
71         assertFiltering(filter, new File JavaDoc("fred"), false);
72         assertFiltering(filter, new File JavaDoc(".tes"), true);
73         assertFiltering(filter, new File JavaDoc("fred.test"), true);
74         
75         filter = new SuffixFileFilter("est");
76         assertFiltering(filter, new File JavaDoc("test"), true);
77         assertFiltering(filter, new File JavaDoc("fred"), false);
78         
79         try {
80             new SuffixFileFilter((String JavaDoc) null);
81             fail();
82         } catch (IllegalArgumentException JavaDoc ex) {
83         }
84     }
85
86     public void testDirectory() throws Exception JavaDoc {
87         IOFileFilter filter = new DirectoryFileFilter();
88
89         assertFiltering(filter, new File JavaDoc("src/"), true);
90         assertFiltering(filter, new File JavaDoc("src/java/"), true);
91
92         assertFiltering(filter, new File JavaDoc("project.xml"), false);
93
94         assertFiltering(filter, new File JavaDoc("imaginary"), false);
95         assertFiltering(filter, new File JavaDoc("imaginary/"), false);
96
97         assertFiltering(filter, new File JavaDoc("STATUS.html"), false);
98     }
99
100     public void testPrefix() throws Exception JavaDoc {
101         IOFileFilter filter = new PrefixFileFilter(new String JavaDoc[] { "foo", "bar" });
102         assertFiltering(filter, new File JavaDoc("foo.test"), true);
103         assertFiltering(filter, new File JavaDoc("FOO.test"), false); //case-sensitive
104
assertFiltering(filter, new File JavaDoc("foo"), true);
105         assertFiltering(filter, new File JavaDoc("bar"), true);
106         assertFiltering(filter, new File JavaDoc("food/"), true);
107         assertFiltering(filter, new File JavaDoc("barred\\"), true);
108         assertFiltering(filter, new File JavaDoc("test"), false);
109         assertFiltering(filter, new File JavaDoc("fo_o.test"), false);
110         assertFiltering(filter, new File JavaDoc("abar.exe"), false);
111         
112         filter = new PrefixFileFilter("tes");
113         assertFiltering(filter, new File JavaDoc("test"), true);
114         assertFiltering(filter, new File JavaDoc("fred"), false);
115         
116         try {
117             new PrefixFileFilter((String JavaDoc) null);
118             fail();
119         } catch (IllegalArgumentException JavaDoc ex) {
120         }
121     }
122     
123     public void testNameFilter() throws Exception JavaDoc {
124         IOFileFilter filter = new NameFileFilter(new String JavaDoc[] { "foo", "bar" });
125         assertFiltering(filter, new File JavaDoc("foo"), true);
126         assertFiltering(filter, new File JavaDoc("bar"), true);
127         assertFiltering(filter, new File JavaDoc("fred"), false);
128
129         // repeat for a List
130
java.util.ArrayList JavaDoc list = new java.util.ArrayList JavaDoc();
131         list.add("foo");
132         list.add("bar");
133         filter = new NameFileFilter(list);
134         assertFiltering(filter, new File JavaDoc("foo"), true);
135         assertFiltering(filter, new File JavaDoc("bar"), true);
136         assertFiltering(filter, new File JavaDoc("fred"), false);
137
138         filter = new NameFileFilter("foo");
139         assertFiltering(filter, new File JavaDoc("foo"), true);
140         assertFiltering(filter, new File JavaDoc("FOO"), false); //case-sensitive
141
assertFiltering(filter, new File JavaDoc("barfoo"), false);
142         assertFiltering(filter, new File JavaDoc("foobar"), false);
143         assertFiltering(filter, new File JavaDoc("fred"), false);
144     }
145
146     public void testTrue() throws Exception JavaDoc {
147         IOFileFilter filter = FileFilterUtils.trueFileFilter();
148         assertFiltering(filter, new File JavaDoc("foo.test"), true);
149         assertFiltering(filter, new File JavaDoc("foo"), true);
150         assertFiltering(filter, null, true);
151     }
152
153     public void testFalse() throws Exception JavaDoc {
154         IOFileFilter filter = FileFilterUtils.falseFileFilter();
155         assertFiltering(filter, new File JavaDoc("foo.test"), false);
156         assertFiltering(filter, new File JavaDoc("foo"), false);
157         assertFiltering(filter, null, false);
158     }
159
160     public void testNot() throws Exception JavaDoc {
161         IOFileFilter filter = FileFilterUtils.notFileFilter(FileFilterUtils.trueFileFilter());
162         assertFiltering(filter, new File JavaDoc("foo.test"), false);
163         assertFiltering(filter, new File JavaDoc("foo"), false);
164         assertFiltering(filter, null, false);
165         try {
166             new NotFileFilter(null);
167             fail();
168         } catch (IllegalArgumentException JavaDoc ex) {
169         }
170     }
171
172     public void testAnd() throws Exception JavaDoc {
173         IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
174         IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
175         assertFiltering(new AndFileFilter(trueFilter, trueFilter), new File JavaDoc("foo.test"), true);
176         assertFiltering(new AndFileFilter(trueFilter, falseFilter), new File JavaDoc("foo.test"), false);
177         assertFiltering(new AndFileFilter(falseFilter, trueFilter), new File JavaDoc("foo.test"), false);
178         assertFiltering(new AndFileFilter(falseFilter, falseFilter), new File JavaDoc("foo.test"), false);
179         try {
180             new AndFileFilter(falseFilter, null);
181             fail();
182         } catch (IllegalArgumentException JavaDoc ex) {
183         }
184     }
185
186     public void testOr() throws Exception JavaDoc {
187         IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
188         IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
189         assertFiltering(new OrFileFilter(trueFilter, trueFilter), new File JavaDoc("foo.test"), true);
190         assertFiltering(new OrFileFilter(trueFilter, falseFilter), new File JavaDoc("foo.test"), true);
191         assertFiltering(new OrFileFilter(falseFilter, trueFilter), new File JavaDoc("foo.test"), true);
192         assertFiltering(new OrFileFilter(falseFilter, falseFilter), new File JavaDoc("foo.test"), false);
193         try {
194             new OrFileFilter(falseFilter, null);
195             fail();
196         } catch (IllegalArgumentException JavaDoc ex) {
197         }
198     }
199
200 }
201
Popular Tags