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:
0018: package org.apache.harmony.luni.tests.java.io;
0019:
0020: import java.io.ByteArrayInputStream;
0021: import java.io.ByteArrayOutputStream;
0022: import java.io.DataInputStream;
0023: import java.io.FileInputStream;
0024: import java.io.FileOutputStream;
0025: import java.io.IOException;
0026: import java.io.InputStream;
0027: import java.io.InvalidObjectException;
0028: import java.io.NotActiveException;
0029: import java.io.ObjectInputStream;
0030: import java.io.ObjectOutputStream;
0031: import java.io.ObjectStreamClass;
0032: import java.io.ObjectStreamException;
0033: import java.io.Serializable;
0034: import java.io.StreamCorruptedException;
0035: import java.io.WriteAbortedException;
0036: import java.security.Permission;
0037: import java.security.PermissionCollection;
0038: import java.util.ArrayList;
0039: import java.util.Arrays;
0040: import java.util.Calendar;
0041: import java.util.GregorianCalendar;
0042: import java.util.HashMap;
0043: import java.util.HashSet;
0044: import java.util.Hashtable;
0045: import java.util.IdentityHashMap;
0046: import java.util.LinkedHashMap;
0047: import java.util.LinkedHashSet;
0048: import java.util.LinkedList;
0049: import java.util.List;
0050: import java.util.Map;
0051: import java.util.PropertyPermission;
0052: import java.util.Set;
0053: import java.util.SimpleTimeZone;
0054: import java.util.SortedMap;
0055: import java.util.SortedSet;
0056: import java.util.TimeZone;
0057: import java.util.TreeMap;
0058: import java.util.TreeSet;
0059: import java.util.Vector;
0060:
0061: /**
0062: * Automated Test Suite for class java.io.ObjectOutputStream
0063: *
0064: */
0065: @SuppressWarnings("serial")
0066: public class SerializationStressTest extends junit.framework.TestCase
0067: implements Serializable {
0068:
0069: // protected static final String MODE_XLOAD = "xload";
0070:
0071: // protected static final String MODE_XDUMP = "xdump";
0072:
0073: static final String FOO = "foo";
0074:
0075: static final String MSG_TEST_FAILED = "Failed to write/read/assertion checking: ";
0076:
0077: protected static final boolean DEBUG = false;
0078:
0079: protected static boolean xload = false;
0080:
0081: protected static boolean xdump = false;
0082:
0083: protected static String xFileName = null;
0084:
0085: protected transient int dumpCount = 0;
0086:
0087: protected transient ObjectInputStream ois;
0088:
0089: protected transient ObjectOutputStream oos;
0090:
0091: protected transient ByteArrayOutputStream bao;
0092:
0093: // -----------------------------------------------------------------------------------
0094:
0095: private static class ObjectInputStreamSubclass extends
0096: ObjectInputStream {
0097: private Vector<Class> resolvedClasses = new Vector<Class>();
0098:
0099: public ObjectInputStreamSubclass(InputStream in)
0100: throws IOException, StreamCorruptedException {
0101: super (in);
0102: }
0103:
0104: public Class<?> resolveClass(ObjectStreamClass osClass)
0105: throws IOException, ClassNotFoundException {
0106: Class result = super .resolveClass(osClass);
0107: resolvedClasses.addElement(result);
0108: return result;
0109: }
0110:
0111: public Class[] resolvedClasses() {
0112: return (Class[]) resolvedClasses
0113: .toArray(new Class[resolvedClasses.size()]);
0114: }
0115: }
0116:
0117: static final Map<String, String> TABLE = new Hashtable<String, String>();
0118:
0119: static final Map<String, String> MAP = new HashMap<String, String>();
0120:
0121: static final SortedMap<String, String> TREE = new TreeMap<String, String>();
0122:
0123: static final LinkedHashMap<String, String> LINKEDMAP = new LinkedHashMap<String, String>();
0124:
0125: static final LinkedHashSet<String> LINKEDSET = new LinkedHashSet<String>();
0126:
0127: static final IdentityHashMap<String, String> IDENTITYMAP = new IdentityHashMap<String, String>();
0128:
0129: static final List<String> ALIST = Arrays.asList(new String[] { "a",
0130: "list", "of", "strings" });
0131:
0132: static final List<String> LIST = new ArrayList<String>(ALIST);
0133:
0134: static final Set<String> SET = new HashSet<String>(Arrays
0135: .asList(new String[] { "one", "two", "three" }));
0136:
0137: static final Permission PERM = new PropertyPermission(
0138: "file.encoding", "write");
0139:
0140: static final PermissionCollection PERMCOL = PERM
0141: .newPermissionCollection();
0142:
0143: static final SortedSet<String> SORTSET = new TreeSet<String>(Arrays
0144: .asList(new String[] { "one", "two", "three" }));
0145:
0146: static final java.text.DateFormat DATEFORM = java.text.DateFormat
0147: .getInstance();
0148:
0149: static final java.text.ChoiceFormat CHOICE = new java.text.ChoiceFormat(
0150: "1#one|2#two|3#three");
0151:
0152: static final java.text.NumberFormat NUMBERFORM = java.text.NumberFormat
0153: .getInstance();
0154:
0155: static final java.text.MessageFormat MESSAGE = new java.text.MessageFormat(
0156: "the time: {0,time} and date {0,date}");
0157:
0158: static final LinkedList<String> LINKEDLIST = new LinkedList<String>(
0159: Arrays.asList(new String[] { "a", "linked", "list", "of",
0160: "strings" }));
0161:
0162: static final SimpleTimeZone TIME_ZONE = new SimpleTimeZone(3600000,
0163: "S-TEST");
0164:
0165: static final Calendar CALENDAR = new GregorianCalendar(TIME_ZONE);
0166:
0167: static {
0168: TABLE.put("one", "1");
0169: TABLE.put("two", "2");
0170: TABLE.put("three", "3");
0171: MAP.put("one", "1");
0172: MAP.put("two", "2");
0173: MAP.put("three", "3");
0174: LINKEDMAP.put("one", "1");
0175: LINKEDMAP.put("two", "2");
0176: LINKEDMAP.put("three", "3");
0177: IDENTITYMAP.put("one", "1");
0178: IDENTITYMAP.put("two", "2");
0179: IDENTITYMAP.put("three", "3");
0180: LINKEDSET.add("one");
0181: LINKEDSET.add("two");
0182: LINKEDSET.add("three");
0183: TREE.put("one", "1");
0184: TREE.put("two", "2");
0185: TREE.put("three", "3");
0186: PERMCOL.add(PERM);
0187: // To make sure they all use the same Calendar
0188: CALENDAR.setTimeZone(new SimpleTimeZone(0, "GMT"));
0189: CALENDAR.set(1999, Calendar.JUNE, 23, 15, 47, 13);
0190: CALENDAR.set(Calendar.MILLISECOND, 553);
0191: DATEFORM.setCalendar(CALENDAR);
0192: java.text.DateFormatSymbols symbols = new java.text.DateFormatSymbols();
0193: symbols.setZoneStrings(new String[][] { { "a", "b", "c", "d" },
0194: { "e", "f", "g", "h" } });
0195: ((java.text.SimpleDateFormat) DATEFORM)
0196: .setDateFormatSymbols(symbols);
0197: DATEFORM.setNumberFormat(new java.text.DecimalFormat(
0198: "#.#;'-'#.#"));
0199: DATEFORM.setTimeZone(TimeZone.getTimeZone("EST"));
0200: ((java.text.DecimalFormat) NUMBERFORM)
0201: .applyPattern("#.#;'-'#.#");
0202: MESSAGE.setFormat(0, DATEFORM);
0203: MESSAGE.setFormat(1, DATEFORM);
0204: }
0205:
0206: public SerializationStressTest() {
0207: }
0208:
0209: public SerializationStressTest(String name) {
0210: super (name);
0211: }
0212:
0213: public String getDumpName() {
0214: return getName() + dumpCount;
0215: }
0216:
0217: protected void dump(Object o) throws IOException,
0218: ClassNotFoundException {
0219: if (dumpCount > 0)
0220: setUp();
0221: // Dump the object
0222: try {
0223: oos.writeObject(o);
0224: } finally {
0225: oos.close();
0226: }
0227: }
0228:
0229: protected Object dumpAndReload(Object o) throws IOException,
0230: ClassNotFoundException {
0231: dump(o);
0232: return reload();
0233: }
0234:
0235: protected InputStream loadStream() throws IOException {
0236: // Choose the load stream
0237: if (xload || xdump) {
0238: // Load from pre-existing file
0239: return new FileInputStream(xFileName + "-" + getDumpName()
0240: + ".ser");
0241: } else {
0242: // Just load from memory, we dumped to memory
0243: return new ByteArrayInputStream(bao.toByteArray());
0244: }
0245: }
0246:
0247: protected Object reload() throws IOException,
0248: ClassNotFoundException {
0249: ois = new ObjectInputStream(loadStream());
0250: dumpCount++;
0251: try {
0252: return ois.readObject();
0253: } finally {
0254: ois.close();
0255: }
0256: }
0257:
0258: /**
0259: * Sets up the fixture, for example, open a network connection. This method
0260: * is called before a test is executed.
0261: */
0262: protected void setUp() {
0263: try {
0264: if (xdump) {
0265: oos = new ObjectOutputStream(new FileOutputStream(
0266: xFileName + "-" + getDumpName() + ".ser"));
0267: } else {
0268: oos = new ObjectOutputStream(
0269: bao = new ByteArrayOutputStream());
0270: }
0271: } catch (Exception e) {
0272: fail("Exception thrown during setup : " + e.getMessage());
0273: }
0274: }
0275:
0276: /**
0277: * Tears down the fixture, for example, close a network connection. This
0278: * method is called after a test is executed.
0279: */
0280: protected void tearDown() {
0281: if (oos != null) {
0282: try {
0283: oos.close();
0284: } catch (Exception e) {
0285: }
0286: }
0287: }
0288:
0289: public void test_1_Constructor() throws Exception {
0290: // Test for method java.io.ObjectOutputStream(java.io.OutputStream)
0291: oos.close();
0292: oos = new ObjectOutputStream(new ByteArrayOutputStream());
0293: oos.close();
0294: }
0295:
0296: public void test_2_close() {
0297: // Test for method void java.io.ObjectOutputStream.close()
0298: try {
0299: oos.close();
0300: oos = new ObjectOutputStream(
0301: bao = new ByteArrayOutputStream());
0302: oos.close();
0303: oos.writeChar('T');
0304: oos.writeObject(FOO);
0305: // Writing to a closed stream does not cause problems. This is
0306: // the expected behavior
0307: } catch (IOException e) {
0308: fail("Operation on closed stream threw IOException : "
0309: + e.getMessage());
0310: }
0311: }
0312:
0313: public void test_3_defaultWriteObject() {
0314: // Test for method void java.io.ObjectOutputStream.defaultWriteObject()
0315:
0316: try {
0317: oos.defaultWriteObject();
0318: } catch (NotActiveException e) {
0319: // Correct
0320: return;
0321: } catch (IOException e) {
0322: }
0323: fail("Failed to throw NotActiveException when invoked outside readObject");
0324: }
0325:
0326: public void test_4_flush() {
0327: // Test for method void java.io.ObjectOutputStream.flush()
0328: try {
0329: oos.close();
0330: oos = new ObjectOutputStream(
0331: bao = new ByteArrayOutputStream());
0332: int size = bao.size();
0333: oos.writeByte(127);
0334: assertTrue("Data flushed already", bao.size() == size);
0335: oos.flush();
0336: assertTrue("Failed to flush data", bao.size() > size);
0337: // we don't know how many bytes are actually written for 1 byte,
0338: // so we test > <before>
0339: oos.close();
0340: oos = null;
0341: } catch (IOException e) {
0342: fail("IOException serializing data : " + e.getMessage());
0343: }
0344: }
0345:
0346: public void test_5_reset() {
0347: // Test for method void java.io.ObjectOutputStream.reset()
0348: try {
0349: String o = "HelloWorld";
0350: oos.writeObject(o);
0351: oos.writeObject(o);
0352: oos.reset();
0353: oos.writeObject(o);
0354: ois = new ObjectInputStream(loadStream());
0355: ois.close();
0356: } catch (IOException e) {
0357: fail("IOException serializing data : " + e.getMessage());
0358: }
0359: }
0360:
0361: public void test_6_write() {
0362: // Test for method void java.io.ObjectOutputStream.write(byte [], int,
0363: // int)
0364: try {
0365: byte[] buf = new byte[255];
0366: byte[] output = new byte[255];
0367: for (int i = 0; i < output.length; i++)
0368: output[i] = (byte) i;
0369: oos.write(output, 0, output.length);
0370: oos.close();
0371: ois = new ObjectInputStream(loadStream());
0372: ois.readFully(buf);
0373: ois.close();
0374: for (int i = 0; i < output.length; i++)
0375: if (buf[i] != output[i])
0376: fail("Read incorrect byte: " + i);
0377: } catch (IOException e) {
0378: fail("IOException serializing data : " + e.getMessage());
0379: }
0380: }
0381:
0382: public void test_6a_write() {
0383: // Test for method void java.io.ObjectOutputStream.write(byte [], int,
0384: // int)
0385: try {
0386: byte[] buf = new byte[256];
0387: byte[] output = new byte[256];
0388: for (int i = 0; i < output.length; i++)
0389: output[i] = (byte) (i & 0xff);
0390: oos.write(output, 0, output.length);
0391: oos.close();
0392: ois = new ObjectInputStream(loadStream());
0393: ois.readFully(buf);
0394: ois.close();
0395: for (int i = 0; i < output.length; i++)
0396: if (buf[i] != output[i])
0397: fail("Read incorrect byte: " + i);
0398: } catch (IOException e) {
0399: fail("IOException serializing data : " + e.getMessage());
0400: }
0401: }
0402:
0403: public void test_7_write() {
0404: // Test for method void java.io.ObjectOutputStream.write(int)
0405: try {
0406: oos.write('T');
0407: oos.close();
0408: ois = new ObjectInputStream(loadStream());
0409: assertEquals("Read incorrect byte", 'T', ois.read());
0410: ois.close();
0411: } catch (IOException e) {
0412: fail("IOException serializing data : " + e.getMessage());
0413: }
0414: }
0415:
0416: public void test_8_write() {
0417: // Test for method void java.io.ObjectOutputStream.write(byte [])
0418: try {
0419: byte[] buf = new byte[10];
0420: oos.write("HelloWorld".getBytes());
0421: oos.close();
0422: ois = new ObjectInputStream(loadStream());
0423: ois.read(buf, 0, 10);
0424: ois.close();
0425: assertEquals("Read incorrect bytes", "HelloWorld",
0426: new String(buf, 0, 10));
0427: } catch (IOException e) {
0428: fail("IOException serializing data : " + e.getMessage());
0429: }
0430: }
0431:
0432: public void test_9_writeBoolean() {
0433: // Test for method void java.io.ObjectOutputStream.writeBoolean(boolean)
0434: try {
0435: oos.writeBoolean(true);
0436: oos.close();
0437: ois = new ObjectInputStream(loadStream());
0438: assertTrue("Wrote incorrect byte value", ois.readBoolean());
0439: } catch (IOException e) {
0440: fail("IOException serializing data : " + e.getMessage());
0441: }
0442: }
0443:
0444: public void test_10_writeByte() {
0445: // Test for method void java.io.ObjectOutputStream.writeByte(int)
0446: try {
0447: oos.writeByte(127);
0448: oos.close();
0449: ois = new ObjectInputStream(loadStream());
0450: assertEquals("Wrote incorrect byte value", 127, ois
0451: .readByte());
0452: } catch (IOException e) {
0453: fail("IOException serializing data : " + e.getMessage());
0454: }
0455: }
0456:
0457: public void test_11_writeBytes() {
0458: // Test for method void
0459: // java.io.ObjectOutputStream.writeBytes(java.lang.String)
0460: try {
0461: byte[] buf = new byte[10];
0462: oos.writeBytes("HelloWorld");
0463: oos.close();
0464: ois = new ObjectInputStream(loadStream());
0465: ois.readFully(buf);
0466: ois.close();
0467: assertEquals("Wrote incorrect bytes value", "HelloWorld",
0468: new String(buf, 0, 10));
0469: } catch (IOException e) {
0470: fail("IOException serializing data : " + e.getMessage());
0471: }
0472: }
0473:
0474: public void test_12_writeChar() {
0475: // Test for method void java.io.ObjectOutputStream.writeChar(int)
0476: try {
0477: oos.writeChar('T');
0478: oos.close();
0479: ois = new ObjectInputStream(loadStream());
0480: assertEquals("Wrote incorrect char value", 'T', ois
0481: .readChar());
0482: } catch (IOException e) {
0483: fail("IOException serializing data : " + e.getMessage());
0484: }
0485: }
0486:
0487: public void test_13_writeChars() {
0488: // Test for method void
0489: // java.io.ObjectOutputStream.writeChars(java.lang.String)
0490: try {
0491: int avail = 0;
0492: char[] buf = new char[10];
0493: oos.writeChars("HelloWorld");
0494: oos.close();
0495: ois = new ObjectInputStream(loadStream());
0496: // Number of prim data bytes in stream / 2 to give char index
0497: avail = ois.available() / 2;
0498: for (int i = 0; i < avail; ++i)
0499: buf[i] = ois.readChar();
0500: ois.close();
0501: assertEquals("Wrote incorrect chars", "HelloWorld",
0502: new String(buf, 0, 10));
0503: } catch (IOException e) {
0504: fail("IOException serializing data : " + e.getMessage());
0505: }
0506: }
0507:
0508: public void test_14_writeDouble() {
0509: // Test for method void java.io.ObjectOutputStream.writeDouble(double)
0510: try {
0511: oos.writeDouble(Double.MAX_VALUE);
0512: oos.close();
0513: ois = new ObjectInputStream(loadStream());
0514: assertTrue("Wrote incorrect double value",
0515: ois.readDouble() == Double.MAX_VALUE);
0516: } catch (IOException e) {
0517: fail("IOException serializing data : " + e.getMessage());
0518: }
0519: }
0520:
0521: public void test_15_writeFloat() {
0522: // Test for method void java.io.ObjectOutputStream.writeFloat(float)
0523: try {
0524: oos.writeFloat(Float.MAX_VALUE);
0525: oos.close();
0526: ois = new ObjectInputStream(loadStream());
0527: assertTrue("Wrote incorrect double value",
0528: ois.readFloat() == Float.MAX_VALUE);
0529: ois.close();
0530: ois = null;
0531: } catch (IOException e) {
0532: fail("IOException serializing data : " + e.getMessage());
0533: }
0534: }
0535:
0536: public void test_16_writeInt() {
0537: // Test for method void java.io.ObjectOutputStream.writeInt(int)
0538: try {
0539: oos.writeInt(Integer.MAX_VALUE);
0540: oos.close();
0541: ois = new ObjectInputStream(loadStream());
0542: assertTrue("Wrote incorrect double value",
0543: ois.readInt() == Integer.MAX_VALUE);
0544: ois.close();
0545: } catch (IOException e) {
0546: fail("IOException serializing data : " + e.getMessage());
0547: }
0548: }
0549:
0550: public void test_17_writeLong() {
0551: // Test for method void java.io.ObjectOutputStream.writeLong(long)
0552: try {
0553: oos.writeLong(Long.MAX_VALUE);
0554: oos.close();
0555: ois = new ObjectInputStream(loadStream());
0556: assertTrue("Wrote incorrect double value",
0557: ois.readLong() == Long.MAX_VALUE);
0558: } catch (IOException e) {
0559: fail("IOException serializing data : " + e.getMessage());
0560: }
0561: }
0562:
0563: public void test_19_writeShort() {
0564: // Test for method void java.io.ObjectOutputStream.writeShort(int)
0565: try {
0566: oos.writeShort(127);
0567: oos.close();
0568: ois = new ObjectInputStream(loadStream());
0569: assertEquals("Wrote incorrect short value", 127, ois
0570: .readShort());
0571: } catch (IOException e) {
0572: fail("IOException serializing data : " + e.getMessage());
0573: }
0574: }
0575:
0576: public void test_20_writeUTF() {
0577: // Test for method void
0578: // java.io.ObjectOutputStream.writeUTF(java.lang.String)
0579: try {
0580: oos.writeUTF("HelloWorld");
0581: oos.close();
0582: ois = new ObjectInputStream(loadStream());
0583: assertEquals("Wrote incorrect UTF value", "HelloWorld", ois
0584: .readUTF());
0585: } catch (IOException e) {
0586: fail("IOException serializing data : " + e.getMessage());
0587: }
0588: }
0589:
0590: public void test_25_available() {
0591: try {
0592: oos.writeObject(FOO);
0593: oos.writeObject(FOO);
0594: oos.flush();
0595: int available1 = 0;
0596: int available2 = 0;
0597: Object obj1 = null;
0598: Object obj2 = null;
0599: ObjectInputStream ois = new ObjectInputStream(loadStream());
0600: available1 = ois.available();
0601: obj1 = ois.readObject();
0602: available2 = ois.available();
0603: obj2 = ois.readObject();
0604:
0605: assertEquals("available returned incorrect value", 0,
0606: available1);
0607: assertEquals("available returned incorrect value", 0,
0608: available2);
0609:
0610: assertTrue("available caused incorrect reading", FOO
0611: .equals(obj1));
0612: assertTrue("available returned incorrect value", FOO
0613: .equals(obj2));
0614:
0615: } catch (IOException e) {
0616: fail("IOException serializing object : " + e.getMessage());
0617: } catch (ClassNotFoundException e) {
0618: fail("Unable to read Object type : " + e.toString());
0619: } catch (Error err) {
0620: System.out.println("Error " + err);
0621: throw err;
0622: }
0623:
0624: }
0625:
0626: protected void t_MixPrimitivesAndObjects() throws IOException,
0627: ClassNotFoundException {
0628: int i = 7;
0629: String s1 = "string 1";
0630: String s2 = "string 2";
0631: byte[] bytes = { 1, 2, 3 };
0632:
0633: oos.writeInt(i);
0634: oos.writeObject(s1);
0635: oos.writeUTF(s2);
0636: oos.writeObject(bytes);
0637: oos.close();
0638: try {
0639: ois = new ObjectInputStream(loadStream());
0640:
0641: int j = ois.readInt();
0642: assertTrue("Wrong int :" + j, i == j);
0643:
0644: String l1 = (String) ois.readObject();
0645: assertTrue("Wrong obj String :" + l1, s1.equals(l1));
0646:
0647: String l2 = (String) ois.readUTF();
0648: assertTrue("Wrong UTF String :" + l2, s2.equals(l2));
0649:
0650: byte[] bytes2 = (byte[]) ois.readObject();
0651: assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2));
0652:
0653: } finally {
0654: ois.close();
0655: }
0656: }
0657:
0658: public void test_resolveClass() {
0659: try {
0660: oos.writeObject(new Object[] { Integer.class,
0661: new Integer(1) });
0662: oos.close();
0663:
0664: ois = new ObjectInputStreamSubclass(loadStream());
0665: ois.readObject();
0666: ois.close();
0667: } catch (IOException e1) {
0668: fail("IOException : " + e1.getMessage());
0669: } catch (ClassNotFoundException e2) {
0670: fail("ClassNotFoundException : " + e2.getMessage());
0671: }
0672:
0673: Class[] resolvedClasses = ((ObjectInputStreamSubclass) ois)
0674: .resolvedClasses();
0675: assertEquals("missing resolved", 3, resolvedClasses.length);
0676: assertTrue("resolved class 1",
0677: resolvedClasses[0] == Object[].class);
0678: assertTrue("resolved class 2",
0679: resolvedClasses[1] == Integer.class);
0680: assertTrue("resolved class 3",
0681: resolvedClasses[2] == Number.class);
0682: }
0683:
0684: public void test_reset() {
0685: try {
0686: oos.reset();
0687: oos.writeObject("R");
0688: oos.reset();
0689: oos.writeByte(24);
0690: oos.close();
0691:
0692: DataInputStream dis = new DataInputStream(loadStream());
0693: byte[] input = new byte[dis.available()];
0694: dis.readFully(input);
0695: byte[] result = new byte[] { (byte) 0xac, (byte) 0xed,
0696: (byte) 0, (byte) 5, (byte) 0x79, (byte) 0x74,
0697: (byte) 0, (byte) 1, (byte) 'R', (byte) 0x79,
0698: (byte) 0x77, (byte) 1, (byte) 24 };
0699: assertTrue("incorrect output", Arrays.equals(input, result));
0700:
0701: ois = new ObjectInputStreamSubclass(loadStream());
0702: assertEquals("Wrong result from readObject()", "R", ois
0703: .readObject());
0704: assertEquals("Wrong result from readByte()", 24, ois
0705: .readByte());
0706: ois.close();
0707: } catch (IOException e1) {
0708: fail("IOException : " + e1.getMessage());
0709: } catch (ClassNotFoundException e2) {
0710: fail("ClassNotFoundException : " + e2.getMessage());
0711: }
0712: }
0713:
0714: public void test_serialVersionUID(Class clazz, long svUID)
0715: throws Exception {
0716: final String idWrong = "serialVersionUID is wrong for: ";
0717: long reflectedSvUID = 0L;
0718: reflectedSvUID = clazz.getField("serialVersionUID").getLong(
0719: null);
0720: assertTrue(idWrong + clazz + ": " + reflectedSvUID
0721: + " does not equal " + svUID, reflectedSvUID == svUID);
0722: }
0723:
0724: private static class ResolveObjectTest implements Serializable {
0725: Object field1, field2;
0726: }
0727:
0728: private static class ResolveObjectInputStream extends
0729: ObjectInputStream {
0730: ResolveObjectInputStream(InputStream in)
0731: throws StreamCorruptedException, IOException {
0732: super (in);
0733: }
0734:
0735: public void enableResolve() {
0736: enableResolveObject(true);
0737: }
0738:
0739: public Object resolveObject(Object obj) {
0740: if (obj instanceof Vector) // test_1_resolveObject()
0741: return new Hashtable();
0742: else if ("abc".equals(obj)) // test_2_resolveObject()
0743: return "ABC";
0744: else if (obj instanceof String) // test_3_resolveObject()
0745: return String.valueOf(((String) obj).length());
0746: else if (obj instanceof int[]) // test_4_resolveObject()
0747: return new Object[1];
0748: else if (obj instanceof Object[]
0749: && ((Object[]) obj).length == 2) // test_5_resolveObject()
0750: return new char[1];
0751: return obj;
0752: }
0753: }
0754:
0755: public void test_1_resolveObject() {
0756: try {
0757: ResolveObjectTest obj = new ResolveObjectTest();
0758: obj.field1 = new Vector();
0759: obj.field2 = obj.field1;
0760: oos.writeObject(obj);
0761: oos.close();
0762: ois = new ResolveObjectInputStream(loadStream());
0763: ((ResolveObjectInputStream) ois).enableResolve();
0764: ResolveObjectTest result = null;
0765: try {
0766: result = (ResolveObjectTest) ois.readObject();
0767: } catch (ClassNotFoundException e) {
0768: fail(e.toString());
0769: }
0770: assertTrue("Object not resolved",
0771: result.field1 instanceof Hashtable);
0772: assertTrue("Second reference not resolved",
0773: result.field1 == result.field2);
0774: } catch (IOException e) {
0775: fail("IOException serializing data : " + e.getMessage());
0776: }
0777: }
0778:
0779: public void test_2_resolveObject() {
0780: try {
0781: ResolveObjectTest obj = new ResolveObjectTest();
0782: obj.field1 = "abc";
0783: obj.field2 = obj.field1;
0784: oos.writeObject(obj);
0785: oos.close();
0786: ois = new ResolveObjectInputStream(loadStream());
0787: ((ResolveObjectInputStream) ois).enableResolve();
0788: ResolveObjectTest result = null;
0789: try {
0790: result = (ResolveObjectTest) ois.readObject();
0791: } catch (ClassNotFoundException e) {
0792: fail(e.toString());
0793: }
0794: assertEquals("String not resolved", "ABC", result.field1);
0795: assertTrue("Second reference not resolved",
0796: result.field1 == result.field2);
0797: } catch (IOException e) {
0798: fail("IOException serializing data : " + e.getMessage());
0799: }
0800: }
0801:
0802: public void test_3_resolveObject() {
0803: try {
0804: ResolveObjectTest obj = new ResolveObjectTest();
0805: char[] lchars = new char[70000];
0806: obj.field1 = new String(lchars);
0807: obj.field2 = obj.field1;
0808: oos.writeObject(obj);
0809: oos.close();
0810: ois = new ResolveObjectInputStream(loadStream());
0811: ((ResolveObjectInputStream) ois).enableResolve();
0812: ResolveObjectTest result = null;
0813: try {
0814: result = (ResolveObjectTest) ois.readObject();
0815: } catch (ClassNotFoundException e) {
0816: fail(e.toString());
0817: }
0818: assertTrue("Long String not resolved", "70000"
0819: .equals(result.field1));
0820: assertTrue("Second reference not resolved",
0821: result.field1 == result.field2);
0822: } catch (IOException e) {
0823: fail("IOException serializing data : " + e.getMessage());
0824: }
0825: }
0826:
0827: public void test_4_resolveObject() {
0828: try {
0829: ResolveObjectTest obj = new ResolveObjectTest();
0830: obj.field1 = new int[5];
0831: obj.field2 = obj.field1;
0832: oos.writeObject(obj);
0833: oos.close();
0834: ois = new ResolveObjectInputStream(loadStream());
0835: ((ResolveObjectInputStream) ois).enableResolve();
0836: ResolveObjectTest result = null;
0837: try {
0838: result = (ResolveObjectTest) ois.readObject();
0839: } catch (ClassNotFoundException e) {
0840: fail(e.toString());
0841: }
0842: Class cl = new Object[0].getClass();
0843: assertTrue("int[] not resolved",
0844: result.field1.getClass() == cl);
0845: assertTrue("Second reference not resolved",
0846: result.field1 == result.field2);
0847: } catch (IOException e) {
0848: fail("IOException serializing data : " + e.getMessage());
0849: }
0850: }
0851:
0852: public void test_5_resolveObject() {
0853: try {
0854: ResolveObjectTest obj = new ResolveObjectTest();
0855: obj.field1 = new Object[2];
0856: obj.field2 = obj.field1;
0857: oos.writeObject(obj);
0858: oos.close();
0859: ois = new ResolveObjectInputStream(loadStream());
0860: ((ResolveObjectInputStream) ois).enableResolve();
0861: ResolveObjectTest result = null;
0862: try {
0863: result = (ResolveObjectTest) ois.readObject();
0864: } catch (ClassNotFoundException e) {
0865: fail(e.toString());
0866: }
0867: Class cl = new char[0].getClass();
0868: assertTrue("int[] not resolved",
0869: result.field1.getClass() == cl);
0870: assertTrue("Second reference not resolved",
0871: result.field1 == result.field2);
0872: } catch (IOException e) {
0873: fail("IOException serializing data : " + e.getMessage());
0874: }
0875: }
0876:
0877: static class WriteReplaceTestA implements Serializable {
0878: public Object writeReplace() throws ObjectStreamException {
0879: return new ReadResolveTestB();
0880: }
0881: }
0882:
0883: static class WriteReplaceTestB extends WriteReplaceTestA {
0884: }
0885:
0886: static class WriteReplaceTestC extends WriteReplaceTestA {
0887: public Object writeReplace() throws ObjectStreamException {
0888: return new ReadResolveTestC();
0889: }
0890: }
0891:
0892: static class WriteReplaceTestD implements Serializable {
0893: private Object writeReplace() throws ObjectStreamException {
0894: return new ReadResolveTestD();
0895: }
0896: }
0897:
0898: static class WriteReplaceTestE extends WriteReplaceTestD {
0899: }
0900:
0901: static class WriteReplaceTestF implements Serializable {
0902: int type, readType;
0903:
0904: public WriteReplaceTestF(int type, int readType) {
0905: this .type = type;
0906: this .readType = readType;
0907: }
0908:
0909: public Object writeReplace() throws ObjectStreamException {
0910: switch (type) {
0911: case 0:
0912: throw new InvalidObjectException("invalid");
0913: case 1:
0914: throw new RuntimeException("runtime");
0915: case 2:
0916: throw new Error("error");
0917: default:
0918: return new ReadResolveTestE(readType);
0919: }
0920: }
0921: }
0922:
0923: static class ReadResolveTestA implements Serializable {
0924: public Object readResolve() throws ObjectStreamException {
0925: return new ReadResolveTestA();
0926: }
0927: }
0928:
0929: static class ReadResolveTestB extends ReadResolveTestA {
0930: }
0931:
0932: static class ReadResolveTestC implements Serializable {
0933: private Object readResolve() throws ObjectStreamException {
0934: return new ReadResolveTestB();
0935: }
0936: }
0937:
0938: static class ReadResolveTestD extends ReadResolveTestC {
0939: }
0940:
0941: static class ReadResolveTestE implements Serializable {
0942: int type;
0943:
0944: public ReadResolveTestE(int type) {
0945: this .type = type;
0946: }
0947:
0948: public Object readResolve() throws ObjectStreamException {
0949: switch (type) {
0950: case 0:
0951: throw new InvalidObjectException("invalid");
0952: case 1:
0953: throw new RuntimeException("runtime");
0954: case 2:
0955: throw new Error("error");
0956: case 3:
0957: return this ;
0958: default:
0959: return new ReadResolveTestF();
0960: }
0961: }
0962: }
0963:
0964: static class ReadResolveTestF implements Serializable {
0965: }
0966:
0967: public void test_1_writeReplace() {
0968: try {
0969: Vector<Object> v = new Vector<Object>();
0970: v.addElement(new WriteReplaceTestA());
0971: v.addElement(new WriteReplaceTestB());
0972: v.addElement(new WriteReplaceTestB());
0973: v.addElement(new WriteReplaceTestC());
0974: v.addElement(new WriteReplaceTestD());
0975: v.addElement(new WriteReplaceTestE());
0976: oos.writeObject(v);
0977: oos.close();
0978: ois = new ObjectInputStream(loadStream());
0979: Vector result = (Vector) ois.readObject();
0980: assertTrue("invalid 0 : " + result.elementAt(0), result
0981: .elementAt(0).getClass() == ReadResolveTestA.class);
0982: assertTrue("invalid 1 : " + result.elementAt(1), result
0983: .elementAt(1).getClass() == ReadResolveTestA.class);
0984: assertTrue("invalid 2 : " + result.elementAt(2), result
0985: .elementAt(2).getClass() == ReadResolveTestA.class);
0986: assertTrue("invalid 3 : " + result.elementAt(3), result
0987: .elementAt(3).getClass() == ReadResolveTestB.class);
0988: assertTrue("invalid 4 : " + result.elementAt(4), result
0989: .elementAt(4).getClass() == ReadResolveTestD.class);
0990: assertTrue("invalid 5 : " + result.elementAt(5), result
0991: .elementAt(5).getClass() == WriteReplaceTestE.class);
0992: } catch (IOException e) {
0993: fail("IOException serializing data : " + e.getMessage());
0994: } catch (ClassNotFoundException e) {
0995: fail("ClassNotFoundException serializing data : "
0996: + e.getMessage());
0997: }
0998: }
0999:
1000: public void test_2_writeReplace() {
1001: try {
1002: boolean exception = false;
1003: try {
1004: oos.writeObject(new WriteReplaceTestF(0, -1));
1005: } catch (ObjectStreamException e) {
1006: exception = true;
1007: }
1008: assertTrue("Should throw ObjectStreamException", exception);
1009: exception = false;
1010: try {
1011: oos.writeObject(new WriteReplaceTestF(1, -1));
1012: } catch (RuntimeException e) {
1013: exception = true;
1014: }
1015: assertTrue("Should throw RuntimeException", exception);
1016: exception = false;
1017: try {
1018: oos.writeObject(new WriteReplaceTestF(2, -1));
1019: } catch (Error e) {
1020: exception = true;
1021: }
1022: assertTrue("Should throw Error", exception);
1023:
1024: oos.writeObject(new WriteReplaceTestF(3, 0));
1025: oos.writeObject(new WriteReplaceTestF(3, 1));
1026: oos.writeObject(new WriteReplaceTestF(3, 2));
1027: WriteReplaceTestF test = new WriteReplaceTestF(3, 3);
1028: oos.writeObject(test);
1029: oos.writeObject(test);
1030: WriteReplaceTestF test2 = new WriteReplaceTestF(3, 4);
1031: oos.writeObject(test2);
1032: oos.writeObject(test2);
1033: oos.close();
1034: ois = new ObjectInputStream(loadStream());
1035: try {
1036: ois.readObject();
1037: } catch (WriteAbortedException e) {
1038: }
1039:
1040: exception = false;
1041: try {
1042: ois.readObject();
1043: } catch (ObjectStreamException e) {
1044: exception = true;
1045: }
1046: assertTrue("Expected ObjectStreamException", exception);
1047: exception = false;
1048: try {
1049: ois.readObject();
1050: } catch (RuntimeException e) {
1051: exception = true;
1052: }
1053: assertTrue("Expected RuntimeException", exception);
1054: exception = false;
1055: try {
1056: ois.readObject();
1057: } catch (Error e) {
1058: exception = true;
1059: }
1060: assertTrue("Expected Error", exception);
1061:
1062: Object readE1 = ois.readObject();
1063: Object readE2 = ois.readObject();
1064: assertTrue("Replaced objects should be identical",
1065: readE1 == readE2);
1066: Object readF1 = ois.readObject();
1067: Object readF2 = ois.readObject();
1068: assertTrue(
1069: "Replaced resolved objects should be identical: "
1070: + readF1 + " " + readF2, readF1 == readF2);
1071: } catch (IOException e) {
1072: fail("IOException serializing data : " + e.getMessage());
1073: } catch (ClassNotFoundException e) {
1074: fail("ClassNotFoundException serializing data : "
1075: + e.getMessage());
1076: }
1077: }
1078: }
|