1 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 ; 76 import java.io.ByteArrayOutputStream ; 77 import java.io.IOException ; 78 import java.io.ObjectInputStream ; 79 import java.io.ObjectOutputStream ; 80 81 82 88 public class SerializationTest 89 extends ExpressoTestCase { 90 91 private static final int REPEAT_TEST = 1000; 92 93 98 public SerializationTest(String testName) 99 throws Exception { 100 super(testName); 101 } 102 103 public static void main(String [] args) 104 throws Exception { 105 junit.textui.TestRunner.run(suite()); 106 } 107 108 public static junit.framework.Test suite() 109 throws Exception { 110 return new TestSuite(SerializationTest.class); 111 } 112 113 117 public void testSerializationWithTest2Object() 118 throws Exception { 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 ioe) { 131 ioe.printStackTrace(); 132 fail(ioe.getMessage()); 133 } 134 } 135 136 139 public void testSerializationWithTest1Object() 140 throws Exception { 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("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("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 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 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 244 public void testSecuredDBObject() 245 throws Exception { 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 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(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 ex) { 304 ex.printStackTrace(); 305 fail(ex.getMessage()); 306 } catch (IOException ex) { 307 ex.printStackTrace(); 308 fail(ex.getMessage()); 309 } catch (Exception ex) { 310 ex.printStackTrace(); 311 fail(ex.getMessage()); 312 } 313 314 } 315 316 private DataTransferObject runThroughSerializerTransfer(DataTransferObject testObject, boolean printSize) throws java.io.IOException , 317 java.lang.ClassNotFoundException { 318 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 319 ObjectOutputStream oos = new ObjectOutputStream (bos); 320 oos.writeObject(testObject); 321 if (printSize) { 322 System.out.println(testObject.getClass().getName() + " Serialized Size: " + bos.size()); 323 } 324 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 325 ObjectInputStream ois = new ObjectInputStream (bis); 326 327 DataTransferObject dto2 = (DataTransferObject) ois.readObject(); 328 return dto2; 329 330 } 331 332 340 private DBObject runThroughSerializer(DBObject testObject, boolean printSize) 341 throws Exception { 342 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 343 ObjectOutputStream oos = new ObjectOutputStream (bos); 344 oos.writeObject(testObject); 345 if (printSize) { 346 System.out.println(testObject.getClass().getName() + " Serialized Size: " + bos.size()); 347 } 348 349 ByteArrayInputStream bis = new ByteArrayInputStream (bos.toByteArray()); 350 ObjectInputStream ois = new ObjectInputStream (bis); 351 352 return (DBObject) ois.readObject(); 353 } 354 } | Popular Tags |