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