1 26 27 package net.sourceforge.groboutils.util.datastruct.v1; 28 29 import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 36 43 public class PathRegistryUTest extends TestCase 44 { 45 private static final Class THIS_CLASS = PathRegistryUTest.class; 46 47 public PathRegistryUTest( String name ) 48 { 49 super( name ); 50 } 51 52 public static Test suite() 53 { 54 TestSuite suite = new TestSuite( THIS_CLASS ); 55 56 return suite; 57 } 58 59 public static void main( String [] args ) 60 { 61 String [] name = { THIS_CLASS.getName() }; 62 63 66 junit.textui.TestRunner.main( name ); 67 } 68 69 protected void setUp() throws Exception 70 { 71 super.setUp(); 72 73 } 75 76 77 protected void tearDown() throws Exception 78 { 79 81 super.tearDown(); 82 } 83 84 85 public void testInstantiate() 86 { 87 new PathRegistry( '.', true ); 88 new PathRegistry( '.', false ); 89 } 90 91 92 public void testAddCaseSensitiveNotRecursive() 93 throws PathAlreadyRegisteredException 94 { 95 PathRegistry pr = new PathRegistry( '/', true ); 96 97 notRegisteredTest( pr, "root" ); 99 100 Object o1 = new Object (); 101 pr.register( "root", o1, false, true ); 102 registeredRightTest( pr, "root", o1 ); 103 Object o2; 104 105 caseNotRegisteredTest( pr, "root", '/' ); 107 108 notRegisteredTest( pr, "root/empty" ); 110 111 notRegisteredTest( pr, "unknown" ); 113 114 notRegisteredTest( pr, "/unknown" ); 116 117 notRegisteredTest( pr, "root/deep" ); 119 Object o1a = new Object (); 120 pr.register( "root/deep", o1a, false, true ); 121 registeredRightTest( pr, "root/deep", o1a ); 122 123 caseNotRegisteredTest( pr, "root/deep", '/' ); 125 126 notRegisteredTest( pr, "root/empty" ); 128 notRegisteredTest( pr, "root/deep/empty" ); 129 130 notRegisteredTest( pr, "unknown" ); 132 } 133 134 135 public void testAddTwiceException() 136 throws PathAlreadyRegisteredException 137 { 138 PathRegistry pr = new PathRegistry( '/', true ); 139 Object o1 = new Object (); 140 notRegisteredTest( pr, "root" ); 141 142 pr.register( "root", o1, false, true ); 143 registeredRightTest( pr, "root", o1 ); 144 145 alreadyRegisteredTest( pr, "root" ); 147 } 148 149 150 public void testAddCaseInsensitiveNotRecursive() 151 throws PathAlreadyRegisteredException 152 { 153 PathRegistry pr = new PathRegistry( '/', true ); 154 notRegisteredTest( pr, "root" ); 155 156 Object o1 = new Object (); 157 pr.register( "root", o1, false, false ); 158 159 caseRegisteredRightTest( pr, "root", o1, '/' ); 161 162 caseNotRegisteredTest( pr, "root/unknown", '/' ); 164 } 165 166 167 public void testAddTwiceExceptionCaseInsensitive() 168 throws PathAlreadyRegisteredException 169 { 170 PathRegistry pr = new PathRegistry( '/', true ); 171 notRegisteredTest( pr, "root" ); 172 Object o1 = new Object (); 173 pr.register( "root", o1, false, false ); 174 registeredRightTest( pr, "root", o1 ); 175 176 caseAlreadyRegisteredTest( pr, "root", '/' ); 178 } 179 180 181 public void testAddCaseSensitiveRecursive() 182 throws PathAlreadyRegisteredException 183 { 184 PathRegistry pr = new PathRegistry( '/', true ); 185 notRegisteredTest( pr, "root" ); 186 Object o1 = new Object (); 187 pr.register( "root", o1, true, true ); 188 registeredRightTest( pr, "root", o1 ); 189 190 191 caseNotRegisteredTest( pr, "root", '/' ); 193 194 registeredRightTest( pr, "root/empty", o1 ); 196 197 notRegisteredTest( pr, "unknown" ); 199 200 notRegisteredTest( pr, "unknown/subpath" ); 202 } 203 204 205 public void testAddTwiceExceptionRecursive() 206 throws PathAlreadyRegisteredException 207 { 208 PathRegistry pr = new PathRegistry( '/', true ); 209 notRegisteredTest( pr, "root" ); 210 Object o1 = new Object (); 211 pr.register( "root", o1, true, true ); 212 registeredRightTest( pr, "root", o1 ); 213 214 alreadyRegisteredTest( pr, "root" ); 215 alreadyRegisteredTest( pr, "root/empty" ); 216 alreadyRegisteredTest( pr, "root/whatever" ); 217 alreadyRegisteredTest( pr, "root/empty/whatever" ); 218 } 219 220 221 public void testRemoveCaseSensitiveNotResursive() 222 throws PathAlreadyRegisteredException, 223 NoRegisteredComponentException 224 { 225 PathRegistry pr = new PathRegistry( '/', true ); 226 notRegisteredTest( pr, "root" ); 227 Object o1 = new Object (); 228 pr.register( "root", o1, true, true ); 229 registeredRightTest( pr, "root", o1 ); 230 231 cantRemoveEntryTest( pr, "unknown" ); 232 233 caseCantRemoveEntryTest( pr, "root", '/' ); 234 cantRemoveEntryTest( pr, "root/empty" ); 235 236 pr.remove( "root" ); 237 238 cantRemoveEntryTest( pr, "root" ); 239 } 240 241 242 243 246 247 250 protected void notRegisteredTest( PathRegistry pr, String path ) 251 { 252 Object o = pr.get( path ); 253 assertTrue( "Must return null on bad path and sub-path '"+path+ 254 "', but found ["+o+"].", o == null ); 255 } 256 257 258 262 protected void caseNotRegisteredTest( PathRegistry pr, String path, 263 char separator ) 264 { 265 String [] variations = getCaseVariations( path, separator ); 266 for (int i = 0; i < variations.length; i++) 267 { 268 if (!path.equals( variations[i] )) 269 { 270 notRegisteredTest( pr, variations[i] ); 271 } 272 } 273 } 274 275 276 279 protected void registeredRightTest( PathRegistry pr, String path, 280 Object expectedObj ) 281 { 282 Object o2 = pr.get( path ); 283 assertNotNull( "Path must be registered.", o2 ); 284 assertEquals( "What gets put in, needs to equal what gets put out.", 285 expectedObj, o2 ); 286 } 287 288 289 292 protected void caseRegisteredRightTest( PathRegistry pr, String path, 293 Object expectedObj, char separator ) 294 { 295 registeredRightTest( pr, path, expectedObj ); 296 String [] variations = getCaseVariations( path, separator ); 297 for (int i = 0; i < variations.length; i++) 298 { 299 registeredRightTest( pr, variations[i], expectedObj ); 300 } 301 } 302 303 304 309 protected void alreadyRegisteredTest( PathRegistry pr, String path ) 310 { 311 try 312 { 313 pr.register( path, new Object (), false, false ); 314 } 315 catch (PathAlreadyRegisteredException pare) 316 { 317 return; 319 } 320 fail( "PathRegistry should have thrown an exception" ); 321 } 322 323 324 328 protected void cantRemoveEntryTest( PathRegistry pr, String path ) 329 { 330 try 331 { 332 pr.remove( path ); 333 } 334 catch (NoRegisteredComponentException nrce) 335 { 336 return; 338 } 339 fail( "PathRegistry should have thrown an exception" ); 340 } 341 342 343 protected void caseCantRemoveEntryTest( PathRegistry pr, String path, 344 char separator ) 345 { 346 String [] variations = getCaseVariations( path, separator ); 347 for (int i = 0; i < variations.length; i++) 348 { 349 if (!path.equals( variations[i] )) 350 { 351 cantRemoveEntryTest( pr, variations[i] ); 352 } 353 } 354 } 355 356 359 protected void caseAlreadyRegisteredTest( PathRegistry pr, String path, 360 char separator ) 361 { 362 alreadyRegisteredTest( pr, path ); 363 String [] variations = getCaseVariations( path, separator ); 364 for (int i = 0; i < variations.length; i++) 365 { 366 alreadyRegisteredTest( pr, variations[i] ); 367 } 368 } 369 370 371 private static java.util.Random s_rand = new java.util.Random (); 372 private static synchronized int nextInt( int n ) 373 { 374 if (n <= 0) throw new IllegalArgumentException ("n must be > 0"); 376 377 if ((n & -n) == n) { 379 return (int)((n * ((long)s_rand.nextInt() & 0x7fffffffL) ) >> 31); 381 } 382 383 int bits, val; 384 do { 385 bits = s_rand.nextInt() & 0x7fffffff; 387 val = bits % n; 388 } while(bits - val + (n-1) < 0); 389 return val; 390 } 391 392 396 protected String [] getCaseVariations( String orig, char separator ) 397 { 398 orig = orig.toLowerCase(); 399 char buff[] = orig.toCharArray(); 400 int len = buff.length; 401 if (len <= 0) 402 { 403 return new String [0]; 404 } 405 if (len == 1) 406 { 407 return new String [] { orig, orig.toUpperCase() }; 408 } 409 410 java.util.Vector var = new java.util.Vector (); 411 for (int i = 0; i < len; i++) 412 { 413 int pos1 = nextInt( len ); 414 int pos2 = nextInt( len ); 415 char origChar1 = buff[ pos1 ]; 416 char origChar2 = buff[ pos2 ]; 417 if (origChar1 != separator) 418 buff[ pos1 ] = Character.toUpperCase( origChar1 ); 419 String s = new String ( buff ); 420 if (!s.equals( orig )) 421 var.addElement( s ); 422 if (origChar2 != separator) 423 buff[ pos2 ] = Character.toUpperCase( origChar2 ); 424 if (!s.equals( orig )) 425 var.addElement( s ); 426 if (origChar1 != separator) 427 buff[ pos1 ] = Character.toLowerCase( origChar1 ); 428 if (!s.equals( orig )) 429 var.addElement( s ); 430 if (origChar2 != separator) 431 buff[ pos2 ] = Character.toLowerCase( origChar2 ); 432 if (!s.equals( orig )) 433 var.addElement( s ); 434 buff[ pos1 ] = origChar1; 435 buff[ pos2 ] = origChar2; 436 } 437 String ss[] = new String [ var.size() ]; 438 var.copyInto( ss ); 439 return ss; 440 } 441 } 442 | Popular Tags |