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.Bound; 29 import org.jacorb.test.BoundedDataHelper; 30 31 37 38 public class DynAnyBoundedSeqTest extends TestCase 39 { 40 private static org.omg.DynamicAny.DynAnyFactory factory = null; 41 private static org.omg.CORBA.ORB orb = null; 42 43 public DynAnyBoundedSeqTest (String name) 44 { 45 super (name); 46 } 47 48 49 public static Test suite () 50 { 51 TestSuite suite = new TestSuite ("Bounded DynSequence Tests"); 52 Setup setup = new Setup (suite); 53 ORBSetup osetup = new ORBSetup (setup); 54 55 suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromAny")); 56 suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromTypeCode")); 57 suite.addTest (new DynAnyBoundedSeqTest ("testFactoryCreateFromIDLTypeCode")); 58 suite.addTest (new DynAnyBoundedSeqTest ("testCompareDynAny")); 59 suite.addTest (new DynAnyBoundedSeqTest ("testIterateDynAny")); 60 suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqElements")); 61 suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqDynAnyElements")); 62 suite.addTest (new DynAnyBoundedSeqTest ("testAccessSeqElementsEx")); 63 suite.addTest (new DynAnyBoundedSeqTest ("testDynAnyTypeCode")); 64 suite.addTest (new DynAnyBoundedSeqTest ("testInitDynAnyFromDynAny")); 65 suite.addTest (new DynAnyBoundedSeqTest ("testInitDynAnyFromAny")); 66 suite.addTest (new DynAnyBoundedSeqTest ("testInitFromAnyTypeMismatchEx")); 67 suite.addTest (new DynAnyBoundedSeqTest ("testGenerateAnyFromDynAny")); 68 suite.addTest (new DynAnyBoundedSeqTest ("testDestroyDynAny")); 69 suite.addTest (new DynAnyBoundedSeqTest ("testDestroyComponent")); 70 suite.addTest (new DynAnyBoundedSeqTest ("testCopyDynAny")); 71 72 return osetup; 73 } 74 75 76 80 public void testFactoryCreateFromAny () 81 { 82 int [] type = null; 83 org.omg.CORBA.Any any = null; 84 85 type = getIntSeq (); 86 any = orb.create_any (); 87 BoundedDataHelper.insert (any, type); 88 89 createDynAnyFromAny (any); 90 } 91 92 93 97 public void testFactoryCreateFromTypeCode () 98 { 99 org.omg.CORBA.TypeCode tc = null; 100 101 tc = orb.get_primitive_tc (TCKind.tk_long); 102 tc = orb.create_sequence_tc (Bound.value, tc); 103 createDynAnyFromTypeCode (tc); 104 } 105 106 107 111 public void testFactoryCreateFromIDLTypeCode () 112 { 113 org.omg.CORBA.TypeCode tc = null; 114 115 tc = BoundedDataHelper.type (); 116 createDynAnyFromTypeCode (tc); 117 } 118 119 120 123 public void testCompareDynAny () 124 { 125 String msg; 126 int [] type; 127 org.omg.CORBA.Any any = null; 128 org.omg.DynamicAny.DynSequence dynAny = null; 129 org.omg.DynamicAny.DynSequence dynAny2 = null; 130 131 type = getIntSeq (); 132 any = orb.create_any (); 133 BoundedDataHelper.insert (any, type); 134 dynAny = createDynAnyFromAny (any); 135 dynAny2 = createDynAnyFromAny (any); 136 137 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 138 assertTrue (msg, dynAny.equal (dynAny2)); 139 } 140 141 142 145 public void testIterateDynAny () 146 { 147 String msg; 148 int [] type; 149 int compCount = -1; 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 (); 158 any = orb.create_any (); 159 BoundedDataHelper.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, Bound.value, 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 = BoundedDataHelper.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 = Bound.value / 2; 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 = Bound.value; 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 = BoundedDataHelper.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 = Bound.value / 2; 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 = Bound.value; 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 int len; 471 org.omg.CORBA.TypeCode tc = null; 472 org.omg.DynamicAny.DynSequence dynAny = null; 473 org.omg.CORBA.Any [] anys = null; 474 475 tc = BoundedDataHelper.type (); 476 dynAny = createDynAnyFromTypeCode (tc); 477 478 msg = "Failed to raise an InvalidValue exception when setting the "; 480 msg += "length of a DynSequence object beyond its bounds using "; 481 msg += "DynSequence::set_length"; 482 try 483 { 484 dynAny.set_length (Bound.value + 1); 485 } 486 catch (Throwable ex) 487 { 488 if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.InvalidValue )) 489 { 490 fail (msg + ": " + ex); 491 } 492 } 493 494 anys = new org.omg.CORBA.Any [1]; 496 anys [0] = orb.create_any (); 497 anys [0].insert_string ("BadType"); 498 499 msg = "Failed to raise a TypeMismatch exception when setting a "; 500 msg += "DynSequence object with components of the wrong type using "; 501 msg += "DynSequence::set_elements"; 502 try 503 { 504 dynAny.set_elements (anys); 505 506 fail (msg); 507 } 508 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 509 { 510 } 512 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 513 { 514 fail (msg + ": " + ex); 515 } 516 517 len = Bound.value + 1; 519 anys = new org.omg.CORBA.Any [len]; 520 for (int i = 0; i < len; i++) 521 { 522 anys [i] = orb.create_any (); 523 anys [i].insert_long (i); 524 } 525 526 msg = "Failed to raise an InvalidValue exception when setting a "; 527 msg += "DynSequence object with more elements than the sequence bounds"; 528 msg += "using DynSequence::set_elements"; 529 try 530 { 531 dynAny.set_elements (anys); 532 } 533 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 534 { 535 } 537 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 538 { 539 fail (msg + ": " + ex); 540 } 541 } 542 543 544 547 public void testDynAnyTypeCode () 548 { 549 String msg; 550 org.omg.CORBA.TypeCode tc = null; 551 org.omg.DynamicAny.DynSequence dynAny = null; 552 553 tc = orb.get_primitive_tc (TCKind.tk_long); 554 tc = orb.create_sequence_tc (Bound.value, tc); 555 dynAny = createDynAnyFromTypeCode (tc); 556 557 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 558 assertTrue (msg, dynAny.type ().equal (tc)); 559 } 560 561 562 565 public void testInitDynAnyFromDynAny () 566 { 567 String msg; 568 int [] type; 569 org.omg.CORBA.Any any = null; 570 org.omg.CORBA.TypeCode tc = null; 571 org.omg.DynamicAny.DynSequence dynAny = null; 572 org.omg.DynamicAny.DynSequence dynAny2 = null; 573 574 tc = BoundedDataHelper.type (); 575 dynAny = createDynAnyFromTypeCode (tc); 576 577 type = getIntSeq (); 578 any = orb.create_any (); 579 BoundedDataHelper.insert (any, type); 580 dynAny2 = createDynAnyFromAny (any); 581 582 msg = "Failed to initialize a DynAny object from another DynAny "; 583 msg += "object using the DynAny::assign operation"; 584 try 585 { 586 dynAny.assign (dynAny2); 587 } 588 catch (Throwable ex) 589 { 590 fail (msg + ": " + ex); 591 } 592 assertTrue (msg, dynAny.equal (dynAny2)); 593 } 594 595 596 599 public void testInitDynAnyFromAny () 600 { 601 String msg; 602 int [] type; 603 org.omg.CORBA.Any any = null; 604 org.omg.CORBA.TypeCode tc = null; 605 org.omg.DynamicAny.DynSequence dynAny = null; 606 org.omg.DynamicAny.DynSequence dynAny2 = null; 607 608 tc = BoundedDataHelper.type (); 609 dynAny = createDynAnyFromTypeCode (tc); 610 611 type = getIntSeq (); 612 any = orb.create_any (); 613 BoundedDataHelper.insert (any, type); 614 dynAny2 = createDynAnyFromAny (any); 615 616 msg = "Failed to initialize a DynAny object from an Any object "; 617 msg += "using the DynAny::from_any operation"; 618 try 619 { 620 dynAny.from_any (any); 621 } 622 catch (Throwable ex) 623 { 624 fail (msg + ": " + ex); 625 } 626 assertTrue (msg, dynAny.equal (dynAny2)); 627 } 628 629 630 634 public void testInitFromAnyTypeMismatchEx () 635 { 636 String msg; 637 org.omg.CORBA.Any any = null; 638 org.omg.CORBA.TypeCode tc = null; 639 org.omg.DynamicAny.DynSequence dynAny = null; 640 641 any = orb.create_any (); 642 any.insert_string ("Hello"); 643 644 tc = orb.get_primitive_tc (TCKind.tk_long); 645 tc = orb.create_sequence_tc (Bound.value, tc); 646 dynAny = createDynAnyFromTypeCode (tc); 647 648 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 649 msg += "operation when DynAny and Any operands have different types"; 650 try 651 { 652 dynAny.from_any (any); 653 654 fail (msg); 655 } 656 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 657 { 658 } 660 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 661 { 662 fail (msg + ": " + ex); 663 } 664 } 665 666 667 670 public void testGenerateAnyFromDynAny () 671 { 672 String msg; 673 org.omg.CORBA.Any any = null; 674 org.omg.CORBA.TypeCode tc = null; 675 org.omg.DynamicAny.DynSequence dynAny = null; 676 org.omg.DynamicAny.DynSequence dynAny2 = null; 677 678 tc = BoundedDataHelper.type (); 679 dynAny = createDynAnyFromTypeCode (tc); 680 681 any = orb.create_any (); 682 any = dynAny.to_any (); 683 dynAny2 = createDynAnyFromAny (any); 684 685 msg = "The DynAny::to_any operation failed to create an Any "; 686 msg += "object with the same value as the DynAny object"; 687 assertTrue (msg, dynAny.equal (dynAny2)); 688 } 689 690 691 694 public void testDestroyDynAny () 695 { 696 String msg; 697 int [] type; 698 org.omg.CORBA.Any any = null; 699 org.omg.DynamicAny.DynSequence dynAny = null; 700 701 type = getIntSeq (); 702 any = orb.create_any (); 703 BoundedDataHelper.insert (any, type); 704 dynAny = createDynAnyFromAny (any); 705 dynAny.destroy (); 706 707 try 708 { 709 dynAny.type (); 710 711 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 712 msg += "calling DynAny::type operation on a destroyed DynAny object "; 713 msg += "did not raise OBJECT_NOT_EXIST exception"; 714 fail (msg); 715 } 716 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 717 { 718 } 720 721 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 722 msg += "calling DynAny::current_component operation on a destroyed "; 723 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 724 try 725 { 726 dynAny.current_component (); 727 728 fail (msg); 729 } 730 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 731 { 732 } 734 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 735 { 736 fail (msg + ": " + ex); 737 } 738 } 739 740 741 744 public void testDestroyComponent () 745 { 746 String msg; 747 int [] type; 748 org.omg.CORBA.Any any = null; 749 org.omg.DynamicAny.DynSequence dynAny = null; 750 org.omg.DynamicAny.DynAny comp = null; 751 752 type = getIntSeq (); 753 any = orb.create_any (); 754 BoundedDataHelper.insert (any, type); 755 dynAny = createDynAnyFromAny (any); 756 757 try 758 { 759 comp = dynAny.current_component (); 760 } 761 catch (Throwable ex) 762 { 763 msg = "Failed to get the current component of the DynAny using the "; 764 msg += "DynAny::current_component operation before calling the "; 765 msg += "DynAny::destroy operation"; 766 fail (msg + ": " + ex); 767 } 768 769 comp.destroy (); 770 try 771 { 772 comp = dynAny.current_component (); 773 } 774 catch (Throwable ex) 775 { 776 msg = "Failed to get the current component of the DynAny using the "; 777 msg += "DynAny::current_component operation after calling the "; 778 msg += "DynAny::destroy operation"; 779 fail (msg + ": " + ex); 780 } 781 782 try 783 { 784 comp.type (); 785 } 786 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 787 { 788 msg = "Calling destroy on a component resulted in destroying the "; 789 msg += "component object"; 790 fail (msg + ": " + ex); 791 } 792 } 793 794 795 798 public void testCopyDynAny () 799 { 800 String msg; 801 org.omg.CORBA.TypeCode tc = null; 802 org.omg.DynamicAny.DynSequence dynAny = null; 803 org.omg.DynamicAny.DynSequence dynAny2 = null; 804 805 tc = BoundedDataHelper.type (); 806 dynAny = createDynAnyFromTypeCode (tc); 807 dynAny2 = (org.omg.DynamicAny.DynSequence ) dynAny.copy (); 808 809 msg = "The DynAny object created with the DynAny::copy operation "; 810 msg += "is not equal to the DynAny object it was copied from"; 811 assertTrue (msg, dynAny.equal (dynAny2)); 812 } 813 814 815 private static class Setup extends TestSetup 816 { 817 public Setup (Test test) 818 { 819 super (test); 820 } 821 822 protected void setUp () 823 { 824 org.omg.CORBA.Object obj = null; 825 826 orb = ORBSetup.getORB (); 827 try 828 { 829 obj = orb.resolve_initial_references ("DynAnyFactory"); 830 } 831 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 832 { 833 fail ("Failed to resolve DynAnyFactory: " + ex); 834 } 835 try 836 { 837 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 838 } 839 catch (Throwable ex) 840 { 841 fail ("Failed to narrow to DynAnyFactory: " + ex); 842 } 843 } 844 845 protected void tearDown () 846 { 847 } 848 } 849 850 851 854 private static org.omg.DynamicAny.DynSequence createDynAnyFromAny 855 (org.omg.CORBA.Any any) 856 { 857 String msg; 858 org.omg.DynamicAny.DynSequence dynAny = null; 859 860 try 861 { 862 dynAny = (org.omg.DynamicAny.DynSequence ) factory.create_dyn_any (any); 863 } 864 catch (Throwable ex) 865 { 866 msg = "Factory failed to create DynAny from Any using "; 867 msg += "DynAny::create_dyn_any operation: " + ex; 868 fail (msg); 869 } 870 return dynAny; 871 } 872 873 874 877 private static org.omg.DynamicAny.DynSequence createDynAnyFromTypeCode 878 (org.omg.CORBA.TypeCode tc) 879 { 880 String msg; 881 org.omg.DynamicAny.DynSequence dynAny = null; 882 883 try 884 { 885 dynAny = (org.omg.DynamicAny.DynSequence ) 886 factory.create_dyn_any_from_type_code (tc); 887 } 888 catch (Throwable ex) 889 { 890 msg = "Factory failed to create DynAny from TypeCode using "; 891 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 892 fail (msg); 893 } 894 return dynAny; 895 } 896 897 898 901 private static int [] getIntSeq () 902 { 903 int [] type = new int [Bound.value]; 904 for (int i = 0; i < Bound.value; i++) 905 { 906 type [i] = i; 907 } 908 return type; 909 } 910 911 } 912 | Popular Tags |