⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializablecontacttest.java

📁 moblie syncml mail javame
💻 JAVA
字号:
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * 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 Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

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 + -