1 package org.jacorb.test.orb.dynany; 2 3 22 23 import junit.framework.*; 24 import junit.extensions.TestSetup; 25 import org.jacorb.test.common.ORBSetup; 26 27 33 34 public class DynAnyBaseTest extends TestCase 35 { 36 private static org.omg.DynamicAny.DynAnyFactory factory = null; 37 private static org.omg.CORBA.ORB orb = null; 38 39 40 public DynAnyBaseTest (String name) 41 { 42 super (name); 43 } 44 45 46 public static Test suite () 47 { 48 TestSuite suite = new TestSuite ("DynAny Base Tests"); 49 Setup setup = new Setup (suite); 50 ORBSetup osetup = new ORBSetup (setup); 51 52 suite.addTest (new DynAnyBaseTest ("testFactoryCreateFromAny")); 53 suite.addTest (new DynAnyBaseTest ("testFactoryCreateFromTypeCode")); 54 suite.addTest (new DynAnyBaseTest ("testFactoryInconsistentTypeCodeEx")); 55 suite.addTest (new DynAnyBaseTest ("testDynAnyLocalityConstraint")); 56 suite.addTest (new DynAnyBaseTest ("testCompareDynAny")); 57 suite.addTest (new DynAnyBaseTest ("testAccessBasicValue")); 58 suite.addTest (new DynAnyBaseTest ("testAccessTypeMismatchEx")); 59 suite.addTest (new DynAnyBaseTest ("testDynAnyTypeCode")); 60 suite.addTest (new DynAnyBaseTest ("testInitDynAnyFromDynAny")); 61 suite.addTest (new DynAnyBaseTest ("testInitFromDynAnyTypeMismatchEx")); 62 suite.addTest (new DynAnyBaseTest ("testInitDynAnyFromAny")); 63 suite.addTest (new DynAnyBaseTest ("testInitFromAnyTypeMismatchEx")); 64 suite.addTest (new DynAnyBaseTest ("testGenerateAnyFromDynAny")); 65 suite.addTest (new DynAnyBaseTest ("testDestroyDynAny")); 66 suite.addTest (new DynAnyBaseTest ("testCopyDynAny")); 67 suite.addTest (new DynAnyBaseTest ("testIterateDynAny")); 68 69 return osetup; 70 } 71 72 73 77 public void testFactoryCreateFromAny () 78 { 79 org.omg.CORBA.Any any = null; 80 81 any = orb.create_any (); 82 any.insert_long (700); 83 84 createDynAnyFromAny (any); 86 } 87 88 89 93 public void testFactoryCreateFromTypeCode () 94 { 95 org.omg.CORBA.TypeCode tc = null; 96 97 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 98 99 createDynAnyFromTypeCode (tc); 101 } 102 103 104 108 public void testFactoryInconsistentTypeCodeEx () 109 { 110 String msg; 111 org.omg.CORBA.Any any = null; 112 org.omg.CORBA.TypeCode tc = null; 113 org.omg.DynamicAny.DynAny dynAny = null; 114 115 any = orb.create_any (); 116 any.type (orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_Principal)); 117 118 msg = "Failed to throw InconsistentTypeCode exception when "; 120 msg += "creating DynAny from Any with TypeCode tk_Principal"; 121 try 122 { 123 dynAny = factory.create_dyn_any (any); 124 125 fail (msg); 126 } 127 catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode ex) 128 { 129 } 131 } 132 133 134 137 public void testDynAnyLocalityConstraint () 138 { 139 String msg; 140 org.omg.CORBA.TypeCode tc = null; 141 org.omg.DynamicAny.DynAny dynAny = null; 142 143 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 144 dynAny = createDynAnyFromTypeCode (tc); 145 146 msg = "MARSHAL system exception not raised by ORB::object_to_string "; 147 msg += "operation"; 148 try 149 { 150 orb.object_to_string (dynAny); 151 152 fail (msg); 153 } 154 catch (org.omg.CORBA.MARSHAL ex) 155 { 156 } 158 } 159 160 161 164 public void testCompareDynAny () 165 { 166 String msg; 167 org.omg.CORBA.Any any = null; 168 org.omg.DynamicAny.DynAny dynAny = null; 169 org.omg.DynamicAny.DynAny dynAny2 = null; 170 171 any = orb.create_any (); 172 any.insert_long (700); 173 dynAny = createDynAnyFromAny (any); 174 dynAny2 = createDynAnyFromAny (any); 175 176 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 177 assertTrue (msg, dynAny.equal (dynAny2)); 178 } 179 180 181 184 public void testAccessBasicValue () 185 { 186 String msg; 187 int intVal; 188 int intVal2; 189 org.omg.CORBA.TypeCode tc = null; 190 org.omg.DynamicAny.DynAny dynAny = null; 191 192 intVal = 700; 193 intVal2 = 0; 194 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 195 dynAny = createDynAnyFromTypeCode (tc); 196 197 try 198 { 199 dynAny.insert_long (intVal); 200 } 201 catch (Throwable ex) 202 { 203 fail ("Failed to insert value into DynAny object: " + ex); 204 } 205 206 try 207 { 208 intVal2 = dynAny.get_long (); 209 } 210 catch (Throwable ex) 211 { 212 fail ("Failed to extract value from DynAny object: " + ex); 213 } 214 215 msg = "Value inserted into DynAny object is not equal to value "; 216 msg += "extracted from same DynAny object"; 217 assertEquals (msg, intVal, intVal2); 218 } 219 220 221 226 public void testAccessTypeMismatchEx () 227 { 228 String msg; 229 org.omg.CORBA.TypeCode tc = null; 230 org.omg.DynamicAny.DynAny dynAny = null; 231 232 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string); 233 dynAny = createDynAnyFromTypeCode (tc); 234 235 msg = "TypeMismatch exception not thrown by insert operation when "; 236 msg += "insert operation is not valid for TypeCode of DynAny"; 237 try 238 { 239 dynAny.insert_long (700); 240 241 fail (msg); 242 } 243 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 244 { 245 } 247 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 248 { 249 fail (msg + ": " + ex); 250 } 251 252 msg = "TypeMismatch exception not thrown by get operation when "; 253 msg += "get operation is not valid for TypeCode of DynAny"; 254 try 255 { 256 dynAny.get_long (); 257 } 258 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 259 { 260 } 262 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 263 { 264 fail (msg + ": " + ex); 265 } 266 } 267 268 269 272 public void testDynAnyTypeCode () 273 { 274 String msg; 275 org.omg.CORBA.TypeCode tc = null; 276 org.omg.DynamicAny.DynAny dynAny = null; 277 278 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 279 dynAny = createDynAnyFromTypeCode (tc); 280 281 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 282 assertTrue (msg, dynAny.type ().equal (tc)); 283 } 284 285 286 289 public void testInitDynAnyFromDynAny () 290 { 291 String msg; 292 org.omg.CORBA.Any any = null; 293 org.omg.CORBA.TypeCode tc = null; 294 org.omg.DynamicAny.DynAny dynAny = null; 295 org.omg.DynamicAny.DynAny dynAny2 = null; 296 297 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 298 dynAny = createDynAnyFromTypeCode (tc); 299 300 any = orb.create_any (); 301 any.insert_long (700); 302 dynAny2 = createDynAnyFromAny (any); 303 304 msg = "Failed to initialize a DynAny object from another DynAny "; 305 msg += "object using the DynAny::assign operation"; 306 try 307 { 308 dynAny.assign (dynAny2); 309 } 310 catch (Throwable ex) 311 { 312 fail (msg + ": " + ex); 313 } 314 assertTrue (msg, dynAny.equal (dynAny2)); 315 } 316 317 318 322 public void testInitFromDynAnyTypeMismatchEx () 323 { 324 String msg; 325 org.omg.CORBA.Any any = null; 326 org.omg.CORBA.TypeCode tc = null; 327 org.omg.DynamicAny.DynAny dynAny = null; 328 org.omg.DynamicAny.DynAny dynAny2 = null; 329 330 any = orb.create_any (); 331 any.insert_long (700); 332 dynAny = createDynAnyFromAny (any); 333 334 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string); 335 dynAny2 = createDynAnyFromTypeCode (tc); 336 337 msg = "TypeMismatch exception not thrown by DynAny::assign "; 338 msg += "operation when DynAny operands have different types"; 339 try 340 { 341 dynAny.assign (dynAny2); 342 343 fail (msg); 344 } 345 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 346 { 347 } 349 } 350 351 352 355 public void testInitDynAnyFromAny () 356 { 357 String msg; 358 org.omg.CORBA.Any any = null; 359 org.omg.CORBA.TypeCode tc = null; 360 org.omg.DynamicAny.DynAny dynAny = null; 361 org.omg.DynamicAny.DynAny dynAny2 = null; 362 363 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 364 dynAny = createDynAnyFromTypeCode (tc); 365 366 any = orb.create_any (); 367 any.insert_long (700); 368 dynAny2 = createDynAnyFromAny (any); 369 370 msg = "Failed to initialize a DynAny object from an Any object "; 371 msg += "using the DynAny::from_any operation"; 372 try 373 { 374 dynAny.from_any (any); 375 } 376 catch (Throwable ex) 377 { 378 fail (msg + ": " + ex); 379 } 380 assertTrue (msg, dynAny.equal (dynAny2)); 381 } 382 383 384 388 public void testInitFromAnyTypeMismatchEx () 389 { 390 String msg; 391 org.omg.CORBA.Any any = null; 392 org.omg.CORBA.TypeCode tc = null; 393 org.omg.DynamicAny.DynAny dynAny = null; 394 395 any = orb.create_any (); 396 any.insert_long (700); 397 398 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_string); 399 dynAny = createDynAnyFromTypeCode (tc); 400 401 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 402 msg += "operation when DynAny and Any operands have different types"; 403 try 404 { 405 dynAny.from_any (any); 406 407 fail (msg); 408 } 409 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 410 { 411 } 413 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 414 { 415 fail (msg + ": " + ex); 416 } 417 } 418 419 420 423 public void testGenerateAnyFromDynAny () 424 { 425 String msg; 426 org.omg.CORBA.Any any = null; 427 org.omg.CORBA.TypeCode tc = null; 428 org.omg.DynamicAny.DynAny dynAny = null; 429 org.omg.DynamicAny.DynAny dynAny2 = null; 430 431 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 432 dynAny = createDynAnyFromTypeCode (tc); 433 434 any = orb.create_any (); 435 any = dynAny.to_any (); 436 dynAny2 = createDynAnyFromAny (any); 437 438 msg = "The DynAny::to_any operation failed to create an Any "; 439 msg += "object with the same value as the DynAny object"; 440 assertTrue (msg, dynAny.equal (dynAny2)); 441 } 442 443 444 447 public void testDestroyDynAny () 448 { 449 String msg; 450 org.omg.CORBA.Any any = null; 451 org.omg.DynamicAny.DynAny dynAny = null; 452 453 any = orb.create_any (); 454 any.insert_long (700); 455 dynAny = createDynAnyFromAny (any); 456 dynAny.destroy (); 457 458 try 459 { 460 dynAny.type (); 461 462 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 463 msg += "calling DynAny::type operation on a destroyed DynAny object "; 464 msg += "did not raise OBJECT_NOT_EXIST exception"; 465 fail (msg); 466 } 467 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 468 { 469 } 471 472 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 473 msg += "calling DynAny::current_component operation on a destroyed "; 474 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 475 try 476 { 477 dynAny.current_component (); 478 479 fail (msg); 480 } 481 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 482 { 483 } 485 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 486 { 487 fail (msg + ": " + ex); 488 } 489 } 490 491 492 495 public void testCopyDynAny () 496 { 497 String msg; 498 org.omg.CORBA.TypeCode tc = null; 499 org.omg.DynamicAny.DynAny dynAny = null; 500 org.omg.DynamicAny.DynAny dynAny2 = null; 501 502 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 503 dynAny = createDynAnyFromTypeCode (tc); 504 505 try 506 { 507 dynAny.insert_long (700); 508 } 509 catch (Throwable ex) 510 { 511 fail ("Failed to insert value into DynAny object: " + ex); 512 } 513 dynAny2 = dynAny.copy (); 514 515 msg = "The DynAny object created with the DynAny::copy operation "; 516 msg += "is not equal to the DynAny object it was copied from"; 517 assertTrue (msg, dynAny.equal (dynAny2)); 518 } 519 520 521 524 public void testIterateDynAny () 525 { 526 String msg; 527 int compCount = -1; 528 boolean seek; 529 org.omg.CORBA.TypeCode tc = null; 530 org.omg.DynamicAny.DynAny dynAny = null; 531 532 tc = orb.get_primitive_tc (org.omg.CORBA.TCKind.tk_long); 533 dynAny = createDynAnyFromTypeCode (tc); 534 535 try 537 { 538 compCount = dynAny.component_count (); 539 } 540 catch (Throwable ex) 541 { 542 fail ("Unexpected error raised by DynAny::component_count operation"); 544 } 545 msg = "The number of components returned from the "; 546 msg += "DynAny::component_count operation is incorrect"; 547 assertEquals (msg, 0, compCount); 548 549 msg = "The DynAny::seek operation indicates that a valid component "; 551 msg += "exists but the DynAny should have no components"; 552 seek = dynAny.seek (0); 553 assertTrue (msg, !seek); 554 555 try 557 { 558 dynAny = dynAny.current_component (); 559 560 msg = "A TypeMismatch exception was not raised by the "; 561 msg += "DynAny::current_component operation when trying to access "; 562 msg += "the current component of a DynAny with no components"; 563 fail (msg); 564 } 565 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 566 { 567 } 569 } 570 571 572 private static class Setup extends TestSetup 573 { 574 public Setup (Test test) 575 { 576 super (test); 577 } 578 579 protected void setUp () 580 { 581 org.omg.CORBA.Object obj = null; 582 583 orb = ORBSetup.getORB (); 584 try 585 { 586 obj = orb.resolve_initial_references ("DynAnyFactory"); 587 } 588 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 589 { 590 fail ("Failed to resolve DynAnyFactory: " + ex); 591 } 592 try 593 { 594 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 595 } 596 catch (Throwable ex) 597 { 598 fail ("Failed to narrow to DynAnyFactory: " + ex); 599 } 600 } 601 602 protected void tearDown () 603 { 604 } 605 } 606 607 608 611 private static org.omg.DynamicAny.DynAny createDynAnyFromAny 612 (org.omg.CORBA.Any any) 613 { 614 String msg; 615 org.omg.DynamicAny.DynAny dynAny = null; 616 617 try 618 { 619 dynAny = factory.create_dyn_any (any); 620 } 621 catch (Throwable ex) 622 { 623 msg = "Factory failed to create DynAny from Any using "; 624 msg += "DynAny::create_dyn_any operation: " + ex; 625 fail (msg); 626 } 627 return dynAny; 628 } 629 630 631 634 private static org.omg.DynamicAny.DynAny createDynAnyFromTypeCode 635 (org.omg.CORBA.TypeCode tc) 636 { 637 String msg; 638 org.omg.DynamicAny.DynAny dynAny = null; 639 640 try 641 { 642 dynAny = factory.create_dyn_any_from_type_code (tc); 643 } 644 catch (Throwable ex) 645 { 646 msg = "Factory failed to create DynAny from TypeCode using "; 647 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 648 fail (msg); 649 } 650 return dynAny; 651 } 652 653 } 654 | Popular Tags |