phonebookengines/contactsmodel/tsrc/cntmodel2/T_PlTransactionManager.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "T_PlTransactionManager.h"
       
    17 _LIT(KTestName, "T_PlTransactionManager"); // Used by Testhelper - gives an warning under ARMv5
       
    18 #include "../../tsrc/testhelpers.h"
       
    19 #include <cntdef.h>
       
    20 
       
    21 
       
    22 CPlTransMgrTest* CPlTransMgrTest::NewLC()
       
    23 	{
       
    24 	CPlTransMgrTest* self = new (ELeave) CPlTransMgrTest();
       
    25 	CleanupStack::PushL(self);
       
    26 
       
    27 	self->ConstructL();
       
    28 	return self;	
       
    29 	}
       
    30 
       
    31 
       
    32 
       
    33 void CPlTransMgrTest::ConstructL()
       
    34 	{
       
    35 	User::LeaveIfError(iFs.Connect());
       
    36 	iPersistLayer = CPersistenceLayer::NewLC(iFs);
       
    37 	CleanupStack::Pop(iPersistLayer); 
       
    38 
       
    39 	MLplContactsFile& cntFile = iPersistLayer->ContactsFileL();
       
    40 	// Empty loops required to remove winscw warning
       
    41 	cntFile.CreateL(nsPlTransMgr::KFilename, MLplContactsFile::EPlOverwrite);
       
    42 	while(cntFile.OpenStepL   (nsPlTransMgr::KFilename)){};
       
    43 
       
    44 	iCntItemBldr = CCntItemBuilder::NewLC(iPersistLayer->ContactProperties().SystemTemplateL());
       
    45 	CleanupStack::Pop(iCntItemBldr);
       
    46 
       
    47 	iMatchAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,
       
    48 										   CContactItemViewDef::EIncludeHiddenFields);
       
    49 	iMatchAll->AddL(KUidContactFieldMatchAll);
       
    50 	CleanupStack::Pop(iMatchAll);
       
    51 	}
       
    52 
       
    53 
       
    54 
       
    55 CPlTransMgrTest::~CPlTransMgrTest()
       
    56 	{
       
    57 	delete iMatchAll;
       
    58 	delete iCntItemBldr;
       
    59 	delete iPersistLayer;
       
    60 	iFs.Close();
       
    61 	}
       
    62 
       
    63 
       
    64 
       
    65 void CPlTransMgrTest::RunTestsL()
       
    66 	{
       
    67 	UpdateAndCommitTestL();
       
    68 	CreateAndCommitTestL(); 
       
    69 	DeleteAndCommitTestL();
       
    70 	RollbackAndRecoverTestL();
       
    71 	OutOfMemoryUpdateTestL();
       
    72 	OutOfMemoryDeleteTestL();
       
    73 	OutOfMemoryAddTestL();
       
    74 	}
       
    75 
       
    76 // ---------- Tests ---------
       
    77 
       
    78 // Create/commit & Rollback/recover tests.
       
    79 
       
    80 void CPlTransMgrTest::CreateAndCommitTestL()
       
    81 	{
       
    82 	test.Next(_L("-> Create Contact and Commit Test"));
       
    83 
       
    84 	TInt cntID = AddAndCommitContactL();
       
    85 	
       
    86 	if (IsContactInDbL(cntID))
       
    87 		{
       
    88 		test.Next(_L("-> TEST SUCCESS - Contact FOUND after commit test"));
       
    89 
       
    90 		return;
       
    91 		}
       
    92 	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after commit test"));
       
    93 
       
    94 	}
       
    95 
       
    96 void CPlTransMgrTest::RollbackAndRecoverTestL()
       
    97 	{
       
    98 	test.Next(_L("-> Rollback & Recover Test"));
       
    99 
       
   100 	iPersistLayer->TransactionManager().StartTransactionL();
       
   101 
       
   102 	TInt cntID = AddContactL();
       
   103 	
       
   104 	iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
       
   105   	// Close the database to allow recovery
       
   106   	iPersistLayer->ContactsFileL().Close();
       
   107   		
       
   108 	iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
       
   109 	
       
   110 	if (!IsContactInDbL(cntID))
       
   111 		{
       
   112 		test.Next(_L("-> TEST SUCCESS - Contact was NOT Found after Rollback test"));
       
   113 
       
   114 		return;
       
   115 		}
       
   116 	test.Next(_L("-> TEST FAILED - Contact WAS Found after ROLLBACK test"));
       
   117 
       
   118 	}
       
   119 
       
   120 
       
   121 void CPlTransMgrTest::UpdateAndCommitTestL()
       
   122 	{
       
   123 	test.Next(_L("-> Update Contact and Commit Test"));
       
   124 
       
   125 
       
   126 	TInt cntID = AddAndCommitContactL();
       
   127 
       
   128 	iPersistLayer->TransactionManager().StartTransactionL();	
       
   129 	UpdateContactL(cntID);
       
   130 	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
       
   131 	
       
   132 	if (CheckChangedFieldL(cntID))
       
   133 		{
       
   134 		test.Next(_L("-> TEST SUCCESS - Contact FOUND after Update commit test"));
       
   135 
       
   136 		return;
       
   137 		}
       
   138 	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after Update commit test"));
       
   139 
       
   140 	}
       
   141 
       
   142 
       
   143 void CPlTransMgrTest::DeleteAndCommitTestL()
       
   144 	{
       
   145 	test.Next(_L("-> Contact and Commit Test"));
       
   146 
       
   147 
       
   148 	TInt cntID = AddAndCommitContactL();
       
   149 
       
   150 	iPersistLayer->TransactionManager().StartTransactionL();	
       
   151 	DeleteContactL(cntID);
       
   152 	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
       
   153 	
       
   154 	if (!IsContactInDbL(cntID))
       
   155 		{
       
   156 		test.Next(_L("-> TEST SUCCESS - Contact FOUND after Update commit test"));
       
   157 
       
   158 		return;
       
   159 		}
       
   160 	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after Update commit test"));
       
   161 
       
   162 	}
       
   163 
       
   164 
       
   165 
       
   166 // Out Of Memory/Rollback/Recover Tests
       
   167 
       
   168 void CPlTransMgrTest::OutOfMemoryAddTestL()
       
   169 	{
       
   170 
       
   171 	TInt ret=KErrNoMemory;
       
   172 	TInt failAt=0;	  
       
   173 
       
   174 	while (ret!=KErrNone)
       
   175 		{
       
   176 		// Add a successful contact, guess the next contact id
       
   177 		TInt cntID = AddAndCommitContactL();
       
   178 		++cntID;
       
   179 		iPersistLayer->TransactionManager().StartTransactionL();
       
   180 		failAt++;
       
   181 		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
       
   182 		__UHEAP_MARK;			
       
   183 		TRAPD(err, AddContactL());
       
   184 		if (err==KErrNone)
       
   185 			{
       
   186 			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
       
   187 			if (err==KErrNone)
       
   188 				{
       
   189 				test.Next(_L("-> TEST SUCCESS - Contact committed during OOM Test"));
       
   190 
       
   191 				__UHEAP_RESET;
       
   192 				return;
       
   193 				}
       
   194 			}
       
   195 		__UHEAP_RESET;
       
   196 		if (err!=KErrNoMemory && err!=KErrNone)
       
   197 			{
       
   198 			test.Printf(_L("Non standard error: %d\n"),err);
       
   199 			}
       
   200 		if (err == KErrNoMemory)
       
   201 			{
       
   202 			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
       
   203 			 	// Close the database to allow recovery
       
   204   			iPersistLayer->ContactsFileL().Close();
       
   205 			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
       
   206 			if (!IsContactInDbL(cntID))
       
   207 				{
       
   208 				test.Printf(_L(" ->Correct result - Create rollback during OOM - Contact NOT Found... Step: %d \n\r"),failAt); 
       
   209 				}
       
   210 				else
       
   211 				{
       
   212 				test.Printf(_L(" ->Error - Delete rollback during OOM - Contact Found... Step: %d \n\r"),failAt);
       
   213 				User::Leave(KErrNotFound);
       
   214 				}
       
   215 			}
       
   216 		}
       
   217 	}
       
   218 
       
   219 
       
   220 void CPlTransMgrTest::OutOfMemoryDeleteTestL()
       
   221 	{
       
   222 	TInt ret	= KErrNoMemory;
       
   223 	TInt failAt = 0;	  
       
   224 
       
   225 	while (ret!=KErrNone)
       
   226 		{
       
   227 		failAt++;
       
   228 		TInt cntID = AddAndCommitContactL();
       
   229 		iPersistLayer->TransactionManager().StartTransactionL();
       
   230 		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
       
   231 		__UHEAP_MARK;			
       
   232 		TRAPD(err, DeleteContactL(cntID));
       
   233 		
       
   234 		if (err==KErrNone)
       
   235 			{
       
   236 			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
       
   237 			if (err==KErrNone)
       
   238 				{
       
   239 				test.Next(_L("-> TEST SUCCESS - Delete Contact committed during OOM Test"));
       
   240 
       
   241 				__UHEAP_RESET;
       
   242 				return;
       
   243 				}
       
   244 			}
       
   245 		__UHEAP_RESET;
       
   246 		if (err!=KErrNoMemory && err!=KErrNone)
       
   247 			{
       
   248 			test.Printf(_L("Non standard error: %d\n"),err);
       
   249 			}
       
   250 		if (err == KErrNoMemory)
       
   251 			{
       
   252 			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
       
   253 			// Close the database to allow recovery
       
   254   			iPersistLayer->ContactsFileL().Close();
       
   255 			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
       
   256 			if (IsContactInDbL(cntID))
       
   257 				{
       
   258 				test.Printf(_L(" ->Correct result - Delete rollback during OOM - Contact Found... Step: %d \n\r"),failAt); 
       
   259 				}
       
   260 				else
       
   261 				{
       
   262 				test.Printf(_L(" ->Error - Delete rollback during OOM - Contact NOT Found... Step: %d \n\r"),failAt);
       
   263 				User::Leave(KErrNotFound);
       
   264 				}
       
   265 			}
       
   266 		}
       
   267 	}
       
   268 
       
   269 
       
   270 void CPlTransMgrTest::OutOfMemoryUpdateTestL()
       
   271 	{
       
   272 
       
   273 	TInt ret=KErrNoMemory;
       
   274 	TInt failAt=0;	  
       
   275 	
       
   276 	iTestNo = 0;
       
   277 	while (ret!=KErrNone)
       
   278 		{
       
   279 		++iTestNo;
       
   280 		//if (iTestNo == 790)
       
   281 		//	TInt i = 0;
       
   282 		TInt cntID = AddAndCommitContactL();
       
   283 		iPersistLayer->TransactionManager().StartTransactionL();
       
   284 		failAt++;
       
   285 		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
       
   286 		__UHEAP_MARK;			
       
   287 		TRAPD(err, UpdateContactL(cntID));
       
   288 		if (err==KErrNone)
       
   289 			{
       
   290 			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
       
   291 			if (err==KErrNone)
       
   292 				{
       
   293 				test.Next(_L("-> TEST SUCCESS - Contact committed during OOM Test"));
       
   294 
       
   295 				__UHEAP_RESET;
       
   296 				return;
       
   297 				}
       
   298 			}
       
   299 			
       
   300 		__UHEAP_RESET;
       
   301 		if (err!=KErrNoMemory && err!=KErrNone)
       
   302 			{
       
   303 			test.Printf(_L("Non standard error: %d\n"),err);
       
   304 			}
       
   305 		if (err == KErrNoMemory)
       
   306 			{
       
   307 			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
       
   308 			// Close the database to allow recovery
       
   309   			iPersistLayer->ContactsFileL().Close();
       
   310 			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
       
   311 			
       
   312 			// Has the contact changed?
       
   313 			if (!CheckChangedFieldL(cntID))
       
   314 				{
       
   315 				test.Printf(_L(" ->Correct result - Update rollback during OOM - Updated Contact NOT Found... Step: %d \n\r"),failAt);			
       
   316 				}
       
   317 				else
       
   318 				{
       
   319 				test.Printf(_L(" ->Error - Update rollback during OOM - Updated Contact Found... Step: %d \n\r"),failAt);
       
   320 				User::Leave(KErrNotFound);
       
   321 				}
       
   322 			}
       
   323 		}
       
   324 	}
       
   325 
       
   326 
       
   327 
       
   328 // ----------- Helper methods -----------
       
   329 
       
   330 TInt CPlTransMgrTest::AddContactL()
       
   331 	{
       
   332 	// Create the contact
       
   333 	CContactItem* cntItem = iCntItemBldr->GetCntItemLC();    
       
   334 
       
   335     TInt cntID = iPersistLayer->PersistenceBroker().CreateL(*cntItem, 0);
       
   336 
       
   337 	CleanupStack::PopAndDestroy(cntItem);
       
   338 	cntItem = NULL;
       
   339 	return cntID;
       
   340 	}
       
   341 
       
   342 
       
   343 TInt CPlTransMgrTest::AddAndCommitContactL()
       
   344 	{
       
   345 	iPersistLayer->TransactionManager().StartTransactionL();
       
   346 	TInt cntID = AddContactL();
       
   347 	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
       
   348 	return cntID;
       
   349 	}
       
   350 
       
   351 
       
   352 void CPlTransMgrTest::DeleteContactL(TInt aCntID) 
       
   353 	{
       
   354     CContactItem* cntItem = iPersistLayer->PersistenceBroker().DeleteLC(aCntID, 0, ESendEvent);
       
   355 	CleanupStack::PopAndDestroy(cntItem);
       
   356 	}
       
   357 
       
   358 
       
   359 void CPlTransMgrTest::UpdateContactL(TInt aCntID)
       
   360 	{
       
   361 	CContactItem* cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
       
   362 	CCntItemModifier* cntModifier = CCntItemModifier::NewLC();
       
   363 	
       
   364 	cntModifier->SetTextFieldL(KUidContactFieldGivenName, nsPlTransMgr::KName,
       
   365 				 *cntItem);
       
   366 
       
   367 	cntModifier->SetTextFieldL(KUidContactFieldPhoneNumber,
       
   368 				KUidContactFieldVCardMapWORK, 
       
   369 				KUidContactFieldVCardMapVOICE,
       
   370 				KUidContactFieldVCardMapCELL,
       
   371 		 		nsPlTransMgr::KNumber, *cntItem);
       
   372 
       
   373 	cntModifier->SetTextFieldL(KUidContactFieldEMail,
       
   374 			    KUidContactFieldVCardMapWORK,
       
   375 			    nsPlTransMgr::KEmail, *cntItem);
       
   376 
       
   377 
       
   378 	cntModifier->SetTextFieldL(KUidContactFieldAddress, 
       
   379 				nsPlTransMgr::KHomeAdd, *cntItem);
       
   380 
       
   381 	CleanupStack::PopAndDestroy(cntModifier);
       
   382 
       
   383 	iPersistLayer->PersistenceBroker().UpdateL(*cntItem, 0);
       
   384 	CleanupStack::PopAndDestroy(cntItem);
       
   385 	}
       
   386 
       
   387 
       
   388 
       
   389 TBool CPlTransMgrTest::CheckChangedFieldL(TInt aCntID)
       
   390 	{
       
   391 	CContactItem* cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
       
   392 	CCntItemModifier* cntModifier = CCntItemModifier::NewLC();
       
   393 
       
   394 	TBool retval = cntModifier->FindTextFieldL(KUidContactFieldGivenName, nsPlTransMgr::KName,
       
   395 				 *cntItem) & 
       
   396 		cntModifier->FindTextFieldL(KUidContactFieldPhoneNumber,
       
   397 				KUidContactFieldVCardMapWORK, 
       
   398 				KUidContactFieldVCardMapVOICE,
       
   399 				KUidContactFieldVCardMapCELL,
       
   400 		 		nsPlTransMgr::KNumber, *cntItem) &
       
   401 		cntModifier->FindTextFieldL(KUidContactFieldAddress, 
       
   402 				nsPlTransMgr::KHomeAdd, *cntItem)&
       
   403 		cntModifier->FindTextFieldL(KUidContactFieldEMail,
       
   404 			    KUidContactFieldVCardMapWORK,
       
   405 			    nsPlTransMgr::KEmail, *cntItem); 
       
   406 
       
   407 	CleanupStack::PopAndDestroy(cntModifier);
       
   408 	CleanupStack::PopAndDestroy(cntItem);
       
   409 	return retval;
       
   410 	}
       
   411 
       
   412 
       
   413 
       
   414 TBool CPlTransMgrTest::IsContactInDbL(TInt aCntID)
       
   415 	{
       
   416 	CContactItem* cntItem = NULL;
       
   417 	TRAPD(err, cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
       
   418 		CleanupStack::PopAndDestroy(cntItem);
       
   419 		cntItem = NULL;
       
   420 		);
       
   421 	
       
   422 	if (err == KErrNotFound)
       
   423 		{
       
   424 		return FALSE;	
       
   425 		}
       
   426 	User::LeaveIfError(err);
       
   427 	return TRUE;
       
   428 	}
       
   429 
       
   430 
       
   431 
       
   432 /**
       
   433 
       
   434 @SYMTestCaseID     PIM-T-PLTRANSACTIONMANAGER-0001
       
   435 
       
   436 */
       
   437 
       
   438 LOCAL_C void DoTestsL()
       
   439 	{
       
   440     CleanupClosePushL(test);
       
   441 	test.Start(_L("@SYMTESTCaseID:PIM-T-PLTRANSACTIONMANAGER-0001 ----------- PLTransactionManager tests ----------"));
       
   442 
       
   443 
       
   444 	CPlTransMgrTest* tranMgrTest = CPlTransMgrTest::NewLC();	
       
   445 
       
   446 	tranMgrTest->RunTestsL();
       
   447 
       
   448 	test.End();
       
   449     CleanupStack::PopAndDestroy(2); // test.Close & tranMgrTest
       
   450 	}
       
   451 
       
   452 LOCAL_C void CleanupFilesL()
       
   453 	{
       
   454     // delete the database file
       
   455     RFs fs;
       
   456     CleanupClosePushL(fs);
       
   457     User::LeaveIfError(fs.Connect() );
       
   458 	CPersistenceLayer* persistLayer = CPersistenceLayer::NewLC(fs);
       
   459     MLplContactsFile& cntFile = persistLayer->ContactsFileL();
       
   460     if (cntFile.DatabaseExistsL(nsPlTransMgr::KFilename() ) )
       
   461     	{
       
   462     	cntFile.DeleteL(nsPlTransMgr::KFilename() );
       
   463     	}
       
   464     CleanupStack::PopAndDestroy(2, &fs); // and persistLayer
       
   465 	}
       
   466 
       
   467 
       
   468 GLDEF_C TInt E32Main()
       
   469 	{
       
   470     // Init
       
   471     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   472     if (!cleanupStack)
       
   473         return KErrNoMemory;
       
   474           
       
   475     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   476     if (!activeScheduler)
       
   477         {
       
   478         return KErrNoMemory;
       
   479         }
       
   480     CActiveScheduler::Install(activeScheduler);
       
   481 
       
   482     // Run the tests
       
   483     __UHEAP_MARK;
       
   484     TRAPD(err, DoTestsL());
       
   485     __UHEAP_MARKEND;
       
   486 
       
   487     TRAP_IGNORE(CleanupFilesL() );
       
   488 
       
   489     test(__NB_failures == 0);
       
   490 
       
   491     // Cleanup
       
   492 	delete activeScheduler;
       
   493     delete cleanupStack;
       
   494 	return err;
       
   495     }