1 // 2 // Copyright (c) ZeroC, Inc. All rights reserved. 3 // 4 5 package test.Ice.slicing.exceptions; 6 7 import test.Ice.slicing.exceptions.client.Test.*; 8 9 public class AllTests 10 { test(boolean b)11 private static void test(boolean b) 12 { 13 if(!b) 14 { 15 throw new RuntimeException(); 16 } 17 } 18 19 private static class Callback 20 { Callback()21 Callback() 22 { 23 _called = false; 24 } 25 check()26 public synchronized void check() 27 { 28 while(!_called) 29 { 30 try 31 { 32 wait(); 33 } 34 catch(InterruptedException ex) 35 { 36 } 37 } 38 39 _called = false; 40 } 41 called()42 public synchronized void called() 43 { 44 assert(!_called); 45 _called = true; 46 notify(); 47 } 48 49 private boolean _called; 50 } 51 52 private static class RelayI implements Relay 53 { 54 @Override knownPreservedAsBase(com.zeroc.Ice.Current current)55 public void knownPreservedAsBase(com.zeroc.Ice.Current current) 56 throws Base 57 { 58 KnownPreservedDerived ex = new KnownPreservedDerived(); 59 ex.b = "base"; 60 ex.kp = "preserved"; 61 ex.kpd = "derived"; 62 throw ex; 63 } 64 65 @Override knownPreservedAsKnownPreserved(com.zeroc.Ice.Current current)66 public void knownPreservedAsKnownPreserved(com.zeroc.Ice.Current current) 67 throws KnownPreserved 68 { 69 KnownPreservedDerived ex = new KnownPreservedDerived(); 70 ex.b = "base"; 71 ex.kp = "preserved"; 72 ex.kpd = "derived"; 73 throw ex; 74 } 75 76 @Override unknownPreservedAsBase(com.zeroc.Ice.Current current)77 public void unknownPreservedAsBase(com.zeroc.Ice.Current current) 78 throws Base 79 { 80 Preserved2 ex = new Preserved2(); 81 ex.b = "base"; 82 ex.kp = "preserved"; 83 ex.kpd = "derived"; 84 ex.p1 = new PreservedClass("bc", "pc"); 85 ex.p2 = ex.p1; 86 throw ex; 87 } 88 89 @Override unknownPreservedAsKnownPreserved(com.zeroc.Ice.Current current)90 public void unknownPreservedAsKnownPreserved(com.zeroc.Ice.Current current) 91 throws KnownPreserved 92 { 93 Preserved2 ex = new Preserved2(); 94 ex.b = "base"; 95 ex.kp = "preserved"; 96 ex.kpd = "derived"; 97 ex.p1 = new PreservedClass("bc", "pc"); 98 ex.p2 = ex.p1; 99 throw ex; 100 } 101 } 102 allTests(test.TestHelper helper, boolean collocated)103 public static TestIntfPrx allTests(test.TestHelper helper, boolean collocated) 104 { 105 java.io.PrintWriter out = helper.getWriter(); 106 com.zeroc.Ice.Communicator communicator = helper.communicator(); 107 108 out.print("testing stringToProxy... "); 109 out.flush(); 110 String ref = "Test:" + helper.getTestEndpoint(0) + " -t 10000"; 111 com.zeroc.Ice.ObjectPrx base = communicator.stringToProxy(ref); 112 test(base != null); 113 out.println("ok"); 114 115 out.print("testing checked cast... "); 116 out.flush(); 117 TestIntfPrx test = TestIntfPrx.checkedCast(base); 118 test(test != null); 119 test(test.equals(base)); 120 out.println("ok"); 121 122 out.print("base... "); 123 out.flush(); 124 { 125 try 126 { 127 test.baseAsBase(); 128 test(false); 129 } 130 catch(Base b) 131 { 132 test(b.b.equals("Base.b")); 133 test(b.ice_id().equals("::Test::Base")); 134 } 135 catch(Exception ex) 136 { 137 test(false); 138 } 139 } 140 out.println("ok"); 141 142 out.print("base (AMI)... "); 143 out.flush(); 144 { 145 Callback cb = new Callback(); 146 test.baseAsBaseAsync().whenComplete((result, ex) -> 147 { 148 test(ex != null && ex instanceof Base); 149 Base b = (Base)ex; 150 test(b.b.equals("Base.b")); 151 test(b.ice_id().equals("::Test::Base")); 152 cb.called(); 153 }); 154 cb.check(); 155 } 156 out.println("ok"); 157 158 out.print("slicing of unknown derived... "); 159 out.flush(); 160 { 161 try 162 { 163 test.unknownDerivedAsBase(); 164 test(false); 165 } 166 catch(Base b) 167 { 168 test(b.b.equals("UnknownDerived.b")); 169 test(b.ice_id().equals("::Test::Base")); 170 } 171 catch(Exception ex) 172 { 173 test(false); 174 } 175 } 176 out.println("ok"); 177 178 out.print("slicing of unknown derived (AMI)... "); 179 out.flush(); 180 { 181 Callback cb = new Callback(); 182 test.unknownDerivedAsBaseAsync().whenComplete((result, ex) -> 183 { 184 test(ex != null && ex instanceof Base); 185 Base b = (Base)ex; 186 test(b.b.equals("UnknownDerived.b")); 187 test(b.ice_id().equals("::Test::Base")); 188 cb.called(); 189 }); 190 cb.check(); 191 } 192 out.println("ok"); 193 194 out.print("non-slicing of known derived as base... "); 195 out.flush(); 196 { 197 try 198 { 199 test.knownDerivedAsBase(); 200 test(false); 201 } 202 catch(KnownDerived k) 203 { 204 test(k.b.equals("KnownDerived.b")); 205 test(k.kd.equals("KnownDerived.kd")); 206 test(k.ice_id().equals("::Test::KnownDerived")); 207 } 208 catch(Exception ex) 209 { 210 test(false); 211 } 212 } 213 out.println("ok"); 214 215 out.print("non-slicing of known derived as base (AMI)... "); 216 out.flush(); 217 { 218 Callback cb = new Callback(); 219 test.knownDerivedAsBaseAsync().whenComplete((result, ex) -> 220 { 221 test(ex != null && ex instanceof KnownDerived); 222 KnownDerived k = (KnownDerived)ex; 223 test(k.b.equals("KnownDerived.b")); 224 test(k.kd.equals("KnownDerived.kd")); 225 test(k.ice_id().equals("::Test::KnownDerived")); 226 cb.called(); 227 }); 228 cb.check(); 229 } 230 out.println("ok"); 231 232 out.print("non-slicing of known derived as derived... "); 233 out.flush(); 234 { 235 try 236 { 237 test.knownDerivedAsKnownDerived(); 238 test(false); 239 } 240 catch(KnownDerived k) 241 { 242 test(k.b.equals("KnownDerived.b")); 243 test(k.kd.equals("KnownDerived.kd")); 244 test(k.ice_id().equals("::Test::KnownDerived")); 245 } 246 catch(Exception ex) 247 { 248 test(false); 249 } 250 } 251 out.println("ok"); 252 253 out.print("non-slicing of known derived as derived (AMI)... "); 254 out.flush(); 255 { 256 Callback cb = new Callback(); 257 test.knownDerivedAsKnownDerivedAsync().whenComplete((result, ex) -> 258 { 259 test(ex != null && ex instanceof KnownDerived); 260 KnownDerived k = (KnownDerived)ex; 261 test(k.b.equals("KnownDerived.b")); 262 test(k.kd.equals("KnownDerived.kd")); 263 test(k.ice_id().equals("::Test::KnownDerived")); 264 cb.called(); 265 }); 266 cb.check(); 267 } 268 out.println("ok"); 269 270 out.print("slicing of unknown intermediate as base... "); 271 out.flush(); 272 { 273 try 274 { 275 test.unknownIntermediateAsBase(); 276 test(false); 277 } 278 catch(Base b) 279 { 280 test(b.b.equals("UnknownIntermediate.b")); 281 test(b.ice_id().equals("::Test::Base")); 282 } 283 catch(Exception ex) 284 { 285 test(false); 286 } 287 } 288 out.println("ok"); 289 290 out.print("slicing of unknown intermediate as base (AMI)... "); 291 out.flush(); 292 { 293 Callback cb = new Callback(); 294 test.unknownIntermediateAsBaseAsync().whenComplete((result, ex) -> 295 { 296 test(ex != null && ex instanceof Base); 297 Base b = (Base)ex; 298 test(b.b.equals("UnknownIntermediate.b")); 299 test(b.ice_id().equals("::Test::Base")); 300 cb.called(); 301 }); 302 cb.check(); 303 } 304 out.println("ok"); 305 306 out.print("slicing of known intermediate as base... "); 307 out.flush(); 308 { 309 try 310 { 311 test.knownIntermediateAsBase(); 312 test(false); 313 } 314 catch(KnownIntermediate ki) 315 { 316 test(ki.b.equals("KnownIntermediate.b")); 317 test(ki.ki.equals("KnownIntermediate.ki")); 318 test(ki.ice_id().equals("::Test::KnownIntermediate")); 319 } 320 catch(Exception ex) 321 { 322 test(false); 323 } 324 } 325 out.println("ok"); 326 327 out.print("slicing of known intermediate as base (AMI)... "); 328 out.flush(); 329 { 330 Callback cb = new Callback(); 331 test.knownIntermediateAsBaseAsync().whenComplete((result, ex) -> 332 { 333 test(ex != null && ex instanceof KnownIntermediate); 334 KnownIntermediate ki = (KnownIntermediate)ex; 335 test(ki.b.equals("KnownIntermediate.b")); 336 test(ki.ki.equals("KnownIntermediate.ki")); 337 test(ki.ice_id().equals("::Test::KnownIntermediate")); 338 cb.called(); 339 }); 340 cb.check(); 341 } 342 out.println("ok"); 343 344 out.print("slicing of known most derived as base... "); 345 out.flush(); 346 { 347 try 348 { 349 test.knownMostDerivedAsBase(); 350 test(false); 351 } 352 catch(KnownMostDerived kmd) 353 { 354 test(kmd.b.equals("KnownMostDerived.b")); 355 test(kmd.ki.equals("KnownMostDerived.ki")); 356 test(kmd.kmd.equals("KnownMostDerived.kmd")); 357 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 358 } 359 catch(Exception ex) 360 { 361 test(false); 362 } 363 } 364 out.println("ok"); 365 366 out.print("slicing of known most derived as base (AMI)... "); 367 out.flush(); 368 { 369 Callback cb = new Callback(); 370 test.knownMostDerivedAsBaseAsync().whenComplete((result, ex) -> 371 { 372 test(ex != null && ex instanceof KnownMostDerived); 373 KnownMostDerived kmd = (KnownMostDerived)ex; 374 test(kmd.b.equals("KnownMostDerived.b")); 375 test(kmd.ki.equals("KnownMostDerived.ki")); 376 test(kmd.kmd.equals("KnownMostDerived.kmd")); 377 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 378 cb.called(); 379 }); 380 cb.check(); 381 } 382 out.println("ok"); 383 384 out.print("non-slicing of known intermediate as intermediate... "); 385 out.flush(); 386 { 387 try 388 { 389 test.knownIntermediateAsKnownIntermediate(); 390 test(false); 391 } 392 catch(KnownIntermediate ki) 393 { 394 test(ki.b.equals("KnownIntermediate.b")); 395 test(ki.ki.equals("KnownIntermediate.ki")); 396 test(ki.ice_id().equals("::Test::KnownIntermediate")); 397 } 398 catch(Exception ex) 399 { 400 test(false); 401 } 402 } 403 out.println("ok"); 404 405 out.print("non-slicing of known intermediate as intermediate (AMI)... "); 406 out.flush(); 407 { 408 Callback cb = new Callback(); 409 test.knownIntermediateAsKnownIntermediateAsync().whenComplete((result, ex) -> 410 { 411 test(ex != null && ex instanceof KnownIntermediate); 412 KnownIntermediate ki = (KnownIntermediate)ex; 413 test(ki.b.equals("KnownIntermediate.b")); 414 test(ki.ki.equals("KnownIntermediate.ki")); 415 test(ki.ice_id().equals("::Test::KnownIntermediate")); 416 cb.called(); 417 }); 418 cb.check(); 419 } 420 out.println("ok"); 421 422 out.print("non-slicing of known most derived as intermediate... "); 423 out.flush(); 424 { 425 try 426 { 427 test.knownMostDerivedAsKnownIntermediate(); 428 test(false); 429 } 430 catch(KnownMostDerived kmd) 431 { 432 test(kmd.b.equals("KnownMostDerived.b")); 433 test(kmd.ki.equals("KnownMostDerived.ki")); 434 test(kmd.kmd.equals("KnownMostDerived.kmd")); 435 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 436 } 437 catch(Exception ex) 438 { 439 test(false); 440 } 441 } 442 out.println("ok"); 443 444 out.print("non-slicing of known most derived as intermediate (AMI)... "); 445 out.flush(); 446 { 447 Callback cb = new Callback(); 448 test.knownMostDerivedAsKnownIntermediateAsync().whenComplete((result, ex) -> 449 { 450 test(ex != null && ex instanceof KnownMostDerived); 451 KnownMostDerived kmd = (KnownMostDerived)ex; 452 test(kmd.b.equals("KnownMostDerived.b")); 453 test(kmd.ki.equals("KnownMostDerived.ki")); 454 test(kmd.kmd.equals("KnownMostDerived.kmd")); 455 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 456 cb.called(); 457 }); 458 cb.check(); 459 } 460 out.println("ok"); 461 462 out.print("non-slicing of known most derived as most derived... "); 463 out.flush(); 464 { 465 try 466 { 467 test.knownMostDerivedAsKnownMostDerived(); 468 test(false); 469 } 470 catch(KnownMostDerived kmd) 471 { 472 test(kmd.b.equals("KnownMostDerived.b")); 473 test(kmd.ki.equals("KnownMostDerived.ki")); 474 test(kmd.kmd.equals("KnownMostDerived.kmd")); 475 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 476 } 477 catch(Exception ex) 478 { 479 test(false); 480 } 481 } 482 out.println("ok"); 483 484 out.print("non-slicing of known most derived as most derived (AMI)... "); 485 out.flush(); 486 { 487 Callback cb = new Callback(); 488 test.knownMostDerivedAsKnownMostDerivedAsync().whenComplete((result, ex) -> 489 { 490 test(ex != null && ex instanceof KnownMostDerived); 491 KnownMostDerived kmd = (KnownMostDerived)ex; 492 test(kmd.b.equals("KnownMostDerived.b")); 493 test(kmd.ki.equals("KnownMostDerived.ki")); 494 test(kmd.kmd.equals("KnownMostDerived.kmd")); 495 test(kmd.ice_id().equals("::Test::KnownMostDerived")); 496 cb.called(); 497 }); 498 cb.check(); 499 } 500 out.println("ok"); 501 502 out.print("slicing of unknown most derived, known intermediate as base... "); 503 out.flush(); 504 { 505 try 506 { 507 test.unknownMostDerived1AsBase(); 508 test(false); 509 } 510 catch(KnownIntermediate ki) 511 { 512 test(ki.b.equals("UnknownMostDerived1.b")); 513 test(ki.ki.equals("UnknownMostDerived1.ki")); 514 test(ki.ice_id().equals("::Test::KnownIntermediate")); 515 } 516 catch(Exception ex) 517 { 518 test(false); 519 } 520 } 521 out.println("ok"); 522 523 out.print("slicing of unknown most derived, known intermediate as base (AMI)... "); 524 out.flush(); 525 { 526 Callback cb = new Callback(); 527 test.unknownMostDerived1AsBaseAsync().whenComplete((result, ex) -> 528 { 529 test(ex != null && ex instanceof KnownIntermediate); 530 KnownIntermediate ki = (KnownIntermediate)ex; 531 test(ki.b.equals("UnknownMostDerived1.b")); 532 test(ki.ki.equals("UnknownMostDerived1.ki")); 533 test(ki.ice_id().equals("::Test::KnownIntermediate")); 534 cb.called(); 535 }); 536 cb.check(); 537 } 538 out.println("ok"); 539 540 out.print("slicing of unknown most derived, known intermediate as intermediate... "); 541 out.flush(); 542 { 543 try 544 { 545 test.unknownMostDerived1AsKnownIntermediate(); 546 test(false); 547 } 548 catch(KnownIntermediate ki) 549 { 550 test(ki.b.equals("UnknownMostDerived1.b")); 551 test(ki.ki.equals("UnknownMostDerived1.ki")); 552 test(ki.ice_id().equals("::Test::KnownIntermediate")); 553 } 554 catch(Exception ex) 555 { 556 test(false); 557 } 558 } 559 out.println("ok"); 560 561 out.print("slicing of unknown most derived, known intermediate as intermediate (AMI)... "); 562 out.flush(); 563 { 564 Callback cb = new Callback(); 565 test.unknownMostDerived1AsKnownIntermediateAsync().whenComplete((result, ex) -> 566 { 567 test(ex != null && ex instanceof KnownIntermediate); 568 KnownIntermediate ki = (KnownIntermediate)ex; 569 test(ki.b.equals("UnknownMostDerived1.b")); 570 test(ki.ki.equals("UnknownMostDerived1.ki")); 571 test(ki.ice_id().equals("::Test::KnownIntermediate")); 572 cb.called(); 573 }); 574 cb.check(); 575 } 576 out.println("ok"); 577 578 out.print("slicing of unknown most derived, unknown intermediate thrown as base... "); 579 out.flush(); 580 { 581 try 582 { 583 test.unknownMostDerived2AsBase(); 584 test(false); 585 } 586 catch(Base b) 587 { 588 test(b.b.equals("UnknownMostDerived2.b")); 589 test(b.ice_id().equals("::Test::Base")); 590 } 591 catch(Exception ex) 592 { 593 test(false); 594 } 595 } 596 out.println("ok"); 597 598 out.print("slicing of unknown most derived, unknown intermediate thrown as base (AMI)... "); 599 out.flush(); 600 { 601 Callback cb = new Callback(); 602 test.unknownMostDerived2AsBaseAsync().whenComplete((result, ex) -> 603 { 604 test(ex != null && ex instanceof Base); 605 Base b = (Base)ex; 606 test(b.b.equals("UnknownMostDerived2.b")); 607 test(b.ice_id().equals("::Test::Base")); 608 cb.called(); 609 }); 610 cb.check(); 611 } 612 out.println("ok"); 613 614 out.print("unknown most derived in compact format... "); 615 out.flush(); 616 { 617 try 618 { 619 test.unknownMostDerived2AsBaseCompact(); 620 test(false); 621 } 622 catch(Base ex) 623 { 624 // 625 // For the 1.0 encoding, the unknown exception is sliced to Base. 626 // 627 test(test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)); 628 } 629 catch(com.zeroc.Ice.UnknownUserException ex) 630 { 631 // 632 // An UnknownUserException is raised for the compact format because the 633 // most-derived type is unknown and the exception cannot be sliced. 634 // 635 test(!test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)); 636 } 637 catch(com.zeroc.Ice.OperationNotExistException ex) 638 { 639 } 640 catch(Exception ex) 641 { 642 test(false); 643 } 644 } 645 out.println("ok"); 646 647 out.print("preserved exceptions... "); 648 out.flush(); 649 { 650 try 651 { 652 test.unknownPreservedAsBase(); 653 test(false); 654 } 655 catch(Base ex) 656 { 657 if(test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)) 658 { 659 test(ex.ice_getSlicedData() == null); 660 } 661 else 662 { 663 com.zeroc.Ice.SlicedData slicedData = ex.ice_getSlicedData(); 664 test(slicedData != null); 665 test(slicedData.slices.length == 2); 666 test(slicedData.slices[1].typeId.equals("::Test::SPreserved1")); 667 test(slicedData.slices[0].typeId.equals("::Test::SPreserved2")); 668 } 669 } 670 671 try 672 { 673 test.unknownPreservedAsKnownPreserved(); 674 test(false); 675 } 676 catch(KnownPreserved ex) 677 { 678 test(ex.kp.equals("preserved")); 679 if(test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)) 680 { 681 test(ex.ice_getSlicedData() == null); 682 } 683 else 684 { 685 com.zeroc.Ice.SlicedData slicedData = ex.ice_getSlicedData(); 686 test(slicedData != null); 687 test(slicedData.slices.length == 2); 688 test(slicedData.slices[1].typeId.equals("::Test::SPreserved1")); 689 test(slicedData.slices[0].typeId.equals("::Test::SPreserved2")); 690 } 691 } 692 693 com.zeroc.Ice.ObjectAdapter adapter = communicator.createObjectAdapter(""); 694 RelayPrx relay = RelayPrx.uncheckedCast(adapter.addWithUUID(new RelayI())); 695 adapter.activate(); 696 test.ice_getConnection().setAdapter(adapter); 697 698 try 699 { 700 test.relayKnownPreservedAsBase(relay); 701 test(false); 702 } 703 catch(KnownPreservedDerived ex) 704 { 705 test(ex.b.equals("base")); 706 test(ex.kp.equals("preserved")); 707 test(ex.kpd.equals("derived")); 708 } 709 catch(com.zeroc.Ice.OperationNotExistException ex) 710 { 711 } 712 catch(Exception ex) 713 { 714 test(false); 715 } 716 717 try 718 { 719 test.relayKnownPreservedAsKnownPreserved(relay); 720 test(false); 721 } 722 catch(KnownPreservedDerived ex) 723 { 724 test(ex.b.equals("base")); 725 test(ex.kp.equals("preserved")); 726 test(ex.kpd.equals("derived")); 727 } 728 catch(com.zeroc.Ice.OperationNotExistException ex) 729 { 730 } 731 catch(Exception ex) 732 { 733 test(false); 734 } 735 736 try 737 { 738 test.relayUnknownPreservedAsBase(relay); 739 test(false); 740 } 741 catch(Preserved2 ex) 742 { 743 test(ex.b.equals("base")); 744 test(ex.kp.equals("preserved")); 745 test(ex.kpd.equals("derived")); 746 test(ex.p1.ice_id().equals(PreservedClass.ice_staticId())); 747 PreservedClass pc = (PreservedClass)ex.p1; 748 test(pc.bc.equals("bc")); 749 test(pc.pc.equals("pc")); 750 test(ex.p2 == ex.p1); 751 } 752 catch(KnownPreservedDerived ex) 753 { 754 // 755 // For the 1.0 encoding, the unknown exception is sliced to KnownPreserved. 756 // 757 test(test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)); 758 test(ex.b.equals("base")); 759 test(ex.kp.equals("preserved")); 760 test(ex.kpd.equals("derived")); 761 } 762 catch(com.zeroc.Ice.OperationNotExistException ex) 763 { 764 } 765 catch(Exception ex) 766 { 767 test(false); 768 } 769 770 try 771 { 772 test.relayUnknownPreservedAsKnownPreserved(relay); 773 test(false); 774 } 775 catch(Preserved2 ex) 776 { 777 test(ex.b.equals("base")); 778 test(ex.kp.equals("preserved")); 779 test(ex.kpd.equals("derived")); 780 test(ex.p1.ice_id().equals(PreservedClass.ice_staticId())); 781 PreservedClass pc = (PreservedClass)ex.p1; 782 test(pc.bc.equals("bc")); 783 test(pc.pc.equals("pc")); 784 test(ex.p2 == ex.p1); 785 } 786 catch(KnownPreservedDerived ex) 787 { 788 // 789 // For the 1.0 encoding, the unknown exception is sliced to KnownPreserved. 790 // 791 test(test.ice_getEncodingVersion().equals(com.zeroc.Ice.Util.Encoding_1_0)); 792 test(ex.b.equals("base")); 793 test(ex.kp.equals("preserved")); 794 test(ex.kpd.equals("derived")); 795 } 796 catch(com.zeroc.Ice.OperationNotExistException ex) 797 { 798 } 799 catch(Exception ex) 800 { 801 test(false); 802 } 803 804 adapter.destroy(); 805 } 806 out.println("ok"); 807 808 return test; 809 } 810 } 811