KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > SerializationTestUtils


1 /*
2  * The Spring Framework is published under the terms
3  * of the Apache Software License.
4  */

5
6 package org.springframework.util;
7
8 import java.awt.Point JavaDoc;
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.NotSerializableException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.Serializable JavaDoc;
17
18 import junit.framework.TestCase;
19
20 import org.springframework.beans.TestBean;
21
22 /**
23  * Utilities for testing serializability of objects.
24  * Exposes static methods for use in other test cases.
25  * Extends TestCase only to test itself.
26  *
27  * @author Rod Johnson
28  */

29 public class SerializationTestUtils extends TestCase {
30     
31     public static void testSerialization(Object JavaDoc o) throws IOException JavaDoc {
32         OutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
33         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
34         oos.writeObject(o);
35     }
36     
37     public static boolean isSerializable(Object JavaDoc o) throws IOException JavaDoc {
38         try {
39             testSerialization(o);
40             return true;
41         }
42         catch (NotSerializableException JavaDoc ex) {
43             return false;
44         }
45     }
46     
47     public static Object JavaDoc serializeAndDeserialize(Object JavaDoc o) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
48         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
49         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
50         oos.writeObject(o);
51         oos.flush();
52         baos.flush();
53         byte[] bytes = baos.toByteArray();
54         
55         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(bytes);
56         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(is);
57         Object JavaDoc o2 = ois.readObject();
58         
59         return o2;
60     }
61     
62     public SerializationTestUtils(String JavaDoc s) {
63         super(s);
64     }
65     
66     public void testWithNonSerializableObject() throws IOException JavaDoc {
67         TestBean o = new TestBean();
68         assertFalse(o instanceof Serializable JavaDoc);
69         
70         assertFalse(isSerializable(o));
71         
72         try {
73             testSerialization(o);
74             fail();
75         }
76         catch (NotSerializableException JavaDoc ex) {
77             // Ok
78
}
79     }
80     
81     public void testWithSerializableObject() throws Exception JavaDoc {
82         int x = 5;
83         int y = 10;
84         Point JavaDoc p = new Point JavaDoc(x, y);
85         assertTrue(p instanceof Serializable JavaDoc);
86     
87         testSerialization(p);
88         
89         assertTrue(isSerializable(p));
90         
91         Point JavaDoc p2 = (Point JavaDoc) serializeAndDeserialize(p);
92         assertNotSame(p, p2);
93         assertEquals(x, (int) p2.getX());
94         assertEquals(y, (int) p2.getY());
95     }
96
97 }
98
Popular Tags