1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/dbms/tdbms/t_dbwindow.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,516 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <d32dbms.h>
1.20 +#include <s32file.h>
1.21 +#include <e32test.h>
1.22 +
1.23 +LOCAL_D RTest test(_L("t_dbwindow: Testing the 'window' stage"));
1.24 +LOCAL_D CTrapCleanup* TheTrapCleanup;
1.25 +LOCAL_D CFileStore* TheStore;
1.26 +LOCAL_D RDbStoreDatabase TheDatabase;
1.27 +LOCAL_D RDbView TheView;
1.28 +LOCAL_D RFs TheFs;
1.29 +
1.30 +const TInt KTestCleanupStack=0x20;
1.31 +const TPtrC KTestDir=_S("C:\\DBMS-TST\\");
1.32 +const TPtrC KTestFile=_S("T_WINDOW.DB");
1.33 +const TPtrC KTableName=_S("TestTable");
1.34 +const TPtrC KColumnID=_S("id");
1.35 +const TPtrC KIndexName=_S("TestIndex");
1.36 +const TInt KRecords=10;
1.37 +const TPtrC KCreateTable=_L("CREATE TABLE TestTable (id INTEGER NOT NULL)");
1.38 +const TPtrC KCreateIndex=_L("CREATE UNIQUE INDEX TestIndex ON TestTable (id)");
1.39 +const TPtrC KEmptyQuery=_L("select * from TestTable where id is null");
1.40 +const TPtrC KOrderQuery=_L("select * from TestTable order by id");
1.41 +const TDbWindow KSingleSlotWindow(0,0);
1.42 +const TDbWindow KSmallWindow(2,2);
1.43 +const TDbWindow KLargeWindow(20,0);
1.44 +
1.45 +//
1.46 +// Create the database-in-a-store
1.47 +//
1.48 +LOCAL_C void CreateDatabaseL()
1.49 + {
1.50 + CFileStore* store=CPermanentFileStore::ReplaceLC(TheFs,KTestFile,EFileRead|EFileWrite);
1.51 + store->SetTypeL(KPermanentFileStoreLayoutUid);
1.52 + TStreamId id;
1.53 + id=TheDatabase.CreateL(store);
1.54 + store->SetRootL(id);
1.55 + store->CommitL();
1.56 + CleanupStack::Pop();
1.57 + TheStore=store;
1.58 + }
1.59 +
1.60 +LOCAL_C void CloseDatabaseL()
1.61 + {
1.62 + // cannot evaluate size info for store database
1.63 + RDbDatabase::TSize size=TheDatabase.Size();
1.64 + test (size.iSize<0);
1.65 + test (size.iUsage<0);
1.66 + test (TheDatabase.UpdateStats()==KErrNone);
1.67 + size=TheDatabase.Size();
1.68 + test (size.iSize<0);
1.69 + test (size.iUsage<0);
1.70 + TheDatabase.Close();
1.71 + delete TheStore;
1.72 + }
1.73 +
1.74 +LOCAL_C void CreateTableL()
1.75 + {
1.76 + TheDatabase.Begin();
1.77 + test(TheDatabase.Execute(KCreateTable)==KErrNone);
1.78 + RDbTable table;
1.79 + test(table.Open(TheDatabase,KTableName,table.EInsertOnly)==KErrNone);
1.80 + for (TInt ii=0;ii<KRecords;++ii)
1.81 + {
1.82 + table.InsertL();
1.83 + table.SetColL(1,ii);
1.84 + table.PutL();
1.85 + }
1.86 + table.Close();
1.87 + test(TheDatabase.Execute(KCreateIndex)==KErrNone);
1.88 + test (TheDatabase.Commit()==KErrNone);
1.89 + }
1.90 +
1.91 +LOCAL_C void InitL()
1.92 + {
1.93 + CreateDatabaseL();
1.94 + CreateTableL();
1.95 + }
1.96 +
1.97 +/**
1.98 +@SYMTestCaseID SYSLIB-DBMS-CT-0638
1.99 +@SYMTestCaseDesc Tests for navigation
1.100 +@SYMTestPriority Medium
1.101 +@SYMTestActions Tests for navigation in an empty window
1.102 +@SYMTestExpectedResults Test must not fail
1.103 +@SYMREQ REQ0000
1.104 +*/
1.105 +LOCAL_C void TestEmptyL()
1.106 + {
1.107 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0638 Unevaluated "));
1.108 + test(TheView.Prepare(TheDatabase,KEmptyQuery,TDbWindow::EUnlimited)==KErrNone);
1.109 + test(TheView.CountL(TheView.EQuick)==0);
1.110 + test(!TheView.FirstL());
1.111 + test(TheView.AtEnd());
1.112 + test(!TheView.PreviousL());
1.113 + test(TheView.AtBeginning());
1.114 + test(!TheView.LastL());
1.115 + test(TheView.AtBeginning());
1.116 + test(!TheView.NextL());
1.117 + test(TheView.AtEnd());
1.118 + test.Next(_L("Evaluated"));
1.119 + test(TheView.EvaluateAll()==KErrNone);
1.120 + test(TheView.CountL()==0);
1.121 + test(!TheView.FirstL());
1.122 + test(TheView.AtEnd());
1.123 + test(!TheView.PreviousL());
1.124 + test(TheView.AtBeginning());
1.125 + test(!TheView.LastL());
1.126 + test(TheView.AtBeginning());
1.127 + test(!TheView.NextL());
1.128 + test(TheView.AtEnd());
1.129 + test.End();
1.130 + TheView.Close();
1.131 + }
1.132 +
1.133 +/**
1.134 +@SYMTestCaseID SYSLIB-DBMS-CT-0639
1.135 +@SYMTestCaseDesc Tests for navigation
1.136 +@SYMTestPriority Medium
1.137 +@SYMTestActions Tests for the unlimited (dynaset) window
1.138 +@SYMTestExpectedResults Test must not fail
1.139 +@SYMREQ REQ0000
1.140 +*/
1.141 +LOCAL_C void TestUnlimitedL()
1.142 + {
1.143 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0639 EvaluateAll "));
1.144 + test(TheView.Prepare(TheDatabase,KOrderQuery,TDbWindow::EUnlimited)==KErrNone);
1.145 + test(TheView.EvaluateAll()==KErrNone);
1.146 + test(!TheView.Unevaluated());
1.147 + test(TheView.CountL()==KRecords);
1.148 + TInt cc=0;
1.149 + for (TheView.BeginningL();TheView.NextL();)
1.150 + ++cc;
1.151 + test(cc==KRecords);
1.152 + for (TheView.EndL();TheView.PreviousL();)
1.153 + --cc;
1.154 + test(cc==0);
1.155 + test(!TheView.Unevaluated());
1.156 + test.Next(_L("Incremental evaluate"));
1.157 + TheView.Reset();
1.158 + cc=TheView.CountL(TheView.EQuick);
1.159 + test(cc==0);
1.160 + test(!TheView.FirstL());
1.161 + while (TheView.Unevaluated())
1.162 + {
1.163 + test(TheView.Evaluate()>=KErrNone);
1.164 + TInt ii=TheView.CountL()-cc;
1.165 + test(ii>=0);
1.166 + cc+=ii;
1.167 + while (--ii>=0)
1.168 + {
1.169 + test(TheView.AtRow());
1.170 + TheView.NextL();
1.171 + }
1.172 + test(!TheView.AtRow());
1.173 + }
1.174 + test(cc==KRecords);
1.175 + test.Next(_L("Insert"));
1.176 +// The new records should go on the end of the set
1.177 + TheView.InsertL();
1.178 + TheView.SetColL(1,TInt(-1));
1.179 + TheView.PutL();
1.180 + test(TheView.CountL()==KRecords+1);
1.181 + test(TheView.LastL());
1.182 + TheView.GetL();
1.183 + test(TheView.ColInt(1)==-1);
1.184 +// because of order, it should appear at beginning on re-evaluation
1.185 + TheView.Reset();
1.186 + test(TheView.EvaluateAll()==KErrNone);
1.187 + test(TheView.CountL()==KRecords+1);
1.188 + test(TheView.FirstL());
1.189 + TheView.GetL();
1.190 + test(TheView.ColInt(1)==-1);
1.191 + test.Next(_L("Update"));
1.192 +// updated records should not move (or disappear) in the set
1.193 + TheView.UpdateL();
1.194 + TheView.SetColL(1,KRecords);
1.195 + TheView.PutL();
1.196 + test(TheView.CountL()==KRecords+1);
1.197 + test(TheView.FirstL());
1.198 + TheView.GetL();
1.199 + test(TheView.ColInt(1)==KRecords);
1.200 +// because of order, it should appear at end on re-evaluation
1.201 + TheView.Reset();
1.202 + test(TheView.EvaluateAll()==KErrNone);
1.203 + test(TheView.CountL()==KRecords+1);
1.204 + test(TheView.LastL());
1.205 + TheView.GetL();
1.206 + test(TheView.ColInt(1)==KRecords);
1.207 + test.Next(_L("Bookmarks"));
1.208 + TDbBookmark mark=TheView.Bookmark();
1.209 + TheView.BeginningL();
1.210 + TRAPD(r,TheView.GotoL(mark));
1.211 + test(r==KErrNone);
1.212 + TheView.GetL();
1.213 + test(TheView.ColInt(1)==KRecords);
1.214 + test(!TheView.NextL());
1.215 + test(TheView.PreviousL());
1.216 + test.Next(_L("Delete"));
1.217 +// should disappear from the set
1.218 + TheView.DeleteL();
1.219 + test(TheView.CountL()==KRecords);
1.220 + for (TheView.BeginningL();TheView.NextL();)
1.221 + {
1.222 + TheView.GetL();
1.223 + test(TheView.ColInt(1)!=KRecords);
1.224 + }
1.225 + TRAP(r,TheView.GotoL(mark));
1.226 + test(r!=KErrNone);
1.227 + TheView.Reset();
1.228 + test(TheView.EvaluateAll()==KErrNone);
1.229 + test(TheView.CountL()==KRecords);
1.230 + for (TheView.BeginningL();TheView.NextL();)
1.231 + {
1.232 + TheView.GetL();
1.233 + test(TheView.ColInt(1)!=KRecords);
1.234 + }
1.235 + TRAP(r,TheView.GotoL(mark));
1.236 + test(r!=KErrNone);
1.237 + test.End();
1.238 + TheView.Close();
1.239 + }
1.240 +
1.241 +//
1.242 +// do as much incremental evaluation as possible
1.243 +//
1.244 +LOCAL_C void Evaluate()
1.245 + {
1.246 + test(TheView.EvaluateAll()==KErrNone);
1.247 + }
1.248 +
1.249 +//
1.250 +// get to the true end of set
1.251 +//
1.252 +LOCAL_C void GotoEndL()
1.253 + {
1.254 + for (;;)
1.255 + {
1.256 + while (TheView.AtRow())
1.257 + TheView.NextL();
1.258 + if (!TheView.Unevaluated())
1.259 + break;
1.260 + Evaluate();
1.261 + }
1.262 + }
1.263 +
1.264 +LOCAL_C void CheckWindowL(TInt forward,TInt back)
1.265 + {
1.266 + TInt ii;
1.267 + for (ii=0;ii<forward;++ii)
1.268 + test(TheView.NextL());
1.269 + test(!TheView.NextL());
1.270 + for (ii=0;ii<forward+back+1;++ii)
1.271 + test(TheView.PreviousL());
1.272 + test(!TheView.PreviousL());
1.273 + for (ii=0;ii<back+1;++ii)
1.274 + test(TheView.NextL());
1.275 + }
1.276 +
1.277 +/**
1.278 +@SYMTestCaseID SYSLIB-DBMS-CT-0640
1.279 +@SYMTestCaseDesc Tests for RDbView
1.280 +@SYMTestPriority Medium
1.281 +@SYMTestActions Tests for a restricted sized window
1.282 +@SYMTestExpectedResults Test must not fail
1.283 +@SYMREQ REQ0000
1.284 +*/
1.285 +LOCAL_C void TestRestrictedL()
1.286 + {
1.287 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0640 Behaviour at start of set "));
1.288 + test(TheView.Prepare(TheDatabase,KOrderQuery,KSingleSlotWindow)==KErrNone);
1.289 + test(TheView.EvaluateAll()==KErrNone);
1.290 + test(TheView.CountL()==1);
1.291 + test(!TheView.Unevaluated());
1.292 + test(TheView.NextL());
1.293 + test(!TheView.Unevaluated());
1.294 + TheView.GetL();
1.295 + TInt id=TheView.ColInt(1);
1.296 + test(!TheView.NextL());
1.297 + test(TheView.Unevaluated());
1.298 + Evaluate();
1.299 + test(TheView.CountL()==1);
1.300 + test(TheView.AtRow());
1.301 + test(!TheView.PreviousL());
1.302 + test(TheView.Unevaluated());
1.303 + Evaluate();
1.304 + test(TheView.CountL()==1);
1.305 + test(TheView.AtRow());
1.306 + TheView.GetL();
1.307 + test(TheView.ColInt(1)==id);
1.308 + test(!TheView.PreviousL());
1.309 + test(TheView.Unevaluated());
1.310 + Evaluate();
1.311 + test(TheView.CountL()==1);
1.312 + test(TheView.AtBeginning());
1.313 + test(TheView.NextL());
1.314 + TheView.GetL();
1.315 + test(TheView.ColInt(1)==id);
1.316 + test(!TheView.Unevaluated());
1.317 + test.Next(_L("Behaviour at end of set"));
1.318 + GotoEndL();
1.319 + test(TheView.LastL());
1.320 + test(!TheView.Unevaluated());
1.321 + TheView.GetL();
1.322 + id=TheView.ColInt(1);
1.323 + test(!TheView.PreviousL());
1.324 + test(TheView.Unevaluated());
1.325 + Evaluate();
1.326 + test(TheView.CountL()==1);
1.327 + test(TheView.AtRow());
1.328 + test(!TheView.NextL());
1.329 + test(TheView.Unevaluated());
1.330 + Evaluate();
1.331 + test(TheView.CountL()==1);
1.332 + test(TheView.AtRow());
1.333 + TheView.GetL();
1.334 + test(TheView.ColInt(1)==id);
1.335 + test(!TheView.NextL());
1.336 + test(TheView.Unevaluated());
1.337 + Evaluate();
1.338 + test(TheView.CountL()==1);
1.339 + test(TheView.AtEnd());
1.340 + test(TheView.PreviousL());
1.341 + TheView.GetL();
1.342 + test(TheView.ColInt(1)==id);
1.343 + test(!TheView.Unevaluated());
1.344 + TheView.Close();
1.345 +//
1.346 + test.Next(_L("Forward and backwards slots"));
1.347 + test(TheView.Prepare(TheDatabase,KOrderQuery,KSmallWindow)==KErrNone);
1.348 + test(TheView.EvaluateAll()==KErrNone);
1.349 + test(TheView.CountL()==5);
1.350 + test(TheView.FirstL());
1.351 + CheckWindowL(4,0);
1.352 + test(TheView.NextL());
1.353 + test(TheView.NextL()); // now on preferred slot (id=2)
1.354 + test(!TheView.Unevaluated());
1.355 + test(TheView.NextL());
1.356 + test(TheView.Unevaluated());
1.357 + Evaluate();
1.358 + CheckWindowL(2,2);
1.359 + test(TheView.LastL()); // id=5
1.360 + Evaluate();
1.361 + test(TheView.LastL()); // id=7
1.362 + Evaluate(); // should now have last five rows
1.363 + CheckWindowL(2,2);
1.364 + test(TheView.LastL()); // id=9
1.365 + Evaluate(); // no more rows
1.366 + CheckWindowL(0,4); // all behind us now
1.367 + TheView.GetL();
1.368 + test(TheView.ColInt(1)==9);
1.369 + TheView.FirstL(); // id=5
1.370 + Evaluate();
1.371 + TheView.FirstL(); // id=3
1.372 + Evaluate();
1.373 + TheView.FirstL(); // id=1
1.374 + Evaluate();
1.375 + CheckWindowL(3,1);
1.376 +//
1.377 + test.Next(_L("Bookmarks"));
1.378 + TDbBookmark mark=TheView.Bookmark();
1.379 + test(TheView.NextL());
1.380 + test(TheView.NextL());
1.381 + test(TheView.NextL());
1.382 + Evaluate();
1.383 + TRAPD(r,TheView.GotoL(mark));
1.384 + test(r!=KErrNone);
1.385 + TheView.FirstL();
1.386 + Evaluate();
1.387 + TRAP(r,TheView.GotoL(mark));
1.388 + test(r==KErrNone);
1.389 + test.Next(_L("Delete"));
1.390 + test(TheView.FirstL());
1.391 + TheView.GetL();
1.392 + test(TheView.ColInt(1)==0);
1.393 + TheView.DeleteL();
1.394 + test(TheView.CountL()==4);
1.395 + TheView.FirstL();
1.396 + CheckWindowL(3,0);
1.397 + test.Next(_L("Insert"));
1.398 + TheView.InsertL();
1.399 + TheView.SetColL(1,TInt(0));
1.400 + TheView.PutL();
1.401 + test(TheView.CountL()==4);
1.402 + TheView.FirstL();
1.403 + TheView.GetL();
1.404 + test(TheView.ColInt(1)==1);
1.405 + TheView.Reset();
1.406 + test(TheView.EvaluateAll()==KErrNone);
1.407 + test(TheView.FirstL());
1.408 + TheView.GetL();
1.409 + test(TheView.ColInt(1)==0);
1.410 +//
1.411 + test.End();
1.412 + TheView.Close();
1.413 + }
1.414 +
1.415 +LOCAL_C void TestL()
1.416 + {
1.417 + __UHEAP_MARK;
1.418 + test.Start(_L("Setting up test table"));
1.419 + TRAPD(r,InitL();)
1.420 + test(r==KErrNone);
1.421 + test.Next(_L("Empty Window"));
1.422 + TRAP(r,TestEmptyL();)
1.423 + test(r==KErrNone);
1.424 + test.Next(_L("Unlimited Window"));
1.425 + TRAP(r,TestUnlimitedL();)
1.426 + test(r==KErrNone);
1.427 + test.Next(_L("Sized window"));
1.428 + TRAP(r,TestRestrictedL();)
1.429 + test(r==KErrNone);
1.430 + test.Next(_L("Cleanup"));
1.431 + TRAP(r,CloseDatabaseL();)
1.432 + test(r==KErrNone);
1.433 + test.End();
1.434 + __UHEAP_MARKEND;
1.435 + }
1.436 +
1.437 +//
1.438 +// Prepare the test directory.
1.439 +//
1.440 +LOCAL_C void setupTestDirectory()
1.441 + {
1.442 + TInt r=TheFs.Connect();
1.443 + test(r==KErrNone);
1.444 +//
1.445 + r=TheFs.MkDir(KTestDir);
1.446 + test(r==KErrNone || r==KErrAlreadyExists);
1.447 + r=TheFs.SetSessionPath(KTestDir);
1.448 + test(r==KErrNone);
1.449 + }
1.450 +
1.451 +//
1.452 +// Initialise the cleanup stack.
1.453 +//
1.454 +LOCAL_C void setupCleanup()
1.455 + {
1.456 + TheTrapCleanup=CTrapCleanup::New();
1.457 + test(TheTrapCleanup!=NULL);
1.458 + TRAPD(r,\
1.459 + {\
1.460 + for (TInt i=KTestCleanupStack;i>0;i--)\
1.461 + CleanupStack::PushL((TAny*)0);\
1.462 + CleanupStack::Pop(KTestCleanupStack);\
1.463 + });
1.464 + test(r==KErrNone);
1.465 + }
1.466 +
1.467 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
1.468 + {
1.469 + RFs fsSession;
1.470 + TInt err = fsSession.Connect();
1.471 + if(err == KErrNone)
1.472 + {
1.473 + TEntry entry;
1.474 + if(fsSession.Entry(aFullName, entry) == KErrNone)
1.475 + {
1.476 + RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
1.477 + err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
1.478 + if(err != KErrNone)
1.479 + {
1.480 + RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
1.481 + }
1.482 + err = fsSession.Delete(aFullName);
1.483 + if(err != KErrNone)
1.484 + {
1.485 + RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
1.486 + }
1.487 + }
1.488 + fsSession.Close();
1.489 + }
1.490 + else
1.491 + {
1.492 + RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
1.493 + }
1.494 + }
1.495 +
1.496 +//
1.497 +// Test streaming conversions.
1.498 +//
1.499 +GLDEF_C TInt E32Main()
1.500 + {
1.501 + test.Title();
1.502 + setupTestDirectory();
1.503 + setupCleanup();
1.504 +//
1.505 + test.Start(_L("TDbWindow - database test"));
1.506 + TRAPD(err, TestL());
1.507 + test(err == KErrNone);
1.508 +
1.509 + //deletion of data files must be done before call to end - DEF047652
1.510 + _LIT(KTestDbName, "C:\\DBMS-TST\\T_WINDOW.DB");
1.511 + ::DeleteDataFile(KTestDbName);
1.512 + test.End();
1.513 +//
1.514 +
1.515 + delete TheTrapCleanup;
1.516 + TheFs.Close();
1.517 + test.Close();
1.518 + return 0;
1.519 + }