persistentstorage/dbms/tdbms/t_dbpanic.cpp
changeset 0 08ec8eefde2f
child 55 44f437012c90
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2003-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 #include <e32math.h>
       
    20 #include <s32mem.h>
       
    21 
       
    22 LOCAL_D RTest test(_L("t_dbpanic - Panic test when cancelling two blobs transactions"));
       
    23 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    24 LOCAL_D RDbTable TheTables[2];
       
    25 LOCAL_D RFs TheFs;
       
    26 LOCAL_D RDbs TheDbs;
       
    27 LOCAL_D RDbNamedDatabase TheDatabase;
       
    28 
       
    29 const TPtrC KTestDatabase=_L("\\DBMS-TST\\T_PANIC.DB");
       
    30 
       
    31 /**
       
    32 @SYMTestCaseID          SYSLIB-DBMS-CT-0641
       
    33 @SYMTestCaseDesc        Tests for creating the database and tables
       
    34 @SYMTestPriority        Medium
       
    35 @SYMTestActions         Tests for creating the tables.Leave on error.
       
    36 @SYMTestExpectedResults Test must not fail
       
    37 @SYMREQ                 REQ0000
       
    38 */
       
    39 LOCAL_C void PreTestL()
       
    40 	{
       
    41 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0641 "));
       
    42 	// Create the database:
       
    43 	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
       
    44 	CleanupClosePushL(TheDatabase);
       
    45 
       
    46 	// Create tables in the database:
       
    47 	User::LeaveIfError(TheDatabase.Execute(_L("create table ta (a integer, b Long Varbinary)")));
       
    48 	User::LeaveIfError(TheDatabase.Execute(_L("create table tb (a integer, b Long Varbinary)")));
       
    49 
       
    50 	// Open the tables:
       
    51 	User::LeaveIfError(TheTables[0].Open(TheDatabase, _L("ta")));
       
    52 	CleanupClosePushL(TheTables[0]);
       
    53 	User::LeaveIfError(TheTables[1].Open(TheDatabase, _L("tb")));
       
    54 	CleanupClosePushL(TheTables[1]);
       
    55 	}
       
    56 
       
    57 /**
       
    58 @SYMTestCaseID          SYSLIB-DBMS-CT-0642
       
    59 @SYMTestCaseDesc        Tests for transaction of large data
       
    60 @SYMTestPriority        Medium
       
    61 @SYMTestActions         Tests for streaming of blob data
       
    62 @SYMTestExpectedResults Test must not fail
       
    63 @SYMREQ                 REQ0000
       
    64 */
       
    65 LOCAL_C void TestL()
       
    66 	{
       
    67 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0642 "));
       
    68 	// Start a transaction:
       
    69 	TheDatabase.Begin();
       
    70 
       
    71 	// Create a new row on each table:
       
    72 	TheTables[0].InsertL();
       
    73 	TheTables[1].InsertL();
       
    74 
       
    75 	for(TInt i = 0; i < 2; ++i)
       
    76 		{
       
    77 		// Setting to null sets the dirty flag:
       
    78 		TheTables[i].SetColNullL(1);
       
    79 
       
    80 		// Create a blob of data:
       
    81 		_LIT8(blobdata, "abcdefghijklmnopqrstuvwxyz");
       
    82 		CBufFlat * blobbuff = CBufFlat::NewL(32);
       
    83 		CleanupStack::PushL(blobbuff);
       
    84 		blobbuff->InsertL(0, blobdata());
       
    85 
       
    86 		// Open a read stream on the blob:
       
    87 		RBufReadStream blobstream;
       
    88 		blobstream.Open(*blobbuff, 0);
       
    89 		CleanupClosePushL(blobstream);
       
    90 
       
    91 		// Open a write stream on the table:
       
    92 		RDbColWriteStream blobwrite;
       
    93 		blobwrite.OpenLC(TheTables[i], 2);
       
    94 
       
    95 		// Stream data from the read stream to the write stream:
       
    96 		blobwrite.WriteL(blobstream);
       
    97 		blobwrite.CommitL();
       
    98 
       
    99 		// Close the write stream:
       
   100 		CleanupStack::PopAndDestroy();
       
   101 		// Close the read stream:
       
   102 		CleanupStack::PopAndDestroy();
       
   103 		// Delete the blob of data:
       
   104 		CleanupStack::PopAndDestroy(blobbuff);
       
   105 		}
       
   106 
       
   107 	TheTables[0].Cancel();
       
   108 	TheTables[1].Cancel(); //This call to cancel panics.
       
   109 
       
   110 
       
   111 	// End the transaction:
       
   112 	TheDatabase.Commit();
       
   113 	}
       
   114 
       
   115 /**
       
   116 @SYMTestCaseID          SYSLIB-DBMS-CT-0643
       
   117 @SYMTestCaseDesc        Tests for closing of tables
       
   118 @SYMTestPriority        Medium
       
   119 @SYMTestActions         Tests for closing of tables and database
       
   120 @SYMTestExpectedResults Test must not fail
       
   121 @SYMREQ                 REQ0000
       
   122 */
       
   123 LOCAL_C void PostTestL()
       
   124 	{
       
   125 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0643 "));
       
   126 	// Close the tables:
       
   127 	TheTables[0].Close();
       
   128 	CleanupStack::Pop(); // Table close
       
   129 	TheTables[1].Close();
       
   130 	CleanupStack::Pop(); // Table close
       
   131 
       
   132 	// Close the database:
       
   133 	CleanupStack::PopAndDestroy();
       
   134 	}
       
   135 
       
   136 void doTestL()
       
   137 	{
       
   138 	test.Start(_L("bang"));
       
   139 
       
   140 	// Open a connection to the DBMS server:
       
   141 	User::LeaveIfError(TheDbs.Connect());
       
   142 	CleanupClosePushL(TheDbs);
       
   143 
       
   144 	PreTestL();
       
   145 	TestL();
       
   146 	PostTestL();
       
   147 
       
   148 	CleanupStack::PopAndDestroy(); // TheDbs close
       
   149 	}
       
   150 
       
   151 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
       
   152 	{
       
   153 	RFs fsSession;
       
   154 	TInt err = fsSession.Connect();
       
   155 	if(err == KErrNone)
       
   156 		{
       
   157 		TEntry entry;
       
   158 		if(fsSession.Entry(aFullName, entry) == KErrNone)
       
   159 			{
       
   160 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
       
   161 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
       
   162 			if(err != KErrNone)
       
   163 				{
       
   164 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
       
   165 				}
       
   166 			err = fsSession.Delete(aFullName);
       
   167 			if(err != KErrNone)
       
   168 				{
       
   169 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
       
   170 				}
       
   171 			}
       
   172 		fsSession.Close();
       
   173 		}
       
   174 	else
       
   175 		{
       
   176 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
       
   177 		}
       
   178 	}
       
   179 
       
   180 //
       
   181 // Test streaming conversions.
       
   182 //
       
   183 GLDEF_C TInt E32Main()
       
   184     {
       
   185 	test.Title();
       
   186 	TheTrapCleanup=CTrapCleanup::New();
       
   187 
       
   188 	TInt r=TheFs.Connect();
       
   189 	test(r==KErrNone);
       
   190 	r=TheFs.MkDir(KTestDatabase);
       
   191 	test(r==KErrNone || r==KErrAlreadyExists);
       
   192 
       
   193 	TRAP(r, doTestL());
       
   194 	test(r == KErrNone);
       
   195 
       
   196 	::DeleteDataFile(KTestDatabase);
       
   197 
       
   198 	test.End();
       
   199 
       
   200 	TheFs.Close();
       
   201 	delete TheTrapCleanup;
       
   202 	test.Close();
       
   203 	return r;
       
   204     }