1 8 package org.apache.avalon.excalibur.naming.test; 9 10 import junit.framework.AssertionFailedError; 11 import junit.framework.TestCase; 12 13 import javax.naming.Context; 14 import javax.naming.NameNotFoundException; 15 import javax.naming.NamingException; 16 import java.util.Enumeration; 17 18 24 public abstract class AbstractContextTestCase 25 extends TestCase 26 { 27 protected final static Object O1 = "iO1"; 28 protected final static Object O2 = "iO2"; 29 protected final static Object O3 = "iO3"; 30 protected final static Object O4 = "iO4"; 31 protected final static Object O5 = "iO5"; 32 protected final static Object O6 = "iO6"; 33 protected final static Object O7 = "iO7"; 34 protected final static Object O8 = "iO8"; 35 36 protected Context m_context; 37 38 public AbstractContextTestCase( String name ) 39 { 40 super( name ); 41 } 42 43 public void testBindToDirectContext() 44 throws AssertionFailedError 45 { 46 try 47 { 48 m_context.bind( "O1", O1 ); 49 assertTrue( "Make sure lookup returns correct object", 50 m_context.lookup("O1").equals( O1 ) ); 51 52 m_context.bind( "O2", O2 ); 53 m_context.bind( "O3", O3 ); 54 m_context.bind( "O4", O4 ); 55 m_context.bind( "O5", O5 ); 56 m_context.bind( "O6", O6 ); 57 m_context.bind( "O7", O7 ); 58 m_context.bind( "O8", O8 ); 59 60 assertTrue( "Make sure lookup O2 returns correct object", 61 m_context.lookup("O2").equals( O2 ) ); 62 assertTrue( "Make sure lookup O3 returns correct object", 63 m_context.lookup("O3").equals( O3 ) ); 64 assertTrue( "Make sure lookup O4 returns correct object", 65 m_context.lookup("O4").equals( O4 ) ); 66 assertTrue( "Make sure lookup O5 returns correct object", 67 m_context.lookup("O5").equals( O5 ) ); 68 assertTrue( "Make sure lookup O6 returns correct object", 69 m_context.lookup("O6").equals( O6 ) ); 70 assertTrue( "Make sure lookup O7 returns correct object", 71 m_context.lookup("O7").equals( O7 ) ); 72 assertTrue( "Make sure lookup O8 returns correct object", 73 m_context.lookup("O8").equals( O8 ) ); 74 } 75 catch( final NamingException ne ) 76 { 77 throw new AssertionFailedError( ne.toString() ); 78 } 79 } 80 81 public void testUnBindFromDirectContext() 82 throws AssertionFailedError 83 { 84 testBindToDirectContext(); 85 86 try 87 { 88 m_context.unbind("O1"); 89 m_context.unbind("O2"); 90 m_context.unbind("O3"); 91 m_context.unbind("O4"); 92 m_context.unbind("O5"); 93 m_context.unbind("O6"); 94 m_context.unbind("O7"); 95 m_context.unbind("O8"); 96 97 final Enumeration enum = m_context.list(""); 98 99 if( enum.hasMoreElements() ) 100 { 101 fail( "Failed to unbind all test elements: ie " + 102 enum.nextElement() ); 103 } 104 } 105 catch( final NamingException ne ) 106 { 107 throw new AssertionFailedError( ne.toString() ); 108 } 109 } 110 111 public void testBindToDirectSubContext() 112 throws AssertionFailedError 113 { 114 try 115 { 116 m_context.createSubcontext( "x" ); 117 m_context.bind( "x/O1", O1 ); 118 assertTrue( "Make sure lookup x/O1 returns correct object", 119 m_context.lookup("x/O1").equals( O1 ) ); 120 assertTrue( "Make sure lookup x/ returns correct object", 121 m_context.lookup("x/") instanceof Context ); 122 assertTrue( "Make sure lookup x returns correct object", 123 m_context.lookup("x") instanceof Context ); 124 125 m_context.bind( "x/O2", O2 ); 126 assertTrue( "Make sure lookup x/O2 returns correct object", 127 m_context.lookup("x/O2").equals( O2 ) ); 128 assertTrue( "Make sure lookup x/ returns correct object", 129 m_context.lookup("x/") instanceof Context ); 130 assertTrue( "Make sure lookup x returns correct object", 131 m_context.lookup("x") instanceof Context ); 132 133 m_context.bind( "x/O3", O3 ); 134 assertTrue( "Make sure lookup x/O3 returns correct object", 135 m_context.lookup("x/O3").equals( O3 ) ); 136 assertTrue( "Make sure lookup x/ returns correct object", 137 m_context.lookup("x/") instanceof Context ); 138 assertTrue( "Make sure lookup x returns correct object", 139 m_context.lookup("x") instanceof Context ); 140 } 141 catch( final NamingException ne ) 142 { 143 throw new AssertionFailedError( ne.toString() ); 144 } 145 } 146 147 public void testUnBindFromDirectSubContext() 148 throws AssertionFailedError 149 { 150 testBindToDirectSubContext(); 151 152 try 153 { 154 m_context.unbind("x/O1"); 155 m_context.unbind("x/O2"); 156 m_context.unbind("x/O3"); 157 158 final Enumeration enum = m_context.list("x/"); 159 160 if( enum.hasMoreElements() ) 161 { 162 fail( "Failed to unbind all test elements: ie " + 163 enum.nextElement() ); 164 } 165 166 m_context.unbind("a"); 168 m_context.unbind("x/a"); 169 } 170 catch( final NamingException ne ) 171 { 172 throw new AssertionFailedError( ne.toString() ); 173 } 174 } 175 176 public void testBindToArbitarySubContexts() 177 throws AssertionFailedError 178 { 179 try 180 { 181 m_context.createSubcontext( "x" ); 182 m_context.createSubcontext( "x/y" ); 183 m_context.bind( "x/y/O1", O1 ); 184 assertTrue( "Make sure lookup x/y/O1 returns correct object", 185 m_context.lookup("x/y/O1").equals( O1 ) ); 186 assertTrue( "Make sure lookup x/y/ returns correct object", 187 m_context.lookup("x/y/") instanceof Context ); 188 assertTrue( "Make sure lookup x/y returns correct object", 189 m_context.lookup("x/y") instanceof Context ); 190 assertTrue( "Make sure lookup x returns correct object", 191 m_context.lookup("x") instanceof Context ); 192 193 try 194 { 195 m_context.bind( "x/y", O2 ); 196 assertTrue("Bound object to directory x/y.", false ); 197 } 198 catch( final NamingException ne ) {} 199 200 try 201 { 202 m_context.bind( "x/y/", O2 ); 203 assertTrue("Bound object to directory x/y/.", false ); 204 } 205 catch( final NamingException ne ) {} 206 207 try 208 { 209 m_context.bind( "x/", O2 ); 210 assertTrue("Bound object to directory x/.", false ); 211 } 212 catch( final NamingException ne ) {} 213 214 try 215 { 216 m_context.bind( "x", O2 ); 217 assertTrue("Bound object to directory x.", false ); 218 } 219 catch( final NamingException ne ) {} 220 221 try 222 { 223 m_context.createSubcontext( "z" ); 224 m_context.bind( "z/", O2 ); 225 assertTrue("Bound object to empty name z/.", false ); 226 } 227 catch( final NamingException ne ) {} 228 } 229 catch( final NamingException ne ) 230 { 231 throw new AssertionFailedError( ne.toString() ); 232 } 233 } 234 235 public void testUnBindFromArbitarySubContext() 236 throws AssertionFailedError 237 { 238 testBindToArbitarySubContexts(); 239 240 try 241 { 242 m_context.unbind("x/y/O1"); 243 244 m_context.unbind("x/O2"); 246 m_context.unbind("x/O3"); 247 248 final Enumeration enum = m_context.list("x/y"); 249 250 if( enum.hasMoreElements() ) 251 { 252 fail( "Failed to unbind all test elements: ie " + 253 enum.nextElement() ); 254 } 255 256 265 266 m_context.unbind("a"); 268 m_context.unbind("x/a"); 269 } 270 catch( final NamingException ne ) 271 { 272 throw new AssertionFailedError( ne.toString() ); 273 } 274 } 275 276 public void testCreateSubContext() 277 throws AssertionFailedError 278 { 279 try 280 { 281 m_context.createSubcontext( "x" ); 282 assertTrue( "Make sure lookup x returns correct object", 283 m_context.lookup("x") instanceof Context ); 284 m_context.createSubcontext( "x/y" ); 285 assertTrue( "Make sure lookup x/y returns correct object", 286 m_context.lookup("x/y") instanceof Context ); 287 288 try 289 { 290 m_context.createSubcontext( "z/x/y" ); 291 assertTrue( "Created a subcontext when intermediate contexts not created", false ); 292 } 293 catch( final NamingException ne ) {} 294 295 try 296 { 297 m_context.createSubcontext( "x/y" ); 298 assertTrue("createSubContext when context alreadty exists.", false ); 299 } 300 catch( final NamingException ne ) {} 301 } 302 catch( final NamingException ne ) 303 { 304 throw new AssertionFailedError( ne.toString() ); 305 } 306 } 307 308 public void testDestroySubContext() 309 throws AssertionFailedError 310 { 311 testCreateSubContext(); 312 313 try 314 { 315 try 316 { 317 m_context.destroySubcontext( "x" ); 318 assertTrue("destroySubContext with existing subContexts.", false ); 319 } 320 catch( final NamingException ne ) {} 321 322 try 323 { 324 m_context.destroySubcontext( "x/y/" ); 325 assertTrue("destroySubContext with empty subContext name.", false ); 326 } 327 catch( final NamingException ne ) {} 328 329 m_context.destroySubcontext( "x/y" ); 330 m_context.destroySubcontext( "x" ); 331 332 try 333 { 334 m_context.lookup("z"); 335 assertTrue("subContext exists after delete.", false ); 336 } 337 catch( final NamingException ne ) {} 338 } 339 catch( final NamingException ne ) 340 { 341 throw new AssertionFailedError( ne.toString() ); 342 } 343 } 344 345 346 public void testRenameToDirectContext() 347 throws AssertionFailedError 348 { 349 try 350 { 351 m_context.bind( "O1", O1 ); 352 m_context.rename( "O1", "+O1" ); 353 assertTrue( "Make sure lookup not null", 354 m_context.lookup("+O1") != null ); 355 assertTrue( "Make sure lookup +O1 returns correct object", 356 m_context.lookup("+O1").equals( O1 ) ); 357 358 try 359 { 360 m_context.lookup("O1"); 361 assertTrue( "Old name still bound after rename", false ); 362 } 363 catch( final NameNotFoundException nnfe ) {} 364 365 m_context.bind( "O2", O2 ); 366 m_context.rename( "O2", "+O2" ); 367 assertTrue( "Make sure lookup not null", 368 m_context.lookup("+O2") != null ); 369 assertTrue( "Make sure lookup +O2 returns correct object", 370 m_context.lookup("+O2").equals( O2 ) ); 371 372 try 373 { 374 m_context.lookup("O2"); 375 assertTrue( "Old name O2 still bound after rename", false ); 376 } 377 catch( final NameNotFoundException nnfe ) {} 378 379 m_context.bind( "O3", O3 ); 380 m_context.rename( "O3", "+O3" ); 381 assertTrue( "Make sure lookup not null", 382 m_context.lookup("+O3") != null ); 383 assertTrue( "Make sure lookup +O3 returns correct object", 384 m_context.lookup("+O3").equals( O3 ) ); 385 try 386 { 387 m_context.lookup("O3"); 388 assertTrue( "Old name O3 still bound after rename", false ); 389 } 390 catch( final NameNotFoundException nnfe ) {} 391 392 m_context.bind( "O4", O4 ); 393 m_context.rename( "O4", "+O4" ); 394 assertTrue( "Make sure lookup not null", 395 m_context.lookup("+O4") != null ); 396 assertTrue( "Make sure lookup +04 returns correct object", 397 m_context.lookup("+O4").equals( O4 ) ); 398 399 try 400 { 401 m_context.lookup("O3"); 402 assertTrue( "Old name O3 still bound after rename", false ); 403 } 404 catch( final NameNotFoundException nnfe ) {} 405 } 406 catch( final NamingException ne ) 407 { 408 throw new AssertionFailedError( ne.toString() ); 409 } 410 } 411 412 public void testReBind() 413 throws AssertionFailedError 414 { 415 try 416 { 417 m_context.bind( "O1", O1 ); 418 assertTrue( "Make sure lookup returns correct object", 419 m_context.lookup("O1").equals( O1 ) ); 420 421 m_context.bind( "O2", O2 ); 422 m_context.bind( "O3", O3 ); 423 m_context.bind( "O4", O4 ); 424 m_context.bind( "O5", O5 ); 425 m_context.bind( "O6", O6 ); 426 m_context.bind( "O7", O7 ); 427 m_context.bind( "O8", O8 ); 428 assertTrue( "Make sure lookup O2 returns correct object", 429 m_context.lookup("O2").equals( O2 ) ); 430 assertTrue( "Make sure lookup O3 returns correct object", 431 m_context.lookup("O3").equals( O3 ) ); 432 assertTrue( "Make sure lookup O4 returns correct object", 433 m_context.lookup("O4").equals( O4 ) ); 434 assertTrue( "Make sure lookup O5 returns correct object", 435 m_context.lookup("O5").equals( O5 ) ); 436 assertTrue( "Make sure lookup O6 returns correct object", 437 m_context.lookup("O6").equals( O6 ) ); 438 assertTrue( "Make sure lookup O7 returns correct object", 439 m_context.lookup("O7").equals( O7 ) ); 440 assertTrue( "Make sure lookup O8 returns correct object", 441 m_context.lookup("O8").equals( O8 ) ); 442 } 443 catch( final NamingException ne ) 444 { 445 throw new AssertionFailedError( ne.toString() ); 446 } 447 448 try 449 { 450 m_context.rebind( "O1", O2 ); 451 assertTrue( "Rebind of O1 returns correct object", 452 m_context.lookup("O1").equals( O2 ) ); 453 454 m_context.rebind( "O2", O3 ); 455 m_context.rebind( "O3", O4 ); 456 m_context.rebind( "O4", O5 ); 457 m_context.rebind( "O5", O6 ); 458 m_context.rebind( "O6", O7 ); 459 m_context.rebind( "O7", O8 ); 460 m_context.rebind( "O8", O1 ); 461 assertTrue( "Rebind of O2 returns correct object", 462 m_context.lookup("O2").equals( O3 ) ); 463 assertTrue( "Rebind of O3 returns correct object", 464 m_context.lookup("O3").equals( O4 ) ); 465 assertTrue( "Rebind of O4 returns correct object", 466 m_context.lookup("O4").equals( O5 ) ); 467 assertTrue( "Rebind of O5 returns correct object", 468 m_context.lookup("O5").equals( O6 ) ); 469 assertTrue( "Rebind of O6 returns correct object", 470 m_context.lookup("O6").equals( O7 ) ); 471 assertTrue( "Rebind of O7 returns correct object", 472 m_context.lookup("O7").equals( O8 ) ); 473 assertTrue( "Rebind of O8 returns correct object", 474 m_context.lookup("O8").equals( O1 ) ); 475 476 m_context.bind( "x", O1 ); 477 assertTrue( "Make sure lookup x returns correct object", 478 m_context.lookup("x").equals( O1 ) ); 479 m_context.rebind( "x", O8 ); 480 assertTrue( "Rebind of x returns correct object", 481 m_context.lookup("x").equals( O8 ) ); 482 } 483 catch( final NamingException ne ) 484 { 485 throw new AssertionFailedError( ne.toString() ); 486 } 487 488 try 489 { 490 m_context.createSubcontext( "x" ); 491 m_context.rebind( "x/", O1 ); 492 assertTrue( "Able to rebind empty name", false ); 493 } 494 catch( final NamingException ne ) {} 495 } 496 } 497 | Popular Tags |