1 10 11 package com.triactive.jdo.test; 12 13 import com.triactive.jdo.SCO; 14 import java.io.BufferedInputStream ; 15 import java.io.BufferedOutputStream ; 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import javax.jdo.PersistenceManager; 22 import javax.jdo.Transaction; 23 import org.apache.log4j.Category; 24 25 26 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 name) 44 { 45 super(name); 46 } 47 48 49 protected void setUp() throws Exception 50 { 51 super.setUp(); 52 53 if (!schemaInitialized) 54 { 55 addClassesToSchema(new Class [] 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 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 94 { 95 runSerializationTestFor(StringWidget.class); 96 } 97 98 public void testBinaryWidgets() throws Exception 99 { 100 runSerializationTestFor(BinaryWidget.class); 101 } 102 103 public void testDateWidgets() throws Exception 104 { 105 runSerializationTestFor(DateWidget.class); 106 } 107 108 public void testDecimalWidgets() throws Exception 109 { 110 runSerializationTestFor(DecimalWidget.class); 111 } 112 113 public void testFloatWidgets() throws Exception 114 { 115 runSerializationTestFor(FloatWidget.class); 116 } 117 118 public void testCollectionWidgets() throws Exception 119 { 120 runSerializationTestFor(CollectionWidget.class); 121 } 122 123 public void testSetWidgets() throws Exception 124 { 125 runSerializationTestFor(SetWidget.class); 126 } 127 128 public void testMapWidgets() throws Exception 129 { 130 runSerializationTestFor(MapWidget.class); 131 } 132 133 public void testHashtableWidgets() throws Exception 134 { 135 runSerializationTestFor(HashtableWidget.class); 136 } 137 138 public void runSerializationTestFor(Class c) throws Exception 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 [] 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 [] objs) throws IOException 188 { 189 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 190 ObjectOutputStream oout = new ObjectOutputStream (new BufferedOutputStream (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 [] objs) throws IOException , ClassNotFoundException 202 { 203 ByteArrayInputStream bin = new ByteArrayInputStream (ba); 204 ObjectInputStream oin = new ObjectInputStream (new BufferedInputStream (bin)); 205 206 for (int i = 0; i < objs.length; ++i) 207 objs[i] = oin.readObject(); 208 } 209 } 210 | Popular Tags |