summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/cntmodel2/t_pltransactionmanager.cpp
blob: 2987eb9d61bf11e562c13c963f7fe7dc1ec7d9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


#include "t_pltransactionmanager.h"
_LIT(KTestName, "T_PlTransactionManager"); // Used by Testhelper - gives an warning under ARMv5
#include "../../tsrc/testhelpers.h"
#include <cntdef.h>


CPlTransMgrTest* CPlTransMgrTest::NewLC()
	{
	CPlTransMgrTest* self = new (ELeave) CPlTransMgrTest();
	CleanupStack::PushL(self);

	self->ConstructL();
	return self;	
	}



void CPlTransMgrTest::ConstructL()
	{
	User::LeaveIfError(iFs.Connect());
	iPersistLayer = CPersistenceLayer::NewLC(iFs);
	CleanupStack::Pop(iPersistLayer); 

	MLplContactsFile& cntFile = iPersistLayer->ContactsFileL();
	// Empty loops required to remove winscw warning
	cntFile.CreateL(nsPlTransMgr::KFilename, MLplContactsFile::EPlOverwrite);
	while(cntFile.OpenStepL   (nsPlTransMgr::KFilename)){};

	iCntItemBldr = CCntItemBuilder::NewLC(iPersistLayer->ContactProperties().SystemTemplateL());
	CleanupStack::Pop(iCntItemBldr);

	iMatchAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,
										   CContactItemViewDef::EIncludeHiddenFields);
	iMatchAll->AddL(KUidContactFieldMatchAll);
	CleanupStack::Pop(iMatchAll);
	}



CPlTransMgrTest::~CPlTransMgrTest()
	{
	delete iMatchAll;
	delete iCntItemBldr;
	delete iPersistLayer;
	iFs.Close();
	}



void CPlTransMgrTest::RunTestsL()
	{
	UpdateAndCommitTestL();
	CreateAndCommitTestL(); 
	DeleteAndCommitTestL();
	RollbackAndRecoverTestL();
	OutOfMemoryUpdateTestL();
	OutOfMemoryDeleteTestL();
	OutOfMemoryAddTestL();
	}

// ---------- Tests ---------

// Create/commit & Rollback/recover tests.

void CPlTransMgrTest::CreateAndCommitTestL()
	{
	test.Next(_L("-> Create Contact and Commit Test"));

	TInt cntID = AddAndCommitContactL();
	
	if (IsContactInDbL(cntID))
		{
		test.Next(_L("-> TEST SUCCESS - Contact FOUND after commit test"));

		return;
		}
	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after commit test"));

	}

void CPlTransMgrTest::RollbackAndRecoverTestL()
	{
	test.Next(_L("-> Rollback & Recover Test"));

	iPersistLayer->TransactionManager().StartTransactionL();

	TInt cntID = AddContactL();
	
	iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
  	// Close the database to allow recovery
  	iPersistLayer->ContactsFileL().Close();
  		
	iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
	
	if (!IsContactInDbL(cntID))
		{
		test.Next(_L("-> TEST SUCCESS - Contact was NOT Found after Rollback test"));

		return;
		}
	test.Next(_L("-> TEST FAILED - Contact WAS Found after ROLLBACK test"));

	}


void CPlTransMgrTest::UpdateAndCommitTestL()
	{
	test.Next(_L("-> Update Contact and Commit Test"));


	TInt cntID = AddAndCommitContactL();

	iPersistLayer->TransactionManager().StartTransactionL();	
	UpdateContactL(cntID);
	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
	
	if (CheckChangedFieldL(cntID))
		{
		test.Next(_L("-> TEST SUCCESS - Contact FOUND after Update commit test"));

		return;
		}
	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after Update commit test"));

	}


void CPlTransMgrTest::DeleteAndCommitTestL()
	{
	test.Next(_L("-> Contact and Commit Test"));


	TInt cntID = AddAndCommitContactL();

	iPersistLayer->TransactionManager().StartTransactionL();	
	DeleteContactL(cntID);
	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
	
	if (!IsContactInDbL(cntID))
		{
		test.Next(_L("-> TEST SUCCESS - Contact FOUND after Update commit test"));

		return;
		}
	test.Next(_L("-> TEST FAILED - Contact NOT FOUND after Update commit test"));

	}



// Out Of Memory/Rollback/Recover Tests

void CPlTransMgrTest::OutOfMemoryAddTestL()
	{

	TInt ret=KErrNoMemory;
	TInt failAt=0;	  

	while (ret!=KErrNone)
		{
		// Add a successful contact, guess the next contact id
		TInt cntID = AddAndCommitContactL();
		++cntID;
		iPersistLayer->TransactionManager().StartTransactionL();
		failAt++;
		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
		__UHEAP_MARK;			
		TRAPD(err, AddContactL());
		if (err==KErrNone)
			{
			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
			if (err==KErrNone)
				{
				test.Next(_L("-> TEST SUCCESS - Contact committed during OOM Test"));

				__UHEAP_RESET;
				return;
				}
			}
		__UHEAP_RESET;
		if (err!=KErrNoMemory && err!=KErrNone)
			{
			test.Printf(_L("Non standard error: %d\n"),err);
			}
		if (err == KErrNoMemory)
			{
			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
			 	// Close the database to allow recovery
  			iPersistLayer->ContactsFileL().Close();
			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
			if (!IsContactInDbL(cntID))
				{
				test.Printf(_L(" ->Correct result - Create rollback during OOM - Contact NOT Found... Step: %d \n\r"),failAt); 
				}
				else
				{
				test.Printf(_L(" ->Error - Delete rollback during OOM - Contact Found... Step: %d \n\r"),failAt);
				User::Leave(KErrNotFound);
				}
			}
		}
	}


void CPlTransMgrTest::OutOfMemoryDeleteTestL()
	{
	TInt ret	= KErrNoMemory;
	TInt failAt = 0;	  

	while (ret!=KErrNone)
		{
		failAt++;
		TInt cntID = AddAndCommitContactL();
		iPersistLayer->TransactionManager().StartTransactionL();
		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
		__UHEAP_MARK;			
		TRAPD(err, DeleteContactL(cntID));
		
		if (err==KErrNone)
			{
			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
			if (err==KErrNone)
				{
				test.Next(_L("-> TEST SUCCESS - Delete Contact committed during OOM Test"));

				__UHEAP_RESET;
				return;
				}
			}
		__UHEAP_RESET;
		if (err!=KErrNoMemory && err!=KErrNone)
			{
			test.Printf(_L("Non standard error: %d\n"),err);
			}
		if (err == KErrNoMemory)
			{
			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
			// Close the database to allow recovery
  			iPersistLayer->ContactsFileL().Close();
			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
			if (IsContactInDbL(cntID))
				{
				test.Printf(_L(" ->Correct result - Delete rollback during OOM - Contact Found... Step: %d \n\r"),failAt); 
				}
				else
				{
				test.Printf(_L(" ->Error - Delete rollback during OOM - Contact NOT Found... Step: %d \n\r"),failAt);
				User::Leave(KErrNotFound);
				}
			}
		}
	}


void CPlTransMgrTest::OutOfMemoryUpdateTestL()
	{

	TInt ret=KErrNoMemory;
	TInt failAt=0;	  
	
	iTestNo = 0;
	while (ret!=KErrNone)
		{
		++iTestNo;
		//if (iTestNo == 790)
		//	TInt i = 0;
		TInt cntID = AddAndCommitContactL();
		iPersistLayer->TransactionManager().StartTransactionL();
		failAt++;
		__UHEAP_SETFAIL(RHeap::EDeterministic,failAt);
		__UHEAP_MARK;			
		TRAPD(err, UpdateContactL(cntID));
		if (err==KErrNone)
			{
			TRAP(err, iPersistLayer->TransactionManager().CommitCurrentTransactionL(0));
			if (err==KErrNone)
				{
				test.Next(_L("-> TEST SUCCESS - Contact committed during OOM Test"));

				__UHEAP_RESET;
				return;
				}
			}
			
		__UHEAP_RESET;
		if (err!=KErrNoMemory && err!=KErrNone)
			{
			test.Printf(_L("Non standard error: %d\n"),err);
			}
		if (err == KErrNoMemory)
			{
			iPersistLayer->TransactionManager().RollbackCurrentTransactionL(0);
			// Close the database to allow recovery
  			iPersistLayer->ContactsFileL().Close();
			iPersistLayer->ContactsFileL().RecoverL(nsPlTransMgr::KFilename);
			
			// Has the contact changed?
			if (!CheckChangedFieldL(cntID))
				{
				test.Printf(_L(" ->Correct result - Update rollback during OOM - Updated Contact NOT Found... Step: %d \n\r"),failAt);			
				}
				else
				{
				test.Printf(_L(" ->Error - Update rollback during OOM - Updated Contact Found... Step: %d \n\r"),failAt);
				User::Leave(KErrNotFound);
				}
			}
		}
	}



// ----------- Helper methods -----------

TInt CPlTransMgrTest::AddContactL()
	{
	// Create the contact
	CContactItem* cntItem = iCntItemBldr->GetCntItemLC();    

    TInt cntID = iPersistLayer->PersistenceBroker().CreateL(*cntItem, 0);

	CleanupStack::PopAndDestroy(cntItem);
	cntItem = NULL;
	return cntID;
	}


TInt CPlTransMgrTest::AddAndCommitContactL()
	{
	iPersistLayer->TransactionManager().StartTransactionL();
	TInt cntID = AddContactL();
	iPersistLayer->TransactionManager().CommitCurrentTransactionL(0);
	return cntID;
	}


void CPlTransMgrTest::DeleteContactL(TInt aCntID) 
	{
    CContactItem* cntItem = iPersistLayer->PersistenceBroker().DeleteLC(aCntID, 0, ESendEvent);
	CleanupStack::PopAndDestroy(cntItem);
	}


void CPlTransMgrTest::UpdateContactL(TInt aCntID)
	{
	CContactItem* cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
	CCntItemModifier* cntModifier = CCntItemModifier::NewLC();
	
	cntModifier->SetTextFieldL(KUidContactFieldGivenName, nsPlTransMgr::KName,
				 *cntItem);

	cntModifier->SetTextFieldL(KUidContactFieldPhoneNumber,
				KUidContactFieldVCardMapWORK, 
				KUidContactFieldVCardMapVOICE,
				KUidContactFieldVCardMapCELL,
		 		nsPlTransMgr::KNumber, *cntItem);

	cntModifier->SetTextFieldL(KUidContactFieldEMail,
			    KUidContactFieldVCardMapWORK,
			    nsPlTransMgr::KEmail, *cntItem);


	cntModifier->SetTextFieldL(KUidContactFieldAddress, 
				nsPlTransMgr::KHomeAdd, *cntItem);

	CleanupStack::PopAndDestroy(cntModifier);

	iPersistLayer->PersistenceBroker().UpdateL(*cntItem, 0);
	CleanupStack::PopAndDestroy(cntItem);
	}



TBool CPlTransMgrTest::CheckChangedFieldL(TInt aCntID)
	{
	CContactItem* cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
	CCntItemModifier* cntModifier = CCntItemModifier::NewLC();

	TBool retval = cntModifier->FindTextFieldL(KUidContactFieldGivenName, nsPlTransMgr::KName,
				 *cntItem) & 
		cntModifier->FindTextFieldL(KUidContactFieldPhoneNumber,
				KUidContactFieldVCardMapWORK, 
				KUidContactFieldVCardMapVOICE,
				KUidContactFieldVCardMapCELL,
		 		nsPlTransMgr::KNumber, *cntItem) &
		cntModifier->FindTextFieldL(KUidContactFieldAddress, 
				nsPlTransMgr::KHomeAdd, *cntItem)&
		cntModifier->FindTextFieldL(KUidContactFieldEMail,
			    KUidContactFieldVCardMapWORK,
			    nsPlTransMgr::KEmail, *cntItem); 

	CleanupStack::PopAndDestroy(cntModifier);
	CleanupStack::PopAndDestroy(cntItem);
	return retval;
	}



TBool CPlTransMgrTest::IsContactInDbL(TInt aCntID)
	{
	CContactItem* cntItem = NULL;
	TRAPD(err, cntItem = iPersistLayer->PersistenceBroker().ReadLC(aCntID, *iMatchAll, EPlAllInfo, 0);
		CleanupStack::PopAndDestroy(cntItem);
		cntItem = NULL;
		);
	
	if (err == KErrNotFound)
		{
		return FALSE;	
		}
	User::LeaveIfError(err);
	return TRUE;
	}



/**

@SYMTestCaseID     PIM-T-PLTRANSACTIONMANAGER-0001

*/

LOCAL_C void DoTestsL()
	{
    CleanupClosePushL(test);
	test.Start(_L("@SYMTESTCaseID:PIM-T-PLTRANSACTIONMANAGER-0001 ----------- PLTransactionManager tests ----------"));


	CPlTransMgrTest* tranMgrTest = CPlTransMgrTest::NewLC();	

	tranMgrTest->RunTestsL();

	test.End();
    CleanupStack::PopAndDestroy(2); // test.Close & tranMgrTest
	}

LOCAL_C void CleanupFilesL()
	{
    // delete the database file
    RFs fs;
    CleanupClosePushL(fs);
    User::LeaveIfError(fs.Connect() );
	CPersistenceLayer* persistLayer = CPersistenceLayer::NewLC(fs);
    MLplContactsFile& cntFile = persistLayer->ContactsFileL();
    if (cntFile.DatabaseExistsL(nsPlTransMgr::KFilename() ) )
    	{
    	cntFile.DeleteL(nsPlTransMgr::KFilename() );
    	}
    CleanupStack::PopAndDestroy(2, &fs); // and persistLayer
	}


GLDEF_C TInt E32Main()
	{
    // Init
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        return KErrNoMemory;
          
    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    __UHEAP_MARK;
    TRAPD(err, DoTestsL());
    __UHEAP_MARKEND;

    TRAP_IGNORE(CleanupFilesL() );

    test(__NB_failures == 0);

    // Cleanup
	delete activeScheduler;
    delete cleanupStack;
	return err;
    }