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

📄 addressserializationtest.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.mail.Address;
import com.funambol.mail.MailException;

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.util.Log;


public class AddressSerializationTest extends TestCase {
    
    
    /**
     * private objects to be serialized
     */
    private Address fullAddress;
    private Address partialAddress;
    
    /**
     * private objects returned after deserialization
     */
    private Address fullAddressReturned;
    private Address partialAddressReturned;
    
    
    
    /**
     * private constants used to build and test the objects
     */
    
    private final String NAME = "name";
    private final String EMAIL = "the@address";
    private final int TYPE = Address.TO;
    
    
    
    /**
     * Private streams to be deserialized
     */
    private byte[] fullAddressStream;
    private byte[] partialAddressStream;
    
    
    
    /**
     * Creates a new instance of this testcase
     */
    public AddressSerializationTest() {
        super(4,"Adrress Serialization Test");
    }
    
    /**
     * TestCase setUp default method
     */
    public void setUp() throws MailException {
        createObjects();
    }
    
    /**
     * TestCase tearDown default method
     */
    public void tearDown() {
    
    }
    
    /**
     * TestCase core with all tests
     */
    public void test(int testNumber) throws Throwable {
        switch(testNumber) {
            case 0:
                serializeFullAddressTest();
                break;
            case 1:
                serializePartialAddressTest();
                break;
            case 2:
                deserializeFullAddressTest();
                break;
            case 3:
                deserializePartialAddressTest();
                break;
            default:
                break;
        }
    }
    
    /**
     * Test of method serialize for a fully initialized Address
     */
    public void serializeFullAddressTest() throws Exception {
        Log.info("1.Full Address Serialization Test");
        //Serialization of Integer with ComplexSerializer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baos);
        
        fullAddress.serialize(dout);
        
        //Building Expected result
        ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
        DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
        
        expectedDout.writeBoolean(true);
        expectedDout.writeUTF(NAME);
        expectedDout.writeBoolean(true);
        expectedDout.writeUTF(EMAIL);
        expectedDout.writeInt(TYPE);
        
        //Assert true if baos is equal to expectedBaos
        fullAddressStream = compareStream(baos, expectedBaos);
        Log.info("1.Full Address Serialization Test Succesfull");
    }
    
    /**
     * Test of method serialize for a partially initialized Address
     */
    public void serializePartialAddressTest() throws Exception {
        Log.info("2.Partial Address Serialization Test");
        //Serialization of Integer with ComplexSerializer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baos);
        
        partialAddress.serialize(dout);
        
        //Building Expected result
        ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
        DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
        expectedDout.writeBoolean(false);
        expectedDout.writeBoolean(true);
        expectedDout.writeUTF(EMAIL);
        expectedDout.writeInt(TYPE);
        
        //Assert true if baos is equal to expectedBaos
        partialAddressStream = compareStream(baos, expectedBaos);
        Log.info("2.Partial Address Serialization Test Succesfull");
        
    }
    
    private void createObjects() throws MailException {
        fullAddress = new Address(TYPE, NAME, EMAIL);
        fullAddressReturned = new Address();
        partialAddress = new Address(TYPE,EMAIL);
        partialAddressReturned = new Address();
    }
    
    /**
     * 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 fully initialized address :
     */
    
    private void deserializeFullAddressTest() throws Exception {
        Log.info("3.Deserializing Full Address Test");
        
        ByteArrayInputStream bais = new ByteArrayInputStream(fullAddressStream);
        DataInputStream din = new DataInputStream(bais);
        fullAddressReturned.deserialize(din);
        
        assertTrue(fullAddressReturned.getName().equals(NAME));
        assertTrue(fullAddressReturned.getEmail().equals(EMAIL));
        assertTrue(fullAddressReturned.getType() == TYPE);
        
        Log.info("3.Deserializing Full Address Test Succesfull");
    }
    
    
    private void deserializePartialAddressTest() throws Exception {
        Log.info("3.Deserializing Partial Address Test");
        
        ByteArrayInputStream bais = new ByteArrayInputStream(partialAddressStream);
        DataInputStream din = new DataInputStream(bais);
        
        partialAddressReturned.deserialize(din);
        
        assertTrue(partialAddressReturned.getName() == null);
        assertTrue(partialAddressReturned.getEmail().equals(EMAIL));
        assertTrue(partialAddressReturned.getType() == TYPE);
        
        Log.info("3.Deserializing Partial Address Test Succesfull");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -