001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.basic;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.mapper.lib.Object2StringSerializer;
021: import org.objectweb.util.monolog.api.BasicLevel;
022:
023: import java.io.File;
024: import java.io.IOException;
025:
026: /**
027: *
028: * @author S.Chassande-Barrioz
029: */
030: public class TestObject2StringSerializer extends SpeedoTestHelper {
031:
032: String output;
033:
034: public TestObject2StringSerializer(String s) {
035: super (s);
036: output = System.getProperty("out.test", File.separatorChar
037: + "tmp");
038: logger.log(BasicLevel.DEBUG, "output=" + output);
039: }
040:
041: protected String getLoggerName() {
042: return "org.objectweb.speedo.runtime.basic.TestObject2StringSerializer";
043: }
044:
045: public void testSerialization() {
046: testSerialization("fdfdmfd,ng,nbdlfdfgkf,n");
047: }
048:
049: public void testSerializationBigObject() {
050: StringBuffer sb = new StringBuffer(239000);
051: for (int i = 0; i < 50000; i++) {
052: sb.append(i);
053: }
054: testSerialization(sb.toString());
055: }
056:
057: public void testSerialization(Object o) {
058: String value = null;
059: try {
060: value = Object2StringSerializer.serialize(o);
061: assertNotNull("Null string", value);
062: } catch (IOException e) {
063: logger.log(BasicLevel.ERROR, "The serialization fails", e);
064: fail("The serialization fails");
065: }
066: try {
067: Object o2 = Object2StringSerializer.deserialize(value);
068: assertEquals("Objects not equal", o, o2);
069: } catch (IOException e) {
070: logger.log(BasicLevel.ERROR, e.getMessage(), e);
071: fail("The deserialization fails");
072: } catch (ClassNotFoundException e) {
073: logger.log(BasicLevel.ERROR, e.getMessage(), e);
074: fail("The deserialization fails");
075: }
076: }
077:
078: private void testSerializationIntoFile(Object o, String fileName) {
079: String jdoFile = fileName + ".jdo";
080: String className = Object2StringSerializer
081: .descFileName2ClassName(jdoFile);
082: try {
083: Object2StringSerializer.serialize(output, jdoFile, o,
084: logger);
085: } catch (Exception e) {
086: logger.log(BasicLevel.ERROR, e.getMessage(), e);
087: fail("The class creation fail");
088: }
089: int i = 0;
090: try {
091: Class c = Class.forName(className);
092: i = 1;
093: Object2StringSerializer o2ss = (Object2StringSerializer) c
094: .newInstance();
095: i = 2;
096: Object2StringSerializer.deserialize(o2ss
097: .getSerializedObject());
098: } catch (Exception e) {
099: e.printStackTrace();
100: logger.log(BasicLevel.ERROR, e.getMessage(), e);
101: switch (i) {
102: case 0:
103: fail("The class loading fail");
104: break;
105: case 1:
106: fail("The class instanciation fail");
107: break;
108: default:
109: fail("The deserialization fail");
110: break;
111: }
112: } finally {
113: String fn = Object2StringSerializer
114: .descFileName2ClassName(fileName + ".jdo");
115: className = className.replace('.', '/');
116: File f = new File(output, fn + ".class");
117: if (f.exists()) {
118: logger.log(BasicLevel.DEBUG, "Remove the file "
119: + f.getAbsolutePath());
120: f.delete();
121: }
122: }
123: }
124:
125: public void testSerializationIntoFile() {
126: testSerializationIntoFile("fdfdmfd,ng,nbdlfdfgkf,n",
127: "testSerializationIntoFile");
128: }
129:
130: public void testSerializationIntoFileBigObject() {
131: StringBuffer sb = new StringBuffer(239000);
132: for (int i = 0; i < 50000; i++) {
133: sb.append(i);
134: }
135: testSerializationIntoFile(sb.toString(),
136: "testSerializationIntoFileBigObject");
137: }
138: }
|