telephonyserverplugins/simtsy/testconfigfileparser/test/t_testconfigfileparser.cpp
branchRCL_3
changeset 20 07a122eea281
parent 19 630d2f34d719
child 21 4814c5a49428
equal deleted inserted replaced
19:630d2f34d719 20:07a122eea281
     1 // Copyright (c) 2001-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 // Tests classes defined in testconfigfileparser.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file t_testconfigfileparser.cpp
       
    20  @since Hurricane
       
    21  @copyright Copyright (c) 2001 Symbian Ltd.  All rights reserved.
       
    22 */
       
    23 
       
    24 #include <autotest.h>
       
    25 #include <e32base.h>
       
    26 #include <e32test.h>		//< for RTest
       
    27 #include "testconfigfileparser.h"
       
    28 #include <f32file.h>
       
    29 
       
    30 
       
    31 _LIT(KTestScriptFile, "t_testconfigfileparser.txt");
       
    32 _LIT(KTestScriptComponent, "testscripts");
       
    33 _LIT(KTestScriptComponentFull, "\\testscripts\\");
       
    34 _LIT(KTestScriptOutput, "c:\\output.txt");
       
    35 
       
    36 CAutoTest* gAutoTest;
       
    37 RTest gTest(_L("Test Config File Test"));
       
    38 
       
    39 void TestGetElementL(TInt aTestCase, const TDesC8& aInput, TChar aDel, TInt aIndex, const TDesC8& aExpected, TBool aTrimOutput=ETrue)
       
    40 	{
       
    41 	TPtrC8 item(KNullDesC8);
       
    42 	const TInt err = CTestConfig::GetElement(aInput, aDel, aIndex, item, aTrimOutput);
       
    43 
       
    44 	const TInt KPrintfMaxLen = 200;
       
    45 	TBuf<KPrintfMaxLen> temp;
       
    46 
       
    47 	if (aInput.Length() <= KPrintfMaxLen)
       
    48 		{
       
    49 		temp.Copy(aInput);
       
    50 		gTest.Printf(_L("aInput=\"%S\" "), &temp);
       
    51 		}
       
    52 	else
       
    53 		gTest.Printf(_L("aInput=TOO LONG "));
       
    54 
       
    55 	gTest.Printf(_L("aIndex=%d "), aIndex);
       
    56 	gTest.Printf(_L("aTimeOutput=%d "), aTrimOutput);
       
    57 
       
    58 	if (aExpected.Length() <= KPrintfMaxLen)
       
    59 		{
       
    60 		temp.Copy(aExpected);
       
    61 		gTest.Printf(_L("aExpected=\"%S\" "), &temp);
       
    62 		}
       
    63 	else
       
    64 		gTest.Printf(_L("aExpected=TOO LONG "));
       
    65 
       
    66 	if (item.Length() <= KPrintfMaxLen)
       
    67 		{
       
    68 		temp.Copy(item);
       
    69 		gTest.Printf(_L("Get=\"%S\" "), &temp);
       
    70 		}
       
    71 	else
       
    72 		gTest.Printf(_L("Get=TOO LONG "));
       
    73 
       
    74 	gTest.Printf(_L("Err=%d\n"), err);
       
    75 
       
    76 	GLOBAL_CHECKPOINT_COMPARE(err, KErrNone, _L("Error getting element"));
       
    77 
       
    78 	temp.Format(_L("Element not expected [test=%d"), aTestCase);
       
    79 	GLOBAL_CHECKPOINT_COMPARE(!(item == aExpected), EFalse, temp);
       
    80 	}
       
    81 
       
    82 void TestGetElementL(const CTestConfig& aFile)
       
    83 	{
       
    84 	TInt sectionCount = aFile.Sections().Count();
       
    85 
       
    86 	while (sectionCount--)
       
    87 		{
       
    88 		const CTestConfigSection& section = aFile[sectionCount];
       
    89 
       
    90 		TInt itemCount = section.Items().Count();
       
    91 
       
    92 		while (itemCount--)
       
    93 			{
       
    94 			const CTestConfigItem& item = section[itemCount];
       
    95 			TPtrC8 output(KNullDesC8);
       
    96 			aFile.GetElement(item.Value(), ',', 0, output);
       
    97 			}
       
    98 		}
       
    99 	}
       
   100 
       
   101 void TestParseFileL()
       
   102 	{
       
   103 	RFs fs;
       
   104 	User::LeaveIfError(fs.Connect());
       
   105 	CleanupClosePushL(fs);
       
   106 
       
   107 	TFindFile findFile(fs);
       
   108 	_LIT(KWild, "*.txt");
       
   109 
       
   110 	CDir* dirs=NULL;
       
   111 	User::LeaveIfError(findFile.FindWildByDir(KWild, KTestScriptComponentFull, dirs));
       
   112 
       
   113 	TInt count = dirs->Count();
       
   114 
       
   115 	while (count--)
       
   116 		{
       
   117 		const TFileName fileName((*dirs)[count].iName);
       
   118 
       
   119 		CTestConfig* file = CTestConfig::NewLC(fs, KTestScriptComponent, fileName); 
       
   120 
       
   121 		file->WriteFileL(KTestScriptOutput);
       
   122 
       
   123 		CTestConfig* output = CTestConfig::NewLC(fs, KTestScriptComponent, KTestScriptOutput); 
       
   124 
       
   125 		TBool test = (*output == *file);
       
   126 
       
   127 		GLOBAL_CHECKPOINT_COMPARE(!test, EFalse, _L("Output != File"));
       
   128 
       
   129 		TestGetElementL(*file);
       
   130 
       
   131 		CleanupStack::PopAndDestroy(output);
       
   132 		CleanupStack::PopAndDestroy(file);
       
   133 		}
       
   134 
       
   135 	CleanupStack::PopAndDestroy(&fs);
       
   136 	}
       
   137 
       
   138 const TInt del = ';';
       
   139 _LIT8(KFirst, "First");
       
   140 _LIT8(KMiddle, "Middle");
       
   141 _LIT8(KLast, "Last");
       
   142 _LIT8(KElem1, "First;Middle;Last");
       
   143 _LIT8(KElem2, "    First;           ;Last");
       
   144 _LIT8(KElem3, ";   Middle       ;Last");
       
   145 _LIT8(KElem4, "First;Middle;     ");
       
   146 _LIT8(KElem5, ";;");
       
   147 
       
   148 void TestGetElementL()
       
   149 	{
       
   150 	TInt testCase = 0;
       
   151 
       
   152 	TestGetElementL(++testCase, KElem1, del, 0, KFirst);
       
   153 	TestGetElementL(++testCase, KElem1, del, 1, KMiddle);
       
   154 	TestGetElementL(++testCase, KElem1, del, 2, KLast);
       
   155 
       
   156 	TestGetElementL(++testCase, KElem2, del, 0, KFirst);
       
   157 	TestGetElementL(++testCase, KElem2, del, 1, KNullDesC8);
       
   158 	TestGetElementL(++testCase, KElem2, del, 2, KLast);
       
   159 
       
   160 	TestGetElementL(++testCase, KElem3, del, 0, KNullDesC8);
       
   161 	TestGetElementL(++testCase, KElem3, del, 1, KMiddle);
       
   162 	TestGetElementL(++testCase, KElem3, del, 2, KLast);
       
   163 
       
   164 	TestGetElementL(++testCase, KElem4, del, 0, KFirst);
       
   165 	TestGetElementL(++testCase, KElem4, del, 1, KMiddle);
       
   166 	TestGetElementL(++testCase, KElem4, del, 2, KNullDesC8);
       
   167 
       
   168 	TestGetElementL(++testCase, KElem5, del, 0, KNullDesC8);
       
   169 	TestGetElementL(++testCase, KElem5, del, 1, KNullDesC8);
       
   170 	TestGetElementL(++testCase, KElem5, del, 2, KNullDesC8);
       
   171 	}
       
   172 
       
   173 void PrepareBuf(TDes8& aOutput, TInt aLeadingSpaces, const TDesC8& aInput, TInt aTrailingSpaces)
       
   174 	{
       
   175 	const TInt KSpace = ' ';
       
   176 	aOutput.Zero();
       
   177 	aOutput.AppendFill(KSpace, aLeadingSpaces);
       
   178 	aOutput.Append(aInput);
       
   179 	aOutput.AppendFill(KSpace, aTrailingSpaces);
       
   180 	}
       
   181 
       
   182 void TestGetElementWithoutTrimL()
       
   183 	{
       
   184 	TInt testCase = 0;
       
   185 	TBuf8<32> temp;
       
   186 
       
   187 	TestGetElementL(++testCase, KElem1, del, 0, KFirst, EFalse);
       
   188 	TestGetElementL(++testCase, KElem1, del, 1, KMiddle, EFalse);
       
   189 	TestGetElementL(++testCase, KElem1, del, 2, KLast, EFalse);
       
   190 
       
   191 	PrepareBuf(temp, 4, KFirst, 0);
       
   192 	TestGetElementL(++testCase, KElem2, del, 0, temp, EFalse);
       
   193 	PrepareBuf(temp, 11, KNullDesC8, 0);
       
   194 	TestGetElementL(++testCase, KElem2, del, 1, temp, EFalse);
       
   195 	TestGetElementL(++testCase, KElem2, del, 2, KLast, EFalse);
       
   196 
       
   197 	TestGetElementL(++testCase, KElem3, del, 0, KNullDesC8, EFalse);
       
   198 	PrepareBuf(temp, 3, KMiddle, 7);
       
   199 	TestGetElementL(++testCase, KElem3, del, 1, temp, EFalse);
       
   200 	TestGetElementL(++testCase, KElem3, del, 2, KLast, EFalse);
       
   201 
       
   202 	TestGetElementL(++testCase, KElem4, del, 0, KFirst, EFalse);
       
   203 	TestGetElementL(++testCase, KElem4, del, 1, KMiddle, EFalse);
       
   204 	PrepareBuf(temp, 5, KNullDesC8, 0);
       
   205 	TestGetElementL(++testCase, KElem4, del, 2, temp, EFalse);
       
   206 
       
   207 	TestGetElementL(++testCase, KElem5, del, 0, KNullDesC8, EFalse);
       
   208 	TestGetElementL(++testCase, KElem5, del, 1, KNullDesC8, EFalse);
       
   209 	TestGetElementL(++testCase, KElem5, del, 2, KNullDesC8, EFalse);
       
   210 
       
   211 	TPtrC8 output;
       
   212 	const TInt err = CTestConfig::GetElement(KNullDesC8, 0, 0, output);
       
   213 	GLOBAL_CHECKPOINT(err != KErrNone);
       
   214 	}
       
   215 
       
   216 
       
   217 // Set up the array of test functions, which make the API calls
       
   218 LOCAL_C const TAutoTestCase Cases[] =
       
   219 	{
       
   220 	{TestParseFileL,_S("Parse File")},
       
   221 	{TestGetElementL,_S("Get Element")},
       
   222 	{TestGetElementWithoutTrimL,_S("Get Element Without Trim")}
       
   223 	};
       
   224 
       
   225 void AutoTestExecuteL()
       
   226 	{
       
   227 	_LIT(KDummyTests,"TestConfigFileParser_testreport.txt");
       
   228 	_LIT8(KComponentInfo,"TestConfigFileParser");
       
   229 	// Library defined macro
       
   230 	// Uses a global pointer instance of CAutoTest
       
   231 	GLOBAL_AUTOTEST_EXECUTE(Cases, KDummyTests, KComponentInfo, gTest);
       
   232 	}
       
   233 
       
   234 TInt E32Main()
       
   235 /**
       
   236  * Main
       
   237  */
       
   238 	{
       
   239 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   240 
       
   241 	__UHEAP_MARK;
       
   242 
       
   243 	gTest.SetLogged(ETrue);
       
   244 	gTest.Title();
       
   245 	gTest.Start(_L("Starting tests..."));
       
   246 
       
   247 	TRAPD(err, AutoTestExecuteL());
       
   248 
       
   249 	if (err!=KErrNone)
       
   250 		gTest.Printf(_L("ERROR: Leave %d\n"),err);
       
   251 
       
   252 	gTest.Printf(_L("Test Complete. Press any key ...\n"));
       
   253 	gTest.Getch();
       
   254 	gTest.End();
       
   255 	gTest.Close();
       
   256 
       
   257 	__UHEAP_MARKEND;
       
   258 
       
   259 	delete cleanup;
       
   260 	return KErrNone;
       
   261 	}