KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > basic > TestObject2StringSerializer


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  *
29  * @author S.Chassande-Barrioz
30  */

31 public class TestObject2StringSerializer extends SpeedoTestHelper {
32
33     String JavaDoc output;
34
35     public TestObject2StringSerializer(String JavaDoc 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 JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc o) {
57         String JavaDoc value = null;
58         try {
59             value = Object2StringSerializer.serialize(o);
60             assertNotNull("Null string", value);
61         } catch (IOException JavaDoc e) {
62             logger.log(BasicLevel.ERROR, "The serialization fail", e);
63             fail("The serialization fail");
64         }
65         try {
66             Object JavaDoc o2 = Object2StringSerializer.deserialize(value);
67             assertEquals("Objects not equal", o, o2);
68         } catch (IOException JavaDoc e) {
69             logger.log(BasicLevel.ERROR, e.getMessage(), e);
70             fail("The deserialization fail");
71         } catch (ClassNotFoundException JavaDoc e) {
72             logger.log(BasicLevel.ERROR, e.getMessage(), e);
73             fail("The deserialization fail");
74         }
75     }
76     private void testSerializationIntoFile(Object JavaDoc o, String JavaDoc fileName) {
77         String JavaDoc jdoFile = fileName + ".jdo";
78         String JavaDoc className = Object2StringSerializer.jdoFileName2ClassName(jdoFile);
79         try {
80             Object2StringSerializer.serialize(
81                     output,
82                     jdoFile,
83                     o,
84                     logger);
85         } catch (Exception JavaDoc e) {
86             logger.log(BasicLevel.ERROR, e.getMessage(), e);
87             fail("The class creation fail");
88         }
89         int i = 0;
90         try {
91             Class JavaDoc c = Class.forName(className);
92             i = 1;
93             Object2StringSerializer o2ss = (Object2StringSerializer) c.newInstance();
94             i = 2;
95             Object2StringSerializer.deserialize(o2ss.getSerializedObject());
96         } catch (Exception JavaDoc 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 JavaDoc fn = Object2StringSerializer.jdoFileName2ClassName(fileName + ".jdo");
112             className = className.replace('.', '/');
113             File JavaDoc f = new File JavaDoc(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 JavaDoc sb = new StringBuffer JavaDoc(239000);
126         for(int i=0; i<50000; i++) {
127             sb.append(i);
128         }
129         testSerializationIntoFile(sb.toString(), "testSerializationIntoFileBigObject");
130     }
131 }
132
Popular Tags