📄 serializablecontacttest.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.mailclient.cm.rms;
import com.funambol.mailclient.cm.ContactManagerException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import jmunit.framework.cldc10.AssertionFailedException;
import jmunit.framework.cldc10.TestCase;
import com.funambol.mailclient.cm.Contact;
import com.funambol.util.Log;
public class SerializableContactTest extends TestCase {
/**
* Private Object to be serialized
*/
private Contact c;
private SerializableContact sc;
private SerializableContact scLazy;
/**
* Private Object to be deserialized
*/
private byte[] contactStream;
private byte[] lazyContactStream;
/**
* Creates a new instance of this testcase
*/
public SerializableContactTest() {
super(4,"SerializableContact Test");
}
/**
* TestCase setUp default method
*/
public void setUp() throws ContactManagerException {
createObjects();
}
/**
* TestCase tearDown default method
*/
public void tearDown() {
}
/**
* TestCase core with all tests
*/
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0:
serializeFullContactTest();
break;
case 1:
serializeLazyContactTest();
break;
case 2:
deserializeFullContactTest();
break;
case 3:
deserializeLazyContactTest();
break;
default:
break;
}
}
/**
* Test of method serialize for SerializableContact:
* com.funambol.storage.ComplexSerializer.
*/
public void serializeFullContactTest() throws Exception {
Log.info("1.Full Serializable Contact Test");
//Serialization of Integer with ComplexSerializer
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
sc.serialize(dout);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
expectedDout.writeBoolean(false);
expectedDout.writeBoolean(true);
expectedDout.writeUTF("Jhon");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("Doe");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoeDefault@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoe2@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoe3@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+39032348258");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+390382525764");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+393331345204");
//Assert true if baos is equal to expectedBaos
contactStream = compareStream(baos, expectedBaos);
Log.info("1.Full Serializable Contact Test Succesfull");
}
/**
* Test of method serialize for SerializableContact:
* com.funambol.storage.ComplexSerializer.
*/
public void serializeLazyContactTest() throws Exception {
Log.info("2.Lazy Serializable Contact Test");
//Serialization of Integer with ComplexSerializer
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
scLazy.serialize(dout);
//Building Expected result
ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
expectedDout.writeBoolean(false);
expectedDout.writeBoolean(false);
expectedDout.writeBoolean(false);
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoeDefault@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoe2@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("jhondoe3@funambol.com");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+39032348258");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+390382525764");
expectedDout.writeBoolean(true);
expectedDout.writeUTF("+393331345204");
//Assert true if baos is equal to expectedBaos
lazyContactStream = compareStream(baos, expectedBaos);
Log.info("2.Lazy Serializable Contact Test Succesfull");
}
private void createObjects() throws ContactManagerException {
sc = new SerializableContact();
scLazy = new SerializableContact();
sc.setFirstName("Jhon");
sc.setLastName("Doe");
sc.setDefaultEmail("jhondoeDefault@funambol.com");
sc.setEmail_2("jhondoe2@funambol.com");
sc.setEmail_3("jhondoe3@funambol.com");
sc.setHomePhone("+39032348258");
sc.setJobPhone("+390382525764");
sc.setMobilePhone("+393331345204");
scLazy.setDefaultEmail("jhondoeDefault@funambol.com");
scLazy.setEmail_2("jhondoe2@funambol.com");
scLazy.setEmail_3("jhondoe3@funambol.com");
scLazy.setHomePhone("+39032348258");
scLazy.setJobPhone("+390382525764");
scLazy.setMobilePhone("+393331345204");
}
/**
* Compare actual and expected Byte array byte by byte.
* @param baos is the ByteArrayOutputStream obtained from serialization
* @param expectedBaos is the expected ByteArrayOutputStream
* @return byte[] result for next test.
*/
private byte[] compareStream(ByteArrayOutputStream baos,
ByteArrayOutputStream expectedBaos)
throws AssertionFailedException {
byte[] actual = expectedBaos.toByteArray();
byte[] expected = baos.toByteArray();
for (int j=0; j<actual.length; j++) {
assertTrue(actual[j]==expected[j]);
}
//Return the byteArray to use for deserialization test
return actual;
}
/**
* Test of method deserialize for Lazy SerializableContact:
* com.funambol.storage.ComplexSerializer.
*/
private void deserializeFullContactTest() throws Exception {
Log.info("3.Deserializing Full Contact Test");
ByteArrayInputStream bais = new ByteArrayInputStream(contactStream);
DataInputStream din = new DataInputStream(bais);
sc.deserialize(din);
assertTrue(sc.getNickName()==null);
assertTrue(sc.getFirstName().equals("Jhon"));
assertTrue(sc.getLastName().equals("Doe"));
assertTrue(sc.getDefaultEmail().equals("jhondoeDefault@funambol.com"));
assertTrue(sc.getEmail_2().equals("jhondoe2@funambol.com"));
assertTrue(sc.getEmail_3().equals("jhondoe3@funambol.com"));
assertTrue(sc.getHomePhone().equals("+39032348258"));
assertTrue(sc.getJobPhone().equals("+390382525764"));
assertTrue(sc.getMobilePhone().equals("+393331345204"));
Log.info("3.Deserializing Full Contact Test Succesfull");
}
/**
* Test of method deserialize for Full SerializableContact:
* com.funambol.storage.ComplexSerializer.
*/
private void deserializeLazyContactTest() throws Exception {
Log.info("4.Deserializing Lazy Contact Test");
ByteArrayInputStream bais = new ByteArrayInputStream(lazyContactStream);
DataInputStream din = new DataInputStream(bais);
scLazy.deserialize(din);
assertTrue(scLazy.getNickName()==null);
assertTrue(scLazy.getFirstName()==null);
assertTrue(scLazy.getLastName()==null);
assertTrue(scLazy.getDefaultEmail().equals("jhondoeDefault@funambol.com"));
assertTrue(scLazy.getEmail_2().equals("jhondoe2@funambol.com"));
assertTrue(scLazy.getEmail_3().equals("jhondoe3@funambol.com"));
assertTrue(scLazy.getHomePhone().equals("+39032348258"));
assertTrue(scLazy.getJobPhone().equals("+390382525764"));
assertTrue(scLazy.getMobilePhone().equals("+393331345204"));
Log.info("4.Deserializing Lazy Contact Test Succesfull");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -