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.ArrayTypeHelper; 30 31 37 38 public class DynAnyArrayTest extends TestCase 39 { 40 private static org.omg.DynamicAny.DynAnyFactory factory = null; 41 private static org.omg.CORBA.ORB orb = null; 42 43 public DynAnyArrayTest (String name) 44 { 45 super (name); 46 } 47 48 49 public static Test suite () 50 { 51 TestSuite suite = new TestSuite ("DynArray Tests"); 52 Setup setup = new Setup (suite); 53 ORBSetup osetup = new ORBSetup (setup); 54 55 suite.addTest (new DynAnyArrayTest ("testFactoryCreateFromAny")); 56 suite.addTest (new DynAnyArrayTest ("testFactoryCreateFromTypeCode")); 57 suite.addTest (new DynAnyArrayTest ("testFactoryCreateFromIDLTypeCode")); 58 suite.addTest (new DynAnyArrayTest ("testCompareDynAny")); 59 suite.addTest (new DynAnyArrayTest ("testIterateDynAny")); 60 suite.addTest (new DynAnyArrayTest ("testAccessArrayElements")); 61 suite.addTest (new DynAnyArrayTest ("testAccessArrayDynAnyElements")); 62 suite.addTest (new DynAnyArrayTest ("testAccessArrayElementsEx")); 63 suite.addTest (new DynAnyArrayTest ("testDynAnyTypeCode")); 64 suite.addTest (new DynAnyArrayTest ("testInitDynAnyFromDynAny")); 65 suite.addTest (new DynAnyArrayTest ("testInitDynAnyFromAny")); 66 suite.addTest (new DynAnyArrayTest ("testInitFromAnyTypeMismatchEx")); 67 suite.addTest (new DynAnyArrayTest ("testGenerateAnyFromDynAny")); 68 suite.addTest (new DynAnyArrayTest ("testDestroyDynAny")); 69 suite.addTest (new DynAnyArrayTest ("testDestroyComponent")); 70 suite.addTest (new DynAnyArrayTest ("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 = getIntArray (); 86 any = orb.create_any (); 87 ArrayTypeHelper.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_array_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 = ArrayTypeHelper.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.DynArray dynAny = null; 129 org.omg.DynamicAny.DynArray dynAny2 = null; 130 131 type = getIntArray (); 132 any = orb.create_any (); 133 ArrayTypeHelper.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.DynArray dynAny = null; 154 org.omg.DynamicAny.DynAny compSeek = null; 155 org.omg.DynamicAny.DynAny compRewind = null; 156 157 type = getIntArray (); 158 any = orb.create_any (); 159 ArrayTypeHelper.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 testAccessArrayElements () 232 { 233 String msg; 234 int len; 235 int curVal; 236 boolean next; 237 org.omg.CORBA.TypeCode tc = null; 238 org.omg.DynamicAny.DynArray dynAny = null; 239 org.omg.CORBA.Any [] anys = null; 240 241 tc = ArrayTypeHelper.type (); 242 dynAny = createDynAnyFromTypeCode (tc); 243 244 len = Bound.value; 246 anys = new org.omg.CORBA.Any [len]; 247 for (int i = 0; i < len; i++) 248 { 249 anys [i] = orb.create_any (); 250 anys [i].insert_long (i); 251 } 252 253 try 254 { 255 dynAny.set_elements (anys); 256 } 257 catch (Throwable ex) 258 { 259 msg = "Failed to set the elements using the DynArray::set_elements "; 260 msg += "operation"; 261 fail (msg + ": " + ex); 262 } 263 264 anys = dynAny.get_elements (); 266 for (int i = 0; i < anys.length; i++) 267 { 268 msg = "Failed to get the correct value of the DynArray at "; 269 msg += "position " + i + " when array element is returned using "; 270 msg += "DynArray::get_elements operation"; 271 curVal = anys [i].extract_long (); 272 assertEquals (msg, i, curVal); 273 } 274 } 275 276 277 280 public void testAccessArrayDynAnyElements () 281 { 282 String msg; 283 int len; 284 int curVal; 285 boolean next; 286 org.omg.CORBA.TypeCode tc = null; 287 org.omg.DynamicAny.DynArray dynAny = null; 288 org.omg.DynamicAny.DynAny [] dynAnys = null; 289 290 tc = ArrayTypeHelper.type (); 291 dynAny = createDynAnyFromTypeCode (tc); 292 293 len = Bound.value; 295 dynAnys = new org.omg.DynamicAny.DynAny [len]; 296 tc = orb.get_primitive_tc (TCKind.tk_long); 297 for (int i = 0; i < len; i++) 298 { 299 try 300 { 301 dynAnys [i] = factory.create_dyn_any_from_type_code (tc); 302 } 303 catch (Throwable ex) 304 { 305 fail ("Failed to create a DynAny at position " + i + ": " + ex); 306 } 307 308 try 309 { 310 dynAnys [i].insert_long (i); 311 } 312 catch (Throwable ex) 313 { 314 msg = "Failed to insert a value into a DynAny at position " + i; 315 msg += ": " + ex; 316 fail (msg); 317 } 318 } 319 320 try 321 { 322 dynAny.set_elements_as_dyn_any (dynAnys); 323 } 324 catch (Throwable ex) 325 { 326 msg = "Failed to set the elements using the "; 327 msg += "DynArray::set_elements_as_dyn_any operation"; 328 fail (msg + ": " + ex); 329 } 330 331 dynAnys = dynAny.get_elements_as_dyn_any (); 333 for (int i = 0; i < dynAnys.length; i++) 334 { 335 msg = "Failed to get the correct value of the DynArray at "; 336 msg += "position " + i + " when array element is returned using "; 337 msg += "DynArray::get_elements_as_dyn_any operation"; 338 curVal = -1; 339 try 340 { 341 curVal = dynAnys [i].get_long (); 342 } 343 catch (Throwable ex) 344 { 345 fail (msg + ": " + ex); 346 } 347 assertEquals (msg, i, curVal); 348 } 349 } 350 351 352 356 public void testAccessArrayElementsEx () 357 { 358 String msg; 359 int len; 360 org.omg.CORBA.TypeCode tc = null; 361 org.omg.DynamicAny.DynArray dynAny = null; 362 org.omg.CORBA.Any [] anys = null; 363 364 tc = ArrayTypeHelper.type (); 365 dynAny = createDynAnyFromTypeCode (tc); 366 367 len = Bound.value; 369 anys = new org.omg.CORBA.Any [len]; 370 for (int i = 0; i < len; i++) 371 { 372 anys [i] = orb.create_any (); 373 anys [i].insert_string ("BadType"); 374 } 375 376 msg = "Failed to raise a TypeMismatch exception when setting a "; 377 msg += "DynArray object with components of the wrong type using "; 378 msg += "DynArray::set_elements"; 379 try 380 { 381 dynAny.set_elements (anys); 382 383 fail (msg); 384 } 385 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 386 { 387 } 389 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 390 { 391 fail (msg + ": " + ex); 392 } 393 394 len = Bound.value + 1; 396 anys = new org.omg.CORBA.Any [len]; 397 for (int i = 0; i < len; i++) 398 { 399 anys [i] = orb.create_any (); 400 anys [i].insert_long (i); 401 } 402 403 msg = "Failed to raise an InvalidValue exception when setting a "; 404 msg += "DynArray object with an incorrect number of elements using "; 405 msg += "DynArray::set_elements"; 406 try 407 { 408 dynAny.set_elements (anys); 409 } 410 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 411 { 412 } 414 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 415 { 416 fail (msg + ": " + ex); 417 } 418 } 419 420 421 424 public void testDynAnyTypeCode () 425 { 426 String msg; 427 org.omg.CORBA.TypeCode tc = null; 428 org.omg.DynamicAny.DynArray dynAny = null; 429 430 tc = orb.get_primitive_tc (TCKind.tk_long); 431 tc = orb.create_array_tc (Bound.value, tc); 432 dynAny = createDynAnyFromTypeCode (tc); 433 434 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 435 assertTrue (msg, dynAny.type ().equal (tc)); 436 } 437 438 439 442 public void testInitDynAnyFromDynAny () 443 { 444 String msg; 445 int [] type; 446 org.omg.CORBA.Any any = null; 447 org.omg.CORBA.TypeCode tc = null; 448 org.omg.DynamicAny.DynArray dynAny = null; 449 org.omg.DynamicAny.DynArray dynAny2 = null; 450 451 tc = ArrayTypeHelper.type (); 452 dynAny = createDynAnyFromTypeCode (tc); 453 454 type = getIntArray (); 455 any = orb.create_any (); 456 ArrayTypeHelper.insert (any, type); 457 dynAny2 = createDynAnyFromAny (any); 458 459 msg = "Failed to initialize a DynAny object from another DynAny "; 460 msg += "object using the DynAny::assign operation"; 461 try 462 { 463 dynAny.assign (dynAny2); 464 } 465 catch (Throwable ex) 466 { 467 fail (msg + ": " + ex); 468 } 469 assertTrue (msg, dynAny.equal (dynAny2)); 470 } 471 472 473 476 public void testInitDynAnyFromAny () 477 { 478 String msg; 479 int [] type; 480 org.omg.CORBA.Any any = null; 481 org.omg.CORBA.TypeCode tc = null; 482 org.omg.DynamicAny.DynArray dynAny = null; 483 org.omg.DynamicAny.DynArray dynAny2 = null; 484 485 tc = ArrayTypeHelper.type (); 486 dynAny = createDynAnyFromTypeCode (tc); 487 488 type = getIntArray (); 489 any = orb.create_any (); 490 ArrayTypeHelper.insert (any, type); 491 dynAny2 = createDynAnyFromAny (any); 492 493 msg = "Failed to initialize a DynAny object from an Any object "; 494 msg += "using the DynAny::from_any operation"; 495 try 496 { 497 dynAny.from_any (any); 498 } 499 catch (Throwable ex) 500 { 501 fail (msg + ": " + ex); 502 } 503 assertTrue (msg, dynAny.equal (dynAny2)); 504 } 505 506 507 511 public void testInitFromAnyTypeMismatchEx () 512 { 513 String msg; 514 org.omg.CORBA.Any any = null; 515 org.omg.CORBA.TypeCode tc = null; 516 org.omg.DynamicAny.DynArray dynAny = null; 517 518 any = orb.create_any (); 519 any.insert_string ("Hello"); 520 521 tc = orb.get_primitive_tc (TCKind.tk_long); 522 tc = orb.create_array_tc (Bound.value, tc); 523 dynAny = createDynAnyFromTypeCode (tc); 524 525 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 526 msg += "operation when DynAny and Any operands have different types"; 527 try 528 { 529 dynAny.from_any (any); 530 531 fail (msg); 532 } 533 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 534 { 535 } 537 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 538 { 539 fail (msg + ": " + ex); 540 } 541 } 542 543 544 547 public void testGenerateAnyFromDynAny () 548 { 549 String msg; 550 org.omg.CORBA.Any any = null; 551 org.omg.CORBA.TypeCode tc = null; 552 org.omg.DynamicAny.DynArray dynAny = null; 553 org.omg.DynamicAny.DynArray dynAny2 = null; 554 555 tc = ArrayTypeHelper.type (); 556 dynAny = createDynAnyFromTypeCode (tc); 557 558 any = orb.create_any (); 559 any = dynAny.to_any (); 560 dynAny2 = createDynAnyFromAny (any); 561 562 msg = "The DynAny::to_any operation failed to create an Any "; 563 msg += "object with the same value as the DynAny object"; 564 assertTrue (msg, dynAny.equal (dynAny2)); 565 } 566 567 568 571 public void testDestroyDynAny () 572 { 573 String msg; 574 int [] type; 575 org.omg.CORBA.Any any = null; 576 org.omg.DynamicAny.DynArray dynAny = null; 577 578 type = getIntArray (); 579 any = orb.create_any (); 580 ArrayTypeHelper.insert (any, type); 581 dynAny = createDynAnyFromAny (any); 582 dynAny.destroy (); 583 584 try 585 { 586 dynAny.type (); 587 588 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 589 msg += "calling DynAny::type operation on a destroyed DynAny object "; 590 msg += "did not raise OBJECT_NOT_EXIST exception"; 591 fail (msg); 592 } 593 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 594 { 595 } 597 598 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 599 msg += "calling DynAny::current_component operation on a destroyed "; 600 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 601 try 602 { 603 dynAny.current_component (); 604 605 fail (msg); 606 } 607 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 608 { 609 } 611 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 612 { 613 fail (msg + ": " + ex); 614 } 615 } 616 617 618 621 public void testDestroyComponent () 622 { 623 String msg; 624 int [] type; 625 org.omg.CORBA.Any any = null; 626 org.omg.DynamicAny.DynArray dynAny = null; 627 org.omg.DynamicAny.DynAny comp = null; 628 629 type = getIntArray (); 630 any = orb.create_any (); 631 ArrayTypeHelper.insert (any, type); 632 dynAny = createDynAnyFromAny (any); 633 634 try 635 { 636 comp = dynAny.current_component (); 637 } 638 catch (Throwable ex) 639 { 640 msg = "Failed to get the current component of the DynAny using the "; 641 msg += "DynAny::current_component operation before calling the "; 642 msg += "DynAny::destroy operation"; 643 fail (msg + ": " + ex); 644 } 645 646 comp.destroy (); 647 try 648 { 649 comp = dynAny.current_component (); 650 } 651 catch (Throwable ex) 652 { 653 msg = "Failed to get the current component of the DynAny using the "; 654 msg += "DynAny::current_component operation after calling the "; 655 msg += "DynAny::destroy operation"; 656 fail (msg + ": " + ex); 657 } 658 659 try 660 { 661 comp.type (); 662 } 663 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 664 { 665 msg = "Calling destroy on a component resulted in destroying the "; 666 msg += "component object"; 667 fail (msg + ": " + ex); 668 } 669 } 670 671 672 675 public void testCopyDynAny () 676 { 677 String msg; 678 org.omg.CORBA.TypeCode tc = null; 679 org.omg.DynamicAny.DynArray dynAny = null; 680 org.omg.DynamicAny.DynArray dynAny2 = null; 681 682 tc = ArrayTypeHelper.type (); 683 dynAny = createDynAnyFromTypeCode (tc); 684 dynAny2 = (org.omg.DynamicAny.DynArray ) dynAny.copy (); 685 686 msg = "The DynAny object created with the DynAny::copy operation "; 687 msg += "is not equal to the DynAny object it was copied from"; 688 assertTrue (msg, dynAny.equal (dynAny2)); 689 } 690 691 692 private static class Setup extends TestSetup 693 { 694 public Setup (Test test) 695 { 696 super (test); 697 } 698 699 protected void setUp () 700 { 701 org.omg.CORBA.Object obj = null; 702 703 orb = ORBSetup.getORB (); 704 try 705 { 706 obj = orb.resolve_initial_references ("DynAnyFactory"); 707 } 708 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 709 { 710 fail ("Failed to resolve DynAnyFactory: " + ex); 711 } 712 try 713 { 714 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 715 } 716 catch (Throwable ex) 717 { 718 fail ("Failed to narrow to DynAnyFactory: " + ex); 719 } 720 } 721 722 protected void tearDown () 723 { 724 } 725 } 726 727 728 731 private static org.omg.DynamicAny.DynArray createDynAnyFromAny 732 (org.omg.CORBA.Any any) 733 { 734 String msg; 735 org.omg.DynamicAny.DynArray dynAny = null; 736 737 try 738 { 739 dynAny = (org.omg.DynamicAny.DynArray ) factory.create_dyn_any (any); 740 } 741 catch (Throwable ex) 742 { 743 msg = "Factory failed to create DynAny from Any using "; 744 msg += "DynAny::create_dyn_any operation: " + ex; 745 fail (msg); 746 } 747 return dynAny; 748 } 749 750 751 754 private static org.omg.DynamicAny.DynArray createDynAnyFromTypeCode 755 (org.omg.CORBA.TypeCode tc) 756 { 757 String msg; 758 org.omg.DynamicAny.DynArray dynAny = null; 759 760 try 761 { 762 dynAny = (org.omg.DynamicAny.DynArray ) 763 factory.create_dyn_any_from_type_code (tc); 764 } 765 catch (Throwable ex) 766 { 767 msg = "Factory failed to create DynAny from TypeCode using "; 768 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 769 fail (msg); 770 } 771 return dynAny; 772 } 773 774 775 778 private static int [] getIntArray () 779 { 780 int [] type = new int [Bound.value]; 781 for (int i = 0; i < Bound.value; i++) 782 { 783 type [i] = i; 784 } 785 return type; 786 } 787 788 } 789 | Popular Tags |