SerializationStressTest1.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package tests.api.java.io; 18 19import dalvik.annotation.TestInfo; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTarget; 22import dalvik.annotation.TestTargetClass; 23 24import java.io.IOException; 25import java.io.NotSerializableException; 26import java.io.Serializable; 27import java.util.Arrays; 28 29@TestTargetClass(Serializable.class) 30public class SerializationStressTest1 extends SerializationStressTest { 31 32 // The purpose of these two classes is to test if serialization, when 33 // loading, runs the object's constructor (wrong) or the constructor defined 34 // at the topmost Serializable superclass(correct). 35 static final int INIT_INT_VALUE = 7; 36 37 // HAS to be static class so that our constructor signature will remain 38 // untouched (no synthetic param) 39 private static class SerializationTest implements java.io.Serializable { 40 int anInt = INIT_INT_VALUE; 41 42 public SerializationTest() { 43 super(); 44 } 45 } 46 47 static final String INIT_STR_VALUE = "a string that is blortz"; 48 49 // HAS to be static class so that our constructor signature will remain 50 // untouched (no synthetic param) 51 private static class SerializationTestSubclass1 extends SerializationTest { 52 String aString = INIT_STR_VALUE; 53 54 public SerializationTestSubclass1() { 55 super(); 56 // Just to change default superclass init value 57 anInt = INIT_INT_VALUE / 2; 58 } 59 } 60 61 // ----------------------------------------------------------------------------------- 62 63 private static class SpecTestSuperClass implements Runnable { 64 protected java.lang.String instVar; 65 66 public void run() { 67 } 68 } 69 70 private static class SpecTest extends SpecTestSuperClass implements 71 Cloneable, Serializable { 72 public java.lang.String instVar1; 73 74 public static java.lang.String staticVar1; 75 76 public static java.lang.String staticVar2; 77 { 78 instVar1 = "NonStaticInitialValue"; 79 } 80 static { 81 staticVar1 = "StaticInitialValue"; 82 staticVar1 = new String(staticVar1); 83 } 84 85 public Object method(Object objParam, Object objParam2) { 86 return new Object(); 87 } 88 89 public boolean method(boolean bParam, Object objParam) { 90 return true; 91 } 92 93 public boolean method(boolean bParam, Object objParam, Object objParam2) { 94 return true; 95 } 96 97 } 98 99 private static class SpecTestSubclass extends SpecTest { 100 public transient java.lang.String transientInstVar = "transientValue"; 101 } 102 103 // ----------------------------------------------------------------------------------- 104 105 // This one tests what happens if the read/writeObject methods are defined 106 // Serialization should work fine. 107 private static class ReadWriteObject implements java.io.Serializable { 108 public boolean calledWriteObject = false; 109 110 public boolean calledReadObject = false; 111 112 public ReadWriteObject() { 113 super(); 114 } 115 116 private void readObject(java.io.ObjectInputStream in) 117 throws java.io.IOException, ClassNotFoundException { 118 calledReadObject = true; 119 String s = ((String) in.readObject()); 120 } 121 122 private void writeObject(java.io.ObjectOutputStream out) 123 throws java.io.IOException { 124 calledWriteObject = true; 125 out.writeObject(FOO); 126 } 127 } 128 129 // This one tests what happens if the read/writeObject methods are not 130 // private. 131 // Serialization should fail. 132 private static class PublicReadWriteObject implements java.io.Serializable { 133 public boolean calledWriteObject = false; 134 135 public boolean calledReadObject = false; 136 137 public PublicReadWriteObject() { 138 super(); 139 } 140 141 public void readObject(java.io.ObjectInputStream in) 142 throws java.io.IOException, ClassNotFoundException { 143 calledReadObject = true; 144 String s = ((String) in.readObject()); 145 } 146 147 public void writeObject(java.io.ObjectOutputStream out) 148 throws java.io.IOException { 149 calledWriteObject = true; 150 out.writeObject(FOO); 151 } 152 } 153 154 // This one tests if field names are serialized in the same way (sorting) 155 // across different VMs 156 private static class FieldOrder implements Serializable { 157 String aaa1NonPrimitive = "aaa1"; 158 159 int bbb1PrimitiveInt = 5; 160 161 boolean aaa2PrimitiveBoolean = true; 162 163 String bbb2NonPrimitive = "bbb2"; 164 } 165 166 // This one tests what happens if you define just readObject, but not 167 // writeObject. 168 // Does it run or not ? 169 private static class JustReadObject implements java.io.Serializable { 170 public boolean calledReadObject = false; 171 172 public JustReadObject() { 173 super(); 174 } 175 176 private void readObject(java.io.ObjectInputStream in) 177 throws java.io.IOException, ClassNotFoundException { 178 calledReadObject = true; 179 in.defaultReadObject(); 180 } 181 } 182 183 // This one tests what happens if you define just writeObject, but not 184 // readObject. 185 // Does it run or not ? 186 private static class JustWriteObject implements java.io.Serializable { 187 public boolean calledWriteObject = false; 188 189 public JustWriteObject() { 190 super(); 191 } 192 193 private void writeObject(java.io.ObjectOutputStream out) 194 throws java.io.IOException, ClassNotFoundException { 195 calledWriteObject = true; 196 out.defaultWriteObject(); 197 } 198 } 199 200 // This one tests class-based replacement when dumping 201 private static class ClassBasedReplacementWhenDumping implements 202 java.io.Serializable { 203 public boolean calledReplacement = false; 204 205 public ClassBasedReplacementWhenDumping() { 206 super(); 207 } 208 209 private Object writeReplace() { 210 calledReplacement = true; 211 return FOO; // Replacement is a String 212 } 213 } 214 215 // This one tests whether class-based replacement supports multiple levels. 216 // MultipleClassBasedReplacementWhenDumping -> C1 -> C2 -> C3 -> FOO 217 private static class MultipleClassBasedReplacementWhenDumping implements 218 java.io.Serializable { 219 private static class C1 implements java.io.Serializable { 220 private Object writeReplace() { 221 return new C2(); 222 } 223 } 224 225 private static class C2 implements java.io.Serializable { 226 private Object writeReplace() { 227 return new C3(); 228 } 229 } 230 231 private static class C3 implements java.io.Serializable { 232 private Object writeReplace() { 233 return FOO; 234 } 235 } 236 237 public MultipleClassBasedReplacementWhenDumping() { 238 super(); 239 } 240 241 private Object writeReplace() { 242 return new C1(); 243 } 244 } 245 246 // This one tests class-based replacement when loading 247 private static class ClassBasedReplacementWhenLoading implements 248 java.io.Serializable { 249 public ClassBasedReplacementWhenLoading() { 250 super(); 251 } 252 253 private Object readResolve() { 254 return FOO; // Replacement is a String 255 } 256 } 257 258 // This one tests what happens if a loading-replacement is not 259 // type-compatible with the original object 260 private static class ClassBasedReplacementWhenLoadingViolatesFieldType 261 implements java.io.Serializable { 262 public ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading(); 263 264 public ClassBasedReplacementWhenLoadingViolatesFieldType() { 265 super(); 266 } 267 } 268 269 // What happens if dumping causes an error and you try to reload ? 270 // Should the load throw the same exception ? 271 private static class MyExceptionWhenDumping1 implements 272 java.io.Serializable { 273 private static class MyException extends java.io.IOException { 274 }; 275 276 // A primitive instance variable exposes a bug in the serialization 277 // spec. 278 // Primitive instance variables are written without primitive data tags 279 // and so are read without checking for tags. If an exception is 280 // written, reading primitive data will just read bytes from the stream 281 // which may be tags 282 public boolean anInstanceVar = false; 283 284 public MyExceptionWhenDumping1() { 285 super(); 286 } 287 288 private void readObject(java.io.ObjectInputStream in) 289 throws java.io.IOException, ClassNotFoundException { 290 in.defaultReadObject(); 291 } 292 293 private void writeObject(java.io.ObjectOutputStream out) 294 throws java.io.IOException, ClassNotFoundException { 295 throw new MyException(); 296 } 297 } 298 299 // What happens if dumping causes an error and you try to reload ? 300 // Should the load throw the same exception ? 301 private static class MyExceptionWhenDumping2 implements 302 java.io.Serializable { 303 private static class MyException extends java.io.IOException { 304 }; 305 306 public Integer anInstanceVar = new Integer(0xA1); 307 308 public MyExceptionWhenDumping2() { 309 super(); 310 } 311 312 private void readObject(java.io.ObjectInputStream in) 313 throws java.io.IOException, ClassNotFoundException { 314 in.defaultReadObject(); 315 } 316 317 private void writeObject(java.io.ObjectOutputStream out) 318 throws java.io.IOException, ClassNotFoundException { 319 throw new MyException(); 320 } 321 } 322 323 // What happens if dumping causes an error (NonSerializable inst var) and 324 // you try to reload ? 325 // Should the load throw the same exception ? 326 private static class NonSerializableExceptionWhenDumping implements 327 java.io.Serializable { 328 public Object anInstanceVar = new Object(); 329 330 public NonSerializableExceptionWhenDumping() { 331 super(); 332 } 333 } 334 335 // What happens if dumping causes an error (which is not serializable) and 336 // you try to reload ? 337 // Should the load throw the same exception ? 338 private static class MyUnserializableExceptionWhenDumping implements 339 java.io.Serializable { 340 private static class MyException extends java.io.IOException { 341 private Object notSerializable = new Object(); 342 }; 343 344 public boolean anInstanceVar = false; 345 346 public MyUnserializableExceptionWhenDumping() { 347 super(); 348 } 349 350 private void readObject(java.io.ObjectInputStream in) 351 throws java.io.IOException, ClassNotFoundException { 352 in.defaultReadObject(); 353 } 354 355 private void writeObject(java.io.ObjectOutputStream out) 356 throws java.io.IOException, ClassNotFoundException { 357 throw new MyException(); 358 } 359 } 360 361 public SerializationStressTest1(String name) { 362 super(name); 363 } 364 365 @TestInfo( 366 level = TestLevel.COMPLETE, 367 purpose = "Verifies serialization.", 368 targets = { 369 @TestTarget( 370 methodName = "!Serialization", 371 methodArgs = {} 372 ) 373 }) 374 public void test_18_1_writeObject() { 375 // Test for method void 376 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 377 378 Object objToSave = null; 379 Object objLoaded; 380 381 try { 382 objToSave = "HelloWorld"; 383 if (DEBUG) 384 System.out.println("Obj = " + objToSave); 385 objLoaded = dumpAndReload(objToSave); 386 assertTrue(MSG_TEST_FAILED + objToSave, (((String) objLoaded) 387 .equals((String) objToSave))); 388 389 } catch (IOException e) { 390 fail("IOException serializing data : " + e.getMessage()); 391 } catch (ClassNotFoundException e) { 392 fail("ClassNotFoundException reading Object type: " 393 + e.getMessage()); 394 } catch (Error err) { 395 System.out.println("Error when obj = " + objToSave); 396 // err.printStackTrace(); 397 throw err; 398 } 399 } 400 401 @TestInfo( 402 level = TestLevel.COMPLETE, 403 purpose = "Verifies serialization.", 404 targets = { 405 @TestTarget( 406 methodName = "!Serialization", 407 methodArgs = {} 408 ) 409 }) 410 public void test_18_2_writeObject() { 411 // Test for method void 412 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 413 414 Object objToSave = null; 415 Object objLoaded; 416 417 try { 418 objToSave = null; 419 if (DEBUG) 420 System.out.println("Obj = " + objToSave); 421 objLoaded = dumpAndReload(objToSave); 422 assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave); 423 424 } catch (IOException e) { 425 fail("IOException serializing data : " + e.getMessage()); 426 } catch (ClassNotFoundException e) { 427 fail("ClassNotFoundException reading Object type : " 428 + e.getMessage()); 429 } catch (Error err) { 430 System.out.println("Error when obj = " + objToSave); 431 // err.printStackTrace(); 432 throw err; 433 } 434 } 435 436 @TestInfo( 437 level = TestLevel.COMPLETE, 438 purpose = "Verifies serialization.", 439 targets = { 440 @TestTarget( 441 methodName = "!Serialization", 442 methodArgs = {} 443 ) 444 }) 445 public void test_18_3_writeObject() { 446 // Test for method void 447 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 448 449 Object objToSave = null; 450 Object objLoaded; 451 452 try { 453 byte[] bytes = { 0, 1, 2, 3 }; 454 objToSave = bytes; 455 if (DEBUG) 456 System.out.println("Obj = " + objToSave); 457 objLoaded = dumpAndReload(objToSave); 458 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 459 (byte[]) objLoaded, (byte[]) objToSave)); 460 461 } catch (IOException e) { 462 fail("IOException serializing data : " + e.getMessage()); 463 } catch (ClassNotFoundException e) { 464 fail("ClassNotFoundException reading Object type : " 465 + e.getMessage()); 466 } catch (Error err) { 467 System.out.println("Error when obj = " + objToSave); 468 // err.printStackTrace(); 469 throw err; 470 } 471 } 472 473 @TestInfo( 474 level = TestLevel.COMPLETE, 475 purpose = "Verifies serialization.", 476 targets = { 477 @TestTarget( 478 methodName = "!Serialization", 479 methodArgs = {} 480 ) 481 }) 482 public void test_18_4_writeObject() { 483 // Test for method void 484 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 485 486 Object objToSave = null; 487 Object objLoaded; 488 489 try { 490 int[] ints = { 0, 1, 2, 3 }; 491 objToSave = ints; 492 if (DEBUG) 493 System.out.println("Obj = " + objToSave); 494 objLoaded = dumpAndReload(objToSave); 495 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 496 (int[]) objLoaded, (int[]) objToSave)); 497 498 } catch (IOException e) { 499 fail("IOException serializing data : " + e.getMessage()); 500 } catch (ClassNotFoundException e) { 501 fail("ClassNotFoundException reading Object type : " 502 + e.getMessage()); 503 } catch (Error err) { 504 System.out.println("Error when obj = " + objToSave); 505 throw err; 506 } 507 } 508 509 @TestInfo( 510 level = TestLevel.COMPLETE, 511 purpose = "Verifies serialization.", 512 targets = { 513 @TestTarget( 514 methodName = "!Serialization", 515 methodArgs = {} 516 ) 517 }) 518 public void test_18_5_writeObject() { 519 // Test for method void 520 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 521 522 Object objToSave = null; 523 Object objLoaded; 524 525 try { 526 527 short[] shorts = { 0, 1, 2, 3 }; 528 objToSave = shorts; 529 if (DEBUG) 530 System.out.println("Obj = " + objToSave); 531 objLoaded = dumpAndReload(objToSave); 532 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 533 (short[]) objLoaded, (short[]) objToSave)); 534 535 } catch (IOException e) { 536 fail("IOException serializing data : " + e.getMessage()); 537 } catch (ClassNotFoundException e) { 538 fail("ClassNotFoundException reading Object type : " 539 + e.getMessage()); 540 } catch (Error err) { 541 System.out.println("Error when obj = " + objToSave); 542 // err.printStackTrace(); 543 throw err; 544 } 545 } 546 547 @TestInfo( 548 level = TestLevel.COMPLETE, 549 purpose = "Verifies serialization.", 550 targets = { 551 @TestTarget( 552 methodName = "!Serialization", 553 methodArgs = {} 554 ) 555 }) 556 public void test_18_6_writeObject() { 557 // Test for method void 558 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 559 560 Object objToSave = null; 561 Object objLoaded; 562 563 try { 564 long[] longs = { 0, 1, 2, 3 }; 565 objToSave = longs; 566 if (DEBUG) 567 System.out.println("Obj = " + objToSave); 568 objLoaded = dumpAndReload(objToSave); 569 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 570 (long[]) objLoaded, (long[]) objToSave)); 571 572 } catch (IOException e) { 573 fail("IOException serializing data : " + e.getMessage()); 574 } catch (ClassNotFoundException e) { 575 fail("ClassNotFoundException reading Object type : " 576 + e.getMessage()); 577 } catch (Error err) { 578 System.out.println("Error when obj = " + objToSave); 579 // err.printStackTrace(); 580 throw err; 581 } 582 } 583 584 @TestInfo( 585 level = TestLevel.COMPLETE, 586 purpose = "Verifies serialization.", 587 targets = { 588 @TestTarget( 589 methodName = "!Serialization", 590 methodArgs = {} 591 ) 592 }) 593 public void test_18_7_writeObject() { 594 // Test for method void 595 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 596 597 Object objToSave = null; 598 Object objLoaded; 599 600 try { 601 float[] floats = { 0.0f, 1.1f, 2.2f, 3.3f }; 602 objToSave = floats; 603 if (DEBUG) 604 System.out.println("Obj = " + objToSave); 605 objLoaded = dumpAndReload(objToSave); 606 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 607 (float[]) objLoaded, (float[]) objToSave)); 608 609 } catch (IOException e) { 610 fail("IOException serializing data: " + e.getMessage()); 611 } catch (ClassNotFoundException e) { 612 fail("ClassNotFoundException reading Object type : " 613 + e.getMessage()); 614 } catch (Error err) { 615 System.out.println("Error when obj = " + objToSave); 616 // err.printStackTrace(); 617 throw err; 618 } 619 } 620 621 @TestInfo( 622 level = TestLevel.COMPLETE, 623 purpose = "Verifies serialization.", 624 targets = { 625 @TestTarget( 626 methodName = "!Serialization", 627 methodArgs = {} 628 ) 629 }) 630 public void test_18_8_writeObject() { 631 // Test for method void 632 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 633 634 Object objToSave = null; 635 Object objLoaded; 636 637 try { 638 double[] doubles = { 0.0, 1.1, 2.2, 3.3 }; 639 objToSave = doubles; 640 if (DEBUG) 641 System.out.println("Obj = " + objToSave); 642 objLoaded = dumpAndReload(objToSave); 643 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 644 (double[]) objLoaded, (double[]) objToSave)); 645 646 } catch (IOException e) { 647 fail("IOException serializing data : " + e.getMessage()); 648 } catch (ClassNotFoundException e) { 649 fail("ClassNotFoundException reading Object type : " 650 + e.getMessage()); 651 } catch (Error err) { 652 System.out.println("Error when obj = " + objToSave); 653 // err.printStackTrace(); 654 throw err; 655 } 656 } 657 658 @TestInfo( 659 level = TestLevel.COMPLETE, 660 purpose = "Verifies serialization.", 661 targets = { 662 @TestTarget( 663 methodName = "!Serialization", 664 methodArgs = {} 665 ) 666 }) 667 public void test_18_9_writeObject() { 668 // Test for method void 669 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 670 671 Object objToSave = null; 672 Object objLoaded; 673 674 try { 675 boolean[] booleans = { true, false, false, true }; 676 objToSave = booleans; 677 if (DEBUG) 678 System.out.println("Obj = " + objToSave); 679 objLoaded = dumpAndReload(objToSave); 680 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 681 (boolean[]) objLoaded, (boolean[]) objToSave)); 682 683 } catch (IOException e) { 684 fail("IOException serializing data : " + e.getMessage()); 685 } catch (ClassNotFoundException e) { 686 fail("ClassNotFoundException reading Object type : " + e.getMessage()); 687 } catch (Error err) { 688 System.out.println("Error when obj = " + objToSave); 689 // err.printStackTrace(); 690 throw err; 691 } 692 } 693 694 @TestInfo( 695 level = TestLevel.COMPLETE, 696 purpose = "Verifies serialization.", 697 targets = { 698 @TestTarget( 699 methodName = "!Serialization", 700 methodArgs = {} 701 ) 702 }) 703 public void test_18_10_writeObject() { 704 // Test for method void 705 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 706 707 Object objToSave = null; 708 Object objLoaded; 709 710 try { 711 712 String[] strings = { "foo", "bar", "java" }; 713 objToSave = strings; 714 if (DEBUG) 715 System.out.println("Obj = " + objToSave); 716 objLoaded = dumpAndReload(objToSave); 717 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals( 718 (Object[]) objLoaded, (Object[]) objToSave)); 719 720 } catch (IOException e) { 721 fail("IOException serializing " + objToSave + " : " 722 + e.getMessage()); 723 } catch (ClassNotFoundException e) { 724 fail("Unable to read Object type: " + e.toString()); 725 } catch (Error err) { 726 System.out.println("Error when obj = " + objToSave); 727 // err.printStackTrace(); 728 throw err; 729 } 730 } 731 732 @TestInfo( 733 level = TestLevel.COMPLETE, 734 purpose = "Verifies serialization.", 735 targets = { 736 @TestTarget( 737 methodName = "!Serialization", 738 methodArgs = {} 739 ) 740 }) 741 public void test_18_11_writeObject() { 742 // Test for method void 743 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 744 745 Object objToSave = null; 746 Object objLoaded; 747 748 try { 749 750 objToSave = new Object(); // Not serializable 751 if (DEBUG) 752 System.out.println("Obj = " + objToSave); 753 boolean passed = false; 754 Throwable t = null; 755 try { 756 objLoaded = dumpAndReload(objToSave); 757 } catch (NotSerializableException ns) { 758 passed = true; 759 t = ns; 760 } catch (Exception wrongExc) { 761 passed = false; 762 t = wrongExc; 763 } 764 assertTrue( 765 "Failed to throw NotSerializableException when serializing " 766 + objToSave + " Threw(if non-null) this: " + t, 767 passed); 768 } catch (Error err) { 769 System.out.println("Error when obj = " + objToSave); 770 // err.printStackTrace(); 771 throw err; 772 } 773 } 774 775 @TestInfo( 776 level = TestLevel.COMPLETE, 777 purpose = "Verifies serialization.", 778 targets = { 779 @TestTarget( 780 methodName = "!Serialization", 781 methodArgs = {} 782 ) 783 }) 784 public void test_18_12_writeObject() { 785 // Test for method void 786 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 787 788 try { 789 if (DEBUG) 790 System.out.println("Obj = <mixed>"); 791 t_MixPrimitivesAndObjects(); 792 } catch (IOException e) { 793 fail("IOException serializing data : " + e.getMessage()); 794 } catch (ClassNotFoundException e) { 795 fail("ClassNotFoundException reading Object type : " 796 + e.getMessage()); 797 } catch (Error err) { 798 System.out.println("Error when dumping mixed types"); 799 // err.printStackTrace(); 800 throw err; 801 } 802 } 803 804 @TestInfo( 805 level = TestLevel.COMPLETE, 806 purpose = "Verifies serialization.", 807 targets = { 808 @TestTarget( 809 methodName = "!Serialization", 810 methodArgs = {} 811 ) 812 }) 813 public void test_18_13_writeObject() { 814 // Test for method void 815 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 816 817 Object objToSave = null; 818 Object objLoaded; 819 820 try { 821 SerializationTestSubclass1 st = new SerializationTestSubclass1(); 822 // Just change the default ivar values 823 st.anInt = Integer.MAX_VALUE; 824 st.aString = FOO; 825 objToSave = st; 826 if (DEBUG) 827 System.out.println("Obj = " + objToSave); 828 objLoaded = dumpAndReload(objToSave); 829 // non-serializable inst var has to be initialized from top 830 // constructor 831 assertTrue( 832 MSG_TEST_FAILED + objToSave, 833 ((SerializationTestSubclass1) objLoaded).anInt == Integer.MAX_VALUE); 834 // but serialized var has to be restored as it was in the object 835 // when dumped 836 assertTrue(MSG_TEST_FAILED + objToSave, 837 ((SerializationTestSubclass1) objLoaded).aString 838 .equals(FOO)); 839 } catch (IOException e) { 840 fail("Exception serializing " + objToSave + "\t->" 841 + e.toString()); 842 } catch (ClassNotFoundException e) { 843 fail("ClassNotFoundException reading Object type : " 844 + e.getMessage()); 845 } catch (Error err) { 846 System.out.println("Error when obj = " + objToSave); 847 err.printStackTrace(); 848 throw err; 849 } 850 } 851 852 @TestInfo( 853 level = TestLevel.COMPLETE, 854 purpose = "Verifies serialization.", 855 targets = { 856 @TestTarget( 857 methodName = "!Serialization", 858 methodArgs = {} 859 ) 860 }) 861 public void _test_18_14_writeObject() { 862 // Test for method void 863 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 864 865 Object objToSave = null; 866 Object objLoaded; 867 868 try { 869 SpecTest specTest = new SpecTest(); 870 // Just change the default ivar values 871 specTest.instVar = FOO; 872 specTest.instVar1 = specTest.instVar; 873 objToSave = specTest; 874 if (DEBUG) 875 System.out.println("Obj = " + objToSave); 876 objLoaded = dumpAndReload(objToSave); 877 // non-serializable inst var has to be initialized from top 878 // constructor 879 assertNull(MSG_TEST_FAILED + objToSave, 880 ((SpecTest) objLoaded).instVar); 881 // instVar from non-serialized class, cant be saved/restored 882 // by serialization but serialized ivar has to be restored as it 883 // was in the object when dumped 884 assertTrue(MSG_TEST_FAILED + objToSave, 885 ((SpecTest) objLoaded).instVar1.equals(FOO)); 886 887 } catch (IOException e) { 888 fail("Exception serializing " + objToSave + "\t->" 889 + e.toString()); 890 } catch (ClassNotFoundException e) { 891 fail("ClassNotFoundException reading Object type : " 892 + e.getMessage()); 893 } catch (Error err) { 894 System.out.println("Error when obj = " + objToSave); 895 // err.printStackTrace(); 896 throw err; 897 } 898 } 899 900 @TestInfo( 901 level = TestLevel.COMPLETE, 902 purpose = "Verifies serialization.", 903 targets = { 904 @TestTarget( 905 methodName = "!Serialization", 906 methodArgs = {} 907 ) 908 }) 909 public void _test_18_15_writeObject() { 910 // Test for method void 911 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 912 913 Object objToSave = null; 914 Object objLoaded; 915 916 try { 917 SpecTestSubclass specTestSubclass = new SpecTestSubclass(); 918 // Just change the default ivar values 919 specTestSubclass.transientInstVar = FOO; 920 objToSave = specTestSubclass; 921 if (DEBUG) 922 System.out.println("Obj = " + objToSave); 923 objLoaded = dumpAndReload(objToSave); 924 // non-serializable inst var cant be saved, and it is not init'ed 925 // from top constructor in this case 926 assertNull(MSG_TEST_FAILED + objToSave, 927 ((SpecTestSubclass) objLoaded).transientInstVar); 928 // transient slot, cant be saved/restored by serialization 929 } catch (IOException e) { 930 fail("Exception serializing " + objToSave + "\t->" 931 + e.toString()); 932 } catch (ClassNotFoundException e) { 933 fail("ClassNotFoundException reading Object type : " 934 + e.getMessage()); 935 } catch (Error err) { 936 System.out.println("Error when obj = " + objToSave); 937 // err.printStackTrace(); 938 throw err; 939 } 940 } 941 942 @TestInfo( 943 level = TestLevel.COMPLETE, 944 purpose = "Verifies serialization.", 945 targets = { 946 @TestTarget( 947 methodName = "!Serialization", 948 methodArgs = {} 949 ) 950 }) 951 public void test_18_16_writeObject() { 952 // Test for method void 953 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 954 955 Object objToSave = null; 956 Object objLoaded; 957 958 try { 959 960 String[] strings = new String[2]; 961 strings[0] = FOO; 962 strings[1] = (" " + FOO + " ").trim(); // Safe way to get a copy 963 // that is not == 964 objToSave = strings; 965 if (DEBUG) 966 System.out.println("Obj = " + objToSave); 967 objLoaded = dumpAndReload(objToSave); 968 String[] stringsLoaded = (String[]) objLoaded; 969 // Serialization has to use identity-based table for assigning IDs 970 assertTrue(MSG_TEST_FAILED + objToSave, 971 !(stringsLoaded[0] == stringsLoaded[1])); 972 } catch (IOException e) { 973 fail("Exception serializing " + objToSave + "\t->" 974 + e.toString()); 975 } catch (ClassNotFoundException e) { 976 fail("ClassNotFoundException reading Object type : " 977 + e.getMessage()); 978 } catch (Error err) { 979 System.out.println("Error when obj = " + objToSave); 980 // err.printStackTrace(); 981 throw err; 982 } 983 } 984 985 @TestInfo( 986 level = TestLevel.COMPLETE, 987 purpose = "Verifies serialization.", 988 targets = { 989 @TestTarget( 990 methodName = "!Serialization", 991 methodArgs = {} 992 ) 993 }) 994 public void test_18_17_writeObject() { 995 // Test for method void 996 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 997 998 Object objToSave = null; 999 Object objLoaded; 1000 1001 try { 1002 1003 ReadWriteObject readWrite = new ReadWriteObject(); 1004 objToSave = readWrite; 1005 if (DEBUG) 1006 System.out.println("Obj = " + objToSave); 1007 objLoaded = dumpAndReload(objToSave); 1008 // has to have called the writeObject on the instance to dump 1009 assertTrue(MSG_TEST_FAILED + objToSave, readWrite.calledWriteObject); 1010 // has to have called the readObject on the instance loaded 1011 assertTrue(MSG_TEST_FAILED + objToSave, 1012 ((ReadWriteObject) objLoaded).calledReadObject); 1013 1014 } catch (IOException e) { 1015 fail("Exception serializing " + objToSave + "\t->" 1016 + e.toString()); 1017 } catch (ClassNotFoundException e) { 1018 fail("ClassNotFoundException reading Object type : " 1019 + e.getMessage()); 1020 } catch (Error err) { 1021 System.out.println("Error when obj = " + objToSave); 1022 // err.printStackTrace(); 1023 throw err; 1024 } 1025 } 1026 1027 @TestInfo( 1028 level = TestLevel.COMPLETE, 1029 purpose = "Verifies serialization.", 1030 targets = { 1031 @TestTarget( 1032 methodName = "!Serialization", 1033 methodArgs = {} 1034 ) 1035 }) 1036 public void test_18_18_writeObject() { 1037 // Test for method void 1038 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1039 1040 Object objToSave = null; 1041 Object objLoaded; 1042 1043 try { 1044 PublicReadWriteObject publicReadWrite = new PublicReadWriteObject(); 1045 objToSave = publicReadWrite; 1046 if (DEBUG) 1047 System.out.println("Obj = " + objToSave); 1048 objLoaded = dumpAndReload(objToSave); 1049 // Can't have called the writeObject on the instance to dump 1050 assertTrue(MSG_TEST_FAILED + objToSave, 1051 !publicReadWrite.calledWriteObject); 1052 // Can't have called the readObject on the instance loaded 1053 assertTrue(MSG_TEST_FAILED + objToSave, 1054 !((PublicReadWriteObject) objLoaded).calledReadObject); 1055 1056 } catch (IOException e) { 1057 fail("Exception serializing " + objToSave + "\t->" 1058 + e.toString()); 1059 } catch (ClassNotFoundException e) { 1060 fail("ClassNotFoundException reading Object type : " 1061 + e.getMessage()); 1062 } catch (Error err) { 1063 System.out.println("Error when obj = " + objToSave); 1064 // err.printStackTrace(); 1065 throw err; 1066 } 1067 } 1068 1069 @TestInfo( 1070 level = TestLevel.COMPLETE, 1071 purpose = "Verifies serialization.", 1072 targets = { 1073 @TestTarget( 1074 methodName = "!Serialization", 1075 methodArgs = {} 1076 ) 1077 }) 1078 public void test_18_19_writeObject() { 1079 // Test for method void 1080 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1081 1082 Object objToSave = null; 1083 Object objLoaded; 1084 1085 try { 1086 FieldOrder fieldOrder = new FieldOrder(); 1087 objToSave = fieldOrder; 1088 if (DEBUG) 1089 System.out.println("Obj = " + objToSave); 1090 objLoaded = dumpAndReload(objToSave); 1091 // This test is only useful for X-loading, so if it managed to 1092 // dump&load, we passed the test 1093 assertTrue(MSG_TEST_FAILED + objToSave, true); 1094 1095 } catch (IOException e) { 1096 fail("IOException serializing " + objToSave + " : " 1097 + e.getMessage()); 1098 } catch (ClassNotFoundException e) { 1099 fail("ClassNotFoundException reading Object type : " 1100 + e.getMessage()); 1101 } catch (Error err) { 1102 System.out.println("Error when obj = " + objToSave); 1103 // err.printStackTrace(); 1104 throw err; 1105 } 1106 } 1107 1108 @TestInfo( 1109 level = TestLevel.COMPLETE, 1110 purpose = "Verifies serialization.", 1111 targets = { 1112 @TestTarget( 1113 methodName = "!Serialization", 1114 methodArgs = {} 1115 ) 1116 }) 1117 public void test_18_20_writeObject() { 1118 // Test for method void 1119 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1120 1121 Object objToSave = null; 1122 Object objLoaded; 1123 1124 try { 1125 objToSave = Class.forName("java.lang.Integer"); 1126 if (DEBUG) 1127 System.out.println("Obj = " + objToSave); 1128 objLoaded = dumpAndReload(objToSave); 1129 // Classes with the same name are unique, so test for == 1130 assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave); 1131 1132 } catch (IOException e) { 1133 fail("IOException serializing " + objToSave + " : " 1134 + e.getMessage()); 1135 } catch (ClassNotFoundException e) { 1136 fail("ClassNotFoundException reading Object type : " 1137 + e.getMessage()); 1138 } catch (Error err) { 1139 System.out.println("Error when obj = " + objToSave); 1140 // err.printStackTrace(); 1141 throw err; 1142 } 1143 } 1144 1145 @TestInfo( 1146 level = TestLevel.COMPLETE, 1147 purpose = "Verifies serialization.", 1148 targets = { 1149 @TestTarget( 1150 methodName = "!Serialization", 1151 methodArgs = {} 1152 ) 1153 }) 1154 public void test_18_21_writeObject() { 1155 // Test for method void 1156 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1157 1158 Object objToSave = null; 1159 Object objLoaded; 1160 1161 try { 1162 // Even though instances of java.lang.Object are not Serializable, 1163 // instances of java.lang.Class are. So, the object 1164 // java.lang.Object.class 1165 // should be serializable 1166 objToSave = Class.forName("java.lang.Object"); 1167 if (DEBUG) 1168 System.out.println("Obj = " + objToSave); 1169 objLoaded = dumpAndReload(objToSave); 1170 // Classes with the same name are unique, so test for == 1171 assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave); 1172 1173 } catch (IOException e) { 1174 fail("IOException serializing " + objToSave + " : " 1175 + e.getMessage()); 1176 } catch (ClassNotFoundException e) { 1177 fail("ClassNotFoundException reading Object type : " 1178 + e.getMessage()); 1179 } catch (Error err) { 1180 System.out.println("Error when obj = " + objToSave); 1181 // err.printStackTrace(); 1182 throw err; 1183 } 1184 } 1185 1186 @TestInfo( 1187 level = TestLevel.COMPLETE, 1188 purpose = "Verifies serialization.", 1189 targets = { 1190 @TestTarget( 1191 methodName = "!Serialization", 1192 methodArgs = {} 1193 ) 1194 }) 1195 public void test_18_22_writeObject() { 1196 // Test for method void 1197 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1198 1199 Object objToSave = null; 1200 Object objLoaded; 1201 1202 try { 1203 java.net.URL url = new java.net.URL("http://localhost/a.txt"); 1204 objToSave = url; 1205 if (DEBUG) 1206 System.out.println("Obj = " + objToSave); 1207 objLoaded = dumpAndReload(objToSave); 1208 assertTrue("URLs are not the same: " + url + "\t,\t" + objLoaded, 1209 url.equals(objLoaded)); 1210 1211 } catch (IOException e) { 1212 fail("IOException serializing " + objToSave + " : " 1213 + e.getMessage()); 1214 } catch (ClassNotFoundException e) { 1215 fail("ClassNotFoundException reading Object type : " 1216 + e.getMessage()); 1217 } catch (Error err) { 1218 System.out.println("Error when obj = " + objToSave); 1219 // err.printStackTrace(); 1220 throw err; 1221 } 1222 } 1223 1224 @TestInfo( 1225 level = TestLevel.COMPLETE, 1226 purpose = "Verifies serialization.", 1227 targets = { 1228 @TestTarget( 1229 methodName = "!Serialization", 1230 methodArgs = {} 1231 ) 1232 }) 1233 public void test_18_23_writeObject() { 1234 // Test for method void 1235 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1236 1237 Object objToSave = null; 1238 Object objLoaded; 1239 1240 try { 1241 1242 JustReadObject justReadObject = new JustReadObject(); 1243 objToSave = justReadObject; 1244 if (DEBUG) 1245 System.out.println("Obj = " + objToSave); 1246 objLoaded = dumpAndReload(objToSave); 1247 // Only calls readObject on the instance loaded if writeObject was 1248 // also defined 1249 assertTrue("Called readObject on an object without a writeObject", 1250 !((JustReadObject) objLoaded).calledReadObject); 1251 1252 } catch (IOException e) { 1253 fail("IOException serializing " + objToSave + " : " 1254 + e.getMessage()); 1255 } catch (ClassNotFoundException e) { 1256 fail("ClassNotFoundException reading Object type : " 1257 + e.getMessage()); 1258 } catch (Error err) { 1259 System.out.println("Error when obj = " + objToSave); 1260 // err.printStackTrace(); 1261 throw err; 1262 } 1263 } 1264 1265 @TestInfo( 1266 level = TestLevel.COMPLETE, 1267 purpose = "Verifies serialization.", 1268 targets = { 1269 @TestTarget( 1270 methodName = "!Serialization", 1271 methodArgs = {} 1272 ) 1273 }) 1274 public void test_18_24_writeObject() { 1275 // Test for method void 1276 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1277 1278 Object objToSave = null; 1279 Object objLoaded; 1280 1281 try { 1282 1283 JustWriteObject justWriteObject = new JustWriteObject(); 1284 objToSave = justWriteObject; 1285 if (DEBUG) 1286 System.out.println("Obj = " + objToSave); 1287 objLoaded = dumpAndReload(objToSave); 1288 // Call writeObject on the instance even if it does not define 1289 // readObject 1290 assertTrue(MSG_TEST_FAILED + objToSave, 1291 justWriteObject.calledWriteObject); 1292 1293 } catch (IOException e) { 1294 fail("IOException serializing " + objToSave + " : " 1295 + e.getMessage()); 1296 } catch (ClassNotFoundException e) { 1297 fail("ClassNotFoundException reading Object type: " 1298 + e.getMessage()); 1299 } catch (Error err) { 1300 System.out.println("Error when obj = " + objToSave); 1301 // err.printStackTrace(); 1302 throw err; 1303 } 1304 } 1305 1306 @TestInfo( 1307 level = TestLevel.COMPLETE, 1308 purpose = "Verifies serialization.", 1309 targets = { 1310 @TestTarget( 1311 methodName = "!Serialization", 1312 methodArgs = {} 1313 ) 1314 }) 1315 public void test_18_25_writeObject() { 1316 // Test for method void 1317 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1318 1319 Object objToSave = null; 1320 Object objLoaded; 1321 1322 try { 1323 java.util.Vector vector = new java.util.Vector(1); 1324 vector.add(FOO); 1325 objToSave = vector; 1326 if (DEBUG) 1327 System.out.println("Obj = " + objToSave); 1328 objLoaded = dumpAndReload(objToSave); 1329 // Has to have the string there 1330 assertTrue(MSG_TEST_FAILED + objToSave, FOO 1331 .equals(((java.util.Vector) objLoaded).elementAt(0))); 1332 1333 } catch (IOException e) { 1334 fail("IOException serializing " + objToSave + " : " 1335 + e.getMessage()); 1336 } catch (ClassNotFoundException e) { 1337 fail("ClassNotFoundException reading Object type : " 1338 + e.getMessage()); 1339 } catch (Error err) { 1340 System.out.println("Error when obj = " + objToSave); 1341 throw err; 1342 } 1343 } 1344 1345 @TestInfo( 1346 level = TestLevel.COMPLETE, 1347 purpose = "Verifies serialization.", 1348 targets = { 1349 @TestTarget( 1350 methodName = "!Serialization", 1351 methodArgs = {} 1352 ) 1353 }) 1354 public void test_18_26_writeObject() { 1355 // Test for method void 1356 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1357 1358 Object objToSave = null; 1359 Object objLoaded; 1360 1361 try { 1362 java.util.Hashtable hashTable = new java.util.Hashtable(5); 1363 hashTable.put(FOO, FOO); 1364 objToSave = hashTable; 1365 if (DEBUG) 1366 System.out.println("Obj = " + objToSave); 1367 objLoaded = dumpAndReload(objToSave); 1368 java.util.Hashtable loadedHashTable = (java.util.Hashtable) objLoaded; 1369 // Has to have the key/value there (FOO -> FOO) 1370 assertTrue(MSG_TEST_FAILED + objToSave, FOO.equals(loadedHashTable 1371 .get(FOO))); 1372 1373 } catch (IOException e) { 1374 fail("IOException serializing " + objToSave + " : " 1375 + e.getMessage()); 1376 } catch (ClassNotFoundException e) { 1377 fail("ClassNotFoundException reading Object type : " 1378 + e.getMessage()); 1379 } catch (Error err) { 1380 System.out.println("Error when obj = " + objToSave); 1381 throw err; 1382 } 1383 } 1384 1385 @TestInfo( 1386 level = TestLevel.COMPLETE, 1387 purpose = "Verifies serialization.", 1388 targets = { 1389 @TestTarget( 1390 methodName = "!Serialization", 1391 methodArgs = {} 1392 ) 1393 }) 1394 public void test_18_27_writeObject() { 1395 // Test for method void 1396 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1397 1398 Object objToSave = null; 1399 Object objLoaded; 1400 1401 try { 1402 ClassBasedReplacementWhenDumping classBasedReplacementWhenDumping = new ClassBasedReplacementWhenDumping(); 1403 objToSave = classBasedReplacementWhenDumping; 1404 if (DEBUG) 1405 System.out.println("Obj = " + objToSave); 1406 objLoaded = dumpAndReload(objToSave); 1407 // Has to have run the replacement method 1408 assertTrue("Did not run writeReplace", 1409 classBasedReplacementWhenDumping.calledReplacement); 1410 1411 // Has to have loaded a String (replacement object) 1412 assertTrue("Did not replace properly", FOO.equals(objLoaded)); 1413 1414 } catch (IOException e) { 1415 fail("IOException serializing " + objToSave + " : " 1416 + e.getMessage()); 1417 } catch (ClassNotFoundException e) { 1418 fail("ClassNotFoundException reading Object type : " 1419 + e.getMessage()); 1420 } catch (Error err) { 1421 System.out.println("Error when obj = " + objToSave); 1422 // err.printStackTrace(); 1423 throw err; 1424 } 1425 } 1426 1427 @TestInfo( 1428 level = TestLevel.COMPLETE, 1429 purpose = "Verifies serialization.", 1430 targets = { 1431 @TestTarget( 1432 methodName = "!Serialization", 1433 methodArgs = {} 1434 ) 1435 }) 1436 public void test_18_28_writeObject() { 1437 // Test for method void 1438 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1439 1440 Object objToSave = null; 1441 Object objLoaded; 1442 1443 try { 1444 MultipleClassBasedReplacementWhenDumping multipleClassBasedReplacementWhenDumping = new MultipleClassBasedReplacementWhenDumping(); 1445 objToSave = multipleClassBasedReplacementWhenDumping; 1446 if (DEBUG) 1447 System.out.println("Obj = " + objToSave); 1448 objLoaded = dumpAndReload(objToSave); 1449 // Has to have loaded a String (replacement object) 1450 assertTrue( 1451 "Executed multiple levels of replacement (see PR 1F9RNT1), loaded= " 1452 + objLoaded, 1453 objLoaded instanceof MultipleClassBasedReplacementWhenDumping.C1); 1454 1455 } catch (IOException e) { 1456 fail("IOException serializing " + objToSave + " : " 1457 + e.getMessage()); 1458 } catch (ClassNotFoundException e) { 1459 fail("ClassNotFoundException reading Object type : " 1460 + e.toString()); 1461 } catch (Error err) { 1462 System.out.println("Error when obj = " + objToSave); 1463 // err.printStackTrace(); 1464 throw err; 1465 } 1466 } 1467 1468 @TestInfo( 1469 level = TestLevel.COMPLETE, 1470 purpose = "Verifies serialization.", 1471 targets = { 1472 @TestTarget( 1473 methodName = "!Serialization", 1474 methodArgs = {} 1475 ) 1476 }) 1477 public void test_18_29_writeObject() { 1478 // Test for method void 1479 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1480 1481 Object objToSave = null; 1482 Object objLoaded; 1483 1484 try { 1485 ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading(); 1486 objToSave = classBasedReplacementWhenLoading; 1487 if (DEBUG) 1488 System.out.println("Obj = " + objToSave); 1489 objLoaded = dumpAndReload(objToSave); 1490 // Has to have loaded a String (replacement object) 1491 assertTrue("Did not run readResolve", FOO.equals(objLoaded)); 1492 1493 } catch (IOException e) { 1494 fail("IOException serializing " + objToSave + " : " 1495 + e.getMessage()); 1496 } catch (ClassNotFoundException e) { 1497 fail("ClassNotFoundException reading Object type : " 1498 + e.getMessage()); 1499 } catch (Error err) { 1500 System.out.println("Error when obj = " + objToSave); 1501 // err.printStackTrace(); 1502 throw err; 1503 } 1504 } 1505 1506 @TestInfo( 1507 level = TestLevel.COMPLETE, 1508 purpose = "Verifies serialization.", 1509 targets = { 1510 @TestTarget( 1511 methodName = "!Serialization", 1512 methodArgs = {} 1513 ) 1514 }) 1515 public void test_18_30_writeObject() { 1516 // Test for method void 1517 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1518 1519 Object objToSave = null; 1520 Object objLoaded; 1521 1522 try { 1523 ClassBasedReplacementWhenLoadingViolatesFieldType classBasedReplacementWhenLoadingViolatesFieldType = new ClassBasedReplacementWhenLoadingViolatesFieldType(); 1524 objToSave = classBasedReplacementWhenLoadingViolatesFieldType; 1525 if (DEBUG) 1526 System.out.println("Obj = " + objToSave); 1527 objLoaded = dumpAndReload(objToSave); 1528 // We cannot gere here, the load replacement must have caused a 1529 // field type violation 1530 fail( 1531 "Loading replacements can cause field type violation in this implementation"); 1532 1533 } catch (IOException e) { 1534 fail("IOException serializing " + objToSave + " : " 1535 + e.getMessage()); 1536 } catch (ClassNotFoundException e) { 1537 fail("ClassNotFoundException reading Object type : " 1538 + e.getMessage()); 1539 } catch (ClassCastException e) { 1540 assertTrue( 1541 "Loading replacements can NOT cause field type violation in this implementation", 1542 true); 1543 } catch (Error err) { 1544 System.out.println("Error when obj = " + objToSave); 1545 // err.printStackTrace(); 1546 throw err; 1547 } 1548 } 1549 1550 @TestInfo( 1551 level = TestLevel.COMPLETE, 1552 purpose = "Verifies serialization.", 1553 targets = { 1554 @TestTarget( 1555 methodName = "!Serialization", 1556 methodArgs = {} 1557 ) 1558 }) 1559 public void test_18_31_writeObject() { 1560 // Test for method void 1561 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1562 1563 Object objToSave = null; 1564 Object objLoaded; 1565 1566 try { 1567 MyExceptionWhenDumping1 exceptionWhenDumping = new MyExceptionWhenDumping1(); 1568 objToSave = exceptionWhenDumping; 1569 if (DEBUG) 1570 System.out.println("Obj = " + objToSave); 1571 boolean causedException = false; 1572 try { 1573 dump(objToSave); 1574 } catch (MyExceptionWhenDumping1.MyException e) { 1575 causedException = true; 1576 } 1577 ; 1578 assertTrue("Should have caused an exception when dumping", 1579 causedException); 1580 causedException = false; 1581 try { 1582 objLoaded = reload(); 1583 // Although the spec says we should get a WriteAbortedException, 1584 // the serialization format handle an Exception when reading 1585 // primitive data so we get ClassCastException instead 1586 } catch (ClassCastException e) { 1587 causedException = true; 1588 } 1589 ; 1590 assertTrue("Should have caused a ClassCastException when loading", 1591 causedException); 1592 } catch (IOException e) { 1593 fail("IOException serializing " + objToSave + " : " 1594 + e.getMessage()); 1595 } catch (ClassNotFoundException e) { 1596 fail("ClassNotFoundException reading Object type : " 1597 + e.getMessage()); 1598 } catch (Error err) { 1599 System.out.println("Error when obj = " + objToSave); 1600 // err.printStackTrace(); 1601 throw err; 1602 } 1603 } 1604 1605 @TestInfo( 1606 level = TestLevel.COMPLETE, 1607 purpose = "Verifies serialization.", 1608 targets = { 1609 @TestTarget( 1610 methodName = "!Serialization", 1611 methodArgs = {} 1612 ) 1613 }) 1614 public void test_18_32_writeObject() { 1615 // Test for method void 1616 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1617 1618 Object objToSave = null; 1619 Object objLoaded; 1620 1621 try { 1622 MyExceptionWhenDumping2 exceptionWhenDumping = new MyExceptionWhenDumping2(); 1623 objToSave = exceptionWhenDumping; 1624 if (DEBUG) 1625 System.out.println("Obj = " + objToSave); 1626 boolean causedException = false; 1627 try { 1628 dump(objToSave); 1629 } catch (MyExceptionWhenDumping2.MyException e) { 1630 causedException = true; 1631 } 1632 ; 1633 assertTrue("Should have caused an exception when dumping", 1634 causedException); 1635 causedException = false; 1636 try { 1637 objLoaded = reload(); 1638 } catch (java.io.WriteAbortedException e) { 1639 causedException = true; 1640 } 1641 ; 1642 assertTrue( 1643 "Should have caused a java.io.WriteAbortedException when loading", 1644 causedException); 1645 } catch (IOException e) { 1646 fail("IOException serializing " + objToSave + " : " 1647 + e.getMessage()); 1648 } catch (ClassNotFoundException e) { 1649 fail("ClassNotFoundException reading Object type : " 1650 + e.getMessage()); 1651 } catch (ClassCastException e) { 1652 fail("ClassCastException : " + e.getMessage()); 1653 } catch (Error err) { 1654 System.out.println("Error when obj = " + objToSave); 1655 throw err; 1656 } 1657 } 1658 1659 @TestInfo( 1660 level = TestLevel.COMPLETE, 1661 purpose = "Verifies serialization.", 1662 targets = { 1663 @TestTarget( 1664 methodName = "!Serialization", 1665 methodArgs = {} 1666 ) 1667 }) 1668 public void test_NonSerializableExceptionWhenDumping() { 1669 // Test for method void 1670 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1671 1672 Object objToSave = null; 1673 Object objLoaded; 1674 1675 try { 1676 NonSerializableExceptionWhenDumping nonSerializableExceptionWhenDumping = new NonSerializableExceptionWhenDumping(); 1677 objToSave = nonSerializableExceptionWhenDumping; 1678 if (DEBUG) 1679 System.out.println("Obj = " + objToSave); 1680 boolean causedException = false; 1681 try { 1682 dump(objToSave); 1683 } catch (java.io.NotSerializableException e) { 1684 causedException = true; 1685 } 1686 ; 1687 assertTrue("Should have caused an exception when dumping", 1688 causedException); 1689 causedException = false; 1690 try { 1691 objLoaded = reload(); 1692 } catch (java.io.WriteAbortedException e) { 1693 causedException = true; 1694 } 1695 ; 1696 assertTrue( 1697 "Should have caused a java.io.WriteAbortedException when loading", 1698 causedException); 1699 } catch (IOException e) { 1700 fail("IOException serializing " + objToSave + " : " 1701 + e.getMessage()); 1702 } catch (ClassNotFoundException e) { 1703 fail("ClassNotFoundException reading Object type : " 1704 + e.getMessage()); 1705 } catch (Error err) { 1706 System.out.println("Error when obj = " + objToSave); 1707 // err.printStackTrace(); 1708 throw err; 1709 } 1710 } 1711 1712 @TestInfo( 1713 level = TestLevel.COMPLETE, 1714 purpose = "Verifies serialization.", 1715 targets = { 1716 @TestTarget( 1717 methodName = "!Serialization", 1718 methodArgs = {} 1719 ) 1720 }) 1721 public void test_18_33_writeObject() { 1722 // Test for method void 1723 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1724 1725 Object objToSave = null; 1726 Object objLoaded; 1727 1728 try { 1729 MyUnserializableExceptionWhenDumping exceptionWhenDumping = new MyUnserializableExceptionWhenDumping(); 1730 objToSave = exceptionWhenDumping; 1731 if (DEBUG) 1732 System.out.println("Obj = " + objToSave); 1733 boolean causedException = false; 1734 try { 1735 dump(objToSave); 1736 } catch (java.io.StreamCorruptedException e) { 1737 causedException = true; 1738 } 1739 ; 1740 assertTrue("Should have caused an exception when dumping", 1741 causedException); 1742 // As the stream is corrupted, reading the stream will have 1743 // undefined results 1744 } catch (IOException e) { 1745 fail("IOException serializing " + objToSave + " : " 1746 + e.getMessage()); 1747 } catch (ClassNotFoundException e) { 1748 fail("ClassNotFoundException reading Object type : " 1749 + e.getMessage()); 1750 } catch (Error err) { 1751 System.out.println("Error when obj = " + objToSave); 1752 // err.printStackTrace(); 1753 throw err; 1754 } 1755 } 1756 1757 @TestInfo( 1758 level = TestLevel.COMPLETE, 1759 purpose = "Verifies serialization.", 1760 targets = { 1761 @TestTarget( 1762 methodName = "!Serialization", 1763 methodArgs = {} 1764 ) 1765 }) 1766 public void test_18_34_writeObject() { 1767 // Test for method void 1768 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1769 1770 Object objToSave = null; 1771 Object objLoaded; 1772 1773 try { 1774 java.io.IOException ioe = new java.io.IOException(); 1775 objToSave = ioe; 1776 if (DEBUG) 1777 System.out.println("Obj = " + objToSave); 1778 objLoaded = dumpAndReload(objToSave); 1779 // Has to be able to save/load an exception 1780 assertTrue(MSG_TEST_FAILED + objToSave, true); 1781 1782 } catch (IOException e) { 1783 fail("IOException serializing " + objToSave + " : " 1784 + e.getMessage()); 1785 } catch (ClassNotFoundException e) { 1786 fail("ClassNotFoundException reading Object type : " 1787 + e.getMessage()); 1788 } catch (Error err) { 1789 System.out.println("Error when obj = " + objToSave); 1790 // err.printStackTrace(); 1791 throw err; 1792 } 1793 } 1794 1795 @TestInfo( 1796 level = TestLevel.COMPLETE, 1797 purpose = "Verifies serialization.", 1798 targets = { 1799 @TestTarget( 1800 methodName = "!Serialization", 1801 methodArgs = {} 1802 ) 1803 }) 1804 public void test_18_35_writeObject() { 1805 // Test for method void 1806 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1807 1808 Object objToSave = null; 1809 Object objLoaded; 1810 1811 try { 1812 objToSave = Class.forName("java.util.Hashtable"); 1813 if (DEBUG) 1814 System.out.println("Obj = " + objToSave); 1815 objLoaded = dumpAndReload(objToSave); 1816 // Classes with the same name are unique, so test for == 1817 assertTrue(MSG_TEST_FAILED + objToSave, objLoaded == objToSave); 1818 1819 } catch (IOException e) { 1820 fail("IOException serializing " + objToSave + " : " 1821 + e.getMessage()); 1822 } catch (ClassNotFoundException e) { 1823 fail("ClassNotFoundException reading Object type : " 1824 + e.getMessage()); 1825 } catch (Error err) { 1826 System.out.println("Error when obj = " + objToSave); 1827 // err.printStackTrace(); 1828 throw err; 1829 } 1830 } 1831 1832 @TestInfo( 1833 level = TestLevel.COMPLETE, 1834 purpose = "Verifies serialization.", 1835 targets = { 1836 @TestTarget( 1837 methodName = "!Serialization", 1838 methodArgs = {} 1839 ) 1840 }) 1841 public void test_18_36_writeObject() { 1842 // Test for method void 1843 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1844 1845 Object objToSave = null; 1846 Object objLoaded; 1847 1848 try { 1849 java.io.IOException ex = new java.io.InvalidClassException(FOO); 1850 objToSave = ex; 1851 if (DEBUG) 1852 System.out.println("Obj = " + objToSave); 1853 objLoaded = dumpAndReload(objToSave); 1854 // Has to be able to save/load an exception 1855 assertTrue(MSG_TEST_FAILED + objToSave, true); 1856 1857 } catch (IOException e) { 1858 fail("IOException serializing " + objToSave + " : " 1859 + e.getMessage()); 1860 } catch (ClassNotFoundException e) { 1861 fail("ClassNotFoundException reading Object type : " 1862 + e.getMessage()); 1863 } catch (Error err) { 1864 System.out.println("Error when obj = " + objToSave); 1865 // err.printStackTrace(); 1866 throw err; 1867 } 1868 } 1869 1870 @TestInfo( 1871 level = TestLevel.COMPLETE, 1872 purpose = "Verifies serialization.", 1873 targets = { 1874 @TestTarget( 1875 methodName = "!Serialization", 1876 methodArgs = {} 1877 ) 1878 }) 1879 public void test_18_37_writeObject() { 1880 // Test for method void 1881 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1882 1883 Object objToSave = null; 1884 Object objLoaded; 1885 1886 try { 1887 java.io.IOException ex = new java.io.InvalidObjectException(FOO); 1888 objToSave = ex; 1889 if (DEBUG) 1890 System.out.println("Obj = " + objToSave); 1891 objLoaded = dumpAndReload(objToSave); 1892 // Has to be able to save/load an exception 1893 assertTrue(MSG_TEST_FAILED + objToSave, true); 1894 1895 } catch (IOException e) { 1896 fail("IOException serializing " + objToSave + " : " 1897 + e.getMessage()); 1898 } catch (ClassNotFoundException e) { 1899 fail("ClassNotFoundException reading Object type : " 1900 + e.getMessage()); 1901 } catch (Error err) { 1902 System.out.println("Error when obj = " + objToSave); 1903 // err.printStackTrace(); 1904 throw err; 1905 } 1906 } 1907 1908 @TestInfo( 1909 level = TestLevel.COMPLETE, 1910 purpose = "Verifies serialization.", 1911 targets = { 1912 @TestTarget( 1913 methodName = "!Serialization", 1914 methodArgs = {} 1915 ) 1916 }) 1917 public void test_18_38_writeObject() { 1918 // Test for method void 1919 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1920 1921 Object objToSave = null; 1922 Object objLoaded; 1923 1924 try { 1925 java.io.IOException ex = new java.io.NotActiveException(FOO); 1926 objToSave = ex; 1927 if (DEBUG) 1928 System.out.println("Obj = " + objToSave); 1929 objLoaded = dumpAndReload(objToSave); 1930 // Has to be able to save/load an exception 1931 assertTrue(MSG_TEST_FAILED + objToSave, true); 1932 1933 } catch (IOException e) { 1934 fail("IOException serializing " + objToSave + " : " 1935 + e.getMessage()); 1936 } catch (ClassNotFoundException e) { 1937 fail("ClassNotFoundException reading Object type : " 1938 + e.getMessage()); 1939 } catch (Error err) { 1940 System.out.println("Error when obj = " + objToSave); 1941 // err.printStackTrace(); 1942 throw err; 1943 } 1944 } 1945 1946 @TestInfo( 1947 level = TestLevel.COMPLETE, 1948 purpose = "Verifies serialization.", 1949 targets = { 1950 @TestTarget( 1951 methodName = "!Serialization", 1952 methodArgs = {} 1953 ) 1954 }) 1955 public void test_18_39_writeObject() { 1956 // Test for method void 1957 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1958 1959 Object objToSave = null; 1960 Object objLoaded; 1961 1962 try { 1963 java.io.IOException ex = new java.io.NotSerializableException(FOO); 1964 objToSave = ex; 1965 if (DEBUG) 1966 System.out.println("Obj = " + objToSave); 1967 objLoaded = dumpAndReload(objToSave); 1968 // Has to be able to save/load an exception 1969 assertTrue(MSG_TEST_FAILED + objToSave, true); 1970 1971 } catch (IOException e) { 1972 fail("IOException serializing " + objToSave + " : " 1973 + e.getMessage()); 1974 } catch (ClassNotFoundException e) { 1975 fail("ClassNotFoundException reading Object type : " 1976 + e.getMessage()); 1977 } catch (Error err) { 1978 System.out.println("Error when obj = " + objToSave); 1979 // err.printStackTrace(); 1980 throw err; 1981 } 1982 } 1983 1984 @TestInfo( 1985 level = TestLevel.COMPLETE, 1986 purpose = "Verifies serialization.", 1987 targets = { 1988 @TestTarget( 1989 methodName = "!Serialization", 1990 methodArgs = {} 1991 ) 1992 }) 1993 public void test_18_40_writeObject() { 1994 // Test for method void 1995 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1996 1997 Object objToSave = null; 1998 Object objLoaded; 1999 2000 try { 2001 java.io.IOException ex = new java.io.StreamCorruptedException(FOO); 2002 objToSave = ex; 2003 if (DEBUG) 2004 System.out.println("Obj = " + objToSave); 2005 objLoaded = dumpAndReload(objToSave); 2006 // Has to be able to save/load an exception 2007 assertTrue(MSG_TEST_FAILED + objToSave, true); 2008 2009 } catch (IOException e) { 2010 fail("IOException serializing " + objToSave + " : " 2011 + e.getMessage()); 2012 } catch (ClassNotFoundException e) { 2013 fail("ClassNotFoundException reading Object type : " 2014 + e.getMessage()); 2015 } catch (Error err) { 2016 System.out.println("Error when obj = " + objToSave); 2017 // err.printStackTrace(); 2018 throw err; 2019 } 2020 } 2021} 2022