KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > JDK15SerializationTest


1 /*
2  * Copyright (c) 2003-2006 Terracotta, Inc. All rights reserved.
3  */

4 package com.tctest;
5
6 import com.tc.object.BaseDSOTestCase;
7
8 import java.io.ByteArrayInputStream JavaDoc;
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.Serializable JavaDoc;
14 import java.util.concurrent.ConcurrentHashMap JavaDoc;
15 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
16 import java.util.concurrent.locks.ReentrantLock JavaDoc;
17
18 /*
19  * TODO:: More tests needed. 1) Populated collections needs to be serialized and deserialized. 2) Serialized
20  * instrumented version of collections/objects should be deserializable by uninstrumented versions and vice versa
21  */

22 public class JDK15SerializationTest extends BaseDSOTestCase {
23   public void testConcurrentHashMapSerialization() throws Exception JavaDoc {
24     ConcurrentHashMap JavaDoc m = new ConcurrentHashMap JavaDoc();
25
26     m.put("key1", "value1");
27     m.put("key2", "value2");
28
29     checkSerialization(m);
30
31   }
32
33   public void testLinkedBlockingQueueSerialization() throws Exception JavaDoc {
34     LinkedBlockingQueue JavaDoc q = new LinkedBlockingQueue JavaDoc();
35     q.put("value1");
36     q.put("value2");
37
38     checkSerialization(q);
39   }
40   
41   public void testReentrantLockSerialization() throws Exception JavaDoc {
42     ReentrantLock JavaDoc lock = new ReentrantLock JavaDoc();
43     
44     lock.lock();
45     try {
46       ReentrantLock JavaDoc deserializedLock = (ReentrantLock JavaDoc)checkSerialization(lock);
47       assertFalse(deserializedLock.isLocked());
48       assertTrue(lock.isLocked());
49     } finally {
50       lock.unlock();
51     }
52   }
53
54   private Object JavaDoc checkSerialization(Object JavaDoc o) throws Exception JavaDoc {
55     if (!(o instanceof Serializable JavaDoc)) {
56       System.err.println("Skipping non-serializable " + o.getClass().getName());
57       return null;
58     }
59
60     return validateSerialization(o);
61   }
62   
63   private Object JavaDoc validateSerialization(Object JavaDoc o) throws Exception JavaDoc {
64     System.out.println("TESTING " + o.getClass());
65     assertTrue(o instanceof Serializable JavaDoc);
66
67     return deserialize(serialize(o));
68   }
69   
70   private static byte[] serialize(Object JavaDoc obj) throws IOException JavaDoc {
71     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
72     ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
73     oos.writeObject(obj);
74     oos.close();
75     return baos.toByteArray();
76   }
77   
78   private static Object JavaDoc deserialize(byte[] data) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
79     ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(data));
80     Object JavaDoc rv = ois.readObject();
81     ois.close();
82     return rv;
83   }
84
85 }
86
Popular Tags