📄 rmscontactmanagertest.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.Cache;
import com.funambol.mailclient.syncml.PimItem;
import com.funambol.mailclient.cm.AlreadyExistentContactException;
import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManager;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.ContactManagerFactory;
import com.funambol.mailclient.cm.rms.*;
import com.funambol.mailclient.syncml.PimItem;
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.storage.ObjectStore;
import com.funambol.storage.Serialized;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SyncException;
import com.funambol.syncml.spds.SyncInfo;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.util.Log;
import java.util.Date;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;
import javax.microedition.pim.PIMItem;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import jmunit.framework.cldc10.*;
public class RMSContactManagerTest extends TestCase {
private ContactManager cm;
private Contact cont;
private Contact[] contactList;
private Contact[] bigContactList;
private Contact noMailContact1;
private Contact noMailContact2;
private Vector vCard;
private MailClientConfig conf;
private static final int CONTACT_NUMBER = 101;
public void test(int testNumber) throws Throwable {
Log.info("Running Test " + testNumber);
switch(testNumber) {
case 0:
testResetContactList();
break;
case 1:
testAddSyncItem();
break;
case 2:
testGetSyncItems();
break;
case 3:
testGetSyncItemInfo();
break;
case 4:
testRemoveSyncItem();
break;
case 5:
testAddContact();
break;
case 6:
testAddContactWithBoolean();
break;
case 7:
testGetContact();
break;
case 8:
testGetFullAddressList();
break;
case 9:
testUpdateContact();
break;
case 10:
testRemoveContact();
break;
default:
break;
}
}
/**
* add a sync item and check if it has been stored correctly
*/
public void testAddSyncItem() throws Exception {
Log.info("============= Add Sync Item Info Test ============");
cm.resetContactList();
String s = new Date().toString();
char c = 'k';
cm.addSyncItem(s , c);
Enumeration e = cm.getSyncItems(c);
assertTrue(e.hasMoreElements());
Log.info("=========================================== [ OK ]");
}
/**
* test the getsyncitems method by adding some syncitems,
*/
public void testGetSyncItems() throws Exception {
Log.info("============= Get Sync Items Test ============");
cm.resetContactList();
String[] s = {"aaa", "bbb", "ccc", "ddd"};
char[] c = {'a', 'b', 'c', 'd'};
for (int i=0; i<4; i++) {
cm.addSyncItem(s[i] , c[i]);
}
for (int i=0; i<4; i++) {
Enumeration e = cm.getSyncItems(c[i]);
assertTrue(e.hasMoreElements());
assertEquals( ((PimItem)e.nextElement()).getKey(), s[i] );
assertTrue(!e.hasMoreElements());
}
Log.info("=========================================== [ OK ]");
}
/**
* test get
*/
public void testGetSyncItemInfo() throws Exception {
Log.info("============= Get Sync Item Info Test ============");
cm.resetContactList();
String[] s = {"aaa", "bbb", "ccc", "ddd"};
char[] c = {'a', 'b', 'c', 'd'};
for (int i=0; i<4; i++) {
cm.addSyncItem(s[i] , c[i]);
}
for (int i=0; i<4; i++) {
PimItem t = cm.getSyncItemInfo(s[i]);
assertEquals( t.getState(),c[i] );
}
Log.info("=========================================== [ OK ]");
}
public void testRemoveSyncItem() throws Exception {
Log.info("============= Remove Sync Item Test ============");
cm.resetContactList();
String[] s = {"aaa", "bbb", "ccc", "ddd"};
char[] c = {'a', 'b', 'c', 'd'};
for (int i=0; i<4; i++) {
cm.addSyncItem(s[i] , c[i]);
}
for (int i=0; i<4; i++) {
cm.removeSyncItem(s[i]);
}
for (int i=0; i<4; i++) {
PimItem t = cm.getSyncItemInfo(s[i]);
assertTrue( t == null);
}
Log.info("=========================================== [ OK ]");
}
/**
* test reset contact list
*
*/
public void testResetContactList() throws Exception {
Log.info("============= Reset Contact List Test ============");
cm.resetContactList();
assertTrue(cm.getContactCount() == 0);
assertTrue(!cm.getSyncItems(SyncItem.STATE_DELETED).hasMoreElements());
assertTrue(!cm.getSyncItems(SyncItem.STATE_NEW).hasMoreElements());
assertTrue(!cm.getSyncItems(SyncItem.STATE_UPDATED).hasMoreElements());
Log.info("\tcontact list is now empty");
Log.info("=========================================== [ OK ]");
}
/**
* Test of addContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testAddContact() throws Exception {
Log.info("============= Add Contact Test ============");
cm.resetContactList();
Log.info("\tresetting contact list");
Contact c = createUniqueContact();
Log.info("\tcontact " + c.getVisibleName() + " created");
// is the contact in memory?
assertTrue(0==c.getContactId());
//store the contact
cm.addContact(c);
//check if something has been stored
assertTrue(0!=c.getContactId());
assertTrue(cm.getContactCount()==1);
Log.info("\tcontact stored");
// ok we should not use cm.getcontacts here, because we're testing
// add contact, but we need this to verify the correct add
Enumeration e = cm.getContactList();
//we have a contact in our contact list
assertTrue(e.hasMoreElements());
//getting the contact
Contact stored = (Contact) e.nextElement();
Log.info("\tstored: " + stored.toString());
Log.info("\tmemory: " + c.toString());
// check if the contact is exactly as it should be
assertTrue(c.toString().equals(stored.toString()));
// we need to be sure not to have added more than one contact
assertTrue(!e.hasMoreElements());
Log.info("=========================================== [ OK ]");
}
public void testAddContactWithBoolean() throws Exception {
Log.info("============= Add Contact With Boolean Test ============");
cm.resetContactList();
Log.info("\tresetting contact list");
Contact c = createUniqueContact();
Log.info("\tcontact " + c.getVisibleName() + " created");
// is the contact in memory?
assertTrue(0==c.getContactId());
//store the contact logging the addition
cm.addContact(c, true);
//check that the log has been added
Enumeration e = cm.getSyncItems(SyncItem.STATE_NEW);
//we have a contact in our contact list
assertTrue(e.hasMoreElements());
//getting the contact
PimItem stored = (PimItem) e.nextElement();
// check if the contact is exactly as it should be
assertTrue(c.getContactId() == Integer.valueOf(stored.getKey()).intValue());
// we need to be sure not to have added more than one contact
assertTrue(!e.hasMoreElements());
Log.info("=========================================== [ OK ]");
}
/**
* Test of getContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testGetContact() throws AssertionFailedException, Exception {
Log.info("============= Get Contact Test ============");
Contact c = createUniqueContact();
int id = cm.addContact(c);
// get the contact by id
Contact stored = cm.getContact(id);
// check if the contact has been retrieved correctly
assertEquals(c.toString(), stored.toString());
Log.info("\tGet Contact Test Succesfull");
Log.info("=========================================== [ OK ]");
}
/**
* Test of searchContacts method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
/**
* Test of getContactList method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testGetFullAddressList() throws AssertionFailedException, Exception {
Log.info("============= Get Full Address List ============");
cm.resetContactList();
int cNum = 10;
// store the contacts in the rms and memory
Vector contactList = new Vector(cNum);
for(int i=0; i<cNum; i++) {
Contact c = createUniqueContact();
contactList.addElement(c);
cm.addContact(c);
}
// get the contacts from rms
Enumeration e = cm.getContactList();
assertTrue(cm.getContactCount() == cNum);
int i=0;
//retrieve the contacts from rms and delete them one by one from memory
while (e.hasMoreElements()) {
Contact c = (Contact) e.nextElement();
Log.info("\tRemoving contact \n"+ c.toString());
assertTrue(contactList.removeElement(c));
i++;
Log.info("\tRemoved contact n
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -