📄 rmscontactmanagertest.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.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.mailclient.MailClientException;
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.SyncItem;
import com.funambol.util.Log;
import java.util.Enumeration;
import java.util.Vector;
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 c;
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;
/**
* Test of addContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testaddContact() throws Exception {
Log.info("1.Add Contact Test");
testAddNoMailContacts();
try {
Log.info("First");
cm.addContact(c);
Log.info("Second");
cm.addContact(c);
} catch (AlreadyExistentContactException ex) {
ex.printStackTrace();
Log.info(ex.getExistentContact().toString());
}
assertTrue(1==c.getContactId());
Log.info("1.Add Contact Test Succesfull");
}
/**
* Test of getContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testgetContact() throws AssertionFailedException, Exception {
Log.info("2.Get Contact Test");
Contact contactToCompare = cm.getContact(c.getContactId());
assertEquals(c.toString(), contactToCompare.toString());
Log.info("2.Get Contact Test Succesfull");
}
/**
* Test of searchContacts method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testsearchContacts() throws AssertionFailedException, Exception {
Log.info("3.Search Contact Test");
Enumeration expected = cm.searchContacts("mail");
int num = 0;
while (expected.hasMoreElements()) {
Contact c = (Contact) expected.nextElement();
assertTrue(c.getDefaultEmail().substring(1,5).equals("mail"));
num++;
}
assertEquals(1, num);
Log.info("3.Search Contact Test Succesfull");
}
/**
* Test of getContactList method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testgetFullAddressList() throws AssertionFailedException, Exception {
Log.info("4.Get Full Address List Test");
int num=0;
Enumeration expected = cm.getContactList();
while (expected.hasMoreElements()) {
Contact c = (Contact) expected.nextElement();
num++;
assertTrue(c.getDefaultEmail().substring(1,5).equals("mail"));
assertTrue(c.getVisibleName().equals("JDoe"));
assertTrue(c.getNickName().equals("JDoe"));
assertTrue(c.getFirstName().equals("Jhon"));
assertTrue(c.getLastName().equals("Doe"));
}
assertEquals(1, num);
Log.info("4.Get Full Address List Test Succesfull");
}
public void setUp() throws Throwable {
System.gc();
}
public void tearDown() {
System.gc();
}
/**
* Test of updateContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testupdateContact() throws AssertionFailedException, Exception {
Log.info("5.Update Contact Test");
c.setDefaultEmail("mail");
c.setFirstName("Joe the kid");
cm.updateContact(c);
int num = 0;
Enumeration expected = cm.getContactList();
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
num++;
assertTrue(sc.getDefaultEmail().equals("mail"));
assertTrue(sc.getFirstName().equals("Joe the kid"));
}
assertEquals(1, num);
Log.info("5.Update Contact Test Succesfull");
}
/**
* Test of removeContact method, of class com.funambol.mailclient.cm.rms.RMSContactManager.
*/
public void testremoveContact() throws AssertionFailedException, Exception {
Log.info("6.Remove Contact Test");
cm.removeContact(c.getContactId());
Enumeration expected = cm.getContactList();
int numElement=0;
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(0, numElement);
Log.info("6.Remove Contact Test Succesfull");
}
private void testAddContactArray() throws Exception{
Log.info("7.Add Contact Array Test");
int numElement = 0;
cm.addContact(contactList[0]);
Enumeration expected = cm.getContactList();
assertTrue(2==contactList[0].getContactId());
cm.addContact(contactList[1]);
expected = cm.getContactList();
assertTrue(3==contactList[1].getContactId());
cm.addContact(contactList[2]);
expected = cm.getContactList();
assertTrue(4==contactList[2].getContactId());
cm.addContact(contactList[3]);
expected = cm.getContactList();
assertTrue(5==contactList[3].getContactId());
Log.info("7.Add Contact Array Test Succesfull");
}
private void testGetContactArrayList() throws ContactManagerException, AssertionFailedException {
Log.info("8.Get Contact Array Address List Test");
int numElement = 0;
Enumeration expected = cm.getContactList();
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(4, numElement);
Log.info("8.Get Contact Array List Test Succesfull");
}
private void testSearchContactArrayList() throws ContactManagerException, AssertionFailedException {
Log.info("9.Search Contact Array Address List Test");
int numElement = 0;
Enumeration expected = cm.searchContacts("Katy");
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(1,numElement);
numElement = 0;
expected = cm.searchContacts("Tim");
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(2, numElement);
numElement = 0;
expected = cm.searchContacts("mail");
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(4, numElement);
Log.info("9.Search Contact Array List Test Succesfull");
}
private void testSearchBigContactArrayList() throws AssertionFailedException, ContactManagerException {
Log.info("10.Big Contact Array Address List Test");
for (int i=0; i<CONTACT_NUMBER; i++) {
cm.addContact(bigContactList[i]);
}
try {
int numElement = 0;
Enumeration expected = cm.getContactList();
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(14, numElement);
numElement = 0;
expected = cm.searchContacts("0");
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
numElement++;
}
assertEquals(2, numElement);
numElement = 0;
while (expected.hasMoreElements()) {
Contact sc = (Contact) expected.nextElement();
Contact first = cm.getContact(sc.getContactId());
assertEquals(bigContactList[0].toString(), first.toString());
sc = (Contact) expected.nextElement();
Contact second = cm.getContact(sc.getContactId());
assertEquals(contactList[2].toString(), second.toString());
}
} catch (ContactManagerException ex) {
ex.printStackTrace();
}
showResult();
Log.info("10.Big Contact Array List Test Succesfull");
}
public RMSContactManagerTest() {
super(16,"RMSContactManagerTest");
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("vCardRemoteURI"));
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 {
createTestObjects();
} catch (ContactManagerException ex) {
ex.printStackTrace();
}
}
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0:
testaddContact();
break;
case 1:
testgetContact();
break;
case 2:
testsearchContacts();
break;
case 3:
testgetFullAddressList();
break;
case 4:
testupdateContact();
break;
case 5:
testremoveContact();
break;
case 6:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -