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

📄 messagemanagertest.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.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.MailClientException;
import com.funambol.mailclient.syncml.FlagQueue;
import com.funambol.syncml.spds.SyncConfig;
import com.funambol.util.Log;
import java.util.Hashtable;
 
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(10, "MessageManager Test");
         super(4, "MessageManager Test"); 

        MailClientConfig conf = null;
        Log.setLogLevel(Log.DEBUG);
        
        Log.debug("Init test config");
        
        try {
            conf = ConfigManager.getConfig(this);
            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
<<<<<<< MessageManagerTest.java
             case 0: 
                testInit();                 
=======
            case 0: 
                testInit();     
>>>>>>> 1.15
                break;
            // List the newly created folders
            case 1: 
                testListFolders();          
                break;
            // Perform the initial slow sync
<<<<<<< MessageManagerTest.java
            case 2:  
                testSync();                 
                break;
=======
            case 2: 
                MessageInfo[] mi = Cache.getMsgSnapShot(Cache.INBOX);
                testSync();     
                mi = new MessageInfo[2];
                Store s = StoreFactory.getStore();//.getFolder(Store.INBOX);
                FolderInfo folder = new FolderInfo(Store.INBOX, s);
                
                mi[0]=new MessageInfo(refmsg, folder);
                mi[1]=new MessageInfo(refmsg, folder);
                Cache.saveMsgSnapShot(Cache.INBOX, mi);
                testSync();               
                Cache.getMsgSnapShot(Cache.INBOX);
                break;  
>>>>>>> 1.15
            // Show the messages retrieved
<<<<<<< MessageManagerTest.java
           case 3: 
                testListInbox();            
=======
            case 3: 
                testListInbox();
>>>>>>> 1.15
                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 (MailClientException ex) {
            fail();
        } 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);
        
        MessageInfo[] list = inbox.getMsgList();
        
        for(int i=0, l=list.length; i<l; i++) {
            Log.debug(list[i].toString());
        }
    }
    
    /**
     * Test sending a message.
     * Successful if the message is moved in sent.
     */
    public void testSend(Message msg) throws Exception {
        Log.debug("------------------------- testSend ------------------------");
        mm.send(msg);
        
        // Check result
        Log.trace("check result");
        FolderInfo sent = mm.getFolderInfo(Store.SENT);
        
        Log.trace("Get Messages Info");
        MessageInfo[] msglist = sent.getMsgList();
        String msgid = msg.getMessageId();
        Log.trace("Compare messages");
        for(int i=0, l=msglist.length; i<l; i++) {
            Log.debug(msglist[i].toString());
            
            if(msgid.equals(msglist[i].messageId)) {
                assertEquals(msg.getSubject(), msglist[i].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);
        MessageInfo[] msglist = inbox.getMsgList();
        
        Log.info("Sent message: "+ref.getSubject());
        
        for(int i=0, l=msglist.length; i<l; i++) {
            Log.debug(msglist[i].toString());
            if(ref.getSubject().equals(msglist[i].subject) ) {
                Log.debug("Message "+msglist[i].subject+" found");
                // Save the received message as reference
                // for the next tests.
                this.refmsg = msglist[i].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(fullmsg);
        Log.debug("Message is: " + mf.toString());
    }
    
    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 + -