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 */ 17 18package org.apache.harmony.luni.tests.java.io; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.DataOutputStream; 23import java.io.IOException; 24import java.io.InvalidClassException; 25import java.io.NotActiveException; 26import java.io.ObjectInputStream; 27import java.io.ObjectOutputStream; 28import java.io.ObjectStreamClass; 29import java.io.ObjectStreamConstants; 30import java.io.ObjectStreamField; 31import java.io.OptionalDataException; 32import java.io.Serializable; 33import java.util.ArrayList; 34import java.util.Arrays; 35import java.util.Date; 36import java.util.Locale; 37 38 39@SuppressWarnings( { "serial", "unused" }) 40public class SerializationStressTest2 extends SerializationStressTest { 41 42 private static class ReadWriteObjectAndPrimitiveData implements 43 java.io.Serializable { 44 transient long milliseconds; 45 46 public boolean calledWriteObject = false; 47 48 public boolean calledReadObject = false; 49 50 public ReadWriteObjectAndPrimitiveData() { 51 super(); 52 } 53 54 private void readObject(java.io.ObjectInputStream in) 55 throws java.io.IOException, ClassNotFoundException { 56 in.defaultReadObject(); 57 // This *has* to come after the call to defaultReadObject or the 58 // value from the stream will override 59 calledReadObject = true; 60 milliseconds = in.readLong(); 61 } 62 63 private void writeObject(java.io.ObjectOutputStream out) 64 throws java.io.IOException { 65 calledWriteObject = true; 66 out.defaultWriteObject(); 67 out.writeLong(milliseconds); 68 } 69 } 70 71 // What happens if a class defines serialPersistentFields that do not match 72 // real fields but does not override read/writeObject 73 private static class WithUnmatchingSerialPersistentFields implements 74 java.io.Serializable { 75 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 76 "value", String.class) }; 77 78 public int anInstanceVar = 5; 79 80 public WithUnmatchingSerialPersistentFields() { 81 super(); 82 } 83 } 84 85 // What happens if a class defines serialPersistentFields which match actual 86 // fields 87 private static class WithMatchingSerialPersistentFields implements 88 java.io.Serializable { 89 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 90 "anInstanceVar", String.class) }; 91 92 public String anInstanceVar = FOO + FOO; 93 94 public WithMatchingSerialPersistentFields() { 95 super(); 96 } 97 } 98 99 // Tests the oficial behavior for serialPersistentFields 100 private static class SerialPersistentFields implements java.io.Serializable { 101 private static final String SIMULATED_FIELD_NAME = "text"; 102 103 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 104 SIMULATED_FIELD_NAME, String.class) }; 105 106 public int anInstanceVar = 5; 107 108 public SerialPersistentFields() { 109 super(); 110 } 111 112 private void readObject(java.io.ObjectInputStream in) 113 throws java.io.IOException, ClassNotFoundException { 114 ObjectInputStream.GetField fields = in.readFields(); 115 anInstanceVar = Integer.parseInt((String) fields.get( 116 SIMULATED_FIELD_NAME, "-5")); 117 } 118 119 private void writeObject(java.io.ObjectOutputStream out) 120 throws java.io.IOException, ClassNotFoundException { 121 ObjectOutputStream.PutField fields = out.putFields(); 122 fields.put(SIMULATED_FIELD_NAME, Integer.toString(anInstanceVar)); 123 out.writeFields(); 124 } 125 } 126 127 // Tests the behavior for serialPersistentFields when no fields are actually 128 // set 129 private static class WriteFieldsWithoutFetchingPutFields implements 130 java.io.Serializable { 131 private static final String SIMULATED_FIELD_NAME = "text"; 132 133 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 134 SIMULATED_FIELD_NAME, String.class) }; 135 136 public int anInstanceVar = 5; 137 138 public WriteFieldsWithoutFetchingPutFields() { 139 super(); 140 } 141 142 private void readObject(java.io.ObjectInputStream in) 143 throws java.io.IOException, ClassNotFoundException { 144 ObjectInputStream.GetField fields = in.readFields(); 145 } 146 147 private void writeObject(java.io.ObjectOutputStream out) 148 throws java.io.IOException, ClassNotFoundException { 149 out.writeFields(); 150 } 151 } 152 153 // Tests what happens if one asks for PutField/getField when the class does 154 // not declare one 155 private static class SerialPersistentFieldsWithoutField implements 156 java.io.Serializable { 157 public int anInstanceVar = 5; 158 159 public SerialPersistentFieldsWithoutField() { 160 super(); 161 } 162 163 private void readObject(java.io.ObjectInputStream in) 164 throws java.io.IOException, ClassNotFoundException { 165 ObjectInputStream.GetField fields = in.readFields(); 166 } 167 168 private void writeObject(java.io.ObjectOutputStream out) 169 throws java.io.IOException, ClassNotFoundException { 170 ObjectOutputStream.PutField fields = out.putFields(); 171 out.writeFields(); 172 } 173 } 174 175 // ----------------------------------------------------------------------------------- 176 177 // writeObject writes extra primitive types and objects which readObject 178 // does not consume. Have to make sure we can load object properly AND 179 // object after it (to show the extra byte[] is consumed) 180 private static class OptionalDataNotRead implements java.io.Serializable { 181 private int field1, field2; 182 183 public OptionalDataNotRead() { 184 } 185 186 private static final ObjectStreamField[] serialPersistentFields = { 187 new ObjectStreamField("field1", Integer.TYPE), 188 new ObjectStreamField("field2", Integer.TYPE), 189 new ObjectStreamField("monthLength", byte[].class), }; 190 191 private void writeObject(ObjectOutputStream stream) throws IOException { 192 ObjectOutputStream.PutField fields = stream.putFields(); 193 fields.put("field1", 1); 194 fields.put("field2", 2); 195 fields.put("monthLength", new byte[] { 7, 8, 9 }); 196 stream.writeFields(); 197 stream.writeInt(4); 198 byte[] values = new byte[4]; 199 values[0] = (byte) 16; 200 values[1] = (byte) 17; 201 values[2] = (byte) 18; 202 values[3] = (byte) 19; 203 stream.writeObject(values); 204 } 205 206 private void readObject(ObjectInputStream stream) throws IOException, 207 ClassNotFoundException { 208 ObjectInputStream.GetField fields = stream.readFields(); 209 field1 = fields.get("field1", 0); 210 field2 = fields.get("field1", 0); 211 } 212 } 213 214 // ----------------------------------------------------------------------------------- 215 private static class NestedPutField implements java.io.Serializable { 216 public OptionalDataNotRead field1; 217 218 public NestedPutField() { 219 } 220 221 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 222 "field1", OptionalDataNotRead.class), }; 223 224 private void writeObject(ObjectOutputStream stream) throws IOException { 225 ObjectOutputStream.PutField fields = stream.putFields(); 226 fields.put("field1", new OptionalDataNotRead()); 227 stream.writeFields(); 228 } 229 230 private void readObject(ObjectInputStream stream) throws IOException, 231 ClassNotFoundException { 232 ObjectInputStream.GetField fields = stream.readFields(); 233 field1 = (OptionalDataNotRead) fields.get("field1", null); 234 } 235 } 236 237 // ----------------------------------------------------------------------------------- 238 239 // This one tests stream-based replacement when dumping 240 private static class StreamBasedReplacementWhenDumping extends 241 java.io.ObjectOutputStream { 242 public boolean calledArrayReplacement = false; 243 244 public boolean calledStringReplacement = false; 245 246 public boolean calledClassReplacement = false; 247 248 public boolean calledObjectStreamClassReplacement = false; 249 250 public StreamBasedReplacementWhenDumping(java.io.OutputStream output) 251 throws java.io.IOException { 252 super(output); 253 enableReplaceObject(true); 254 } 255 256 protected Object replaceObject(Object obj) throws IOException { 257 Class objClass = obj.getClass(); 258 if (objClass == String.class) 259 calledStringReplacement = true; 260 261 if (objClass == Class.class) 262 calledClassReplacement = true; 263 264 if (objClass == ObjectStreamClass.class) 265 calledObjectStreamClassReplacement = true; 266 267 if (objClass.isArray()) 268 calledArrayReplacement = true; 269 270 return obj; 271 } 272 } 273 274 // ----------------------------------------------------------------------------------- 275 276 private static class ArrayOfSerializable implements Serializable { 277 private Serializable[] testField = null; 278 279 public ArrayOfSerializable() { 280 testField = new Serializable[2]; 281 testField[0] = "Hi"; 282 testField[1] = "there!"; 283 } 284 } 285 286 // ----------------------------------------------------------------------------------- 287 288 private static class ClassSubClassTest0 extends java.lang.Object implements 289 java.io.Serializable { 290 String stringVar; 291 292 public ClassSubClassTest0(String init) { 293 stringVar = init; 294 } 295 } 296 297 private static class ClassSubClassTest1 extends ClassSubClassTest0 { 298 String subStringVar; 299 300 public ClassSubClassTest1(String superString, String subString) { 301 super(superString); 302 subStringVar = subString; 303 } 304 305 public boolean equals(Object obj) { 306 if (obj == null) 307 return false; 308 if (!(obj instanceof ClassSubClassTest1)) 309 return false; 310 311 ClassSubClassTest1 inst = (ClassSubClassTest1) obj; 312 return inst.subStringVar.equals(this.subStringVar) 313 && inst.stringVar.equals(this.stringVar); 314 } 315 } 316 317 // ----------------------------------------------------------------------------------- 318 private static class ConstructorTestA { 319 public String instVar_classA; 320 321 public final static String ConstrA = "Init in Constructor Class A"; 322 323 public final static String ConstrB = "Init in Constructor Class B"; 324 325 public final static String ConstrC = "Init in Constructor Class C"; 326 327 public final static String ChangedC = "Changed before Serialize - Class C"; 328 329 public ConstructorTestA() { 330 instVar_classA = ConstrA; 331 } 332 } 333 334 private static class ConstructorTestB extends ConstructorTestA implements 335 java.io.Serializable { 336 public String instVar_classB; 337 338 public ConstructorTestB() { 339 instVar_classA = ConstrB; 340 instVar_classB = ConstrB; 341 } 342 } 343 344 private static class ConstructorTestC extends ConstructorTestB { 345 public String instVar_classC; 346 347 public ConstructorTestC() { 348 instVar_classA = ConstrC; 349 instVar_classB = ConstrC; 350 instVar_classC = ConstrC; 351 } 352 353 public boolean verify(Object obj) { 354 if (obj == null) 355 return false; 356 if (!(obj instanceof ConstructorTestC)) 357 return false; 358 359 ConstructorTestC inst = (ConstructorTestC) obj; 360 return inst.instVar_classC.equals(this.instVar_classC) 361 && inst.instVar_classB.equals(this.instVar_classB) 362 && inst.instVar_classA.equals(ConstrA); 363 } 364 } 365 366 // ----------------------------------------------------------------------------------- 367 private static class HashCodeTest implements java.io.Serializable { 368 private boolean serializationUsesHashCode = false; 369 370 public int hashCode() { 371 serializationUsesHashCode = true; 372 return super.hashCode(); 373 } 374 } 375 376 // ----------------------------------------------------------------------------------- 377 private static class InitializerFieldsTest implements java.io.Serializable { 378 public java.lang.String toBeSerialized; 379 380 public static java.lang.String toBeNotSerialized; 381 382 public static java.lang.String toBeNotSerialized2; 383 384 { 385 toBeSerialized = "NonStaticInitialValue"; 386 } 387 388 static { 389 toBeNotSerialized = "StaticInitialValue"; 390 toBeNotSerialized2 = new String(toBeNotSerialized); 391 } 392 393 public boolean equals(Object obj) { 394 /* 395 * This method is not answering it the objs is equal. It is 396 * answering if the vars have the value that it have to have after 397 * dumping and loading 398 */ 399 400 if (obj == null) 401 return false; 402 if (!(obj instanceof InitializerFieldsTest)) 403 return false; 404 405 InitializerFieldsTest inst = (InitializerFieldsTest) obj; 406 return inst.toBeSerialized.equals(this.toBeSerialized) 407 && InitializerFieldsTest.toBeNotSerialized.equals(toBeNotSerialized2); 408 } 409 } 410 411 private static class InitializerFieldsTest2 implements java.io.Serializable { 412 public java.lang.String toBeSerialized; 413 414 public static java.lang.String toBeNotSerialized; 415 416 public static java.lang.String toBeNotSerialized2; 417 418 { 419 toBeSerialized = "NonStaticInitialValue"; 420 } 421 422 public java.lang.String toBeSerialized3; 423 424 public java.lang.String toBeSerialized4; 425 static { 426 toBeNotSerialized = "StaticInitialValue"; 427 toBeNotSerialized2 = new String(toBeNotSerialized); 428 } 429 430 public java.lang.String toBeSerialized5; 431 432 public boolean equals(Object obj) { 433 /* 434 * This method is not answering it the objs is equal. It is 435 * answering if the vars have the value that it have to have after 436 * dumping and loading 437 */ 438 439 if (obj == null) 440 return false; 441 if (!(obj instanceof InitializerFieldsTest2)) 442 return false; 443 444 InitializerFieldsTest2 inst = (InitializerFieldsTest2) obj; 445 return inst.toBeSerialized.equals(this.toBeSerialized) 446 && inst.toBeSerialized3.equals(this.toBeSerialized3) 447 && inst.toBeSerialized4.equals(this.toBeSerialized4) 448 && inst.toBeSerialized5.equals(this.toBeSerialized5) 449 && InitializerFieldsTest2.toBeNotSerialized.equals(toBeNotSerialized2); 450 } 451 } 452 453 private static class InitializerFieldsTest3 extends InitializerFieldsTest2 454 implements java.io.Serializable { 455 public java.lang.String sub_toBeSerialized; 456 457 public static java.lang.String sub_toBeNotSerialized; 458 459 public static java.lang.String sub_toBeNotSerialized2; 460 461 { 462 sub_toBeSerialized = "NonStaticInitialValue"; 463 } 464 465 public java.lang.String sub_toBeSerialized3; 466 467 public java.lang.String sub_toBeSerialized4; 468 static { 469 sub_toBeNotSerialized = "StaticInitialValue"; 470 sub_toBeNotSerialized2 = new String(sub_toBeNotSerialized); 471 } 472 473 public java.lang.String sub_toBeSerialized5; 474 475 public boolean equals(Object obj) { 476 /* 477 * This method is not answering it the objs is equal. It is 478 * answering if the vars have the value that it have to have after 479 * dumping and loading 480 */ 481 482 if (!super.equals(obj)) 483 return false; 484 if (!(obj instanceof InitializerFieldsTest3)) 485 return false; 486 487 InitializerFieldsTest3 inst = (InitializerFieldsTest3) obj; 488 return inst.sub_toBeSerialized.equals(this.sub_toBeSerialized) 489 && inst.sub_toBeSerialized3 490 .equals(this.sub_toBeSerialized3) 491 && inst.sub_toBeSerialized4 492 .equals(this.sub_toBeSerialized4) 493 && inst.sub_toBeSerialized5 494 .equals(this.sub_toBeSerialized5) 495 && InitializerFieldsTest3.sub_toBeNotSerialized 496 .equals(sub_toBeNotSerialized2); 497 } 498 } 499 500 // ----------------------------------------------------------------------------------- 501 private static class DeepNesting implements java.io.Serializable { 502 public float id; 503 504 public DeepNesting next; 505 506 public boolean dump; 507 508 public boolean load; 509 510 public DeepNesting(float id) { 511 this.id = id; 512 next = null; 513 dump = false; 514 load = false; 515 } 516 517 public DeepNesting(int howMany) { 518 DeepNesting prev = new DeepNesting(0.0F); 519 next(prev); 520 for (int i = 1; i < howMany; i++) { 521 prev = prev.next(new DeepNesting(i * 1.0F)); 522 } 523 } 524 525 public boolean equals(Object obj) { 526 if (obj == null) 527 return false; 528 if (!(obj instanceof DeepNesting)) 529 return false; 530 531 DeepNesting inst = (DeepNesting) obj; 532 if (inst.dump != this.dump || inst.load != this.load) 533 return false; 534 535 if (inst.next == null || this.next == null) 536 return inst.next == this.next; // both null 537 return this.next.equals(inst.next); 538 } 539 540 public DeepNesting next(DeepNesting ivt) { 541 next = ivt; 542 return ivt; 543 } 544 } 545 546 // ----------------------------------------------------------------------------------- 547 private static class DeepNestingWithWriteObject implements 548 java.io.Serializable { 549 public float id; 550 551 public DeepNestingWithWriteObject next; 552 553 public boolean dump; 554 555 public boolean load; 556 557 public DeepNestingWithWriteObject(float id) { 558 this.id = id; 559 next = null; 560 dump = false; 561 load = false; 562 } 563 564 public DeepNestingWithWriteObject(int howMany) { 565 DeepNestingWithWriteObject prev = new DeepNestingWithWriteObject( 566 0.0F); 567 next(prev); 568 for (int i = 1; i < howMany; i++) { 569 prev = prev.next(new DeepNestingWithWriteObject(i * 1.0F)); 570 } 571 } 572 573 public boolean equals(Object obj) { 574 if (obj == null) 575 return false; 576 if (!(obj instanceof DeepNestingWithWriteObject)) 577 return false; 578 579 DeepNestingWithWriteObject inst = (DeepNestingWithWriteObject) obj; 580 if (inst.dump != this.dump || inst.load != this.load) 581 return false; 582 583 if (inst.next == null || this.next == null) 584 return inst.next == this.next; // both null; 585 return this.next.equals(inst.next); 586 } 587 588 public DeepNestingWithWriteObject next(DeepNestingWithWriteObject ivt) { 589 next = ivt; 590 return ivt; 591 } 592 593 private void writeObject(java.io.ObjectOutputStream s) 594 throws IOException { 595 s.defaultWriteObject(); 596 } 597 598 private void readObject(java.io.ObjectInputStream s) 599 throws IOException, ClassNotFoundException { 600 s.defaultReadObject(); 601 } 602 } 603 604 // ----------------------------------------------------------------------------------- 605 static class NonPublicClassTest extends java.lang.Object implements 606 java.io.Serializable { 607 int field = 1; 608 609 public NonPublicClassTest() { 610 field = 10; 611 } 612 613 public boolean equals(Object o) { 614 if (o instanceof NonPublicClassTest) 615 return field == ((NonPublicClassTest) o).field; 616 return false; 617 } 618 619 public void x10() { 620 field *= 10; 621 } 622 } 623 624 // ----------------------------------------------------------------------------------- 625 private static class SameInstVarNameSuperClass { 626 private int foo; 627 628 public SameInstVarNameSuperClass() { 629 super(); 630 } 631 632 public SameInstVarNameSuperClass(int fooValue) { 633 foo = fooValue; 634 } 635 636 public String toString() { 637 return "foo = " + foo; 638 } 639 } 640 641 private static class SameInstVarNameSubClass extends 642 SameInstVarNameSuperClass implements java.io.Serializable { 643 protected int foo; 644 645 public SameInstVarNameSubClass() { 646 super(); 647 } 648 649 public SameInstVarNameSubClass(int fooValue) { 650 super(-fooValue); 651 foo = fooValue; 652 } 653 } 654 655 // ----------------------------------------------------------------------------------- 656 private static class SInterfaceTest implements java.io.Serializable { 657 public static int staticVar = 5; 658 659 public transient int[] transVar = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 660 661 public int instanceVar = 7; 662 663 public boolean equals(Object obj) { 664 if (obj == null) 665 return false; 666 if (!(obj instanceof SInterfaceTest)) 667 return false; 668 669 SInterfaceTest inst = (SInterfaceTest) obj; 670 if (this.instanceVar != inst.instanceVar) 671 return false; 672 if (inst.transVar == null || this.transVar == null) 673 return inst.transVar == this.transVar; // both null 674 for (int i = 0; i < transVar.length; i++) 675 if (inst.transVar[i] != this.transVar[i]) 676 return false; 677 return true; 678 } 679 680 private void readObject(java.io.ObjectInputStream s) 681 throws IOException, ClassNotFoundException { 682 Object arr; 683 s.defaultReadObject(); 684 arr = s.readObject(); 685 transVar = (int[]) arr; 686 } 687 688 private void writeObject(java.io.ObjectOutputStream s) 689 throws IOException { 690 s.defaultWriteObject(); 691 s.writeObject(transVar); 692 } 693 694 public void x10() { 695 for (int i = 0; i < transVar.length; i++) 696 transVar[i] = transVar[i] * 10; 697 instanceVar = instanceVar * 10; 698 } 699 } 700 701 // ----------------------------------------------------------------------------------- 702 private static class SInterfaceTest2 extends SInterfaceTest { 703 private void readObject(java.io.ObjectInputStream s) 704 throws IOException, ClassNotFoundException { 705 Object arr; 706 instanceVar = s.readInt(); 707 arr = s.readObject(); 708 transVar = (int[]) arr; 709 } 710 711 private void writeObject(java.io.ObjectOutputStream s) 712 throws IOException { 713 s.writeInt(instanceVar); 714 s.writeObject(transVar); 715 } 716 } 717 718 // ----------------------------------------------------------------------------------- 719 private static class SuperclassTest extends java.lang.Object implements 720 java.io.Serializable { 721 int superfield = 1; 722 723 public SuperclassTest() { 724 superfield = 10; 725 } 726 727 public boolean equals(Object o) { 728 if (o.getClass() == this.getClass()) 729 return superfield == ((SuperclassTest) o).superfield; 730 return false; 731 } 732 733 private void readObject(java.io.ObjectInputStream s) 734 throws IOException, ClassNotFoundException { 735 superfield = s.readInt(); 736 } 737 738 private void writeObject(java.io.ObjectOutputStream s) 739 throws IOException { 740 s.writeInt(superfield); 741 } 742 743 public void x10() { 744 superfield *= 10; 745 } 746 } 747 748 // ----------------------------------------------------------------------------------- 749 private static class SuperclassTest2 extends SuperclassTest { 750 int subfield = 5; 751 752 public SuperclassTest2() { 753 subfield = 50; 754 } 755 756 public boolean equals(Object o) { 757 if (o instanceof SuperclassTest2) 758 if (subfield == ((SuperclassTest2) o).subfield) 759 return super.equals(o); 760 return false; 761 } 762 763 private void readObject(java.io.ObjectInputStream s) 764 throws IOException, ClassNotFoundException { 765 subfield = s.readInt(); 766 } 767 768 private void writeObject(java.io.ObjectOutputStream s) 769 throws IOException { 770 s.writeInt(subfield); 771 } 772 773 public void x10() { 774 subfield *= 10; 775 super.x10(); 776 } 777 } 778 779 // ----------------------------------------------------------------------------------- 780 private static class SyntheticFieldTest implements java.io.Serializable { 781 public boolean equals(Object obj) { 782 /* 783 * This method is not answering it the objs is equal. It is 784 * answering if the vars have the value that it have to have after 785 * dumping and loading 786 */ 787 if (obj == null) 788 return false; 789 return obj instanceof SyntheticFieldTest; 790 } 791 792 public int hashCode() { 793 // Insert code to generate a hash code for the receiver here. 794 // This implementation forwards the message to super. You may 795 // replace or supplement this. 796 // NOTE: if two objects are equal (equals Object) returns true) they 797 // must have the same hash code 798 Class[] c = { String.class }; // *** synthetic field 799 return super.hashCode(); 800 } 801 } 802 803 public SerializationStressTest2(String name) { 804 super(name); 805 } 806 807 public void test_18_41_writeObject() { 808 // Test for method void 809 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 810 811 Object objToSave = null; 812 Object objLoaded; 813 814 try { 815 java.io.IOException ex = new java.io.WriteAbortedException(FOO, 816 null); 817 objToSave = ex; 818 if (DEBUG) 819 System.out.println("Obj = " + objToSave); 820 objLoaded = dumpAndReload(objToSave); 821 // Has to be able to save/load an exception 822 assertTrue(MSG_TEST_FAILED + objToSave, true); 823 824 } catch (IOException e) { 825 fail("IOException serializing " + objToSave + " : " 826 + e.getMessage()); 827 } catch (ClassNotFoundException e) { 828 fail("ClassNotFoundException reading Object type : " 829 + e.getMessage()); 830 } catch (Error err) { 831 System.out.println("Error when obj = " + objToSave); 832 // err.printStackTrace(); 833 throw err; 834 } 835 } 836 837 public void test_18_42_writeObject() { 838 // Test for method void 839 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 840 841 Object objToSave = null; 842 Object objLoaded; 843 844 try { 845 WithUnmatchingSerialPersistentFields spf = new WithUnmatchingSerialPersistentFields(); 846 objToSave = spf; 847 if (DEBUG) 848 System.out.println("Obj = " + objToSave); 849 boolean causedException = false; 850 try { 851 objLoaded = dumpAndReload(objToSave); 852 } catch (InvalidClassException ce) { 853 causedException = true; 854 } 855 assertTrue("serialPersistentFields do not match real fields", 856 causedException); 857 858 } catch (IOException e) { 859 fail("IOException serializing " + objToSave + " : " 860 + e.getMessage()); 861 } catch (ClassNotFoundException e) { 862 fail("ClassNotFoundException reading Object type : " 863 + e.getMessage()); 864 } catch (Error err) { 865 System.out.println("Error when obj = " + objToSave); 866 // err.printStackTrace(); 867 throw err; 868 } 869 } 870 871 public void test_18_43_writeObject() { 872 // Test for method void 873 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 874 875 Object objToSave = null; 876 Object objLoaded; 877 878 try { 879 WithMatchingSerialPersistentFields spf = new WithMatchingSerialPersistentFields(); 880 spf.anInstanceVar = FOO; 881 objToSave = spf; 882 if (DEBUG) 883 System.out.println("Obj = " + objToSave); 884 objLoaded = dumpAndReload(objToSave); 885 assertTrue( 886 "serialPersistentFields do not work properly in this implementation", 887 FOO 888 .equals(((WithMatchingSerialPersistentFields) objLoaded).anInstanceVar)); 889 890 } catch (IOException e) { 891 fail("IOException serializing " + objToSave + " : " 892 + e.getMessage()); 893 } catch (ClassNotFoundException e) { 894 fail("ClassNotFoundException reading Object type : " 895 + e.getMessage()); 896 } catch (Error err) { 897 System.out.println("Error when obj = " + objToSave); 898 // err.printStackTrace(); 899 throw err; 900 } 901 } 902 903 public void test_18_44_writeObject() { 904 // Test for method void 905 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 906 907 Object objToSave = null; 908 Object objLoaded; 909 910 try { 911 SerialPersistentFields spf = new SerialPersistentFields(); 912 final int CONST = -500; 913 spf.anInstanceVar = CONST; 914 objToSave = spf; 915 if (DEBUG) 916 System.out.println("Obj = " + objToSave); 917 objLoaded = dumpAndReload(objToSave); 918 assertTrue( 919 "serialPersistentFields do not work properly in this implementation", 920 ((SerialPersistentFields) objLoaded).anInstanceVar == CONST); 921 922 } catch (IOException e) { 923 fail("IOException serializing " + objToSave + " : " 924 + e.getMessage()); 925 } catch (ClassNotFoundException e) { 926 fail("ClassNotFoundException reading Object type : " 927 + e.getMessage()); 928 } catch (Error err) { 929 System.out.println("Error when obj = " + objToSave); 930 // err.printStackTrace(); 931 throw err; 932 } 933 } 934 935 public void test_18_45_writeObject() { 936 // Test for method void 937 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 938 939 Object objToSave = null; 940 Object objLoaded; 941 942 try { 943 WriteFieldsWithoutFetchingPutFields spf = new WriteFieldsWithoutFetchingPutFields(); 944 objToSave = spf; 945 if (DEBUG) 946 System.out.println("Obj = " + objToSave); 947 boolean causedException = false; 948 try { 949 objLoaded = dumpAndReload(objToSave); 950 } catch (NotActiveException ce) { 951 causedException = true; 952 } 953 assertTrue("WriteFieldsWithoutFetchingPutFields", causedException); 954 955 } catch (IOException e) { 956 fail("IOException serializing " + objToSave + " : " 957 + e.getMessage()); 958 } catch (ClassNotFoundException e) { 959 fail("ClassNotFoundException reading Object type : " 960 + e.getMessage()); 961 } catch (Error err) { 962 System.out.println("Error when obj = " + objToSave); 963 // err.printStackTrace(); 964 throw err; 965 } 966 } 967 968 public void test_18_46_writeObject() { 969 // Test for method void 970 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 971 972 Object objToSave = null; 973 Object objLoaded; 974 975 try { 976 objToSave = SerialPersistentFields.class; // Test for 1FA7TA6 977 if (DEBUG) 978 System.out.println("Obj = " + objToSave); 979 objLoaded = dumpAndReload(objToSave); 980 // Has to be able to save/load an exception 981 assertTrue(MSG_TEST_FAILED + objToSave, true); 982 983 } catch (IOException e) { 984 fail("IOException serializing " + objToSave + " : " 985 + e.getMessage()); 986 } catch (ClassNotFoundException e) { 987 fail("ClassNotFoundException reading Object type : " 988 + e.getMessage()); 989 } catch (Error err) { 990 System.out.println("Error when obj = " + objToSave); 991 // err.printStackTrace(); 992 throw err; 993 } 994 } 995 996 public void test_18_47_writeObject() { 997 // Test for method void 998 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 999 1000 Object objToSave = null; 1001 Object objLoaded; 1002 1003 try { 1004 objToSave = ObjectStreamClass.lookup(SerialPersistentFields.class); // Test 1005 // for 1006 // 1FA7TA6 1007 if (DEBUG) 1008 System.out.println("Obj = " + objToSave); 1009 objLoaded = dumpAndReload(objToSave); 1010 // Has to be able to save/load an exception 1011 assertTrue(MSG_TEST_FAILED + objToSave, true); 1012 1013 } catch (IOException e) { 1014 fail("IOException serializing " + objToSave + " : " 1015 + e.getMessage()); 1016 } catch (ClassNotFoundException e) { 1017 fail("ClassNotFoundException reading Object type : " 1018 + e.getMessage()); 1019 } catch (Error err) { 1020 System.out.println("Error when obj = " + objToSave); 1021 // err.printStackTrace(); 1022 throw err; 1023 } 1024 } 1025 1026 public void test_18_48_writeObject() { 1027 // Test for method void 1028 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1029 1030 Object objToSave = null; 1031 Object objLoaded; 1032 1033 try { 1034 SerialPersistentFieldsWithoutField spf = new SerialPersistentFieldsWithoutField(); 1035 final int CONST = -500; 1036 spf.anInstanceVar = CONST; 1037 objToSave = spf; 1038 if (DEBUG) 1039 System.out.println("Obj = " + objToSave); 1040 objLoaded = dumpAndReload(objToSave); 1041 assertTrue( 1042 "serialPersistentFields do not work properly in this implementation", 1043 ((SerialPersistentFieldsWithoutField) objLoaded).anInstanceVar != CONST); 1044 1045 } catch (IOException e) { 1046 fail("IOException serializing " + objToSave + " : " 1047 + e.getMessage()); 1048 } catch (ClassNotFoundException e) { 1049 fail("ClassNotFoundException reading Object type : " 1050 + e.getMessage()); 1051 } catch (Error err) { 1052 System.out.println("Error when obj = " + objToSave); 1053 // err.printStackTrace(); 1054 throw err; 1055 } 1056 } 1057 1058 public void test_18_51_writeObject() { 1059 // Test for method void 1060 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1061 1062 Object objToSave = null; 1063 Object objLoaded; 1064 1065 try { 1066 1067 ReadWriteObjectAndPrimitiveData readWrite = new ReadWriteObjectAndPrimitiveData(); 1068 objToSave = readWrite; 1069 if (DEBUG) 1070 System.out.println("Obj = " + objToSave); 1071 objLoaded = dumpAndReload(objToSave); 1072 // has to have called the writeObject on the instance to dump 1073 assertTrue(MSG_TEST_FAILED + objToSave, readWrite.calledWriteObject); 1074 // has to have called the readObject on the instance loaded 1075 assertTrue( 1076 MSG_TEST_FAILED + objToSave, 1077 ((ReadWriteObjectAndPrimitiveData) objLoaded).calledReadObject); 1078 1079 } catch (IOException e) { 1080 fail("IOException serializing " + objToSave + " : " 1081 + e.getMessage()); 1082 } catch (ClassNotFoundException e) { 1083 fail("ClassNotFoundException reading Object type : " 1084 + e.getMessage()); 1085 } catch (Error err) { 1086 System.out.println("Error when obj = " + objToSave); 1087 // err.printStackTrace(); 1088 throw err; 1089 } 1090 } 1091 1092 public void test_18_52_writeObject() { 1093 // Test for method void 1094 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1095 1096 Object objToSave = null; 1097 Object objLoaded; 1098 1099 try { 1100 1101 ArrayList list = new ArrayList<String>(Arrays.asList(new String[] { "a", 1102 "list", "of", "strings" })); 1103 objToSave = list; 1104 if (DEBUG) 1105 System.out.println("Obj = " + objToSave); 1106 objLoaded = dumpAndReload(objToSave); 1107 // Has to have worked 1108 assertTrue(MSG_TEST_FAILED + objToSave, true); 1109 1110 } catch (IOException e) { 1111 fail("IOException serializing " + objToSave + " : " 1112 + e.getMessage()); 1113 } catch (ClassNotFoundException e) { 1114 fail("ClassNotFoundException reading Object type : " 1115 + e.getMessage()); 1116 } catch (Error err) { 1117 System.out.println("Error when obj = " + objToSave); 1118 // err.printStackTrace(); 1119 throw err; 1120 } 1121 } 1122 1123 public void test_18_53_writeObject() { 1124 // Test for method void 1125 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1126 1127 Object objToSave = null; 1128 Object objLoaded; 1129 1130 try { 1131 1132 objToSave = Locale.CHINESE; 1133 if (DEBUG) 1134 System.out.println("Obj = " + objToSave); 1135 objLoaded = dumpAndReload(objToSave); 1136 // Has to have worked 1137 assertTrue(MSG_TEST_FAILED + objToSave, true); 1138 1139 } catch (IOException e) { 1140 fail("IOException serializing " + objToSave + " : " 1141 + e.getMessage()); 1142 } catch (ClassNotFoundException e) { 1143 fail("ClassNotFoundException reading Object type : " 1144 + e.getMessage()); 1145 } catch (Error err) { 1146 System.out.println("Error when obj = " + objToSave); 1147 // err.printStackTrace(); 1148 throw err; 1149 } 1150 } 1151 1152 public void test_OptionalDataNotRead() { 1153 // Test for method void 1154 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1155 1156 Object objToSave = null; 1157 Object objLoaded; 1158 1159 try { 1160 OptionalDataNotRead test = new OptionalDataNotRead(); 1161 // Have to save an object after the one above, and when we read it, 1162 // it cannot be a byte[] 1163 Date now = new Date(); 1164 Object[] twoObjects = new Object[2]; 1165 twoObjects[0] = test; 1166 twoObjects[1] = now; 1167 objToSave = twoObjects; 1168 if (DEBUG) 1169 System.out.println("Obj = " + objToSave); 1170 objLoaded = dumpAndReload(objToSave); 1171 // Has to have worked 1172 Object[] twoLoadedObjects = (Object[]) objLoaded; 1173 assertTrue(MSG_TEST_FAILED + objToSave, twoLoadedObjects[0] 1174 .getClass() == OptionalDataNotRead.class); 1175 assertTrue(MSG_TEST_FAILED + objToSave, twoLoadedObjects[1] 1176 .getClass() == Date.class); 1177 1178 } catch (IOException e) { 1179 fail("IOException serializing " + objToSave + " : " 1180 + e.getMessage()); 1181 } catch (ClassNotFoundException e) { 1182 fail("ClassNotFoundException reading Object type : " 1183 + e.getMessage()); 1184 } catch (Error err) { 1185 System.out.println("Error when obj = " + objToSave); 1186 // err.printStackTrace(); 1187 throw err; 1188 } 1189 } 1190 1191 public void test_18_55_writeObject() { 1192 // Test for method void 1193 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1194 1195 Object objToSave = null; 1196 Object objLoaded; 1197 1198 try { 1199 Object[] threeObjects = new Object[3]; 1200 threeObjects[0] = new Integer(2); 1201 threeObjects[1] = Date.class; 1202 threeObjects[2] = threeObjects[0]; // has to be the same 1203 objToSave = threeObjects; 1204 if (DEBUG) 1205 System.out.println("Obj = " + objToSave); 1206 objLoaded = dumpAndReload(objToSave); 1207 // Has to have worked 1208 Object[] threeLoadedObjects = (Object[]) objLoaded; 1209 assertTrue(MSG_TEST_FAILED + objToSave, threeLoadedObjects[0] 1210 .getClass() == Integer.class); 1211 assertTrue(MSG_TEST_FAILED + objToSave, 1212 threeLoadedObjects[1] == Date.class); 1213 assertTrue(MSG_TEST_FAILED + objToSave, 1214 threeLoadedObjects[0] == threeLoadedObjects[2]); 1215 1216 } catch (IOException e) { 1217 fail("IOException serializing " + objToSave + " : " 1218 + e.getMessage()); 1219 } catch (ClassNotFoundException e) { 1220 fail("ClassNotFoundException reading Object type : " 1221 + e.getMessage()); 1222 } catch (Error err) { 1223 System.out.println("Error when obj = " + objToSave); 1224 // err.printStackTrace(); 1225 throw err; 1226 } 1227 } 1228 1229 public void test_18_56_writeObject() { 1230 // Test for method void 1231 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1232 1233 Object objToSave = null; 1234 Object objLoaded; 1235 1236 try { 1237 // Test for 1FD24BY 1238 NestedPutField test = new NestedPutField(); 1239 objToSave = test; 1240 if (DEBUG) 1241 System.out.println("Obj = " + objToSave); 1242 objLoaded = dumpAndReload(objToSave); 1243 // Has to have worked 1244 assertNotNull(MSG_TEST_FAILED + objToSave, 1245 ((NestedPutField) objLoaded).field1); 1246 1247 } catch (IOException e) { 1248 fail("IOException serializing " + objToSave + " : " 1249 + e.getMessage()); 1250 } catch (ClassNotFoundException e) { 1251 fail("ClassNotFoundException reading Object type : " 1252 + e.getMessage()); 1253 } catch (Error err) { 1254 System.out.println("Error when obj = " + objToSave); 1255 // err.printStackTrace(); 1256 throw err; 1257 } 1258 } 1259 1260 public void test_18_57_writeObject() { 1261 // Test for method void 1262 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1263 1264 Object objToSave = null; 1265 Object objLoaded; 1266 1267 try { 1268 ByteArrayOutputStream out; 1269 StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping; 1270 1271 out = new ByteArrayOutputStream(); 1272 streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping( 1273 out); 1274 ; 1275 objToSave = FOO.getClass(); 1276 if (DEBUG) 1277 System.out.println("Obj = " + objToSave); 1278 streamBasedReplacementWhenDumping.writeObject(objToSave); 1279 // Has to have run the replacement method 1280 assertTrue("Executed replacement when it should not: " + objToSave, 1281 !streamBasedReplacementWhenDumping.calledClassReplacement); 1282 1283 } catch (IOException e) { 1284 fail("Exception serializing " + objToSave + "\t->" 1285 + e.toString()); 1286 } catch (Error err) { 1287 System.out.println("Error " + err + " when obj = " + objToSave); 1288 throw err; 1289 } 1290 } 1291 1292 public void test_18_58_writeObject() { 1293 // Test for method void 1294 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1295 1296 Object objToSave = null; 1297 Object objLoaded; 1298 1299 try { 1300 ByteArrayOutputStream out; 1301 StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping; 1302 1303 out = new ByteArrayOutputStream(); 1304 streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping( 1305 out); 1306 ; 1307 objToSave = ObjectStreamClass.lookup(FOO.getClass()); 1308 if (DEBUG) 1309 System.out.println("Obj = " + objToSave); 1310 streamBasedReplacementWhenDumping.writeObject(objToSave); 1311 // Has to have run the replacement method 1312 assertTrue( 1313 "Executed replacement when it should not: " + objToSave, 1314 !streamBasedReplacementWhenDumping.calledObjectStreamClassReplacement); 1315 1316 } catch (IOException e) { 1317 fail("Exception serializing " + objToSave + "\t->" 1318 + e.toString()); 1319 } catch (Error err) { 1320 System.out.println("Error " + err + " when obj = " + objToSave); 1321 throw err; 1322 } 1323 } 1324 1325 public void test_18_59_writeObject() { 1326 // Test for method void 1327 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1328 1329 Object objToSave = null; 1330 Object objLoaded; 1331 1332 try { 1333 ByteArrayOutputStream out; 1334 StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping; 1335 1336 out = new ByteArrayOutputStream(); 1337 streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping( 1338 out); 1339 ; 1340 objToSave = new int[3]; 1341 if (DEBUG) 1342 System.out.println("Obj = " + objToSave); 1343 streamBasedReplacementWhenDumping.writeObject(objToSave); 1344 // Has to have run the replacement method 1345 assertTrue("DId not execute replacement when it should: " 1346 + objToSave, 1347 streamBasedReplacementWhenDumping.calledArrayReplacement); 1348 1349 } catch (IOException e) { 1350 fail("Exception serializing " + objToSave + "\t->" 1351 + e.toString()); 1352 } catch (Error err) { 1353 System.out.println("Error " + err + " when obj = " + objToSave); 1354 throw err; 1355 } 1356 } 1357 1358 public void test_18_60_writeObject() { 1359 // Test for method void 1360 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1361 1362 Object objToSave = null; 1363 Object objLoaded; 1364 1365 try { 1366 ByteArrayOutputStream out; 1367 StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping; 1368 1369 out = new ByteArrayOutputStream(); 1370 streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping( 1371 out); 1372 ; 1373 objToSave = FOO; 1374 if (DEBUG) 1375 System.out.println("Obj = " + objToSave); 1376 streamBasedReplacementWhenDumping.writeObject(objToSave); 1377 // Has to have run the replacement method 1378 assertTrue("Did not execute replacement when it should: " 1379 + objToSave, 1380 streamBasedReplacementWhenDumping.calledStringReplacement); 1381 1382 } catch (IOException e) { 1383 fail("Exception serializing " + objToSave + "\t->" 1384 + e.toString()); 1385 } catch (Error err) { 1386 System.out.println("Error " + err + " when obj = " + objToSave); 1387 throw err; 1388 } 1389 } 1390 1391 public void test_18_61_writeObject() { 1392 // Test for method void 1393 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1394 1395 Object objToSave = null; 1396 Object objLoaded; 1397 1398 try { 1399 ArrayOfSerializable test = new ArrayOfSerializable(); 1400 objToSave = test; 1401 if (DEBUG) 1402 System.out.println("Obj = " + objToSave); 1403 objLoaded = dumpAndReload(objToSave); 1404 // Has to have worked 1405 assertTrue(MSG_TEST_FAILED + objToSave, true); 1406 1407 } catch (IOException e) { 1408 fail("IOException serializing " + objToSave + " : " 1409 + e.getMessage()); 1410 } catch (ClassNotFoundException e) { 1411 fail("ClassNotFoundException reading Object type : " 1412 + e.getMessage()); 1413 } catch (Error err) { 1414 System.out.println("Error when obj = " + objToSave); 1415 // err.printStackTrace(); 1416 throw err; 1417 } 1418 } 1419 1420 public void test_18_62_writeObject() { 1421 // Test for method void 1422 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1423 1424 Object objToSave = null; 1425 Object objLoaded; 1426 1427 try { 1428 ClassSubClassTest1 test = new ClassSubClassTest1( 1429 "SuperInitialString", "SubInitialString"); 1430 objToSave = test; 1431 if (DEBUG) 1432 System.out.println("Obj = " + objToSave); 1433 objLoaded = dumpAndReload(objToSave); 1434 // Has to have worked 1435 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 1436 1437 } catch (IOException e) { 1438 fail("IOException serializing " + objToSave + " : " 1439 + e.getMessage()); 1440 } catch (ClassNotFoundException e) { 1441 fail("ClassNotFoundException reading Object type : " 1442 + e.getMessage()); 1443 } catch (Error err) { 1444 System.out.println("Error when obj = " + objToSave); 1445 // err.printStackTrace(); 1446 throw err; 1447 } 1448 } 1449 1450 public void test_18_63_writeObject() { 1451 // Test for method void 1452 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1453 1454 Object objToSave = null; 1455 Object objLoaded; 1456 1457 try { 1458 ConstructorTestC test = new ConstructorTestC(); 1459 objToSave = test; 1460 if (DEBUG) 1461 System.out.println("Obj = " + objToSave); 1462 objLoaded = dumpAndReload(objToSave); 1463 // Has to have worked 1464 assertTrue(MSG_TEST_FAILED + objToSave, test.verify(objLoaded)); 1465 1466 } catch (IOException e) { 1467 fail("IOException serializing " + objToSave + " : " 1468 + e.getMessage()); 1469 } catch (ClassNotFoundException e) { 1470 fail("ClassNotFoundException reading Object type : " 1471 + e.getMessage()); 1472 } catch (Error err) { 1473 System.out.println("Error when obj = " + objToSave); 1474 // err.printStackTrace(); 1475 throw err; 1476 } 1477 } 1478 1479 public void test_18_64_writeObject() { 1480 // Test for method void 1481 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1482 1483 Object objToSave = null; 1484 Object objLoaded; 1485 1486 try { 1487 HashCodeTest test = new HashCodeTest(); 1488 objToSave = test; 1489 if (DEBUG) 1490 System.out.println("Obj = " + objToSave); 1491 objLoaded = dumpAndReload(objToSave); 1492 // Has to have worked 1493 assertTrue(MSG_TEST_FAILED + objToSave, 1494 !((HashCodeTest) objLoaded).serializationUsesHashCode); 1495 1496 } catch (IOException e) { 1497 fail("IOException serializing " + objToSave + " : " 1498 + e.getMessage()); 1499 } catch (ClassNotFoundException e) { 1500 fail("ClassNotFoundException reading Object type : " 1501 + e.getMessage()); 1502 } catch (Error err) { 1503 System.out.println("Error when obj = " + objToSave); 1504 // err.printStackTrace(); 1505 throw err; 1506 } 1507 } 1508 1509 public void test_18_65_writeObject() { 1510 // Test for method void 1511 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1512 1513 Object objToSave = null; 1514 Object objLoaded; 1515 1516 try { 1517 InitializerFieldsTest test = new InitializerFieldsTest(); 1518 test.toBeSerialized = "serializing"; 1519 InitializerFieldsTest.toBeNotSerialized = "It should not have this value after loaded from a File"; 1520 InitializerFieldsTest.toBeNotSerialized2 = "Good-This is the rigth value."; 1521 1522 objToSave = test; 1523 if (DEBUG) 1524 System.out.println("Obj = " + objToSave); 1525 dump(objToSave); 1526 InitializerFieldsTest.toBeNotSerialized = new String( 1527 InitializerFieldsTest.toBeNotSerialized2); 1528 objLoaded = reload(); 1529 1530 // Has to have worked 1531 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 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 (Error err) { 1540 System.out.println("Error when obj = " + objToSave); 1541 // err.printStackTrace(); 1542 throw err; 1543 } 1544 } 1545 1546 public void test_18_66_writeObject() { 1547 // Test for method void 1548 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1549 1550 Object objToSave = null; 1551 Object objLoaded; 1552 1553 try { 1554 InitializerFieldsTest2 test = new InitializerFieldsTest2(); 1555 test.toBeSerialized = "serializing"; 1556 test.toBeSerialized3 = "serializing3"; 1557 test.toBeSerialized4 = "serializing4"; 1558 test.toBeSerialized5 = "serializing5"; 1559 InitializerFieldsTest2.toBeNotSerialized = "It should not have this value after loaded from a File"; 1560 InitializerFieldsTest2.toBeNotSerialized2 = "Good-This is the rigth value."; 1561 1562 objToSave = test; 1563 if (DEBUG) 1564 System.out.println("Obj = " + objToSave); 1565 dump(objToSave); 1566 InitializerFieldsTest2.toBeNotSerialized = new String( 1567 InitializerFieldsTest2.toBeNotSerialized2); 1568 objLoaded = reload(); 1569 1570 // Has to have worked 1571 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 1572 1573 } catch (IOException e) { 1574 fail("IOException serializing " + objToSave + " : " 1575 + e.getMessage()); 1576 } catch (ClassNotFoundException e) { 1577 fail("ClassNotFoundException reading Object type : " 1578 + e.getMessage()); 1579 } catch (Error err) { 1580 System.out.println("Error when obj = " + objToSave); 1581 // err.printStackTrace(); 1582 throw err; 1583 } 1584 } 1585 1586 public void test_18_67_writeObject() { 1587 // Test for method void 1588 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1589 1590 Object objToSave = null; 1591 Object objLoaded; 1592 1593 try { 1594 InitializerFieldsTest3 test = new InitializerFieldsTest3(); 1595 test.toBeSerialized = "serializing"; 1596 test.toBeSerialized3 = "serializing3"; 1597 test.toBeSerialized4 = "serializing4"; 1598 test.toBeSerialized5 = "serializing5"; 1599 InitializerFieldsTest2.toBeNotSerialized = "It should not have this value after loaded from a File"; 1600 InitializerFieldsTest2.toBeNotSerialized2 = "Good-This is the rigth value."; 1601 test.sub_toBeSerialized = "serializingSub"; 1602 test.sub_toBeSerialized3 = "serializing3sub"; 1603 test.sub_toBeSerialized4 = "serializing4sub"; 1604 test.sub_toBeSerialized5 = "serializing5sub"; 1605 InitializerFieldsTest3.sub_toBeNotSerialized = "(Subclass) It should not have this value after loaded from a File"; 1606 InitializerFieldsTest3.sub_toBeNotSerialized2 = "(Subclass) Good-This is the rigth value."; 1607 // Before dumping the two static vars are differents. 1608 // After dumping the value of toBeNotSerialized2 is put in 1609 // toBeNotSerialized 1610 // After loading it must be the same. 1611 objToSave = test; 1612 if (DEBUG) 1613 System.out.println("Obj = " + objToSave); 1614 dump(objToSave); 1615 InitializerFieldsTest2.toBeNotSerialized = new String( 1616 InitializerFieldsTest2.toBeNotSerialized2); 1617 InitializerFieldsTest3.sub_toBeNotSerialized = new String( 1618 InitializerFieldsTest3.sub_toBeNotSerialized2); 1619 objLoaded = reload(); 1620 1621 // Has to have worked 1622 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 1623 1624 } catch (IOException e) { 1625 fail("IOException serializing " + objToSave + " : " 1626 + e.getMessage()); 1627 } catch (ClassNotFoundException e) { 1628 fail("ClassNotFoundException reading Object type : " 1629 + e.getMessage()); 1630 } catch (Error err) { 1631 System.out.println("Error when obj = " + objToSave); 1632 // err.printStackTrace(); 1633 throw err; 1634 } 1635 } 1636 1637 public void test_DeepNesting() { 1638 // Test for method void 1639 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1640 1641 Object objToSave = null; 1642 Object objLoaded; 1643 1644 try { 1645 DeepNesting test = new DeepNesting(50); 1646 objToSave = test; 1647 if (DEBUG) 1648 System.out.println("Obj = " + objToSave); 1649 objLoaded = dumpAndReload(objToSave); 1650 1651 // Has to have worked 1652 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 1653 1654 } catch (IOException e) { 1655 fail("IOException serializing " + objToSave + " : " 1656 + e.getMessage()); 1657 } catch (ClassNotFoundException e) { 1658 fail("ClassNotFoundException reading Object type : " 1659 + e.getMessage()); 1660 } catch (Error err) { 1661 // err.printStackTrace(); 1662 System.out.println("Error " + err + " when obj = " + objToSave); 1663 throw err; 1664 } 1665 } 1666 1667 public void test_DeepNestingWithWriteObject() { 1668 // Test for method void 1669 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1670 1671 Object objToSave = null; 1672 Object objLoaded; 1673 1674 try { 1675 DeepNestingWithWriteObject test = new DeepNestingWithWriteObject(50); 1676 objToSave = test; 1677 if (DEBUG) 1678 System.out.println("Obj = " + objToSave); 1679 objLoaded = dumpAndReload(objToSave); 1680 1681 // Has to have worked 1682 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 1683 1684 } catch (IOException e) { 1685 fail("IOException serializing " + objToSave + " : " 1686 + e.getMessage()); 1687 } catch (ClassNotFoundException e) { 1688 fail("ClassNotFoundException reading Object type : " 1689 + e.getMessage()); 1690 } catch (Error err) { 1691 // err.printStackTrace(); 1692 System.out.println("Error " + err + " when obj = " + objToSave); 1693 throw err; 1694 } 1695 } 1696 1697 public void test_18_69_writeObject() { 1698 // Test for method void 1699 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1700 1701 Object objToSave = null; 1702 Object objLoaded; 1703 1704 try { 1705 NonPublicClassTest test = new NonPublicClassTest(); 1706 test.x10(); 1707 objToSave = test; 1708 if (DEBUG) 1709 System.out.println("Obj = " + objToSave); 1710 objLoaded = dumpAndReload(objToSave); 1711 1712 // Has to have worked 1713 assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded))); 1714 1715 } catch (IOException e) { 1716 fail("IOException serializing " + objToSave + " : " 1717 + e.getMessage()); 1718 } catch (ClassNotFoundException e) { 1719 fail("ClassNotFoundException reading Object type : " 1720 + e.getMessage()); 1721 } catch (Error err) { 1722 System.out.println("Error when obj = " + objToSave); 1723 // err.printStackTrace(); 1724 throw err; 1725 } 1726 } 1727 1728 public void test_18_70_writeObject() { 1729 // Test for method void 1730 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1731 1732 Object objToSave = null; 1733 Object objLoaded; 1734 1735 try { 1736 int[] test = new int[1]; 1737 int intValue = 0; 1738 test[0] = intValue; 1739 objToSave = test; 1740 if (DEBUG) 1741 System.out.println("Obj = " + objToSave); 1742 objLoaded = dumpAndReload(objToSave); 1743 1744 // Has to have worked 1745 assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(test, 1746 (int[]) objLoaded)); 1747 1748 } catch (IOException e) { 1749 fail("IOException serializing " + objToSave + " : " 1750 + e.getMessage()); 1751 } catch (ClassNotFoundException e) { 1752 fail("ClassNotFoundException reading Object type : " 1753 + e.getMessage()); 1754 } catch (Error err) { 1755 System.out.println("Error when obj = " + objToSave); 1756 // err.printStackTrace(); 1757 throw err; 1758 } 1759 } 1760 1761 public void test_18_71_writeObject() { 1762 // Test for method void 1763 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1764 1765 Object objToSave = null; 1766 Object objLoaded; 1767 1768 try { 1769 int i, j, maxJ = 3, maxI = 200; 1770 byte[][] obj = new byte[maxJ][maxI]; 1771 for (j = 0; j < maxJ; j++) { 1772 for (i = 0; i < maxI; i++) 1773 obj[j][i] = (byte) (i - 100); 1774 } 1775 objToSave = obj; 1776 if (DEBUG) 1777 System.out.println("Obj = " + objToSave); 1778 objLoaded = dumpAndReload(objToSave); 1779 byte[][] toCompare = (byte[][]) objLoaded; 1780 1781 boolean ok = true; 1782 // Has to have worked 1783 for (j = 0; j < maxJ; j++) { 1784 for (i = 0; i < maxI; i++) 1785 if (obj[j][i] != toCompare[j][i]) { 1786 ok = false; 1787 break; 1788 } 1789 } 1790 1791 assertTrue(MSG_TEST_FAILED + objToSave, ok); 1792 1793 } catch (IOException e) { 1794 fail("IOException serializing " + objToSave + " : " 1795 + e.getMessage()); 1796 } catch (ClassNotFoundException e) { 1797 fail("ClassNotFoundException reading Object type : " 1798 + e.getMessage()); 1799 } catch (Error err) { 1800 System.out.println("Error when obj = " + objToSave); 1801 // err.printStackTrace(); 1802 throw err; 1803 } 1804 } 1805 1806 public void test_18_72_writeObject() { 1807 // Test for method void 1808 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1809 1810 Object objToSave = null; 1811 Object objLoaded; 1812 1813 try { 1814 int i, j, maxJ = 3, maxI = 200; 1815 int[][] obj = new int[maxJ][maxI]; 1816 for (j = 0; j < maxJ; j++) { 1817 for (i = 0; i < maxI; i++) 1818 obj[j][i] = (i - 100); 1819 } 1820 objToSave = obj; 1821 if (DEBUG) 1822 System.out.println("Obj = " + objToSave); 1823 objLoaded = dumpAndReload(objToSave); 1824 int[][] toCompare = (int[][]) objLoaded; 1825 1826 boolean ok = true; 1827 // Has to have worked 1828 for (j = 0; j < maxJ; j++) { 1829 for (i = 0; i < maxI; i++) 1830 if (obj[j][i] != toCompare[j][i]) { 1831 ok = false; 1832 break; 1833 } 1834 } 1835 1836 assertTrue(MSG_TEST_FAILED + objToSave, ok); 1837 1838 } catch (IOException e) { 1839 fail("IOException serializing " + objToSave + " : " 1840 + e.getMessage()); 1841 } catch (ClassNotFoundException e) { 1842 fail("ClassNotFoundException reading Object type : " 1843 + e.getMessage()); 1844 } catch (Error err) { 1845 System.out.println("Error when obj = " + objToSave); 1846 // err.printStackTrace(); 1847 throw err; 1848 } 1849 } 1850 1851 public void test_18_73_writeObject() { 1852 // Test for method void 1853 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1854 1855 Object objToSave = null; 1856 Object objLoaded; 1857 1858 try { 1859 String org = "abcdefghijklmnopqrstuvxyz1234567890abcdefghijklmnopqrstuvxyz1234567890"; 1860 int i, j, maxJ = 3, maxI = 70; 1861 String[][] obj = new String[maxJ][maxI]; 1862 for (j = 0; j < maxJ; j++) { 1863 for (i = 0; i < maxI; i++) 1864 obj[j][i] = org.substring(0, i); 1865 } 1866 objToSave = obj; 1867 if (DEBUG) 1868 System.out.println("Obj = " + objToSave); 1869 objLoaded = dumpAndReload(objToSave); 1870 String[][] toCompare = (String[][]) objLoaded; 1871 1872 boolean ok = true; 1873 // Has to have worked 1874 for (j = 0; j < maxJ; j++) { 1875 for (i = 0; i < maxI; i++) 1876 if (!obj[j][i].equals(toCompare[j][i])) { 1877 ok = false; 1878 break; 1879 } 1880 } 1881 1882 assertTrue(MSG_TEST_FAILED + objToSave, ok); 1883 1884 } catch (IOException e) { 1885 fail("IOException serializing " + objToSave + " : " 1886 + e.getMessage()); 1887 } catch (ClassNotFoundException e) { 1888 fail("ClassNotFoundException reading Object type : " 1889 + e.getMessage()); 1890 } catch (Error err) { 1891 System.out.println("Error when obj = " + objToSave); 1892 // err.printStackTrace(); 1893 throw err; 1894 } 1895 } 1896 1897 public void test_18_74_writeObject() { 1898 // Test for method void 1899 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1900 1901 Object objToSave = null; 1902 Object objLoaded; 1903 1904 try { 1905 SameInstVarNameSubClass test = new SameInstVarNameSubClass(100); 1906 objToSave = test; 1907 if (DEBUG) 1908 System.out.println("Obj = " + objToSave); 1909 objLoaded = dumpAndReload(objToSave); 1910 // Has to have worked 1911 assertTrue(MSG_TEST_FAILED + objToSave, 1912 ((SameInstVarNameSubClass) objLoaded).foo == 100); 1913 1914 } catch (IOException e) { 1915 fail("IOException serializing " + objToSave + " : " 1916 + e.getMessage()); 1917 } catch (ClassNotFoundException e) { 1918 fail("ClassNotFoundException reading Object type : " 1919 + e.getMessage()); 1920 } catch (Error err) { 1921 System.out.println("Error when obj = " + objToSave); 1922 // err.printStackTrace(); 1923 throw err; 1924 } 1925 } 1926 1927 public void test_18_75_writeObject() { 1928 // Test for method void 1929 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1930 1931 Object objToSave = null; 1932 Object objLoaded; 1933 1934 try { 1935 SInterfaceTest test = new SInterfaceTest(); 1936 objToSave = test; 1937 if (DEBUG) 1938 System.out.println("Obj = " + objToSave); 1939 objLoaded = dumpAndReload(objToSave); 1940 // Has to have worked 1941 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 1942 1943 } catch (IOException e) { 1944 fail("IOException serializing " + objToSave + " : " 1945 + e.getMessage()); 1946 } catch (ClassNotFoundException e) { 1947 fail("ClassNotFoundException reading Object type : " 1948 + e.getMessage()); 1949 } catch (Error err) { 1950 System.out.println("Error when obj = " + objToSave); 1951 // err.printStackTrace(); 1952 throw err; 1953 } 1954 } 1955 1956 public void test_18_76_writeObject() { 1957 // Test for method void 1958 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1959 1960 Object objToSave = null; 1961 Object objLoaded; 1962 1963 try { 1964 SInterfaceTest2 test = new SInterfaceTest2(); 1965 objToSave = test; 1966 if (DEBUG) 1967 System.out.println("Obj = " + objToSave); 1968 objLoaded = dumpAndReload(objToSave); 1969 // Has to have worked 1970 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 1971 1972 } catch (IOException e) { 1973 fail("IOException serializing " + objToSave + " : " 1974 + e.getMessage()); 1975 } catch (ClassNotFoundException e) { 1976 fail("ClassNotFoundException reading Object type : " 1977 + e.getMessage()); 1978 } catch (Error err) { 1979 System.out.println("Error when obj = " + objToSave); 1980 // err.printStackTrace(); 1981 throw err; 1982 } 1983 } 1984 1985 public void test_18_77_writeObject() { 1986 // Test for method void 1987 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1988 1989 Object objToSave = null; 1990 Object objLoaded; 1991 1992 try { 1993 SuperclassTest test = new SuperclassTest(); 1994 objToSave = test; 1995 if (DEBUG) 1996 System.out.println("Obj = " + objToSave); 1997 objLoaded = dumpAndReload(objToSave); 1998 // Has to have worked 1999 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 2000 2001 } catch (IOException e) { 2002 fail("IOException serializing " + objToSave + " : " 2003 + e.getMessage()); 2004 } catch (ClassNotFoundException e) { 2005 fail("ClassNotFoundException reading Object type : " 2006 + e.getMessage()); 2007 } catch (Error err) { 2008 System.out.println("Error when obj = " + objToSave); 2009 // err.printStackTrace(); 2010 throw err; 2011 } 2012 } 2013 2014 public void test_18_78_writeObject() { 2015 // Test for method void 2016 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 2017 2018 Object objToSave = null; 2019 Object objLoaded; 2020 2021 try { 2022 SuperclassTest2 test = new SuperclassTest2(); 2023 objToSave = test; 2024 if (DEBUG) 2025 System.out.println("Obj = " + objToSave); 2026 objLoaded = dumpAndReload(objToSave); 2027 // Has to have worked 2028 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 2029 2030 } catch (IOException e) { 2031 fail("IOException serializing " + objToSave + " : " 2032 + e.getMessage()); 2033 } catch (ClassNotFoundException e) { 2034 fail("ClassNotFoundException reading Object type : " 2035 + e.getMessage()); 2036 } catch (Error err) { 2037 System.out.println("Error when obj = " + objToSave); 2038 // err.printStackTrace(); 2039 throw err; 2040 } 2041 } 2042 2043 public void test_18_79_writeObject() { 2044 // Test for method void 2045 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 2046 2047 Object objToSave = null; 2048 Object objLoaded; 2049 2050 try { 2051 SyntheticFieldTest test = new SyntheticFieldTest(); 2052 objToSave = test; 2053 if (DEBUG) 2054 System.out.println("Obj = " + objToSave); 2055 objLoaded = dumpAndReload(objToSave); 2056 // Has to have worked 2057 assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded)); 2058 2059 } catch (IOException e) { 2060 fail("IOException serializing " + objToSave + " : " 2061 + e.getMessage()); 2062 } catch (ClassNotFoundException e) { 2063 fail("ClassNotFoundException reading Object type : " 2064 + e.getMessage()); 2065 } catch (Error err) { 2066 System.out.println("Error when obj = " + objToSave); 2067 // err.printStackTrace(); 2068 throw err; 2069 } 2070 } 2071 2072 public void test_18_80_writeObject() { 2073 // Test for method void 2074 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 2075 2076 try { 2077 ByteArrayOutputStream out = new ByteArrayOutputStream(); 2078 DataOutputStream dos = new DataOutputStream(out); 2079 new ObjectOutputStream(dos); // just to make sure we get a header 2080 dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA); 2081 int length = 99; 2082 dos.writeByte(length); 2083 for (int i = 0; i < length; i++) { 2084 dos.writeByte(0); // actual value does not matter 2085 } 2086 dos.flush(); 2087 int lengthRead = 0; 2088 try { 2089 ObjectInputStream ois = new ObjectInputStream( 2090 new ByteArrayInputStream(out.toByteArray())); 2091 Object obj = ois.readObject(); 2092 } catch (OptionalDataException e) { 2093 lengthRead = e.length; 2094 } 2095 assertTrue("Did not throw exception with optional data size ", 2096 length == lengthRead); 2097 } catch (ClassNotFoundException e) { 2098 fail("Unable to read BLOCKDATA: " + e.getMessage()); 2099 } catch (IOException e) { 2100 fail("IOException testing BLOCKDATA : " + e.getMessage()); 2101 } catch (Error err) { 2102 System.out.println("Error " + err + " when testing BLOCKDATA"); 2103 throw err; 2104 } 2105 } 2106} 2107