View Javadoc
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.io.FileFilter;
21  import java.io.FilenameFilter;
22  import java.io.OutputStream;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.Date;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  import org.apache.commons.io.Charsets;
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.io.IOCase;
34  import org.apache.commons.io.IOUtils;
35  import org.apache.commons.io.testtools.FileBasedTestCase;
36  
37  /**
38   * Used to test FileFilterUtils.
39   */
40  public class FileFilterTestCase extends FileBasedTestCase {
41  
42      /**
43       * The subversion directory name.
44       */
45      static final String SVN_DIR_NAME = ".svn";
46  
47      private static final boolean WINDOWS = File.separatorChar == '\\';
48  
49      public FileFilterTestCase(final String name) {
50          super(name);
51      }
52  
53      @Override
54      public void setUp() {
55          getTestDirectory().mkdirs();
56      }
57  
58      @Override
59      public void tearDown() throws Exception {
60          FileUtils.deleteDirectory(getTestDirectory());
61      }
62  
63      public void assertFiltering(final IOFileFilter filter, final File file, final boolean expected) throws Exception {
64          // Note. This only tests the (File, String) version if the parent of
65          //       the File passed in is not null
66          assertEquals(
67              "Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file,
68              expected, filter.accept(file));
69  
70          if (file != null && file.getParentFile() != null) {
71              assertEquals(
72                  "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file,
73                  expected, filter.accept(file.getParentFile(), file.getName()));
74          } else if (file == null) {
75              assertEquals(
76                  "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null",
77                  expected, filter.accept(file));
78          }
79          assertNotNull(filter.toString());
80      }
81  
82      public void testSuffix() throws Exception {
83          IOFileFilter filter = new SuffixFileFilter(new String[] { "tes", "est" });
84          final File testFile = new File( "test" );
85          final File fredFile = new File( "fred" );
86          assertFiltering(filter, new File("fred.tes"), true);
87          assertFiltering(filter, new File("fred.est"), true);
88          assertFiltering(filter, new File("fred.EST"), false); //case-sensitive
89          assertFiltering(filter, new File("fred.exe"), false);
90  
91          filter = FileFilterUtils.or(
92                      FileFilterUtils.suffixFileFilter( "tes" ),
93                      FileFilterUtils.suffixFileFilter( "est" ) );
94          assertFiltering(filter, new File("fred"), false);
95          assertFiltering(filter, new File(".tes"), true);
96          assertFiltering(filter, new File("fred.test"), true);
97  
98          filter = new SuffixFileFilter("est");
99          assertFiltering(filter, new File("test"), true);
100         assertFiltering(filter, new File("fred"), false);
101 
102         assertTrue( filter.accept( testFile.getParentFile(), testFile.getName() ) );
103         assertTrue( !filter.accept( fredFile.getParentFile(), fredFile.getName() ) );
104 
105         final List<String> prefixes = Arrays.asList( new String[] { "ood", "red" } );
106         final IOFileFilter listFilter = new SuffixFileFilter( prefixes );
107 
108         assertTrue( !listFilter.accept( testFile.getParentFile(), testFile.getName() ) );
109         assertTrue( listFilter.accept( fredFile.getParentFile(), fredFile.getName() ) );
110 
111         try {
112             new SuffixFileFilter((String) null);
113             fail();
114         } catch (final IllegalArgumentException ex) {
115         }
116 
117         try {
118             new SuffixFileFilter((String[]) null);
119             fail();
120         } catch (final IllegalArgumentException ex) {
121         }
122 
123         try {
124             new SuffixFileFilter((List<String>) null);
125             fail();
126         } catch (final IllegalArgumentException ex) {
127         }
128 }
129 
130     public void testSuffixCaseInsensitive() throws Exception {
131 
132         IOFileFilter filter = new SuffixFileFilter(new String[] { "tes", "est" }, IOCase.INSENSITIVE);
133         assertFiltering(filter, new File("foo.tes"), true);
134         assertFiltering(filter, new File("foo.est"), true);
135         assertFiltering(filter, new File("foo.EST"), true); //case-sensitive
136         assertFiltering(filter, new File("foo.TES"), true); //case-sensitive
137         assertFiltering(filter, new File("foo.exe"), false);
138 
139         filter = new SuffixFileFilter("est", IOCase.INSENSITIVE);
140         assertFiltering(filter, new File("test"), true);
141         assertFiltering(filter, new File("TEST"), true);
142 
143         final List<String> suffixes = Arrays.asList( new String[] { "tes", "est" } );
144         filter = new SuffixFileFilter(suffixes, IOCase.INSENSITIVE);
145         assertFiltering(filter, new File("bar.tes"), true);
146         assertFiltering(filter, new File("bar.est"), true);
147         assertFiltering(filter, new File("bar.EST"), true); //case-sensitive
148         assertFiltering(filter, new File("bar.TES"), true); //case-sensitive
149         assertFiltering(filter, new File("bar.exe"), false);
150 
151         try {
152             new SuffixFileFilter((String) null, IOCase.INSENSITIVE);
153             fail();
154         } catch (final IllegalArgumentException ex) {
155         }
156 
157         try {
158             new SuffixFileFilter((String[]) null, IOCase.INSENSITIVE);
159             fail();
160         } catch (final IllegalArgumentException ex) {
161         }
162 
163         try {
164             new SuffixFileFilter((List<String>) null, IOCase.INSENSITIVE);
165             fail();
166         } catch (final IllegalArgumentException ex) {
167         }
168 
169         // FileFilterUtils.suffixFileFilter(String, IOCase) tests
170         filter = FileFilterUtils.suffixFileFilter("est", IOCase.INSENSITIVE);
171         assertFiltering(filter, new File("test"), true);
172         assertFiltering(filter, new File("TEST"), true);
173 
174         try {
175             FileFilterUtils.suffixFileFilter((String) null, IOCase.INSENSITIVE);
176             fail();
177         } catch (final IllegalArgumentException ex) {
178         }
179     }
180 
181     public void testDirectory() throws Exception {
182         // XXX: This test presumes the current working dir is the base dir of the source checkout.
183         final IOFileFilter filter = new DirectoryFileFilter();
184 
185         assertFiltering(filter, new File("src/"), true);
186         assertFiltering(filter, new File("src/main/java/"), true);
187 
188         assertFiltering(filter, new File("pom.xml"), false);
189 
190         assertFiltering(filter, new File("imaginary"), false);
191         assertFiltering(filter, new File("imaginary/"), false);
192 
193         assertFiltering(filter, new File("LICENSE.txt"), false);
194 
195         assertSame(DirectoryFileFilter.DIRECTORY, DirectoryFileFilter.INSTANCE);
196     }
197 
198     public void testFiles() throws Exception {
199         // XXX: This test presumes the current working dir is the base dir of the source checkout.
200         final IOFileFilter filter = FileFileFilter.FILE;
201 
202         assertFiltering(filter, new File("src/"), false);
203         assertFiltering(filter, new File("src/java/"), false);
204 
205         assertFiltering(filter, new File("pom.xml"), true);
206 
207         assertFiltering(filter, new File("imaginary"), false);
208         assertFiltering(filter, new File("imaginary/"), false);
209 
210         assertFiltering(filter, new File("LICENSE.txt"), true);
211     }
212 
213     public void testPrefix() throws Exception {
214         IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" });
215         final File testFile = new File( "test" );
216         final File fredFile = new File( "fred" );
217 
218         assertFiltering(filter, new File("foo.test"), true);
219         assertFiltering(filter, new File("FOO.test"), false);  //case-sensitive
220         assertFiltering(filter, new File("foo"), true);
221         assertFiltering(filter, new File("bar"), true);
222         assertFiltering(filter, new File("food/"), true);
223 
224         filter = FileFilterUtils.prefixFileFilter( "bar" );
225         assertFiltering(filter, new File("barred\\"), true);
226         assertFiltering(filter, new File("test"), false);
227         assertFiltering(filter, new File("fo_o.test"), false);
228         assertFiltering(filter, new File("abar.exe"), false);
229 
230         filter = new PrefixFileFilter("tes");
231         assertFiltering(filter, new File("test"), true);
232         assertFiltering(filter, new File("fred"), false);
233 
234         assertTrue( filter.accept( testFile.getParentFile(), testFile.getName() ) );
235         assertTrue( !filter.accept( fredFile.getParentFile(), fredFile.getName() ) );
236 
237         final List<String> prefixes = Arrays.asList( new String[] { "foo", "fre" } );
238         final IOFileFilter listFilter = new PrefixFileFilter( prefixes );
239 
240         assertTrue( !listFilter.accept( testFile.getParentFile(), testFile.getName() ) );
241         assertTrue( listFilter.accept( fredFile.getParentFile(), fredFile.getName() ) );
242 
243 
244         try {
245             new PrefixFileFilter((String) null);
246             fail();
247         } catch (final IllegalArgumentException ex) {
248         }
249 
250         try {
251             new PrefixFileFilter((String[]) null);
252             fail();
253         } catch (final IllegalArgumentException ex) {
254         }
255 
256         try {
257             new PrefixFileFilter((List<String>) null);
258             fail();
259         } catch (final IllegalArgumentException ex) {
260         }
261     }
262 
263     public void testPrefixCaseInsensitive() throws Exception {
264 
265         IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" }, IOCase.INSENSITIVE);
266         assertFiltering(filter, new File("foo.test1"), true);
267         assertFiltering(filter, new File("bar.test1"), true);
268         assertFiltering(filter, new File("FOO.test1"), true);  //case-sensitive
269         assertFiltering(filter, new File("BAR.test1"), true);  //case-sensitive
270 
271         filter = new PrefixFileFilter("bar", IOCase.INSENSITIVE);
272         assertFiltering(filter, new File("foo.test2"), false);
273         assertFiltering(filter, new File("bar.test2"), true);
274         assertFiltering(filter, new File("FOO.test2"), false); //case-sensitive
275         assertFiltering(filter, new File("BAR.test2"), true);  //case-sensitive
276 
277         final List<String> prefixes = Arrays.asList( new String[] { "foo", "bar" } );
278         filter = new PrefixFileFilter(prefixes, IOCase.INSENSITIVE);
279         assertFiltering(filter, new File("foo.test3"), true);
280         assertFiltering(filter, new File("bar.test3"), true);
281         assertFiltering(filter, new File("FOO.test3"), true);  //case-sensitive
282         assertFiltering(filter, new File("BAR.test3"), true);  //case-sensitive
283 
284         try {
285             new PrefixFileFilter((String) null, IOCase.INSENSITIVE);
286             fail();
287         } catch (final IllegalArgumentException ex) {
288         }
289 
290         try {
291             new PrefixFileFilter((String[]) null, IOCase.INSENSITIVE);
292             fail();
293         } catch (final IllegalArgumentException ex) {
294         }
295 
296         try {
297             new PrefixFileFilter((List<String>) null, IOCase.INSENSITIVE);
298             fail();
299         } catch (final IllegalArgumentException ex) {
300         }
301 
302         // FileFilterUtils.prefixFileFilter(String, IOCase) tests
303         filter = FileFilterUtils.prefixFileFilter("bar", IOCase.INSENSITIVE);
304         assertFiltering(filter, new File("foo.test2"), false);
305         assertFiltering(filter, new File("bar.test2"), true);
306         assertFiltering(filter, new File("FOO.test2"), false); //case-sensitive
307         assertFiltering(filter, new File("BAR.test2"), true);  //case-sensitive
308 
309         try {
310             FileFilterUtils.prefixFileFilter((String) null, IOCase.INSENSITIVE);
311             fail();
312         } catch (final IllegalArgumentException ex) {
313         }
314     }
315 
316     public void testNameFilter() throws Exception {
317         IOFileFilter filter = new NameFileFilter(new String[] { "foo", "bar" });
318         assertFiltering(filter, new File("foo"), true);
319         assertFiltering(filter, new File("bar"), true);
320         assertFiltering(filter, new File("fred"), false);
321 
322         filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.SENSITIVE);
323         assertFiltering(filter, new File("foo"), true);
324         assertFiltering(filter, new File("bar"), true);
325         assertFiltering(filter, new File("FOO"), false);
326         assertFiltering(filter, new File("BAR"), false);
327 
328         filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.INSENSITIVE);
329         assertFiltering(filter, new File("foo"), true);
330         assertFiltering(filter, new File("bar"), true);
331         assertFiltering(filter, new File("FOO"), true);
332         assertFiltering(filter, new File("BAR"), true);
333 
334         filter = new NameFileFilter(new String[] { "foo", "bar" }, IOCase.SYSTEM);
335         assertFiltering(filter, new File("foo"), true);
336         assertFiltering(filter, new File("bar"), true);
337         assertFiltering(filter, new File("FOO"), WINDOWS);
338         assertFiltering(filter, new File("BAR"), WINDOWS);
339 
340         filter = new NameFileFilter(new String[] { "foo", "bar" }, (IOCase) null);
341         assertFiltering(filter, new File("foo"), true);
342         assertFiltering(filter, new File("bar"), true);
343         assertFiltering(filter, new File("FOO"), false);
344         assertFiltering(filter, new File("BAR"), false);
345 
346         // repeat for a List
347         final java.util.ArrayList<String> list = new java.util.ArrayList<String>();
348         list.add("foo");
349         list.add("bar");
350         filter = new NameFileFilter(list);
351         assertFiltering(filter, new File("foo"), true);
352         assertFiltering(filter, new File("bar"), true);
353         assertFiltering(filter, new File("fred"), false);
354 
355         filter = new NameFileFilter("foo");
356         assertFiltering(filter, new File("foo"), true);
357         assertFiltering(filter, new File("FOO"), false); //case-sensitive
358         assertFiltering(filter, new File("barfoo"), false);
359         assertFiltering(filter, new File("foobar"), false);
360         assertFiltering(filter, new File("fred"), false);
361 
362         // FileFilterUtils.nameFileFilter(String, IOCase) tests
363         filter = FileFilterUtils.nameFileFilter("foo", IOCase.INSENSITIVE);
364         assertFiltering(filter, new File("foo"), true);
365         assertFiltering(filter, new File("FOO"), true); //case-insensitive
366         assertFiltering(filter, new File("barfoo"), false);
367         assertFiltering(filter, new File("foobar"), false);
368         assertFiltering(filter, new File("fred"), false);
369     }
370 
371     public void testNameFilterNullArgument() throws Exception {
372         final String test = null;
373         try {
374             new NameFileFilter(test);
375             fail( "constructing a NameFileFilter with a null String argument should fail.");
376         } catch( final IllegalArgumentException iae ) {
377         }
378 
379         try {
380             FileFilterUtils.nameFileFilter(test, IOCase.INSENSITIVE);
381             fail( "constructing a NameFileFilter with a null String argument should fail.");
382         } catch( final IllegalArgumentException iae ) {
383         }
384     }
385 
386     public void testNameFilterNullArrayArgument() throws Exception {
387         final String[] test = null;
388         try {
389             new NameFileFilter(test);
390             fail( "constructing a NameFileFilter with a null String[] argument should fail.");
391         } catch( final IllegalArgumentException iae ) {
392         }
393     }
394 
395     public void testNameFilterNullListArgument() throws Exception {
396         final List<String> test = null;
397         try {
398             new NameFileFilter(test);
399             fail( "constructing a NameFileFilter with a null List argument should fail.");
400         } catch( final IllegalArgumentException iae ) {
401         }
402     }
403 
404     public void testTrue() throws Exception {
405         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
406         assertFiltering(filter, new File("foo.test"), true);
407         assertFiltering(filter, new File("foo"), true);
408         assertFiltering(filter, null, true);
409         assertSame(TrueFileFilter.TRUE, TrueFileFilter.INSTANCE);
410     }
411 
412     public void testFalse() throws Exception {
413         final IOFileFilter filter = FileFilterUtils.falseFileFilter();
414         assertFiltering(filter, new File("foo.test"), false);
415         assertFiltering(filter, new File("foo"), false);
416         assertFiltering(filter, null, false);
417         assertSame(FalseFileFilter.FALSE, FalseFileFilter.INSTANCE);
418     }
419 
420     public void testNot() throws Exception {
421         final IOFileFilter filter = FileFilterUtils.notFileFilter(FileFilterUtils.trueFileFilter());
422         assertFiltering(filter, new File("foo.test"), false);
423         assertFiltering(filter, new File("foo"), false);
424         assertFiltering(filter, null, false);
425         try {
426             new NotFileFilter(null);
427             fail();
428         } catch (final IllegalArgumentException ex) {
429         }
430     }
431 
432     public void testAnd() throws Exception {
433         final IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
434         final IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
435         assertFiltering(new AndFileFilter(trueFilter, trueFilter), new File("foo.test"), true);
436         assertFiltering(new AndFileFilter(trueFilter, falseFilter), new File("foo.test"), false);
437         assertFiltering(new AndFileFilter(falseFilter, trueFilter), new File("foo.test"), false);
438         assertFiltering(new AndFileFilter(falseFilter, falseFilter), new File("foo.test"), false);
439 
440         final List<IOFileFilter> filters = new ArrayList<IOFileFilter>();
441         assertFiltering( new AndFileFilter( filters ), new File( "test" ), false );
442         assertFiltering( new AndFileFilter(), new File( "test" ), false );
443 
444         try {
445             new AndFileFilter(falseFilter, null);
446             fail();
447         } catch (final IllegalArgumentException ex) {
448         }
449 
450         try {
451             new AndFileFilter(null, falseFilter);
452             fail();
453         } catch (final IllegalArgumentException ex) {
454         }
455 
456         final AndFileFilter f = new AndFileFilter((List<IOFileFilter>) null);
457         assertTrue(f.getFileFilters().isEmpty());
458 
459         assertNotNull(f.toString()); // TODO better tests
460     }
461 
462     public void testOr() throws Exception {
463         final IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
464         final IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
465         final File testFile = new File( "foo.test" );
466         assertFiltering(new OrFileFilter(trueFilter, trueFilter), testFile, true);
467         assertFiltering(new OrFileFilter(trueFilter, falseFilter), testFile, true);
468         assertFiltering(new OrFileFilter(falseFilter, trueFilter), testFile, true);
469         assertFiltering(new OrFileFilter(falseFilter, falseFilter), testFile, false);
470         assertFiltering(new OrFileFilter(), testFile, false);
471 
472         final List<IOFileFilter> filters = new ArrayList<IOFileFilter>();
473         filters.add( trueFilter );
474         filters.add( falseFilter );
475 
476         final OrFileFilter orFilter = new OrFileFilter( filters );
477 
478         assertFiltering(orFilter, testFile, true);
479         assertEquals( orFilter.getFileFilters(), filters );
480         orFilter.removeFileFilter( trueFilter );
481         assertFiltering(orFilter, testFile, false);
482         orFilter.setFileFilters( filters );
483         assertFiltering(orFilter, testFile, true);
484 
485         assertTrue( orFilter.accept( testFile.getParentFile(), testFile.getName() ) );
486         orFilter.removeFileFilter( trueFilter );
487         assertTrue( !orFilter.accept( testFile.getParentFile(), testFile.getName() ) );
488 
489         try {
490             new OrFileFilter(falseFilter, null);
491             fail();
492         } catch (final IllegalArgumentException ex) {
493         }
494 
495         final OrFileFilter f = new OrFileFilter((List<IOFileFilter>) null);
496         assertTrue(f.getFileFilters().isEmpty());
497     }
498     public void testFileFilterUtils_and() throws Exception {
499         final IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
500         final IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
501         assertFiltering(FileFilterUtils.and(trueFilter, trueFilter, trueFilter), new File("foo.test"), true);
502         assertFiltering(FileFilterUtils.and(trueFilter, falseFilter, trueFilter), new File("foo.test"), false);
503         assertFiltering(FileFilterUtils.and(falseFilter, trueFilter), new File("foo.test"), false);
504         assertFiltering(FileFilterUtils.and(falseFilter, falseFilter), new File("foo.test"), false);
505     }
506 
507     public void testFileFilterUtils_or() throws Exception {
508         final IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
509         final IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
510         final File testFile = new File( "foo.test" );
511         assertFiltering(FileFilterUtils.or(trueFilter, trueFilter), testFile, true);
512         assertFiltering(FileFilterUtils.or(trueFilter, trueFilter, falseFilter), testFile, true);
513         assertFiltering(FileFilterUtils.or(falseFilter, trueFilter), testFile, true);
514         assertFiltering(FileFilterUtils.or(falseFilter, falseFilter, falseFilter), testFile, false);
515     }
516 
517     @SuppressWarnings("deprecation")
518     public void testDeprecatedWildcard() throws Exception {
519         IOFileFilter filter = new WildcardFilter("*.txt");
520         final List<String> patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );
521         final IOFileFilter listFilter = new WildcardFilter( patternList );
522         final File txtFile = new File( "test.txt" );
523         final File bmpFile = new File( "test.bmp" );
524         final File dir = new File( "src/java" );
525 
526         assertFiltering(filter, new File("log.txt"), true);
527 //        assertFiltering(filter, new File("log.txt.bak"), false);
528 
529         filter = new WildcardFilter("log?.txt");
530         assertFiltering(filter, new File("log1.txt"), true);
531         assertFiltering(filter, new File("log12.txt"), false);
532 
533         filter = new WildcardFilter("open??.????04");
534         assertFiltering(filter, new File("openAB.102504"), true);
535         assertFiltering(filter, new File("openA.102504"), false);
536         assertFiltering(filter, new File("openXY.123103"), false);
537 //        assertFiltering(filter, new File("openAB.102504.old"), false);
538 
539         filter = new WildcardFilter(new String[] {"*.java", "*.class"});
540         assertFiltering(filter, new File("Test.java"), true);
541         assertFiltering(filter, new File("Test.class"), true);
542         assertFiltering(filter, new File("Test.jsp"), false);
543 
544         assertFiltering(listFilter, new File("Test.txt"), true);
545         assertFiltering(listFilter, new File("Test.xml"), true);
546         assertFiltering(listFilter, new File("Test.gif"), true);
547         assertFiltering(listFilter, new File("Test.bmp"), false);
548 
549         assertTrue( listFilter.accept( txtFile ) );
550         assertTrue( !listFilter.accept( bmpFile ) );
551         assertTrue( !listFilter.accept( dir ) );
552 
553         assertTrue( listFilter.accept( txtFile.getParentFile(), txtFile.getName() ) );
554         assertTrue( !listFilter.accept( bmpFile.getParentFile(), bmpFile.getName() ) );
555         assertTrue( !listFilter.accept( dir.getParentFile(), dir.getName() ) );
556 
557         try {
558             new WildcardFilter((String) null);
559             fail();
560         } catch (final IllegalArgumentException ex) {
561             // expected
562         }
563 
564         try {
565             new WildcardFilter((String[]) null);
566             fail();
567         } catch (final IllegalArgumentException ex) {
568             // expected
569         }
570 
571         try {
572             new WildcardFilter((List<String>) null);
573             fail();
574         } catch (final IllegalArgumentException ex) {
575             // expected
576         }
577     }
578 
579     public void testWildcard() throws Exception {
580         IOFileFilter filter = new WildcardFileFilter("*.txt");
581         assertFiltering(filter, new File("log.txt"), true);
582         assertFiltering(filter, new File("log.TXT"), false);
583 
584         filter = new WildcardFileFilter("*.txt", IOCase.SENSITIVE);
585         assertFiltering(filter, new File("log.txt"), true);
586         assertFiltering(filter, new File("log.TXT"), false);
587 
588         filter = new WildcardFileFilter("*.txt", IOCase.INSENSITIVE);
589         assertFiltering(filter, new File("log.txt"), true);
590         assertFiltering(filter, new File("log.TXT"), true);
591 
592         filter = new WildcardFileFilter("*.txt", IOCase.SYSTEM);
593         assertFiltering(filter, new File("log.txt"), true);
594         assertFiltering(filter, new File("log.TXT"), WINDOWS);
595 
596         filter = new WildcardFileFilter("*.txt", (IOCase) null);
597         assertFiltering(filter, new File("log.txt"), true);
598         assertFiltering(filter, new File("log.TXT"), false);
599 
600         filter = new WildcardFileFilter(new String[] {"*.java", "*.class"});
601         assertFiltering(filter, new File("Test.java"), true);
602         assertFiltering(filter, new File("Test.class"), true);
603         assertFiltering(filter, new File("Test.jsp"), false);
604 
605         filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.SENSITIVE);
606         assertFiltering(filter, new File("Test.java"), true);
607         assertFiltering(filter, new File("Test.JAVA"), false);
608 
609         filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.INSENSITIVE);
610         assertFiltering(filter, new File("Test.java"), true);
611         assertFiltering(filter, new File("Test.JAVA"), true);
612 
613         filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, IOCase.SYSTEM);
614         assertFiltering(filter, new File("Test.java"), true);
615         assertFiltering(filter, new File("Test.JAVA"), WINDOWS);
616 
617         filter = new WildcardFileFilter(new String[] {"*.java", "*.class"}, (IOCase) null);
618         assertFiltering(filter, new File("Test.java"), true);
619         assertFiltering(filter, new File("Test.JAVA"), false);
620 
621         final List<String> patternList = Arrays.asList( new String[] { "*.txt", "*.xml", "*.gif" } );
622         final IOFileFilter listFilter = new WildcardFileFilter( patternList );
623         assertFiltering(listFilter, new File("Test.txt"), true);
624         assertFiltering(listFilter, new File("Test.xml"), true);
625         assertFiltering(listFilter, new File("Test.gif"), true);
626         assertFiltering(listFilter, new File("Test.bmp"), false);
627 
628         final File txtFile = new File( "test.txt" );
629         final File bmpFile = new File( "test.bmp" );
630         final File dir = new File( "src/java" );
631         assertTrue( listFilter.accept( txtFile ) );
632         assertTrue( !listFilter.accept( bmpFile ) );
633         assertTrue( !listFilter.accept( dir ) );
634 
635         assertTrue( listFilter.accept( txtFile.getParentFile(), txtFile.getName() ) );
636         assertTrue( !listFilter.accept( bmpFile.getParentFile(), bmpFile.getName() ) );
637         assertTrue( !listFilter.accept( dir.getParentFile(), dir.getName() ) );
638 
639         try {
640             new WildcardFileFilter((String) null);
641             fail();
642         } catch (final IllegalArgumentException ex) {}
643         try {
644             new WildcardFileFilter((String[]) null);
645             fail();
646         } catch (final IllegalArgumentException ex) {}
647         try {
648             new WildcardFileFilter((List<String>) null);
649             fail();
650         } catch (final IllegalArgumentException ex) {}
651     }
652 
653     public void testDelegateFileFilter() throws Exception {
654         final OrFileFilter orFilter = new OrFileFilter();
655         final File testFile = new File( "test.txt" );
656 
657         IOFileFilter filter = new DelegateFileFilter((FileFilter) orFilter);
658         assertFiltering( filter, testFile, false );
659         assertNotNull(filter.toString()); // TODO better test
660 
661         filter = new DelegateFileFilter((FilenameFilter) orFilter);
662         assertFiltering( filter, testFile, false );
663         assertNotNull(filter.toString()); // TODO better test
664 
665         try {
666             new DelegateFileFilter((FileFilter) null);
667             fail();
668         } catch( final IllegalArgumentException iae ) {
669         }
670 
671         try {
672             new DelegateFileFilter((FilenameFilter) null);
673             fail();
674         } catch( final IllegalArgumentException iae ) {
675         }
676 
677     }
678 
679     public void testMakeCVSAware() throws Exception {
680         final IOFileFilter filter1 = FileFilterUtils.makeCVSAware(null);
681         final IOFileFilter filter2 = FileFilterUtils.makeCVSAware(FileFilterUtils
682             .nameFileFilter("test-file1.txt"));
683 
684         File file = new File(getTestDirectory(), "CVS");
685         file.mkdirs();
686         assertFiltering(filter1, file, false);
687         assertFiltering(filter2, file, false);
688         FileUtils.deleteDirectory(file);
689 
690         file = new File(getTestDirectory(), "test-file1.txt");
691         createFile(file, 0);
692         assertFiltering(filter1, file, true);
693         assertFiltering(filter2, file, true);
694 
695         file = new File(getTestDirectory(), "test-file2.log");
696         createFile(file, 0);
697         assertFiltering(filter1, file, true);
698         assertFiltering(filter2, file, false);
699 
700         file = new File(getTestDirectory(), "CVS");
701         createFile(file, 0);
702         assertFiltering(filter1, file, true);
703         assertFiltering(filter2, file, false);
704     }
705 
706     public void testMakeSVNAware() throws Exception {
707         final IOFileFilter filter1 = FileFilterUtils.makeSVNAware(null);
708         final IOFileFilter filter2 = FileFilterUtils.makeSVNAware(FileFilterUtils
709             .nameFileFilter("test-file1.txt"));
710 
711         File file = new File(getTestDirectory(), SVN_DIR_NAME);
712         file.mkdirs();
713         assertFiltering(filter1, file, false);
714         assertFiltering(filter2, file, false);
715         FileUtils.deleteDirectory(file);
716 
717         file = new File(getTestDirectory(), "test-file1.txt");
718         createFile(file, 0);
719         assertFiltering(filter1, file, true);
720         assertFiltering(filter2, file, true);
721 
722         file = new File(getTestDirectory(), "test-file2.log");
723         createFile(file, 0);
724         assertFiltering(filter1, file, true);
725         assertFiltering(filter2, file, false);
726 
727         file = new File(getTestDirectory(), SVN_DIR_NAME);
728         createFile(file, 0);
729         assertFiltering(filter1, file, true);
730         assertFiltering(filter2, file, false);
731     }
732 
733     public void testAgeFilter() throws Exception {
734         final File oldFile = new File(getTestDirectory(), "old.txt");
735         final File reference = new File(getTestDirectory(), "reference.txt");
736         final File newFile = new File(getTestDirectory(), "new.txt");
737 
738         createFile(oldFile, 0);
739 
740         do {
741             try {
742                 Thread.sleep(1000);
743             } catch(final InterruptedException ie) {
744                 // ignore
745             }
746             createFile(reference, 0);
747         } while( oldFile.lastModified() == reference.lastModified() );
748 
749         final Date date = new Date();
750         final long now = date.getTime();
751 
752         do {
753             try {
754                 Thread.sleep(1000);
755             } catch(final InterruptedException ie) {
756                 // ignore
757             }
758             createFile(newFile, 0);
759         } while( reference.lastModified() == newFile.lastModified() );
760 
761         final IOFileFilter filter1 = FileFilterUtils.ageFileFilter(now);
762         final IOFileFilter filter2 = FileFilterUtils.ageFileFilter(now, true);
763         final IOFileFilter filter3 = FileFilterUtils.ageFileFilter(now, false);
764         final IOFileFilter filter4 = FileFilterUtils.ageFileFilter(date);
765         final IOFileFilter filter5 = FileFilterUtils.ageFileFilter(date, true);
766         final IOFileFilter filter6 = FileFilterUtils.ageFileFilter(date, false);
767         final IOFileFilter filter7 = FileFilterUtils.ageFileFilter(reference);
768         final IOFileFilter filter8 = FileFilterUtils.ageFileFilter(reference, true);
769         final IOFileFilter filter9 = FileFilterUtils.ageFileFilter(reference, false);
770 
771         assertFiltering(filter1, oldFile, true);
772         assertFiltering(filter2, oldFile, true);
773         assertFiltering(filter3, oldFile, false);
774         assertFiltering(filter4, oldFile, true);
775         assertFiltering(filter5, oldFile, true);
776         assertFiltering(filter6, oldFile, false);
777         assertFiltering(filter7, oldFile, true);
778         assertFiltering(filter8, oldFile, true);
779         assertFiltering(filter9, oldFile, false);
780         assertFiltering(filter1, newFile, false);
781         assertFiltering(filter2, newFile, false);
782         assertFiltering(filter3, newFile, true);
783         assertFiltering(filter4, newFile, false);
784         assertFiltering(filter5, newFile, false);
785         assertFiltering(filter6, newFile, true);
786         assertFiltering(filter7, newFile, false);
787         assertFiltering(filter8, newFile, false);
788         assertFiltering(filter9, newFile, true);
789     }
790 
791     public void testSizeFilter() throws Exception {
792         final File smallFile = new File(getTestDirectory(), "small.txt");
793         createFile(smallFile, 32);
794         final File largeFile = new File(getTestDirectory(), "large.txt");
795         createFile(largeFile, 128);
796         final IOFileFilter filter1 = FileFilterUtils.sizeFileFilter(64);
797         final IOFileFilter filter2 = FileFilterUtils.sizeFileFilter(64, true);
798         final IOFileFilter filter3 = FileFilterUtils.sizeFileFilter(64, false);
799 
800         assertFiltering(filter1, smallFile, false);
801         assertFiltering(filter2, smallFile, false);
802         assertFiltering(filter3, smallFile, true);
803         assertFiltering(filter1, largeFile, true);
804         assertFiltering(filter2, largeFile, true);
805         assertFiltering(filter3, largeFile, false);
806 
807         // size range tests
808         final IOFileFilter filter4 = FileFilterUtils.sizeRangeFileFilter(33, 127);
809         final IOFileFilter filter5 = FileFilterUtils.sizeRangeFileFilter(32, 127);
810         final IOFileFilter filter6 = FileFilterUtils.sizeRangeFileFilter(33, 128);
811         final IOFileFilter filter7 = FileFilterUtils.sizeRangeFileFilter(31, 129);
812         final IOFileFilter filter8 = FileFilterUtils.sizeRangeFileFilter(128, 128);
813 
814         assertFiltering(filter4, smallFile, false);
815         assertFiltering(filter4, largeFile, false);
816         assertFiltering(filter5, smallFile, true);
817         assertFiltering(filter5, largeFile, false);
818         assertFiltering(filter6, smallFile, false);
819         assertFiltering(filter6, largeFile, true);
820         assertFiltering(filter7, smallFile, true);
821         assertFiltering(filter7, largeFile, true);
822         assertFiltering(filter8, largeFile, true);
823 
824         try {
825             FileFilterUtils.sizeFileFilter(-1);
826             fail();
827         } catch (final IllegalArgumentException ex) {
828             // expected
829         }
830     }
831 
832     public void testHidden() throws Exception {
833         final File hiddenDir = new File(SVN_DIR_NAME);
834         if (hiddenDir.exists()) {
835             assertFiltering(HiddenFileFilter.HIDDEN,  hiddenDir, hiddenDir.isHidden());
836             assertFiltering(HiddenFileFilter.VISIBLE, hiddenDir, !hiddenDir.isHidden());
837         }
838         assertFiltering(HiddenFileFilter.HIDDEN,  getTestDirectory(), false);
839         assertFiltering(HiddenFileFilter.VISIBLE, getTestDirectory(), true);
840     }
841 
842     public void testCanRead() throws Exception {
843         final File readOnlyFile = new File(getTestDirectory(), "read-only-file1.txt");
844         createFile(readOnlyFile, 32);
845         readOnlyFile.setReadOnly();
846         assertFiltering(CanReadFileFilter.CAN_READ,  readOnlyFile, true);
847         assertFiltering(CanReadFileFilter.CANNOT_READ,  readOnlyFile, false);
848         assertFiltering(CanReadFileFilter.READ_ONLY, readOnlyFile, true);
849         readOnlyFile.delete();
850     }
851 
852     public void testCanWrite() throws Exception {
853         final File readOnlyFile = new File(getTestDirectory(), "read-only-file2.txt");
854         createFile(readOnlyFile, 32);
855         readOnlyFile.setReadOnly();
856         assertFiltering(CanWriteFileFilter.CAN_WRITE,    getTestDirectory(), true);
857         assertFiltering(CanWriteFileFilter.CANNOT_WRITE, getTestDirectory(), false);
858         assertFiltering(CanWriteFileFilter.CAN_WRITE,    readOnlyFile, false);
859         assertFiltering(CanWriteFileFilter.CANNOT_WRITE, readOnlyFile, true);
860         readOnlyFile.delete();
861     }
862 
863     public void testEmpty() throws Exception {
864 
865         // Empty Dir
866         final File emptyDir  = new File(getTestDirectory(), "empty-dir");
867         emptyDir.mkdirs();
868         assertFiltering(EmptyFileFilter.EMPTY, emptyDir, true);
869         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, false);
870 
871         // Empty File
872         final File emptyFile = new File(emptyDir, "empty-file.txt");
873         createFile(emptyFile, 0);
874         assertFiltering(EmptyFileFilter.EMPTY, emptyFile, true);
875         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyFile, false);
876 
877         // Not Empty Dir
878         assertFiltering(EmptyFileFilter.EMPTY, emptyDir, false);
879         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, true);
880 
881         // Not Empty File
882         final File notEmptyFile = new File(emptyDir, "not-empty-file.txt");
883         createFile(notEmptyFile, 32);
884         assertFiltering(EmptyFileFilter.EMPTY, notEmptyFile, false);
885         assertFiltering(EmptyFileFilter.NOT_EMPTY, notEmptyFile, true);
886         FileUtils.forceDelete(emptyDir);
887     }
888 
889     //-----------------------------------------------------------------------
890     public void testMakeDirectoryOnly() throws Exception {
891         assertSame(DirectoryFileFilter.DIRECTORY, FileFilterUtils.makeDirectoryOnly(null));
892 
893         final IOFileFilter filter = FileFilterUtils.makeDirectoryOnly(
894                 FileFilterUtils.nameFileFilter("B"));
895 
896         final File fileA = new File(getTestDirectory(), "A");
897         final File fileB = new File(getTestDirectory(), "B");
898 
899         fileA.mkdirs();
900         fileB.mkdirs();
901 
902         assertFiltering(filter, fileA, false);
903         assertFiltering(filter, fileB, true);
904 
905         FileUtils.deleteDirectory(fileA);
906         FileUtils.deleteDirectory(fileB);
907 
908         createFile(fileA, 32);
909         createFile(fileB, 32);
910 
911         assertFiltering(filter, fileA, false);
912         assertFiltering(filter, fileB, false);
913 
914         fileA.delete();
915         fileB.delete();
916     }
917 
918     //-----------------------------------------------------------------------
919     public void testMakeFileOnly() throws Exception {
920         assertSame(FileFileFilter.FILE, FileFilterUtils.makeFileOnly(null));
921 
922         final IOFileFilter filter = FileFilterUtils.makeFileOnly(
923                 FileFilterUtils.nameFileFilter("B"));
924 
925         final File fileA = new File(getTestDirectory(), "A");
926         final File fileB = new File(getTestDirectory(), "B");
927 
928         fileA.mkdirs();
929         fileB.mkdirs();
930 
931         assertFiltering(filter, fileA, false);
932         assertFiltering(filter, fileB, false);
933 
934         FileUtils.deleteDirectory(fileA);
935         FileUtils.deleteDirectory(fileB);
936 
937         createFile(fileA, 32);
938         createFile(fileB, 32);
939 
940         assertFiltering(filter, fileA, false);
941         assertFiltering(filter, fileB, true);
942 
943         fileA.delete();
944         fileB.delete();
945     }
946 
947     //-----------------------------------------------------------------------
948 
949     public void testMagicNumberFileFilterBytes() throws Exception {
950         final byte[] classFileMagicNumber =
951             new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
952         final String xmlFileContent = "<?xml version=\"1.0\" encoding=\"UTF-8\">\n" +
953             "<element>text</element>";
954 
955         final File classFileA = new File(getTestDirectory(), "A.class");
956         final File xmlFileB = new File(getTestDirectory(), "B.xml");
957         final File emptyFile = new File(getTestDirectory(), "C.xml");
958         final File dir = new File(getTestDirectory(), "D");
959         dir.mkdirs();
960 
961         final OutputStream classFileAStream = FileUtils.openOutputStream(classFileA);
962         IOUtils.write(classFileMagicNumber, classFileAStream);
963         generateTestData(classFileAStream, 32);
964         classFileAStream.close();
965 
966         FileUtils.write(xmlFileB, xmlFileContent, Charsets.UTF_8);
967         FileUtils.touch(emptyFile);
968 
969         IOFileFilter filter = new MagicNumberFileFilter(classFileMagicNumber);
970 
971         assertFiltering(filter, classFileA, true);
972         assertFiltering(filter, xmlFileB, false);
973         assertFiltering(filter, emptyFile, false);
974         assertFiltering(filter, dir, false);
975 
976 
977         filter = FileFilterUtils.magicNumberFileFilter(classFileMagicNumber);
978 
979         assertFiltering(filter, classFileA, true);
980         assertFiltering(filter, xmlFileB, false);
981         assertFiltering(filter, emptyFile, false);
982         assertFiltering(filter, dir, false);
983     }
984 
985     public void testMagicNumberFileFilterBytesOffset() throws Exception {
986         final byte[] tarMagicNumber = new byte[] {0x75, 0x73, 0x74, 0x61, 0x72};
987         final long tarMagicNumberOffset = 257;
988 
989         final File tarFileA = new File(getTestDirectory(), "A.tar");
990         final File randomFileB = new File(getTestDirectory(), "B.txt");
991         final File dir = new File(getTestDirectory(), "D");
992         dir.mkdirs();
993 
994         final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
995         generateTestData(tarFileAStream, tarMagicNumberOffset);
996         IOUtils.write(tarMagicNumber, tarFileAStream);
997         tarFileAStream.close();
998 
999         createFile(randomFileB, 2 * tarMagicNumberOffset);
1000 
1001         IOFileFilter filter =
1002             new MagicNumberFileFilter(tarMagicNumber, tarMagicNumberOffset);
1003 
1004         assertFiltering(filter, tarFileA, true);
1005         assertFiltering(filter, randomFileB, false);
1006         assertFiltering(filter, dir, false);
1007 
1008         filter = FileFilterUtils.magicNumberFileFilter(tarMagicNumber,
1009                 tarMagicNumberOffset);
1010 
1011         assertFiltering(filter, tarFileA, true);
1012         assertFiltering(filter, randomFileB, false);
1013         assertFiltering(filter, dir, false);
1014     }
1015 
1016     public void testMagicNumberFileFilterString() throws Exception {
1017         final byte[] classFileMagicNumber =
1018             new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
1019         final String xmlFileContent = "<?xml version=\"1.0\" encoding=\"UTF-8\">\n" +
1020             "<element>text</element>";
1021         final String xmlMagicNumber = "<?xml version=\"1.0\"";
1022 
1023         final File classFileA = new File(getTestDirectory(), "A.class");
1024         final File xmlFileB = new File(getTestDirectory(), "B.xml");
1025         final File dir = new File(getTestDirectory(), "D");
1026         dir.mkdirs();
1027 
1028         final OutputStream classFileAStream = FileUtils.openOutputStream(classFileA);
1029         IOUtils.write(classFileMagicNumber, classFileAStream);
1030         generateTestData(classFileAStream, 32);
1031         classFileAStream.close();
1032 
1033         FileUtils.write(xmlFileB, xmlFileContent, Charsets.UTF_8);
1034 
1035         IOFileFilter filter = new MagicNumberFileFilter(xmlMagicNumber);
1036 
1037         assertFiltering(filter, classFileA, false);
1038         assertFiltering(filter, xmlFileB, true);
1039         assertFiltering(filter, dir, false);
1040 
1041         filter = FileFilterUtils.magicNumberFileFilter(xmlMagicNumber);
1042 
1043         assertFiltering(filter, classFileA, false);
1044         assertFiltering(filter, xmlFileB, true);
1045         assertFiltering(filter, dir, false);
1046     }
1047 
1048     public void testMagicNumberFileFilterStringOffset() throws Exception {
1049         final String tarMagicNumber = "ustar";
1050         final long tarMagicNumberOffset = 257;
1051 
1052         final File tarFileA = new File(getTestDirectory(), "A.tar");
1053         final File randomFileB = new File(getTestDirectory(), "B.txt");
1054         final File dir = new File(getTestDirectory(), "D");
1055         dir.mkdirs();
1056 
1057         final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
1058         generateTestData(tarFileAStream, tarMagicNumberOffset);
1059         IOUtils.write(tarMagicNumber, tarFileAStream, Charsets.UTF_8);
1060         tarFileAStream.close();
1061 
1062         createFile(randomFileB, 2 * tarMagicNumberOffset);
1063 
1064         IOFileFilter filter =
1065             new MagicNumberFileFilter(tarMagicNumber, tarMagicNumberOffset);
1066 
1067         assertFiltering(filter, tarFileA, true);
1068         assertFiltering(filter, randomFileB, false);
1069         assertFiltering(filter, dir, false);
1070 
1071         filter = FileFilterUtils.magicNumberFileFilter(tarMagicNumber,
1072                 tarMagicNumberOffset);
1073 
1074         assertFiltering(filter, tarFileA, true);
1075         assertFiltering(filter, randomFileB, false);
1076         assertFiltering(filter, dir, false);
1077     }
1078 
1079     public void testMagicNumberFileFilterValidation() {
1080         try {
1081             new MagicNumberFileFilter((String)null, 0);
1082             fail();
1083         } catch (final IllegalArgumentException iae) {
1084             // expected
1085         }
1086         try {
1087             new MagicNumberFileFilter("0", -1);
1088             fail();
1089         } catch (final IllegalArgumentException iae) {
1090             // expected
1091         }
1092         try {
1093             new MagicNumberFileFilter("", 0);
1094             fail();
1095         } catch (final IllegalArgumentException iae) {
1096             // expected
1097         }
1098         try {
1099             new MagicNumberFileFilter((byte[])null, 0);
1100             fail();
1101         } catch (final IllegalArgumentException iae) {
1102             // expected
1103         }
1104         try {
1105             new MagicNumberFileFilter(new byte[]{0}, -1);
1106             fail();
1107         } catch (final IllegalArgumentException iae) {
1108             // expected
1109         }
1110         try {
1111             new MagicNumberFileFilter(new byte[]{}, 0);
1112             fail();
1113         } catch (final IllegalArgumentException iae) {
1114             // expected
1115         }
1116 }
1117 
1118     /**
1119      * Test method for {@link FileFilterUtils#filter(IOFileFilter, File...)}
1120      * that tests that the method properly filters files from the list.
1121      */
1122     public void testFilterArray() throws Exception {
1123         final File fileA = newFile("A");
1124         final File fileB = newFile("B");
1125 
1126         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1127 
1128         final File[] filtered = FileFilterUtils.filter(filter, fileA, fileB);
1129 
1130         assertEquals(1, filtered.length);
1131         assertEquals(fileA, filtered[0]);
1132     }
1133 
1134     /**
1135      * Test method for {@link FileFilterUtils#filter(IOFileFilter, java.lang.Iterable)}
1136      * that tests that the method properly filters files from the list.
1137      */
1138     public void testFilterArray_fromList() throws Exception {
1139         final File fileA = newFile("A");
1140         final File fileB = newFile("B");
1141         final List<File> fileList = Arrays.asList(fileA, fileB);
1142 
1143         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1144 
1145         final File[] filtered = FileFilterUtils.filter(filter, fileList);
1146 
1147         assertEquals(1, filtered.length);
1148         assertEquals(fileA, filtered[0]);
1149     }
1150 
1151     /**
1152      * Test method for {@link FileFilterUtils#filter(IOFileFilter, File...)}
1153      * that tests {@code null} parameters and {@code null} elements
1154      * in the provided list.
1155      */
1156     public void testFilterArrayNullParameters() throws Exception {
1157         final File fileA = newFile("A");
1158         final File fileB = newFile("B");
1159         try {
1160             FileFilterUtils.filter(null, fileA, fileB);
1161             fail();
1162         } catch (final IllegalArgumentException iae) {
1163             // Test passes, exception thrown for null filter
1164         }
1165 
1166         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1167         try {
1168             FileFilterUtils.filter(filter, fileA, null);
1169             fail();
1170         } catch (final IllegalArgumentException iae) {
1171             // Test passes, exception thrown for list containing null
1172         }
1173 
1174         final File[] filtered = FileFilterUtils.filter(filter, (File[])null);
1175         assertEquals(0, filtered.length);
1176     }
1177 
1178     /**
1179      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, java.lang.Iterable)}
1180      * that tests that the method properly filters files from the list.
1181      */
1182     public void testFilterList() throws Exception {
1183         final File fileA = newFile("A");
1184         final File fileB = newFile("B");
1185         final List<File> fileList = Arrays.asList(fileA, fileB);
1186 
1187         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1188 
1189         final List<File> filteredList = FileFilterUtils.filterList(filter, fileList);
1190 
1191         assertTrue(filteredList.contains(fileA));
1192         assertFalse(filteredList.contains(fileB));
1193     }
1194 
1195     /**
1196      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, File...)}
1197      * that tests that the method properly filters files from the list.
1198      */
1199     public void testFilterList_fromArray() throws Exception {
1200         final File fileA = newFile("A");
1201         final File fileB = newFile("B");
1202 
1203         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1204 
1205         final List<File> filteredList = FileFilterUtils.filterList(filter, fileA, fileB);
1206 
1207         assertTrue(filteredList.contains(fileA));
1208         assertFalse(filteredList.contains(fileB));
1209     }
1210 
1211     /**
1212      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, java.lang.Iterable)}
1213      * that tests {@code null} parameters and {@code null} elements
1214      * in the provided list.
1215      */
1216     public void testFilterListNullParameters() {
1217         try {
1218             FileFilterUtils.filterList(null, Collections.<File>emptyList());
1219             fail();
1220         } catch (final IllegalArgumentException iae) {
1221             // Test passes, exception thrown for null filter
1222         }
1223 
1224         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1225         try {
1226             FileFilterUtils.filterList(filter, Arrays.<File>asList((File) null));
1227             fail();
1228         } catch (final IllegalArgumentException iae) {
1229             // Test passes, exception thrown for list containing null
1230         }
1231 
1232         final List<File> filteredList = FileFilterUtils.filterList(filter, (List<File>)null);
1233         assertEquals(0, filteredList.size());
1234     }
1235 
1236     /**
1237      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, java.lang.Iterable)}
1238      * that tests that the method properly filters files from the set.
1239      */
1240     public void testFilterSet() throws Exception {
1241         final File fileA = newFile("A");
1242         final File fileB = newFile("B");
1243         final Set<File> fileList = new HashSet<File>(Arrays.asList(fileA, fileB));
1244 
1245         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1246 
1247         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, fileList);
1248 
1249         assertTrue(filteredSet.contains(fileA));
1250         assertFalse(filteredSet.contains(fileB));
1251     }
1252 
1253     /**
1254      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, File...)}
1255      * that tests that the method properly filters files from the set.
1256      */
1257     public void testFilterSet_fromArray() throws Exception {
1258         final File fileA = newFile("A");
1259         final File fileB = newFile("B");
1260 
1261         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1262 
1263         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, fileA, fileB);
1264 
1265         assertTrue(filteredSet.contains(fileA));
1266         assertFalse(filteredSet.contains(fileB));
1267     }
1268 
1269     /**
1270      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, java.lang.Iterable)}
1271      * that tests {@code null} parameters and {@code null} elements
1272      * in the provided set.
1273      */
1274    public void testFilterSetNullParameters() {
1275         try {
1276             FileFilterUtils.filterSet(null, Collections.<File>emptySet());
1277             fail();
1278         } catch (final IllegalArgumentException iae) {
1279             // Test passes, exception thrown for null filter
1280         }
1281 
1282         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1283         try {
1284             FileFilterUtils.filterSet(filter, new HashSet<File>(Arrays.<File>asList((File) null)));
1285             fail();
1286         } catch (final IllegalArgumentException iae) {
1287             // Test passes, exception thrown for set containing null
1288         }
1289 
1290         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, (Set<File>)null);
1291         assertEquals(0, filteredSet.size());
1292     }
1293 
1294        public void testEnsureTestCoverage() {
1295            assertNotNull(new FileFilterUtils()); // dummy for test coverage
1296        }
1297 
1298        public void testNullFilters() {
1299            try {
1300                FileFilterUtils.toList((IOFileFilter)null);
1301                fail("Expected IllegalArgumentException");
1302            } catch (final IllegalArgumentException expected) {
1303                // expected
1304            }
1305            try {
1306                FileFilterUtils.toList(new IOFileFilter[]{null});
1307                fail("Expected IllegalArgumentException");
1308            } catch (final IllegalArgumentException expected) {
1309             // expected
1310            }
1311        }
1312 
1313        public void testDelegation() { // TODO improve these tests
1314            assertNotNull(FileFilterUtils.asFileFilter((FileFilter)FalseFileFilter.INSTANCE));
1315            assertNotNull(FileFilterUtils.asFileFilter((FilenameFilter)FalseFileFilter.INSTANCE).toString());
1316        }
1317 }