01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util;
04:
05: import com.tc.logging.TCLogger;
06: import com.tc.logging.TCLogging;
07:
08: import java.io.ByteArrayInputStream;
09: import java.io.ByteArrayOutputStream;
10: import java.io.ObjectInputStream;
11: import java.io.ObjectOutputStream;
12:
13: import junit.framework.Assert;
14:
15: /**
16: * Utilities for use in testing serialization.
17: */
18: public class SerializationTestUtil {
19:
20: private static final TCLogger logger = TCLogging
21: .getLogger(SerializationTestUtil.class);
22:
23: public static void testSerialization(Object o) throws Exception {
24: testSerialization(o, false, false);
25: }
26:
27: public static void testSerializationWithRestore(Object o)
28: throws Exception {
29: testSerialization(o, true, false);
30: }
31:
32: public static void testSerializationAndEquals(Object o)
33: throws Exception {
34: testSerialization(o, true, true);
35: }
36:
37: public static void testSerialization(Object o,
38: boolean checkRestore, boolean checkEquals) throws Exception {
39: Assert.assertNotNull(o);
40:
41: ByteArrayOutputStream baos = new ByteArrayOutputStream();
42: ObjectOutputStream oos = new ObjectOutputStream(baos);
43:
44: oos.writeObject(o);
45: oos.flush();
46:
47: logger.debug("Object " + o + " of class " + o.getClass()
48: + " serialized to " + baos.toByteArray().length
49: + " bytes.");
50:
51: if (checkRestore) {
52: ByteArrayInputStream bais = new ByteArrayInputStream(baos
53: .toByteArray());
54: ObjectInputStream ois = new ObjectInputStream(bais);
55:
56: Object restored = ois.readObject();
57:
58: if (checkEquals) {
59: Assert.assertEquals(o, restored);
60: Assert.assertEquals(restored, o);
61:
62: Assert.assertEquals(o.hashCode(), restored.hashCode());
63:
64: Assert.assertNotSame(o, restored);
65: }
66: }
67: }
68:
69: }
|