Update contrib.
1 // Copyright (c) 2004-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
23 #undef __UHEAP_MARKEND
25 #define __UHEAP_MARKEND
27 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
29 #ifndef __linux__ //No CRC test on LINUX
31 const TPtrC KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_LONGCOL.CRC");
33 const TPtrC KCrcRecord=_L("C:\\dbms-tst\\T_LONGCOL.CRC");
37 static RTest TheTest(_L("T_LONGCOL"));
38 static CTrapCleanup* TheTrapCleanup;
39 const TInt KTestCleanupStack=0x40;
43 const TPtrC KTestDir=_L(".\\dbms-tst\\");
45 const TPtrC KTestDir=_L("C:\\dbms-tst\\");
49 const TPtrC KSearchTestDbPath = _L("c:\\dbms-tst\\eposlmserachtest.ldb");
51 const TPtrC KSearchTestDbPath = _L(".\\eposlmserachtest.ldb");
54 //Delete "aFullName" file.
55 static void DeleteDataFile(const TDesC& aFullName)
58 TInt err = fsSession.Connect();
62 if(fsSession.Entry(aFullName, entry) == KErrNone)
64 RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
65 err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
68 RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
70 err = fsSession.Delete(aFullName);
73 RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
80 RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
84 ///////////////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////////////
86 //Tests macros and functions.
87 //If (!aValue) then the test will be panicked, the test data files will be deleted.
88 static void Check(TInt aValue, TInt aLine)
92 ::DeleteDataFile(KSearchTestDbPath);
93 TheTest(EFalse, aLine);
96 //If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
97 static void Check(TInt aValue, TInt aExpected, TInt aLine)
99 if(aValue != aExpected)
101 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
102 ::DeleteDataFile(KSearchTestDbPath);
103 TheTest(EFalse, aLine);
106 //Use these to test conditions.
107 #define TEST(arg) ::Check((arg), __LINE__)
108 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
110 ///////////////////////////////////////////////////////////////////////////////////////
112 static void Leave(TInt aLine, TInt aError)
114 RDebug::Print(_L("*** Leave. Error: %d, line: %d\r\n"), aError, aLine);
117 static void LeaveIfError(TInt aLine, TInt aError)
119 if(aError < KErrNone)
121 ::Leave(aLine, aError);
124 #define __LEAVE(err) ::Leave(__LINE__, err)
125 #define __LEAVE_IF_ERROR(err) ::LeaveIfError(__LINE__, err)
127 ///////////////////////////////////////////////////////////////////////////////////////
128 ///////////////////////////////////////////////////////////////////////////////////////
131 // Prepare the test directory.
133 static void SetupTestDirectory()
135 TInt r=TheFsSession.MkDir(KTestDir);
136 TEST(r==KErrNone || r==KErrAlreadyExists);
137 r=TheFsSession.SetSessionPath(KTestDir);
139 // On TOOLS2 - RFs::SetSessionPath() will affect all RFs Sessions,
140 // the two RFs need same session path anyway
142 r=TheCrcChecker.SetSessionPath(KTestDir);
147 static TInt SearchFor( const TPtrC& aSearchString, RDbNamedDatabase& aDb )
149 _LIT( KSQLSearchStatement, "select Notes from CDs where Notes like '%S'" );
151 query.Format( KSQLSearchStatement, &aSearchString );
154 _LIT( KQueryFormatter, "\r\n %S\r\n" );
156 msg.Format( KQueryFormatter, &query );
157 TheTest.Printf( msg );
159 // create a view on the database
161 // use EDbCompareCollated in order to search case-insensitive
162 __LEAVE_IF_ERROR( view.Prepare( aDb, TDbQuery( query, EDbCompareCollated ) ) );
163 __LEAVE_IF_ERROR( view.EvaluateAll() );
165 // iterate across the row set
166 TInt noOfMatches = 0;
167 for ( view.FirstL(); view.AtRow(); view.NextL() )
174 // Display no of matches
175 _LIT( KNoOfMatchFormatter, " Found %d matches\r\n" );
176 msg.Format( KNoOfMatchFormatter, noOfMatches );
177 TheTest.Printf( msg );
185 @SYMTestCaseID SYSLIB-DBMS-CT-0645
186 @SYMTestCaseDesc Searching for data from a database
187 @SYMTestPriority Medium
188 @SYMTestActions Tests for EDbColText,EDbColLongText column type
189 @SYMTestExpectedResults Test must not fail
192 static void TestSearchL( TInt aIndex )
195 _LIT( KComposer1, "Elgar" );
196 _LIT( KCol1, "Artist" );
197 _LIT( KCol2, "Notes" );
198 _LIT( KTable, "CDs" );
200 TInt err = TheFsSession.Delete( KSearchTestDbPath );
201 if ( ( err != KErrNone ) && ( err != KErrNotFound ) && ( err != KErrPathNotFound ) )
207 CleanupClosePushL( db );
208 __LEAVE_IF_ERROR( db.Create( TheFsSession, KSearchTestDbPath ) );
211 // Create the database table
213 // Create a table definition
214 CDbColSet* columns = CDbColSet::NewLC();
216 columns->AddL( TDbCol( KCol1, EDbColText ) );
218 columns->AddL( TDbCol( KCol2, EDbColText ) );
220 columns->AddL( TDbCol( KCol2, EDbColLongText ) );
221 // if the column is of type "EDbColText" instead,
222 // all searching is working properly
224 __LEAVE_IF_ERROR( db.CreateTable( KTable, *columns ) );
225 // cleanup the column set
226 CleanupStack::PopAndDestroy( columns );
231 // create a view on the database
232 _LIT( KSQLStatement, "select Artist,Notes from CDs order by Artist" );
234 __LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
235 __LEAVE_IF_ERROR( view.EvaluateAll() );
237 // Get the structure of rowset
238 CDbColSet* colSet = view.ColSetL();
241 view.SetColL( colSet->ColNo( KCol1 ), KComposer1 ); // Artist
243 // RDbColWriteStream out;
244 // TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
246 // out.OpenLC( view, col );
247 // out.WriteL( _L( "Some additional comment here." ) );
250 // CleanupStack::PopAndDestroy(); // out
251 view.SetColL( colSet->ColNo( KCol2 ), _L( "Some additional comment here." ) );
260 _LIT( KRowFormatter, "\r\n Artist: %S \r\n Notes: %S \r\n" );
261 __LEAVE_IF_ERROR( view.Prepare( db, TDbQuery( KSQLStatement, EDbCompareNormal ) ) );
262 __LEAVE_IF_ERROR( view.EvaluateAll() );
264 // Get the structure of the rowset
265 colSet = view.ColSetL();
266 // iterate across the row set
267 for ( view.FirstL(); view.AtRow(); view.NextL() )
271 // while the rowset is on this row, can use a TPtrC to
272 // refer to any text columns
273 TPtrC artist = view.ColDes( colSet->ColNo( KCol1 ) );
274 // and a stream for long columns
276 TDbColNo col = colSet->ColNo( KCol2 ); // Ordinal position of long column
277 TBuf<256> notes; // Buffer for out notes
278 in.OpenLC( view, col );
279 in.ReadL( notes, view.ColLength( col ) );
281 CleanupStack::PopAndDestroy(); // in
282 // Display the artist and notes
284 msg.Format( KRowFormatter, &artist, ¬es );
285 TheTest.Printf( msg );
292 // Search for the data
295 result = SearchFor( _L( "Some*" ), db ); // matches
297 result = SearchFor( _L( "some*" ), db ); // defect causes no match, should match
299 result = SearchFor( _L( "*some*" ), db ); // matches
301 result = SearchFor( _L( "s?me*" ), db ); // matches
303 result = SearchFor( _L( "Some additional comment here." ), db ); // matches
305 result = SearchFor( _L( "some additional comment here." ), db ); // defect causes no match, should match
308 CleanupStack::PopAndDestroy( &db );
309 TheCrcChecker.GenerateCrcL(KSearchTestDbPath);
313 // Initialise the cleanup stack.
315 static void setupCleanup()
317 TheTrapCleanup=CTrapCleanup::New();
318 TEST(TheTrapCleanup!=NULL);
321 for (TInt i=KTestCleanupStack;i>0;i--)\
322 CleanupStack::PushL((TAny*)0);\
323 CleanupStack::Pop(KTestCleanupStack);\
329 // Test streaming conversions.
331 GLDEF_C TInt E32Main()
337 TInt err = TheFsSession.Connect();
338 TEST2(err, KErrNone);
339 ::SetupTestDirectory();
341 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColText "));
342 TRAP(err, TestSearchL( 0 ));
344 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0645 Testing DBMS - EDbColLongText "));
345 TRAP(err, TestSearchL( 1 ));
347 ::DeleteDataFile(KSearchTestDbPath);
351 TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
352 TheTest(err==KErrNone);
353 TheTest(lc==KErrNone);
355 TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
357 TheCrcChecker.ErrorReportL(err, errmsg);
358 RDebug::Print(errmsg);
359 TheTest(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
365 TheFsSession.Close();
368 delete TheTrapCleanup;