1 package org.jacorb.test.orb.dynany; 2 3 22 23 import junit.framework.*; 24 import junit.extensions.TestSetup; 25 26 import org.omg.CORBA.TCKind ; 27 import org.omg.CORBA.StructMember ; 28 import org.omg.DynamicAny.NameValuePair ; 29 import org.omg.DynamicAny.NameDynAnyPair ; 30 31 import org.jacorb.test.common.ORBSetup; 32 import org.jacorb.test.EmptyException; 33 import org.jacorb.test.EmptyExceptionHelper; 34 35 41 42 public class DynAnyEmptyExTest 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 = "MyEmptyException"; 49 private static final StructMember [] MEMBERS = new StructMember [0]; 50 51 public DynAnyEmptyExTest (String name) 52 { 53 super (name); 54 } 55 56 57 public static Test suite () 58 { 59 TestSuite suite = new TestSuite ("Empty Exception DynStruct Tests"); 60 Setup setup = new Setup (suite); 61 ORBSetup osetup = new ORBSetup (setup); 62 63 suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromAny")); 64 suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromTypeCode")); 65 suite.addTest (new DynAnyEmptyExTest ("testFactoryCreateFromIDLTypeCode")); 66 suite.addTest (new DynAnyEmptyExTest ("testCompareDynAny")); 67 suite.addTest (new DynAnyEmptyExTest ("testIterateDynAny")); 68 suite.addTest (new DynAnyEmptyExTest ("testAccessStructMembers")); 69 suite.addTest (new DynAnyEmptyExTest ("testAccessStructNameValuePairs")); 70 suite.addTest (new DynAnyEmptyExTest ("testAccessStructDynAnyPairs")); 71 suite.addTest (new DynAnyEmptyExTest ("testAccessStructPairsEx")); 72 suite.addTest (new DynAnyEmptyExTest ("testDynAnyTypeCode")); 73 suite.addTest (new DynAnyEmptyExTest ("testInitDynAnyFromDynAny")); 74 suite.addTest (new DynAnyEmptyExTest ("testInitDynAnyFromAny")); 75 suite.addTest (new DynAnyEmptyExTest ("testInitFromAnyTypeMismatchEx")); 76 suite.addTest (new DynAnyEmptyExTest ("testGenerateAnyFromDynAny")); 77 suite.addTest (new DynAnyEmptyExTest ("testDestroyDynAny")); 78 suite.addTest (new DynAnyEmptyExTest ("testCopyDynAny")); 79 80 return osetup; 81 } 82 83 84 88 public void testFactoryCreateFromAny () 89 { 90 EmptyException type = null; 91 org.omg.CORBA.Any any = null; 92 93 type = new EmptyException (); 94 any = orb.create_any (); 95 EmptyExceptionHelper.insert (any, type); 96 97 createDynAnyFromAny (any); 98 } 99 100 101 105 public void testFactoryCreateFromTypeCode () 106 { 107 org.omg.CORBA.TypeCode tc = null; 108 109 tc = orb.create_exception_tc (ID, NAME, MEMBERS); 110 createDynAnyFromTypeCode (tc); 111 } 112 113 114 118 public void testFactoryCreateFromIDLTypeCode () 119 { 120 org.omg.CORBA.TypeCode tc = null; 121 122 tc = EmptyExceptionHelper.type (); 123 createDynAnyFromTypeCode (tc); 124 } 125 126 127 130 public void testCompareDynAny () 131 { 132 String msg; 133 EmptyException type; 134 org.omg.CORBA.Any any = null; 135 org.omg.DynamicAny.DynStruct dynAny = null; 136 org.omg.DynamicAny.DynStruct dynAny2 = null; 137 138 type = new EmptyException (); 139 any = orb.create_any (); 140 EmptyExceptionHelper.insert (any, type); 141 dynAny = createDynAnyFromAny (any); 142 dynAny2 = createDynAnyFromAny (any); 143 144 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 145 assertTrue (msg, dynAny.equal (dynAny2)); 146 } 147 148 149 152 public void testIterateDynAny () 153 { 154 String msg; 155 int compCount = -1; 156 boolean seek; 157 org.omg.CORBA.TypeCode tc = null; 158 org.omg.DynamicAny.DynStruct dynAny = null; 159 160 tc = EmptyExceptionHelper.type (); 161 dynAny = createDynAnyFromTypeCode (tc); 162 163 try 165 { 166 compCount = dynAny.component_count (); 167 } 168 catch (Throwable ex) 169 { 170 fail ("Unexpected error raised by DynAny::component_count operation"); 172 } 173 msg = "The number of components returned from the "; 174 msg += "DynAny::component_count operation is incorrect"; 175 assertEquals (msg, 0, compCount); 176 177 msg = "The DynAny::seek operation indicates that a valid component "; 179 msg += "exists but the DynAny should have no components"; 180 seek = dynAny.seek (0); 181 assertTrue (msg, !seek); 182 183 try 185 { 186 dynAny.current_component (); 187 188 msg = "A TypeMismatch exception was not raised by the "; 189 msg += "DynAny::current_component operation when trying to access "; 190 msg += "the current component of a DynAny with no components"; 191 fail (msg); 192 } 193 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 194 { 195 } 197 } 198 199 200 203 public void testAccessStructMembers () 204 { 205 String msg; 206 String memberName = null; 207 TCKind memberKind = null; 208 org.omg.CORBA.Any any = null; 209 EmptyException exception = null; 210 org.omg.DynamicAny.DynStruct dynAny = null; 211 212 exception = new EmptyException (); 213 any = orb.create_any (); 214 EmptyExceptionHelper.insert (any, exception); 215 dynAny = createDynAnyFromAny (any); 216 217 msg = "Failed to throw a TypeMismatch exception when calling "; 219 msg += "DynStruct::current_member_name operation on an empty exception"; 220 try 221 { 222 memberName = dynAny.current_member_name (); 223 224 fail (msg); 225 } 226 catch (AssertionFailedError ex) 227 { 228 throw ex; 229 } 230 catch (Throwable ex) 231 { 232 if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.TypeMismatch )) 233 { 234 fail (msg + ": " + ex); 235 } 236 } 237 238 msg = "Failed to throw a TypeMismatch exception when calling "; 240 msg += "DynStruct::current_member_kind operation on an empty exception"; 241 try 242 { 243 memberKind = dynAny.current_member_kind (); 244 245 fail (msg); 246 } 247 catch (AssertionFailedError ex) 248 { 249 throw ex; 250 } 251 catch (Throwable ex) 252 { 253 if (!(ex instanceof org.omg.DynamicAny.DynAnyPackage.TypeMismatch )) 254 { 255 fail (msg + ": " + ex); 256 } 257 } 258 } 259 260 261 264 public void testAccessStructNameValuePairs () 265 { 266 String msg; 267 org.omg.CORBA.Any any = null; 268 EmptyException type = null; 269 org.omg.DynamicAny.DynStruct dynAny = null; 270 org.omg.DynamicAny.NameValuePair [] pairs = null; 271 272 type = new EmptyException (); 273 any = orb.create_any (); 274 EmptyExceptionHelper.insert (any, type); 275 dynAny = createDynAnyFromAny (any); 276 277 pairs = dynAny.get_members (); 279 msg = "Returned number of members from DynStruct::get_members operation "; 280 msg += "is non-zero"; 281 assertEquals (msg, 0, pairs.length); 282 283 pairs = new NameValuePair [0]; 284 try 285 { 286 dynAny.set_members (pairs); 287 } 288 catch (Throwable ex) 289 { 290 msg = "Failed to set DynStruct members using DynStruct::set_members "; 291 msg += "operation"; 292 fail (msg + ": " + ex); 293 } 294 } 295 296 297 300 public void testAccessStructDynAnyPairs () 301 { 302 String msg; 303 org.omg.CORBA.Any any = null; 304 EmptyException type = null; 305 org.omg.DynamicAny.DynStruct dynAny = null; 306 org.omg.DynamicAny.NameDynAnyPair [] pairs = null; 307 308 type = new EmptyException (); 309 any = orb.create_any (); 310 EmptyExceptionHelper.insert (any, type); 311 dynAny = createDynAnyFromAny (any); 312 313 pairs = dynAny.get_members_as_dyn_any (); 315 msg = "Returned number of members from DynStruct::get_members operation "; 316 msg += "is non-zero"; 317 assertEquals (msg, 0, pairs.length); 318 319 pairs = new NameDynAnyPair [0]; 320 try 321 { 322 dynAny.set_members_as_dyn_any (pairs); 323 } 324 catch (Throwable ex) 325 { 326 msg = "Failed to set DynStruct members using DynStruct::set_members "; 327 msg += "operation"; 328 fail (msg + ": " + ex); 329 } 330 } 331 332 333 337 public void testAccessStructPairsEx () 338 { 339 String msg; 340 org.omg.CORBA.Any any = null; 341 org.omg.CORBA.TypeCode tc = null; 342 org.omg.DynamicAny.DynStruct dynAny = null; 343 org.omg.DynamicAny.NameValuePair [] pairs = null; 344 345 tc = orb.create_exception_tc (ID, NAME, MEMBERS); 346 dynAny = createDynAnyFromTypeCode (tc); 347 348 pairs = new NameValuePair [1]; 350 any = orb.create_any (); 351 any.insert_char ('a'); 352 pairs [0] = new NameValuePair ("name", any); 353 354 msg = "Failed to raise an InvalidValue exception when passing in a "; 355 msg += "name/value sequence containing too many members"; 356 try 357 { 358 dynAny.set_members (pairs); 359 360 fail (msg); 361 } 362 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 363 { 364 } 366 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 367 { 368 fail (msg + ": " + ex); 369 } 370 } 371 372 373 376 public void testDynAnyTypeCode () 377 { 378 String msg; 379 org.omg.CORBA.TypeCode tc = null; 380 org.omg.DynamicAny.DynStruct dynAny = null; 381 382 tc = orb.create_exception_tc (ID, NAME, MEMBERS); 383 dynAny = createDynAnyFromTypeCode (tc); 384 385 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 386 assertTrue (msg, dynAny.type ().equal (tc)); 387 } 388 389 390 393 public void testInitDynAnyFromDynAny () 394 { 395 String msg; 396 EmptyException type; 397 org.omg.CORBA.Any any = null; 398 org.omg.CORBA.TypeCode tc = null; 399 org.omg.DynamicAny.DynStruct dynAny = null; 400 org.omg.DynamicAny.DynStruct dynAny2 = null; 401 402 tc = EmptyExceptionHelper.type (); 403 dynAny = createDynAnyFromTypeCode (tc); 404 405 type = new EmptyException (); 406 any = orb.create_any (); 407 EmptyExceptionHelper.insert (any, type); 408 dynAny2 = createDynAnyFromAny (any); 409 410 msg = "Failed to initialize a DynAny object from another DynAny "; 411 msg += "object using the DynAny::assign operation"; 412 try 413 { 414 dynAny.assign (dynAny2); 415 } 416 catch (Throwable ex) 417 { 418 fail (msg + ": " + ex); 419 } 420 assertTrue (msg, dynAny.equal (dynAny2)); 421 } 422 423 424 427 public void testInitDynAnyFromAny () 428 { 429 String msg; 430 EmptyException type; 431 org.omg.CORBA.Any any = null; 432 org.omg.CORBA.TypeCode tc = null; 433 org.omg.DynamicAny.DynStruct dynAny = null; 434 org.omg.DynamicAny.DynStruct dynAny2 = null; 435 436 tc = EmptyExceptionHelper.type (); 437 dynAny = createDynAnyFromTypeCode (tc); 438 439 type = new EmptyException (); 440 any = orb.create_any (); 441 EmptyExceptionHelper.insert (any, type); 442 dynAny2 = createDynAnyFromAny (any); 443 444 msg = "Failed to initialize a DynAny object from an Any object "; 445 msg += "using the DynAny::from_any operation"; 446 try 447 { 448 dynAny.from_any (any); 449 } 450 catch (Throwable ex) 451 { 452 fail (msg + ": " + ex); 453 } 454 assertTrue (msg, dynAny.equal (dynAny2)); 455 } 456 457 458 462 public void testInitFromAnyTypeMismatchEx () 463 { 464 String msg; 465 org.omg.CORBA.Any any = null; 466 org.omg.CORBA.TypeCode tc = null; 467 org.omg.DynamicAny.DynStruct dynAny = null; 468 469 any = orb.create_any (); 470 any.insert_string ("Hello"); 471 472 tc = orb.create_exception_tc (ID, NAME, MEMBERS); 473 dynAny = createDynAnyFromTypeCode (tc); 474 475 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 476 msg += "operation when DynAny and Any operands have different types"; 477 try 478 { 479 dynAny.from_any (any); 480 481 fail (msg); 482 } 483 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 484 { 485 } 487 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 488 { 489 fail (msg + ": " + ex); 490 } 491 } 492 493 494 497 public void testGenerateAnyFromDynAny () 498 { 499 String msg; 500 org.omg.CORBA.Any any = null; 501 org.omg.CORBA.TypeCode tc = null; 502 org.omg.DynamicAny.DynStruct dynAny = null; 503 org.omg.DynamicAny.DynStruct dynAny2 = null; 504 505 tc = EmptyExceptionHelper.type (); 506 dynAny = createDynAnyFromTypeCode (tc); 507 508 any = orb.create_any (); 509 any = dynAny.to_any (); 510 dynAny2 = createDynAnyFromAny (any); 511 512 msg = "The DynAny::to_any operation failed to create an Any "; 513 msg += "object with the same value as the DynAny object"; 514 assertTrue (msg, dynAny.equal (dynAny2)); 515 } 516 517 518 521 public void testDestroyDynAny () 522 { 523 String msg; 524 EmptyException type; 525 org.omg.CORBA.Any any = null; 526 org.omg.DynamicAny.DynStruct dynAny = null; 527 528 type = new EmptyException (); 529 any = orb.create_any (); 530 EmptyExceptionHelper.insert (any, type); 531 dynAny = createDynAnyFromAny (any); 532 dynAny.destroy (); 533 534 try 535 { 536 dynAny.type (); 537 538 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 539 msg += "calling DynAny::type operation on a destroyed DynAny object "; 540 msg += "did not raise OBJECT_NOT_EXIST exception"; 541 fail (msg); 542 } 543 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 544 { 545 } 547 548 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 549 msg += "calling DynAny::current_component operation on a destroyed "; 550 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 551 try 552 { 553 dynAny.current_component (); 554 555 fail (msg); 556 } 557 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 558 { 559 } 561 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 562 { 563 fail (msg + ": " + ex); 564 } 565 } 566 567 568 571 public void testCopyDynAny () 572 { 573 String msg; 574 org.omg.CORBA.TypeCode tc = null; 575 org.omg.DynamicAny.DynStruct dynAny = null; 576 org.omg.DynamicAny.DynStruct dynAny2 = null; 577 578 tc = EmptyExceptionHelper.type (); 579 dynAny = createDynAnyFromTypeCode (tc); 580 dynAny2 = (org.omg.DynamicAny.DynStruct ) dynAny.copy (); 581 582 msg = "The DynAny object created with the DynAny::copy operation "; 583 msg += "is not equal to the DynAny object it was copied from"; 584 assertTrue (msg, dynAny.equal (dynAny2)); 585 } 586 587 588 private static class Setup extends TestSetup 589 { 590 public Setup (Test test) 591 { 592 super (test); 593 } 594 595 protected void setUp () 596 { 597 org.omg.CORBA.Object obj = null; 598 599 orb = ORBSetup.getORB (); 600 try 601 { 602 obj = orb.resolve_initial_references ("DynAnyFactory"); 603 } 604 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 605 { 606 fail ("Failed to resolve DynAnyFactory: " + ex); 607 } 608 try 609 { 610 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 611 } 612 catch (Throwable ex) 613 { 614 fail ("Failed to narrow to DynAnyFactory: " + ex); 615 } 616 } 617 618 protected void tearDown () 619 { 620 } 621 } 622 623 624 627 private static org.omg.DynamicAny.DynStruct createDynAnyFromAny 628 (org.omg.CORBA.Any any) 629 { 630 String msg; 631 org.omg.DynamicAny.DynStruct dynAny = null; 632 633 try 634 { 635 dynAny = (org.omg.DynamicAny.DynStruct ) factory.create_dyn_any (any); 636 } 637 catch (Throwable ex) 638 { 639 msg = "Factory failed to create DynAny from Any using "; 640 msg += "DynAny::create_dyn_any operation: " + ex; 641 fail (msg); 642 } 643 return dynAny; 644 } 645 646 647 650 private static org.omg.DynamicAny.DynStruct createDynAnyFromTypeCode 651 (org.omg.CORBA.TypeCode tc) 652 { 653 String msg; 654 org.omg.DynamicAny.DynStruct dynAny = null; 655 656 try 657 { 658 dynAny = (org.omg.DynamicAny.DynStruct ) 659 factory.create_dyn_any_from_type_code (tc); 660 } 661 catch (Throwable ex) 662 { 663 msg = "Factory failed to create DynAny from TypeCode using "; 664 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 665 fail (msg); 666 } 667 return dynAny; 668 } 669 670 } 671 | Popular Tags |