1 package org.jacorb.test.orb.dynany; 2 3 22 23 import junit.framework.*; 24 import junit.extensions.TestSetup; 25 import org.jacorb.test.common.ORBSetup; 26 import org.jacorb.test.EnumType; 27 import org.jacorb.test.EnumTypeHelper; 28 29 35 36 public class DynAnyEnumTest extends TestCase 37 { 38 private static org.omg.DynamicAny.DynAnyFactory factory = null; 39 private static org.omg.CORBA.ORB orb = null; 40 41 private static final String ID = "IDL:test:1.0"; 42 private static final String NAME = "MyEnum"; 43 private static final String [] ENUM = {"one", "two", "three"}; 44 45 46 public DynAnyEnumTest (String name) 47 { 48 super (name); 49 } 50 51 52 public static Test suite () 53 { 54 TestSuite suite = new TestSuite ("DynEnum Tests"); 55 Setup setup = new Setup (suite); 56 ORBSetup osetup = new ORBSetup (setup); 57 58 suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromAny")); 59 suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromTypeCode")); 60 suite.addTest (new DynAnyEnumTest ("testFactoryCreateFromIDLTypeCode")); 61 suite.addTest (new DynAnyEnumTest ("testCompareDynAny")); 62 suite.addTest (new DynAnyEnumTest ("testAccessEnumValue")); 63 suite.addTest (new DynAnyEnumTest ("testAccessInvalidValueEx")); 64 suite.addTest (new DynAnyEnumTest ("testDynAnyTypeCode")); 65 suite.addTest (new DynAnyEnumTest ("testInitDynAnyFromDynAny")); 66 suite.addTest (new DynAnyEnumTest ("testInitDynAnyFromAny")); 67 suite.addTest (new DynAnyEnumTest ("testInitFromAnyTypeMismatchEx")); 68 suite.addTest (new DynAnyEnumTest ("testGenerateAnyFromDynAny")); 69 suite.addTest (new DynAnyEnumTest ("testDestroyDynAny")); 70 suite.addTest (new DynAnyEnumTest ("testCopyDynAny")); 71 suite.addTest (new DynAnyEnumTest ("testIterateDynAny")); 72 73 return osetup; 74 } 75 76 77 81 public void testFactoryCreateFromAny () 82 { 83 org.omg.CORBA.Any any = null; 84 85 any = orb.create_any (); 86 EnumTypeHelper.insert (any, EnumType.from_int (0)); 87 88 createDynAnyFromAny (any); 89 } 90 91 92 96 public void testFactoryCreateFromTypeCode () 97 { 98 org.omg.CORBA.TypeCode tc = null; 99 100 tc = orb.create_enum_tc (ID, NAME, ENUM); 101 createDynAnyFromTypeCode (tc); 102 } 103 104 105 109 public void testFactoryCreateFromIDLTypeCode () 110 { 111 org.omg.CORBA.TypeCode tc = null; 112 113 tc = EnumTypeHelper.type (); 114 createDynAnyFromTypeCode (tc); 115 } 116 117 118 121 public void testCompareDynAny () 122 { 123 String msg; 124 org.omg.CORBA.Any any = null; 125 org.omg.DynamicAny.DynEnum dynAny = null; 126 org.omg.DynamicAny.DynEnum dynAny2 = null; 127 128 any = orb.create_any (); 129 EnumTypeHelper.insert (any, EnumType.from_int (0)); 130 dynAny = createDynAnyFromAny (any); 131 dynAny2 = createDynAnyFromAny (any); 132 133 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 134 assertTrue (msg, dynAny.equal (dynAny2)); 135 } 136 137 138 141 public void testAccessEnumValue () 142 { 143 String msg; 144 String type1; 145 int type2; 146 org.omg.CORBA.Any any = null; 147 org.omg.CORBA.TypeCode tc = null; 148 org.omg.DynamicAny.DynEnum dynAny = null; 149 org.omg.DynamicAny.DynEnum dynAny2 = null; 150 151 tc = EnumTypeHelper.type (); 152 dynAny = createDynAnyFromTypeCode (tc); 153 154 any = orb.create_any (); 155 EnumTypeHelper.insert (any, EnumType.from_int (0)); 156 dynAny2 = createDynAnyFromAny (any); 157 158 type1 = "first"; try 160 { 161 dynAny.set_as_string (type1); 162 } 163 catch (Throwable ex) 164 { 165 msg = "Failed to set value as string in DynEnum object with "; 166 msg += "DynEnum::set_as_string"; 167 fail (msg + ": " + ex); 168 } 169 170 type2 = 1; 171 try 172 { 173 dynAny2.set_as_ulong (type2); 174 } 175 catch (Throwable ex) 176 { 177 msg = "Failed to set value as int in DynEnum object with "; 178 msg += "DynEnum::set_as_ulong"; 179 fail (msg + ": " + ex); 180 } 181 182 msg = "String value inserted into DynEnum object is not equal to value "; 183 msg += "extracted from same DynEnum object with DynEnum::get_as_string"; 184 assertEquals (msg, type1, dynAny.get_as_string ()); 185 186 msg = "Integer value inserted into DynEnum object is not equal to value "; 187 msg += "extracted from same DynEnum object with DynEnum::get_as_ulong"; 188 assertEquals (msg, type2, dynAny2.get_as_ulong ()); 189 } 190 191 192 196 public void testAccessInvalidValueEx () 197 { 198 String msg; 199 org.omg.CORBA.TypeCode tc = null; 200 org.omg.DynamicAny.DynEnum dynAny = null; 201 202 tc = orb.create_enum_tc (ID, NAME, ENUM); 203 dynAny = createDynAnyFromTypeCode (tc); 204 205 try 206 { 207 dynAny.set_as_string ("BadValue"); 208 209 msg = "InvalidValue exception not thrown by DynEnum::set_as_string "; 210 msg += "operation when value is out of range"; 211 fail (msg); 212 } 213 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 214 { 215 } 217 218 try 219 { 220 dynAny.set_as_ulong (-1); 221 222 msg = "InvalidValue exception not thrown by DynEnum::set_as_ulong "; 223 msg += "operation when value is out of range"; 224 fail (msg); 225 } 226 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 227 { 228 } 230 } 231 232 233 236 public void testDynAnyTypeCode () 237 { 238 String msg; 239 org.omg.CORBA.TypeCode tc = null; 240 org.omg.DynamicAny.DynEnum dynAny = null; 241 242 tc = orb.create_enum_tc (ID, NAME, ENUM); 243 dynAny = createDynAnyFromTypeCode (tc); 244 245 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 246 assertTrue (msg, dynAny.type ().equal (tc)); 247 } 248 249 250 253 public void testInitDynAnyFromDynAny () 254 { 255 String msg; 256 org.omg.CORBA.Any any = null; 257 org.omg.CORBA.TypeCode tc = null; 258 org.omg.DynamicAny.DynEnum dynAny = null; 259 org.omg.DynamicAny.DynEnum dynAny2 = null; 260 261 tc = EnumTypeHelper.type (); 262 dynAny = createDynAnyFromTypeCode (tc); 263 264 any = orb.create_any (); 265 EnumTypeHelper.insert (any, EnumType.from_int (1)); 266 dynAny2 = createDynAnyFromAny (any); 267 268 msg = "Failed to initialize a DynAny object from another DynAny "; 269 msg += "object using the DynAny::assign operation"; 270 try 271 { 272 dynAny.assign (dynAny2); 273 } 274 catch (Throwable ex) 275 { 276 fail (msg + ": " + ex); 277 } 278 assertTrue (msg, dynAny.equal (dynAny2)); 279 } 280 281 282 285 public void testInitDynAnyFromAny () 286 { 287 String msg; 288 org.omg.CORBA.Any any = null; 289 org.omg.CORBA.TypeCode tc = null; 290 org.omg.DynamicAny.DynEnum dynAny = null; 291 org.omg.DynamicAny.DynEnum dynAny2 = null; 292 293 tc = EnumTypeHelper.type (); 294 dynAny = createDynAnyFromTypeCode (tc); 295 296 any = orb.create_any (); 297 EnumTypeHelper.insert (any, EnumType.from_int (1)); 298 dynAny2 = createDynAnyFromAny (any); 299 300 msg = "Failed to initialize a DynAny object from an Any object "; 301 msg += "using the DynAny::from_any operation"; 302 try 303 { 304 dynAny.from_any (any); 305 } 306 catch (Throwable ex) 307 { 308 fail (msg + ": " + ex); 309 } 310 assertTrue (msg, dynAny.equal (dynAny2)); 311 } 312 313 314 318 public void testInitFromAnyTypeMismatchEx () 319 { 320 String msg; 321 org.omg.CORBA.Any any = null; 322 org.omg.CORBA.TypeCode tc = null; 323 org.omg.DynamicAny.DynEnum dynAny = null; 324 325 any = orb.create_any (); 326 any.insert_string ("Hello"); 327 328 tc = orb.create_enum_tc (ID, NAME, ENUM); 329 dynAny = createDynAnyFromTypeCode (tc); 330 331 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 332 msg += "operation when DynAny and Any operands have different types"; 333 try 334 { 335 dynAny.from_any (any); 336 337 fail (msg); 338 } 339 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 340 { 341 } 343 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 344 { 345 fail (msg + ": " + ex); 346 } 347 } 348 349 350 353 public void testGenerateAnyFromDynAny () 354 { 355 String msg; 356 org.omg.CORBA.Any any = null; 357 org.omg.CORBA.TypeCode tc = null; 358 org.omg.DynamicAny.DynEnum dynAny = null; 359 org.omg.DynamicAny.DynEnum dynAny2 = null; 360 361 tc = EnumTypeHelper.type (); 362 dynAny = createDynAnyFromTypeCode (tc); 363 364 any = orb.create_any (); 365 any = dynAny.to_any (); 366 dynAny2 = createDynAnyFromAny (any); 367 368 msg = "Failed to initialize an Any object from a DynAny object "; 369 msg += "using the DynAny::to_any operation"; 370 assertTrue (msg, dynAny.equal (dynAny2)); 371 } 372 373 374 377 public void testDestroyDynAny () 378 { 379 String msg; 380 org.omg.CORBA.Any any = null; 381 org.omg.DynamicAny.DynEnum dynAny = null; 382 383 any = orb.create_any (); 384 EnumTypeHelper.insert (any, EnumType.from_int (1)); 385 dynAny = createDynAnyFromAny (any); 386 dynAny.destroy (); 387 388 try 389 { 390 dynAny.type (); 391 392 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 393 msg += "calling DynAny::type operation on a destroyed DynAny object "; 394 msg += "did not raise OBJECT_NOT_EXIST exception"; 395 fail (msg); 396 } 397 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 398 { 399 } 401 402 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 403 msg += "calling DynAny::current_component operation on a destroyed "; 404 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 405 try 406 { 407 dynAny.current_component (); 408 409 fail (msg); 410 } 411 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 412 { 413 } 415 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 416 { 417 fail (msg + ": " + ex); 418 } 419 } 420 421 422 425 public void testCopyDynAny () 426 { 427 String msg; 428 org.omg.CORBA.TypeCode tc = null; 429 org.omg.DynamicAny.DynEnum dynAny = null; 430 org.omg.DynamicAny.DynEnum dynAny2 = null; 431 432 tc = EnumTypeHelper.type (); 433 dynAny = createDynAnyFromTypeCode (tc); 434 dynAny2 = (org.omg.DynamicAny.DynEnum ) dynAny.copy (); 435 436 msg = "The DynAny object created with the DynAny::copy operation "; 437 msg += "is not equal to the DynAny object it was copied from"; 438 assertTrue (msg, dynAny.equal (dynAny2)); 439 } 440 441 442 445 public void testIterateDynAny () 446 { 447 String msg; 448 int compCount = -1; 449 boolean seek; 450 org.omg.CORBA.TypeCode tc = null; 451 org.omg.DynamicAny.DynEnum dynAny = null; 452 453 tc = EnumTypeHelper.type (); 454 dynAny = createDynAnyFromTypeCode (tc); 455 456 try 458 { 459 compCount = dynAny.component_count (); 460 } 461 catch (Throwable ex) 462 { 463 fail ("Unexpected error raised by DynAny::component_count operation"); 465 } 466 msg = "The number of components returned from the "; 467 msg += "DynAny::component_count operation is incorrect"; 468 assertEquals (msg, 0, compCount); 469 470 msg = "The DynAny::seek operation indicates that a valid component "; 472 msg += "exists but the DynAny should have no components"; 473 seek = dynAny.seek (0); 474 assertTrue (msg, !seek); 475 476 try 478 { 479 dynAny = (org.omg.DynamicAny.DynEnum ) dynAny.current_component (); 480 481 msg = "A TypeMismatch exception was not raised by the "; 482 msg += "DynAny::current_component operation when trying to access "; 483 msg += "the current component of a DynAny with no components"; 484 fail (msg); 485 } 486 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 487 { 488 } 490 } 491 492 493 private static class Setup extends TestSetup 494 { 495 public Setup (Test test) 496 { 497 super (test); 498 } 499 500 protected void setUp () 501 { 502 org.omg.CORBA.Object obj = null; 503 504 orb = ORBSetup.getORB (); 505 try 506 { 507 obj = orb.resolve_initial_references ("DynAnyFactory"); 508 } 509 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 510 { 511 fail ("Failed to resolve DynAnyFactory: " + ex); 512 } 513 try 514 { 515 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 516 } 517 catch (Throwable ex) 518 { 519 fail ("Failed to narrow to DynAnyFactory: " + ex); 520 } 521 } 522 523 protected void tearDown () 524 { 525 } 526 } 527 528 529 532 private static org.omg.DynamicAny.DynEnum createDynAnyFromAny 533 (org.omg.CORBA.Any any) 534 { 535 String msg; 536 org.omg.DynamicAny.DynEnum dynAny = null; 537 538 try 539 { 540 dynAny = (org.omg.DynamicAny.DynEnum ) factory.create_dyn_any (any); 541 } 542 catch (Throwable ex) 543 { 544 msg = "Factory failed to create DynAny from Any using "; 545 msg += "DynAny::create_dyn_any operation: " + ex; 546 fail (msg); 547 } 548 return dynAny; 549 } 550 551 552 555 private static org.omg.DynamicAny.DynEnum createDynAnyFromTypeCode 556 (org.omg.CORBA.TypeCode tc) 557 { 558 String msg; 559 org.omg.DynamicAny.DynEnum dynAny = null; 560 561 try 562 { 563 dynAny = (org.omg.DynamicAny.DynEnum ) 564 factory.create_dyn_any_from_type_code (tc); 565 } 566 catch (Throwable ex) 567 { 568 msg = "Factory failed to create DynAny from TypeCode using "; 569 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 570 fail (msg); 571 } 572 return dynAny; 573 } 574 575 } 576 | Popular Tags |