KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > tests > SerializationTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dbobj.tests;
66
67 import com.jcorporate.expresso.core.dataobjects.DataTransferObject;
68 import com.jcorporate.expresso.core.db.DBException;
69 import com.jcorporate.expresso.core.dbobj.DBObject;
70 import com.jcorporate.expresso.services.dbobj.MimeTypes;
71 import com.jcorporate.expresso.services.test.ExpressoTestCase;
72 import com.jcorporate.expresso.services.test.TestSystemInitializer;
73 import junit.framework.TestSuite;
74
75 import java.io.ByteArrayInputStream JavaDoc;
76 import java.io.ByteArrayOutputStream JavaDoc;
77 import java.io.IOException JavaDoc;
78 import java.io.ObjectInputStream JavaDoc;
79 import java.io.ObjectOutputStream JavaDoc;
80
81
82 /**
83  * Tests to make sure serialization works for classes
84  *
85  * @author Michael Rimov
86  * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:22 $
87  */

88 public class SerializationTest
89         extends ExpressoTestCase {
90
91     private static final int REPEAT_TEST = 1000;
92
93     /**
94      * Default testing constructor
95      *
96      * @param testName the Test Name
97      */

98     public SerializationTest(String JavaDoc testName)
99             throws Exception JavaDoc {
100         super(testName);
101     }
102
103     public static void main(String JavaDoc[] args)
104             throws Exception JavaDoc {
105         junit.textui.TestRunner.run(suite());
106     }
107
108     public static junit.framework.Test suite()
109             throws Exception JavaDoc {
110         return new TestSuite(SerializationTest.class);
111     }
112
113     /**
114      * Tests the les complicated test DBOBject that only has a couple of
115      * fields.
116      */

117     public void testSerializationWithTest2Object()
118             throws Exception JavaDoc {
119         try {
120             Test2 t = new Test2();
121             t.setDataContext(TestSystemInitializer.getTestContext());
122             t.setField("TestKey", "34");
123             t.setField("Descrip", "This is a test descrip.");
124
125             Test2 t2 = (Test2) runThroughSerializer(t, true);
126             assertTrue("Database not right", t2.getDataContext().equals(TestSystemInitializer.getTestContext()));
127             assertTrue("KeyField not right", t2.getFieldInt("TestKey") == 34);
128             assertTrue("DescripField not right",
129                     t2.getField("Descrip").equals(t.getField("Descrip")));
130         } catch (java.io.IOException JavaDoc ioe) {
131             ioe.printStackTrace();
132             fail(ioe.getMessage());
133         }
134     }
135
136     /**
137      * Tests the more complicated DBObject with basically all expresso types.
138      */

139     public void testSerializationWithTest1Object()
140             throws Exception JavaDoc {
141         try {
142             Test1 t = new Test1();
143             t.setDataContext(TestSystemInitializer.getTestContext());
144             t.setField("TestKey", 34);
145             t.setField("BigIntTest", 450);
146 // t.setField("BinaryTest", "Test");
147
t.setField("BitTest", "1");
148             t.setField("BooleanTest", "true");
149             t.setField("CharTest", "Y");
150             t.setField("DateTest", "1980-01-01");
151             t.setField("DecimalTest", "1.23");
152             t.setField("DoubleTest", "1.00");
153             t.setField("FloatTest", "1.23");
154             t.setField("IntTest", 123);
155             t.setField("NumericTest", "234");
156             t.setField("RealTest", "1.11");
157             t.setField("SmallIntTest", "24");
158             t.setField("TimeTest", "01:01:00");
159             t.setField("VarCharTest", "This is a Varchar Test");
160             t.setField("TextTest", "This is a Text Test");
161
162             Test1 t2 = (Test1) runThroughSerializer(t, true);
163             assertTrue("Database not right", t2.getDataContext().equals(TestSystemInitializer.getTestContext()));
164             assertTrue("KeyField not right", t2.getFieldInt("TestKey") == 34);
165             assertTrue("TestKey Not Right",
166                     t2.getField("TestKey").equals("34"));
167             assertTrue("BigIntTest Not Right",
168                     t2.getField("BigIntTest").equals("450"));
169 // assertTrue("BinaryTest Not Right",
170
// t2.getField("BinaryTest").equals("Test"));
171
assertTrue("BitTest Not Right", t2.getField("BitTest").equals("1"));
172             assertTrue("BooleanTest Not Right",
173                     t2.getField("BooleanTest").equals("true"));
174             assertTrue("CharTest Not Right",
175                     t2.getField("CharTest").equals("Y"));
176             assertTrue("DateTest Not Right",
177                     t2.getField("DateTest").equals("1980-01-01"));
178             assertTrue("DecimalTest Not Right",
179                     t2.getField("DecimalTest").equals("1.23"));
180             assertTrue("DoubleTest Not Right",
181                     t2.getField("DoubleTest").equals("1.00"));
182             assertTrue("FloatTest Not Right",
183                     t2.getField("FloatTest").equals("1.23"));
184             assertTrue("IntTest Not Right",
185                     t2.getField("IntTest").equals("123"));
186             assertTrue("NumericTest Not Right",
187                     t2.getField("NumericTest").equals("234"));
188             assertTrue("RealTest Not Right",
189                     t2.getField("RealTest").equals("1.11"));
190             assertTrue("SmallIntTest Not Right",
191                     t2.getField("SmallIntTest").equals("24"));
192             assertTrue("TimeTest Not Right",
193                     t2.getField("TimeTest").equals("01:01:00"));
194             assertTrue("VarCharTest Not Right",
195                     t2.getField("VarCharTest").equals("This is a Varchar Test"));
196             assertTrue("TextTests Not Right",
197                     t2.getField("TextTest").equals("This is a Text Test"));
198         } catch (java.io.IOException JavaDoc ioe) {
199             ioe.printStackTrace();
200             fail(ioe.getMessage());
201         }
202     }
203
204     public void testDataTransferObjectSerialization() {
205         try {
206
207             MimeTypes mt = new MimeTypes();
208             mt.setRequestingUid(1);
209             mt.setDataContext(TestSystemInitializer.getTestContext());
210             mt.setField(MimeTypes.FLD_MIMENUMBER, 123);
211             mt.setField(MimeTypes.FLD_MIMETYPE, "text/html");
212             mt.setField(MimeTypes.FLD_DESCRIPTION, "HTML Document");
213             DataTransferObject dto = mt.getDataTransferObject();
214
215             DataTransferObject dto2 = runThroughSerializerTransfer(dto, true);
216             MimeTypes mt2 = (MimeTypes) DBObject.getThisDBbj(dto2);
217             mt2.setRequestingUid(1);
218
219             assertTrue("Proper DB Name", mt2.getDataContext().equals(TestSystemInitializer.getTestContext()));
220             assertTrue("Proper UID", mt2.getRequestingUid() == 1);
221             assertTrue("Mime Number Incorrect",
222                     mt2.getField(MimeTypes.FLD_MIMENUMBER).equals("123"));
223             assertTrue("MimeType Incorrect",
224                     mt2.getField(MimeTypes.FLD_MIMETYPE).equals("text/html"));
225             assertTrue("Mime Description Incorrect",
226                     mt2.getField(MimeTypes.FLD_MIMETYPE).equals("text/html"));
227         } catch (DBException ex) {
228             ex.printStackTrace();
229             fail("Caught Exception: " + ex.getMessage());
230         } catch (IOException JavaDoc ex) {
231             ex.printStackTrace();
232             fail("Caught Exception: " + ex.getMessage());
233         } catch (ClassNotFoundException JavaDoc ex) {
234             ex.printStackTrace();
235             fail("Caught Exception: " + ex.getMessage());
236         }
237
238     }
239
240
241     /**
242      * Test to make sure secured DBObjects also serialize
243      */

244     public void testSecuredDBObject()
245             throws Exception JavaDoc {
246         try {
247             MimeTypes mt = new MimeTypes();
248             mt.setRequestingUid(1);
249             mt.setDataContext(TestSystemInitializer.getTestContext());
250             mt.setField(MimeTypes.FLD_MIMENUMBER, 123);
251             mt.setField(MimeTypes.FLD_MIMETYPE, "text/html");
252             mt.setField(MimeTypes.FLD_DESCRIPTION, "HTML Document");
253
254             try {
255                 MimeTypes mt2 = (MimeTypes) runThroughSerializer(mt, true);
256                 assertTrue("Proper DB Name", mt2.getDataContext().equals(TestSystemInitializer.getTestContext()));
257                 assertTrue("Proper UID", mt2.getRequestingUid() == 1);
258                 assertTrue("Mime Number Incorrect",
259                         mt2.getField(MimeTypes.FLD_MIMENUMBER).equals("123"));
260                 assertTrue("MimeType Incorrect",
261                         mt2.getField(MimeTypes.FLD_MIMETYPE).equals("text/html"));
262                 assertTrue("Mime Description Incorrect",
263                         mt2.getField(MimeTypes.FLD_MIMETYPE).equals("text/html"));
264             } catch (java.io.IOException JavaDoc ioe) {
265                 ioe.printStackTrace();
266                 fail(ioe.getMessage());
267             }
268         } catch (Exception JavaDoc ex) {
269             ex.printStackTrace();
270             fail(ex.getMessage());
271         }
272     }
273
274     public void testStressSerialization() {
275         try {
276             long startTime = System.currentTimeMillis();
277
278             MimeTypes mt = new MimeTypes();
279             mt.setRequestingUid(1);
280             mt.setDataContext(TestSystemInitializer.getTestContext());
281             mt.setField(MimeTypes.FLD_MIMENUMBER, 123);
282             mt.setField(MimeTypes.FLD_MIMETYPE, "text/html");
283             mt.setField(MimeTypes.FLD_DESCRIPTION, "HTML Document");
284             DataTransferObject dto = mt.getDataTransferObject();
285             for (int i = 0; i < REPEAT_TEST; i++) {
286                 DataTransferObject dto2 = runThroughSerializerTransfer(dto, false);
287             }
288
289             System.out.println("Total time for : " + REPEAT_TEST + " data transfer serializations: " +
290                     (System.currentTimeMillis() - startTime) + "ms.");
291
292             startTime = System.currentTimeMillis();
293             for (int i = 0; i < REPEAT_TEST; i++) {
294                 MimeTypes mt2 = (MimeTypes) runThroughSerializer(mt, false);
295             }
296
297             System.out.println("Total time for : " + REPEAT_TEST + " mimetype serializations: " +
298                     (System.currentTimeMillis() - startTime) + "ms.");
299
300         } catch (DBException ex) {
301             ex.printStackTrace();
302             fail(ex.getMessage());
303         } catch (ClassNotFoundException JavaDoc ex) {
304             ex.printStackTrace();
305             fail(ex.getMessage());
306         } catch (IOException JavaDoc ex) {
307             ex.printStackTrace();
308             fail(ex.getMessage());
309         } catch (Exception JavaDoc ex) {
310             ex.printStackTrace();
311             fail(ex.getMessage());
312         }
313
314     }
315
316     private DataTransferObject runThroughSerializerTransfer(DataTransferObject testObject, boolean printSize) throws java.io.IOException JavaDoc,
317             java.lang.ClassNotFoundException JavaDoc {
318         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
319         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
320         oos.writeObject(testObject);
321         if (printSize) {
322             System.out.println(testObject.getClass().getName() + " Serialized Size: " + bos.size());
323         }
324         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
325         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
326
327         DataTransferObject dto2 = (DataTransferObject) ois.readObject();
328         return dto2;
329
330     }
331
332     /**
333      * Helper functino that serializes a DBOBject to a memory stream and then
334      * reads a new instance out from the stream.
335      *
336      * @param testObject the DBObject to attempt to serialize
337      * @param printSize True if you want the function to print out the object's serialized size
338      * @return the deserialized DBObject
339      */

340     private DBObject runThroughSerializer(DBObject testObject, boolean printSize)
341             throws Exception JavaDoc {
342         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
343         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
344         oos.writeObject(testObject);
345         if (printSize) {
346             System.out.println(testObject.getClass().getName() + " Serialized Size: " + bos.size());
347         }
348
349         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
350         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
351
352         return (DBObject) ois.readObject();
353     }
354 }
Popular Tags