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