1 28 package org.objectweb.carol.jtests.conform.basic.clients; 29 30 import java.util.Hashtable ; 31 32 import javax.naming.Binding ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NameClassPair ; 36 import javax.naming.NameNotFoundException ; 37 import javax.naming.NamingEnumeration ; 38 import javax.naming.NamingException ; 39 import javax.rmi.PortableRemoteObject ; 40 import javax.rmi.CORBA.Stub ; 41 42 import junit.framework.Test; 43 import junit.framework.TestCase; 44 import junit.framework.TestSuite; 45 46 import org.omg.CORBA.ORB ; 47 48 import org.objectweb.carol.jtests.conform.basic.server.BasicMultiObjectItf; 49 import org.objectweb.carol.jtests.conform.basic.server.BasicObjectItf; 50 import org.objectweb.carol.jtests.conform.basic.server.BasicRemoteObject; 51 import org.objectweb.carol.jtests.conform.basic.server.BasicSerializableObject; 52 import org.objectweb.carol.util.configuration.CarolDefaultValues; 53 import org.objectweb.carol.util.configuration.ConfigurationRepository; 54 55 61 public class MultiProtocolTests extends TestCase { 62 63 66 private String basicName = null; 67 68 71 private String basicMultiName = null; 72 73 76 private InitialContext ic = null; 77 78 81 private BasicObjectItf ba = null; 82 83 86 private BasicMultiObjectItf bma = null; 87 88 92 public MultiProtocolTests(String name) { 93 super(name); 94 } 95 96 100 public void setUp() throws Exception { 101 super.setUp(); 102 103 ConfigurationRepository.init(); 104 105 ic = new InitialContext (); 106 107 basicName = "basicname"; 109 basicMultiName = "basicmultiname"; 110 111 ba = (BasicObjectItf) PortableRemoteObject.narrow(ic.lookup(basicName), BasicObjectItf.class); 113 bma = (BasicMultiObjectItf) PortableRemoteObject.narrow(ic.lookup(basicMultiName), BasicMultiObjectItf.class); 114 } 115 116 120 public void tearDown() throws Exception { 121 basicName = null; 122 basicMultiName = null; 123 ba = null; 124 bma = null; 125 ic.close(); 126 super.tearDown(); 127 } 128 129 133 public void testString() { 134 try { 135 String expected = ba.getString(); 136 assertEquals(expected, "string"); 137 } catch (Exception e) { 138 e.printStackTrace(); 139 fail("Can't get string" + e); 140 } 141 } 142 143 148 public void testMultiString() { 149 try { 150 String expected = bma.getMultiString(); 151 assertEquals(expected, "multi string call: " + "string"); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 fail("Can't get multi string" + e); 155 } 156 } 157 158 161 public void testReferenceString() { 162 try { 163 String expected = bma.getBasicRefString(); 164 assertEquals(expected, "string2"); 165 } catch (Exception e) { 166 e.printStackTrace(); 167 fail("Can't get ref string" + e); 168 } 169 } 170 171 175 public void testStub() { 176 try { 177 BasicObjectItf ob = (BasicObjectItf) PortableRemoteObject 178 .narrow(bma.getBasicObject(), BasicObjectItf.class); 179 String expected = ob.getString(); 180 assertEquals(expected, "string"); 181 } catch (Exception e) { 182 e.printStackTrace(); 183 fail("Can't narrow Remote Object :" + e); 184 } 185 } 186 187 190 public void testSerializable() { 191 BasicSerializableObject bso = new BasicSerializableObject("test1"); 192 BasicSerializableObject bsoResult = null; 193 try { 194 ic.bind("testSerializable", bso); 195 } catch (Exception e) { 196 fail("Can't bind object : " + e); 197 } 198 Object lookupObj = null; 199 try { 200 lookupObj = ic.lookup("testSerializable"); 201 } catch (Exception e) { 202 fail("Can't lookup object : " + e); 203 } 204 205 try { 206 bsoResult = (BasicSerializableObject) PortableRemoteObject.narrow(lookupObj, BasicSerializableObject.class); 207 } catch (Exception e) { 208 fail("Can't narrow object : " + e); 209 } 210 211 assertTrue(bso.equals(bsoResult)); 212 213 } 214 215 218 public void testContextCommonContextMethods() { 219 try { 220 ic.bind("testContextCommonContextMethods", bma); 221 } catch (Exception e) { 222 fail("Can't bind object : " + e); 223 } 224 Object lookupObj = null; 225 try { 226 lookupObj = ic.lookup("testContextCommonContextMethods"); 227 } catch (Exception e) { 228 fail("Can't lookup object : " + e); 229 } 230 231 try { 233 ic.rebind("testContextCommonContextMethods", bma); 234 } catch (Exception e) { 235 fail("Can't rebind object : " + e); 236 } 237 238 try { 240 ic.unbind("testContextCommonContextMethods"); 241 } catch (Exception e) { 242 fail("Can't unbind object : " + e); 243 } 244 245 try { 247 lookupObj = ic.lookup("testContextCommonContextMethods"); 248 fail("lookup of the object should fail as the object has been unbind"); 249 } catch (Exception e) { 250 assertTrue("exception is : " + e.getMessage(), NamingException .class.isAssignableFrom(e.getClass())); 251 } 252 253 BasicSerializableObject bso = new BasicSerializableObject("testContextCommonContextMethods2"); 255 try { 256 ic.bind("testContextCommonContextMethods2", bso); 257 ic.rename("testContextCommonContextMethods2", "testContextCommonContextMethods2renamed"); 258 } catch (Exception e) { 259 fail("Can't bind object : " + e); 260 } 261 try { 263 lookupObj = ic.lookup("testContextCommonContextMethods2"); 264 fail("lookup of the object should fail as the object has been renamed"); 265 } catch (Exception e) { 266 assertTrue("exception is : " + e.getMessage(), NamingException .class.isAssignableFrom(e.getClass())); 267 } 268 try { 269 lookupObj = ic.lookup("testContextCommonContextMethods2renamed"); 270 BasicSerializableObject bsoResult = (BasicSerializableObject) PortableRemoteObject.narrow(lookupObj, 271 BasicSerializableObject.class); 272 assertTrue(bso.equals(bsoResult)); 273 } catch (Exception e) { 274 fail("lookup of the object should not fail as a previous object has been renamed"); 275 } 276 277 try { 279 ic.bind("testContextCommonContextMethods4", bso); 280 NamingEnumeration ne = ic.list(""); 281 boolean found = false; 282 while (ne.hasMore() && !found) { 283 NameClassPair ncp = (NameClassPair ) ne.next(); 284 String n = ncp.getName(); 285 if (n.equals("testContextCommonContextMethods4")) { 286 found = true; 287 } 288 } 289 if (!found) { 290 fail("object bind was not find in ic.list(\"\")"); 291 } 292 293 ne = ic.listBindings(""); 295 found = false; 296 while (ne.hasMore() && !found) { 297 Binding binding = (Binding ) ne.next(); 298 String n = binding.getName(); 299 if (n.equals("testContextCommonContextMethods4")) { 300 found = true; 301 } 302 } 303 if (!found) { 304 fail("object bind was not find in ic.listBindings(\"\")"); 305 } 306 307 ic.unbind("testContextCommonContextMethods4"); 309 } catch (Exception e) { 310 fail("list() method fails"); 311 } 312 313 try { 315 ic.unbind("testContextCommonContextMethods4"); 316 } catch (Exception e) { 317 assertTrue(e instanceof NameNotFoundException ); 318 } 319 320 } 321 322 326 public void testPortableRemoteObject() { 327 BasicRemoteObject bro = new BasicRemoteObject("testPortableRemoteObject"); 328 329 try { 331 PortableRemoteObject.exportObject(bro); 332 } catch (Exception e) { 333 e.printStackTrace(); 334 fail("Cannot export object '" + bro + "' : " + e.getMessage()); 335 } 336 337 try { 339 Object remoteVal = PortableRemoteObject.toStub(bro); 340 341 if (remoteVal instanceof Stub ) { 344 ((Stub ) remoteVal).connect(ORB.init(new String [0], null)); 345 } 346 } catch (Exception e) { 347 e.printStackTrace(); 348 fail("Cannot use toStub() method on object '" + bro + "' : " + e.getMessage()); 349 } 350 351 try { 353 PortableRemoteObject.unexportObject(bro); 354 } catch (Exception e) { 355 e.printStackTrace(); 356 fail("Cannot unexportObject object '" + bro + "' : " + e.getMessage()); 357 } 358 359 try { 361 Object o = ic.lookup("basicmultiname"); 362 PortableRemoteObject.narrow(o, BasicMultiObjectItf.class); 363 } catch (Exception e) { 364 e.printStackTrace(); 365 fail("Cannot narrow object '" + bro + "'."); 366 } 367 368 } 369 370 377 public void testCompositeNameId() { 378 379 final String id1 = "objects/testCompositeNameId1"; 380 final String id2 = "objects/testCompositeNameId2"; 381 382 BasicSerializableObject bso1 = new BasicSerializableObject(id1); 383 BasicSerializableObject bso2 = new BasicSerializableObject(id2); 384 385 try { 387 ic.bind(id1, bso1); 388 } catch (Exception e) { 389 fail("Can't bind object 1: " + e); 390 } 391 392 try { 393 ic.bind(id2, bso2); 394 } catch (Exception e) { 395 fail("Can't bind object 1: " + e); 396 } 397 398 BasicSerializableObject bsoResult1 = null; 400 BasicSerializableObject bsoResult2 = null; 401 402 try { 403 bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(ic.lookup(id1), 404 BasicSerializableObject.class); 405 bsoResult2 = (BasicSerializableObject) PortableRemoteObject.narrow(ic.lookup(id2), 406 BasicSerializableObject.class); 407 } catch (Exception e) { 408 fail("Can't lookup object : " + e); 409 } 410 411 assertFalse("Should be different : obj1 = " + bsoResult1 + ", obj2 = " + bsoResult2 + ".", bsoResult1 413 .equals(bsoResult2)); 414 415 try { 417 NamingEnumeration ne = ic.list(""); 418 boolean found = false; 419 boolean found1 = false; 420 boolean found2 = false; 421 String listNames = ""; 422 while (ne.hasMore() && !found) { 423 NameClassPair ncp = (NameClassPair ) ne.next(); 424 String n = ncp.getName(); 425 listNames += n + " : "; 426 if (n.equals(id1)) { 427 found1 = true; 428 continue; 429 } 430 if (n.equals(id2)) { 431 found2 = true; 432 continue; 433 } 434 435 found = found1 && found2; 436 } 437 if (!found1) { 438 fail("object named " + id1 + " was not find in the list. Names found were : " + listNames); 439 } 440 if (!found2) { 441 fail("object named " + id2 + " was not find in the list. Names found were : " + listNames); 442 } 443 444 } catch (Exception e) { 445 fail("Can't list registry : " + e); 446 } 447 448 try { 450 ic.unbind(id1); 451 ic.unbind(id2); 452 } catch (Exception e) { 453 fail("Can't unbind object : " + e); 454 } 455 456 } 457 458 461 public void testJavaCompEnvironment() { 462 463 String tstId = "testJavaEnvironment"; 464 try { 466 ic.bind("java:comp/env/testJavaEnvironment", tstId); 467 } catch (Exception e) { 468 fail("Can't bind java:comp/env/testJavaEnvironment " + e); 469 } 470 471 Context javaComp = null; 472 try { 473 javaComp = (Context ) ic.lookup("java:comp/"); 474 } catch (NamingException e) { 475 fail("Cannot lookup java:comp : " + e); 476 } 477 478 try { 479 javaComp.lookup("env"); 480 } catch (NamingException e) { 481 fail("Cannot lookup javaComp.lookup(\"env\") : " + e); 482 } 483 484 Context javaCompEnv = null; 485 try { 486 javaCompEnv = (Context ) ic.lookup("java:comp/env"); 487 } catch (NamingException e) { 488 fail("Cannot lookup java:comp/env : " + e); 489 } 490 491 try { 492 javaCompEnv.createSubcontext("ejb"); 493 } catch (NamingException e) { 494 fail("Cannot use lookup javaCompEnv.createSubcontext(\"ejb\") " + e); 495 } 496 497 String resId = null; 499 try { 500 resId = (String ) ic.lookup("java:comp/env/testJavaEnvironment"); 501 } catch (Exception e) { 502 fail("Can't lookup object java:comp/env/testJavaEnvironment : " + e); 503 } 504 505 assertEquals(tstId, resId); 506 507 } 508 509 512 public void testSingleInitialContext() { 513 Hashtable testEnv = new Hashtable (); 514 String providerURL = ConfigurationRepository.getCurrentConfiguration().getProviderURL(); 515 testEnv.put(Context.PROVIDER_URL, providerURL); 516 517 Context tmpContext = null; 518 try { 519 tmpContext = new InitialContext (testEnv); 520 } catch (NamingException e) { 521 e.printStackTrace(); 522 fail("Cannot build a new initial context with provider url = " + providerURL); 523 } 524 525 String id = "testSingleInitialContext"; 526 BasicSerializableObject bso1 = new BasicSerializableObject(id); 527 try { 529 tmpContext.bind(id, bso1); 530 } catch (Exception e) { 531 fail("Can't bind object 1: " + e); 532 } 533 534 BasicSerializableObject bsoResult1 = null; 536 try { 537 bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(tmpContext.lookup(id), 538 BasicSerializableObject.class); 539 } catch (Exception e) { 540 fail("Can't lookup object : " + e); 541 } 542 543 assertTrue("Should be the same", bso1.equals(bsoResult1)); 545 546 Hashtable testEnv2 = new Hashtable (); 548 String initFactory = ConfigurationRepository.getCurrentConfiguration().getProtocol().getInitialContextFactoryClassName(); 549 testEnv2.put(Context.PROVIDER_URL, providerURL); 550 testEnv2.put(Context.INITIAL_CONTEXT_FACTORY, initFactory); 551 552 Context tmpContext2 = null; 553 try { 554 tmpContext2 = new InitialContext (testEnv2); 555 } catch (NamingException e) { 556 e.printStackTrace(); 557 fail("Cannot build a new initial context with provider url = " + providerURL + " and factory = " 558 + initFactory); 559 } 560 561 String id2 = "testSingleInitialContext2"; 562 BasicSerializableObject bso2 = new BasicSerializableObject(id); 563 try { 565 tmpContext2.bind(id2, bso2); 566 } catch (Exception e) { 567 fail("Can't bind object 1: " + e); 568 } 569 570 BasicSerializableObject bsoResult2 = null; 572 try { 573 bsoResult2 = (BasicSerializableObject) PortableRemoteObject.narrow(tmpContext2.lookup(id2), 574 BasicSerializableObject.class); 575 } catch (Exception e) { 576 fail("Can't lookup object : " + e); 577 } 578 579 assertTrue("Should be the same", bso2.equals(bsoResult2)); 581 582 Hashtable env3 = new Hashtable (); 584 env3.put(Context.PROVIDER_URL, "dummy://123.123.123.123:9876"); 585 try { 586 new InitialContext (env3); 587 fail("The context should fail with invalid properties"); 588 } catch (NamingException e) { 589 e.printStackTrace(); 591 } 592 593 } 594 595 599 public void testIctxEnvironment() { 600 Hashtable testEnv = null; 601 602 try { 603 testEnv = ic.getEnvironment(); 604 } catch (NamingException e) { 605 e.printStackTrace(); 606 fail("Cannot get environment"); 607 } 608 609 Context newIctx = null; 610 try { 611 newIctx = new InitialContext (testEnv); 612 } catch (NamingException e) { 613 e.printStackTrace(); 614 fail("Cannot get ICTX environment"); 615 } 616 617 try { 619 assertTrue(newIctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY).equals( 620 CarolDefaultValues.MULTI_JNDI)); 621 } catch (NamingException ne) { 622 ne.printStackTrace(); 623 fail("Should be able to get the environment : " + ne.getMessage()); 624 } 625 626 String id = "testIctxEnvironment"; 627 BasicSerializableObject bso1 = new BasicSerializableObject(id); 628 try { 630 newIctx.bind(id, bso1); 631 } catch (Exception e) { 632 fail("Can't bind object 1: " + e); 633 } 634 635 BasicSerializableObject bsoResult1 = null; 637 try { 638 bsoResult1 = (BasicSerializableObject) PortableRemoteObject.narrow(newIctx.lookup(id), 639 BasicSerializableObject.class); 640 } catch (Exception e) { 641 fail("Can't lookup object : " + e); 642 } 643 644 assertTrue("Should be the same", bso1.equals(bsoResult1)); 646 647 } 648 649 653 public static Test suite() { 654 return new TestSuite(MultiProtocolTests.class); 655 659 } 660 } | Popular Tags |