0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.harmony.luni.tests.java.io;
0018:
0019: import java.io.IOException;
0020: import java.io.NotSerializableException;
0021: import java.io.Serializable;
0022: import java.util.Arrays;
0023: import java.util.Hashtable;
0024: import java.util.Vector;
0025:
0026: @SuppressWarnings({"serial","unused"})
0027: public class SerializationStressTest1 extends SerializationStressTest {
0028:
0029: // The purpose of these two classes is to test if serialization, when
0030: // loading, runs the object's constructor (wrong) or the constructor defined
0031: // at the topmost Serializable superclass(correct).
0032: static final int INIT_INT_VALUE = 7;
0033:
0034: // HAS to be static class so that our constructor signature will remain
0035: // untouched (no synthetic param)
0036: private static class SerializationTest implements
0037: java.io.Serializable {
0038: int anInt = INIT_INT_VALUE;
0039:
0040: public SerializationTest() {
0041: super ();
0042: }
0043: }
0044:
0045: static final String INIT_STR_VALUE = "a string that is blortz";
0046:
0047: // HAS to be static class so that our constructor signature will remain
0048: // untouched (no synthetic param)
0049: private static class SerializationTestSubclass1 extends
0050: SerializationTest {
0051: String aString = INIT_STR_VALUE;
0052:
0053: public SerializationTestSubclass1() {
0054: super ();
0055: // Just to change default superclass init value
0056: anInt = INIT_INT_VALUE / 2;
0057: }
0058: }
0059:
0060: // -----------------------------------------------------------------------------------
0061:
0062: private static class SpecTestSuperClass implements Runnable {
0063: protected java.lang.String instVar;
0064:
0065: public void run() {
0066: }
0067: }
0068:
0069: private static class SpecTest extends SpecTestSuperClass implements
0070: Cloneable, Serializable {
0071: public java.lang.String instVar1;
0072:
0073: public static java.lang.String staticVar1;
0074:
0075: public static java.lang.String staticVar2;
0076: {
0077: instVar1 = "NonStaticInitialValue";
0078: }
0079: static {
0080: staticVar1 = "StaticInitialValue";
0081: staticVar1 = new String(staticVar1);
0082: }
0083:
0084: public Object method(Object objParam, Object objParam2) {
0085: return new Object();
0086: }
0087:
0088: public boolean method(boolean bParam, Object objParam) {
0089: return true;
0090: }
0091:
0092: public boolean method(boolean bParam, Object objParam,
0093: Object objParam2) {
0094: return true;
0095: }
0096:
0097: }
0098:
0099: private static class SpecTestSubclass extends SpecTest {
0100: public transient java.lang.String transientInstVar = "transientValue";
0101: }
0102:
0103: // -----------------------------------------------------------------------------------
0104:
0105: // This one tests what happens if the read/writeObject methods are defined
0106: // Serialization should work fine.
0107: private static class ReadWriteObject implements
0108: java.io.Serializable {
0109: public boolean calledWriteObject = false;
0110:
0111: public boolean calledReadObject = false;
0112:
0113: public ReadWriteObject() {
0114: super ();
0115: }
0116:
0117: private void readObject(java.io.ObjectInputStream in)
0118: throws java.io.IOException, ClassNotFoundException {
0119: calledReadObject = true;
0120: in.readObject();
0121: }
0122:
0123: private void writeObject(java.io.ObjectOutputStream out)
0124: throws java.io.IOException {
0125: calledWriteObject = true;
0126: out.writeObject(FOO);
0127: }
0128: }
0129:
0130: // This one tests what happens if the read/writeObject methods are not
0131: // private.
0132: // Serialization should fail.
0133: private static class PublicReadWriteObject implements
0134: java.io.Serializable {
0135: public boolean calledWriteObject = false;
0136:
0137: public boolean calledReadObject = false;
0138:
0139: public PublicReadWriteObject() {
0140: super ();
0141: }
0142:
0143: public void readObject(java.io.ObjectInputStream in)
0144: throws java.io.IOException, ClassNotFoundException {
0145: calledReadObject = true;
0146: in.readObject();
0147: }
0148:
0149: public void writeObject(java.io.ObjectOutputStream out)
0150: throws java.io.IOException {
0151: calledWriteObject = true;
0152: out.writeObject(FOO);
0153: }
0154: }
0155:
0156: // This one tests if field names are serialized in the same way (sorting)
0157: // across different VMs
0158: private static class FieldOrder implements Serializable {
0159: String aaa1NonPrimitive = "aaa1";
0160:
0161: int bbb1PrimitiveInt = 5;
0162:
0163: boolean aaa2PrimitiveBoolean = true;
0164:
0165: String bbb2NonPrimitive = "bbb2";
0166: }
0167:
0168: // This one tests what happens if you define just readObject, but not
0169: // writeObject.
0170: // Does it run or not ?
0171: private static class JustReadObject implements java.io.Serializable {
0172: public boolean calledReadObject = false;
0173:
0174: public JustReadObject() {
0175: super ();
0176: }
0177:
0178: private void readObject(java.io.ObjectInputStream in)
0179: throws java.io.IOException, ClassNotFoundException {
0180: calledReadObject = true;
0181: in.defaultReadObject();
0182: }
0183: }
0184:
0185: // This one tests what happens if you define just writeObject, but not
0186: // readObject.
0187: // Does it run or not ?
0188: private static class JustWriteObject implements
0189: java.io.Serializable {
0190: public boolean calledWriteObject = false;
0191:
0192: public JustWriteObject() {
0193: super ();
0194: }
0195:
0196: private void writeObject(java.io.ObjectOutputStream out)
0197: throws java.io.IOException, ClassNotFoundException {
0198: calledWriteObject = true;
0199: out.defaultWriteObject();
0200: }
0201: }
0202:
0203: // This one tests class-based replacement when dumping
0204: private static class ClassBasedReplacementWhenDumping implements
0205: java.io.Serializable {
0206: public boolean calledReplacement = false;
0207:
0208: public ClassBasedReplacementWhenDumping() {
0209: super ();
0210: }
0211:
0212: private Object writeReplace() {
0213: calledReplacement = true;
0214: return FOO; // Replacement is a String
0215: }
0216: }
0217:
0218: // This one tests whether class-based replacement supports multiple levels.
0219: // MultipleClassBasedReplacementWhenDumping -> C1 -> C2 -> C3 -> FOO
0220: private static class MultipleClassBasedReplacementWhenDumping
0221: implements java.io.Serializable {
0222: private static class C1 implements java.io.Serializable {
0223: private Object writeReplace() {
0224: return new C2();
0225: }
0226: }
0227:
0228: private static class C2 implements java.io.Serializable {
0229: private Object writeReplace() {
0230: return new C3();
0231: }
0232: }
0233:
0234: private static class C3 implements java.io.Serializable {
0235: private Object writeReplace() {
0236: return FOO;
0237: }
0238: }
0239:
0240: public MultipleClassBasedReplacementWhenDumping() {
0241: super ();
0242: }
0243:
0244: private Object writeReplace() {
0245: return new C1();
0246: }
0247: }
0248:
0249: // This one tests class-based replacement when loading
0250: private static class ClassBasedReplacementWhenLoading implements
0251: java.io.Serializable {
0252: public ClassBasedReplacementWhenLoading() {
0253: super ();
0254: }
0255:
0256: private Object readResolve() {
0257: return FOO; // Replacement is a String
0258: }
0259: }
0260:
0261: // This one tests what happens if a loading-replacement is not
0262: // type-compatible with the original object
0263: private static class ClassBasedReplacementWhenLoadingViolatesFieldType
0264: implements java.io.Serializable {
0265: public ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading();
0266:
0267: public ClassBasedReplacementWhenLoadingViolatesFieldType() {
0268: super ();
0269: }
0270: }
0271:
0272: // What happens if dumping causes an error and you try to reload ?
0273: // Should the load throw the same exception ?
0274: private static class MyExceptionWhenDumping1 implements
0275: java.io.Serializable {
0276: private static class MyException extends java.io.IOException {
0277: };
0278:
0279: // A primitive instance variable exposes a bug in the serialization
0280: // spec.
0281: // Primitive instance variables are written without primitive data tags
0282: // and so are read without checking for tags. If an exception is
0283: // written, reading primitive data will just read bytes from the stream
0284: // which may be tags
0285: public boolean anInstanceVar = false;
0286:
0287: public MyExceptionWhenDumping1() {
0288: super ();
0289: }
0290:
0291: private void readObject(java.io.ObjectInputStream in)
0292: throws java.io.IOException, ClassNotFoundException {
0293: in.defaultReadObject();
0294: }
0295:
0296: private void writeObject(java.io.ObjectOutputStream out)
0297: throws java.io.IOException, ClassNotFoundException {
0298: throw new MyException();
0299: }
0300: }
0301:
0302: // What happens if dumping causes an error and you try to reload ?
0303: // Should the load throw the same exception ?
0304: private static class MyExceptionWhenDumping2 implements
0305: java.io.Serializable {
0306: private static class MyException extends java.io.IOException {
0307: };
0308:
0309: public Integer anInstanceVar = new Integer(0xA1);
0310:
0311: public MyExceptionWhenDumping2() {
0312: super ();
0313: }
0314:
0315: private void readObject(java.io.ObjectInputStream in)
0316: throws java.io.IOException, ClassNotFoundException {
0317: in.defaultReadObject();
0318: }
0319:
0320: private void writeObject(java.io.ObjectOutputStream out)
0321: throws java.io.IOException, ClassNotFoundException {
0322: throw new MyException();
0323: }
0324: }
0325:
0326: // What happens if dumping causes an error (NonSerializable inst var) and
0327: // you try to reload ?
0328: // Should the load throw the same exception ?
0329: private static class NonSerializableExceptionWhenDumping implements
0330: java.io.Serializable {
0331: public Object anInstanceVar = new Object();
0332:
0333: public NonSerializableExceptionWhenDumping() {
0334: super ();
0335: }
0336: }
0337:
0338: // What happens if dumping causes an error (which is not serializable) and
0339: // you try to reload ?
0340: // Should the load throw the same exception ?
0341: private static class MyUnserializableExceptionWhenDumping implements
0342: java.io.Serializable {
0343: private static class MyException extends java.io.IOException {
0344: private Object notSerializable = new Object();
0345: };
0346:
0347: public boolean anInstanceVar = false;
0348:
0349: public MyUnserializableExceptionWhenDumping() {
0350: super ();
0351: }
0352:
0353: private void readObject(java.io.ObjectInputStream in)
0354: throws java.io.IOException, ClassNotFoundException {
0355: in.defaultReadObject();
0356: }
0357:
0358: private void writeObject(java.io.ObjectOutputStream out)
0359: throws java.io.IOException, ClassNotFoundException {
0360: throw new MyException();
0361: }
0362: }
0363:
0364: public SerializationStressTest1(String name) {
0365: super (name);
0366: }
0367:
0368: public void test_18_1_writeObject() {
0369: // Test for method void
0370: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0371:
0372: Object objToSave = null;
0373: Object objLoaded;
0374:
0375: try {
0376: objToSave = "HelloWorld";
0377: if (DEBUG)
0378: System.out.println("Obj = " + objToSave);
0379: objLoaded = dumpAndReload(objToSave);
0380: assertTrue(MSG_TEST_FAILED + objToSave,
0381: (((String) objLoaded).equals((String) objToSave)));
0382:
0383: } catch (IOException e) {
0384: fail("IOException serializing data : " + e.getMessage());
0385: } catch (ClassNotFoundException e) {
0386: fail("ClassNotFoundException reading Object type: "
0387: + e.getMessage());
0388: } catch (Error err) {
0389: System.out.println("Error when obj = " + objToSave);
0390: // err.printStackTrace();
0391: throw err;
0392: }
0393: }
0394:
0395: public void test_18_2_writeObject() {
0396: // Test for method void
0397: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0398:
0399: Object objToSave = null;
0400: Object objLoaded;
0401:
0402: try {
0403: objToSave = null;
0404: if (DEBUG)
0405: System.out.println("Obj = " + objToSave);
0406: objLoaded = dumpAndReload(objToSave);
0407: assertTrue(MSG_TEST_FAILED + objToSave,
0408: objLoaded == objToSave);
0409:
0410: } catch (IOException e) {
0411: fail("IOException serializing data : " + e.getMessage());
0412: } catch (ClassNotFoundException e) {
0413: fail("ClassNotFoundException reading Object type : "
0414: + e.getMessage());
0415: } catch (Error err) {
0416: System.out.println("Error when obj = " + objToSave);
0417: // err.printStackTrace();
0418: throw err;
0419: }
0420: }
0421:
0422: public void test_18_3_writeObject() {
0423: // Test for method void
0424: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0425:
0426: Object objToSave = null;
0427: Object objLoaded;
0428:
0429: try {
0430: byte[] bytes = { 0, 1, 2, 3 };
0431: objToSave = bytes;
0432: if (DEBUG)
0433: System.out.println("Obj = " + objToSave);
0434: objLoaded = dumpAndReload(objToSave);
0435: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0436: (byte[]) objLoaded, (byte[]) objToSave));
0437:
0438: } catch (IOException e) {
0439: fail("IOException serializing data : " + e.getMessage());
0440: } catch (ClassNotFoundException e) {
0441: fail("ClassNotFoundException reading Object type : "
0442: + e.getMessage());
0443: } catch (Error err) {
0444: System.out.println("Error when obj = " + objToSave);
0445: // err.printStackTrace();
0446: throw err;
0447: }
0448: }
0449:
0450: public void test_18_4_writeObject() {
0451: // Test for method void
0452: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0453:
0454: Object objToSave = null;
0455: Object objLoaded;
0456:
0457: try {
0458: int[] ints = { 0, 1, 2, 3 };
0459: objToSave = ints;
0460: if (DEBUG)
0461: System.out.println("Obj = " + objToSave);
0462: objLoaded = dumpAndReload(objToSave);
0463: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0464: (int[]) objLoaded, (int[]) objToSave));
0465:
0466: } catch (IOException e) {
0467: fail("IOException serializing data : " + e.getMessage());
0468: } catch (ClassNotFoundException e) {
0469: fail("ClassNotFoundException reading Object type : "
0470: + e.getMessage());
0471: } catch (Error err) {
0472: System.out.println("Error when obj = " + objToSave);
0473: throw err;
0474: }
0475: }
0476:
0477: public void test_18_5_writeObject() {
0478: // Test for method void
0479: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0480:
0481: Object objToSave = null;
0482: Object objLoaded;
0483:
0484: try {
0485:
0486: short[] shorts = { 0, 1, 2, 3 };
0487: objToSave = shorts;
0488: if (DEBUG)
0489: System.out.println("Obj = " + objToSave);
0490: objLoaded = dumpAndReload(objToSave);
0491: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0492: (short[]) objLoaded, (short[]) objToSave));
0493:
0494: } catch (IOException e) {
0495: fail("IOException serializing data : " + e.getMessage());
0496: } catch (ClassNotFoundException e) {
0497: fail("ClassNotFoundException reading Object type : "
0498: + e.getMessage());
0499: } catch (Error err) {
0500: System.out.println("Error when obj = " + objToSave);
0501: // err.printStackTrace();
0502: throw err;
0503: }
0504: }
0505:
0506: public void test_18_6_writeObject() {
0507: // Test for method void
0508: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0509:
0510: Object objToSave = null;
0511: Object objLoaded;
0512:
0513: try {
0514: long[] longs = { 0, 1, 2, 3 };
0515: objToSave = longs;
0516: if (DEBUG)
0517: System.out.println("Obj = " + objToSave);
0518: objLoaded = dumpAndReload(objToSave);
0519: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0520: (long[]) objLoaded, (long[]) objToSave));
0521:
0522: } catch (IOException e) {
0523: fail("IOException serializing data : " + e.getMessage());
0524: } catch (ClassNotFoundException e) {
0525: fail("ClassNotFoundException reading Object type : "
0526: + e.getMessage());
0527: } catch (Error err) {
0528: System.out.println("Error when obj = " + objToSave);
0529: // err.printStackTrace();
0530: throw err;
0531: }
0532: }
0533:
0534: public void test_18_7_writeObject() {
0535: // Test for method void
0536: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0537:
0538: Object objToSave = null;
0539: Object objLoaded;
0540:
0541: try {
0542: float[] floats = { 0.0f, 1.1f, 2.2f, 3.3f };
0543: objToSave = floats;
0544: if (DEBUG)
0545: System.out.println("Obj = " + objToSave);
0546: objLoaded = dumpAndReload(objToSave);
0547: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0548: (float[]) objLoaded, (float[]) objToSave));
0549:
0550: } catch (IOException e) {
0551: fail("IOException serializing data: " + e.getMessage());
0552: } catch (ClassNotFoundException e) {
0553: fail("ClassNotFoundException reading Object type : "
0554: + e.getMessage());
0555: } catch (Error err) {
0556: System.out.println("Error when obj = " + objToSave);
0557: // err.printStackTrace();
0558: throw err;
0559: }
0560: }
0561:
0562: public void test_18_8_writeObject() {
0563: // Test for method void
0564: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0565:
0566: Object objToSave = null;
0567: Object objLoaded;
0568:
0569: try {
0570: double[] doubles = { 0.0, 1.1, 2.2, 3.3 };
0571: objToSave = doubles;
0572: if (DEBUG)
0573: System.out.println("Obj = " + objToSave);
0574: objLoaded = dumpAndReload(objToSave);
0575: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0576: (double[]) objLoaded, (double[]) objToSave));
0577:
0578: } catch (IOException e) {
0579: fail("IOException serializing data : " + e.getMessage());
0580: } catch (ClassNotFoundException e) {
0581: fail("ClassNotFoundException reading Object type : "
0582: + e.getMessage());
0583: } catch (Error err) {
0584: System.out.println("Error when obj = " + objToSave);
0585: // err.printStackTrace();
0586: throw err;
0587: }
0588: }
0589:
0590: public void test_18_9_writeObject() {
0591: // Test for method void
0592: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0593:
0594: Object objToSave = null;
0595: Object objLoaded;
0596:
0597: try {
0598: boolean[] booleans = { true, false, false, true };
0599: objToSave = booleans;
0600: if (DEBUG)
0601: System.out.println("Obj = " + objToSave);
0602: objLoaded = dumpAndReload(objToSave);
0603: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0604: (boolean[]) objLoaded, (boolean[]) objToSave));
0605:
0606: } catch (IOException e) {
0607: fail("IOException serializing data : " + e.getMessage());
0608: } catch (ClassNotFoundException e) {
0609: fail("ClassNotFoundException reading Object type : "
0610: + e.getMessage());
0611: } catch (Error err) {
0612: System.out.println("Error when obj = " + objToSave);
0613: // err.printStackTrace();
0614: throw err;
0615: }
0616: }
0617:
0618: public void test_18_10_writeObject() {
0619: // Test for method void
0620: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0621:
0622: Object objToSave = null;
0623: Object objLoaded;
0624:
0625: try {
0626:
0627: String[] strings = { "foo", "bar", "java" };
0628: objToSave = strings;
0629: if (DEBUG)
0630: System.out.println("Obj = " + objToSave);
0631: objLoaded = dumpAndReload(objToSave);
0632: assertTrue(MSG_TEST_FAILED + objToSave, Arrays.equals(
0633: (Object[]) objLoaded, (Object[]) objToSave));
0634:
0635: } catch (IOException e) {
0636: fail("IOException serializing " + objToSave + " : "
0637: + e.getMessage());
0638: } catch (ClassNotFoundException e) {
0639: fail("Unable to read Object type: " + e.toString());
0640: } catch (Error err) {
0641: System.out.println("Error when obj = " + objToSave);
0642: // err.printStackTrace();
0643: throw err;
0644: }
0645: }
0646:
0647: public void test_18_11_writeObject() {
0648: // Test for method void
0649: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0650:
0651: Object objToSave = null;
0652: Object objLoaded;
0653:
0654: try {
0655:
0656: objToSave = new Object(); // Not serializable
0657: if (DEBUG)
0658: System.out.println("Obj = " + objToSave);
0659: boolean passed = false;
0660: Throwable t = null;
0661: try {
0662: objLoaded = dumpAndReload(objToSave);
0663: } catch (NotSerializableException ns) {
0664: passed = true;
0665: t = ns;
0666: } catch (Exception wrongExc) {
0667: passed = false;
0668: t = wrongExc;
0669: }
0670: assertTrue(
0671: "Failed to throw NotSerializableException when serializing "
0672: + objToSave + " Threw(if non-null) this: "
0673: + t, passed);
0674: } catch (Error err) {
0675: System.out.println("Error when obj = " + objToSave);
0676: // err.printStackTrace();
0677: throw err;
0678: }
0679: }
0680:
0681: public void test_18_12_writeObject() {
0682: // Test for method void
0683: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0684:
0685: try {
0686: if (DEBUG)
0687: System.out.println("Obj = <mixed>");
0688: t_MixPrimitivesAndObjects();
0689: } catch (IOException e) {
0690: fail("IOException serializing data : " + e.getMessage());
0691: } catch (ClassNotFoundException e) {
0692: fail("ClassNotFoundException reading Object type : "
0693: + e.getMessage());
0694: } catch (Error err) {
0695: System.out.println("Error when dumping mixed types");
0696: // err.printStackTrace();
0697: throw err;
0698: }
0699: }
0700:
0701: public void test_18_13_writeObject() {
0702: // Test for method void
0703: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0704:
0705: Object objToSave = null;
0706: Object objLoaded;
0707:
0708: try {
0709: SerializationTestSubclass1 st = new SerializationTestSubclass1();
0710: // Just change the default ivar values
0711: st.anInt = Integer.MAX_VALUE;
0712: st.aString = FOO;
0713: objToSave = st;
0714: if (DEBUG)
0715: System.out.println("Obj = " + objToSave);
0716: objLoaded = dumpAndReload(objToSave);
0717: // non-serializable inst var has to be initialized from top
0718: // constructor
0719: assertTrue(
0720: MSG_TEST_FAILED + objToSave,
0721: ((SerializationTestSubclass1) objLoaded).anInt == Integer.MAX_VALUE);
0722: // but serialized var has to be restored as it was in the object
0723: // when dumped
0724: assertTrue(MSG_TEST_FAILED + objToSave,
0725: ((SerializationTestSubclass1) objLoaded).aString
0726: .equals(FOO));
0727: } catch (IOException e) {
0728: fail("Exception serializing " + objToSave + "\t->"
0729: + e.toString());
0730: } catch (ClassNotFoundException e) {
0731: fail("ClassNotFoundException reading Object type : "
0732: + e.getMessage());
0733: } catch (Error err) {
0734: System.out.println("Error when obj = " + objToSave);
0735: err.printStackTrace();
0736: throw err;
0737: }
0738: }
0739:
0740: public void test_18_14_writeObject() {
0741: // Test for method void
0742: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0743:
0744: Object objToSave = null;
0745: Object objLoaded;
0746:
0747: try {
0748: SpecTest specTest = new SpecTest();
0749: // Just change the default ivar values
0750: specTest.instVar = FOO;
0751: specTest.instVar1 = specTest.instVar;
0752: objToSave = specTest;
0753: if (DEBUG)
0754: System.out.println("Obj = " + objToSave);
0755: objLoaded = dumpAndReload(objToSave);
0756: // non-serializable inst var has to be initialized from top
0757: // constructor
0758: assertNull(MSG_TEST_FAILED + objToSave,
0759: ((SpecTest) objLoaded).instVar);
0760: // instVar from non-serialized class, cant be saved/restored
0761: // by serialization but serialized ivar has to be restored as it
0762: // was in the object when dumped
0763: assertTrue(MSG_TEST_FAILED + objToSave,
0764: ((SpecTest) objLoaded).instVar1.equals(FOO));
0765:
0766: } catch (IOException e) {
0767: fail("Exception serializing " + objToSave + "\t->"
0768: + e.toString());
0769: } catch (ClassNotFoundException e) {
0770: fail("ClassNotFoundException reading Object type : "
0771: + e.getMessage());
0772: } catch (Error err) {
0773: System.out.println("Error when obj = " + objToSave);
0774: // err.printStackTrace();
0775: throw err;
0776: }
0777: }
0778:
0779: public void test_18_15_writeObject() {
0780: // Test for method void
0781: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0782:
0783: Object objToSave = null;
0784: Object objLoaded;
0785:
0786: try {
0787: SpecTestSubclass specTestSubclass = new SpecTestSubclass();
0788: // Just change the default ivar values
0789: specTestSubclass.transientInstVar = FOO;
0790: objToSave = specTestSubclass;
0791: if (DEBUG)
0792: System.out.println("Obj = " + objToSave);
0793: objLoaded = dumpAndReload(objToSave);
0794: // non-serializable inst var cant be saved, and it is not init'ed
0795: // from top constructor in this case
0796: assertNull(MSG_TEST_FAILED + objToSave,
0797: ((SpecTestSubclass) objLoaded).transientInstVar);
0798: // transient slot, cant be saved/restored by serialization
0799: } catch (IOException e) {
0800: fail("Exception serializing " + objToSave + "\t->"
0801: + e.toString());
0802: } catch (ClassNotFoundException e) {
0803: fail("ClassNotFoundException reading Object type : "
0804: + e.getMessage());
0805: } catch (Error err) {
0806: System.out.println("Error when obj = " + objToSave);
0807: // err.printStackTrace();
0808: throw err;
0809: }
0810: }
0811:
0812: public void test_18_16_writeObject() {
0813: // Test for method void
0814: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0815:
0816: Object objToSave = null;
0817: Object objLoaded;
0818:
0819: try {
0820:
0821: String[] strings = new String[2];
0822: strings[0] = FOO;
0823: strings[1] = (" " + FOO + " ").trim(); // Safe way to get a copy
0824: // that is not ==
0825: objToSave = strings;
0826: if (DEBUG)
0827: System.out.println("Obj = " + objToSave);
0828: objLoaded = dumpAndReload(objToSave);
0829: String[] stringsLoaded = (String[]) objLoaded;
0830: // Serialization has to use identity-based table for assigning IDs
0831: assertTrue(MSG_TEST_FAILED + objToSave,
0832: !(stringsLoaded[0] == stringsLoaded[1]));
0833: } catch (IOException e) {
0834: fail("Exception serializing " + objToSave + "\t->"
0835: + e.toString());
0836: } catch (ClassNotFoundException e) {
0837: fail("ClassNotFoundException reading Object type : "
0838: + e.getMessage());
0839: } catch (Error err) {
0840: System.out.println("Error when obj = " + objToSave);
0841: // err.printStackTrace();
0842: throw err;
0843: }
0844: }
0845:
0846: public void test_18_17_writeObject() {
0847: // Test for method void
0848: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0849:
0850: Object objToSave = null;
0851: Object objLoaded;
0852:
0853: try {
0854:
0855: ReadWriteObject readWrite = new ReadWriteObject();
0856: objToSave = readWrite;
0857: if (DEBUG)
0858: System.out.println("Obj = " + objToSave);
0859: objLoaded = dumpAndReload(objToSave);
0860: // has to have called the writeObject on the instance to dump
0861: assertTrue(MSG_TEST_FAILED + objToSave,
0862: readWrite.calledWriteObject);
0863: // has to have called the readObject on the instance loaded
0864: assertTrue(MSG_TEST_FAILED + objToSave,
0865: ((ReadWriteObject) objLoaded).calledReadObject);
0866:
0867: } catch (IOException e) {
0868: fail("Exception serializing " + objToSave + "\t->"
0869: + e.toString());
0870: } catch (ClassNotFoundException e) {
0871: fail("ClassNotFoundException reading Object type : "
0872: + e.getMessage());
0873: } catch (Error err) {
0874: System.out.println("Error when obj = " + objToSave);
0875: // err.printStackTrace();
0876: throw err;
0877: }
0878: }
0879:
0880: public void test_18_18_writeObject() {
0881: // Test for method void
0882: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0883:
0884: Object objToSave = null;
0885: Object objLoaded;
0886:
0887: try {
0888: PublicReadWriteObject publicReadWrite = new PublicReadWriteObject();
0889: objToSave = publicReadWrite;
0890: if (DEBUG)
0891: System.out.println("Obj = " + objToSave);
0892: objLoaded = dumpAndReload(objToSave);
0893: // Can't have called the writeObject on the instance to dump
0894: assertTrue(MSG_TEST_FAILED + objToSave,
0895: !publicReadWrite.calledWriteObject);
0896: // Can't have called the readObject on the instance loaded
0897: assertTrue(
0898: MSG_TEST_FAILED + objToSave,
0899: !((PublicReadWriteObject) objLoaded).calledReadObject);
0900:
0901: } catch (IOException e) {
0902: fail("Exception serializing " + objToSave + "\t->"
0903: + e.toString());
0904: } catch (ClassNotFoundException e) {
0905: fail("ClassNotFoundException reading Object type : "
0906: + e.getMessage());
0907: } catch (Error err) {
0908: System.out.println("Error when obj = " + objToSave);
0909: // err.printStackTrace();
0910: throw err;
0911: }
0912: }
0913:
0914: public void test_18_19_writeObject() {
0915: // Test for method void
0916: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0917:
0918: Object objToSave = null;
0919: Object objLoaded;
0920:
0921: try {
0922: FieldOrder fieldOrder = new FieldOrder();
0923: objToSave = fieldOrder;
0924: if (DEBUG)
0925: System.out.println("Obj = " + objToSave);
0926: objLoaded = dumpAndReload(objToSave);
0927: // This test is only useful for X-loading, so if it managed to
0928: // dump&load, we passed the test
0929: assertTrue(MSG_TEST_FAILED + objToSave, true);
0930:
0931: } catch (IOException e) {
0932: fail("IOException serializing " + objToSave + " : "
0933: + e.getMessage());
0934: } catch (ClassNotFoundException e) {
0935: fail("ClassNotFoundException reading Object type : "
0936: + e.getMessage());
0937: } catch (Error err) {
0938: System.out.println("Error when obj = " + objToSave);
0939: // err.printStackTrace();
0940: throw err;
0941: }
0942: }
0943:
0944: public void test_18_20_writeObject() {
0945: // Test for method void
0946: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0947:
0948: Object objToSave = null;
0949: Object objLoaded;
0950:
0951: try {
0952: objToSave = Class.forName("java.lang.Integer");
0953: if (DEBUG)
0954: System.out.println("Obj = " + objToSave);
0955: objLoaded = dumpAndReload(objToSave);
0956: // Classes with the same name are unique, so test for ==
0957: assertTrue(MSG_TEST_FAILED + objToSave,
0958: objLoaded == objToSave);
0959:
0960: } catch (IOException e) {
0961: fail("IOException serializing " + objToSave + " : "
0962: + e.getMessage());
0963: } catch (ClassNotFoundException e) {
0964: fail("ClassNotFoundException reading Object type : "
0965: + e.getMessage());
0966: } catch (Error err) {
0967: System.out.println("Error when obj = " + objToSave);
0968: // err.printStackTrace();
0969: throw err;
0970: }
0971: }
0972:
0973: public void test_18_21_writeObject() {
0974: // Test for method void
0975: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
0976:
0977: Object objToSave = null;
0978: Object objLoaded;
0979:
0980: try {
0981: // Even though instances of java.lang.Object are not Serializable,
0982: // instances of java.lang.Class are. So, the object
0983: // java.lang.Object.class
0984: // should be serializable
0985: objToSave = Class.forName("java.lang.Object");
0986: if (DEBUG)
0987: System.out.println("Obj = " + objToSave);
0988: objLoaded = dumpAndReload(objToSave);
0989: // Classes with the same name are unique, so test for ==
0990: assertTrue(MSG_TEST_FAILED + objToSave,
0991: objLoaded == objToSave);
0992:
0993: } catch (IOException e) {
0994: fail("IOException serializing " + objToSave + " : "
0995: + e.getMessage());
0996: } catch (ClassNotFoundException e) {
0997: fail("ClassNotFoundException reading Object type : "
0998: + e.getMessage());
0999: } catch (Error err) {
1000: System.out.println("Error when obj = " + objToSave);
1001: // err.printStackTrace();
1002: throw err;
1003: }
1004: }
1005:
1006: public void test_18_22_writeObject() {
1007: // Test for method void
1008: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1009:
1010: Object objToSave = null;
1011: Object objLoaded;
1012:
1013: try {
1014: java.net.URL url = new java.net.URL(
1015: "http://localhost/a.txt");
1016: objToSave = url;
1017: if (DEBUG)
1018: System.out.println("Obj = " + objToSave);
1019: objLoaded = dumpAndReload(objToSave);
1020: assertTrue("URLs are not the same: " + url + "\t,\t"
1021: + objLoaded, url.equals(objLoaded));
1022:
1023: } catch (IOException e) {
1024: fail("IOException serializing " + objToSave + " : "
1025: + e.getMessage());
1026: } catch (ClassNotFoundException e) {
1027: fail("ClassNotFoundException reading Object type : "
1028: + e.getMessage());
1029: } catch (Error err) {
1030: System.out.println("Error when obj = " + objToSave);
1031: // err.printStackTrace();
1032: throw err;
1033: }
1034: }
1035:
1036: public void test_18_23_writeObject() {
1037: // Test for method void
1038: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1039:
1040: Object objToSave = null;
1041: Object objLoaded;
1042:
1043: try {
1044:
1045: JustReadObject justReadObject = new JustReadObject();
1046: objToSave = justReadObject;
1047: if (DEBUG)
1048: System.out.println("Obj = " + objToSave);
1049: objLoaded = dumpAndReload(objToSave);
1050: // Only calls readObject on the instance loaded if writeObject was
1051: // also defined
1052: assertTrue(
1053: "Called readObject on an object without a writeObject",
1054: !((JustReadObject) objLoaded).calledReadObject);
1055:
1056: } catch (IOException e) {
1057: fail("IOException serializing " + objToSave + " : "
1058: + e.getMessage());
1059: } catch (ClassNotFoundException e) {
1060: fail("ClassNotFoundException reading Object type : "
1061: + e.getMessage());
1062: } catch (Error err) {
1063: System.out.println("Error when obj = " + objToSave);
1064: // err.printStackTrace();
1065: throw err;
1066: }
1067: }
1068:
1069: public void test_18_24_writeObject() {
1070: // Test for method void
1071: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1072:
1073: Object objToSave = null;
1074: Object objLoaded;
1075:
1076: try {
1077:
1078: JustWriteObject justWriteObject = new JustWriteObject();
1079: objToSave = justWriteObject;
1080: if (DEBUG)
1081: System.out.println("Obj = " + objToSave);
1082: objLoaded = dumpAndReload(objToSave);
1083: // Call writeObject on the instance even if it does not define
1084: // readObject
1085: assertTrue(MSG_TEST_FAILED + objToSave,
1086: justWriteObject.calledWriteObject);
1087:
1088: } catch (IOException e) {
1089: fail("IOException serializing " + objToSave + " : "
1090: + e.getMessage());
1091: } catch (ClassNotFoundException e) {
1092: fail("ClassNotFoundException reading Object type: "
1093: + e.getMessage());
1094: } catch (Error err) {
1095: System.out.println("Error when obj = " + objToSave);
1096: // err.printStackTrace();
1097: throw err;
1098: }
1099: }
1100:
1101: public void test_18_25_writeObject() {
1102: // Test for method void
1103: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1104:
1105: Object objToSave = null;
1106: Object objLoaded;
1107:
1108: try {
1109: Vector<String> vector = new Vector<String>(1);
1110: vector.add(FOO);
1111: objToSave = vector;
1112: if (DEBUG)
1113: System.out.println("Obj = " + objToSave);
1114: objLoaded = dumpAndReload(objToSave);
1115: // Has to have the string there
1116: assertTrue(MSG_TEST_FAILED + objToSave,
1117: FOO.equals(((java.util.Vector) objLoaded)
1118: .elementAt(0)));
1119:
1120: } catch (IOException e) {
1121: fail("IOException serializing " + objToSave + " : "
1122: + e.getMessage());
1123: } catch (ClassNotFoundException e) {
1124: fail("ClassNotFoundException reading Object type : "
1125: + e.getMessage());
1126: } catch (Error err) {
1127: System.out.println("Error when obj = " + objToSave);
1128: throw err;
1129: }
1130: }
1131:
1132: public void test_18_26_writeObject() {
1133: // Test for method void
1134: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1135:
1136: Object objToSave = null;
1137: Object objLoaded;
1138:
1139: try {
1140: Hashtable<String, String> hashTable = new Hashtable<String, String>(
1141: 5);
1142: hashTable.put(FOO, FOO);
1143: objToSave = hashTable;
1144: if (DEBUG)
1145: System.out.println("Obj = " + objToSave);
1146: objLoaded = dumpAndReload(objToSave);
1147: java.util.Hashtable loadedHashTable = (java.util.Hashtable) objLoaded;
1148: // Has to have the key/value there (FOO -> FOO)
1149: assertTrue(MSG_TEST_FAILED + objToSave, FOO
1150: .equals(loadedHashTable.get(FOO)));
1151:
1152: } catch (IOException e) {
1153: fail("IOException serializing " + objToSave + " : "
1154: + e.getMessage());
1155: } catch (ClassNotFoundException e) {
1156: fail("ClassNotFoundException reading Object type : "
1157: + e.getMessage());
1158: } catch (Error err) {
1159: System.out.println("Error when obj = " + objToSave);
1160: throw err;
1161: }
1162: }
1163:
1164: public void test_18_27_writeObject() {
1165: // Test for method void
1166: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1167:
1168: Object objToSave = null;
1169: Object objLoaded;
1170:
1171: try {
1172: ClassBasedReplacementWhenDumping classBasedReplacementWhenDumping = new ClassBasedReplacementWhenDumping();
1173: objToSave = classBasedReplacementWhenDumping;
1174: if (DEBUG)
1175: System.out.println("Obj = " + objToSave);
1176: objLoaded = dumpAndReload(objToSave);
1177: // Has to have run the replacement method
1178: assertTrue("Did not run writeReplace",
1179: classBasedReplacementWhenDumping.calledReplacement);
1180:
1181: // Has to have loaded a String (replacement object)
1182: assertTrue("Did not replace properly", FOO
1183: .equals(objLoaded));
1184:
1185: } catch (IOException e) {
1186: fail("IOException serializing " + objToSave + " : "
1187: + e.getMessage());
1188: } catch (ClassNotFoundException e) {
1189: fail("ClassNotFoundException reading Object type : "
1190: + e.getMessage());
1191: } catch (Error err) {
1192: System.out.println("Error when obj = " + objToSave);
1193: // err.printStackTrace();
1194: throw err;
1195: }
1196: }
1197:
1198: public void test_18_28_writeObject() {
1199: // Test for method void
1200: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1201:
1202: Object objToSave = null;
1203: Object objLoaded;
1204:
1205: try {
1206: MultipleClassBasedReplacementWhenDumping multipleClassBasedReplacementWhenDumping = new MultipleClassBasedReplacementWhenDumping();
1207: objToSave = multipleClassBasedReplacementWhenDumping;
1208: if (DEBUG)
1209: System.out.println("Obj = " + objToSave);
1210: objLoaded = dumpAndReload(objToSave);
1211: // Has to have loaded a String (replacement object)
1212: assertTrue(
1213: "Executed multiple levels of replacement (see PR 1F9RNT1), loaded= "
1214: + objLoaded,
1215: objLoaded instanceof MultipleClassBasedReplacementWhenDumping.C1);
1216:
1217: } catch (IOException e) {
1218: fail("IOException serializing " + objToSave + " : "
1219: + e.getMessage());
1220: } catch (ClassNotFoundException e) {
1221: fail("ClassNotFoundException reading Object type : "
1222: + e.toString());
1223: } catch (Error err) {
1224: System.out.println("Error when obj = " + objToSave);
1225: // err.printStackTrace();
1226: throw err;
1227: }
1228: }
1229:
1230: public void test_18_29_writeObject() {
1231: // Test for method void
1232: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1233:
1234: Object objToSave = null;
1235: Object objLoaded;
1236:
1237: try {
1238: ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading();
1239: objToSave = classBasedReplacementWhenLoading;
1240: if (DEBUG)
1241: System.out.println("Obj = " + objToSave);
1242: objLoaded = dumpAndReload(objToSave);
1243: // Has to have loaded a String (replacement object)
1244: assertTrue("Did not run readResolve", FOO.equals(objLoaded));
1245:
1246: } catch (IOException e) {
1247: fail("IOException serializing " + objToSave + " : "
1248: + e.getMessage());
1249: } catch (ClassNotFoundException e) {
1250: fail("ClassNotFoundException reading Object type : "
1251: + e.getMessage());
1252: } catch (Error err) {
1253: System.out.println("Error when obj = " + objToSave);
1254: // err.printStackTrace();
1255: throw err;
1256: }
1257: }
1258:
1259: public void test_18_30_writeObject() {
1260: // Test for method void
1261: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1262:
1263: Object objToSave = null;
1264: Object objLoaded;
1265:
1266: try {
1267: ClassBasedReplacementWhenLoadingViolatesFieldType classBasedReplacementWhenLoadingViolatesFieldType = new ClassBasedReplacementWhenLoadingViolatesFieldType();
1268: objToSave = classBasedReplacementWhenLoadingViolatesFieldType;
1269: if (DEBUG)
1270: System.out.println("Obj = " + objToSave);
1271: objLoaded = dumpAndReload(objToSave);
1272: // We cannot gere here, the load replacement must have caused a
1273: // field type violation
1274: fail("Loading replacements can cause field type violation in this implementation");
1275:
1276: } catch (IOException e) {
1277: fail("IOException serializing " + objToSave + " : "
1278: + e.getMessage());
1279: } catch (ClassNotFoundException e) {
1280: fail("ClassNotFoundException reading Object type : "
1281: + e.getMessage());
1282: } catch (ClassCastException e) {
1283: assertTrue(
1284: "Loading replacements can NOT cause field type violation in this implementation",
1285: true);
1286: } catch (Error err) {
1287: System.out.println("Error when obj = " + objToSave);
1288: // err.printStackTrace();
1289: throw err;
1290: }
1291: }
1292:
1293: public void test_18_31_writeObject() {
1294: // Test for method void
1295: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1296:
1297: Object objToSave = null;
1298: Object objLoaded;
1299:
1300: try {
1301: MyExceptionWhenDumping1 exceptionWhenDumping = new MyExceptionWhenDumping1();
1302: objToSave = exceptionWhenDumping;
1303: if (DEBUG)
1304: System.out.println("Obj = " + objToSave);
1305: boolean causedException = false;
1306: try {
1307: dump(objToSave);
1308: } catch (MyExceptionWhenDumping1.MyException e) {
1309: causedException = true;
1310: }
1311: ;
1312: assertTrue("Should have caused an exception when dumping",
1313: causedException);
1314: causedException = false;
1315: try {
1316: objLoaded = reload();
1317: // Although the spec says we should get a WriteAbortedException,
1318: // the serialization format handle an Exception when reading
1319: // primitive data so we get ClassCastException instead
1320: } catch (ClassCastException e) {
1321: causedException = true;
1322: }
1323: ;
1324: assertTrue(
1325: "Should have caused a ClassCastException when loading",
1326: causedException);
1327: } catch (IOException e) {
1328: fail("IOException serializing " + objToSave + " : "
1329: + e.getMessage());
1330: } catch (ClassNotFoundException e) {
1331: fail("ClassNotFoundException reading Object type : "
1332: + e.getMessage());
1333: } catch (Error err) {
1334: System.out.println("Error when obj = " + objToSave);
1335: // err.printStackTrace();
1336: throw err;
1337: }
1338: }
1339:
1340: public void test_18_32_writeObject() {
1341: // Test for method void
1342: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1343:
1344: Object objToSave = null;
1345: Object objLoaded;
1346:
1347: try {
1348: MyExceptionWhenDumping2 exceptionWhenDumping = new MyExceptionWhenDumping2();
1349: objToSave = exceptionWhenDumping;
1350: if (DEBUG)
1351: System.out.println("Obj = " + objToSave);
1352: boolean causedException = false;
1353: try {
1354: dump(objToSave);
1355: } catch (MyExceptionWhenDumping2.MyException e) {
1356: causedException = true;
1357: }
1358: ;
1359: assertTrue("Should have caused an exception when dumping",
1360: causedException);
1361: causedException = false;
1362: try {
1363: objLoaded = reload();
1364: } catch (java.io.WriteAbortedException e) {
1365: causedException = true;
1366: }
1367: ;
1368: assertTrue(
1369: "Should have caused a java.io.WriteAbortedException when loading",
1370: causedException);
1371: } catch (IOException e) {
1372: fail("IOException serializing " + objToSave + " : "
1373: + e.getMessage());
1374: } catch (ClassNotFoundException e) {
1375: fail("ClassNotFoundException reading Object type : "
1376: + e.getMessage());
1377: } catch (ClassCastException e) {
1378: fail("ClassCastException : " + e.getMessage());
1379: } catch (Error err) {
1380: System.out.println("Error when obj = " + objToSave);
1381: throw err;
1382: }
1383: }
1384:
1385: public void test_NonSerializableExceptionWhenDumping() {
1386: // Test for method void
1387: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1388:
1389: Object objToSave = null;
1390: Object objLoaded;
1391:
1392: try {
1393: NonSerializableExceptionWhenDumping nonSerializableExceptionWhenDumping = new NonSerializableExceptionWhenDumping();
1394: objToSave = nonSerializableExceptionWhenDumping;
1395: if (DEBUG)
1396: System.out.println("Obj = " + objToSave);
1397: boolean causedException = false;
1398: try {
1399: dump(objToSave);
1400: } catch (java.io.NotSerializableException e) {
1401: causedException = true;
1402: }
1403: ;
1404: assertTrue("Should have caused an exception when dumping",
1405: causedException);
1406: causedException = false;
1407: try {
1408: objLoaded = reload();
1409: } catch (java.io.WriteAbortedException e) {
1410: causedException = true;
1411: }
1412: ;
1413: assertTrue(
1414: "Should have caused a java.io.WriteAbortedException when loading",
1415: causedException);
1416: } catch (IOException e) {
1417: fail("IOException serializing " + objToSave + " : "
1418: + e.getMessage());
1419: } catch (ClassNotFoundException e) {
1420: fail("ClassNotFoundException reading Object type : "
1421: + e.getMessage());
1422: } catch (Error err) {
1423: System.out.println("Error when obj = " + objToSave);
1424: // err.printStackTrace();
1425: throw err;
1426: }
1427: }
1428:
1429: public void test_18_33_writeObject() {
1430: // Test for method void
1431: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1432:
1433: Object objToSave = null;
1434: Object objLoaded;
1435:
1436: try {
1437: MyUnserializableExceptionWhenDumping exceptionWhenDumping = new MyUnserializableExceptionWhenDumping();
1438: objToSave = exceptionWhenDumping;
1439: if (DEBUG)
1440: System.out.println("Obj = " + objToSave);
1441: boolean causedException = false;
1442: try {
1443: dump(objToSave);
1444: } catch (java.io.StreamCorruptedException e) {
1445: causedException = true;
1446: }
1447: ;
1448: assertTrue("Should have caused an exception when dumping",
1449: causedException);
1450: // As the stream is corrupted, reading the stream will have
1451: // undefined results
1452: } catch (IOException e) {
1453: fail("IOException serializing " + objToSave + " : "
1454: + e.getMessage());
1455: } catch (ClassNotFoundException e) {
1456: fail("ClassNotFoundException reading Object type : "
1457: + e.getMessage());
1458: } catch (Error err) {
1459: System.out.println("Error when obj = " + objToSave);
1460: // err.printStackTrace();
1461: throw err;
1462: }
1463: }
1464:
1465: public void test_18_34_writeObject() {
1466: // Test for method void
1467: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1468:
1469: Object objToSave = null;
1470: Object objLoaded;
1471:
1472: try {
1473: java.io.IOException ioe = new java.io.IOException();
1474: objToSave = ioe;
1475: if (DEBUG)
1476: System.out.println("Obj = " + objToSave);
1477: objLoaded = dumpAndReload(objToSave);
1478: // Has to be able to save/load an exception
1479: assertTrue(MSG_TEST_FAILED + objToSave, true);
1480:
1481: } catch (IOException e) {
1482: fail("IOException serializing " + objToSave + " : "
1483: + e.getMessage());
1484: } catch (ClassNotFoundException e) {
1485: fail("ClassNotFoundException reading Object type : "
1486: + e.getMessage());
1487: } catch (Error err) {
1488: System.out.println("Error when obj = " + objToSave);
1489: // err.printStackTrace();
1490: throw err;
1491: }
1492: }
1493:
1494: public void test_18_35_writeObject() {
1495: // Test for method void
1496: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1497:
1498: Object objToSave = null;
1499: Object objLoaded;
1500:
1501: try {
1502: objToSave = Class.forName("java.util.Hashtable");
1503: if (DEBUG)
1504: System.out.println("Obj = " + objToSave);
1505: objLoaded = dumpAndReload(objToSave);
1506: // Classes with the same name are unique, so test for ==
1507: assertTrue(MSG_TEST_FAILED + objToSave,
1508: objLoaded == objToSave);
1509:
1510: } catch (IOException e) {
1511: fail("IOException serializing " + objToSave + " : "
1512: + e.getMessage());
1513: } catch (ClassNotFoundException e) {
1514: fail("ClassNotFoundException reading Object type : "
1515: + e.getMessage());
1516: } catch (Error err) {
1517: System.out.println("Error when obj = " + objToSave);
1518: // err.printStackTrace();
1519: throw err;
1520: }
1521: }
1522:
1523: public void test_18_36_writeObject() {
1524: // Test for method void
1525: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1526:
1527: Object objToSave = null;
1528: Object objLoaded;
1529:
1530: try {
1531: java.io.IOException ex = new java.io.InvalidClassException(
1532: FOO);
1533: objToSave = ex;
1534: if (DEBUG)
1535: System.out.println("Obj = " + objToSave);
1536: objLoaded = dumpAndReload(objToSave);
1537: // Has to be able to save/load an exception
1538: assertTrue(MSG_TEST_FAILED + objToSave, true);
1539:
1540: } catch (IOException e) {
1541: fail("IOException serializing " + objToSave + " : "
1542: + e.getMessage());
1543: } catch (ClassNotFoundException e) {
1544: fail("ClassNotFoundException reading Object type : "
1545: + e.getMessage());
1546: } catch (Error err) {
1547: System.out.println("Error when obj = " + objToSave);
1548: // err.printStackTrace();
1549: throw err;
1550: }
1551: }
1552:
1553: public void test_18_37_writeObject() {
1554: // Test for method void
1555: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1556:
1557: Object objToSave = null;
1558: Object objLoaded;
1559:
1560: try {
1561: java.io.IOException ex = new java.io.InvalidObjectException(
1562: FOO);
1563: objToSave = ex;
1564: if (DEBUG)
1565: System.out.println("Obj = " + objToSave);
1566: objLoaded = dumpAndReload(objToSave);
1567: // Has to be able to save/load an exception
1568: assertTrue(MSG_TEST_FAILED + objToSave, true);
1569:
1570: } catch (IOException e) {
1571: fail("IOException serializing " + objToSave + " : "
1572: + e.getMessage());
1573: } catch (ClassNotFoundException e) {
1574: fail("ClassNotFoundException reading Object type : "
1575: + e.getMessage());
1576: } catch (Error err) {
1577: System.out.println("Error when obj = " + objToSave);
1578: // err.printStackTrace();
1579: throw err;
1580: }
1581: }
1582:
1583: public void test_18_38_writeObject() {
1584: // Test for method void
1585: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1586:
1587: Object objToSave = null;
1588: Object objLoaded;
1589:
1590: try {
1591: java.io.IOException ex = new java.io.NotActiveException(FOO);
1592: objToSave = ex;
1593: if (DEBUG)
1594: System.out.println("Obj = " + objToSave);
1595: objLoaded = dumpAndReload(objToSave);
1596: // Has to be able to save/load an exception
1597: assertTrue(MSG_TEST_FAILED + objToSave, true);
1598:
1599: } catch (IOException e) {
1600: fail("IOException serializing " + objToSave + " : "
1601: + e.getMessage());
1602: } catch (ClassNotFoundException e) {
1603: fail("ClassNotFoundException reading Object type : "
1604: + e.getMessage());
1605: } catch (Error err) {
1606: System.out.println("Error when obj = " + objToSave);
1607: // err.printStackTrace();
1608: throw err;
1609: }
1610: }
1611:
1612: public void test_18_39_writeObject() {
1613: // Test for method void
1614: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1615:
1616: Object objToSave = null;
1617: Object objLoaded;
1618:
1619: try {
1620: java.io.IOException ex = new java.io.NotSerializableException(
1621: FOO);
1622: objToSave = ex;
1623: if (DEBUG)
1624: System.out.println("Obj = " + objToSave);
1625: objLoaded = dumpAndReload(objToSave);
1626: // Has to be able to save/load an exception
1627: assertTrue(MSG_TEST_FAILED + objToSave, true);
1628:
1629: } catch (IOException e) {
1630: fail("IOException serializing " + objToSave + " : "
1631: + e.getMessage());
1632: } catch (ClassNotFoundException e) {
1633: fail("ClassNotFoundException reading Object type : "
1634: + e.getMessage());
1635: } catch (Error err) {
1636: System.out.println("Error when obj = " + objToSave);
1637: // err.printStackTrace();
1638: throw err;
1639: }
1640: }
1641:
1642: public void test_18_40_writeObject() {
1643: // Test for method void
1644: // java.io.ObjectOutputStream.writeObject(java.lang.Object)
1645:
1646: Object objToSave = null;
1647: Object objLoaded;
1648:
1649: try {
1650: java.io.IOException ex = new java.io.StreamCorruptedException(
1651: FOO);
1652: objToSave = ex;
1653: if (DEBUG)
1654: System.out.println("Obj = " + objToSave);
1655: objLoaded = dumpAndReload(objToSave);
1656: // Has to be able to save/load an exception
1657: assertTrue(MSG_TEST_FAILED + objToSave, true);
1658:
1659: } catch (IOException e) {
1660: fail("IOException serializing " + objToSave + " : "
1661: + e.getMessage());
1662: } catch (ClassNotFoundException e) {
1663: fail("ClassNotFoundException reading Object type : "
1664: + e.getMessage());
1665: } catch (Error err) {
1666: System.out.println("Error when obj = " + objToSave);
1667: // err.printStackTrace();
1668: throw err;
1669: }
1670: }
1671: }
|