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.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
426        static {
427            toBeNotSerialized = "StaticInitialValue";
428            toBeNotSerialized2 = new String(toBeNotSerialized);
429        }
430
431        public java.lang.String toBeSerialized5;
432
433        public boolean equals(Object obj) {
434            /*
435                * This method is not answering it the objs is equal. It is
436                * answering if the vars have the value that it have to have after
437                * dumping and loading
438                */
439
440            if (obj == null)
441                return false;
442            if (!(obj instanceof InitializerFieldsTest2))
443                return false;
444
445            InitializerFieldsTest2 inst = (InitializerFieldsTest2) obj;
446            return inst.toBeSerialized.equals(this.toBeSerialized)
447                    && inst.toBeSerialized3.equals(this.toBeSerialized3)
448                    && inst.toBeSerialized4.equals(this.toBeSerialized4)
449                    && inst.toBeSerialized5.equals(this.toBeSerialized5)
450                    && InitializerFieldsTest2.toBeNotSerialized.equals(toBeNotSerialized2);
451        }
452    }
453
454    private static class InitializerFieldsTest3 extends InitializerFieldsTest2
455            implements java.io.Serializable {
456        public java.lang.String sub_toBeSerialized;
457
458        public static java.lang.String sub_toBeNotSerialized;
459
460        public static java.lang.String sub_toBeNotSerialized2;
461
462        {
463            sub_toBeSerialized = "NonStaticInitialValue";
464        }
465
466        public java.lang.String sub_toBeSerialized3;
467
468        public java.lang.String sub_toBeSerialized4;
469
470        static {
471            sub_toBeNotSerialized = "StaticInitialValue";
472            sub_toBeNotSerialized2 = new String(sub_toBeNotSerialized);
473        }
474
475        public java.lang.String sub_toBeSerialized5;
476
477        public boolean equals(Object obj) {
478            /*
479                * This method is not answering it the objs is equal. It is
480                * answering if the vars have the value that it have to have after
481                * dumping and loading
482                */
483
484            if (!super.equals(obj))
485                return false;
486            if (!(obj instanceof InitializerFieldsTest3))
487                return false;
488
489            InitializerFieldsTest3 inst = (InitializerFieldsTest3) obj;
490            return inst.sub_toBeSerialized.equals(this.sub_toBeSerialized)
491                    && inst.sub_toBeSerialized3
492                    .equals(this.sub_toBeSerialized3)
493                    && inst.sub_toBeSerialized4
494                    .equals(this.sub_toBeSerialized4)
495                    && inst.sub_toBeSerialized5
496                    .equals(this.sub_toBeSerialized5)
497                    && InitializerFieldsTest3.sub_toBeNotSerialized
498                    .equals(sub_toBeNotSerialized2);
499        }
500    }
501
502    // -----------------------------------------------------------------------------------
503    private static class DeepNesting implements java.io.Serializable {
504        public float id;
505
506        public DeepNesting next;
507
508        public boolean dump;
509
510        public boolean load;
511
512        public DeepNesting(float id) {
513            this.id = id;
514            next = null;
515            dump = false;
516            load = false;
517        }
518
519        public DeepNesting(int howMany) {
520            DeepNesting prev = new DeepNesting(0.0F);
521            next(prev);
522            for (int i = 1; i < howMany; i++) {
523                prev = prev.next(new DeepNesting(i * 1.0F));
524            }
525        }
526
527        public boolean equals(Object obj) {
528            if (obj == null)
529                return false;
530            if (!(obj instanceof DeepNesting))
531                return false;
532
533            DeepNesting inst = (DeepNesting) obj;
534            if (inst.dump != this.dump || inst.load != this.load)
535                return false;
536
537            if (inst.next == null || this.next == null)
538                return inst.next == this.next; // both null
539            return this.next.equals(inst.next);
540        }
541
542        public DeepNesting next(DeepNesting ivt) {
543            next = ivt;
544            return ivt;
545        }
546    }
547
548    // -----------------------------------------------------------------------------------
549    private static class DeepNestingWithWriteObject implements
550            java.io.Serializable {
551        public float id;
552
553        public DeepNestingWithWriteObject next;
554
555        public boolean dump;
556
557        public boolean load;
558
559        public DeepNestingWithWriteObject(float id) {
560            this.id = id;
561            next = null;
562            dump = false;
563            load = false;
564        }
565
566        public DeepNestingWithWriteObject(int howMany) {
567            DeepNestingWithWriteObject prev = new DeepNestingWithWriteObject(
568                    0.0F);
569            next(prev);
570            for (int i = 1; i < howMany; i++) {
571                prev = prev.next(new DeepNestingWithWriteObject(i * 1.0F));
572            }
573        }
574
575        public boolean equals(Object obj) {
576            if (obj == null)
577                return false;
578            if (!(obj instanceof DeepNestingWithWriteObject))
579                return false;
580
581            DeepNestingWithWriteObject inst = (DeepNestingWithWriteObject) obj;
582            if (inst.dump != this.dump || inst.load != this.load)
583                return false;
584
585            if (inst.next == null || this.next == null)
586                return inst.next == this.next; // both null;
587            return this.next.equals(inst.next);
588        }
589
590        public DeepNestingWithWriteObject next(DeepNestingWithWriteObject ivt) {
591            next = ivt;
592            return ivt;
593        }
594
595        private void writeObject(java.io.ObjectOutputStream s)
596                throws IOException {
597            s.defaultWriteObject();
598        }
599
600        private void readObject(java.io.ObjectInputStream s)
601                throws IOException, ClassNotFoundException {
602            s.defaultReadObject();
603        }
604    }
605
606    // -----------------------------------------------------------------------------------
607    static class NonPublicClassTest extends java.lang.Object implements
608            java.io.Serializable {
609        int field = 1;
610
611        public NonPublicClassTest() {
612            field = 10;
613        }
614
615        public boolean equals(Object o) {
616            if (o instanceof NonPublicClassTest)
617                return field == ((NonPublicClassTest) o).field;
618            return false;
619        }
620
621        public void x10() {
622            field *= 10;
623        }
624    }
625
626    // -----------------------------------------------------------------------------------
627    private static class SameInstVarNameSuperClass {
628        private int foo;
629
630        public SameInstVarNameSuperClass() {
631            super();
632        }
633
634        public SameInstVarNameSuperClass(int fooValue) {
635            foo = fooValue;
636        }
637
638        public String toString() {
639            return "foo = " + foo;
640        }
641    }
642
643    private static class SameInstVarNameSubClass extends
644            SameInstVarNameSuperClass implements java.io.Serializable {
645        protected int foo;
646
647        public SameInstVarNameSubClass() {
648            super();
649        }
650
651        public SameInstVarNameSubClass(int fooValue) {
652            super(-fooValue);
653            foo = fooValue;
654        }
655    }
656
657    // -----------------------------------------------------------------------------------
658    private static class SInterfaceTest implements java.io.Serializable {
659        public static int staticVar = 5;
660
661        public transient int[] transVar = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
662
663        public int instanceVar = 7;
664
665        public boolean equals(Object obj) {
666            if (obj == null)
667                return false;
668            if (!(obj instanceof SInterfaceTest))
669                return false;
670
671            SInterfaceTest inst = (SInterfaceTest) obj;
672            if (this.instanceVar != inst.instanceVar)
673                return false;
674            if (inst.transVar == null || this.transVar == null)
675                return inst.transVar == this.transVar; // both null
676            for (int i = 0; i < transVar.length; i++)
677                if (inst.transVar[i] != this.transVar[i])
678                    return false;
679            return true;
680        }
681
682        private void readObject(java.io.ObjectInputStream s)
683                throws IOException, ClassNotFoundException {
684            Object arr;
685            s.defaultReadObject();
686            arr = s.readObject();
687            transVar = (int[]) arr;
688        }
689
690        private void writeObject(java.io.ObjectOutputStream s)
691                throws IOException {
692            s.defaultWriteObject();
693            s.writeObject(transVar);
694        }
695
696        public void x10() {
697            for (int i = 0; i < transVar.length; i++)
698                transVar[i] = transVar[i] * 10;
699            instanceVar = instanceVar * 10;
700        }
701    }
702
703    // -----------------------------------------------------------------------------------
704    private static class SInterfaceTest2 extends SInterfaceTest {
705        private void readObject(java.io.ObjectInputStream s)
706                throws IOException, ClassNotFoundException {
707            Object arr;
708            instanceVar = s.readInt();
709            arr = s.readObject();
710            transVar = (int[]) arr;
711        }
712
713        private void writeObject(java.io.ObjectOutputStream s)
714                throws IOException {
715            s.writeInt(instanceVar);
716            s.writeObject(transVar);
717        }
718    }
719
720    // -----------------------------------------------------------------------------------
721    private static class SuperclassTest extends java.lang.Object implements
722            java.io.Serializable {
723        int superfield = 1;
724
725        public SuperclassTest() {
726            superfield = 10;
727        }
728
729        public boolean equals(Object o) {
730            if (o.getClass() == this.getClass())
731                return superfield == ((SuperclassTest) o).superfield;
732            return false;
733        }
734
735        private void readObject(java.io.ObjectInputStream s)
736                throws IOException, ClassNotFoundException {
737            superfield = s.readInt();
738        }
739
740        private void writeObject(java.io.ObjectOutputStream s)
741                throws IOException {
742            s.writeInt(superfield);
743        }
744
745        public void x10() {
746            superfield *= 10;
747        }
748    }
749
750    // -----------------------------------------------------------------------------------
751    private static class SuperclassTest2 extends SuperclassTest {
752        int subfield = 5;
753
754        public SuperclassTest2() {
755            subfield = 50;
756        }
757
758        public boolean equals(Object o) {
759            if (o instanceof SuperclassTest2)
760                if (subfield == ((SuperclassTest2) o).subfield)
761                    return super.equals(o);
762            return false;
763        }
764
765        private void readObject(java.io.ObjectInputStream s)
766                throws IOException, ClassNotFoundException {
767            subfield = s.readInt();
768        }
769
770        private void writeObject(java.io.ObjectOutputStream s)
771                throws IOException {
772            s.writeInt(subfield);
773        }
774
775        public void x10() {
776            subfield *= 10;
777            super.x10();
778        }
779    }
780
781    // -----------------------------------------------------------------------------------
782    private static class SyntheticFieldTest implements java.io.Serializable {
783        public boolean equals(Object obj) {
784            /*
785                * This method is not answering it the objs is equal. It is
786                * answering if the vars have the value that it have to have after
787                * dumping and loading
788                */
789            if (obj == null)
790                return false;
791            return obj instanceof SyntheticFieldTest;
792        }
793
794        public int hashCode() {
795            // Insert code to generate a hash code for the receiver here.
796            // This implementation forwards the message to super. You may
797            // replace or supplement this.
798            // NOTE: if two objects are equal (equals Object) returns true) they
799            // must have the same hash code
800            Class[] c = { String.class }; // *** synthetic field
801            return super.hashCode();
802        }
803    }
804
805    public SerializationStressTest2(String name) {
806        super(name);
807    }
808
809    public void test_18_41_writeObject() {
810        // Test for method void
811        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
812
813        Object objToSave = null;
814        Object objLoaded;
815
816        try {
817            java.io.IOException ex = new java.io.WriteAbortedException(FOO,
818                    null);
819            objToSave = ex;
820            if (DEBUG)
821                System.out.println("Obj = " + objToSave);
822            objLoaded = dumpAndReload(objToSave);
823            // Has to be able to save/load an exception
824            assertTrue(MSG_TEST_FAILED + objToSave, true);
825
826        } catch (IOException e) {
827            fail("IOException serializing " + objToSave + " : "
828                    + e.getMessage());
829        } catch (ClassNotFoundException e) {
830            fail("ClassNotFoundException reading Object type : "
831                    + e.getMessage());
832        } catch (Error err) {
833            System.out.println("Error when obj = " + objToSave);
834            // err.printStackTrace();
835            throw err;
836        }
837    }
838
839    public void test_18_42_writeObject() {
840        // Test for method void
841        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
842
843        Object objToSave = null;
844        Object objLoaded;
845
846        try {
847            WithUnmatchingSerialPersistentFields spf = new WithUnmatchingSerialPersistentFields();
848            objToSave = spf;
849            if (DEBUG)
850                System.out.println("Obj = " + objToSave);
851            boolean causedException = false;
852            try {
853                objLoaded = dumpAndReload(objToSave);
854            } catch (InvalidClassException ce) {
855                causedException = true;
856            }
857            assertTrue("serialPersistentFields do not match real fields",
858                    causedException);
859
860        } catch (IOException e) {
861            fail("IOException serializing " + objToSave + " : "
862                    + e.getMessage());
863        } catch (ClassNotFoundException e) {
864            fail("ClassNotFoundException reading Object type : "
865                    + e.getMessage());
866        } catch (Error err) {
867            System.out.println("Error when obj = " + objToSave);
868            // err.printStackTrace();
869            throw err;
870        }
871    }
872
873    public void test_18_43_writeObject() {
874        // Test for method void
875        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
876
877        Object objToSave = null;
878        Object objLoaded;
879
880        try {
881            WithMatchingSerialPersistentFields spf = new WithMatchingSerialPersistentFields();
882            spf.anInstanceVar = FOO;
883            objToSave = spf;
884            if (DEBUG)
885                System.out.println("Obj = " + objToSave);
886            objLoaded = dumpAndReload(objToSave);
887            assertTrue(
888                    "serialPersistentFields do not work properly in this implementation",
889                    FOO
890                            .equals(((WithMatchingSerialPersistentFields) objLoaded).anInstanceVar));
891
892        } catch (IOException e) {
893            fail("IOException serializing " + objToSave + " : "
894                    + e.getMessage());
895        } catch (ClassNotFoundException e) {
896            fail("ClassNotFoundException reading Object type : "
897                    + e.getMessage());
898        } catch (Error err) {
899            System.out.println("Error when obj = " + objToSave);
900            // err.printStackTrace();
901            throw err;
902        }
903    }
904
905    public void test_18_44_writeObject() {
906        // Test for method void
907        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
908
909        Object objToSave = null;
910        Object objLoaded;
911
912        try {
913            SerialPersistentFields spf = new SerialPersistentFields();
914            final int CONST = -500;
915            spf.anInstanceVar = CONST;
916            objToSave = spf;
917            if (DEBUG)
918                System.out.println("Obj = " + objToSave);
919            objLoaded = dumpAndReload(objToSave);
920            assertTrue(
921                    "serialPersistentFields do not work properly in this implementation",
922                    ((SerialPersistentFields) objLoaded).anInstanceVar == CONST);
923
924        } catch (IOException e) {
925            fail("IOException serializing " + objToSave + " : "
926                    + e.getMessage());
927        } catch (ClassNotFoundException e) {
928            fail("ClassNotFoundException reading Object type : "
929                    + e.getMessage());
930        } catch (Error err) {
931            System.out.println("Error when obj = " + objToSave);
932            // err.printStackTrace();
933            throw err;
934        }
935    }
936
937    public void test_18_45_writeObject() {
938        // Test for method void
939        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
940
941        Object objToSave = null;
942        Object objLoaded;
943
944        try {
945            WriteFieldsWithoutFetchingPutFields spf = new WriteFieldsWithoutFetchingPutFields();
946            objToSave = spf;
947            if (DEBUG)
948                System.out.println("Obj = " + objToSave);
949            boolean causedException = false;
950            try {
951                objLoaded = dumpAndReload(objToSave);
952            } catch (NotActiveException ce) {
953                causedException = true;
954            }
955            assertTrue("WriteFieldsWithoutFetchingPutFields", causedException);
956
957        } catch (IOException e) {
958            fail("IOException serializing " + objToSave + " : "
959                    + e.getMessage());
960        } catch (ClassNotFoundException e) {
961            fail("ClassNotFoundException reading Object type : "
962                    + e.getMessage());
963        } catch (Error err) {
964            System.out.println("Error when obj = " + objToSave);
965            // err.printStackTrace();
966            throw err;
967        }
968    }
969
970    public void test_18_46_writeObject() {
971        // Test for method void
972        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
973
974        Object objToSave = null;
975        Object objLoaded;
976
977        try {
978            objToSave = SerialPersistentFields.class; // Test for 1FA7TA6
979            if (DEBUG)
980                System.out.println("Obj = " + objToSave);
981            objLoaded = dumpAndReload(objToSave);
982            // Has to be able to save/load an exception
983            assertTrue(MSG_TEST_FAILED + objToSave, true);
984
985        } catch (IOException e) {
986            fail("IOException serializing " + objToSave + " : "
987                    + e.getMessage());
988        } catch (ClassNotFoundException e) {
989            fail("ClassNotFoundException reading Object type : "
990                    + e.getMessage());
991        } catch (Error err) {
992            System.out.println("Error when obj = " + objToSave);
993            // err.printStackTrace();
994            throw err;
995        }
996    }
997
998    public void test_18_47_writeObject() {
999        // Test for method void
1000        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1001
1002        Object objToSave = null;
1003        Object objLoaded;
1004
1005        try {
1006            objToSave = ObjectStreamClass.lookup(SerialPersistentFields.class); // Test
1007            // for
1008            // 1FA7TA6
1009            if (DEBUG)
1010                System.out.println("Obj = " + objToSave);
1011            objLoaded = dumpAndReload(objToSave);
1012            // Has to be able to save/load an exception
1013            assertTrue(MSG_TEST_FAILED + objToSave, true);
1014
1015        } catch (IOException e) {
1016            fail("IOException serializing " + objToSave + " : "
1017                    + e.getMessage());
1018        } catch (ClassNotFoundException e) {
1019            fail("ClassNotFoundException reading Object type : "
1020                    + e.getMessage());
1021        } catch (Error err) {
1022            System.out.println("Error when obj = " + objToSave);
1023            // err.printStackTrace();
1024            throw err;
1025        }
1026    }
1027
1028    public void test_18_48_writeObject() {
1029        // Test for method void
1030        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1031
1032        Object objToSave = null;
1033        Object objLoaded;
1034
1035        try {
1036            SerialPersistentFieldsWithoutField spf = new SerialPersistentFieldsWithoutField();
1037            final int CONST = -500;
1038            spf.anInstanceVar = CONST;
1039            objToSave = spf;
1040            if (DEBUG)
1041                System.out.println("Obj = " + objToSave);
1042            objLoaded = dumpAndReload(objToSave);
1043            assertTrue(
1044                    "serialPersistentFields do not work properly in this implementation",
1045                    ((SerialPersistentFieldsWithoutField) objLoaded).anInstanceVar != CONST);
1046
1047        } catch (IOException e) {
1048            fail("IOException serializing " + objToSave + " : "
1049                    + e.getMessage());
1050        } catch (ClassNotFoundException e) {
1051            fail("ClassNotFoundException reading Object type : "
1052                    + e.getMessage());
1053        } catch (Error err) {
1054            System.out.println("Error when obj = " + objToSave);
1055            // err.printStackTrace();
1056            throw err;
1057        }
1058    }
1059
1060    public void test_18_51_writeObject() {
1061        // Test for method void
1062        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1063
1064        Object objToSave = null;
1065        Object objLoaded;
1066
1067        try {
1068
1069            ReadWriteObjectAndPrimitiveData readWrite = new ReadWriteObjectAndPrimitiveData();
1070            objToSave = readWrite;
1071            if (DEBUG)
1072                System.out.println("Obj = " + objToSave);
1073            objLoaded = dumpAndReload(objToSave);
1074            // has to have called the writeObject on the instance to dump
1075            assertTrue(MSG_TEST_FAILED + objToSave, readWrite.calledWriteObject);
1076            // has to have called the readObject on the instance loaded
1077            assertTrue(
1078                    MSG_TEST_FAILED + objToSave,
1079                    ((ReadWriteObjectAndPrimitiveData) objLoaded).calledReadObject);
1080
1081        } catch (IOException e) {
1082            fail("IOException serializing " + objToSave + " : "
1083                    + e.getMessage());
1084        } catch (ClassNotFoundException e) {
1085            fail("ClassNotFoundException reading Object type : "
1086                    + e.getMessage());
1087        } catch (Error err) {
1088            System.out.println("Error when obj = " + objToSave);
1089            // err.printStackTrace();
1090            throw err;
1091        }
1092    }
1093
1094    public void test_18_52_writeObject() {
1095        // Test for method void
1096        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1097
1098        Object objToSave = null;
1099        Object objLoaded;
1100
1101        try {
1102
1103            ArrayList list = new ArrayList<String>(Arrays.asList(new String[] { "a",
1104                    "list", "of", "strings" }));
1105            objToSave = list;
1106            if (DEBUG)
1107                System.out.println("Obj = " + objToSave);
1108            objLoaded = dumpAndReload(objToSave);
1109            // Has to have worked
1110            assertTrue(MSG_TEST_FAILED + objToSave, true);
1111
1112        } catch (IOException e) {
1113            fail("IOException serializing " + objToSave + " : "
1114                    + e.getMessage());
1115        } catch (ClassNotFoundException e) {
1116            fail("ClassNotFoundException reading Object type : "
1117                    + e.getMessage());
1118        } catch (Error err) {
1119            System.out.println("Error when obj = " + objToSave);
1120            // err.printStackTrace();
1121            throw err;
1122        }
1123    }
1124
1125    public void test_18_53_writeObject() {
1126        // Test for method void
1127        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1128
1129        Object objToSave = null;
1130        Object objLoaded;
1131
1132        try {
1133
1134            objToSave = Locale.CHINESE;
1135            if (DEBUG)
1136                System.out.println("Obj = " + objToSave);
1137            objLoaded = dumpAndReload(objToSave);
1138            // Has to have worked
1139            assertTrue(MSG_TEST_FAILED + objToSave, true);
1140
1141        } catch (IOException e) {
1142            fail("IOException serializing " + objToSave + " : "
1143                    + e.getMessage());
1144        } catch (ClassNotFoundException e) {
1145            fail("ClassNotFoundException reading Object type : "
1146                    + e.getMessage());
1147        } catch (Error err) {
1148            System.out.println("Error when obj = " + objToSave);
1149            // err.printStackTrace();
1150            throw err;
1151        }
1152    }
1153
1154    public void test_OptionalDataNotRead() {
1155        // Test for method void
1156        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1157
1158        Object objToSave = null;
1159        Object objLoaded;
1160
1161        try {
1162            OptionalDataNotRead test = new OptionalDataNotRead();
1163            // Have to save an object after the one above, and when we read it,
1164            // it cannot be a byte[]
1165            Date now = new Date();
1166            Object[] twoObjects = new Object[2];
1167            twoObjects[0] = test;
1168            twoObjects[1] = now;
1169            objToSave = twoObjects;
1170            if (DEBUG)
1171                System.out.println("Obj = " + objToSave);
1172            objLoaded = dumpAndReload(objToSave);
1173            // Has to have worked
1174            Object[] twoLoadedObjects = (Object[]) objLoaded;
1175            assertTrue(MSG_TEST_FAILED + objToSave, twoLoadedObjects[0]
1176                    .getClass() == OptionalDataNotRead.class);
1177            assertTrue(MSG_TEST_FAILED + objToSave, twoLoadedObjects[1]
1178                    .getClass() == Date.class);
1179
1180        } catch (IOException e) {
1181            fail("IOException serializing " + objToSave + " : "
1182                    + e.getMessage());
1183        } catch (ClassNotFoundException e) {
1184            fail("ClassNotFoundException reading Object type : "
1185                    + e.getMessage());
1186        } catch (Error err) {
1187            System.out.println("Error when obj = " + objToSave);
1188            // err.printStackTrace();
1189            throw err;
1190        }
1191    }
1192
1193    public void test_18_55_writeObject() {
1194        // Test for method void
1195        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1196
1197        Object objToSave = null;
1198        Object objLoaded;
1199
1200        try {
1201            Object[] threeObjects = new Object[3];
1202            threeObjects[0] = new Integer(2);
1203            threeObjects[1] = Date.class;
1204            threeObjects[2] = threeObjects[0]; // has to be the same
1205            objToSave = threeObjects;
1206            if (DEBUG)
1207                System.out.println("Obj = " + objToSave);
1208            objLoaded = dumpAndReload(objToSave);
1209            // Has to have worked
1210            Object[] threeLoadedObjects = (Object[]) objLoaded;
1211            assertTrue(MSG_TEST_FAILED + objToSave, threeLoadedObjects[0]
1212                    .getClass() == Integer.class);
1213            assertTrue(MSG_TEST_FAILED + objToSave,
1214                    threeLoadedObjects[1] == Date.class);
1215            assertTrue(MSG_TEST_FAILED + objToSave,
1216                    threeLoadedObjects[0] == threeLoadedObjects[2]);
1217
1218        } catch (IOException e) {
1219            fail("IOException serializing " + objToSave + " : "
1220                    + e.getMessage());
1221        } catch (ClassNotFoundException e) {
1222            fail("ClassNotFoundException reading Object type : "
1223                    + e.getMessage());
1224        } catch (Error err) {
1225            System.out.println("Error when obj = " + objToSave);
1226            // err.printStackTrace();
1227            throw err;
1228        }
1229    }
1230
1231    public void test_18_56_writeObject() {
1232        // Test for method void
1233        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1234
1235        Object objToSave = null;
1236        Object objLoaded;
1237
1238        try {
1239            // Test for 1FD24BY
1240            NestedPutField test = new NestedPutField();
1241            objToSave = test;
1242            if (DEBUG)
1243                System.out.println("Obj = " + objToSave);
1244            objLoaded = dumpAndReload(objToSave);
1245            // Has to have worked
1246            assertNotNull(MSG_TEST_FAILED + objToSave,
1247                    ((NestedPutField) objLoaded).field1);
1248
1249        } catch (IOException e) {
1250            fail("IOException serializing " + objToSave + " : "
1251                    + e.getMessage());
1252        } catch (ClassNotFoundException e) {
1253            fail("ClassNotFoundException reading Object type : "
1254                    + e.getMessage());
1255        } catch (Error err) {
1256            System.out.println("Error when obj = " + objToSave);
1257            // err.printStackTrace();
1258            throw err;
1259        }
1260    }
1261
1262    public void test_18_57_writeObject() {
1263        // Test for method void
1264        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1265
1266        Object objToSave = null;
1267        Object objLoaded;
1268
1269        try {
1270            ByteArrayOutputStream out;
1271            StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping;
1272
1273            out = new ByteArrayOutputStream();
1274            streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping(out);
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(streamBasedReplacementWhenDumping.calledClassReplacement);
1281        } catch (IOException e) {
1282            fail("Exception serializing " + objToSave + "\t->"
1283                    + e.toString());
1284        } catch (Error err) {
1285            System.out.println("Error " + err + " when obj = " + objToSave);
1286            throw err;
1287        }
1288    }
1289
1290    public void test_18_58_writeObject() {
1291        // Test for method void
1292        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1293
1294        Object objToSave = null;
1295        Object objLoaded;
1296
1297        try {
1298            ByteArrayOutputStream out;
1299            StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping;
1300
1301            out = new ByteArrayOutputStream();
1302            streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping(
1303                    out);
1304
1305            objToSave = ObjectStreamClass.lookup(FOO.getClass());
1306            if (DEBUG)
1307                System.out.println("Obj = " + objToSave);
1308            streamBasedReplacementWhenDumping.writeObject(objToSave);
1309            // Has to have run the replacement method
1310            assertTrue(streamBasedReplacementWhenDumping.calledObjectStreamClassReplacement);
1311        } catch (IOException e) {
1312            fail("Exception serializing " + objToSave + "\t->"
1313                    + e.toString());
1314        } catch (Error err) {
1315            System.out.println("Error " + err + " when obj = " + objToSave);
1316            throw err;
1317        }
1318    }
1319
1320    public void test_18_59_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            ByteArrayOutputStream out;
1329            StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping;
1330
1331            out = new ByteArrayOutputStream();
1332            streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping(
1333                    out);
1334            ;
1335            objToSave = new int[3];
1336            if (DEBUG)
1337                System.out.println("Obj = " + objToSave);
1338            streamBasedReplacementWhenDumping.writeObject(objToSave);
1339            // Has to have run the replacement method
1340            assertTrue("DId not execute replacement when it should: "
1341                    + objToSave,
1342                    streamBasedReplacementWhenDumping.calledArrayReplacement);
1343
1344        } catch (IOException e) {
1345            fail("Exception serializing " + objToSave + "\t->"
1346                    + e.toString());
1347        } catch (Error err) {
1348            System.out.println("Error " + err + " when obj = " + objToSave);
1349            throw err;
1350        }
1351    }
1352
1353    public void test_18_60_writeObject() {
1354        // Test for method void
1355        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1356
1357        Object objToSave = null;
1358        Object objLoaded;
1359
1360        try {
1361            ByteArrayOutputStream out;
1362            StreamBasedReplacementWhenDumping streamBasedReplacementWhenDumping;
1363
1364            out = new ByteArrayOutputStream();
1365            streamBasedReplacementWhenDumping = new StreamBasedReplacementWhenDumping(
1366                    out);
1367            ;
1368            objToSave = FOO;
1369            if (DEBUG)
1370                System.out.println("Obj = " + objToSave);
1371            streamBasedReplacementWhenDumping.writeObject(objToSave);
1372            // Has to have run the replacement method
1373            assertTrue("Did not execute replacement when it should: "
1374                    + objToSave,
1375                    streamBasedReplacementWhenDumping.calledStringReplacement);
1376
1377        } catch (IOException e) {
1378            fail("Exception serializing " + objToSave + "\t->"
1379                    + e.toString());
1380        } catch (Error err) {
1381            System.out.println("Error " + err + " when obj = " + objToSave);
1382            throw err;
1383        }
1384    }
1385
1386    public void test_18_61_writeObject() {
1387        // Test for method void
1388        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1389
1390        Object objToSave = null;
1391        Object objLoaded;
1392
1393        try {
1394            ArrayOfSerializable test = new ArrayOfSerializable();
1395            objToSave = test;
1396            if (DEBUG)
1397                System.out.println("Obj = " + objToSave);
1398            objLoaded = dumpAndReload(objToSave);
1399            // Has to have worked
1400            assertTrue(MSG_TEST_FAILED + objToSave, true);
1401
1402        } catch (IOException e) {
1403            fail("IOException serializing " + objToSave + " : "
1404                    + e.getMessage());
1405        } catch (ClassNotFoundException e) {
1406            fail("ClassNotFoundException reading Object type : "
1407                    + e.getMessage());
1408        } catch (Error err) {
1409            System.out.println("Error when obj = " + objToSave);
1410            // err.printStackTrace();
1411            throw err;
1412        }
1413    }
1414
1415    public void test_18_62_writeObject() {
1416        // Test for method void
1417        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1418
1419        Object objToSave = null;
1420        Object objLoaded;
1421
1422        try {
1423            ClassSubClassTest1 test = new ClassSubClassTest1(
1424                    "SuperInitialString", "SubInitialString");
1425            objToSave = test;
1426            if (DEBUG)
1427                System.out.println("Obj = " + objToSave);
1428            objLoaded = dumpAndReload(objToSave);
1429            // Has to have worked
1430            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
1431
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_63_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            ConstructorTestC test = new ConstructorTestC();
1454            objToSave = test;
1455            if (DEBUG)
1456                System.out.println("Obj = " + objToSave);
1457            objLoaded = dumpAndReload(objToSave);
1458            // Has to have worked
1459            assertTrue(MSG_TEST_FAILED + objToSave, test.verify(objLoaded));
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_64_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            HashCodeTest test = new HashCodeTest();
1483            objToSave = test;
1484            if (DEBUG)
1485                System.out.println("Obj = " + objToSave);
1486            objLoaded = dumpAndReload(objToSave);
1487            // Has to have worked
1488            assertTrue(MSG_TEST_FAILED + objToSave,
1489                    !((HashCodeTest) objLoaded).serializationUsesHashCode);
1490
1491        } catch (IOException e) {
1492            fail("IOException serializing " + objToSave + " : "
1493                    + e.getMessage());
1494        } catch (ClassNotFoundException e) {
1495            fail("ClassNotFoundException reading Object type : "
1496                    + e.getMessage());
1497        } catch (Error err) {
1498            System.out.println("Error when obj = " + objToSave);
1499            // err.printStackTrace();
1500            throw err;
1501        }
1502    }
1503
1504    public void test_18_65_writeObject() {
1505        // Test for method void
1506        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1507
1508        Object objToSave = null;
1509        Object objLoaded;
1510
1511        try {
1512            InitializerFieldsTest test = new InitializerFieldsTest();
1513            test.toBeSerialized = "serializing";
1514            InitializerFieldsTest.toBeNotSerialized = "It should not have this value after loaded from a File";
1515            InitializerFieldsTest.toBeNotSerialized2 = "Good-This is the rigth value.";
1516
1517            objToSave = test;
1518            if (DEBUG)
1519                System.out.println("Obj = " + objToSave);
1520            dump(objToSave);
1521            InitializerFieldsTest.toBeNotSerialized = new String(
1522                    InitializerFieldsTest.toBeNotSerialized2);
1523            objLoaded = reload();
1524
1525            // Has to have worked
1526            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1527
1528        } catch (IOException e) {
1529            fail("IOException serializing " + objToSave + " : "
1530                    + e.getMessage());
1531        } catch (ClassNotFoundException e) {
1532            fail("ClassNotFoundException reading Object type : "
1533                    + e.getMessage());
1534        } catch (Error err) {
1535            System.out.println("Error when obj = " + objToSave);
1536            // err.printStackTrace();
1537            throw err;
1538        }
1539    }
1540
1541    public void test_18_66_writeObject() {
1542        // Test for method void
1543        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1544
1545        Object objToSave = null;
1546        Object objLoaded;
1547
1548        try {
1549            InitializerFieldsTest2 test = new InitializerFieldsTest2();
1550            test.toBeSerialized = "serializing";
1551            test.toBeSerialized3 = "serializing3";
1552            test.toBeSerialized4 = "serializing4";
1553            test.toBeSerialized5 = "serializing5";
1554            InitializerFieldsTest2.toBeNotSerialized = "It should not have this value after loaded from a File";
1555            InitializerFieldsTest2.toBeNotSerialized2 = "Good-This is the rigth value.";
1556
1557            objToSave = test;
1558            if (DEBUG)
1559                System.out.println("Obj = " + objToSave);
1560            dump(objToSave);
1561            InitializerFieldsTest2.toBeNotSerialized = new String(
1562                    InitializerFieldsTest2.toBeNotSerialized2);
1563            objLoaded = reload();
1564
1565            // Has to have worked
1566            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1567
1568        } catch (IOException e) {
1569            fail("IOException serializing " + objToSave + " : "
1570                    + e.getMessage());
1571        } catch (ClassNotFoundException e) {
1572            fail("ClassNotFoundException reading Object type : "
1573                    + e.getMessage());
1574        } catch (Error err) {
1575            System.out.println("Error when obj = " + objToSave);
1576            // err.printStackTrace();
1577            throw err;
1578        }
1579    }
1580
1581    public void test_18_67_writeObject() {
1582        // Test for method void
1583        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1584
1585        Object objToSave = null;
1586        Object objLoaded;
1587
1588        try {
1589            InitializerFieldsTest3 test = new InitializerFieldsTest3();
1590            test.toBeSerialized = "serializing";
1591            test.toBeSerialized3 = "serializing3";
1592            test.toBeSerialized4 = "serializing4";
1593            test.toBeSerialized5 = "serializing5";
1594            InitializerFieldsTest2.toBeNotSerialized = "It should not have this value after loaded from a File";
1595            InitializerFieldsTest2.toBeNotSerialized2 = "Good-This is the rigth value.";
1596            test.sub_toBeSerialized = "serializingSub";
1597            test.sub_toBeSerialized3 = "serializing3sub";
1598            test.sub_toBeSerialized4 = "serializing4sub";
1599            test.sub_toBeSerialized5 = "serializing5sub";
1600            InitializerFieldsTest3.sub_toBeNotSerialized = "(Subclass) It should not have this value after loaded from a File";
1601            InitializerFieldsTest3.sub_toBeNotSerialized2 = "(Subclass) Good-This is the rigth value.";
1602            // Before dumping the two static vars are differents.
1603            // After dumping the value of toBeNotSerialized2 is put in
1604            // toBeNotSerialized
1605            // After loading it must be the same.
1606            objToSave = test;
1607            if (DEBUG)
1608                System.out.println("Obj = " + objToSave);
1609            dump(objToSave);
1610            InitializerFieldsTest2.toBeNotSerialized = new String(
1611                    InitializerFieldsTest2.toBeNotSerialized2);
1612            InitializerFieldsTest3.sub_toBeNotSerialized = new String(
1613                    InitializerFieldsTest3.sub_toBeNotSerialized2);
1614            objLoaded = reload();
1615
1616            // Has to have worked
1617            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1618
1619        } catch (IOException e) {
1620            fail("IOException serializing " + objToSave + " : "
1621                    + e.getMessage());
1622        } catch (ClassNotFoundException e) {
1623            fail("ClassNotFoundException reading Object type : "
1624                    + e.getMessage());
1625        } catch (Error err) {
1626            System.out.println("Error when obj = " + objToSave);
1627            // err.printStackTrace();
1628            throw err;
1629        }
1630    }
1631
1632    public void test_DeepNesting() {
1633        // Test for method void
1634        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1635
1636        Object objToSave = null;
1637        Object objLoaded;
1638
1639        try {
1640            DeepNesting test = new DeepNesting(25);
1641            objToSave = test;
1642            if (DEBUG)
1643                System.out.println("Obj = " + objToSave);
1644            objLoaded = dumpAndReload(objToSave);
1645
1646            // Has to have worked
1647            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1648
1649        } catch (IOException e) {
1650            fail("IOException serializing " + objToSave + " : "
1651                    + e.getMessage());
1652        } catch (ClassNotFoundException e) {
1653            fail("ClassNotFoundException reading Object type : "
1654                    + e.getMessage());
1655        } catch (Error err) {
1656            // err.printStackTrace();
1657            System.out.println("Error " + err + " when obj = " + objToSave);
1658            throw err;
1659        }
1660    }
1661
1662    public void test_DeepNestingWithWriteObject() {
1663        // Test for method void
1664        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1665
1666        Object objToSave = null;
1667        Object objLoaded;
1668
1669        try {
1670            DeepNestingWithWriteObject test = new DeepNestingWithWriteObject(10);
1671            objToSave = test;
1672            if (DEBUG)
1673                System.out.println("Obj = " + objToSave);
1674            objLoaded = dumpAndReload(objToSave);
1675
1676            // Has to have worked
1677            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1678
1679        } catch (IOException e) {
1680            fail("IOException serializing " + objToSave + " : "
1681                    + e.getMessage());
1682        } catch (ClassNotFoundException e) {
1683            fail("ClassNotFoundException reading Object type : "
1684                    + e.getMessage());
1685        } catch (Error err) {
1686            // err.printStackTrace();
1687            System.out.println("Error " + err + " when obj = " + objToSave);
1688            throw err;
1689        }
1690    }
1691
1692    public void test_18_69_writeObject() {
1693        // Test for method void
1694        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1695
1696        Object objToSave = null;
1697        Object objLoaded;
1698
1699        try {
1700            NonPublicClassTest test = new NonPublicClassTest();
1701            test.x10();
1702            objToSave = test;
1703            if (DEBUG)
1704                System.out.println("Obj = " + objToSave);
1705            objLoaded = dumpAndReload(objToSave);
1706
1707            // Has to have worked
1708            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
1709
1710        } catch (IOException e) {
1711            fail("IOException serializing " + objToSave + " : "
1712                    + e.getMessage());
1713        } catch (ClassNotFoundException e) {
1714            fail("ClassNotFoundException reading Object type : "
1715                    + e.getMessage());
1716        } catch (Error err) {
1717            System.out.println("Error when obj = " + objToSave);
1718            // err.printStackTrace();
1719            throw err;
1720        }
1721    }
1722
1723    public void test_18_70_writeObject() {
1724        // Test for method void
1725        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1726
1727        Object objToSave = null;
1728        Object objLoaded;
1729
1730        try {
1731            int[] test = new int[1];
1732            int intValue = 0;
1733            test[0] = intValue;
1734            objToSave = test;
1735            if (DEBUG)
1736                System.out.println("Obj = " + objToSave);
1737            objLoaded = dumpAndReload(objToSave);
1738
1739            // Has to have worked
1740            assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(test,
1741                    (int[]) objLoaded));
1742
1743        } catch (IOException e) {
1744            fail("IOException serializing " + objToSave + " : "
1745                    + e.getMessage());
1746        } catch (ClassNotFoundException e) {
1747            fail("ClassNotFoundException reading Object type : "
1748                    + e.getMessage());
1749        } catch (Error err) {
1750            System.out.println("Error when obj = " + objToSave);
1751            // err.printStackTrace();
1752            throw err;
1753        }
1754    }
1755
1756    public void test_18_71_writeObject() {
1757        // Test for method void
1758        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1759
1760        Object objToSave = null;
1761        Object objLoaded;
1762
1763        try {
1764            int i, j, maxJ = 3, maxI = 200;
1765            byte[][] obj = new byte[maxJ][maxI];
1766            for (j = 0; j < maxJ; j++) {
1767                for (i = 0; i < maxI; i++)
1768                    obj[j][i] = (byte) (i - 100);
1769            }
1770            objToSave = obj;
1771            if (DEBUG)
1772                System.out.println("Obj = " + objToSave);
1773            objLoaded = dumpAndReload(objToSave);
1774            byte[][] toCompare = (byte[][]) objLoaded;
1775
1776            boolean ok = true;
1777            // Has to have worked
1778            for (j = 0; j < maxJ; j++) {
1779                for (i = 0; i < maxI; i++)
1780                    if (obj[j][i] != toCompare[j][i]) {
1781                        ok = false;
1782                        break;
1783                    }
1784            }
1785
1786            assertTrue(MSG_TEST_FAILED + objToSave, ok);
1787
1788        } catch (IOException e) {
1789            fail("IOException serializing " + objToSave + " : "
1790                    + e.getMessage());
1791        } catch (ClassNotFoundException e) {
1792            fail("ClassNotFoundException reading Object type : "
1793                    + e.getMessage());
1794        } catch (Error err) {
1795            System.out.println("Error when obj = " + objToSave);
1796            // err.printStackTrace();
1797            throw err;
1798        }
1799    }
1800
1801    public void test_18_72_writeObject() {
1802        // Test for method void
1803        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1804
1805        Object objToSave = null;
1806        Object objLoaded;
1807
1808        try {
1809            int i, j, maxJ = 3, maxI = 200;
1810            int[][] obj = new int[maxJ][maxI];
1811            for (j = 0; j < maxJ; j++) {
1812                for (i = 0; i < maxI; i++)
1813                    obj[j][i] = (i - 100);
1814            }
1815            objToSave = obj;
1816            if (DEBUG)
1817                System.out.println("Obj = " + objToSave);
1818            objLoaded = dumpAndReload(objToSave);
1819            int[][] toCompare = (int[][]) objLoaded;
1820
1821            boolean ok = true;
1822            // Has to have worked
1823            for (j = 0; j < maxJ; j++) {
1824                for (i = 0; i < maxI; i++)
1825                    if (obj[j][i] != toCompare[j][i]) {
1826                        ok = false;
1827                        break;
1828                    }
1829            }
1830
1831            assertTrue(MSG_TEST_FAILED + objToSave, ok);
1832
1833        } catch (IOException e) {
1834            fail("IOException serializing " + objToSave + " : "
1835                    + e.getMessage());
1836        } catch (ClassNotFoundException e) {
1837            fail("ClassNotFoundException reading Object type : "
1838                    + e.getMessage());
1839        } catch (Error err) {
1840            System.out.println("Error when obj = " + objToSave);
1841            // err.printStackTrace();
1842            throw err;
1843        }
1844    }
1845
1846    public void test_18_73_writeObject() {
1847        // Test for method void
1848        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1849
1850        Object objToSave = null;
1851        Object objLoaded;
1852
1853        try {
1854            String org = "abcdefghijklmnopqrstuvxyz1234567890abcdefghijklmnopqrstuvxyz1234567890";
1855            int i, j, maxJ = 3, maxI = 70;
1856            String[][] obj = new String[maxJ][maxI];
1857            for (j = 0; j < maxJ; j++) {
1858                for (i = 0; i < maxI; i++)
1859                    obj[j][i] = org.substring(0, i);
1860            }
1861            objToSave = obj;
1862            if (DEBUG)
1863                System.out.println("Obj = " + objToSave);
1864            objLoaded = dumpAndReload(objToSave);
1865            String[][] toCompare = (String[][]) objLoaded;
1866
1867            boolean ok = true;
1868            // Has to have worked
1869            for (j = 0; j < maxJ; j++) {
1870                for (i = 0; i < maxI; i++)
1871                    if (!obj[j][i].equals(toCompare[j][i])) {
1872                        ok = false;
1873                        break;
1874                    }
1875            }
1876
1877            assertTrue(MSG_TEST_FAILED + objToSave, ok);
1878
1879        } catch (IOException e) {
1880            fail("IOException serializing " + objToSave + " : "
1881                    + e.getMessage());
1882        } catch (ClassNotFoundException e) {
1883            fail("ClassNotFoundException reading Object type : "
1884                    + e.getMessage());
1885        } catch (Error err) {
1886            System.out.println("Error when obj = " + objToSave);
1887            // err.printStackTrace();
1888            throw err;
1889        }
1890    }
1891
1892    public void test_18_74_writeObject() {
1893        // Test for method void
1894        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1895
1896        Object objToSave = null;
1897        Object objLoaded;
1898
1899        try {
1900            SameInstVarNameSubClass test = new SameInstVarNameSubClass(100);
1901            objToSave = test;
1902            if (DEBUG)
1903                System.out.println("Obj = " + objToSave);
1904            objLoaded = dumpAndReload(objToSave);
1905            // Has to have worked
1906            assertTrue(MSG_TEST_FAILED + objToSave,
1907                    ((SameInstVarNameSubClass) objLoaded).foo == 100);
1908
1909        } catch (IOException e) {
1910            fail("IOException serializing " + objToSave + " : "
1911                    + e.getMessage());
1912        } catch (ClassNotFoundException e) {
1913            fail("ClassNotFoundException reading Object type : "
1914                    + e.getMessage());
1915        } catch (Error err) {
1916            System.out.println("Error when obj = " + objToSave);
1917            // err.printStackTrace();
1918            throw err;
1919        }
1920    }
1921
1922    public void test_18_75_writeObject() {
1923        // Test for method void
1924        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1925
1926        Object objToSave = null;
1927        Object objLoaded;
1928
1929        try {
1930            SInterfaceTest test = new SInterfaceTest();
1931            objToSave = test;
1932            if (DEBUG)
1933                System.out.println("Obj = " + objToSave);
1934            objLoaded = dumpAndReload(objToSave);
1935            // Has to have worked
1936            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
1937
1938        } catch (IOException e) {
1939            fail("IOException serializing " + objToSave + " : "
1940                    + e.getMessage());
1941        } catch (ClassNotFoundException e) {
1942            fail("ClassNotFoundException reading Object type : "
1943                    + e.getMessage());
1944        } catch (Error err) {
1945            System.out.println("Error when obj = " + objToSave);
1946            // err.printStackTrace();
1947            throw err;
1948        }
1949    }
1950
1951    public void test_18_76_writeObject() {
1952        // Test for method void
1953        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1954
1955        Object objToSave = null;
1956        Object objLoaded;
1957
1958        try {
1959            SInterfaceTest2 test = new SInterfaceTest2();
1960            objToSave = test;
1961            if (DEBUG)
1962                System.out.println("Obj = " + objToSave);
1963            objLoaded = dumpAndReload(objToSave);
1964            // Has to have worked
1965            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
1966
1967        } catch (IOException e) {
1968            fail("IOException serializing " + objToSave + " : "
1969                    + e.getMessage());
1970        } catch (ClassNotFoundException e) {
1971            fail("ClassNotFoundException reading Object type : "
1972                    + e.getMessage());
1973        } catch (Error err) {
1974            System.out.println("Error when obj = " + objToSave);
1975            // err.printStackTrace();
1976            throw err;
1977        }
1978    }
1979
1980    public void test_18_77_writeObject() {
1981        // Test for method void
1982        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1983
1984        Object objToSave = null;
1985        Object objLoaded;
1986
1987        try {
1988            SuperclassTest test = new SuperclassTest();
1989            objToSave = test;
1990            if (DEBUG)
1991                System.out.println("Obj = " + objToSave);
1992            objLoaded = dumpAndReload(objToSave);
1993            // Has to have worked
1994            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
1995
1996        } catch (IOException e) {
1997            fail("IOException serializing " + objToSave + " : "
1998                    + e.getMessage());
1999        } catch (ClassNotFoundException e) {
2000            fail("ClassNotFoundException reading Object type : "
2001                    + e.getMessage());
2002        } catch (Error err) {
2003            System.out.println("Error when obj = " + objToSave);
2004            // err.printStackTrace();
2005            throw err;
2006        }
2007    }
2008
2009    public void test_18_78_writeObject() {
2010        // Test for method void
2011        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
2012
2013        Object objToSave = null;
2014        Object objLoaded;
2015
2016        try {
2017            SuperclassTest2 test = new SuperclassTest2();
2018            objToSave = test;
2019            if (DEBUG)
2020                System.out.println("Obj = " + objToSave);
2021            objLoaded = dumpAndReload(objToSave);
2022            // Has to have worked
2023            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
2024
2025        } catch (IOException e) {
2026            fail("IOException serializing " + objToSave + " : "
2027                    + e.getMessage());
2028        } catch (ClassNotFoundException e) {
2029            fail("ClassNotFoundException reading Object type : "
2030                    + e.getMessage());
2031        } catch (Error err) {
2032            System.out.println("Error when obj = " + objToSave);
2033            // err.printStackTrace();
2034            throw err;
2035        }
2036    }
2037
2038    public void test_18_79_writeObject() {
2039        // Test for method void
2040        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
2041
2042        Object objToSave = null;
2043        Object objLoaded;
2044
2045        try {
2046            SyntheticFieldTest test = new SyntheticFieldTest();
2047            objToSave = test;
2048            if (DEBUG)
2049                System.out.println("Obj = " + objToSave);
2050            objLoaded = dumpAndReload(objToSave);
2051            // Has to have worked
2052            assertTrue(MSG_TEST_FAILED + objToSave, test.equals(objLoaded));
2053
2054        } catch (IOException e) {
2055            fail("IOException serializing " + objToSave + " : "
2056                    + e.getMessage());
2057        } catch (ClassNotFoundException e) {
2058            fail("ClassNotFoundException reading Object type : "
2059                    + e.getMessage());
2060        } catch (Error err) {
2061            System.out.println("Error when obj = " + objToSave);
2062            // err.printStackTrace();
2063            throw err;
2064        }
2065    }
2066
2067    public void test_18_80_writeObject() {
2068        // Test for method void
2069        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
2070
2071        try {
2072            ByteArrayOutputStream out = new ByteArrayOutputStream();
2073            DataOutputStream dos = new DataOutputStream(out);
2074            new ObjectOutputStream(dos); // just to make sure we get a header
2075            dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
2076            int length = 99;
2077            dos.writeByte(length);
2078            for (int i = 0; i < length; i++) {
2079                dos.writeByte(0); // actual value does not matter
2080            }
2081            dos.flush();
2082            int lengthRead = 0;
2083            try {
2084                ObjectInputStream ois = new ObjectInputStream(
2085                        new ByteArrayInputStream(out.toByteArray()));
2086                Object obj = ois.readObject();
2087            } catch (OptionalDataException e) {
2088                lengthRead = e.length;
2089            }
2090            assertTrue("Did not throw exception with optional data size ",
2091                    length == lengthRead);
2092        } catch (ClassNotFoundException e) {
2093            fail("Unable to read BLOCKDATA: " + e.getMessage());
2094        } catch (IOException e) {
2095            fail("IOException testing BLOCKDATA : " + e.getMessage());
2096        } catch (Error err) {
2097            System.out.println("Error " + err + " when testing BLOCKDATA");
2098            throw err;
2099        }
2100    }
2101}
2102