1 package org.jacorb.test.orb.dynany; 2 3 22 23 import junit.framework.*; 24 import junit.extensions.TestSetup; 25 import org.omg.CORBA.TCKind ; 26 27 import org.jacorb.test.common.ORBSetup; 28 import org.jacorb.test.EnumType; 29 import org.jacorb.test.EnumTypeHelper; 30 import org.jacorb.test.UnionDefaultType; 31 import org.jacorb.test.UnionDefaultTypeHelper; 32 import org.jacorb.test.UnionNoDefaultType; 33 import org.jacorb.test.UnionNoDefaultTypeHelper; 34 import org.jacorb.test.UnionFullRangeType; 35 import org.jacorb.test.UnionFullRangeTypeHelper; 36 37 43 44 public class DynAnyUnionTest extends TestCase 45 { 46 private static org.omg.DynamicAny.DynAnyFactory factory = null; 47 private static org.omg.CORBA.ORB orb = null; 48 49 private static final String ID = "IDL:test:1.0"; 50 private static final String NAME = "MyUnion"; 51 52 public DynAnyUnionTest (String name) 53 { 54 super (name); 55 } 56 57 58 public static Test suite () 59 { 60 TestSuite suite = new TestSuite 61 ("DynUnion Tests using Enumeration Discriminator"); 62 Setup setup = new Setup (suite); 63 ORBSetup osetup = new ORBSetup (setup); 64 65 suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromAny")); 66 suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromTypeCode")); 67 suite.addTest (new DynAnyUnionTest ("testFactoryCreateFromIDLTypeCode")); 68 suite.addTest (new DynAnyUnionTest ("testCompareDynAnyUnamedMember")); 69 suite.addTest (new DynAnyUnionTest ("testCompareDynAnyNamedMember")); 70 suite.addTest (new DynAnyUnionTest ("testIterateDynAnyNamedMember")); 71 suite.addTest (new DynAnyUnionTest ("testIterateDynAnyUnamedMember")); 72 suite.addTest (new DynAnyUnionTest ("testAccessUnionDisc")); 73 suite.addTest (new DynAnyUnionTest ("testUnionDefaultCase")); 74 suite.addTest (new DynAnyUnionTest ("testUnionNoDefaultCase")); 75 suite.addTest (new DynAnyUnionTest ("testUnionFullRange")); 76 suite.addTest (new DynAnyUnionTest ("testAccessNamedUnionMember")); 77 suite.addTest (new DynAnyUnionTest ("testAccessUnamedUnionMember")); 78 suite.addTest (new DynAnyUnionTest ("testDynAnyTypeCode")); 79 suite.addTest (new DynAnyUnionTest ("testInitDynAnyFromDynAny")); 80 suite.addTest (new DynAnyUnionTest ("testInitDynAnyFromAny")); 81 suite.addTest (new DynAnyUnionTest ("testInitFromAnyTypeMismatchEx")); 82 suite.addTest (new DynAnyUnionTest ("testGenerateAnyFromDynAny")); 83 suite.addTest (new DynAnyUnionTest ("testDestroyDynAny")); 84 suite.addTest (new DynAnyUnionTest ("testDestroyComponent")); 85 suite.addTest (new DynAnyUnionTest ("testCopyDynAny")); 86 87 return osetup; 88 } 89 90 91 95 public void testFactoryCreateFromAny () 96 { 97 UnionDefaultType type = null; 98 org.omg.CORBA.Any any = null; 99 100 type = new UnionDefaultType (); 101 type.win (10); 102 any = orb.create_any (); 103 UnionDefaultTypeHelper.insert (any, type); 104 105 createDynAnyFromAny (any); 106 } 107 108 109 113 public void testFactoryCreateFromTypeCode () 114 { 115 org.omg.CORBA.TypeCode tc = null; 116 117 tc = orb.create_union_tc 118 ( 119 ID, 120 NAME, 121 EnumTypeHelper.type (), 122 getUnionMembers () 123 ); 124 createDynAnyFromTypeCode (tc); 125 } 126 127 128 132 public void testFactoryCreateFromIDLTypeCode () 133 { 134 org.omg.CORBA.TypeCode tc = null; 135 136 tc = UnionDefaultTypeHelper.type (); 137 createDynAnyFromTypeCode (tc); 138 } 139 140 141 144 public void testCompareDynAnyUnamedMember () 145 { 146 String msg; 147 UnionNoDefaultType type; 148 org.omg.CORBA.Any any = null; 149 org.omg.DynamicAny.DynUnion dynAny = null; 150 org.omg.DynamicAny.DynUnion dynAny2 = null; 151 152 type = new UnionNoDefaultType (); 153 type.__default (); 154 any = orb.create_any (); 155 UnionNoDefaultTypeHelper.insert (any, type); 156 dynAny = createDynAnyFromAny (any); 157 dynAny2 = createDynAnyFromAny (any); 158 159 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 160 assertTrue (msg, dynAny.equal (dynAny2)); 161 } 162 163 164 167 public void testCompareDynAnyNamedMember () 168 { 169 String msg; 170 UnionDefaultType type; 171 org.omg.CORBA.Any any = null; 172 org.omg.DynamicAny.DynUnion dynAny = null; 173 org.omg.DynamicAny.DynUnion dynAny2 = null; 174 175 type = new UnionDefaultType (); 176 type.win (10); 177 any = orb.create_any (); 178 UnionDefaultTypeHelper.insert (any, type); 179 dynAny = createDynAnyFromAny (any); 180 dynAny2 = createDynAnyFromAny (any); 181 182 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 183 assertTrue (msg, dynAny.equal (dynAny2)); 184 } 185 186 187 191 public void testIterateDynAnyNamedMember () 192 { 193 String msg; 194 int compCount = -1; 195 boolean seek; 196 org.omg.CORBA.TypeCode tc = null; 197 org.omg.DynamicAny.DynUnion dynAny = null; 198 org.omg.DynamicAny.DynAny compSeek = null; 199 org.omg.DynamicAny.DynAny compRewind = null; 200 201 tc = orb.create_union_tc 202 ( 203 ID, 204 NAME, 205 EnumTypeHelper.type (), 206 getUnionMembers () 207 ); 208 dynAny = createDynAnyFromTypeCode (tc); 209 210 msg = "The number of components returned from the "; 212 msg += "DynAny::component_count method is incorrect"; 213 try 214 { 215 compCount = dynAny.component_count (); 216 } 217 catch (Throwable ex) 218 { 219 fail ("Unexpected error raised by DynAny::component_count operation"); 221 } 222 assertEquals (msg, 2, compCount); 223 224 msg = "The DynAny::seek operation indicates a valid current position "; 226 msg += "when the current position should be invalid"; 227 seek = dynAny.seek (-1); 228 assertTrue (msg, !seek); 229 230 msg = "The DynAny::seek operation indicates an invalid current position "; 232 msg += "when the current position should be valid"; 233 seek = dynAny.seek (0); 234 assertTrue (msg, seek); 235 236 try 238 { 239 compSeek = dynAny.current_component (); 240 } 241 catch (Throwable ex) 242 { 243 msg = "Failed to get the current component using the "; 244 msg += "DynAny::current_component operation after calling the "; 245 msg += "DynAny::seek operation"; 246 fail (msg); 247 } 248 249 msg = "The DynAny::next operation indicates an invalid current position "; 251 msg += "when the current position should be valid"; 252 seek = dynAny.next (); 253 assertTrue (msg, seek); 254 255 dynAny.rewind (); 257 258 try 260 { 261 compRewind = dynAny.current_component (); 262 } 263 catch (Throwable ex) 264 { 265 msg = "Failed to get the current component using the "; 266 msg += "DynAny::current_component operation after calling the "; 267 msg += "DynAny::rewind operation"; 268 fail (msg); 269 } 270 msg = "The component at DynAny::seek(0) is not equal to the "; 271 msg += "component at DynAny::rewind"; 272 assertTrue (msg, compSeek.equal (compRewind)); 273 } 274 275 276 280 public void testIterateDynAnyUnamedMember () 281 { 282 String msg; 283 int compCount = -1; 284 boolean seek; 285 UnionNoDefaultType type; 286 org.omg.CORBA.Any any = null; 287 org.omg.CORBA.TypeCode tc = null; 288 org.omg.DynamicAny.DynUnion dynAny = null; 289 org.omg.DynamicAny.DynEnum disc = null; 290 291 type = new UnionNoDefaultType (); 292 type.__default (); 293 any = orb.create_any (); 294 UnionNoDefaultTypeHelper.insert (any, type); 295 dynAny = createDynAnyFromAny (any); 296 297 disc = (org.omg.DynamicAny.DynEnum ) dynAny.get_discriminator (); 299 300 msg = "The discriminator is incorrectly set for a DynAny created "; 302 msg += "from a union with no active member"; 303 assertEquals (msg, EnumType._fifth, disc.get_as_ulong ()); 304 305 try 307 { 308 compCount = dynAny.component_count (); 309 } 310 catch (Throwable ex) 311 { 312 fail ("Unexpected error raised by DynAny::component_count operation"); 314 } 315 316 msg = "The number of components returned from the "; 317 msg += "DynAny::component_count operation is incorrect"; 318 assertEquals (msg, 1, compCount); 319 320 msg = "The DynAny::seek operation indicates an invalid current position "; 322 msg += "when the current position should be valid"; 323 seek = dynAny.seek (0); 324 assertTrue (msg, seek); 325 326 msg = "The DynAny::next operation indicates a valid current position "; 328 msg += "when the current position should be invalid"; 329 seek = dynAny.next (); 330 assertTrue (msg, !seek); 331 332 msg = "The object returned from the DynAny::current_component "; 334 msg += "operation should be null because the current position is "; 335 msg += "invalid (-1)"; 336 try 337 { 338 dynAny = (org.omg.DynamicAny.DynUnion ) dynAny.current_component (); 339 340 assertNull (msg, dynAny); 341 } 342 catch (Throwable ex) 343 { 344 fail (msg + ": " + ex); 345 } 346 } 347 348 349 352 public void testAccessUnionDisc () 353 { 354 String msg; 355 TCKind discKind = null; 356 org.omg.CORBA.TypeCode tc = null; 357 org.omg.DynamicAny.DynUnion dynAny = null; 358 org.omg.DynamicAny.DynEnum disc = null; 359 org.omg.DynamicAny.DynAny invalidDisc = null; 360 361 tc = UnionDefaultTypeHelper.type (); 362 dynAny = createDynAnyFromTypeCode (tc); 363 364 try 366 { 367 disc = (org.omg.DynamicAny.DynEnum ) 368 factory.create_dyn_any_from_type_code (EnumTypeHelper.type ()); 369 } 370 catch (Throwable ex) 371 { 372 fail ("Failed to create DynAny with correct TypeCode: " + ex); 373 } 374 375 try 376 { 377 disc.set_as_string ("second"); } 379 catch (Throwable ex) 380 { 381 fail ("Failed to set value of discriminator: " + ex); 382 } 383 384 try 385 { 386 dynAny.set_discriminator (disc); 387 } 388 catch (Throwable ex) 389 { 390 msg = "Failed to set the value of the discriminator using the "; 391 msg += "DynUnion::set_discriminator operation"; 392 fail (msg + ": " + ex); 393 } 394 395 msg = "Failed to get the correct value of the discriminator using the "; 397 msg += "DynUnion::get_discriminator operation"; 398 disc = (org.omg.DynamicAny.DynEnum ) dynAny.get_discriminator (); 399 400 assertEquals (msg, EnumType.second.value (), disc.get_as_ulong ()); 402 403 msg = "Failed to get the correct kind of the discriminator using "; 405 msg += "DynUnion::discriminator_kind operation"; 406 discKind = dynAny.discriminator_kind (); 407 408 assertEquals (msg, TCKind._tk_enum, discKind.value ()); 410 411 tc = orb.get_primitive_tc (TCKind.tk_long); 413 try 414 { 415 invalidDisc = factory.create_dyn_any_from_type_code (tc); 416 } 417 catch (Throwable ex) 418 { 419 fail ("Failed to create DynAny with incorrect TypeCode: " + ex); 420 } 421 422 try 423 { 424 dynAny.set_discriminator (invalidDisc); 425 426 msg = "Failed to throw a TypeMismatch exception when calling "; 427 msg += "DynStruct::set_discriminator operation with an incorrect "; 428 msg += "TypeCode"; 429 fail (msg); 430 } 431 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 432 { 433 } 435 } 436 437 438 442 public void testUnionDefaultCase () 443 { 444 String msg; 445 int compCount = -1; 446 UnionDefaultType type; 447 boolean hasNoActiveMember = true; 448 org.omg.CORBA.Any any = null; 449 org.omg.DynamicAny.DynEnum disc = null; 450 org.omg.DynamicAny.DynUnion dynAny = null; 451 452 type = new UnionDefaultType (); 453 type.win (10); 454 any = orb.create_any (); 455 UnionDefaultTypeHelper.insert (any, type); 456 dynAny = createDynAnyFromAny (any); 457 458 msg = "Failed to set the discriminator to the default member using the "; 460 msg += "DynUnion::set_to_default_member operation"; 461 try 462 { 463 dynAny.set_to_default_member (); 464 } 465 catch (Throwable ex) 466 { 467 fail (msg + ": " + ex); 468 } 469 disc = (org.omg.DynamicAny.DynEnum )dynAny.get_discriminator (); 470 471 assertEquals (msg, EnumType.fifth.value (), disc.get_as_ulong ()); 474 475 try 477 { 478 compCount = dynAny.component_count (); 479 } 480 catch (Throwable ex) 481 { 482 fail ("Unexpected error raised by DynAny::component_count operation"); 484 } 485 msg = "Wrong number of components returned from DynAny::component_count "; 486 msg += "operation after calling DynUnion::set_to_default_member "; 487 msg += "operation"; 488 assertEquals (msg, 2, compCount); 489 490 try 492 { 493 dynAny.set_to_no_active_member (); 494 495 msg = "Failed to raise a TypeMismatch exception when calling the "; 496 msg += "DynUnion::set_to_no_active_member operation on a union with "; 497 msg += "an explicit default case"; 498 fail (msg); 499 } 500 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 501 { 502 } 504 505 msg = "The DynUnion::has_no_active_member operation did not return "; 506 msg += "FALSE when called on a union with an explicit default case"; 507 try 508 { 509 hasNoActiveMember = dynAny.has_no_active_member (); 510 } 511 catch (Throwable ex) 512 { 513 fail (msg + ": " + ex); 514 } 515 assertTrue (msg, !hasNoActiveMember); 516 } 517 518 519 523 public void testUnionNoDefaultCase () 524 { 525 String msg; 526 int compCount = -1; 527 UnionNoDefaultType type; 528 boolean hasNoActiveMember = false; 529 org.omg.CORBA.Any any = null; 530 org.omg.DynamicAny.DynUnion dynAny = null; 531 org.omg.DynamicAny.DynEnum disc = null; 532 533 type = new UnionNoDefaultType (); 534 type.win (10); 535 any = orb.create_any (); 536 UnionNoDefaultTypeHelper.insert (any, type); 537 dynAny = createDynAnyFromAny (any); 538 539 msg = "Failed to set the discriminator to no active member using the "; 541 msg += "DynUnion::set_to_no_active_member operation"; 542 try 543 { 544 dynAny.set_to_no_active_member (); 545 } 546 catch (Throwable ex) 547 { 548 fail (msg + ": " + ex); 549 } 550 disc = (org.omg.DynamicAny.DynEnum ) dynAny.get_discriminator (); 551 552 assertEquals (msg, EnumType._fifth, disc.get_as_ulong ()); 554 555 try 557 { 558 compCount = dynAny.component_count (); 559 } 560 catch (Throwable ex) 561 { 562 fail ("Unexpected error raised by DynAny::component_count operation"); 564 } 565 566 msg = "Wrong number of components returned from DynAny::component_count "; 567 msg += "operation after calling DynUnion::set_to_no_active_member "; 568 msg += "operation"; 569 assertEquals (msg, 1, compCount); 570 571 try 573 { 574 dynAny.set_to_default_member (); 575 576 msg = "Failed to raise a TypeMismatch exception when calling the "; 577 msg += "DynUnion::set_to_default_member operation on a union without "; 578 msg += "an explicit default case"; 579 fail (msg); 580 } 581 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 582 { 583 } 585 586 msg = "The DynUnion::has_no_active_member operation did not return "; 587 msg += "TRUE when called on a union without an explicit default case"; 588 try 589 { 590 hasNoActiveMember = dynAny.has_no_active_member (); 591 } 592 catch (Throwable ex) 593 { 594 fail (msg + ": " + ex); 595 } 596 assertTrue (msg, hasNoActiveMember); 597 } 598 599 600 604 public void testUnionFullRange () 605 { 606 String msg; 607 int compCount; 608 UnionFullRangeType type; 609 boolean hasNoActiveMember = true;; 610 org.omg.CORBA.Any any = null; 611 org.omg.DynamicAny.DynUnion dynAny = null; 612 int discVal; 614 type = new UnionFullRangeType (); 615 type.win (10); 616 any = orb.create_any (); 617 UnionFullRangeTypeHelper.insert (any, type); 618 dynAny = createDynAnyFromAny (any); 619 620 try 622 { 623 dynAny.set_to_no_active_member (); 624 625 msg = "Failed to raise a TypeMismatch exception when calling the "; 626 msg += "DynUnion::set_to_no_active_member operation on a union "; 627 msg += "that uses the full range of discriminator values"; 628 fail (msg); 629 } 630 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 631 { 632 } 634 635 try 637 { 638 dynAny.set_to_default_member (); 639 640 msg = "Failed to raise a TypeMismatch exception when calling the "; 641 msg += "DynUnion::set_to_default_member operation on a union that "; 642 msg += "uses the full range of discriminator values"; 643 fail (msg); 644 } 645 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 646 { 647 } 649 650 msg = "The DynUnion::has_no_active_member operation did not return "; 651 msg += "FALSE when called on a union that uses the full range of "; 652 msg += "discriminator values"; 653 try 654 { 655 hasNoActiveMember = dynAny.has_no_active_member (); 656 } 657 catch (Throwable ex) 658 { 659 fail (msg + ": " + ex); 660 } 661 assertTrue (msg, !hasNoActiveMember); 662 } 663 664 665 668 public void testAccessNamedUnionMember () 669 { 670 String msg; 671 int testVal = 10; 672 UnionDefaultType type; 673 TCKind memberKind = null; 674 String memberName = null; 675 int memberVal = -1; 676 org.omg.CORBA.Any any = null; 677 org.omg.DynamicAny.DynUnion dynAny = null; 678 org.omg.DynamicAny.DynAny member = null; 680 type = new UnionDefaultType (); 681 type.win (testVal); 682 any = orb.create_any (); 683 UnionDefaultTypeHelper.insert (any, type); 684 dynAny = createDynAnyFromAny (any); 685 686 msg = "Failed to get the correct kind of the active member using "; 688 msg += "DynUnion::member_kind operation"; 689 try 690 { 691 memberKind = dynAny.member_kind (); 692 } 693 catch (Throwable ex) 694 { 695 fail (msg + ": " + ex); 696 } 697 698 assertEquals (msg, TCKind._tk_long, memberKind.value ()); 700 701 msg = "Failed to get the correct name of the active member using "; 703 msg += "DynUnion::member_name operation"; 704 try 705 { 706 memberName = dynAny.member_name (); 707 } 708 catch (Throwable ex) 709 { 710 fail (msg + ": " + ex); 711 } 712 713 assertEquals (msg, "win", memberName); 715 try 717 { 718 member = dynAny.member (); 719 } 720 catch (Throwable ex) 721 { 722 msg = "Failed to get the correct active union member using "; 723 msg += "DynUnion::member operation"; 724 fail (msg + ": " + ex); 725 } 726 727 msg = "Failed to get the correct value of active union member using "; 728 msg += "DynUnion::member operation"; 729 memberVal = member.to_any ().extract_long (); 730 731 assertEquals (msg, testVal, memberVal); 733 } 734 735 736 740 public void testAccessUnamedUnionMember () 741 { 742 String msg; 743 org.omg.CORBA.TypeCode tc = null; 744 org.omg.DynamicAny.DynUnion dynAny = null; 745 746 tc = UnionNoDefaultTypeHelper.type (); 747 dynAny = createDynAnyFromTypeCode (tc); 748 749 try 750 { 751 dynAny.set_to_no_active_member (); 752 } 753 catch (Throwable ex) 754 { 755 fail ("Failed to set the union to have no active member: " + ex); 756 } 757 758 try 760 { 761 dynAny.member_kind (); 762 763 msg = "Failed to raise an InvalidValue exception when calling the "; 764 msg += "DynUnion::member_kind operation on a union with no active "; 765 msg += "member"; 766 fail (msg); 767 } 768 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 769 { 770 } 772 773 try 775 { 776 dynAny.member_name (); 777 778 msg = "Failed to raise an InvalidValue exception when calling the "; 779 msg += "DynUnion::member_name operation on a union with no active "; 780 msg += "member"; 781 fail (msg); 782 } 783 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 784 { 785 } 787 788 try 790 { 791 dynAny.member (); 792 793 msg = "Failed to raise an InvalidValue exception when calling the "; 794 msg += "DynUnion::member operation on a union with no active member"; 795 fail (msg); 796 } 797 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 798 { 799 } 801 } 802 803 804 807 public void testDynAnyTypeCode () 808 { 809 String msg; 810 org.omg.CORBA.TypeCode tc = null; 811 org.omg.DynamicAny.DynUnion dynAny = null; 812 813 tc = orb.create_union_tc 814 ( 815 ID, 816 NAME, 817 EnumTypeHelper.type (), 818 getUnionMembers () 819 ); 820 dynAny = createDynAnyFromTypeCode (tc); 821 822 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 823 assertTrue (msg, dynAny.type ().equal (tc)); 824 } 825 826 827 830 public void testInitDynAnyFromDynAny () 831 { 832 String msg; 833 UnionDefaultType type; 834 org.omg.CORBA.Any any = null; 835 org.omg.CORBA.TypeCode tc = null; 836 org.omg.DynamicAny.DynUnion dynAny = null; 837 org.omg.DynamicAny.DynUnion dynAny2 = null; 838 839 tc = UnionDefaultTypeHelper.type (); 840 dynAny = createDynAnyFromTypeCode (tc); 841 842 type = new UnionDefaultType (); 843 type.win (10); 844 any = orb.create_any (); 845 UnionDefaultTypeHelper.insert (any, type); 846 dynAny2 = createDynAnyFromAny (any); 847 848 msg = "Failed to initialize a DynAny object from another DynAny "; 849 msg += "object using the DynAny::assign operation"; 850 try 851 { 852 dynAny.assign (dynAny2); 853 } 854 catch (Throwable ex) 855 { 856 fail (msg + ": " + ex); 857 } 858 assertTrue (msg, dynAny.equal (dynAny2)); 859 } 860 861 862 865 public void testInitDynAnyFromAny () 866 { 867 String msg; 868 UnionDefaultType type; 869 org.omg.CORBA.Any any = null; 870 org.omg.CORBA.TypeCode tc = null; 871 org.omg.DynamicAny.DynUnion dynAny = null; 872 org.omg.DynamicAny.DynUnion dynAny2 = null; 873 874 tc = UnionDefaultTypeHelper.type (); 875 dynAny = createDynAnyFromTypeCode (tc); 876 877 type = new UnionDefaultType (); 878 type.win (10); 879 any = orb.create_any (); 880 UnionDefaultTypeHelper.insert (any, type); 881 dynAny2 = createDynAnyFromAny (any); 882 883 msg = "Failed to initialize a DynAny object from an Any object "; 884 msg += "using the DynAny::from_any operation"; 885 try 886 { 887 dynAny.from_any (any); 888 } 889 catch (Throwable ex) 890 { 891 fail (msg + ": " + ex); 892 } 893 assertTrue (msg, dynAny.equal (dynAny2)); 894 } 895 896 897 901 public void testInitFromAnyTypeMismatchEx () 902 { 903 String msg; 904 org.omg.CORBA.Any any = null; 905 org.omg.CORBA.TypeCode tc = null; 906 org.omg.DynamicAny.DynUnion dynAny = null; 907 908 any = orb.create_any (); 909 any.insert_string ("Hello"); 910 911 tc = orb.create_union_tc 912 ( 913 ID, 914 NAME, 915 EnumTypeHelper.type (), 916 getUnionMembers () 917 ); 918 dynAny = createDynAnyFromTypeCode (tc); 919 920 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 921 msg += "operation when DynAny and Any operands have different types"; 922 try 923 { 924 dynAny.from_any (any); 925 926 fail (msg); 927 } 928 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 929 { 930 } 932 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 933 { 934 fail (msg + ": " + ex); 935 } 936 } 937 938 939 942 public void testGenerateAnyFromDynAny () 943 { 944 String msg; 945 org.omg.CORBA.Any any = null; 946 org.omg.CORBA.TypeCode tc = null; 947 org.omg.DynamicAny.DynUnion dynAny = null; 948 org.omg.DynamicAny.DynUnion dynAny2 = null; 949 950 tc = UnionDefaultTypeHelper.type (); 951 dynAny = createDynAnyFromTypeCode (tc); 952 953 any = orb.create_any (); 954 any = dynAny.to_any (); 955 dynAny2 = createDynAnyFromAny (any); 956 957 msg = "The DynAny::to_any operation failed to create an Any "; 958 msg += "object with the same value as the DynAny object"; 959 assertTrue (msg, dynAny.equal (dynAny2)); 960 } 961 962 963 966 public void testDestroyDynAny () 967 { 968 String msg; 969 UnionDefaultType type; 970 org.omg.CORBA.Any any = null; 971 org.omg.DynamicAny.DynUnion dynAny = null; 972 973 type = new UnionDefaultType (); 974 type.win (10); 975 any = orb.create_any (); 976 UnionDefaultTypeHelper.insert (any, type); 977 dynAny = createDynAnyFromAny (any); 978 dynAny.destroy (); 979 980 try 981 { 982 dynAny.type (); 983 984 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 985 msg += "calling DynAny::type operation on a destroyed DynAny object "; 986 msg += "did not raise OBJECT_NOT_EXIST exception"; 987 fail (msg); 988 } 989 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 990 { 991 } 993 994 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 995 msg += "calling DynAny::current_component operation on a destroyed "; 996 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 997 try 998 { 999 dynAny.current_component (); 1000 1001 fail (msg); 1002 } 1003 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 1004 { 1005 } 1007 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 1008 { 1009 fail (msg + ": " + ex); 1010 } 1011 } 1012 1013 1014 1017 public void testDestroyComponent () 1018 { 1019 String msg; 1020 UnionDefaultType type; 1021 org.omg.CORBA.Any any = null; 1022 org.omg.DynamicAny.DynUnion dynAny = null; 1023 org.omg.DynamicAny.DynAny comp = null; 1024 1025 type = new UnionDefaultType (); 1026 type.win (10); 1027 any = orb.create_any (); 1028 UnionDefaultTypeHelper.insert (any, type); 1029 dynAny = createDynAnyFromAny (any); 1030 1031 try 1032 { 1033 comp = dynAny.current_component (); 1034 } 1035 catch (Throwable ex) 1036 { 1037 msg = "Failed to get the current component of the DynAny using the "; 1038 msg += "DynAny::current_component operation before calling the "; 1039 msg += "DynAny::destroy operation"; 1040 fail (msg + ": " + ex); 1041 } 1042 1043 comp.destroy (); 1044 try 1045 { 1046 comp = dynAny.current_component (); 1047 } 1048 catch (Throwable ex) 1049 { 1050 msg = "Failed to get the current component of the DynAny using the "; 1051 msg += "DynAny::current_component operation after calling the "; 1052 msg += "DynAny::destroy operation"; 1053 fail (msg + ": " + ex); 1054 } 1055 1056 try 1057 { 1058 comp.type (); 1059 } 1060 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 1061 { 1062 msg = "Calling destroy on a component resulted in destroying the "; 1063 msg += "component object"; 1064 fail (msg + ": " + ex); 1065 } 1066 } 1067 1068 1069 1072 public void testCopyDynAny () 1073 { 1074 String msg; 1075 org.omg.CORBA.TypeCode tc = null; 1076 org.omg.DynamicAny.DynUnion dynAny = null; 1077 org.omg.DynamicAny.DynUnion dynAny2 = null; 1078 1079 tc = UnionDefaultTypeHelper.type (); 1080 dynAny = createDynAnyFromTypeCode (tc); 1081 dynAny2 = (org.omg.DynamicAny.DynUnion ) dynAny.copy (); 1082 1083 msg = "The DynAny object created with the DynAny::copy operation "; 1084 msg += "is not equal to the DynAny object it was copied from"; 1085 assertTrue (msg, dynAny.equal (dynAny2)); 1086 } 1087 1088 1089 private static class Setup extends TestSetup 1090 { 1091 public Setup (Test test) 1092 { 1093 super (test); 1094 } 1095 1096 protected void setUp () 1097 { 1098 org.omg.CORBA.Object obj = null; 1099 1100 orb = ORBSetup.getORB (); 1101 try 1102 { 1103 obj = orb.resolve_initial_references ("DynAnyFactory"); 1104 } 1105 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 1106 { 1107 fail ("Failed to resolve DynAnyFactory: " + ex); 1108 } 1109 try 1110 { 1111 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 1112 } 1113 catch (Throwable ex) 1114 { 1115 fail ("Failed to narrow to DynAnyFactory: " + ex); 1116 } 1117 } 1118 1119 protected void tearDown () 1120 { 1121 } 1122 } 1123 1124 1125 1128 private static org.omg.DynamicAny.DynUnion createDynAnyFromAny 1129 (org.omg.CORBA.Any any) 1130 { 1131 String msg; 1132 org.omg.DynamicAny.DynUnion dynAny = null; 1133 1134 try 1135 { 1136 dynAny = (org.omg.DynamicAny.DynUnion ) factory.create_dyn_any (any); 1137 } 1138 catch (Throwable ex) 1139 { 1140 msg = "Factory failed to create DynAny from Any using "; 1141 msg += "DynAny::create_dyn_any operation: " + ex; 1142 fail (msg); 1143 } 1144 return dynAny; 1145 } 1146 1147 1148 1151 private static org.omg.DynamicAny.DynUnion createDynAnyFromTypeCode 1152 (org.omg.CORBA.TypeCode tc) 1153 { 1154 String msg; 1155 org.omg.DynamicAny.DynUnion dynAny = null; 1156 1157 try 1158 { 1159 dynAny = (org.omg.DynamicAny.DynUnion ) 1160 factory.create_dyn_any_from_type_code (tc); 1161 } 1162 catch (Throwable ex) 1163 { 1164 msg = "Factory failed to create DynAny from TypeCode using "; 1165 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 1166 fail (msg); 1167 } 1168 return dynAny; 1169 } 1170 1171 1172 1175 private static org.omg.CORBA.UnionMember [] getUnionMembers () 1176 { 1177 final org.omg.CORBA.UnionMember [] members = 1178 new org.omg.CORBA.UnionMember [2]; 1179 org.omg.CORBA.Any any = null; 1180 1181 any = orb.create_any (); 1182 EnumTypeHelper.insert (any, EnumType.first); 1183 members [0] = new org.omg.CORBA.UnionMember 1184 ( 1185 "zero", 1186 any, 1187 orb.get_primitive_tc (TCKind.tk_string), 1188 null 1189 ); 1190 1191 any = orb.create_any (); 1192 EnumTypeHelper.insert (any, EnumType.second); 1193 1194 members [1] = new org.omg.CORBA.UnionMember 1196 ( 1197 "one", 1198 any, 1199 orb.get_primitive_tc (TCKind.tk_char), 1200 null 1201 ); 1202 1203 return members; 1204 } 1205 1206} 1207 | Popular Tags |