os/persistentdata/persistentstorage/dbms/tdbms/t_dbwindow.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 <d32dbms.h>
    17 #include <s32file.h>
    18 #include <e32test.h>
    19 
    20 LOCAL_D RTest test(_L("t_dbwindow: Testing the 'window' stage"));
    21 LOCAL_D CTrapCleanup* TheTrapCleanup;
    22 LOCAL_D CFileStore* TheStore;
    23 LOCAL_D RDbStoreDatabase TheDatabase;
    24 LOCAL_D RDbView TheView;
    25 LOCAL_D RFs TheFs;
    26 
    27 const TInt KTestCleanupStack=0x20;
    28 const TPtrC KTestDir=_S("C:\\DBMS-TST\\");
    29 const TPtrC KTestFile=_S("T_WINDOW.DB");
    30 const TPtrC KTableName=_S("TestTable");
    31 const TPtrC KColumnID=_S("id");
    32 const TPtrC KIndexName=_S("TestIndex");
    33 const TInt KRecords=10;
    34 const TPtrC KCreateTable=_L("CREATE TABLE TestTable (id INTEGER NOT NULL)");
    35 const TPtrC KCreateIndex=_L("CREATE UNIQUE INDEX TestIndex ON TestTable (id)");
    36 const TPtrC KEmptyQuery=_L("select * from TestTable where id is null");
    37 const TPtrC KOrderQuery=_L("select * from TestTable order by id");
    38 const TDbWindow KSingleSlotWindow(0,0);
    39 const TDbWindow KSmallWindow(2,2);
    40 const TDbWindow KLargeWindow(20,0);
    41 
    42 //
    43 // Create the database-in-a-store
    44 //
    45 LOCAL_C void CreateDatabaseL()
    46 	{
    47 	CFileStore* store=CPermanentFileStore::ReplaceLC(TheFs,KTestFile,EFileRead|EFileWrite);
    48 	store->SetTypeL(KPermanentFileStoreLayoutUid);
    49 	TStreamId id;
    50 		id=TheDatabase.CreateL(store);
    51 	store->SetRootL(id);
    52 	store->CommitL();
    53 	CleanupStack::Pop();
    54 	TheStore=store;
    55 	}
    56 
    57 LOCAL_C void CloseDatabaseL()
    58 	{
    59 	// cannot evaluate size info for store database
    60 	RDbDatabase::TSize size=TheDatabase.Size();
    61 	test (size.iSize<0);
    62 	test (size.iUsage<0);
    63 	test (TheDatabase.UpdateStats()==KErrNone);
    64 	size=TheDatabase.Size();
    65 	test (size.iSize<0);
    66 	test (size.iUsage<0);
    67 	TheDatabase.Close();
    68 	delete TheStore;
    69 	}
    70 
    71 LOCAL_C void CreateTableL()
    72 	{
    73 	TheDatabase.Begin();
    74 	test(TheDatabase.Execute(KCreateTable)==KErrNone);
    75 	RDbTable table;
    76 	test(table.Open(TheDatabase,KTableName,table.EInsertOnly)==KErrNone);
    77 	for (TInt ii=0;ii<KRecords;++ii)
    78 		{
    79 		table.InsertL();
    80 		table.SetColL(1,ii);
    81 		table.PutL();
    82 		}
    83 	table.Close();
    84 	test(TheDatabase.Execute(KCreateIndex)==KErrNone);
    85 	test (TheDatabase.Commit()==KErrNone);
    86 	}
    87 
    88 LOCAL_C void InitL()
    89 	{
    90 	CreateDatabaseL();
    91 	CreateTableL();
    92 	}
    93 
    94 /**
    95 @SYMTestCaseID          SYSLIB-DBMS-CT-0638
    96 @SYMTestCaseDesc        Tests for navigation
    97 @SYMTestPriority        Medium
    98 @SYMTestActions         Tests for navigation in an empty window
    99 @SYMTestExpectedResults Test must not fail
   100 @SYMREQ                 REQ0000
   101 */
   102 LOCAL_C void TestEmptyL()
   103 	{
   104 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0638 Unevaluated "));
   105 	test(TheView.Prepare(TheDatabase,KEmptyQuery,TDbWindow::EUnlimited)==KErrNone);
   106 	test(TheView.CountL(TheView.EQuick)==0);
   107 	test(!TheView.FirstL());
   108 	test(TheView.AtEnd());
   109 	test(!TheView.PreviousL());
   110 	test(TheView.AtBeginning());
   111 	test(!TheView.LastL());
   112 	test(TheView.AtBeginning());
   113 	test(!TheView.NextL());
   114 	test(TheView.AtEnd());
   115 	test.Next(_L("Evaluated"));
   116 	test(TheView.EvaluateAll()==KErrNone);
   117 	test(TheView.CountL()==0);
   118 	test(!TheView.FirstL());
   119 	test(TheView.AtEnd());
   120 	test(!TheView.PreviousL());
   121 	test(TheView.AtBeginning());
   122 	test(!TheView.LastL());
   123 	test(TheView.AtBeginning());
   124 	test(!TheView.NextL());
   125 	test(TheView.AtEnd());
   126 	test.End();
   127 	TheView.Close();
   128 	}
   129 
   130 /**
   131 @SYMTestCaseID          SYSLIB-DBMS-CT-0639
   132 @SYMTestCaseDesc        Tests for navigation
   133 @SYMTestPriority        Medium
   134 @SYMTestActions         Tests for the unlimited (dynaset) window
   135 @SYMTestExpectedResults Test must not fail
   136 @SYMREQ                 REQ0000
   137 */
   138 LOCAL_C void TestUnlimitedL()
   139 	{
   140 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0639 EvaluateAll "));
   141 	test(TheView.Prepare(TheDatabase,KOrderQuery,TDbWindow::EUnlimited)==KErrNone);
   142 	test(TheView.EvaluateAll()==KErrNone);
   143 	test(!TheView.Unevaluated());
   144 	test(TheView.CountL()==KRecords);
   145 	TInt cc=0;
   146 	for (TheView.BeginningL();TheView.NextL();)
   147 		++cc;
   148 	test(cc==KRecords);
   149 	for (TheView.EndL();TheView.PreviousL();)
   150 		--cc;
   151 	test(cc==0);
   152 	test(!TheView.Unevaluated());
   153 	test.Next(_L("Incremental evaluate"));
   154 	TheView.Reset();
   155 	cc=TheView.CountL(TheView.EQuick);
   156 	test(cc==0);
   157 	test(!TheView.FirstL());
   158 	while (TheView.Unevaluated())
   159 		{
   160 		test(TheView.Evaluate()>=KErrNone);
   161 		TInt ii=TheView.CountL()-cc;
   162 		test(ii>=0);
   163 		cc+=ii;
   164 		while (--ii>=0)
   165 			{
   166 			test(TheView.AtRow());
   167 			TheView.NextL();
   168 			}
   169 		test(!TheView.AtRow());
   170 		}
   171 	test(cc==KRecords);
   172 	test.Next(_L("Insert"));
   173 // The new records should go on the end of the set
   174 	TheView.InsertL();
   175 	TheView.SetColL(1,TInt(-1));
   176 	TheView.PutL();
   177 	test(TheView.CountL()==KRecords+1);
   178 	test(TheView.LastL());
   179 	TheView.GetL();
   180 	test(TheView.ColInt(1)==-1);
   181 // because of order, it should appear at beginning on re-evaluation
   182 	TheView.Reset();
   183 	test(TheView.EvaluateAll()==KErrNone);
   184 	test(TheView.CountL()==KRecords+1);
   185 	test(TheView.FirstL());
   186 	TheView.GetL();
   187 	test(TheView.ColInt(1)==-1);
   188 	test.Next(_L("Update"));
   189 // updated records should not move (or disappear) in the set
   190 	TheView.UpdateL();
   191 	TheView.SetColL(1,KRecords);
   192 	TheView.PutL();
   193 	test(TheView.CountL()==KRecords+1);
   194 	test(TheView.FirstL());
   195 	TheView.GetL();
   196 	test(TheView.ColInt(1)==KRecords);
   197 // because of order, it should appear at end on re-evaluation
   198 	TheView.Reset();
   199 	test(TheView.EvaluateAll()==KErrNone);
   200 	test(TheView.CountL()==KRecords+1);
   201 	test(TheView.LastL());
   202 	TheView.GetL();
   203 	test(TheView.ColInt(1)==KRecords);
   204 	test.Next(_L("Bookmarks"));
   205 	TDbBookmark mark=TheView.Bookmark();
   206 	TheView.BeginningL();
   207 	TRAPD(r,TheView.GotoL(mark));
   208 	test(r==KErrNone);
   209 	TheView.GetL();
   210 	test(TheView.ColInt(1)==KRecords);
   211 	test(!TheView.NextL());
   212 	test(TheView.PreviousL());
   213 	test.Next(_L("Delete"));
   214 // should disappear from the set
   215 	TheView.DeleteL();
   216 	test(TheView.CountL()==KRecords);
   217 	for (TheView.BeginningL();TheView.NextL();)
   218 		{
   219 		TheView.GetL();
   220 		test(TheView.ColInt(1)!=KRecords);
   221 		}
   222 	TRAP(r,TheView.GotoL(mark));
   223 	test(r!=KErrNone);
   224 	TheView.Reset();
   225 	test(TheView.EvaluateAll()==KErrNone);
   226 	test(TheView.CountL()==KRecords);
   227 	for (TheView.BeginningL();TheView.NextL();)
   228 		{
   229 		TheView.GetL();
   230 		test(TheView.ColInt(1)!=KRecords);
   231 		}
   232 	TRAP(r,TheView.GotoL(mark));
   233 	test(r!=KErrNone);
   234 	test.End();
   235 	TheView.Close();
   236 	}
   237 
   238 //
   239 // do as much incremental evaluation as possible
   240 //
   241 LOCAL_C void Evaluate()
   242 	{
   243 	test(TheView.EvaluateAll()==KErrNone);
   244 	}
   245 
   246 //
   247 // get to the true end of set
   248 //
   249 LOCAL_C void GotoEndL()
   250 	{
   251 	for (;;)
   252 		{
   253 		while (TheView.AtRow())
   254 			TheView.NextL();
   255 		if (!TheView.Unevaluated())
   256 			break;
   257 		Evaluate();
   258 		}
   259 	}
   260 
   261 LOCAL_C void CheckWindowL(TInt forward,TInt back)
   262 	{
   263 	TInt ii;
   264 	for (ii=0;ii<forward;++ii)
   265 		test(TheView.NextL());
   266 	test(!TheView.NextL());
   267 	for (ii=0;ii<forward+back+1;++ii)
   268 		test(TheView.PreviousL());
   269 	test(!TheView.PreviousL());
   270 	for (ii=0;ii<back+1;++ii)
   271 		test(TheView.NextL());
   272 	}
   273 
   274 /**
   275 @SYMTestCaseID          SYSLIB-DBMS-CT-0640
   276 @SYMTestCaseDesc        Tests for RDbView
   277 @SYMTestPriority        Medium
   278 @SYMTestActions         Tests for a restricted sized window
   279 @SYMTestExpectedResults Test must not fail
   280 @SYMREQ                 REQ0000
   281 */
   282 LOCAL_C void TestRestrictedL()
   283 	{
   284 	test.Start(_L("	@SYMTestCaseID:SYSLIB-DBMS-CT-0640 Behaviour at start of set "));
   285 	test(TheView.Prepare(TheDatabase,KOrderQuery,KSingleSlotWindow)==KErrNone);
   286 	test(TheView.EvaluateAll()==KErrNone);
   287 	test(TheView.CountL()==1);
   288 	test(!TheView.Unevaluated());
   289 	test(TheView.NextL());
   290 	test(!TheView.Unevaluated());
   291 	TheView.GetL();
   292 	TInt id=TheView.ColInt(1);
   293 	test(!TheView.NextL());
   294 	test(TheView.Unevaluated());
   295 	Evaluate();
   296 	test(TheView.CountL()==1);
   297 	test(TheView.AtRow());
   298 	test(!TheView.PreviousL());
   299 	test(TheView.Unevaluated());
   300 	Evaluate();
   301 	test(TheView.CountL()==1);
   302 	test(TheView.AtRow());
   303 	TheView.GetL();
   304 	test(TheView.ColInt(1)==id);
   305 	test(!TheView.PreviousL());
   306 	test(TheView.Unevaluated());
   307 	Evaluate();
   308 	test(TheView.CountL()==1);
   309 	test(TheView.AtBeginning());
   310 	test(TheView.NextL());
   311 	TheView.GetL();
   312 	test(TheView.ColInt(1)==id);
   313 	test(!TheView.Unevaluated());
   314 	test.Next(_L("Behaviour at end of set"));
   315 	GotoEndL();
   316 	test(TheView.LastL());
   317 	test(!TheView.Unevaluated());
   318 	TheView.GetL();
   319 	id=TheView.ColInt(1);
   320 	test(!TheView.PreviousL());
   321 	test(TheView.Unevaluated());
   322 	Evaluate();
   323 	test(TheView.CountL()==1);
   324 	test(TheView.AtRow());
   325 	test(!TheView.NextL());
   326 	test(TheView.Unevaluated());
   327 	Evaluate();
   328 	test(TheView.CountL()==1);
   329 	test(TheView.AtRow());
   330 	TheView.GetL();
   331 	test(TheView.ColInt(1)==id);
   332 	test(!TheView.NextL());
   333 	test(TheView.Unevaluated());
   334 	Evaluate();
   335 	test(TheView.CountL()==1);
   336 	test(TheView.AtEnd());
   337 	test(TheView.PreviousL());
   338 	TheView.GetL();
   339 	test(TheView.ColInt(1)==id);
   340 	test(!TheView.Unevaluated());
   341 	TheView.Close();
   342 //
   343 	test.Next(_L("Forward and backwards slots"));
   344 	test(TheView.Prepare(TheDatabase,KOrderQuery,KSmallWindow)==KErrNone);
   345 	test(TheView.EvaluateAll()==KErrNone);
   346 	test(TheView.CountL()==5);
   347 	test(TheView.FirstL());
   348 	CheckWindowL(4,0);
   349 	test(TheView.NextL());
   350 	test(TheView.NextL());		// now on preferred slot (id=2)
   351 	test(!TheView.Unevaluated());
   352 	test(TheView.NextL());
   353 	test(TheView.Unevaluated());
   354 	Evaluate();
   355 	CheckWindowL(2,2);
   356 	test(TheView.LastL());		// id=5
   357 	Evaluate();
   358 	test(TheView.LastL());		// id=7
   359 	Evaluate();					// should now have last five rows
   360 	CheckWindowL(2,2);
   361 	test(TheView.LastL());		// id=9
   362 	Evaluate();					// no more rows
   363 	CheckWindowL(0,4);			// all behind us now
   364 	TheView.GetL();
   365 	test(TheView.ColInt(1)==9);
   366 	TheView.FirstL();			// id=5
   367 	Evaluate();
   368 	TheView.FirstL();			// id=3
   369 	Evaluate();
   370 	TheView.FirstL();			// id=1
   371 	Evaluate();
   372 	CheckWindowL(3,1);
   373 //
   374 	test.Next(_L("Bookmarks"));
   375 	TDbBookmark mark=TheView.Bookmark();
   376 	test(TheView.NextL());
   377 	test(TheView.NextL());
   378 	test(TheView.NextL());
   379 	Evaluate();
   380 	TRAPD(r,TheView.GotoL(mark));
   381 	test(r!=KErrNone);
   382 	TheView.FirstL();
   383 	Evaluate();
   384 	TRAP(r,TheView.GotoL(mark));
   385 	test(r==KErrNone);
   386 	test.Next(_L("Delete"));
   387 	test(TheView.FirstL());
   388 	TheView.GetL();
   389 	test(TheView.ColInt(1)==0);
   390 	TheView.DeleteL();
   391 	test(TheView.CountL()==4);
   392 	TheView.FirstL();
   393 	CheckWindowL(3,0);
   394 	test.Next(_L("Insert"));
   395 	TheView.InsertL();
   396 	TheView.SetColL(1,TInt(0));
   397 	TheView.PutL();
   398 	test(TheView.CountL()==4);
   399 	TheView.FirstL();
   400 	TheView.GetL();
   401 	test(TheView.ColInt(1)==1);
   402 	TheView.Reset();
   403 	test(TheView.EvaluateAll()==KErrNone);
   404 	test(TheView.FirstL());
   405 	TheView.GetL();
   406 	test(TheView.ColInt(1)==0);
   407 //
   408 	test.End();
   409 	TheView.Close();
   410 	}
   411 
   412 LOCAL_C void TestL()
   413 	{
   414 	__UHEAP_MARK;
   415 	test.Start(_L("Setting up test table"));
   416 	TRAPD(r,InitL();)
   417 	test(r==KErrNone);
   418 	test.Next(_L("Empty Window"));
   419 	TRAP(r,TestEmptyL();)
   420 	test(r==KErrNone);
   421 	test.Next(_L("Unlimited Window"));
   422 	TRAP(r,TestUnlimitedL();)
   423 	test(r==KErrNone);
   424 	test.Next(_L("Sized window"));
   425 	TRAP(r,TestRestrictedL();)
   426 	test(r==KErrNone);
   427 	test.Next(_L("Cleanup"));
   428 	TRAP(r,CloseDatabaseL();)
   429 	test(r==KErrNone);
   430 	test.End();
   431 	__UHEAP_MARKEND;
   432 	}
   433 
   434 //
   435 // Prepare the test directory.
   436 //
   437 LOCAL_C void setupTestDirectory()
   438     {
   439 	TInt r=TheFs.Connect();
   440 	test(r==KErrNone);
   441 //
   442 	r=TheFs.MkDir(KTestDir);
   443 	test(r==KErrNone || r==KErrAlreadyExists);
   444 	r=TheFs.SetSessionPath(KTestDir);
   445 	test(r==KErrNone);
   446 	}
   447 
   448 //
   449 // Initialise the cleanup stack.
   450 //
   451 LOCAL_C void setupCleanup()
   452     {
   453 	TheTrapCleanup=CTrapCleanup::New();
   454 	test(TheTrapCleanup!=NULL);
   455 	TRAPD(r,\
   456 		{\
   457 		for (TInt i=KTestCleanupStack;i>0;i--)\
   458 			CleanupStack::PushL((TAny*)0);\
   459 		CleanupStack::Pop(KTestCleanupStack);\
   460 		});
   461 	test(r==KErrNone);
   462 	}
   463 
   464 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   465 	{
   466 	RFs fsSession;
   467 	TInt err = fsSession.Connect();
   468 	if(err == KErrNone)
   469 		{
   470 		TEntry entry;
   471 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   472 			{
   473 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   474 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   475 			if(err != KErrNone)
   476 				{
   477 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   478 				}
   479 			err = fsSession.Delete(aFullName);
   480 			if(err != KErrNone)
   481 				{
   482 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   483 				}
   484 			}
   485 		fsSession.Close();
   486 		}
   487 	else
   488 		{
   489 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   490 		}
   491 	}
   492 
   493 //
   494 // Test streaming conversions.
   495 //
   496 GLDEF_C TInt E32Main()
   497     {
   498 	test.Title();
   499 	setupTestDirectory();
   500 	setupCleanup();
   501 //
   502 	test.Start(_L("TDbWindow - database test"));
   503 	TRAPD(err, TestL());
   504 	test(err == KErrNone);
   505 
   506 	//deletion of data files must be done before call to end - DEF047652
   507 	_LIT(KTestDbName, "C:\\DBMS-TST\\T_WINDOW.DB");
   508 	::DeleteDataFile(KTestDbName);
   509 	test.End();
   510 //
   511 
   512 	delete TheTrapCleanup;
   513 	TheFs.Close();
   514 	test.Close();
   515 	return 0;
   516     }