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.UnboundedDataHelper; 29 30 36 37 public class DynAnyUnboundedSeqTest extends TestCase 38 { 39 private static org.omg.DynamicAny.DynAnyFactory factory = null; 40 private static org.omg.CORBA.ORB orb = null; 41 42 public DynAnyUnboundedSeqTest (String name) 43 { 44 super (name); 45 } 46 47 48 public static Test suite () 49 { 50 TestSuite suite = new TestSuite ("Unbounded DynSequence Tests"); 51 Setup setup = new Setup (suite); 52 ORBSetup osetup = new ORBSetup (setup); 53 54 suite.addTest (new DynAnyUnboundedSeqTest ("testFactoryCreateFromAny")); 55 suite.addTest (new DynAnyUnboundedSeqTest ("testFactoryCreateFromTypeCode")); 56 suite.addTest (new DynAnyUnboundedSeqTest ("testFactoryCreateFromIDLTypeCode")); 57 suite.addTest (new DynAnyUnboundedSeqTest ("testCompareDynAny")); 58 suite.addTest (new DynAnyUnboundedSeqTest ("testIterateDynAny")); 59 suite.addTest (new DynAnyUnboundedSeqTest ("testAccessSeqElements")); 60 suite.addTest (new DynAnyUnboundedSeqTest ("testAccessSeqDynAnyElements")); 61 suite.addTest (new DynAnyUnboundedSeqTest ("testAccessSeqElementsEx")); 62 suite.addTest (new DynAnyUnboundedSeqTest ("testDynAnyTypeCode")); 63 suite.addTest (new DynAnyUnboundedSeqTest ("testInitDynAnyFromDynAny")); 64 suite.addTest (new DynAnyUnboundedSeqTest ("testInitDynAnyFromAny")); 65 suite.addTest (new DynAnyUnboundedSeqTest ("testInitFromAnyTypeMismatchEx")); 66 suite.addTest (new DynAnyUnboundedSeqTest ("testGenerateAnyFromDynAny")); 67 suite.addTest (new DynAnyUnboundedSeqTest ("testDestroyDynAny")); 68 suite.addTest (new DynAnyUnboundedSeqTest ("testDestroyComponent")); 69 suite.addTest (new DynAnyUnboundedSeqTest ("testCopyDynAny")); 70 71 return osetup; 72 } 73 74 75 79 public void testFactoryCreateFromAny () 80 { 81 int [] type = null; 82 org.omg.CORBA.Any any = null; 83 84 type = getIntSeq (10); 85 any = orb.create_any (); 86 UnboundedDataHelper.insert (any, type); 87 88 createDynAnyFromAny (any); 89 } 90 91 92 96 public void testFactoryCreateFromTypeCode () 97 { 98 org.omg.CORBA.TypeCode tc = null; 99 100 tc = orb.get_primitive_tc (TCKind.tk_long); 101 tc = orb.create_sequence_tc (0, tc); 102 createDynAnyFromTypeCode (tc); 103 } 104 105 106 110 public void testFactoryCreateFromIDLTypeCode () 111 { 112 org.omg.CORBA.TypeCode tc = null; 113 114 tc = UnboundedDataHelper.type (); 115 createDynAnyFromTypeCode (tc); 116 } 117 118 119 122 public void testCompareDynAny () 123 { 124 String msg; 125 int [] type; 126 org.omg.CORBA.Any any = null; 127 org.omg.DynamicAny.DynSequence dynAny = null; 128 org.omg.DynamicAny.DynSequence dynAny2 = null; 129 130 type = getIntSeq (10); 131 any = orb.create_any (); 132 UnboundedDataHelper.insert (any, type); 133 dynAny = createDynAnyFromAny (any); 134 dynAny2 = createDynAnyFromAny (any); 135 136 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 137 assertTrue (msg, dynAny.equal (dynAny2)); 138 } 139 140 141 144 public void testIterateDynAny () 145 { 146 String msg; 147 int [] type; 148 int compCount = -1; 149 int len = 10; 150 boolean seek; 151 org.omg.CORBA.Any any = null; 152 org.omg.CORBA.TypeCode tc = null; 153 org.omg.DynamicAny.DynSequence dynAny = null; 154 org.omg.DynamicAny.DynAny compSeek = null; 155 org.omg.DynamicAny.DynAny compRewind = null; 156 157 type = getIntSeq (len); 158 any = orb.create_any (); 159 UnboundedDataHelper.insert (any, type); 160 dynAny = createDynAnyFromAny (any); 161 162 msg = "The number of components returned from the "; 164 msg += "DynAny::component_count method is incorrect"; 165 try 166 { 167 compCount = dynAny.component_count (); 168 } 169 catch (Throwable ex) 170 { 171 fail ("Unexpected error raised by DynAny::component_count operation"); 173 } 174 assertEquals (msg, len, compCount); 175 176 msg = "The DynAny::seek operation indicates a valid current position "; 178 msg += "when the current position should be invalid"; 179 seek = dynAny.seek (-1); 180 assertTrue (msg, !seek); 181 182 msg = "The DynAny::seek operation indicates an invalid current position "; 184 msg += "when the current position should be valid"; 185 seek = dynAny.seek (0); 186 assertTrue (msg, seek); 187 188 try 190 { 191 compSeek = dynAny.current_component (); 192 } 193 catch (Throwable ex) 194 { 195 msg = "Failed to get the current component using the "; 196 msg += "DynAny::current_component operation after calling the "; 197 msg += "DynAny::seek operation"; 198 fail (msg + ": " + ex); 199 } 200 201 msg = "The DynAny::next operation indicates an invalid current position "; 203 msg += "when the current position should be valid"; 204 seek = dynAny.next (); 205 assertTrue (msg, seek); 206 207 dynAny.rewind (); 209 210 try 212 { 213 compRewind = dynAny.current_component (); 214 } 215 catch (Throwable ex) 216 { 217 msg = "Failed to get the current component using the "; 218 msg += "DynAny::current_component operation after calling the "; 219 msg += "DynAny::rewind operation"; 220 fail (msg + ": " + ex); 221 } 222 msg = "The component at DynAny::seek(0) is not equal to the "; 223 msg += "component at DynAny::rewind"; 224 assertTrue (msg, compSeek.equal (compRewind)); 225 } 226 227 228 231 public void testAccessSeqElements () 232 { 233 String msg; 234 int len; 235 int newLen; 236 int curVal; 237 boolean next; 238 org.omg.CORBA.TypeCode tc = null; 239 org.omg.DynamicAny.DynSequence dynAny = null; 240 org.omg.CORBA.Any [] anys = null; 241 242 tc = UnboundedDataHelper.type (); 243 dynAny = createDynAnyFromTypeCode (tc); 244 245 msg = "The default length of DynSequence created from a TypeCode is "; 247 msg += "incorrect"; 248 len = dynAny.get_length (); 249 assertEquals (msg, 0, len); 250 251 len = 5; 253 anys = new org.omg.CORBA.Any [len]; 254 for (int i = 0; i < len; i++) 255 { 256 anys [i] = orb.create_any (); 257 anys [i].insert_long (i); 258 } 259 260 try 261 { 262 dynAny.set_elements (anys); 263 } 264 catch (Throwable ex) 265 { 266 msg = "Failed to set the elements using the DynSequence:set_elements "; 267 msg += "operation"; 268 fail (msg + ": " + ex); 269 } 270 271 newLen = 10; 273 try 274 { 275 dynAny.set_length (newLen); 276 } 277 catch (Throwable ex) 278 { 279 msg = "Failed to set the length of the sequence using "; 280 msg += "DynSequence::set_length operation"; 281 fail (msg + ": " + ex); 282 } 283 284 for (int i = 0; i < newLen; i++) 285 { 286 msg = "Failed to get the correct value of the DynSequence at "; 287 msg += "position " + i; 288 curVal = -1; 289 try 290 { 291 curVal = dynAny.get_long (); 292 } 293 catch (Throwable ex) 294 { 295 fail (msg + ": " + ex); 296 } 297 298 if (i < len) 299 { 300 assertEquals (msg, i, curVal); 301 } 302 else 303 { 304 assertEquals (msg, 0, curVal); 305 } 306 dynAny.next (); 307 } 308 309 newLen = 1; 311 try 312 { 313 dynAny.set_length (newLen); 314 } 315 catch (Throwable ex) 316 { 317 msg = "Failed to set the length of the sequence using "; 318 msg += "DynSequence::set_length operation"; 319 fail (msg + ": " + ex); 320 } 321 anys = dynAny.get_elements (); 322 msg = "The wrong number of elements were returned from the "; 323 msg += "DynSequence::get_elements operation"; 324 assertEquals (msg, newLen, anys.length); 325 326 msg = "Failed to get the correct value of a DynSequence"; 327 curVal = anys [0].extract_long (); 328 assertEquals (msg, 0, curVal); 329 } 330 331 332 335 public void testAccessSeqDynAnyElements () 336 { 337 String msg; 338 int len; 339 int newLen; 340 int curVal; 341 boolean next; 342 org.omg.CORBA.TypeCode tc = null; 343 org.omg.DynamicAny.DynSequence dynAny = null; 344 org.omg.DynamicAny.DynAny [] dynAnys = null; 345 346 tc = UnboundedDataHelper.type (); 347 dynAny = createDynAnyFromTypeCode (tc); 348 349 msg = "The default length of DynSequence created from a TypeCode is "; 351 msg += "incorrect"; 352 len = dynAny.get_length (); 353 assertEquals (msg, 0, len); 354 355 len = 5; 357 dynAnys = new org.omg.DynamicAny.DynAny [len]; 358 tc = orb.get_primitive_tc (TCKind.tk_long); 359 for (int i = 0; i < len; i++) 360 { 361 try 362 { 363 dynAnys [i] = factory.create_dyn_any_from_type_code (tc); 364 } 365 catch (Throwable ex) 366 { 367 fail ("Failed to create a DynAny at position " + i + ": " + ex); 368 } 369 370 try 371 { 372 dynAnys [i].insert_long (i); 373 } 374 catch (Throwable ex) 375 { 376 msg = "Failed to insert a value into a DynAny at position " + i; 377 msg += ": " + ex; 378 fail (msg); 379 } 380 } 381 382 try 383 { 384 dynAny.set_elements_as_dyn_any (dynAnys); 385 } 386 catch (Throwable ex) 387 { 388 msg = "Failed to set the elements using the "; 389 msg += "DynSequence:set_elements_as_dyn_any operation"; 390 fail (msg + ": " + ex); 391 } 392 393 newLen = 10; 395 try 396 { 397 dynAny.set_length (newLen); 398 } 399 catch (Throwable ex) 400 { 401 msg = "Failed to set the length of the sequence using "; 402 msg += "DynSequence::set_length operation"; 403 fail (msg + ": " + ex); 404 } 405 406 for (int i = 0; i < newLen; i++) 407 { 408 msg = "Failed to get the correct value of the DynSequence at "; 409 msg += "position " + i; 410 curVal = -1; 411 try 412 { 413 curVal = dynAny.get_long (); 414 } 415 catch (Throwable ex) 416 { 417 fail (msg + ": " + ex); 418 } 419 420 if (i < len) 421 { 422 assertEquals (msg, i, curVal); 423 } 424 else 425 { 426 assertEquals (msg, 0, curVal); 427 } 428 dynAny.next (); 429 } 430 431 newLen = 1; 433 try 434 { 435 dynAny.set_length (newLen); 436 } 437 catch (Throwable ex) 438 { 439 msg = "Failed to set the length of the sequence using "; 440 msg += "DynSequence::set_length operation"; 441 fail (msg + ": " + ex); 442 } 443 dynAnys = dynAny.get_elements_as_dyn_any (); 444 445 msg = "The wrong number of elements were returned from the "; 446 msg += "DynSequence::get_elements_as_dyn_any operation"; 447 assertEquals (msg, newLen, dynAnys.length); 448 449 msg = "Failed to get the correct value of a DynSequence"; 450 curVal = -1; 451 try 452 { 453 curVal = dynAnys [0].get_long (); 454 } 455 catch (Throwable ex) 456 { 457 fail (msg + ": " + ex); 458 } 459 assertEquals (msg, 0, curVal); 460 } 461 462 463 467 public void testAccessSeqElementsEx () 468 { 469 String msg; 470 org.omg.CORBA.TypeCode tc = null; 471 org.omg.DynamicAny.DynSequence dynAny = null; 472 org.omg.CORBA.Any [] anys = null; 473 474 tc = UnboundedDataHelper.type (); 475 dynAny = createDynAnyFromTypeCode (tc); 476 477 anys = new org.omg.CORBA.Any [1]; 479 anys [0] = orb.create_any (); 480 anys [0].insert_string ("BadType"); 481 482 msg = "Failed to raise a TypeMismatch exception when setting a "; 483 msg += "DynSequence object with components of the wrong type using "; 484 msg += "DynSequence::set_elements"; 485 try 486 { 487 dynAny.set_elements (anys); 488 489 fail (msg); 490 } 491 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 492 { 493 } 495 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 496 { 497 fail (msg + ": " + ex); 498 } 499 } 500 501 502 505 public void testDynAnyTypeCode () 506 { 507 String msg; 508 org.omg.CORBA.TypeCode tc = null; 509 org.omg.DynamicAny.DynSequence dynAny = null; 510 511 tc = orb.get_primitive_tc (TCKind.tk_long); 512 tc = orb.create_sequence_tc (0, tc); 513 dynAny = createDynAnyFromTypeCode (tc); 514 515 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 516 assertTrue (msg, dynAny.type ().equal (tc)); 517 } 518 519 520 523 public void testInitDynAnyFromDynAny () 524 { 525 String msg; 526 int [] type; 527 org.omg.CORBA.Any any = null; 528 org.omg.CORBA.TypeCode tc = null; 529 org.omg.DynamicAny.DynSequence dynAny = null; 530 org.omg.DynamicAny.DynSequence dynAny2 = null; 531 532 tc = UnboundedDataHelper.type (); 533 dynAny = createDynAnyFromTypeCode (tc); 534 535 type = getIntSeq (10); 536 any = orb.create_any (); 537 UnboundedDataHelper.insert (any, type); 538 dynAny2 = createDynAnyFromAny (any); 539 540 msg = "Failed to initialize a DynAny object from another DynAny "; 541 msg += "object using the DynAny::assign operation"; 542 try 543 { 544 dynAny.assign (dynAny2); 545 } 546 catch (Throwable ex) 547 { 548 fail (msg + ": " + ex); 549 } 550 assertTrue (msg, dynAny.equal (dynAny2)); 551 } 552 553 554 557 public void testInitDynAnyFromAny () 558 { 559 String msg; 560 int [] type; 561 org.omg.CORBA.Any any = null; 562 org.omg.CORBA.TypeCode tc = null; 563 org.omg.DynamicAny.DynSequence dynAny = null; 564 org.omg.DynamicAny.DynSequence dynAny2 = null; 565 566 tc = UnboundedDataHelper.type (); 567 dynAny = createDynAnyFromTypeCode (tc); 568 569 type = getIntSeq (10); 570 any = orb.create_any (); 571 UnboundedDataHelper.insert (any, type); 572 dynAny2 = createDynAnyFromAny (any); 573 574 msg = "Failed to initialize a DynAny object from an Any object "; 575 msg += "using the DynAny::from_any operation"; 576 try 577 { 578 dynAny.from_any (any); 579 } 580 catch (Throwable ex) 581 { 582 fail (msg + ": " + ex); 583 } 584 assertTrue (msg, dynAny.equal (dynAny2)); 585 } 586 587 588 592 public void testInitFromAnyTypeMismatchEx () 593 { 594 String msg; 595 org.omg.CORBA.Any any = null; 596 org.omg.CORBA.TypeCode tc = null; 597 org.omg.DynamicAny.DynSequence dynAny = null; 598 599 any = orb.create_any (); 600 any.insert_string ("Hello"); 601 602 tc = orb.get_primitive_tc (TCKind.tk_long); 603 tc = orb.create_sequence_tc (0, tc); 604 dynAny = createDynAnyFromTypeCode (tc); 605 606 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 607 msg += "operation when DynAny and Any operands have different types"; 608 try 609 { 610 dynAny.from_any (any); 611 612 fail (msg); 613 } 614 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 615 { 616 } 618 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 619 { 620 fail (msg + ": " + ex); 621 } 622 } 623 624 625 628 public void testGenerateAnyFromDynAny () 629 { 630 String msg; 631 org.omg.CORBA.Any any = null; 632 org.omg.CORBA.TypeCode tc = null; 633 org.omg.DynamicAny.DynSequence dynAny = null; 634 org.omg.DynamicAny.DynSequence dynAny2 = null; 635 636 tc = UnboundedDataHelper.type (); 637 dynAny = createDynAnyFromTypeCode (tc); 638 639 any = orb.create_any (); 640 any = dynAny.to_any (); 641 dynAny2 = createDynAnyFromAny (any); 642 643 msg = "The DynAny::to_any operation failed to create an Any "; 644 msg += "object with the same value as the DynAny object"; 645 assertTrue (msg, dynAny.equal (dynAny2)); 646 } 647 648 649 652 public void testDestroyDynAny () 653 { 654 String msg; 655 int [] type; 656 org.omg.CORBA.Any any = null; 657 org.omg.DynamicAny.DynSequence dynAny = null; 658 659 type = getIntSeq (10); 660 any = orb.create_any (); 661 UnboundedDataHelper.insert (any, type); 662 dynAny = createDynAnyFromAny (any); 663 dynAny.destroy (); 664 665 try 666 { 667 dynAny.type (); 668 669 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 670 msg += "calling DynAny::type operation on a destroyed DynAny object "; 671 msg += "did not raise OBJECT_NOT_EXIST exception"; 672 fail (msg); 673 } 674 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 675 { 676 } 678 679 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 680 msg += "calling DynAny::current_component operation on a destroyed "; 681 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 682 try 683 { 684 dynAny.current_component (); 685 686 fail (msg); 687 } 688 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 689 { 690 } 692 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 693 { 694 fail (msg + ": " + ex); 695 } 696 } 697 698 699 702 public void testDestroyComponent () 703 { 704 String msg; 705 int [] type; 706 org.omg.CORBA.Any any = null; 707 org.omg.DynamicAny.DynSequence dynAny = null; 708 org.omg.DynamicAny.DynAny comp = null; 709 710 type = getIntSeq (10); 711 any = orb.create_any (); 712 UnboundedDataHelper.insert (any, type); 713 dynAny = createDynAnyFromAny (any); 714 715 try 716 { 717 comp = dynAny.current_component (); 718 } 719 catch (Throwable ex) 720 { 721 msg = "Failed to get the current component of the DynAny using the "; 722 msg += "DynAny::current_component operation before calling the "; 723 msg += "DynAny::destroy operation"; 724 fail (msg + ": " + ex); 725 } 726 727 comp.destroy (); 728 try 729 { 730 comp = dynAny.current_component (); 731 } 732 catch (Throwable ex) 733 { 734 msg = "Failed to get the current component of the DynAny using the "; 735 msg += "DynAny::current_component operation after calling the "; 736 msg += "DynAny::destroy operation"; 737 fail (msg + ": " + ex); 738 } 739 740 try 741 { 742 comp.type (); 743 } 744 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 745 { 746 msg = "Calling destroy on a component resulted in destroying the "; 747 msg += "component object"; 748 fail (msg + ": " + ex); 749 } 750 } 751 752 753 756 public void testCopyDynAny () 757 { 758 String msg; 759 org.omg.CORBA.TypeCode tc = null; 760 org.omg.DynamicAny.DynSequence dynAny = null; 761 org.omg.DynamicAny.DynSequence dynAny2 = null; 762 763 tc = UnboundedDataHelper.type (); 764 dynAny = createDynAnyFromTypeCode (tc); 765 dynAny2 = (org.omg.DynamicAny.DynSequence ) dynAny.copy (); 766 767 msg = "The DynAny object created with the DynAny::copy operation "; 768 msg += "is not equal to the DynAny object it was copied from"; 769 assertTrue (msg, dynAny.equal (dynAny2)); 770 } 771 772 773 private static class Setup extends TestSetup 774 { 775 public Setup (Test test) 776 { 777 super (test); 778 } 779 780 protected void setUp () 781 { 782 org.omg.CORBA.Object obj = null; 783 784 orb = ORBSetup.getORB (); 785 try 786 { 787 obj = orb.resolve_initial_references ("DynAnyFactory"); 788 } 789 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 790 { 791 fail ("Failed to resolve DynAnyFactory: " + ex); 792 } 793 try 794 { 795 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 796 } 797 catch (Throwable ex) 798 { 799 fail ("Failed to narrow to DynAnyFactory: " + ex); 800 } 801 } 802 803 protected void tearDown () 804 { 805 } 806 } 807 808 809 812 private static org.omg.DynamicAny.DynSequence createDynAnyFromAny 813 (org.omg.CORBA.Any any) 814 { 815 String msg; 816 org.omg.DynamicAny.DynSequence dynAny = null; 817 818 try 819 { 820 dynAny = (org.omg.DynamicAny.DynSequence ) factory.create_dyn_any (any); 821 } 822 catch (Throwable ex) 823 { 824 msg = "Factory failed to create DynAny from Any using "; 825 msg += "DynAny::create_dyn_any operation: " + ex; 826 fail (msg); 827 } 828 return dynAny; 829 } 830 831 832 835 private static org.omg.DynamicAny.DynSequence createDynAnyFromTypeCode 836 (org.omg.CORBA.TypeCode tc) 837 { 838 String msg; 839 org.omg.DynamicAny.DynSequence dynAny = null; 840 841 try 842 { 843 dynAny = (org.omg.DynamicAny.DynSequence ) 844 factory.create_dyn_any_from_type_code (tc); 845 } 846 catch (Throwable ex) 847 { 848 msg = "Factory failed to create DynAny from TypeCode using "; 849 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 850 fail (msg); 851 } 852 return dynAny; 853 } 854 855 856 859 private static int [] getIntSeq (int length) 860 { 861 int [] type = new int [length]; 862 for (int i = 0; i < length; i++) 863 { 864 type [i] = i; 865 } 866 return type; 867 } 868 869 } 870 | Popular Tags |