📄 complexserializertest.java
字号:
/*
* Copyright (C) 2006-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.funambol.storage;
import com.funambol.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import jmunit.framework.cldc10.AssertionFailedException;
import jmunit.framework.cldc10.TestCase;
public class ComplexSerializerTest extends TestCase {
/** Private constants suitable for testing*/
private static final int NULL = 0;
private static final int INTEGER = 1;
private static final int STRING = 2;
private static final int SERIALIZED = 3;
private static final int BYTE_ARRAY = 4;
private static final int HASHTABLE = 5;
private static final int VECTOR = 6;
/**
* Private Object to be serialized
*/
private Integer i;
private String str;
private byte[] ba;
private Hashtable ht;
private Vector v;
private Hashtable htVectorHashtable;
private Vector vht;
private Hashtable serializableHt;
/**
* Private Object to be deserialized
*/
private byte[] iStream;
private byte[] strStream;
private byte[] baStream;
private byte[] htStream;
private byte[] vStream;
private byte[] htvhtStream;
private byte[] vhtStream;
private byte[] serializableHtStream;
/**
* Creates a new instance of this testcase
*/
public ComplexSerializerTest() {
super(16,"ComplexSerializerTest");
}
/**
* TestCase setUp default method
*/
public void setUp() {
createObjects();
}
/**
* TestCase tearDown default method
*/
public void tearDown() {
}
/**
* TestCase core with all tests
*/
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0:
serializeIntegerTest();
break;
case 1:
serializeStringTest();
break;
case 2:
serializeByteArrayTest();
break;
case 3:
testSerializeHashTable();
break;
case 4:
testSerializeVector();
break;
case 5:
testSerializeHashtableOfVector();
break;
case 6:
testSerializeVectorOfVector();
break;
case 7:
testDeserializeInteger();
break;
case 8:
testDeserializeString();
break;
case 9:
testDeserializeByteArray();
break;
case 10:
testDeserializeHashTable();
break;
case 11:
testDeserializeVector();
break;
case 12:
testDeserializeHashtableOfVector();
break;
case 13:
testDeserializeVectorOfVector();
break;
case 14:
testSerializeSerializableHashtable();
break;
case 15:
deserializeSerializableHashtable();
break;
default:
break;
}
}
/**
* Test of method serializeObject for Integer:
* com.funambol.storage.ComplexSerializer.
*/
public void serializeIntegerTest() throws Exception {
Log.info("1.Serializing Integer");
//Serialization of Integer with ComplexSerializer
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeObject(dout, i);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
expectedDout.writeByte(INTEGER);
expectedDout.writeInt(((Integer)i).intValue());
//Assert true if baos is equal to expectedBaos
iStream = compareStream(baos, expectedBaos);
Log.info("1.Integer Test Succesfull");
}
/**
* Test of method serializeObject for String:
* com.funambol.storage.ComplexSerializer.
*/
private void serializeStringTest() throws Exception {
//Serialization of String with ComplexSerializer
Log.info("2.Serializing String");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeObject(dout,str);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
expectedDout.writeByte(STRING);
expectedDout.writeUTF((String)str);
//Assert true if baos is equal to expectedBaos
strStream = compareStream(baos, expectedBaos);
Log.info("2.String Test Succesfull");
}
/**
* Test of method serializeObject for Byte[]:
* com.funambol.storage.ComplexSerializer.
*/
private void serializeByteArrayTest() throws Exception{
//Serialization of byte[] with ComplexSerializer
Log.info("3.Serializing Byte Array");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeObject(dout,ba);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
expectedDout.writeByte(BYTE_ARRAY);
expectedDout.writeInt(ba.length);
expectedDout.write(ba, 0, ba.length);
//Assert true if baos is equal to expectedBaos
baStream = compareStream(baos, expectedBaos);
Log.info("3.Byte Array Test Succesfull");
}
/**
* Test of method serializeObject for Hashtable:
* com.funambol.storage.ComplexSerializer.
*/
public void testSerializeHashTable() throws Exception {
//Serialization of String with ComplexSerializer
Log.info("4.Serializing HashTable");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeHashTable(dout,ht);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
//Building Expected result
//expectedDout.writeByte(HASHTABLE);
int n = ht.size();
expectedDout.writeInt(n);
//Writes all the elements of the Hashtable to expectedDout
for(Enumeration e = ht.keys(); e.hasMoreElements(); ){
String key = new String((String)e.nextElement());
Integer val = (Integer) ht.get(key);
//Write ht Key
expectedDout.writeByte(STRING);
expectedDout.writeUTF((String)key);
//Write ht Value
expectedDout.writeByte(INTEGER);
expectedDout.writeInt(((Integer)val).intValue());
}
//Assert true if baos is equal to expectedBaos
htStream = compareStream(baos, expectedBaos);
Log.info("4.HashTable Test Succesfull");
}
/**
* Test of method serializeObject for Vector:
* com.funambol.storage.ComplexSerializer.
*/
public void testSerializeVector() throws Exception {
Log.info("5.Serializing Vector");
//Serialization of Integer with ComplexSerializer
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeVector(dout, v);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
//expectedDout.writeByte(VECTOR);
int n = v.size();
expectedDout.writeInt(n);
expectedDout.write(iStream);
expectedDout.write(strStream);
expectedDout.write(baStream);
expectedDout.writeByte(HASHTABLE);
expectedDout.write(htStream);
//Assert true if baos is equal to expectedBaos
vStream = compareStream(baos, expectedBaos);
Log.info("5.Vector Test Succesfull");
}
/**
* Test of method serializeObject for Hashtable of Vector:
* com.funambol.storage.ComplexSerializer.
*/
private void testSerializeHashtableOfVector() throws Exception {
//Serialization of String with ComplexSerializer
Log.info("6.Serializing Hashtable with Vector and Hashtable");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
ComplexSerializer.serializeHashTable(dout,htVectorHashtable);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
//Building Expected result
int n = htVectorHashtable.size();
expectedDout.writeInt(n);
//Writes all the elements of the Hashtable to expectedDout
expectedDout.writeByte(VECTOR);
expectedDout.write(vStream);
expectedDout.writeByte(HASHTABLE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -