001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dbobj.tests;
066:
067: import com.jcorporate.expresso.core.dataobjects.DataTransferObject;
068: import com.jcorporate.expresso.core.db.DBException;
069: import com.jcorporate.expresso.core.dbobj.DBObject;
070: import com.jcorporate.expresso.services.dbobj.MimeTypes;
071: import com.jcorporate.expresso.services.test.ExpressoTestCase;
072: import com.jcorporate.expresso.services.test.TestSystemInitializer;
073: import junit.framework.TestSuite;
074:
075: import java.io.ByteArrayInputStream;
076: import java.io.ByteArrayOutputStream;
077: import java.io.IOException;
078: import java.io.ObjectInputStream;
079: import java.io.ObjectOutputStream;
080:
081: /**
082: * Tests to make sure serialization works for classes
083: *
084: * @author Michael Rimov
085: * @version $Revision: 1.2 $ $Date: 2004/11/17 20:48:22 $
086: */
087: public class SerializationTest extends ExpressoTestCase {
088:
089: private static final int REPEAT_TEST = 1000;
090:
091: /**
092: * Default testing constructor
093: *
094: * @param testName the Test Name
095: */
096: public SerializationTest(String testName) throws Exception {
097: super (testName);
098: }
099:
100: public static void main(String[] args) throws Exception {
101: junit.textui.TestRunner.run(suite());
102: }
103:
104: public static junit.framework.Test suite() throws Exception {
105: return new TestSuite(SerializationTest.class);
106: }
107:
108: /**
109: * Tests the les complicated test DBOBject that only has a couple of
110: * fields.
111: */
112: public void testSerializationWithTest2Object() throws Exception {
113: try {
114: Test2 t = new Test2();
115: t.setDataContext(TestSystemInitializer.getTestContext());
116: t.setField("TestKey", "34");
117: t.setField("Descrip", "This is a test descrip.");
118:
119: Test2 t2 = (Test2) runThroughSerializer(t, true);
120: assertTrue("Database not right", t2.getDataContext()
121: .equals(TestSystemInitializer.getTestContext()));
122: assertTrue("KeyField not right",
123: t2.getFieldInt("TestKey") == 34);
124: assertTrue("DescripField not right", t2.getField("Descrip")
125: .equals(t.getField("Descrip")));
126: } catch (java.io.IOException ioe) {
127: ioe.printStackTrace();
128: fail(ioe.getMessage());
129: }
130: }
131:
132: /**
133: * Tests the more complicated DBObject with basically all expresso types.
134: */
135: public void testSerializationWithTest1Object() throws Exception {
136: try {
137: Test1 t = new Test1();
138: t.setDataContext(TestSystemInitializer.getTestContext());
139: t.setField("TestKey", 34);
140: t.setField("BigIntTest", 450);
141: // t.setField("BinaryTest", "Test");
142: t.setField("BitTest", "1");
143: t.setField("BooleanTest", "true");
144: t.setField("CharTest", "Y");
145: t.setField("DateTest", "1980-01-01");
146: t.setField("DecimalTest", "1.23");
147: t.setField("DoubleTest", "1.00");
148: t.setField("FloatTest", "1.23");
149: t.setField("IntTest", 123);
150: t.setField("NumericTest", "234");
151: t.setField("RealTest", "1.11");
152: t.setField("SmallIntTest", "24");
153: t.setField("TimeTest", "01:01:00");
154: t.setField("VarCharTest", "This is a Varchar Test");
155: t.setField("TextTest", "This is a Text Test");
156:
157: Test1 t2 = (Test1) runThroughSerializer(t, true);
158: assertTrue("Database not right", t2.getDataContext()
159: .equals(TestSystemInitializer.getTestContext()));
160: assertTrue("KeyField not right",
161: t2.getFieldInt("TestKey") == 34);
162: assertTrue("TestKey Not Right", t2.getField("TestKey")
163: .equals("34"));
164: assertTrue("BigIntTest Not Right", t2
165: .getField("BigIntTest").equals("450"));
166: // assertTrue("BinaryTest Not Right",
167: // t2.getField("BinaryTest").equals("Test"));
168: assertTrue("BitTest Not Right", t2.getField("BitTest")
169: .equals("1"));
170: assertTrue("BooleanTest Not Right", t2.getField(
171: "BooleanTest").equals("true"));
172: assertTrue("CharTest Not Right", t2.getField("CharTest")
173: .equals("Y"));
174: assertTrue("DateTest Not Right", t2.getField("DateTest")
175: .equals("1980-01-01"));
176: assertTrue("DecimalTest Not Right", t2.getField(
177: "DecimalTest").equals("1.23"));
178: assertTrue("DoubleTest Not Right", t2
179: .getField("DoubleTest").equals("1.00"));
180: assertTrue("FloatTest Not Right", t2.getField("FloatTest")
181: .equals("1.23"));
182: assertTrue("IntTest Not Right", t2.getField("IntTest")
183: .equals("123"));
184: assertTrue("NumericTest Not Right", t2.getField(
185: "NumericTest").equals("234"));
186: assertTrue("RealTest Not Right", t2.getField("RealTest")
187: .equals("1.11"));
188: assertTrue("SmallIntTest Not Right", t2.getField(
189: "SmallIntTest").equals("24"));
190: assertTrue("TimeTest Not Right", t2.getField("TimeTest")
191: .equals("01:01:00"));
192: assertTrue("VarCharTest Not Right", t2.getField(
193: "VarCharTest").equals("This is a Varchar Test"));
194: assertTrue("TextTests Not Right", t2.getField("TextTest")
195: .equals("This is a Text Test"));
196: } catch (java.io.IOException ioe) {
197: ioe.printStackTrace();
198: fail(ioe.getMessage());
199: }
200: }
201:
202: public void testDataTransferObjectSerialization() {
203: try {
204:
205: MimeTypes mt = new MimeTypes();
206: mt.setRequestingUid(1);
207: mt.setDataContext(TestSystemInitializer.getTestContext());
208: mt.setField(MimeTypes.FLD_MIMENUMBER, 123);
209: mt.setField(MimeTypes.FLD_MIMETYPE, "text/html");
210: mt.setField(MimeTypes.FLD_DESCRIPTION, "HTML Document");
211: DataTransferObject dto = mt.getDataTransferObject();
212:
213: DataTransferObject dto2 = runThroughSerializerTransfer(dto,
214: true);
215: MimeTypes mt2 = (MimeTypes) DBObject.getThisDBbj(dto2);
216: mt2.setRequestingUid(1);
217:
218: assertTrue("Proper DB Name", mt2.getDataContext().equals(
219: TestSystemInitializer.getTestContext()));
220: assertTrue("Proper UID", mt2.getRequestingUid() == 1);
221: assertTrue("Mime Number Incorrect", mt2.getField(
222: MimeTypes.FLD_MIMENUMBER).equals("123"));
223: assertTrue("MimeType Incorrect", mt2.getField(
224: MimeTypes.FLD_MIMETYPE).equals("text/html"));
225: assertTrue("Mime Description Incorrect", mt2.getField(
226: MimeTypes.FLD_MIMETYPE).equals("text/html"));
227: } catch (DBException ex) {
228: ex.printStackTrace();
229: fail("Caught Exception: " + ex.getMessage());
230: } catch (IOException ex) {
231: ex.printStackTrace();
232: fail("Caught Exception: " + ex.getMessage());
233: } catch (ClassNotFoundException ex) {
234: ex.printStackTrace();
235: fail("Caught Exception: " + ex.getMessage());
236: }
237:
238: }
239:
240: /**
241: * Test to make sure secured DBObjects also serialize
242: */
243: public void testSecuredDBObject() throws Exception {
244: try {
245: MimeTypes mt = new MimeTypes();
246: mt.setRequestingUid(1);
247: mt.setDataContext(TestSystemInitializer.getTestContext());
248: mt.setField(MimeTypes.FLD_MIMENUMBER, 123);
249: mt.setField(MimeTypes.FLD_MIMETYPE, "text/html");
250: mt.setField(MimeTypes.FLD_DESCRIPTION, "HTML Document");
251:
252: try {
253: MimeTypes mt2 = (MimeTypes) runThroughSerializer(mt,
254: true);
255: assertTrue("Proper DB Name", mt2.getDataContext()
256: .equals(TestSystemInitializer.getTestContext()));
257: assertTrue("Proper UID", mt2.getRequestingUid() == 1);
258: assertTrue("Mime Number Incorrect", mt2.getField(
259: MimeTypes.FLD_MIMENUMBER).equals("123"));
260: assertTrue("MimeType Incorrect", mt2.getField(
261: MimeTypes.FLD_MIMETYPE).equals("text/html"));
262: assertTrue("Mime Description Incorrect", mt2.getField(
263: MimeTypes.FLD_MIMETYPE).equals("text/html"));
264: } catch (java.io.IOException ioe) {
265: ioe.printStackTrace();
266: fail(ioe.getMessage());
267: }
268: } catch (Exception 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(
287: dto, false);
288: }
289:
290: System.out.println("Total time for : " + REPEAT_TEST
291: + " data transfer serializations: "
292: + (System.currentTimeMillis() - startTime) + "ms.");
293:
294: startTime = System.currentTimeMillis();
295: for (int i = 0; i < REPEAT_TEST; i++) {
296: MimeTypes mt2 = (MimeTypes) runThroughSerializer(mt,
297: false);
298: }
299:
300: System.out.println("Total time for : " + REPEAT_TEST
301: + " mimetype serializations: "
302: + (System.currentTimeMillis() - startTime) + "ms.");
303:
304: } catch (DBException ex) {
305: ex.printStackTrace();
306: fail(ex.getMessage());
307: } catch (ClassNotFoundException ex) {
308: ex.printStackTrace();
309: fail(ex.getMessage());
310: } catch (IOException ex) {
311: ex.printStackTrace();
312: fail(ex.getMessage());
313: } catch (Exception ex) {
314: ex.printStackTrace();
315: fail(ex.getMessage());
316: }
317:
318: }
319:
320: private DataTransferObject runThroughSerializerTransfer(
321: DataTransferObject testObject, boolean printSize)
322: throws java.io.IOException,
323: java.lang.ClassNotFoundException {
324: ByteArrayOutputStream bos = new ByteArrayOutputStream();
325: ObjectOutputStream oos = new ObjectOutputStream(bos);
326: oos.writeObject(testObject);
327: if (printSize) {
328: System.out.println(testObject.getClass().getName()
329: + " Serialized Size: " + bos.size());
330: }
331: ByteArrayInputStream bis = new ByteArrayInputStream(bos
332: .toByteArray());
333: ObjectInputStream ois = new ObjectInputStream(bis);
334:
335: DataTransferObject dto2 = (DataTransferObject) ois.readObject();
336: return dto2;
337:
338: }
339:
340: /**
341: * Helper functino that serializes a DBOBject to a memory stream and then
342: * reads a new instance out from the stream.
343: *
344: * @param testObject the DBObject to attempt to serialize
345: * @param printSize True if you want the function to print out the object's serialized size
346: * @return the deserialized DBObject
347: */
348: private DBObject runThroughSerializer(DBObject testObject,
349: boolean printSize) throws Exception {
350: ByteArrayOutputStream bos = new ByteArrayOutputStream();
351: ObjectOutputStream oos = new ObjectOutputStream(bos);
352: oos.writeObject(testObject);
353: if (printSize) {
354: System.out.println(testObject.getClass().getName()
355: + " Serialized Size: " + bos.size());
356: }
357:
358: ByteArrayInputStream bis = new ByteArrayInputStream(bos
359: .toByteArray());
360: ObjectInputStream ois = new ObjectInputStream(bis);
361:
362: return (DBObject) ois.readObject();
363: }
364: }
|