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

📄 messageinfoserializationtest.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 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.mm;

import com.funambol.mail.Address;
import com.funambol.mail.Folder;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.StoreFactory;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.rms.*;
import com.funambol.storage.ComplexSerializer;
import com.sun.kvem.midp.pim.AbstractPIMItem;
import com.sun.kvem.midp.pim.PIMFormat;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Date;

import jmunit.framework.cldc10.AssertionFailedException;
import jmunit.framework.cldc10.TestCase;

import com.funambol.mailclient.cm.Contact;

import com.funambol.util.Log;


public class MessageInfoSerializationTest extends TestCase {
    
    /**
     * Private Object to be serialized and deserialized
     */
    private MessageInfo fullMsgInfo;
    private MessageInfo returnedFullMsgInfo;
    /*private MessageInfo partialMsgInfo;
    private MessageInfo returnedPartialMsgInfo;
    */
    /**
     * objects used for building the messageinfo
     **/
    
    private Message fullMessage;
    //private Message partialMessage;
    
    private Address from;
    
    private Address to;
    
    private FolderInfo folderInfo;
    
    /**
     * Constants used for building the objects
     */
    
    private final String TO_NAME = "to";
    private final String TO_EMAIL = "to@email1";
    
    private final String CC_NAME = "cc";
    private final String CC_EMAIL = "cc@email";
    
    private final String FROM = "from";
    private final String FROM_EMAIL = "from@email";
    
    private final String MESSAGE_ID = "messageid";
    
    private final String MESSAGE_SUBJECT = "Subject";
    
    private final Date SENT = new Date(12345);
    private final Date RECEIVED = new Date(54321);
    
    /**
     * Private Object to be deserialized
     */
    private byte[] fullMessageInfoStream;
    private byte[] partialMessageInfoStream;
    
    
    /**
     * Creates a new instance of this testcase
     */
    public MessageInfoSerializationTest() {
        super(2,"MessageInfo 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:
                serializeFullMessageInfoTest();
                break;
           case 1:
                deserializeFullMessageInfoTest();
                break;
        
            default:
                break;
        }
    }
    
   
    
    
    /**
     * Test of method serialize for a fully initialized messageinfo
     */
    public void serializeFullMessageInfoTest() throws Exception {
        Log.info("1.Full MessageInfo Serialization Test");
        //Serialization of Integer with ComplexSerializer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baos);
        
        fullMsgInfo.serialize(dout);
        
        //Building Expected result
        ByteArrayOutputStream expectedBaos = new ByteArrayOutputStream();
        DataOutputStream expectedDout = new DataOutputStream(expectedBaos);
        
        expectedDout.writeUTF(MESSAGE_ID);
        new MessageFlags().serialize(expectedDout);
        from.serialize(expectedDout);
        to.serialize(expectedDout);
        expectedDout.writeBoolean(true);
        expectedDout.writeUTF(MESSAGE_SUBJECT);
        ComplexSerializer.writeDateField(expectedDout, SENT);
        ComplexSerializer.writeDateField(expectedDout, RECEIVED);
        folderInfo.serialize(expectedDout);
        
        //Assert true if baos is equal to expectedBaos
        fullMessageInfoStream = compareStream(baos, expectedBaos);
        Log.info("1.Full MessageInfo Serialization Test Succesfull");
    }
    
    private void createObjects() throws MailException {
        
      
        fullMessage = new Message();
        to = new Address(Address.TO, TO_NAME, TO_EMAIL);
          from = new Address(Address.FROM, FROM, FROM_EMAIL);
      
        fullMessage.addRecipient(to);
        fullMessage.setFrom(from);
       
        
        folderInfo = new FolderInfo("INBOX", StoreFactory.getStore());
        
        fullMsgInfo = new MessageInfo(fullMessage, folderInfo);
        
        //to
        fullMsgInfo.to = to;
        
        //from
        fullMsgInfo.from = from;
        
        //flags
        fullMsgInfo.flags = new MessageFlags();
        
        //msgid
        fullMsgInfo.messageId = MESSAGE_ID;
        
        //subject
        fullMsgInfo.subject = MESSAGE_SUBJECT;
        
        //sent
        fullMsgInfo.sent = SENT;
        
        //received
        fullMsgInfo.received = RECEIVED;
        
        returnedFullMsgInfo = new MessageInfo();
    }
    
    /**
     * 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 deserializeFullMessageInfoTest() throws Exception {
        Log.info("2.Deserializing Full MessageInfo Test");
        
        ByteArrayInputStream bais = new ByteArrayInputStream(fullMessageInfoStream);
        DataInputStream din = new DataInputStream(bais);
        
        returnedFullMsgInfo.deserialize(din);
        
        assertEquals(returnedFullMsgInfo.flags.getFlags(),
                fullMsgInfo.flags.getFlags());
        assertTrue(returnedFullMsgInfo.from.toString().
                equals(fullMsgInfo.from.toString()));
        assertTrue(returnedFullMsgInfo.to.toString().
                equals(fullMsgInfo.to.toString()));
        assertTrue(returnedFullMsgInfo.parent.getName().
                equals(fullMsgInfo.parent.getName()));
        assertTrue(returnedFullMsgInfo.subject.toString().
                equals(fullMsgInfo.subject.toString()));
        Log.debug(""+returnedFullMsgInfo.sent.getTime());
        Log.debug(""+fullMsgInfo.sent.getTime());
        assertTrue(returnedFullMsgInfo.sent.getTime()== 
                fullMsgInfo.sent.getTime());
        assertTrue(returnedFullMsgInfo.received.getTime()== 
                fullMsgInfo.received.getTime());
                
        Log.info("2.Deserializing Full MessageInfo Test Succesfull");
    }
    
    
}

⌨️ 快捷键说明

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