KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > SerializationTest


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SerializationTest.java,v 1.4 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import com.triactive.jdo.SCO;
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import javax.jdo.PersistenceManager;
22 import javax.jdo.Transaction;
23 import org.apache.log4j.Category;
24
25
26 /**
27  * Tests the serializability of persistent objects.
28  *
29  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
30  * @version $Revision: 1.4 $
31  */

32
33 public class SerializationTest extends PersistenceTestCase
34 {
35     private static final Category LOG = Category.getInstance(SerializationTest.class);
36     private static final int TEST_OBJECT_COUNT = 10;
37
38     private PersistenceManager pm;
39     private Transaction tx;
40     private boolean schemaInitialized = false;
41
42
43     public SerializationTest(String JavaDoc name)
44     {
45         super(name);
46     }
47
48
49     protected void setUp() throws Exception JavaDoc
50     {
51         super.setUp();
52
53         if (!schemaInitialized)
54         {
55             addClassesToSchema(new Class JavaDoc[]
56                 {
57                     Widget.class,
58                     DateWidget.class,
59                     DecimalWidget.class,
60                     FloatWidget.class,
61                     StringWidget.class,
62                     BinaryWidget.class,
63                     CollectionWidget.class,
64                     SetWidget.class,
65                     ElementWidget.class,
66                     MapWidget.class,
67                     ValueWidget.class,
68                     HashtableWidget.class
69                 }
70             );
71
72             schemaInitialized = true;
73         }
74
75         pm = pmf.getPersistenceManager();
76         tx = pm.currentTransaction();
77
78         tx.setRetainValues(true);
79     }
80
81
82     protected void tearDown() throws Exception JavaDoc
83     {
84         if (tx.isActive())
85             tx.rollback();
86
87         pm.close();
88
89         super.tearDown();
90     }
91
92
93     public void testStringWidgets() throws Exception JavaDoc
94     {
95         runSerializationTestFor(StringWidget.class);
96     }
97
98     public void testBinaryWidgets() throws Exception JavaDoc
99     {
100         runSerializationTestFor(BinaryWidget.class);
101     }
102
103     public void testDateWidgets() throws Exception JavaDoc
104     {
105         runSerializationTestFor(DateWidget.class);
106     }
107
108     public void testDecimalWidgets() throws Exception JavaDoc
109     {
110         runSerializationTestFor(DecimalWidget.class);
111     }
112
113     public void testFloatWidgets() throws Exception JavaDoc
114     {
115         runSerializationTestFor(FloatWidget.class);
116     }
117
118     public void testCollectionWidgets() throws Exception JavaDoc
119     {
120         runSerializationTestFor(CollectionWidget.class);
121     }
122
123     public void testSetWidgets() throws Exception JavaDoc
124     {
125         runSerializationTestFor(SetWidget.class);
126     }
127
128     public void testMapWidgets() throws Exception JavaDoc
129     {
130         runSerializationTestFor(MapWidget.class);
131     }
132
133     public void testHashtableWidgets() throws Exception JavaDoc
134     {
135         runSerializationTestFor(HashtableWidget.class);
136     }
137
138     public void runSerializationTestFor(Class JavaDoc c) throws Exception JavaDoc
139     {
140         TestObject[] persistent = new TestObject[TEST_OBJECT_COUNT];
141
142         tx.begin();
143
144         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
145         {
146             persistent[i] = (TestObject)c.newInstance();
147             persistent[i].fillRandom();
148         }
149
150         pm.makePersistentAll(persistent);
151
152         tx.commit();
153
154         TestObject[] deserialized = new TestObject[TEST_OBJECT_COUNT];
155         toObjects(toByteArray(persistent), deserialized);
156
157         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
158         {
159             TestObject p = persistent[i];
160             TestObject d = deserialized[i];
161
162             assertTrue("Object did not deserialize into a different object", p != d);
163
164             if (d instanceof HasSCOFields)
165             {
166                 Object JavaDoc[] vals = ((HasSCOFields)d).getSCOFieldValues();
167
168                 for (int j = 0; j < vals.length; ++j)
169                     assertTrue("Deserialized object has a wrapper object in an SCO field", !(vals[j] instanceof SCO));
170             }
171
172             assertFieldsEqual(p, d);
173         }
174
175         tx.begin();
176         pm.deletePersistentAll(persistent);
177         tx.commit();
178     }
179
180
181     private void assertFieldsEqual(TestObject expected, TestObject actual)
182     {
183         assertTrue("Incorrect field values in object, was " + actual + ", should be " + expected, actual.compareTo(expected));
184     }
185
186
187     private static byte[] toByteArray(Object JavaDoc[] objs) throws IOException JavaDoc
188     {
189         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
190         ObjectOutputStream JavaDoc oout = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(bout));
191
192         for (int i = 0; i < objs.length; ++i)
193             oout.writeObject(objs[i]);
194
195         oout.close();
196
197         return bout.toByteArray();
198     }
199
200
201     private static void toObjects(byte[] ba, Object JavaDoc[] objs) throws IOException JavaDoc, ClassNotFoundException JavaDoc
202     {
203         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(ba);
204         ObjectInputStream JavaDoc oin = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(bin));
205
206         for (int i = 0; i < objs.length; ++i)
207             objs[i] = oin.readObject();
208     }
209 }
210
Popular Tags