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

📄 messagemanagertest.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.mm;

import com.funambol.mail.Address;
import com.funambol.mail.Folder;
import com.funambol.mail.MIMEFormatter;
import com.funambol.mail.MailException;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.mailclient.Cache;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.config.MailClientConfig;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.syncml.FlagQueue;
import com.funambol.syncml.spds.SyncConfig;
import com.funambol.util.Log;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

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

/**
 * Test case for the MessageManager class.
 * The tests initialize the message store, perform the initial sync to
 * get what is on the server, send a test message to self, get the response
 * and remove the sent items.
 */
public class MessageManagerTest extends TestCase {
    
    private static final String ADDRESS = "jdoe@funabol.com";
    private static final String NAME = "John Doe";
    
    private static final String BODY =
            "Hi Joe, this is a message from you.\n\nJoe";
    
    Message refmsg = null;
    MessageManager mm = null;
    
    private static final int FULL_MESSAGE_LIST = 20;
    
    public MessageManagerTest() {
        super(4, "MessageManager Test");
        
        MailClientConfig conf = null;
        Log.setLogLevel(Log.DEBUG);
        
        Log.debug("Init test config");
        
        try {
            conf = ConfigManager.getConfig();
            Account a = new Account();
            a.setUrl(this.getAppProperty("Url"));
            a.setUser(this.getAppProperty("User"));
            a.setPassword(this.getAppProperty("Password"));
            a.setName(this.getAppProperty("Name"));
            a.setAddress(this.getAppProperty("Address"));
            a.setRemoteURI(this.getAppProperty("RemoteUri"));
            Log.info("Account to be synchronized: \n" + a.toString());
            conf.setMailAccount(a);
            
        } catch (Exception e) {
            // Set defaults
            conf = new MailClientConfig();
        }
        
        try {
            // Save them
            ConfigManager.saveConfig(conf);
        } catch(Exception ee){
            ee.printStackTrace();
            Log.error("Can't init config. Giving up!");
        }
        
        try {
            mm = MessageManager.getInstance();
            refmsg = new Message();
            refmsg.setSubject("Test message");
            Account ac = conf.getMailAccount();
            refmsg.setFrom(
                    new Address(Address.FROM, ac.getName(), ac.getAddress()) );
            Address[] tolist = new Address[1];
            tolist[0] = new Address(Address.TO, ac.getName(), ac.getAddress() );
            refmsg.setTo(tolist);
            refmsg.setContent(BODY);
        } catch(Exception e) {
            Log.error("Can't init MessageManager");
        }
        
    }
    
    public void tearDown() {
    }
    
    public void test(int testNumber) throws Throwable {
        
        switch(testNumber) {
            
            
            // Reset the message store
            case 0:
                testInit();
                break;
                // List the newly created folders
            case 1:
                testListFolders();
                break;
                // Perform the initial slow sync
            case 2:
                Vector mi = Cache.getMsgSnapShot(Cache.INBOX);
                testSync();
                mi = new Vector(2);
                Store s = StoreFactory.getStore();//.getFolder(Store.INBOX);
                FolderInfo folder = new FolderInfo(Store.INBOX, s);
                
                mi.addElement(new MessageInfo(refmsg, folder));
                mi.addElement(new MessageInfo(refmsg, folder));
                Cache.saveMsgSnapShot(Cache.INBOX, mi);
                testSync();
                Cache.getMsgSnapShot(Cache.INBOX);
                break;
            case 3:
                testListInbox();
                break;
                // Send a message to self
/*           case 4:
                testSend(refmsg);
                break;
            // Receive the message and compare it
            case 5:
                Log.info("Waiting one minute before checking the inbox.");
                Thread.sleep(60000);
                Log.info("Check the inbox.");
                testReceive(refmsg);
                break;
            // Get the full message
             case 6:
                testGetFullMessage(refmsg.getMessageId());
                break;
            case 7:
                testUpdateFlags();
                break;
            case 8:
            // delete a message and propagate deletion to the server
            // repeat sync process and propagate deletion
            // Message is no more in inbox
                testDelete();
                break;
            case 9:
                //Complete test case for MessageMenager
                testUpdateDeleteFlags();
 */           default:
     break;
        }
        
    }
    
    /**
     * Test the message store re-initialization.
     * Successful if no exceptions are thrown.
     */
    public void testInit() throws Exception {
        mm.initStore(true);
    }
    
    /**
     * Test the getRootFolders() method
     */
    public void testListFolders() throws Exception {
        Log.debug("------------------------- testListFolders -----------------");
        FolderInfo[] list = mm.getRootFolders();
        
        for(int i=0, l=list.length; i<l; i++) {
            Log.debug("Folder:"+list[i].getName());
        }
        assertTrue(list[mm.INBOX].getName().equals(Store.INBOX));
        assertTrue(list[mm.OUTBOX].getName().equals(Store.OUTBOX));
        assertTrue(list[mm.DRAFTS].getName().equals(Store.DRAFTS));
        assertTrue(list[mm.SENT].getName().equals(Store.SENT));
    }
    
    /**
     * Test synchronizing mailbox.
     * Successful if no exceptions are thrown.
     */
    public void testSync() throws AssertionFailedException {
        Log.debug("------------------------- testSync ---------------------");
        try {
            // Sync mailbox
            mm.sync();
        } catch (ConfigException ex) {
            fail();
        }
    }
    
    /**
     * List the messages in a folder.
     * Just to show messages came from the server (useful until the
     * client UI is available)
     */
    public void testListInbox() throws Exception {
        Log.debug("------------------------- testListInbox -------------------");
        FolderInfo inbox = mm.getFolderInfo(Store.INBOX);
        
        Vector list = inbox.getMsgList();
        
        Enumeration e = list.elements();
        
        while (e.hasMoreElements()) {
            
            Log.debug( ((MessageInfo)e.nextElement()).toString() );
            
        }
        
    }
    
    /**
     * Test sending a message.
     * Successful if the message is moved in sent.
     */
    public void testSend(Message msg) throws Exception {
        Log.debug("------------------------- testSend ------------------------");
        mm.getFolderInfo(Store.OUTBOX).getFolder().appendMessage(msg);
        mm.send();
        
        // Check result
        Log.trace("check result");
        FolderInfo sent = mm.getFolderInfo(Store.SENT);
        
        Log.trace("Get Messages Info");
        Vector msglist = sent.getMsgList();
        String msgid = msg.getMessageId();
        Log.trace("Compare messages");
        
        
        Enumeration e = msglist.elements();
        
        while (e.hasMoreElements()) {
            
            MessageInfo m = (MessageInfo) e.nextElement();
            Log.debug(m.toString());
            
            if ( msgid.equals(m.messageId) )  {
                assertEquals(msg.getSubject(), m.subject);
                return;
            }
        }
        
        // Test failed.
        assertTrue(false);
    }
    
    /**
     * Test Receiving a message.
     * Successful if the sent message is get back in inbox (it was to self)
     */
    public void testReceive(Message ref) throws Exception {
        Log.debug("------------------------- testReceive ---------------------");
        String msgid = "";
        
        // Receive messages
        mm.receive();
        
        // Check result
        FolderInfo inbox = mm.getFolderInfo(Store.INBOX);
        Vector msglist = inbox.getMsgList();
        
        Log.info("Sent message: "+ref.getSubject());
        
        Enumeration e = msglist.elements();
        
        
        while (e.hasMoreElements()) {
            MessageInfo m = (MessageInfo)e.nextElement();
            Log.debug(m.toString());
            
            if(ref.getSubject().equals(m.subject) ) {
                Log.debug("Message "+m.subject+" found");
                // Save the received message as reference
                // for the next tests.
                this.refmsg = m.getMessage();
                return;
            }
        }
        
        // If we come here, the test failed.
        Log.error("Message '"+ref.getSubject()+"' not found.");
        assertTrue(false);
    }
    
    /**
     * Get the full message.
     * The sync is started to retrieve a single message completely.
     */
    public void testGetFullMessage(String msgId) throws Exception {
        Log.debug("-------------------- testGetFullMessage ------------------");
        
        Message fullmsg = mm.getFullMessage(Store.INBOX, msgId);
        
        assertTrue(fullmsg != null);
        
        MIMEFormatter mf = new MIMEFormatter();
        mf.format(fullmsg);
        Log.debug("Message is: " + mf.format(fullmsg));
    }
    
    public void testUpdateFlags() throws Exception {
        Log.debug("------------------------- testUpdateFlags ---------------------");
        Log.debug("Updating inbox messages: mark all messages as read");
        
        Store store = StoreFactory.getStore();
        
        Folder inbox = store.getFolder(Store.INBOX);
        Message[] msgArray = inbox.getMsgHeaders();
        for (int i=0; i<=inbox.getMessageCount()-1; i++) {
            Log.debug("Message index 

⌨️ 快捷键说明

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