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 java.lang.reflect.Method ; 28 29 import org.jacorb.test.common.ORBSetup; 30 import org.jacorb.test.UnionDefaultIntType; 31 import org.jacorb.test.UnionDefaultIntTypeHelper; 32 import org.jacorb.test.UnionNoDefaultIntType; 33 import org.jacorb.test.UnionNoDefaultIntTypeHelper; 34 35 41 42 public class DynAnyUnionIntTest extends TestCase 43 { 44 private static org.omg.DynamicAny.DynAnyFactory factory = null; 45 private static org.omg.CORBA.ORB orb = null; 46 47 private static final String ID = "IDL:test:1.0"; 48 private static final String NAME = "MyUnion"; 49 50 public DynAnyUnionIntTest (String name) 51 { 52 super (name); 53 } 54 55 56 public static Test suite () 57 { 58 TestSuite suite = new TestSuite 59 ("DynUnion Tests using Integer Discriminator"); 60 Setup setup = new Setup (suite); 61 ORBSetup osetup = new ORBSetup (setup); 62 63 suite.addTest (new DynAnyUnionIntTest ("testFactoryCreateFromAny")); 64 suite.addTest (new DynAnyUnionIntTest ("testFactoryCreateFromTypeCode")); 65 suite.addTest (new DynAnyUnionIntTest ("testFactoryCreateFromIDLTypeCode")); 66 suite.addTest (new DynAnyUnionIntTest ("testCompareDynAnyUnamedMember")); 67 suite.addTest (new DynAnyUnionIntTest ("testCompareDynAnyNamedMember")); 68 suite.addTest (new DynAnyUnionIntTest ("testIterateDynAnyNamedMember")); 69 suite.addTest (new DynAnyUnionIntTest ("testIterateDynAnyUnamedMember")); 70 suite.addTest (new DynAnyUnionIntTest ("testAccessUnionDisc")); 71 suite.addTest (new DynAnyUnionIntTest ("testUnionDefaultCase")); 72 suite.addTest (new DynAnyUnionIntTest ("testUnionNoDefaultCase")); 73 suite.addTest (new DynAnyUnionIntTest ("testAccessNamedUnionMember")); 74 suite.addTest (new DynAnyUnionIntTest ("testAccessUnamedUnionMember")); 75 suite.addTest (new DynAnyUnionIntTest ("testDynAnyTypeCode")); 76 suite.addTest (new DynAnyUnionIntTest ("testInitDynAnyFromDynAny")); 77 suite.addTest (new DynAnyUnionIntTest ("testInitDynAnyFromAny")); 78 suite.addTest (new DynAnyUnionIntTest ("testInitFromAnyTypeMismatchEx")); 79 suite.addTest (new DynAnyUnionIntTest ("testGenerateAnyFromDynAny")); 80 suite.addTest (new DynAnyUnionIntTest ("testDestroyDynAny")); 81 suite.addTest (new DynAnyUnionIntTest ("testDestroyComponent")); 82 suite.addTest (new DynAnyUnionIntTest ("testCopyDynAny")); 83 84 return osetup; 85 } 86 87 88 92 public void testFactoryCreateFromAny () 93 { 94 UnionDefaultIntType type = null; 95 org.omg.CORBA.Any any = null; 96 97 type = new UnionDefaultIntType (); 98 type.win (10); 99 any = orb.create_any (); 100 UnionDefaultIntTypeHelper.insert (any, type); 101 102 createDynAnyFromAny (any); 103 } 104 105 106 110 public void testFactoryCreateFromTypeCode () 111 { 112 org.omg.CORBA.TypeCode tc = null; 113 114 tc = orb.create_union_tc 115 ( 116 ID, 117 NAME, 118 orb.get_primitive_tc (TCKind.tk_short), 119 getUnionMembers () 120 ); 121 createDynAnyFromTypeCode (tc); 122 } 123 124 125 129 public void testFactoryCreateFromIDLTypeCode () 130 { 131 org.omg.CORBA.TypeCode tc = null; 132 133 tc = UnionDefaultIntTypeHelper.type (); 134 createDynAnyFromTypeCode (tc); 135 } 136 137 138 141 public void testCompareDynAnyUnamedMember () 142 { 143 String msg; 144 UnionNoDefaultIntType type; 145 org.omg.CORBA.Any any = null; 146 org.omg.DynamicAny.DynUnion dynAny = null; 147 org.omg.DynamicAny.DynUnion dynAny2 = null; 148 149 type = new UnionNoDefaultIntType (); 150 151 Class unionClass = type.getClass (); 153 Method method = null; 154 155 try 157 { 158 method = unionClass.getDeclaredMethod ("__default", new Class [0]); 159 } 160 catch (NoSuchMethodException ex) 161 { 162 try 163 { 164 method = unionClass.getDeclaredMethod ("_default", new Class [0]); 165 } 166 catch (Throwable e) 167 { 168 msg = "Unexpected error trying to determine method to invoke: " + e; 169 fail (msg); 170 } 171 } 172 173 try 175 { 176 method.invoke (type, new Object [0]); 177 } 178 catch (Throwable ex) 179 { 180 fail ("Failed to invoke method via reflection: " + ex); 181 } 182 183 any = orb.create_any (); 184 UnionNoDefaultIntTypeHelper.insert (any, type); 185 dynAny = createDynAnyFromAny (any); 186 dynAny2 = createDynAnyFromAny (any); 187 188 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 189 assertTrue (msg, dynAny.equal (dynAny2)); 190 } 191 192 193 196 public void testCompareDynAnyNamedMember () 197 { 198 String msg; 199 UnionDefaultIntType type; 200 org.omg.CORBA.Any any = null; 201 org.omg.DynamicAny.DynUnion dynAny = null; 202 org.omg.DynamicAny.DynUnion dynAny2 = null; 203 204 type = new UnionDefaultIntType (); 205 type.win (10); 206 any = orb.create_any (); 207 UnionDefaultIntTypeHelper.insert (any, type); 208 dynAny = createDynAnyFromAny (any); 209 dynAny2 = createDynAnyFromAny (any); 210 211 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 212 assertTrue (msg, dynAny.equal (dynAny2)); 213 } 214 215 216 220 public void testIterateDynAnyNamedMember () 221 { 222 String msg; 223 int compCount = -1; 224 boolean seek; 225 org.omg.CORBA.TypeCode tc = null; 226 org.omg.DynamicAny.DynUnion dynAny = null; 227 org.omg.DynamicAny.DynAny compSeek = null; 228 org.omg.DynamicAny.DynAny compRewind = null; 229 230 tc = orb.create_union_tc 231 ( 232 ID, 233 NAME, 234 orb.get_primitive_tc (TCKind.tk_short), 235 getUnionMembers () 236 ); 237 dynAny = createDynAnyFromTypeCode (tc); 238 239 msg = "The number of components returned from the "; 241 msg += "DynAny::component_count method is incorrect"; 242 try 243 { 244 compCount = dynAny.component_count (); 245 } 246 catch (Throwable ex) 247 { 248 fail ("Unexpected error raised by DynAny::component_count operation"); 250 } 251 assertEquals (msg, 2, compCount); 252 253 msg = "The DynAny::seek operation indicates a valid current position "; 255 msg += "when the current position should be invalid"; 256 seek = dynAny.seek (-1); 257 assertTrue (msg, !seek); 258 259 msg = "The DynAny::seek operation indicates an invalid current position "; 261 msg += "when the current position should be valid"; 262 seek = dynAny.seek (0); 263 assertTrue (msg, seek); 264 265 try 267 { 268 compSeek = dynAny.current_component (); 269 } 270 catch (Throwable ex) 271 { 272 msg = "Failed to get the current component using the "; 273 msg += "DynAny::current_component operation after calling the "; 274 msg += "DynAny::seek operation"; 275 fail (msg); 276 } 277 278 msg = "The DynAny::next operation indicates an invalid current position "; 280 msg += "when the current position should be valid"; 281 seek = dynAny.next (); 282 assertTrue (msg, seek); 283 284 dynAny.rewind (); 286 287 try 289 { 290 compRewind = dynAny.current_component (); 291 } 292 catch (Throwable ex) 293 { 294 msg = "Failed to get the current component using the "; 295 msg += "DynAny::current_component operation after calling the "; 296 msg += "DynAny::rewind operation"; 297 fail (msg); 298 } 299 msg = "The component at DynAny::seek(0) is not equal to the "; 300 msg += "component at DynAny::rewind"; 301 assertTrue (msg, compSeek.equal (compRewind)); 302 } 303 304 305 309 public void testIterateDynAnyUnamedMember () 310 { 311 String msg; 312 int compCount = -1; 313 boolean seek; 314 UnionNoDefaultIntType type; 315 org.omg.CORBA.Any any = null; 316 org.omg.CORBA.TypeCode tc = null; 317 org.omg.DynamicAny.DynUnion dynAny = null; 318 org.omg.DynamicAny.DynAny disc = null; 319 320 type = new UnionNoDefaultIntType (); 321 322 Class unionClass = type.getClass (); 324 Method method = null; 325 326 try 328 { 329 method = unionClass.getDeclaredMethod ("__default", new Class [0]); 330 } 331 catch (NoSuchMethodException ex) 332 { 333 try 334 { 335 method = unionClass.getDeclaredMethod ("_default", new Class [0]); 336 } 337 catch (Throwable e) 338 { 339 msg = "Unexpected error trying to determine method to invoke: " + e; 340 fail (msg); 341 } 342 } 343 344 try 346 { 347 method.invoke (type, new Object [0]); 348 } 349 catch (Throwable ex) 350 { 351 fail ("Failed to invoke method via reflection: " + ex); 352 } 353 354 any = orb.create_any (); 355 UnionNoDefaultIntTypeHelper.insert (any, type); 356 dynAny = createDynAnyFromAny (any); 357 358 disc = dynAny.get_discriminator (); 360 361 msg = "The discriminator is incorrectly set for a DynAny created "; 363 msg += "from a union with no active member"; 364 assertEquals (msg, (short) 4, disc.to_any ().extract_short ()); 365 366 try 368 { 369 compCount = dynAny.component_count (); 370 } 371 catch (Throwable ex) 372 { 373 fail ("Unexpected error raised by DynAny::component_count operation"); 375 } 376 377 msg = "The number of components returned from the "; 378 msg += "DynAny::component_count operation is incorrect"; 379 assertEquals (msg, 1, compCount); 380 381 msg = "The DynAny::seek operation indicates an invalid current position "; 383 msg += "when the current position should be valid"; 384 seek = dynAny.seek (0); 385 assertTrue (msg, seek); 386 387 msg = "The DynAny::next operation indicates a valid current position "; 389 msg += "when the current position should be invalid"; 390 seek = dynAny.next (); 391 assertTrue (msg, !seek); 392 393 msg = "The object returned from the DynAny::current_component "; 395 msg += "operation should be null because the current position is "; 396 msg += "invalid (-1)"; 397 try 398 { 399 dynAny = (org.omg.DynamicAny.DynUnion ) dynAny.current_component (); 400 401 assertNull (msg, dynAny); 402 } 403 catch (Throwable ex) 404 { 405 fail (msg + ": " + ex); 406 } 407 } 408 409 410 413 public void testAccessUnionDisc () 414 { 415 String msg; 416 TCKind discKind = null; 417 org.omg.CORBA.TypeCode tc = null; 418 org.omg.DynamicAny.DynUnion dynAny = null; 419 org.omg.DynamicAny.DynAny disc = null; 420 org.omg.DynamicAny.DynAny invalidDisc = null; 421 422 tc = UnionDefaultIntTypeHelper.type (); 423 dynAny = createDynAnyFromTypeCode (tc); 424 425 tc = orb.get_primitive_tc (TCKind.tk_short); 427 try 428 { 429 disc = factory.create_dyn_any_from_type_code (tc); 430 } 431 catch (Throwable ex) 432 { 433 fail ("Failed to create DynAny with correct TypeCode: " + ex); 434 } 435 436 try 437 { 438 disc.insert_short ((short) 1); } 440 catch (Throwable ex) 441 { 442 fail ("Failed to set value of discriminator: " + ex); 443 } 444 445 try 446 { 447 dynAny.set_discriminator (disc); 448 } 449 catch (Throwable ex) 450 { 451 msg = "Failed to set the value of the discriminator using the "; 452 msg += "DynUnion::set_discriminator operation"; 453 fail (msg + ": " + ex); 454 } 455 456 msg = "Failed to get the correct value of the discriminator using the "; 458 msg += "DynUnion::get_discriminator operation"; 459 disc = dynAny.get_discriminator (); 460 461 assertEquals (msg, (short) 1, disc.to_any ().extract_short ()); 463 464 msg = "Failed to get the correct kind of the discriminator using "; 466 msg += "DynUnion::discriminator_kind operation"; 467 discKind = dynAny.discriminator_kind (); 468 469 assertEquals (msg, TCKind._tk_short, discKind.value ()); 471 472 tc = orb.get_primitive_tc (TCKind.tk_boolean); 474 try 475 { 476 invalidDisc = factory.create_dyn_any_from_type_code (tc); 477 } 478 catch (Throwable ex) 479 { 480 fail ("Failed to create DynAny with incorrect TypeCode: " + ex); 481 } 482 483 try 484 { 485 dynAny.set_discriminator (invalidDisc); 486 487 msg = "Failed to throw a TypeMismatch exception when calling "; 488 msg += "DynStruct::set_discriminator operation with an incorrect "; 489 msg += "TypeCode"; 490 fail (msg); 491 } 492 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 493 { 494 } 496 } 497 498 499 503 public void testUnionDefaultCase () 504 { 505 String msg; 506 int compCount = -1; 507 short discVal = 0; 508 UnionDefaultIntType type; 509 boolean hasNoActiveMember = true; 510 org.omg.CORBA.Any any = null; 511 org.omg.DynamicAny.DynAny disc = null; 512 org.omg.DynamicAny.DynUnion dynAny = null; 513 514 type = new UnionDefaultIntType (); 515 type.win (10); 516 any = orb.create_any (); 517 UnionDefaultIntTypeHelper.insert (any, type); 518 dynAny = createDynAnyFromAny (any); 519 520 msg = "Failed to set the discriminator to the default member using the "; 522 msg += "DynUnion::set_to_default_member operation"; 523 try 524 { 525 dynAny.set_to_default_member (); 526 } 527 catch (Throwable ex) 528 { 529 fail (msg + ": " + ex); 530 } 531 disc = dynAny.get_discriminator (); 532 533 536 discVal = disc.to_any ().extract_short (); 538 assertTrue (msg, discVal < 0 || discVal > 3); 539 540 try 542 { 543 compCount = dynAny.component_count (); 544 } 545 catch (Throwable ex) 546 { 547 fail ("Unexpected error raised by DynAny::component_count operation"); 549 } 550 msg = "Wrong number of components returned from DynAny::component_count "; 551 msg += "operation after calling DynUnion::set_to_default_member "; 552 msg += "operation"; 553 assertEquals (msg, 2, compCount); 554 555 try 557 { 558 dynAny.set_to_no_active_member (); 559 560 msg = "Failed to raise a TypeMismatch exception when calling the "; 561 msg += "DynUnion::set_to_no_active_member operation on a union with "; 562 msg += "an explicit default case"; 563 fail (msg); 564 } 565 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 566 { 567 } 569 570 msg = "The DynUnion::has_no_active_member operation did not return "; 571 msg += "FALSE when called on a union with an explicit default case"; 572 try 573 { 574 hasNoActiveMember = dynAny.has_no_active_member (); 575 } 576 catch (Throwable ex) 577 { 578 fail (msg + ": " + ex); 579 } 580 assertTrue (msg, !hasNoActiveMember); 581 } 582 583 584 588 public void testUnionNoDefaultCase () 589 { 590 String msg; 591 int compCount = -1; 592 UnionNoDefaultIntType type; 593 boolean hasNoActiveMember = false; 594 org.omg.CORBA.Any any = null; 595 org.omg.DynamicAny.DynUnion dynAny = null; 596 org.omg.DynamicAny.DynAny disc = null; 597 598 type = new UnionNoDefaultIntType (); 599 type.win (10); 600 any = orb.create_any (); 601 UnionNoDefaultIntTypeHelper.insert (any, type); 602 dynAny = createDynAnyFromAny (any); 603 604 msg = "Failed to set the discriminator to no active member using the "; 606 msg += "DynUnion::set_to_no_active_member operation"; 607 try 608 { 609 dynAny.set_to_no_active_member (); 610 } 611 catch (Throwable ex) 612 { 613 fail (msg + ": " + ex); 614 } 615 disc = dynAny.get_discriminator (); 616 617 assertEquals (msg, (short) 4, disc.to_any ().extract_short ()); 619 620 try 622 { 623 compCount = dynAny.component_count (); 624 } 625 catch (Throwable ex) 626 { 627 fail ("Unexpected error raised by DynAny::component_count operation"); 629 } 630 631 msg = "Wrong number of components returned from DynAny::component_count "; 632 msg += "operation after calling DynUnion::set_to_no_active_member "; 633 msg += "operation"; 634 assertEquals (msg, 1, compCount); 635 636 try 638 { 639 dynAny.set_to_default_member (); 640 641 msg = "Failed to raise a TypeMismatch exception when calling the "; 642 msg += "DynUnion::set_to_default_member operation on a union without "; 643 msg += "an explicit default case"; 644 fail (msg); 645 } 646 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 647 { 648 } 650 651 msg = "The DynUnion::has_no_active_member operation did not return "; 652 msg += "TRUE when called on a union without an explicit default case"; 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 UnionDefaultIntType 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 UnionDefaultIntType (); 681 type.win (testVal); 682 any = orb.create_any (); 683 UnionDefaultIntTypeHelper.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 = UnionNoDefaultIntTypeHelper.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 orb.get_primitive_tc (TCKind.tk_short), 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 UnionDefaultIntType 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 = UnionDefaultIntTypeHelper.type (); 840 dynAny = createDynAnyFromTypeCode (tc); 841 842 type = new UnionDefaultIntType (); 843 type.win (10); 844 any = orb.create_any (); 845 UnionDefaultIntTypeHelper.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 UnionDefaultIntType 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 = UnionDefaultIntTypeHelper.type (); 875 dynAny = createDynAnyFromTypeCode (tc); 876 877 type = new UnionDefaultIntType (); 878 type.win (10); 879 any = orb.create_any (); 880 UnionDefaultIntTypeHelper.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 orb.get_primitive_tc (TCKind.tk_short), 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 = UnionDefaultIntTypeHelper.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 UnionDefaultIntType type; 970 org.omg.CORBA.Any any = null; 971 org.omg.DynamicAny.DynUnion dynAny = null; 972 973 type = new UnionDefaultIntType (); 974 type.win (10); 975 any = orb.create_any (); 976 UnionDefaultIntTypeHelper.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 UnionDefaultIntType 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 UnionDefaultIntType (); 1026 type.win (10); 1027 any = orb.create_any (); 1028 UnionDefaultIntTypeHelper.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 = UnionDefaultIntTypeHelper.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 any.insert_short ((short) 0); 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 any.insert_short ((short) 1); 1193 members [1] = new org.omg.CORBA.UnionMember 1194 ( 1195 "one", 1196 any, 1197 orb.get_primitive_tc (TCKind.tk_char), 1198 null 1199 ); 1200 1201 return members; 1202 } 1203 1204} 1205 | Popular Tags |