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.FileUtils;
20  import org.apache.commons.io.IOCase;
21  import org.apache.commons.io.IOUtils;
22  import org.apache.commons.io.testtools.FileBasedTestCase;
23  import org.apache.commons.io.testtools.TestUtils;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import java.io.*;
29  import java.nio.charset.StandardCharsets;
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<>();
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<>();
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<>();
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         try (final BufferedOutputStream output2 =
707                 new BufferedOutputStream(new FileOutputStream(file))) {
708             TestUtils.generateTestData(output2, (long) 0);
709         }
710         assertFiltering(filter1, file, true);
711         assertFiltering(filter2, file, true);
712 
713         file = new File(getTestDirectory(), "test-file2.log");
714         if (!file.getParentFile().exists()) {
715             throw new IOException("Cannot create file " + file
716                     + " as the parent directory does not exist");
717         }
718         try (final BufferedOutputStream output1 =
719                 new BufferedOutputStream(new FileOutputStream(file))) {
720             TestUtils.generateTestData(output1, (long) 0);
721         }
722         assertFiltering(filter1, file, true);
723         assertFiltering(filter2, file, false);
724 
725         file = new File(getTestDirectory(), "CVS");
726         if (!file.getParentFile().exists()) {
727             throw new IOException("Cannot create file " + file
728                     + " as the parent directory does not exist");
729         }
730         try (final BufferedOutputStream output =
731                 new BufferedOutputStream(new FileOutputStream(file))) {
732             TestUtils.generateTestData(output, (long) 0);
733         }
734         assertFiltering(filter1, file, true);
735         assertFiltering(filter2, file, false);
736     }
737 
738     @Test
739     public void testMakeSVNAware() throws Exception {
740         final IOFileFilter filter1 = FileFilterUtils.makeSVNAware(null);
741         final IOFileFilter filter2 = FileFilterUtils.makeSVNAware(FileFilterUtils
742             .nameFileFilter("test-file1.txt"));
743 
744         File file = new File(getTestDirectory(), SVN_DIR_NAME);
745         file.mkdirs();
746         assertFiltering(filter1, file, false);
747         assertFiltering(filter2, file, false);
748         FileUtils.deleteDirectory(file);
749 
750         file = new File(getTestDirectory(), "test-file1.txt");
751         if (!file.getParentFile().exists()) {
752             throw new IOException("Cannot create file " + file
753                     + " as the parent directory does not exist");
754         }
755         try (final BufferedOutputStream output2 =
756                 new BufferedOutputStream(new FileOutputStream(file))) {
757             TestUtils.generateTestData(output2, (long) 0);
758         }
759         assertFiltering(filter1, file, true);
760         assertFiltering(filter2, file, true);
761 
762         file = new File(getTestDirectory(), "test-file2.log");
763         if (!file.getParentFile().exists()) {
764             throw new IOException("Cannot create file " + file
765                     + " as the parent directory does not exist");
766         }
767         try (final BufferedOutputStream output1 =
768                 new BufferedOutputStream(new FileOutputStream(file))) {
769             TestUtils.generateTestData(output1, (long) 0);
770         }
771         assertFiltering(filter1, file, true);
772         assertFiltering(filter2, file, false);
773 
774         file = new File(getTestDirectory(), SVN_DIR_NAME);
775         if (!file.getParentFile().exists()) {
776             throw new IOException("Cannot create file " + file
777                     + " as the parent directory does not exist");
778         }
779         try (final BufferedOutputStream output =
780                 new BufferedOutputStream(new FileOutputStream(file))) {
781             TestUtils.generateTestData(output, (long) 0);
782         }
783         assertFiltering(filter1, file, true);
784         assertFiltering(filter2, file, false);
785     }
786 
787     @Test
788     public void testAgeFilter() throws Exception {
789         final File oldFile = new File(getTestDirectory(), "old.txt");
790         final File reference = new File(getTestDirectory(), "reference.txt");
791         final File newFile = new File(getTestDirectory(), "new.txt");
792 
793         if (!oldFile.getParentFile().exists()) {
794             throw new IOException("Cannot create file " + oldFile
795                     + " as the parent directory does not exist");
796         }
797         try (final BufferedOutputStream output1 =
798                 new BufferedOutputStream(new FileOutputStream(oldFile))) {
799             TestUtils.generateTestData(output1, (long) 0);
800         }
801 
802         do {
803             try {
804                 TestUtils.sleep(1000);
805             } catch(final InterruptedException ie) {
806                 // ignore
807             }
808             if (!reference.getParentFile().exists()) {
809                 throw new IOException("Cannot create file " + reference
810                         + " as the parent directory does not exist");
811             }
812             try (final BufferedOutputStream output =
813                     new BufferedOutputStream(new FileOutputStream(reference))) {
814                 TestUtils.generateTestData(output, (long) 0);
815             }
816         } while( oldFile.lastModified() == reference.lastModified() );
817 
818         final Date date = new Date();
819         final long now = date.getTime();
820 
821         do {
822             try {
823                 TestUtils.sleep(1000);
824             } catch(final InterruptedException ie) {
825                 // ignore
826             }
827             if (!newFile.getParentFile().exists()) {
828                 throw new IOException("Cannot create file " + newFile
829                         + " as the parent directory does not exist");
830             }
831             try (final BufferedOutputStream output =
832                     new BufferedOutputStream(new FileOutputStream(newFile))) {
833                 TestUtils.generateTestData(output, (long) 0);
834             }
835         } while( reference.lastModified() == newFile.lastModified() );
836 
837         final IOFileFilter filter1 = FileFilterUtils.ageFileFilter(now);
838         final IOFileFilter filter2 = FileFilterUtils.ageFileFilter(now, true);
839         final IOFileFilter filter3 = FileFilterUtils.ageFileFilter(now, false);
840         final IOFileFilter filter4 = FileFilterUtils.ageFileFilter(date);
841         final IOFileFilter filter5 = FileFilterUtils.ageFileFilter(date, true);
842         final IOFileFilter filter6 = FileFilterUtils.ageFileFilter(date, false);
843         final IOFileFilter filter7 = FileFilterUtils.ageFileFilter(reference);
844         final IOFileFilter filter8 = FileFilterUtils.ageFileFilter(reference, true);
845         final IOFileFilter filter9 = FileFilterUtils.ageFileFilter(reference, false);
846 
847         assertFiltering(filter1, oldFile, true);
848         assertFiltering(filter2, oldFile, true);
849         assertFiltering(filter3, oldFile, false);
850         assertFiltering(filter4, oldFile, true);
851         assertFiltering(filter5, oldFile, true);
852         assertFiltering(filter6, oldFile, false);
853         assertFiltering(filter7, oldFile, true);
854         assertFiltering(filter8, oldFile, true);
855         assertFiltering(filter9, oldFile, false);
856         assertFiltering(filter1, newFile, false);
857         assertFiltering(filter2, newFile, false);
858         assertFiltering(filter3, newFile, true);
859         assertFiltering(filter4, newFile, false);
860         assertFiltering(filter5, newFile, false);
861         assertFiltering(filter6, newFile, true);
862         assertFiltering(filter7, newFile, false);
863         assertFiltering(filter8, newFile, false);
864         assertFiltering(filter9, newFile, true);
865     }
866 
867     @Test
868     public void testSizeFilter() throws Exception {
869         final File smallFile = new File(getTestDirectory(), "small.txt");
870         if (!smallFile.getParentFile().exists()) {
871             throw new IOException("Cannot create file " + smallFile
872                     + " as the parent directory does not exist");
873         }
874         try (final BufferedOutputStream output1 =
875                 new BufferedOutputStream(new FileOutputStream(smallFile))) {
876             TestUtils.generateTestData(output1, (long) 32);
877         }
878         final File largeFile = new File(getTestDirectory(), "large.txt");
879         if (!largeFile.getParentFile().exists()) {
880             throw new IOException("Cannot create file " + largeFile
881                     + " as the parent directory does not exist");
882         }
883         try (final BufferedOutputStream output =
884                 new BufferedOutputStream(new FileOutputStream(largeFile))){
885             TestUtils.generateTestData(output, (long) 128);
886         }
887         final IOFileFilter filter1 = FileFilterUtils.sizeFileFilter(64);
888         final IOFileFilter filter2 = FileFilterUtils.sizeFileFilter(64, true);
889         final IOFileFilter filter3 = FileFilterUtils.sizeFileFilter(64, false);
890 
891         assertFiltering(filter1, smallFile, false);
892         assertFiltering(filter2, smallFile, false);
893         assertFiltering(filter3, smallFile, true);
894         assertFiltering(filter1, largeFile, true);
895         assertFiltering(filter2, largeFile, true);
896         assertFiltering(filter3, largeFile, false);
897 
898         // size range tests
899         final IOFileFilter filter4 = FileFilterUtils.sizeRangeFileFilter(33, 127);
900         final IOFileFilter filter5 = FileFilterUtils.sizeRangeFileFilter(32, 127);
901         final IOFileFilter filter6 = FileFilterUtils.sizeRangeFileFilter(33, 128);
902         final IOFileFilter filter7 = FileFilterUtils.sizeRangeFileFilter(31, 129);
903         final IOFileFilter filter8 = FileFilterUtils.sizeRangeFileFilter(128, 128);
904 
905         assertFiltering(filter4, smallFile, false);
906         assertFiltering(filter4, largeFile, false);
907         assertFiltering(filter5, smallFile, true);
908         assertFiltering(filter5, largeFile, false);
909         assertFiltering(filter6, smallFile, false);
910         assertFiltering(filter6, largeFile, true);
911         assertFiltering(filter7, smallFile, true);
912         assertFiltering(filter7, largeFile, true);
913         assertFiltering(filter8, largeFile, true);
914 
915         try {
916             FileFilterUtils.sizeFileFilter(-1);
917             fail();
918         } catch (final IllegalArgumentException ex) {
919             // expected
920         }
921     }
922 
923     @Test
924     public void testHidden() throws Exception {
925         final File hiddenDir = new File(SVN_DIR_NAME);
926         if (hiddenDir.exists()) {
927             assertFiltering(HiddenFileFilter.HIDDEN,  hiddenDir, hiddenDir.isHidden());
928             assertFiltering(HiddenFileFilter.VISIBLE, hiddenDir, !hiddenDir.isHidden());
929         }
930         assertFiltering(HiddenFileFilter.HIDDEN,  getTestDirectory(), false);
931         assertFiltering(HiddenFileFilter.VISIBLE, getTestDirectory(), true);
932     }
933 
934     @Test
935     public void testCanRead() throws Exception {
936         final File readOnlyFile = new File(getTestDirectory(), "read-only-file1.txt");
937         if (!readOnlyFile.getParentFile().exists()) {
938             throw new IOException("Cannot create file " + readOnlyFile
939                     + " as the parent directory does not exist");
940         }
941         try (final BufferedOutputStream output =
942                 new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
943             TestUtils.generateTestData(output, (long) 32);
944         }
945         readOnlyFile.setReadOnly();
946         assertFiltering(CanReadFileFilter.CAN_READ,  readOnlyFile, true);
947         assertFiltering(CanReadFileFilter.CANNOT_READ,  readOnlyFile, false);
948         assertFiltering(CanReadFileFilter.READ_ONLY, readOnlyFile, true);
949         readOnlyFile.delete();
950     }
951 
952     @Test
953     public void testCanWrite() throws Exception {
954         final File readOnlyFile = new File(getTestDirectory(), "read-only-file2.txt");
955         if (!readOnlyFile.getParentFile().exists()) {
956             throw new IOException("Cannot create file " + readOnlyFile
957                     + " as the parent directory does not exist");
958         }
959         try (final BufferedOutputStream output =
960                 new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
961             TestUtils.generateTestData(output, (long) 32);
962         }
963         readOnlyFile.setReadOnly();
964         assertFiltering(CanWriteFileFilter.CAN_WRITE,    getTestDirectory(), true);
965         assertFiltering(CanWriteFileFilter.CANNOT_WRITE, getTestDirectory(), false);
966         assertFiltering(CanWriteFileFilter.CAN_WRITE,    readOnlyFile, false);
967         assertFiltering(CanWriteFileFilter.CANNOT_WRITE, readOnlyFile, true);
968         readOnlyFile.delete();
969     }
970 
971     @Test
972     public void testEmpty() throws Exception {
973 
974         // Empty Dir
975         final File emptyDir  = new File(getTestDirectory(), "empty-dir");
976         emptyDir.mkdirs();
977         assertFiltering(EmptyFileFilter.EMPTY, emptyDir, true);
978         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, false);
979 
980         // Empty File
981         final File emptyFile = new File(emptyDir, "empty-file.txt");
982         if (!emptyFile.getParentFile().exists()) {
983             throw new IOException("Cannot create file " + emptyFile
984                     + " as the parent directory does not exist");
985         }
986         try (final BufferedOutputStream output1 =
987                 new BufferedOutputStream(new FileOutputStream(emptyFile))){
988             TestUtils.generateTestData(output1, (long) 0);
989         }
990         assertFiltering(EmptyFileFilter.EMPTY, emptyFile, true);
991         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyFile, false);
992 
993         // Not Empty Dir
994         assertFiltering(EmptyFileFilter.EMPTY, emptyDir, false);
995         assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, true);
996 
997         // Not Empty File
998         final File notEmptyFile = new File(emptyDir, "not-empty-file.txt");
999         if (!notEmptyFile.getParentFile().exists()) {
1000             throw new IOException("Cannot create file " + notEmptyFile
1001                     + " as the parent directory does not exist");
1002         }
1003         try (final BufferedOutputStream output =
1004                 new BufferedOutputStream(new FileOutputStream(notEmptyFile))){
1005             TestUtils.generateTestData(output, (long) 32);
1006         }
1007         assertFiltering(EmptyFileFilter.EMPTY, notEmptyFile, false);
1008         assertFiltering(EmptyFileFilter.NOT_EMPTY, notEmptyFile, true);
1009         FileUtils.forceDelete(emptyDir);
1010     }
1011 
1012     //-----------------------------------------------------------------------
1013     @Test
1014     public void testMakeDirectoryOnly() throws Exception {
1015         assertSame(DirectoryFileFilter.DIRECTORY, FileFilterUtils.makeDirectoryOnly(null));
1016 
1017         final IOFileFilter filter = FileFilterUtils.makeDirectoryOnly(
1018                 FileFilterUtils.nameFileFilter("B"));
1019 
1020         final File fileA = new File(getTestDirectory(), "A");
1021         final File fileB = new File(getTestDirectory(), "B");
1022 
1023         fileA.mkdirs();
1024         fileB.mkdirs();
1025 
1026         assertFiltering(filter, fileA, false);
1027         assertFiltering(filter, fileB, true);
1028 
1029         FileUtils.deleteDirectory(fileA);
1030         FileUtils.deleteDirectory(fileB);
1031 
1032         if (!fileA.getParentFile().exists()) {
1033             throw new IOException("Cannot create file " + fileA
1034                     + " as the parent directory does not exist");
1035         }
1036         try (final BufferedOutputStream output1 =
1037                 new BufferedOutputStream(new FileOutputStream(fileA))) {
1038             TestUtils.generateTestData(output1, (long) 32);
1039         }
1040         if (!fileB.getParentFile().exists()) {
1041             throw new IOException("Cannot create file " + fileB
1042                     + " as the parent directory does not exist");
1043         }
1044         try (final BufferedOutputStream output =
1045                 new BufferedOutputStream(new FileOutputStream(fileB))) {
1046             TestUtils.generateTestData(output, (long) 32);
1047         }
1048 
1049         assertFiltering(filter, fileA, false);
1050         assertFiltering(filter, fileB, false);
1051 
1052         fileA.delete();
1053         fileB.delete();
1054     }
1055 
1056     //-----------------------------------------------------------------------
1057     @Test
1058     public void testMakeFileOnly() throws Exception {
1059         assertSame(FileFileFilter.FILE, FileFilterUtils.makeFileOnly(null));
1060 
1061         final IOFileFilter filter = FileFilterUtils.makeFileOnly(
1062                 FileFilterUtils.nameFileFilter("B"));
1063 
1064         final File fileA = new File(getTestDirectory(), "A");
1065         final File fileB = new File(getTestDirectory(), "B");
1066 
1067         fileA.mkdirs();
1068         fileB.mkdirs();
1069 
1070         assertFiltering(filter, fileA, false);
1071         assertFiltering(filter, fileB, false);
1072 
1073         FileUtils.deleteDirectory(fileA);
1074         FileUtils.deleteDirectory(fileB);
1075 
1076         if (!fileA.getParentFile().exists()) {
1077             throw new IOException("Cannot create file " + fileA
1078                     + " as the parent directory does not exist");
1079         }
1080         try (final BufferedOutputStream output1 =
1081                 new BufferedOutputStream(new FileOutputStream(fileA))){
1082             TestUtils.generateTestData(output1, (long) 32);
1083         }
1084         if (!fileB.getParentFile().exists()) {
1085             throw new IOException("Cannot create file " + fileB
1086                     + " as the parent directory does not exist");
1087         }
1088         try (final BufferedOutputStream output =
1089                 new BufferedOutputStream(new FileOutputStream(fileB))){
1090             TestUtils.generateTestData(output, (long) 32);
1091         }
1092 
1093         assertFiltering(filter, fileA, false);
1094         assertFiltering(filter, fileB, true);
1095 
1096         fileA.delete();
1097         fileB.delete();
1098     }
1099 
1100     //-----------------------------------------------------------------------
1101 
1102     @Test
1103     public void testMagicNumberFileFilterBytes() throws Exception {
1104         final byte[] classFileMagicNumber =
1105             new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
1106         final String xmlFileContent = "<?xml version=\"1.0\" encoding=\"UTF-8\">\n" +
1107             "<element>text</element>";
1108 
1109         final File classFileA = new File(getTestDirectory(), "A.class");
1110         final File xmlFileB = new File(getTestDirectory(), "B.xml");
1111         final File emptyFile = new File(getTestDirectory(), "C.xml");
1112         final File dir = new File(getTestDirectory(), "D");
1113         dir.mkdirs();
1114 
1115         final OutputStream classFileAStream = FileUtils.openOutputStream(classFileA);
1116         IOUtils.write(classFileMagicNumber, classFileAStream);
1117         TestUtils.generateTestData(classFileAStream, (long) 32);
1118         classFileAStream.close();
1119 
1120         FileUtils.write(xmlFileB, xmlFileContent, StandardCharsets.UTF_8);
1121         FileUtils.touch(emptyFile);
1122 
1123         IOFileFilter filter = new MagicNumberFileFilter(classFileMagicNumber);
1124 
1125         assertFiltering(filter, classFileA, true);
1126         assertFiltering(filter, xmlFileB, false);
1127         assertFiltering(filter, emptyFile, false);
1128         assertFiltering(filter, dir, false);
1129 
1130 
1131         filter = FileFilterUtils.magicNumberFileFilter(classFileMagicNumber);
1132 
1133         assertFiltering(filter, classFileA, true);
1134         assertFiltering(filter, xmlFileB, false);
1135         assertFiltering(filter, emptyFile, false);
1136         assertFiltering(filter, dir, false);
1137     }
1138 
1139     @Test
1140     public void testMagicNumberFileFilterBytesOffset() throws Exception {
1141         final byte[] tarMagicNumber = new byte[] {0x75, 0x73, 0x74, 0x61, 0x72};
1142         final long tarMagicNumberOffset = 257;
1143 
1144         final File tarFileA = new File(getTestDirectory(), "A.tar");
1145         final File randomFileB = new File(getTestDirectory(), "B.txt");
1146         final File dir = new File(getTestDirectory(), "D");
1147         dir.mkdirs();
1148 
1149         final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
1150         TestUtils.generateTestData(tarFileAStream, tarMagicNumberOffset);
1151         IOUtils.write(tarMagicNumber, tarFileAStream);
1152         tarFileAStream.close();
1153 
1154         if (!randomFileB.getParentFile().exists()) {
1155             throw new IOException("Cannot create file " + randomFileB
1156                     + " as the parent directory does not exist");
1157         }
1158         try (final BufferedOutputStream output =
1159                 new BufferedOutputStream(new FileOutputStream(randomFileB))) {
1160             TestUtils.generateTestData(output, 2 * tarMagicNumberOffset);
1161         }
1162 
1163         IOFileFilter filter =
1164             new MagicNumberFileFilter(tarMagicNumber, tarMagicNumberOffset);
1165 
1166         assertFiltering(filter, tarFileA, true);
1167         assertFiltering(filter, randomFileB, false);
1168         assertFiltering(filter, dir, false);
1169 
1170         filter = FileFilterUtils.magicNumberFileFilter(tarMagicNumber,
1171                 tarMagicNumberOffset);
1172 
1173         assertFiltering(filter, tarFileA, true);
1174         assertFiltering(filter, randomFileB, false);
1175         assertFiltering(filter, dir, false);
1176     }
1177 
1178     @Test
1179     public void testMagicNumberFileFilterString() throws Exception {
1180         final byte[] classFileMagicNumber =
1181             new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
1182         final String xmlFileContent = "<?xml version=\"1.0\" encoding=\"UTF-8\">\n" +
1183             "<element>text</element>";
1184         final String xmlMagicNumber = "<?xml version=\"1.0\"";
1185 
1186         final File classFileA = new File(getTestDirectory(), "A.class");
1187         final File xmlFileB = new File(getTestDirectory(), "B.xml");
1188         final File dir = new File(getTestDirectory(), "D");
1189         dir.mkdirs();
1190 
1191         final OutputStream classFileAStream = FileUtils.openOutputStream(classFileA);
1192         IOUtils.write(classFileMagicNumber, classFileAStream);
1193         TestUtils.generateTestData(classFileAStream, (long) 32);
1194         classFileAStream.close();
1195 
1196         FileUtils.write(xmlFileB, xmlFileContent, StandardCharsets.UTF_8);
1197 
1198         IOFileFilter filter = new MagicNumberFileFilter(xmlMagicNumber);
1199 
1200         assertFiltering(filter, classFileA, false);
1201         assertFiltering(filter, xmlFileB, true);
1202         assertFiltering(filter, dir, false);
1203 
1204         filter = FileFilterUtils.magicNumberFileFilter(xmlMagicNumber);
1205 
1206         assertFiltering(filter, classFileA, false);
1207         assertFiltering(filter, xmlFileB, true);
1208         assertFiltering(filter, dir, false);
1209     }
1210 
1211     @Test
1212     public void testMagicNumberFileFilterStringOffset() throws Exception {
1213         final String tarMagicNumber = "ustar";
1214         final long tarMagicNumberOffset = 257;
1215 
1216         final File tarFileA = new File(getTestDirectory(), "A.tar");
1217         final File randomFileB = new File(getTestDirectory(), "B.txt");
1218         final File dir = new File(getTestDirectory(), "D");
1219         dir.mkdirs();
1220 
1221         final OutputStream tarFileAStream = FileUtils.openOutputStream(tarFileA);
1222         TestUtils.generateTestData(tarFileAStream, tarMagicNumberOffset);
1223         IOUtils.write(tarMagicNumber, tarFileAStream, StandardCharsets.UTF_8);
1224         tarFileAStream.close();
1225 
1226         if (!randomFileB.getParentFile().exists()) {
1227             throw new IOException("Cannot create file " + randomFileB
1228                     + " as the parent directory does not exist");
1229         }
1230         try (final BufferedOutputStream output =
1231                 new BufferedOutputStream(new FileOutputStream(randomFileB))) {
1232             TestUtils.generateTestData(output, 2 * tarMagicNumberOffset);
1233         }
1234 
1235         IOFileFilter filter =
1236             new MagicNumberFileFilter(tarMagicNumber, tarMagicNumberOffset);
1237 
1238         assertFiltering(filter, tarFileA, true);
1239         assertFiltering(filter, randomFileB, false);
1240         assertFiltering(filter, dir, false);
1241 
1242         filter = FileFilterUtils.magicNumberFileFilter(tarMagicNumber,
1243                 tarMagicNumberOffset);
1244 
1245         assertFiltering(filter, tarFileA, true);
1246         assertFiltering(filter, randomFileB, false);
1247         assertFiltering(filter, dir, false);
1248     }
1249 
1250     @Test
1251     public void testMagicNumberFileFilterValidation() {
1252         try {
1253             new MagicNumberFileFilter((String)null, 0);
1254             fail();
1255         } catch (final IllegalArgumentException iae) {
1256             // expected
1257         }
1258         try {
1259             new MagicNumberFileFilter("0", -1);
1260             fail();
1261         } catch (final IllegalArgumentException iae) {
1262             // expected
1263         }
1264         try {
1265             new MagicNumberFileFilter("", 0);
1266             fail();
1267         } catch (final IllegalArgumentException iae) {
1268             // expected
1269         }
1270         try {
1271             new MagicNumberFileFilter((byte[])null, 0);
1272             fail();
1273         } catch (final IllegalArgumentException iae) {
1274             // expected
1275         }
1276         try {
1277             new MagicNumberFileFilter(new byte[]{0}, -1);
1278             fail();
1279         } catch (final IllegalArgumentException iae) {
1280             // expected
1281         }
1282         try {
1283             new MagicNumberFileFilter(new byte[]{}, 0);
1284             fail();
1285         } catch (final IllegalArgumentException iae) {
1286             // expected
1287         }
1288 }
1289 
1290     /*
1291      * Test method for {@link FileFilterUtils#filter(IOFileFilter, File...)}
1292      * that tests that the method properly filters files from the list.
1293      */
1294     @Test
1295     public void testFilterArray() throws Exception {
1296         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1297         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1298 
1299         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1300 
1301         final File[] filtered = FileFilterUtils.filter(filter, fileA, fileB);
1302 
1303         assertEquals(1, filtered.length);
1304         assertEquals(fileA, filtered[0]);
1305     }
1306 
1307     /*
1308      * Test method for {@link FileFilterUtils#filter(IOFileFilter, java.lang.Iterable)}
1309      * that tests that the method properly filters files from the list.
1310      */
1311     @Test
1312     public void testFilterArray_fromList() throws Exception {
1313         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1314         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1315         final List<File> fileList = Arrays.asList(fileA, fileB);
1316 
1317         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1318 
1319         final File[] filtered = FileFilterUtils.filter(filter, fileList);
1320 
1321         assertEquals(1, filtered.length);
1322         assertEquals(fileA, filtered[0]);
1323     }
1324 
1325     /*
1326      * Test method for {@link FileFilterUtils#filter(IOFileFilter, File...)}
1327      * that tests {@code null} parameters and {@code null} elements
1328      * in the provided list.
1329      */
1330     @Test
1331     public void testFilterArrayNullParameters() throws Exception {
1332         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1333         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1334         try {
1335             FileFilterUtils.filter(null, fileA, fileB);
1336             fail();
1337         } catch (final IllegalArgumentException iae) {
1338             // Test passes, exception thrown for null filter
1339         }
1340 
1341         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1342         try {
1343             FileFilterUtils.filter(filter, fileA, null);
1344             fail();
1345         } catch (final IllegalArgumentException iae) {
1346             // Test passes, exception thrown for list containing null
1347         }
1348 
1349         final File[] filtered = FileFilterUtils.filter(filter, (File[])null);
1350         assertEquals(0, filtered.length);
1351     }
1352 
1353     /*
1354      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, java.lang.Iterable)}
1355      * that tests that the method properly filters files from the list.
1356      */
1357     @Test
1358     public void testFilterList() throws Exception {
1359         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1360         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1361         final List<File> fileList = Arrays.asList(fileA, fileB);
1362 
1363         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1364 
1365         final List<File> filteredList = FileFilterUtils.filterList(filter, fileList);
1366 
1367         assertTrue(filteredList.contains(fileA));
1368         assertFalse(filteredList.contains(fileB));
1369     }
1370 
1371     /*
1372      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, File...)}
1373      * that tests that the method properly filters files from the list.
1374      */
1375     @Test
1376     public void testFilterList_fromArray() throws Exception {
1377         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1378         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1379 
1380         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1381 
1382         final List<File> filteredList = FileFilterUtils.filterList(filter, fileA, fileB);
1383 
1384         assertTrue(filteredList.contains(fileA));
1385         assertFalse(filteredList.contains(fileB));
1386     }
1387 
1388     /*
1389      * Test method for {@link FileFilterUtils#filterList(IOFileFilter, java.lang.Iterable)}
1390      * that tests {@code null} parameters and {@code null} elements
1391      * in the provided list.
1392      */
1393     @Test
1394     public void testFilterListNullParameters() {
1395         try {
1396             FileFilterUtils.filterList(null, Collections.<File>emptyList());
1397             fail();
1398         } catch (final IllegalArgumentException iae) {
1399             // Test passes, exception thrown for null filter
1400         }
1401 
1402         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1403         try {
1404             FileFilterUtils.filterList(filter, Arrays.asList((File) null));
1405             fail();
1406         } catch (final IllegalArgumentException iae) {
1407             // Test passes, exception thrown for list containing null
1408         }
1409 
1410         final List<File> filteredList = FileFilterUtils.filterList(filter, (List<File>)null);
1411         assertEquals(0, filteredList.size());
1412     }
1413 
1414     /*
1415      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, java.lang.Iterable)}
1416      * that tests that the method properly filters files from the set.
1417      */
1418     @Test
1419     public void testFilterSet() throws Exception {
1420         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1421         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1422         final Set<File> fileList = new HashSet<>(Arrays.asList(fileA, fileB));
1423 
1424         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1425 
1426         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, fileList);
1427 
1428         assertTrue(filteredSet.contains(fileA));
1429         assertFalse(filteredSet.contains(fileB));
1430     }
1431 
1432     /*
1433      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, File...)}
1434      * that tests that the method properly filters files from the set.
1435      */
1436     @Test
1437     public void testFilterSet_fromArray() throws Exception {
1438         final File fileA = TestUtils.newFile(getTestDirectory(), "A");
1439         final File fileB = TestUtils.newFile(getTestDirectory(), "B");
1440 
1441         final IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
1442 
1443         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, fileA, fileB);
1444 
1445         assertTrue(filteredSet.contains(fileA));
1446         assertFalse(filteredSet.contains(fileB));
1447     }
1448 
1449     /*
1450      * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, java.lang.Iterable)}
1451      * that tests {@code null} parameters and {@code null} elements
1452      * in the provided set.
1453      */
1454     @Test
1455    public void testFilterSetNullParameters() {
1456         try {
1457             FileFilterUtils.filterSet(null, Collections.<File>emptySet());
1458             fail();
1459         } catch (final IllegalArgumentException iae) {
1460             // Test passes, exception thrown for null filter
1461         }
1462 
1463         final IOFileFilter filter = FileFilterUtils.trueFileFilter();
1464         try {
1465             FileFilterUtils.filterSet(filter, new HashSet<>(Arrays.asList((File) null)));
1466             fail();
1467         } catch (final IllegalArgumentException iae) {
1468             // Test passes, exception thrown for set containing null
1469         }
1470 
1471         final Set<File> filteredSet = FileFilterUtils.filterSet(filter, (Set<File>)null);
1472         assertEquals(0, filteredSet.size());
1473     }
1474 
1475     @Test
1476     public void testEnsureTestCoverage() {
1477         assertNotNull(new FileFilterUtils()); // dummy for test coverage
1478     }
1479 
1480     @Test
1481     public void testNullFilters() {
1482         try {
1483             FileFilterUtils.toList((IOFileFilter) null);
1484             fail("Expected IllegalArgumentException");
1485         } catch (final IllegalArgumentException ignore) {
1486             // expected
1487         }
1488         try {
1489             FileFilterUtils.toList(new IOFileFilter[]{null});
1490             fail("Expected IllegalArgumentException");
1491         } catch (final IllegalArgumentException ignore) {
1492             // expected
1493         }
1494     }
1495 
1496     @Test
1497     public void testDelegation() { // TODO improve these tests
1498         assertNotNull(FileFilterUtils.asFileFilter((FileFilter) FalseFileFilter.INSTANCE));
1499         assertNotNull(FileFilterUtils.asFileFilter((FilenameFilter) FalseFileFilter.INSTANCE).toString());
1500     }
1501 }