KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > serializer > DSOSerializerPolicyTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.io.serializer;
5
6 import com.tc.io.serializer.api.BasicSerializer;
7 import com.tc.io.serializer.api.Serializer;
8 import com.tc.object.ObjectID;
9 import com.tc.objectserver.persistence.impl.NullStringIndexPersistor;
10 import com.tc.objectserver.persistence.impl.StringIndexImpl;
11 import com.tc.util.TCAssertionError;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15
16 import junit.framework.TestCase;
17
18 public class DSOSerializerPolicyTest extends TestCase {
19
20   private Serializer serializer;
21
22   public void test() throws Exception JavaDoc {
23     DSOSerializerPolicy policy = new DSOSerializerPolicy(new StringIndexImpl(new NullStringIndexPersistor()));
24     serializer = new BasicSerializer(policy);
25
26     ObjectID oid = new ObjectID(1);
27     assertEquals(oid, serialize(oid));
28
29     Boolean JavaDoc bool = new Boolean JavaDoc(true);
30     assertEquals(bool, serialize(bool));
31
32     Byte JavaDoc b = new Byte JavaDoc((byte) 1);
33     assertEquals(b, serialize(b));
34
35     Character JavaDoc c = new Character JavaDoc('c');
36     assertEquals(c, serialize(c));
37
38     Double JavaDoc d = new Double JavaDoc(1);
39     assertEquals(d, serialize(d));
40
41     Float JavaDoc f = new Float JavaDoc(1);
42     assertEquals(f, serialize(f));
43
44     Integer JavaDoc i = new Integer JavaDoc(1);
45     assertEquals(i, serialize(i));
46
47     Long JavaDoc l = new Long JavaDoc(1);
48     assertEquals(l, serialize(l));
49
50     Short JavaDoc s = new Short JavaDoc((short) 1);
51     assertEquals(s, serialize(s));
52
53     String JavaDoc string = "orion";
54     assertEquals(string, serialize(string));
55
56     Object JavaDoc o = new java.util.Date JavaDoc();
57     try {
58       assertEquals(o, serialize(o));
59       assertTrue(false);
60     } catch (TCAssertionError ar) {
61       // Normal as Date Objects are not supported natively
62
}
63   }
64
65   private Object JavaDoc serialize(Object JavaDoc o) throws Exception JavaDoc {
66     ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
67     TCObjectOutputStream out = new TCObjectOutputStream(bout);
68
69     serializer.serializeTo(o, out);
70     out.flush();
71
72     ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(bout.toByteArray());
73     TCObjectInputStream in = new TCObjectInputStream(bin);
74     return serializer.deserializeFrom(in);
75   }
76 }
77
Popular Tags