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