1 18 package org.objectweb.speedo.runtime.basic; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.mapper.lib.Object2StringSerializer; 22 import org.objectweb.util.monolog.api.BasicLevel; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 27 31 public class TestObject2StringSerializer extends SpeedoTestHelper { 32 33 String output; 34 35 public TestObject2StringSerializer(String s) { 36 super(s); 37 output = System.getProperty("out.test", File.separatorChar + "tmp"); 38 logger.log(BasicLevel.DEBUG, "output=" + output); 39 } 40 41 protected String getLoggerName() { 42 return "org.objectweb.speedo.runtime.basic.TestObject2StringSerializer"; 43 } 44 45 public void testSerialization() { 46 testSerialization("fdfdmfd,ng,nbdlfdfgkf,n"); 47 } 48 public void testSerializationBigObject() { 49 StringBuffer sb = new StringBuffer (239000); 50 for(int i=0; i<50000; i++) { 51 sb.append(i); 52 } 53 testSerialization(sb.toString()); 54 } 55 56 public void testSerialization(Object o) { 57 String value = null; 58 try { 59 value = Object2StringSerializer.serialize(o); 60 assertNotNull("Null string", value); 61 } catch (IOException e) { 62 logger.log(BasicLevel.ERROR, "The serialization fail", e); 63 fail("The serialization fail"); 64 } 65 try { 66 Object o2 = Object2StringSerializer.deserialize(value); 67 assertEquals("Objects not equal", o, o2); 68 } catch (IOException e) { 69 logger.log(BasicLevel.ERROR, e.getMessage(), e); 70 fail("The deserialization fail"); 71 } catch (ClassNotFoundException e) { 72 logger.log(BasicLevel.ERROR, e.getMessage(), e); 73 fail("The deserialization fail"); 74 } 75 } 76 private void testSerializationIntoFile(Object o, String fileName) { 77 String jdoFile = fileName + ".jdo"; 78 String className = Object2StringSerializer.jdoFileName2ClassName(jdoFile); 79 try { 80 Object2StringSerializer.serialize( 81 output, 82 jdoFile, 83 o, 84 logger); 85 } catch (Exception e) { 86 logger.log(BasicLevel.ERROR, e.getMessage(), e); 87 fail("The class creation fail"); 88 } 89 int i = 0; 90 try { 91 Class c = Class.forName(className); 92 i = 1; 93 Object2StringSerializer o2ss = (Object2StringSerializer) c.newInstance(); 94 i = 2; 95 Object2StringSerializer.deserialize(o2ss.getSerializedObject()); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 logger.log(BasicLevel.ERROR, e.getMessage(), e); 99 switch(i) { 100 case 0: 101 fail("The class loading fail"); 102 break; 103 case 1: 104 fail("The class instanciation fail"); 105 break; 106 default: 107 fail("The deserialization fail"); 108 break; 109 } 110 } finally { 111 String fn = Object2StringSerializer.jdoFileName2ClassName(fileName + ".jdo"); 112 className = className.replace('.', '/'); 113 File f = new File (output, fn + ".class"); 114 if (f.exists()) { 115 logger.log(BasicLevel.DEBUG, "Remove the file " + f.getAbsolutePath()); 116 f.delete(); 117 } 118 } 119 } 120 121 public void testSerializationIntoFile() { 122 testSerializationIntoFile("fdfdmfd,ng,nbdlfdfgkf,n", "testSerializationIntoFile"); 123 } 124 public void testSerializationIntoFileBigObject() { 125 StringBuffer sb = new StringBuffer (239000); 126 for(int i=0; i<50000; i++) { 127 sb.append(i); 128 } 129 testSerializationIntoFile(sb.toString(), "testSerializationIntoFileBigObject"); 130 } 131 } 132 | Popular Tags |