1 26 27 package net.sourceforge.groboutils.util.io.v1; 28 29 import java.io.File ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 42 public class ExtensionFilenameFilterUTest extends TestCase 43 { 44 private static final Class THIS_CLASS = ExtensionFilenameFilterUTest.class; 47 48 public ExtensionFilenameFilterUTest( String name ) 49 { 50 super( name ); 51 } 52 53 54 57 public void testInstantiate() 58 { 59 createFilter(); 60 createFilter( "" ); 61 createFilter( "a" ); 62 createFilter( ".a" ); 63 createFilter( ".A" ); 64 createFilter( true ); 65 createFilter( false ); 66 createFilter( new String [] { "" } ); 67 createFilter( new String [] { "B", "A" } ); 68 createFilter( "", true ); 69 createFilter( "", false ); 70 createFilter( "A", true ); 71 createFilter( "A", false ); 72 createFilter( new String [] { "A", "a" }, true ); 73 createFilter( new String [] { "A", "a" }, false ); 74 } 75 76 public void testInstantiateBad1() 77 { 78 try 79 { 80 createFilter( (String )null ); 81 fail("did not throw an IllegalArgumentException." ); 82 } 83 catch (IllegalArgumentException iae) 84 { } 85 } 86 87 public void testInstantiateBad2() 88 { 89 try 90 { 91 createFilter( (String [])null ); 92 fail("did not throw an IllegalArgumentException." ); 93 } 94 catch (IllegalArgumentException iae) 95 { } 96 } 97 98 public void testInstantiateBad3() 99 { 100 try 101 { 102 createFilter( new String [] { null } ); 103 fail("did not throw an IllegalArgumentException." ); 104 } 105 catch (IllegalArgumentException iae) 106 { } 107 } 108 109 public void testInstantiateBad4() 110 { 111 try 112 { 113 createFilter( new String [] { "", null } ); 114 fail("did not throw an IllegalArgumentException." ); 115 } 116 catch (IllegalArgumentException iae) 117 { } 118 } 119 120 public void testInstantiateBad5() 121 { 122 try 123 { 124 createFilter( new String [] { "a", null } ); 125 fail("did not throw an IllegalArgumentException." ); 126 } 127 catch (IllegalArgumentException iae) 128 { } 129 } 130 131 public void testInstantiateBad6() 132 { 133 try 134 { 135 createFilter( (String )null, true ); 136 fail("did not throw an IllegalArgumentException." ); 137 } 138 catch (IllegalArgumentException iae) 139 { } 140 } 141 142 public void testInstantiateBad7() 143 { 144 try 145 { 146 createFilter( (String [])null, false ); 147 fail("did not throw an IllegalArgumentException." ); 148 } 149 catch (IllegalArgumentException iae) 150 { } 151 } 152 153 public void testDefaultDirectorySetting() 154 { 155 assertTrue( 156 "Must allow directories by default.", 157 createFilter().allowsDirectories() ); 158 } 159 160 public void testNullAccept1() 161 { 162 try 163 { 164 createFilter( "a" ).accept( null, null ); 165 fail( "Did not throw IllegalArgumentException." ); 166 } 167 catch (IllegalArgumentException e) 168 { } 169 } 170 171 public void testNullAccept2() 172 { 173 try 174 { 175 createFilter( "a" ).accept( new File ( "b" ), null ); 176 fail( "Did not throw IllegalArgumentException." ); 177 } 178 catch (IllegalArgumentException e) 179 { } 180 } 181 182 183 public void testEmptyAccept() 184 { 185 noMatchFile( createFilter(), new File ( "a" ) ); 186 } 187 188 189 public void testAcceptCaseSensitive() 190 { 191 matchFile( createFilter( "A", false ), new File ( "2/A" ) ); 192 matchFile( createFilter( "A", false ), new File ( "2/.A" ) ); 193 matchFile( createFilter( "A", false ), new File ( "2/asdfA" ) ); 194 matchFile( createFilter( "A", false ), new File ( "2/asdf.A" ) ); 195 matchFile( createFilter( ".a", false ), new File ( "2/b.a" ) ); 196 matchFile( createFilter( ".a", false ), new File ( "2/.a" ) ); 197 matchFile( createFilter( ".a", false ), new File ( "2/asdf.a" ) ); 198 matchFile( createFilter( ".a", false ), new File ( "2/asdf.a.a" ) ); 199 matchFile( createFilter( ".abc", false ), new File ( "2/asdf.a.abc" ) ); 200 201 noMatchFile( createFilter( ".a", false ), new File ( "" ) ); 202 noMatchFile( createFilter( ".a", false ), new File ( "4/asdf" ) ); 203 noMatchFile( createFilter( ".a", false ), new File ( "4/asdf.A" ) ); 204 noMatchFile( createFilter( ".a", false ), new File ( "4/.A" ) ); 205 noMatchFile( createFilter( ".a", false ), new File ( "4/.a.A" ) ); 206 noMatchFile( createFilter( ".a", false ), new File ( "4/asdf.a.A" ) ); 207 noMatchFile( createFilter( ".a", false ), new File ( "4/asdf.a." ) ); 208 noMatchFile( createFilter( ".ABC", false ), new File ( "4/asdf.a.A" ) ); 209 noMatchFile( createFilter( ".ABC", false ), new File ( "4/asdf.a.AB" ) ); 210 noMatchFile( createFilter( ".ABC", false ), new File ( "4/asdf.a.AC" ) ); 211 } 212 213 214 215 public void testAcceptCaseInsensitive() 216 { 217 matchFile( createFilter( "A" ), new File ( "A" ) ); 218 matchFile( createFilter( "A" ), new File ( "asdfA" ) ); 219 matchFile( createFilter( "A" ), new File ( "asdf.A" ) ); 220 matchFile( createFilter( ".a" ), new File ( "b.a" ) ); 221 matchFile( createFilter( ".a" ), new File ( ".a" ) ); 222 matchFile( createFilter( ".a" ), new File ( "asdf.a" ) ); 223 matchFile( createFilter( ".a" ), new File ( "asdf.a.a" ) ); 224 matchFile( createFilter( ".a" ), new File ( "asdf.A" ) ); 225 matchFile( createFilter( ".a" ), new File ( ".A" ) ); 226 matchFile( createFilter( ".a" ), new File ( ".a.A" ) ); 227 matchFile( createFilter( ".a" ), new File ( "asdf.a.A" ) ); 228 229 matchFile( createFilter( "A" ), new File ( "B", "A" ) ); 230 matchFile( createFilter( "A" ), new File ( "B", "asdfA" ) ); 231 matchFile( createFilter( "A" ), new File ( "B", "asdf.A" ) ); 232 matchFile( createFilter( ".a" ), new File ( "a.b", "b.a" ) ); 233 matchFile( createFilter( ".a" ), new File ( "a.b", ".a" ) ); 234 matchFile( createFilter( ".a" ), new File ( "a.b", "asdf.a" ) ); 235 matchFile( createFilter( ".a" ), new File ( "a.b", "asdf.a.a" ) ); 236 matchFile( createFilter( ".a" ), new File ( "a.b", "asdf.A" ) ); 237 matchFile( createFilter( ".a" ), new File ( "a.b", ".A" ) ); 238 matchFile( createFilter( ".a" ), new File ( "a.b", ".a.A" ) ); 239 matchFile( createFilter( ".a" ), new File ( "a.b", "asdf.a.A" ) ); 240 241 matchFile( createFilter( "A", true ), new File ( "A" ) ); 242 matchFile( createFilter( "A", true ), new File ( "asdfA" ) ); 243 matchFile( createFilter( "A", true ), new File ( "asdf.A" ) ); 244 matchFile( createFilter( ".a", true ), new File ( "b.a" ) ); 245 matchFile( createFilter( ".a", true ), new File ( ".a" ) ); 246 matchFile( createFilter( ".a", true ), new File ( "asdf.a" ) ); 247 matchFile( createFilter( ".a", true ), new File ( "asdf.a.a" ) ); 248 matchFile( createFilter( ".a", true ), new File ( "asdf.A" ) ); 249 matchFile( createFilter( ".a", true ), new File ( ".A" ) ); 250 matchFile( createFilter( ".a", true ), new File ( ".a.A" ) ); 251 matchFile( createFilter( ".a", true ), new File ( "asdf.a.A" ) ); 252 253 noMatchFile( createFilter( ".a" ), new File ( "" ) ); 254 noMatchFile( createFilter( ".a" ), new File ( "asdf" ) ); 255 noMatchFile( createFilter( ".a" ), new File ( "asdf.a." ) ); 256 noMatchFile( createFilter( ".a" ), new File ( "asdf.aa" ) ); 257 noMatchFile( createFilter( ".ABC" ), new File ( "asdf.a.A" ) ); 258 noMatchFile( createFilter( ".ABC" ), new File ( "asdf.a.AB" ) ); 259 noMatchFile( createFilter( ".ABC" ), new File ( "asdf.a.AC" ) ); 260 noMatchFile( createFilter( ".ABC" ), new File ( "asdf.a.ACB" ) ); 261 noMatchFile( createFilter( ".ABC" ), new File ( "asdf.a.ABCD" ) ); 262 noMatchFile( createFilter( ".a", true ), new File ( "" ) ); 263 noMatchFile( createFilter( ".a", true ), new File ( "asdf" ) ); 264 noMatchFile( createFilter( ".a", true ), new File ( "asdf.a." ) ); 265 noMatchFile( createFilter( ".ABC", true ), new File ( "asdf.a.A" ) ); 266 noMatchFile( createFilter( ".ABC", true ), new File ( "asdf.a.AB" ) ); 267 noMatchFile( createFilter( ".ABC", true ), new File ( "asdf.a.AC" ) ); 268 noMatchFile( createFilter( ".ABC", true ), new File ( "asdf.a.ACB" ) ); 269 noMatchFile( createFilter( ".ABC", true ), new File ( "asdf.a.ABCD" ) ); 270 271 noMatchFile( createFilter( ".a" ), new File ( "a.a", "asdf" ) ); 272 noMatchFile( createFilter( ".a" ), new File ( "a.a", "asdf.a." ) ); 273 noMatchFile( createFilter( ".ABC" ), new File ( "a.a", "asdf.a.A" ) ); 274 noMatchFile( createFilter( ".ABC" ), new File ( "a.a", "asdf.a.AB" ) ); 275 noMatchFile( createFilter( ".ABC" ), new File ( "a.a", "asdf.a.AC" ) ); 276 noMatchFile( createFilter( ".a", true ), new File ( "" ) ); 277 noMatchFile( createFilter( ".a", true ), new File ( "a.a", "asdf" ) ); 278 noMatchFile( createFilter( ".a", true ), new File ( "a.a", "asdf.a." ) ); 279 noMatchFile( createFilter( ".ABC", true ), new File ( "a.ABC", "asdf.a.A" ) ); 280 noMatchFile( createFilter( ".ABC", true ), new File ( "a.ABC", "asdf.a.AB" ) ); 281 noMatchFile( createFilter( ".ABC", true ), new File ( "a.ABC", "asdf.a.AC" ) ); 282 } 283 284 285 public void testAcceptDirectories() 286 { 287 matchFile( createFilter( "A" ), new File ( "." ) ); 288 matchFile( createFilter( "A", false ), new File ( "." ) ); 289 matchFile( createFilter( "A", true ), new File ( "." ) ); 290 } 291 292 293 public void testDontAcceptDirectories() 294 { 295 ExtensionFilenameFilter eff = createFilter( "A" ); 296 eff.allowsDirectories( false ); 297 noMatchFile( eff, new File ( "." ) ); 298 299 eff = createFilter( "A", true ); 300 eff.allowsDirectories( false ); 301 noMatchFile( eff, new File ( "." ) ); 302 303 eff = createFilter( "A", false ); 304 eff.allowsDirectories( false ); 305 noMatchFile( eff, new File ( "." ) ); 306 } 307 308 309 310 protected void matchFile( ExtensionFilenameFilter eff, File f ) 311 { 312 File parent = getParentFile( f ); 313 String name = f.getName(); 314 assertTrue( 315 "Filter must accept parent '"+parent+"' and name '"+name+"'.", 316 eff.accept( parent, name ) ); 317 } 318 319 320 protected void noMatchFile( ExtensionFilenameFilter eff, File f ) 321 { 322 File parent = getParentFile( f ); 323 String name = f.getName(); 324 assertTrue( 325 "Filter must not accept parent '"+parent+"' and name '"+name+"'.", 326 !eff.accept( parent, name ) ); 327 } 328 329 330 protected ExtensionFilenameFilter createFilter() 331 { 332 return new ExtensionFilenameFilter(); 333 } 334 protected ExtensionFilenameFilter createFilter( String s ) 335 { 336 return new ExtensionFilenameFilter( s ); 337 } 338 protected ExtensionFilenameFilter createFilter( String s[] ) 339 { 340 return new ExtensionFilenameFilter( s ); 341 } 342 protected ExtensionFilenameFilter createFilter( boolean b ) 343 { 344 return new ExtensionFilenameFilter( b ); 345 } 346 protected ExtensionFilenameFilter createFilter( String s, boolean b ) 347 { 348 return new ExtensionFilenameFilter( s, b ); 349 } 350 protected ExtensionFilenameFilter createFilter( String s[], boolean b ) 351 { 352 return new ExtensionFilenameFilter( s, b ); 353 } 354 355 356 protected File getParentFile( File f ) 357 { 358 String pname = f.getParent(); 359 if (pname == null) 360 { 361 return null; 363 } 364 return new File ( pname ); 365 } 366 367 368 371 public static Test suite() 372 { 373 TestSuite suite = new TestSuite( THIS_CLASS ); 374 375 return suite; 376 } 377 378 public static void main( String [] args ) 379 { 380 String [] name = { THIS_CLASS.getName() }; 381 382 385 junit.textui.TestRunner.main( name ); 386 } 387 388 protected void setUp() throws Exception 389 { 390 super.setUp(); 391 392 } 394 395 396 protected void tearDown() throws Exception 397 { 398 400 401 super.tearDown(); 402 } 403 404 } 405 | Popular Tags |