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 27 import java.math.BigDecimal ; 28 import java.lang.reflect.Method ; 29 30 36 37 public class DynAnyFixedTest extends TestCase 38 { 39 private static org.omg.DynamicAny.DynAnyFactory factory = null; 40 private static org.omg.CORBA.ORB orb = null; 41 42 43 public DynAnyFixedTest (String name) 44 { 45 super (name); 46 } 47 48 49 public static Test suite () 50 { 51 TestSuite suite = new TestSuite ("DynFixed Tests"); 52 Setup setup = new Setup (suite); 53 ORBSetup osetup = new ORBSetup (setup); 54 55 suite.addTest (new DynAnyFixedTest ("testFactoryCreateFromAny")); 56 suite.addTest (new DynAnyFixedTest ("testFactoryCreateFromTypeCode")); 57 suite.addTest (new DynAnyFixedTest ("testCompareDynAny")); 58 suite.addTest (new DynAnyFixedTest ("testAccessFixedValue")); 59 suite.addTest (new DynAnyFixedTest ("testAccessTypeMismatchEx")); 60 suite.addTest (new DynAnyFixedTest ("testAccessInvalidValueEx")); 61 suite.addTest (new DynAnyFixedTest ("testDynAnyTypeCode")); 62 suite.addTest (new DynAnyFixedTest ("testInitDynAnyFromDynAny")); 63 suite.addTest (new DynAnyFixedTest ("testInitDynAnyFromAny")); 64 suite.addTest (new DynAnyFixedTest ("testInitFromAnyTypeMismatchEx")); 65 suite.addTest (new DynAnyFixedTest ("testGenerateAnyFromDynAny")); 66 suite.addTest (new DynAnyFixedTest ("testDestroyDynAny")); 67 suite.addTest (new DynAnyFixedTest ("testCopyDynAny")); 68 suite.addTest (new DynAnyFixedTest ("testIterateDynAny")); 69 70 return osetup; 71 } 72 73 74 78 public void testFactoryCreateFromAny () 79 { 80 BigDecimal fixedVal; 81 org.omg.CORBA.Any any = null; 82 org.omg.CORBA.TypeCode tc = null; 83 84 tc = orb.create_fixed_tc ((short) 2, (short) 1); 85 fixedVal = new BigDecimal ("1.0"); 86 any = orb.create_any (); 87 any.insert_fixed (fixedVal, tc); 88 89 createDynAnyFromAny (any); 90 } 91 92 93 97 public void testFactoryCreateFromTypeCode () 98 { 99 org.omg.CORBA.TypeCode tc = null; 100 101 tc = orb.create_fixed_tc ((short) 2, (short) 1); 102 103 createDynAnyFromTypeCode (tc); 104 } 105 106 107 110 public void testCompareDynAny () 111 { 112 String msg; 113 BigDecimal fixedVal; 114 org.omg.CORBA.Any any = null; 115 org.omg.CORBA.TypeCode tc = null; 116 org.omg.DynamicAny.DynFixed dynAny = null; 117 org.omg.DynamicAny.DynFixed dynAny2 = null; 118 119 tc = orb.create_fixed_tc ((short) 2, (short) 1); 120 fixedVal = new BigDecimal ("1.0"); 121 any = orb.create_any (); 122 any.insert_fixed (fixedVal, tc); 123 dynAny = createDynAnyFromAny (any); 124 dynAny2 = createDynAnyFromAny (any); 125 126 msg = "Comparing two equal DynAny values using DynAny::equal failed"; 127 assertTrue (msg, dynAny.equal (dynAny2)); 128 } 129 130 131 134 public void testAccessFixedValue () 135 { 136 String msg; 137 Boolean setVal; 138 BigDecimal fixedVal; 139 BigDecimal fixedVal2; 140 org.omg.CORBA.TypeCode tc = null; 141 org.omg.DynamicAny.DynFixed dynAny = null; 142 143 Class fixedClass = null; 145 Method method = null; 146 Class [] paramTypes = new Class [1]; 147 Object [] params = new Object [1]; 148 149 fixedVal = new BigDecimal ("1.0"); 150 fixedVal2 = null; 151 tc = orb.create_fixed_tc ((short) 2, (short) 1); 152 dynAny = createDynAnyFromTypeCode (tc); 153 154 fixedClass = dynAny.getClass (); 155 paramTypes [0] = String .class; 156 params [0] = fixedVal.toString (); 157 158 try 160 { 161 method = fixedClass.getDeclaredMethod ("set_value", paramTypes); 162 } 163 catch (Throwable ex) 164 { 165 fail ("Unexpected error trying to obtain method to invoke: " + ex); 166 } 167 168 msg = "Failed to set value of DynFixed object with "; 169 msg += "DynFixed::set_value operation when truncation was not required"; 170 setVal = null; 171 try 172 { 173 setVal = (Boolean ) method.invoke (dynAny, params); 174 } 175 catch (Throwable ex) 176 { 177 fail (msg + ": " + ex); 178 } 179 if (setVal != null) 181 { 182 assertTrue (msg, setVal.booleanValue ()); 183 } 184 185 fixedVal = new BigDecimal ("1.01"); 186 params [0] = fixedVal.toString (); 187 188 msg = "Failed to set value of DynFixed object with "; 189 msg += "DynFixed::set_value operation when truncation was required"; 190 setVal = null; 191 try 192 { 193 setVal = (Boolean ) method.invoke (dynAny, params); 194 } 195 catch (Throwable ex) 196 { 197 fail (msg + ": " + ex); 198 } 199 if (setVal != null) 201 { 202 assertTrue (msg, !setVal.booleanValue ()); 203 } 204 205 try 206 { 207 fixedVal2 = new BigDecimal (dynAny.get_value ()); 208 } 209 catch (Throwable ex) 210 { 211 msg = "Failed to get value of DynFixed object with "; 212 msg += "DynFixed::get_value operation"; 213 fail (msg + ": " + ex); 214 } 215 msg = "Value inserted into DynAny object is not equal to value "; 216 msg += "extracted from same DynAny object"; 217 assertTrue (msg, fixedVal2.toString ().equals ("1.0")); 218 } 219 220 221 225 public void testAccessTypeMismatchEx () 226 { 227 String msg; 228 String badFixedVal; 229 org.omg.CORBA.TypeCode tc = null; 230 org.omg.DynamicAny.DynFixed dynAny = null; 231 232 badFixedVal = "j.0"; 233 tc = orb.create_fixed_tc ((short) 2, (short) 1); 234 dynAny = createDynAnyFromTypeCode (tc); 235 236 msg = "TypeMismatch exception not thrown by DynFixed::set_value "; 237 msg += "operation when a DynFixed object is set to an invalid value"; 238 try 239 { 240 dynAny.set_value (badFixedVal); 241 242 fail (msg); 243 } 244 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 245 { 246 } 248 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 249 { 250 fail (msg + ": " + ex); 251 } 252 } 253 254 255 259 public void testAccessInvalidValueEx () 260 { 261 String msg; 262 BigDecimal fixedVal; 263 org.omg.CORBA.TypeCode tc = null; 264 org.omg.DynamicAny.DynFixed dynAny = null; 265 266 fixedVal = new BigDecimal ("10.01"); 267 tc = orb.create_fixed_tc ((short) 2, (short) 1); 268 dynAny = createDynAnyFromTypeCode (tc); 269 270 msg = "InvalidValue exception not thrown by DynFixed::set_value "; 271 msg += "operation when there are too many digits in the fixed value"; 272 try 273 { 274 dynAny.set_value (fixedVal.toString ()); 275 276 fail (msg); 277 } 278 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 279 { 280 } 282 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 283 { 284 fail (msg + ": " + ex); 285 } 286 } 287 288 289 292 public void testDynAnyTypeCode () 293 { 294 String msg; 295 org.omg.CORBA.TypeCode tc = null; 296 org.omg.DynamicAny.DynFixed dynAny = null; 297 298 tc = orb.create_fixed_tc ((short) 2, (short) 1); 299 dynAny = createDynAnyFromTypeCode (tc); 300 301 msg = "Incorrect TypeCode retrieved from DynAny::type operation"; 302 assertTrue (msg, dynAny.type ().equal (tc)); 303 } 304 305 306 309 public void testInitDynAnyFromDynAny () 310 { 311 String msg; 312 BigDecimal fixedVal; 313 org.omg.CORBA.Any any = null; 314 org.omg.CORBA.TypeCode tc = null; 315 org.omg.DynamicAny.DynFixed dynAny = null; 316 org.omg.DynamicAny.DynFixed dynAny2 = null; 317 318 tc = orb.create_fixed_tc ((short) 2, (short) 1); 319 dynAny = createDynAnyFromTypeCode (tc); 320 321 fixedVal = new BigDecimal ("1.0"); 322 any = orb.create_any (); 323 any.insert_fixed (fixedVal, tc); 324 dynAny2 = createDynAnyFromAny (any); 325 326 msg = "Failed to initialize a DynAny object from another DynAny "; 327 msg += "object using the DynAny::assign operation"; 328 try 329 { 330 dynAny.assign (dynAny2); 331 } 332 catch (Throwable ex) 333 { 334 fail (msg + ": " + ex); 335 } 336 assertTrue (msg, dynAny.equal (dynAny2)); 337 } 338 339 340 343 public void testInitDynAnyFromAny () 344 { 345 String msg; 346 BigDecimal fixedVal; 347 org.omg.CORBA.Any any = null; 348 org.omg.CORBA.TypeCode tc = null; 349 org.omg.DynamicAny.DynFixed dynAny = null; 350 org.omg.DynamicAny.DynFixed dynAny2 = null; 351 352 tc = orb.create_fixed_tc ((short) 2, (short) 1); 353 dynAny = createDynAnyFromTypeCode (tc); 354 355 fixedVal = new BigDecimal ("1.0"); 356 any = orb.create_any (); 357 any.insert_fixed (fixedVal, tc); 358 dynAny2 = createDynAnyFromAny (any); 359 360 msg = "Failed to initialize a DynAny object from an Any object "; 361 msg += "using the DynAny::from_any operation"; 362 try 363 { 364 dynAny.from_any (any); 365 } 366 catch (Throwable ex) 367 { 368 fail (msg + ": " + ex); 369 } 370 assertTrue (msg, dynAny.equal (dynAny2)); 371 } 372 373 374 378 public void testInitFromAnyTypeMismatchEx () 379 { 380 String msg; 381 org.omg.CORBA.Any any = null; 382 org.omg.CORBA.TypeCode tc = null; 383 org.omg.DynamicAny.DynFixed dynAny = null; 384 385 any = orb.create_any (); 386 any.insert_string ("Hello"); 387 388 tc = orb.create_fixed_tc ((short) 2, (short) 1); 389 dynAny = createDynAnyFromTypeCode (tc); 390 391 msg = "TypeMismatch exception not thrown by DynAny::from_any "; 392 msg += "operation when DynAny and Any operands have different types"; 393 try 394 { 395 dynAny.from_any (any); 396 397 fail (msg); 398 } 399 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 400 { 401 } 403 catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue ex) 404 { 405 fail (msg + ": " + ex); 406 } 407 } 408 409 410 413 public void testGenerateAnyFromDynAny () 414 { 415 String msg; 416 BigDecimal fixedVal; 417 org.omg.CORBA.Any any = null; 418 org.omg.CORBA.TypeCode tc = null; 419 org.omg.DynamicAny.DynFixed dynAny = null; 420 org.omg.DynamicAny.DynFixed dynAny2 = null; 421 422 tc = orb.create_fixed_tc ((short) 2, (short) 1); 423 fixedVal = new BigDecimal ("1.0"); 424 dynAny = createDynAnyFromTypeCode (tc); 425 426 any = orb.create_any (); 427 any = dynAny.to_any (); 428 dynAny2 = createDynAnyFromAny (any); 429 430 msg = "The DynAny::to_any operation failed to create an Any "; 431 msg += "object with the same value as the DynAny object"; 432 assertTrue (msg, dynAny.equal (dynAny2)); 433 } 434 435 436 439 public void testDestroyDynAny () 440 { 441 String msg; 442 BigDecimal fixedVal; 443 org.omg.CORBA.Any any = null; 444 org.omg.CORBA.TypeCode tc = null; 445 org.omg.DynamicAny.DynFixed dynAny = null; 446 447 tc = orb.create_fixed_tc ((short) 2, (short) 1); 448 fixedVal = new BigDecimal ("1.0"); 449 any = orb.create_any (); 450 any.insert_fixed (fixedVal, tc); 451 dynAny = createDynAnyFromAny (any); 452 dynAny.destroy (); 453 454 try 455 { 456 dynAny.type (); 457 458 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 459 msg += "calling DynAny::type operation on a destroyed DynAny object "; 460 msg += "did not raise OBJECT_NOT_EXIST exception"; 461 fail (msg); 462 } 463 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 464 { 465 } 467 468 msg = "Failed to destroy DynAny using DynAny::destroy operation - "; 469 msg += "calling DynAny::current_component operation on a destroyed "; 470 msg += "DynAny object did not raise OBJECT_NOT_EXIST exception"; 471 try 472 { 473 dynAny.current_component (); 474 475 fail (msg); 476 } 477 catch (org.omg.CORBA.OBJECT_NOT_EXIST ex) 478 { 479 } 481 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 482 { 483 fail (msg + ": " + ex); 484 } 485 } 486 487 488 491 public void testCopyDynAny () 492 { 493 String msg; 494 BigDecimal fixedVal; 495 org.omg.CORBA.TypeCode tc = null; 496 org.omg.DynamicAny.DynFixed dynAny = null; 497 org.omg.DynamicAny.DynFixed dynAny2 = null; 498 499 fixedVal = new BigDecimal ("1.0"); 500 tc = orb.create_fixed_tc ((short) 2, (short) 1); 501 dynAny = createDynAnyFromTypeCode (tc); 502 503 try 504 { 505 dynAny.set_value (fixedVal.toString ()); 506 } 507 catch (Throwable ex) 508 { 509 fail ("Failed to insert value into DynAny object: " + ex); 510 } 511 dynAny2 = (org.omg.DynamicAny.DynFixed ) dynAny.copy (); 512 513 msg = "The DynAny object created with the DynAny::copy operation "; 514 msg += "is not equal to the DynAny object it was copied from"; 515 assertTrue (msg, dynAny.equal (dynAny2)); 516 } 517 518 519 522 public void testIterateDynAny () 523 { 524 String msg; 525 int compCount = -1; 526 boolean seek; 527 org.omg.CORBA.TypeCode tc = null; 528 org.omg.DynamicAny.DynFixed dynAny = null; 529 530 tc = orb.create_fixed_tc ((short) 2, (short) 1); 531 dynAny = createDynAnyFromTypeCode (tc); 532 533 try 535 { 536 compCount = dynAny.component_count (); 537 } 538 catch (Throwable ex) 539 { 540 fail ("Unexpected error raised by DynAny::component_count operation"); 542 } 543 msg = "The number of components returned from the "; 544 msg += "DynAny::component_count operation is incorrect"; 545 assertEquals (msg, 0, compCount); 546 547 msg = "The DynAny::seek operation indicates that a valid component "; 549 msg += "exists but the DynAny should have no components"; 550 seek = dynAny.seek (0); 551 assertTrue (msg, !seek); 552 553 try 555 { 556 dynAny = (org.omg.DynamicAny.DynFixed ) dynAny.current_component (); 557 558 msg = "A TypeMismatch exception was not raised by the "; 559 msg += "DynAny::current_component operation when trying to access "; 560 msg += "the current component of a DynAny with no components"; 561 fail (msg); 562 } 563 catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch ex) 564 { 565 } 567 } 568 569 570 private static class Setup extends TestSetup 571 { 572 public Setup (Test test) 573 { 574 super (test); 575 } 576 577 protected void setUp () 578 { 579 org.omg.CORBA.Object obj = null; 580 581 orb = ORBSetup.getORB (); 582 try 583 { 584 obj = orb.resolve_initial_references ("DynAnyFactory"); 585 } 586 catch (org.omg.CORBA.ORBPackage.InvalidName ex) 587 { 588 fail ("Failed to resolve DynAnyFactory: " + ex); 589 } 590 try 591 { 592 factory = org.omg.DynamicAny.DynAnyFactoryHelper.narrow (obj); 593 } 594 catch (Throwable ex) 595 { 596 fail ("Failed to narrow to DynAnyFactory: " + ex); 597 } 598 } 599 600 protected void tearDown () 601 { 602 } 603 } 604 605 606 609 private static org.omg.DynamicAny.DynFixed createDynAnyFromAny 610 (org.omg.CORBA.Any any) 611 { 612 String msg; 613 org.omg.DynamicAny.DynFixed dynAny = null; 614 615 try 616 { 617 dynAny = (org.omg.DynamicAny.DynFixed ) factory.create_dyn_any (any); 618 } 619 catch (Throwable ex) 620 { 621 msg = "Factory failed to create DynAny from Any using "; 622 msg += "DynAny::create_dyn_any operation: " + ex; 623 fail (msg); 624 } 625 return dynAny; 626 } 627 628 629 632 private static org.omg.DynamicAny.DynFixed createDynAnyFromTypeCode 633 (org.omg.CORBA.TypeCode tc) 634 { 635 String msg; 636 org.omg.DynamicAny.DynFixed dynAny = null; 637 638 try 639 { 640 dynAny = (org.omg.DynamicAny.DynFixed ) 641 factory.create_dyn_any_from_type_code (tc); 642 } 643 catch (Throwable ex) 644 { 645 msg = "Factory failed to create DynAny from TypeCode using "; 646 msg += "DynAny::create_dyn_any_from_type_code operation: " + ex; 647 fail (msg); 648 } 649 return dynAny; 650 } 651 652 } 653 | Popular Tags |