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