MojErr MojObjectTest::emptyTest(MojObject& obj) { MojObject obj2; MojString str1; MojString str2; bool found = false; MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); MojTestAssert(obj.begin() == obj.end()); MojTestAssert(!obj.contains(_T("hello"))); MojTestAssert(!obj.get(_T("hi"), obj2)); MojErr err = obj.del(_T("hello"), found); MojTestErrCheck(err); obj2.assign(obj); MojTestAssert(obj2.type() == obj.type()); MojTestAssert(obj2 == obj); MojTestAssert(!(obj2 != obj)); err = obj.stringValue(str1); MojTestErrCheck(err); err = obj2.stringValue(str2); MojTestErrCheck(err); MojTestAssert(str1 == str2); return MojErrNone; }
MojErr AccountFinderCommand::GetPopAccountResponse(MojObject& response, MojErr err) { CommandTraceFunction(); try { // check database result first ErrorToException(err); MojObject results; err = response.getRequired(_T("results"), results); ErrorToException(err); if (results.size() > 0) { MojObject transportObj; if (results.at(0, transportObj)) { PopAccountAdapter::GetPopAccount(m_accountObj, transportObj, *(m_account.get())); } } MojLogInfo(m_log, "Setting account to pop client"); m_client.SetAccount(m_account); Complete(); } catch (const std::exception& ex) { Failure(ex); } catch (...) { Failure(MailException("unknown exception", __FILE__, __LINE__)); } return MojErrNone; }
MojErr MojDbTextTokenizerTest::check(const MojChar* text, const MojChar* tokens) { // tokenize string MojString textStr; MojErr err = textStr.assign(text); MojTestErrCheck(err); MojSet<MojDbKey> set; MojRefCountedPtr<MojDbTextTokenizer> tokenizer(new MojDbTextTokenizer); MojAllocCheck(tokenizer.get()); err = tokenizer->init(_T("en_US")); MojTestErrCheck(err); err = tokenizer->tokenize(textStr, NULL, set); MojTestErrCheck(err); // check that tokens match MojObject obj; err = obj.fromJson(tokens); MojTestErrCheck(err); MojSize objSize = obj.size(); MojSize setSize = set.size(); MojTestAssert(objSize == setSize); for (MojObject::ConstArrayIterator i = obj.arrayBegin(); i != obj.arrayEnd(); ++i) { MojDbKey key; err = key.assign(*i); MojTestErrCheck(err); MojTestAssert(set.contains(key)); } return MojErrNone; }
MojErr MojDbDistinctTest::check(MojDb& db, const MojDbQuery& query, MojDbCursor& cursor, const MojChar* queryString, const MojChar* expectedIdsJson) { MojErr err = db.find(query, cursor); MojTestErrCheck(err); MojObjectBuilder builder; err = builder.beginArray(); MojTestErrCheck(err); err = cursor.visit(builder); MojTestErrCheck(err); err = cursor.close(); MojTestErrCheck(err); err = builder.endArray(); MojTestErrCheck(err); MojObject results = builder.object(); MojString json; err = results.toJson(json); MojTestErrCheck(err); MojObject expected; err = expected.fromJson(expectedIdsJson); MojTestErrCheck(err); // size check MojTestAssert(expected.size() == results.size()); // value check MojObject::ConstArrayIterator j = results.arrayBegin(); for (MojObject::ConstArrayIterator i = expected.arrayBegin(); i != expected.arrayEnd(); ++i, ++j) { MojObject value; err = j->getRequired(queryString, value); MojTestErrCheck(err); MojTestAssert(*i == value); } return MojErrNone; }
MojErr MojDbSearchTest::check(MojDb& db, const MojDbQuery& query, const MojChar* expectedIdsJson) { MojString str; MojDbSearchCursor cursor(str); MojErr err = db.find(query, cursor); MojTestErrCheck(err); MojObjectBuilder builder; err = builder.beginArray(); MojTestErrCheck(err); err = cursor.visit(builder); MojTestErrCheck(err); err = cursor.close(); MojTestErrCheck(err); err = builder.endArray(); MojTestErrCheck(err); MojObject results = builder.object(); MojString json; err = results.toJson(json); MojTestErrCheck(err); MojObject expected; err = expected.fromJson(expectedIdsJson); MojTestErrCheck(err); MojTestAssert(expected.size() == results.size()); MojObject::ConstArrayIterator j = results.arrayBegin(); for (MojObject::ConstArrayIterator i = expected.arrayBegin(); i != expected.arrayEnd(); ++i, ++j) { MojObject id; err = j->getRequired(MojDb::IdKey, id); MojTestErrCheck(err); MojTestAssert(*i == id); } return MojErrNone; }
void MojoDatabase::UpdateEmailParts(Signal::SlotRef slot, const MojObject& emailId, const MojObject& parts, bool autoDownload) { MojObject email; MojErr err = email.put(PopEmailAdapter::ID, emailId); ErrorToException(err); err = email.put(EmailSchema::PARTS, parts); ErrorToException(err); if ((!parts.undefined() && !parts.null() && parts.size() > 0) || !autoDownload) { err = email.put(PopEmailAdapter::DOWNLOADED, true); ErrorToException(err); } MojLogDebug(PopClient::s_log, "Updating email '%s' with parts: '%s'", AsJsonString(emailId).c_str(), AsJsonString(parts).c_str()); err = m_dbClient.merge(slot, email); ErrorToException(err); }
MojErr PopAccountUpdateCommand::GetAccountResponse(MojObject& response, MojErr err) { try { ErrorToException(err); MojObject results; err = response.getRequired(_T("results"), results); ErrorToException(err); if (results.size() > 0 && results.at(0, m_transportObj)) { GetPassword(); } } catch (const std::exception& ex) { Failure(ex); } catch (...) { Failure(MailException("unknown exception", __FILE__, __LINE__)); } return MojErrNone; }
MojErr CheckDraftsCommand::GetDraftsResponse(MojObject& response, MojErr err) { CommandTraceFunction(); try { ErrorToException(err); MojObject results; err = response.getRequired("results", results); ErrorToException(err); if(results.size() > 0) { // There's at least one draft. // Ask client to fire up a session to upload it. m_client.UploadDrafts(m_activity); Complete(); } else { // TODO: If there's no drafts folder, or we're using Gmail, we can just delete the emails right now Complete(); } } CATCH_AS_FAILURE return MojErrNone; }
MojErr SmtpSendMailCommand::GetEmailResponse(MojObject& response, MojErr err) { try { ResponseToException(response, err); MojString json; response.toJson(json); MojLogInfo(m_log, "got Email response"); MojLogDebug(m_log, "Response: %s", json.data()); // check the database result first ErrorToException(err); MojObject results; err = response.getRequired(_T("results"), results); ErrorToException(err); if(results.size() == 0) { throw MailException("email not found", __FILE__, __LINE__); } // There should only be one email for a given id assert(results.size() == 1); MojObject emailObj; bool hasObject = results.at(0, emailObj); if(!hasObject) { throw MailException("error getting email from results", __FILE__, __LINE__); } EmailAdapter::ParseDatabaseObject(emailObj, m_email); m_toAddress.clear(); if (m_email.GetTo()) { for (EmailAddressList::iterator i = m_email.GetTo()->begin(); i != m_email.GetTo()->end(); i++) m_toAddress.push_back((*i)->GetAddress()); } if (m_email.GetCc()) { for (EmailAddressList::iterator i = m_email.GetCc()->begin(); i != m_email.GetCc()->end(); i++) m_toAddress.push_back((*i)->GetAddress()); } if (m_email.GetBcc()) { for (EmailAddressList::iterator i = m_email.GetBcc()->begin(); i != m_email.GetBcc()->end(); i++) m_toAddress.push_back((*i)->GetAddress()); } m_toIdx = 0; // FIXME: Subscribe to file cache to pin cache contents CalculateEmailSize(); } catch (const std::exception& e) { HandleException(e, __func__, __FILE__, __LINE__); } catch (...) { HandleUnknownException(); } return MojErrNone; }
MojErr MojObjectTest::typeTest() { MojObject obj; MojObject obj2; MojString str1; MojString str2; MojHashMap<MojObject, MojObject> map; // null MojTestAssert(obj.type() == MojObject::TypeUndefined); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); MojErr err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("null")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); MojTestAssert(obj == obj2); MojTestAssert(obj.compare(obj2) == 0); err = obj.coerce(MojObject::TypeNull); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeNull); err = obj.coerce(MojObject::TypeString); MojTestErrCheck(err); MojTestAssert(obj == str1); err = obj.coerce(MojObject::TypeBool); MojTestErrCheck(err); MojTestAssert(obj == true); err = obj.coerce(MojObject::TypeInt); MojTestErrCheck(err); MojTestAssert(obj == 1); err = obj.coerce(MojObject::TypeDecimal); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeDecimal && obj == MojDecimal(1, 0)); err = obj.coerce(MojObject::TypeObject); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeObject); err = obj.coerce(MojObject::TypeArray); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeArray); // object err = obj.put(_T("hello"), 5); MojTestErrCheck(err); MojTestAssert(obj.type() == MojObject::TypeObject); MojTestAssert(obj.size() == 1); MojTestAssert(!obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("{\"hello\":5}")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); obj.clear(MojObject::TypeObject); MojTestAssert(obj.type() == MojObject::TypeObject); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("{}")); // array for (int i = 0; i < 1000; ++i) { err = obj.push(i); MojTestErrCheck(err); } MojTestAssert(obj.type() == MojObject::TypeArray); MojTestAssert(obj.size() == 1000); MojTestAssert(!obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); for (int i = 0; i < 1000; ++i) { MojTestAssert(obj.at(i, obj2)); MojTestAssert(obj2 == i); } MojTestAssert(!obj.at(1000, obj2)); err = obj.setAt(1001, 1001); MojTestErrCheck(err); MojTestAssert(obj.size() == 1002); MojTestAssert(obj.at(1000, obj2)); MojTestAssert(obj2.type() == MojObject::TypeUndefined); obj.clear(MojObject::TypeArray); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("[]")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // string err = str1.assign(_T("yo")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.type() == MojObject::TypeString); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str2); MojTestErrCheck(err); MojTestAssert(str1 == str2); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); obj.clear(MojObject::TypeString); MojTestAssert(obj.boolValue() == false); err = str1.assign(_T("12345")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345); MojTestAssert(obj.decimalValue() == MojDecimal(12345, 0)); err = str1.assign(_T("-67890")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == -67890); MojTestAssert(obj.decimalValue() == MojDecimal(-67890, 0)); err = str1.assign(_T("12345000000000")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345000000000LL); err = str1.assign(_T("12345.6789")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 12345); MojTestAssert(obj.decimalValue() == MojDecimal(12345, 678900)); err = str1.assign(_T("1.0e3")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 1); MojTestAssert(obj.decimalValue() == MojDecimal(1000, 0)); err = str1.assign(_T("45hello")); MojTestErrCheck(err); obj = str1; MojTestAssert(obj.intValue() == 45); MojTestAssert(obj.decimalValue() == MojDecimal(45, 0)); // bool obj = true; MojTestAssert(obj.type() == MojObject::TypeBool); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 1); MojTestAssert(obj.decimalValue() == MojDecimal(1, 0)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("true")); obj.clear(MojObject::TypeBool); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("false")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // MojDecimal obj = MojDecimal(3, 140000); MojTestAssert(obj.type() == MojObject::TypeDecimal); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == 3); MojTestAssert(obj.decimalValue() == MojDecimal(3.14)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("3.14")); obj.clear(MojObject::TypeDecimal); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("0.0")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); // MojDecimal obj = -987654321; MojTestAssert(obj.type() == MojObject::TypeInt); MojTestAssert(obj.size() == 0); MojTestAssert(obj.empty()); MojTestAssert(obj.boolValue() == true); MojTestAssert(obj.intValue() == -987654321); MojTestAssert(obj.decimalValue() == MojDecimal(-987654321, 0)); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("-987654321")); obj.clear(MojObject::TypeInt); MojTestAssert(obj.boolValue() == false); MojTestAssert(obj.intValue() == 0); MojTestAssert(obj.decimalValue() == MojDecimal()); err = obj.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("0")); err = map.put(obj, obj); MojTestErrCheck(err); MojTestAssert(map.contains(obj)); return MojErrNone; }
MojErr MojObjectTest::getTest(const MojObject& obj) { MojObject obj2; MojObject obj3; MojObject array1; MojString str1; MojSize size; size = obj.size(); MojTestAssert(size == 10); MojTestAssert(obj.get(_T("object"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeObject); MojTestAssert(obj2.get(_T("nested"), obj3)); MojErr err = obj3.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("howdy")); MojTestAssert(obj.get(_T("string"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeString); err = obj2.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("howdy")); MojTestAssert(obj.get(_T("bool1"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeBool); MojTestAssert(obj2 == true); MojTestAssert(obj2 != false); MojTestAssert(obj.get(_T("bool2"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeBool); MojTestAssert(obj2 == false); MojTestAssert(obj2 != true); MojTestAssert(obj.get(_T("MojDecimal1"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeDecimal); MojTestAssert(obj2 == MojDecimal(0, 100000)); MojTestAssert(obj2 != MojDecimal(2.34)); MojTestAssert(obj.get(_T("MojDecimal2"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeDecimal); MojTestAssert(obj2 == MojDecimal(2.34)); MojTestAssert(obj2 != MojDecimal(0.1)); MojTestAssert(obj.get(_T("int1"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeInt); MojTestAssert(obj2 == 50); MojTestAssert(obj2 != -260000000000LL); MojTestAssert(obj.get(_T("int2"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeInt); MojTestAssert(obj2 == -260000000000LL); MojTestAssert(obj2 != 50); MojTestAssert(obj.get(_T("null"), obj2)); MojTestAssert(obj2.type() == MojObject::TypeNull); // array MojTestAssert(obj.get(_T("array"), array1)); MojTestAssert(array1.type() == MojObject::TypeArray); MojTestAssert(array1.size() == 8); MojTestAssert(array1.at(0, obj2)); MojTestAssert(obj2.type() == MojObject::TypeObject); MojTestAssert(obj2.empty()); MojTestAssert(array1.at(1, obj2)); MojTestAssert(obj2.type() == MojObject::TypeArray); MojTestAssert(obj2.empty()); MojTestAssert(array1.at(2, obj2)); MojTestAssert(obj2.type() == MojObject::TypeString); err = obj2.stringValue(str1); MojTestErrCheck(err); MojTestAssert(str1 == _T("howdy")); MojTestAssert(array1.at(3, obj2)); MojTestAssert(obj2.type() == MojObject::TypeBool); MojTestAssert(obj2 == false); MojTestAssert(array1.at(4, obj2)); MojTestAssert(obj2.type() == MojObject::TypeDecimal); MojTestAssert(obj2 == MojDecimal(3.14)); MojTestAssert(array1.at(5, obj2)); MojTestAssert(obj2.type() == MojObject::TypeInt); MojTestAssert(obj2 == 100); MojTestAssert(array1.at(6, obj2)); MojTestAssert(obj2.type() == MojObject::TypeNull); MojTestAssert(array1.at(7, obj2)); MojTestAssert(obj2.type() == MojObject::TypeInt); MojTestAssert(obj2 == 4); MojTestAssert(!array1.at(8, obj2)); return MojErrNone; }
MojErr ReconcileEmailsCommand::GetDeletedEmailsResponse(MojObject& response, MojErr err) { try { MojLogDebug(m_log, "Got local deleted emails response: %s", AsJsonString(response).c_str()); // Check database response ErrorToException(err); MojObject results; err = response.getRequired("results", results); ErrorToException(err); if(results.size() > 0) { MojLogInfo(m_log, "Got %d local deleted emails", results.size()); } MojObject::ArrayIterator it; err = results.arrayBegin(it); ErrorToException(err); // Read email properties: _id, UID, flags // If you need additional properties, modify the database query for (; it != results.arrayEnd(); ++it) { MojObject& emailObj = *it; MojObject id; err = emailObj.getRequired(PopEmailAdapter::ID, id); ErrorToException(err); MojString uid; err = emailObj.getRequired(PopEmailAdapter::SERVER_UID, uid); ErrorToException(err); std::string uidStr(uid); m_localDeletedEmailUids.push_back(LocalDeletedEmailInfo(id, uidStr)); ReconcileInfoPtr infoPtr = m_reconcileUidMap[uidStr]; if (infoPtr.get()) { m_reconcileUidMap.erase(std::string(uid)); UidMap::MessageInfo info = infoPtr->GetMessageInfo(); int msgNum = info.GetMessageNumber(); m_reconcileMsgNumMap.erase(msgNum); } } bool hasMore = DatabaseAdapter::GetNextPage(response, m_localEmailsPage); if(hasMore) { // Get next batch of emails MojLogDebug(m_log, "getting another batch of local deleted emails"); GetLocalDeletedEmails(); } else { MojLogDebug(m_log, "Got old emails cache"); GetUidCache(); } } catch(const std::exception& e) { MojLogError(m_log, "Failed to sync folder: %s", e.what()); Failure(e); } catch(...) { MojLogError(m_log, "Uncaught exception %s", __PRETTY_FUNCTION__); MailException exc("Unable to get deleted emails", __FILE__, __LINE__); Failure(exc); } return MojErrNone; }