001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: SerializationTest.java,v 1.4 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import com.triactive.jdo.SCO;
014: import java.io.BufferedInputStream;
015: import java.io.BufferedOutputStream;
016: import java.io.ByteArrayInputStream;
017: import java.io.ByteArrayOutputStream;
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import javax.jdo.PersistenceManager;
022: import javax.jdo.Transaction;
023: import org.apache.log4j.Category;
024:
025: /**
026: * Tests the serializability of persistent objects.
027: *
028: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
029: * @version $Revision: 1.4 $
030: */
031:
032: public class SerializationTest extends PersistenceTestCase {
033: private static final Category LOG = Category
034: .getInstance(SerializationTest.class);
035: private static final int TEST_OBJECT_COUNT = 10;
036:
037: private PersistenceManager pm;
038: private Transaction tx;
039: private boolean schemaInitialized = false;
040:
041: public SerializationTest(String name) {
042: super (name);
043: }
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047:
048: if (!schemaInitialized) {
049: addClassesToSchema(new Class[] { Widget.class,
050: DateWidget.class, DecimalWidget.class,
051: FloatWidget.class, StringWidget.class,
052: BinaryWidget.class, CollectionWidget.class,
053: SetWidget.class, ElementWidget.class,
054: MapWidget.class, ValueWidget.class,
055: HashtableWidget.class });
056:
057: schemaInitialized = true;
058: }
059:
060: pm = pmf.getPersistenceManager();
061: tx = pm.currentTransaction();
062:
063: tx.setRetainValues(true);
064: }
065:
066: protected void tearDown() throws Exception {
067: if (tx.isActive())
068: tx.rollback();
069:
070: pm.close();
071:
072: super .tearDown();
073: }
074:
075: public void testStringWidgets() throws Exception {
076: runSerializationTestFor(StringWidget.class);
077: }
078:
079: public void testBinaryWidgets() throws Exception {
080: runSerializationTestFor(BinaryWidget.class);
081: }
082:
083: public void testDateWidgets() throws Exception {
084: runSerializationTestFor(DateWidget.class);
085: }
086:
087: public void testDecimalWidgets() throws Exception {
088: runSerializationTestFor(DecimalWidget.class);
089: }
090:
091: public void testFloatWidgets() throws Exception {
092: runSerializationTestFor(FloatWidget.class);
093: }
094:
095: public void testCollectionWidgets() throws Exception {
096: runSerializationTestFor(CollectionWidget.class);
097: }
098:
099: public void testSetWidgets() throws Exception {
100: runSerializationTestFor(SetWidget.class);
101: }
102:
103: public void testMapWidgets() throws Exception {
104: runSerializationTestFor(MapWidget.class);
105: }
106:
107: public void testHashtableWidgets() throws Exception {
108: runSerializationTestFor(HashtableWidget.class);
109: }
110:
111: public void runSerializationTestFor(Class c) throws Exception {
112: TestObject[] persistent = new TestObject[TEST_OBJECT_COUNT];
113:
114: tx.begin();
115:
116: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
117: persistent[i] = (TestObject) c.newInstance();
118: persistent[i].fillRandom();
119: }
120:
121: pm.makePersistentAll(persistent);
122:
123: tx.commit();
124:
125: TestObject[] deserialized = new TestObject[TEST_OBJECT_COUNT];
126: toObjects(toByteArray(persistent), deserialized);
127:
128: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
129: TestObject p = persistent[i];
130: TestObject d = deserialized[i];
131:
132: assertTrue(
133: "Object did not deserialize into a different object",
134: p != d);
135:
136: if (d instanceof HasSCOFields) {
137: Object[] vals = ((HasSCOFields) d).getSCOFieldValues();
138:
139: for (int j = 0; j < vals.length; ++j)
140: assertTrue(
141: "Deserialized object has a wrapper object in an SCO field",
142: !(vals[j] instanceof SCO));
143: }
144:
145: assertFieldsEqual(p, d);
146: }
147:
148: tx.begin();
149: pm.deletePersistentAll(persistent);
150: tx.commit();
151: }
152:
153: private void assertFieldsEqual(TestObject expected,
154: TestObject actual) {
155: assertTrue("Incorrect field values in object, was " + actual
156: + ", should be " + expected, actual.compareTo(expected));
157: }
158:
159: private static byte[] toByteArray(Object[] objs) throws IOException {
160: ByteArrayOutputStream bout = new ByteArrayOutputStream();
161: ObjectOutputStream oout = new ObjectOutputStream(
162: new BufferedOutputStream(bout));
163:
164: for (int i = 0; i < objs.length; ++i)
165: oout.writeObject(objs[i]);
166:
167: oout.close();
168:
169: return bout.toByteArray();
170: }
171:
172: private static void toObjects(byte[] ba, Object[] objs)
173: throws IOException, ClassNotFoundException {
174: ByteArrayInputStream bin = new ByteArrayInputStream(ba);
175: ObjectInputStream oin = new ObjectInputStream(
176: new BufferedInputStream(bin));
177:
178: for (int i = 0; i < objs.length; ++i)
179: objs[i] = oin.readObject();
180: }
181: }
|