Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 lines
17 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. main.cpp
  5. Abstract:
  6. This file contains the unit test for the low-level JetBlue objects.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/18/2000
  9. created
  10. ******************************************************************************/
  11. #include "StdAfx.h"
  12. #define LOG__MPC_EXIT_IF_METHOD_FAILS(hr, cmd) \
  13. { \
  14. printf( "Executing %s\n", #cmd ); \
  15. if(FAILED(hr=cmd)) \
  16. { \
  17. printf( "Error: %08x\n", hr ); __MPC_TRACE_HRESULT(hr); __MPC_FUNC_LEAVE; \
  18. } \
  19. }
  20. class BindTest1 : public JetBlue::RecordBindingBase
  21. {
  22. JET_DECLARE_BINDING(BindTest1);
  23. public:
  24. long m_iVal1 ; bool m_fVal1;
  25. MPC::string m_szVal2; bool m_fVal2;
  26. };
  27. JET_BEGIN_RECORDBINDING(BindTest1)
  28. JET_FIELD_BYNAME("Col1",long ,m_iVal1 ,m_fVal1),
  29. JET_FIELD_BYNAME("Col2",string,m_szVal2,m_fVal2),
  30. JET_END_RECORDBINDING(BindTest1)
  31. ////////////////////////////////////////////////////////////////////////////////
  32. static const JET_COLUMNCREATE s_ColDef[] =
  33. {
  34. __MPC_JET_COLUMNCREATE ("Col1",JET_coltypLong ,0,JET_bitColumnNotNULL),
  35. __MPC_JET_COLUMNCREATE_ANSI ("Col2",JET_coltypLongText,0,0),
  36. __MPC_JET_COLUMNCREATE_UNICODE("Col3",JET_coltypLongText,0,0),
  37. };
  38. static const char s_szIndex1[] = "+Col1\0";
  39. static const char s_szIndex2[] = "+Col1\0-Col2\0";
  40. static const JET_INDEXCREATE s_IdxDef[] =
  41. {
  42. __MPC_JET_INDEXCREATE("Idx1",s_szIndex1,0,100),
  43. __MPC_JET_INDEXCREATE("Idx2",s_szIndex2,0,100)
  44. };
  45. static const JET_TABLECREATE s_TblDef = __MPC_JET_TABLECREATE(NULL,10,80,s_ColDef,s_IdxDef);
  46. ////////////////////////////////////////////////////////////////////////////////
  47. static HRESULT GetSession( /*[in/out]*/ JetBlue::SessionHandle& handle )
  48. {
  49. __HCP_FUNC_ENTRY( "GetSession" );
  50. HRESULT hr;
  51. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, JetBlue::SessionPool::s_GLOBAL->GetSession( handle ));
  52. hr = S_OK;
  53. __HCP_FUNC_CLEANUP;
  54. __HCP_FUNC_EXIT(hr);
  55. }
  56. static HRESULT CreateDatabase( /*[in]*/ JetBlue::SessionHandle& handle ,
  57. /*[out]*/ JetBlue::Database*& db ,
  58. /*[in]*/ LPCSTR szName )
  59. {
  60. __HCP_FUNC_ENTRY( "CreateDatabase" );
  61. HRESULT hr;
  62. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, handle->GetDatabase( szName, db, true ));
  63. hr = S_OK;
  64. __HCP_FUNC_CLEANUP;
  65. __HCP_FUNC_EXIT(hr);
  66. }
  67. static HRESULT CreateTable( /*[in]*/ JetBlue::Database*& db ,
  68. /*[out]*/ JetBlue::Table*& table ,
  69. /*[in]*/ LPCSTR szName )
  70. {
  71. __HCP_FUNC_ENTRY( "CreateTable" );
  72. HRESULT hr;
  73. JET_COLUMNCREATE colDef[ARRAYSIZE(s_ColDef)]; ::CopyMemory( colDef, s_ColDef, sizeof(colDef) );
  74. JET_INDEXCREATE idxDef[ARRAYSIZE(s_IdxDef)]; ::CopyMemory( idxDef, s_IdxDef, sizeof(idxDef) );
  75. JET_TABLECREATE tblDef; ::CopyMemory( &tblDef, &s_TblDef, sizeof(tblDef) );
  76. tblDef.szTableName = (LPSTR)szName;
  77. tblDef.rgcolumncreate = colDef;
  78. tblDef.rgindexcreate = idxDef;
  79. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, db->GetTable( NULL, table, &tblDef ));
  80. hr = S_OK;
  81. __HCP_FUNC_CLEANUP;
  82. __HCP_FUNC_EXIT(hr);
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. static HRESULT RestoreTableFromFile( /*[in]*/ LPCSTR szFile ,
  86. /*[in]*/ JetBlue::Table* table )
  87. {
  88. __HCP_FUNC_ENTRY( "RestoreTableFromFile" );
  89. HRESULT hr;
  90. std::ifstream ifile( szFile, ios::nocreate | ios::in );
  91. char rgBuf [2048+1];
  92. WCHAR rgBufUNICODE[2048+1];
  93. LPCWSTR* argv = NULL;
  94. int argc = 0;
  95. int i;
  96. if(ifile.fail())
  97. {
  98. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ERROR_FILE_NOT_FOUND);
  99. }
  100. while(!ifile.eof())
  101. {
  102. MPC::CommandLine_Free( argc, argv );
  103. ifile.getline( rgBuf, sizeof(rgBuf) );
  104. ::MultiByteToWideChar( CP_ACP, 0, rgBuf, -1, rgBufUNICODE, MAXSTRLEN(rgBufUNICODE) );
  105. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, MPC::CommandLine_Parse( argc, argv, rgBufUNICODE, true ));
  106. if(argc == 0) continue;
  107. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->PrepareInsert());
  108. for(i=0; i<argc; i++)
  109. {
  110. if(wcscmp( argv[i], L"<NULL>"))
  111. {
  112. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( i ).Put( argv[i] ));
  113. }
  114. }
  115. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->UpdateRecord( /*fMove*/false ));
  116. }
  117. hr = S_OK;
  118. __HCP_FUNC_CLEANUP;
  119. MPC::CommandLine_Free( argc, argv );
  120. __HCP_FUNC_EXIT(hr);
  121. }
  122. static HRESULT SaveTableToFile( /*[in]*/ LPCSTR szFile ,
  123. /*[in]*/ JetBlue::Table* table ,
  124. /*[in]*/ bool fMoveToFirst = true )
  125. {
  126. __HCP_FUNC_ENTRY( "SaveTableToFile" );
  127. HRESULT hr;
  128. JET_ERR err;
  129. std::ofstream ofile( szFile );
  130. WCHAR rgBufEscaped[ 2048+1];
  131. char rgBufANSI [2*2048+1];
  132. int iCols = table->NumOfColumns();
  133. CComVariant* rgArg = NULL;
  134. int i;
  135. if(ofile.fail())
  136. {
  137. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ERROR_FILE_NOT_FOUND);
  138. }
  139. __MPC_EXIT_IF_ALLOC_FAILS(hr, rgArg, new CComVariant[iCols]);
  140. hr = fMoveToFirst ? table->Move( 0, JET_MoveFirst ) : S_OK;
  141. while(SUCCEEDED(hr))
  142. {
  143. for(i=0; i<iCols; i++) rgArg[i].Clear();
  144. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Get( iCols, rgArg ));
  145. for(i=0; i<iCols; i++)
  146. {
  147. CComVariant& v = rgArg[i];
  148. LPCWSTR szSrc;
  149. LPWSTR szDst;
  150. bool fQuotes = false;
  151. rgBufEscaped[0] = 0;
  152. if(SUCCEEDED(v.ChangeType( VT_BSTR )))
  153. {
  154. LPCWSTR szSrc = SAFEBSTR( v.bstrVal );
  155. LPWSTR szDst = rgBufEscaped;
  156. WCHAR c;
  157. if(szSrc[0] == 0) fQuotes = true;
  158. while((c = *szSrc++))
  159. {
  160. switch(c)
  161. {
  162. case '\'':
  163. case '"' :
  164. case '\\': *szDst++ = '\\';
  165. case ' ' : fQuotes = true;
  166. }
  167. *szDst++ = c;
  168. }
  169. *szDst = 0;
  170. }
  171. else if(v.vt == VT_NULL)
  172. {
  173. wcscpy( rgBufEscaped, L"<NULL>" );
  174. }
  175. ::WideCharToMultiByte( CP_ACP, 0, rgBufEscaped, -1, rgBufANSI, MAXSTRLEN(rgBufANSI), NULL, NULL );
  176. if(i ) ofile << " ";
  177. if(fQuotes) ofile << "\"";
  178. ofile << rgBufANSI;
  179. if(fQuotes) ofile << "\"";
  180. }
  181. std::endl( ofile );
  182. hr = table->Move( 0, JET_MoveNext );
  183. }
  184. hr = S_OK;
  185. __HCP_FUNC_CLEANUP;
  186. __HCP_FUNC_EXIT(hr);
  187. }
  188. ////////////////////////////////////////////////////////////////////////////////
  189. static HRESULT WriteData( /*[in]*/ JetBlue::Table* table )
  190. {
  191. __HCP_FUNC_ENTRY( "WriteData" );
  192. HRESULT hr;
  193. //
  194. // Individual put.
  195. //
  196. {
  197. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->PrepareInsert());
  198. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( 0 ).Put( MPC::wstring( L"124" ) ));
  199. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( 1 ).Put( long ( 678 ) ));
  200. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( 2 ).Put( CComVariant ( "ads" ) ));
  201. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->UpdateRecord( /*fMove*/false ));
  202. }
  203. //
  204. // Batch put.
  205. //
  206. {
  207. CComVariant rgArg[] = { "124", 678, "ads" };
  208. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->PrepareInsert());
  209. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Put( ARRAYSIZE(rgArg), rgArg ));
  210. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->UpdateRecord( /*fMove*/false ));
  211. }
  212. //
  213. // Binding mode.
  214. //
  215. {
  216. BindTest1 rb( table );
  217. rb.m_fVal1 = false; rb.m_iVal1 = 30;
  218. rb.m_fVal2 = true;
  219. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, rb.Insert());
  220. rb.m_fVal1 = false; rb.m_iVal1 = 30;
  221. rb.m_fVal2 = false; rb.m_szVal2 = "Prova di stringa";
  222. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, rb.Insert());
  223. }
  224. hr = S_OK;
  225. __HCP_FUNC_CLEANUP;
  226. __HCP_FUNC_EXIT(hr);
  227. }
  228. static HRESULT ReadData( /*[in]*/ JetBlue::Table* table )
  229. {
  230. __HCP_FUNC_ENTRY( "ReadData" );
  231. HRESULT hr;
  232. //
  233. // Individual get.
  234. //
  235. {
  236. long lCol1;
  237. MPC::wstring szCol2;
  238. MPC::wstring szCol3;
  239. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Move( 0, JET_MoveFirst ));
  240. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( 0 ).Get( lCol1 ));
  241. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( 1 ).Get( szCol2 ));
  242. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->GetCol( "Col2" ).Get( szCol3 ));
  243. }
  244. //
  245. // Batch get.
  246. //
  247. {
  248. CComVariant rgArg[3];
  249. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Move( 0, JET_MoveNext ));
  250. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Get( ARRAYSIZE(rgArg), rgArg ));
  251. }
  252. //
  253. // Binding mode.
  254. //
  255. {
  256. BindTest1 rb( table );
  257. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, rb.Move( 0, JET_MoveFirst ));
  258. }
  259. hr = S_OK;
  260. __HCP_FUNC_CLEANUP;
  261. __HCP_FUNC_EXIT(hr);
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////
  264. static HRESULT DeleteTable( /*[in]*/ JetBlue::Table* table )
  265. {
  266. __HCP_FUNC_ENTRY( "DeleteTable" );
  267. HRESULT hr;
  268. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table->Delete( true ));
  269. hr = S_OK;
  270. __HCP_FUNC_CLEANUP;
  271. __HCP_FUNC_EXIT(hr);
  272. }
  273. static HRESULT DeleteDatabase( /*[in]*/ JetBlue::Database* db )
  274. {
  275. __HCP_FUNC_ENTRY( "DeleteDatabase" );
  276. HRESULT hr;
  277. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, db->Delete( true ));
  278. hr = S_OK;
  279. __HCP_FUNC_CLEANUP;
  280. __HCP_FUNC_EXIT(hr);
  281. }
  282. ////////////////////////////////////////////////////////////////////////////////
  283. static HRESULT RunTests( int argc, WCHAR **argv )
  284. {
  285. __HCP_FUNC_ENTRY( "RunTests" );
  286. HRESULT hr;
  287. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, JetBlue::SessionPool::s_GLOBAL->Init());
  288. {
  289. JetBlue::SessionHandle handle;
  290. JetBlue::Database* db;
  291. JetBlue::Table* table;
  292. JetBlue::Table* table2;
  293. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, GetSession( handle ));
  294. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, CreateDatabase( handle, db, "files\\test.edb" ));
  295. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, handle->BeginTransaction());
  296. ////////////////////////////////////////////////////////////////////////////////
  297. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, CreateTable( db, table, "table1" ));
  298. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, WriteData( table ));
  299. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, ReadData ( table ));
  300. {
  301. JetBlue::TableDefinition tbldef;
  302. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, tbldef.Parse( *table ));
  303. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, tbldef.Save( L"files\\table1.xml" ));
  304. }
  305. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, SaveTableToFile( "files\\runtime.txt", table ));
  306. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, DeleteTable( table ));
  307. ////////////////////////////////////////
  308. {
  309. JetBlue::TableDefinition tbldef;
  310. JET_TABLECREATE tblcrt;
  311. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, tbldef.Load( L"files\\table1.xml" ));
  312. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, tbldef.Generate( tblcrt ));
  313. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, db->GetTable( NULL, table2, &tblcrt ));
  314. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, tbldef.Release( tblcrt ));
  315. }
  316. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, RestoreTableFromFile( "files\\input.txt" , table2 ));
  317. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, SaveTableToFile ( "files\\output.txt", table2 ));
  318. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->SelectIndex( "Idx1" ));
  319. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->Seek ( JET_bitSeekEQ | JET_bitSetIndexRange, 12L ));
  320. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, SaveTableToFile ( "files\\filter.txt", table2, false ));
  321. {
  322. CComVariant rgKeys[2] = { 12, "asd" };
  323. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->SelectIndex( "Idx2" ));
  324. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->Seek ( JET_bitSeekEQ | JET_bitSetIndexRange, rgKeys, 2 ));
  325. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, SaveTableToFile ( "files\\filter2.txt", table2, false ));
  326. }
  327. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->SelectIndex ( "Idx1" ));
  328. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->Seek ( JET_bitSeekEQ | JET_bitSetIndexRange, 12L ));
  329. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->DeleteRecord( ));
  330. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, table2->Seek ( JET_bitSeekEQ | JET_bitSetIndexRange, 12L ));
  331. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, SaveTableToFile ( "files\\filter3.txt", table2, false ));
  332. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, DeleteTable( table2 ));
  333. ////////////////////////////////////////////////////////////////////////////////
  334. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, handle->CommitTransaction());
  335. LOG__MPC_EXIT_IF_METHOD_FAILS(hr, DeleteDatabase( db ));
  336. }
  337. hr = S_OK;
  338. __HCP_FUNC_CLEANUP;
  339. (void)JetBlue::SessionPool::s_GLOBAL->Close( true );
  340. __HCP_FUNC_EXIT(hr);
  341. }
  342. ////////////////////////////////////////////////////////////////////////////////
  343. static HRESULT InitAll()
  344. {
  345. __HCP_FUNC_ENTRY( "InitAll" );
  346. HRESULT hr;
  347. __MPC_EXIT_IF_METHOD_FAILS(hr, JetBlue::SessionPool ::InitializeSystem( ));
  348. __MPC_EXIT_IF_METHOD_FAILS(hr, Taxonomy::InstalledInstanceStore::InitializeSystem( ));
  349. __MPC_EXIT_IF_METHOD_FAILS(hr, Taxonomy::Cache ::InitializeSystem( ));
  350. __MPC_EXIT_IF_METHOD_FAILS(hr, OfflineCache::Root ::InitializeSystem( /*fMaster*/true ));
  351. __MPC_EXIT_IF_METHOD_FAILS(hr, HCUpdate::Config::Root ::InitializeSystem( ));
  352. __MPC_EXIT_IF_METHOD_FAILS(hr, CPCHContentStore ::InitializeSystem( /*fMaster*/true ));
  353. __MPC_EXIT_IF_METHOD_FAILS(hr, CPCHUserProcess ::InitializeSystem( ));
  354. __MPC_EXIT_IF_METHOD_FAILS(hr, CPCHSecurity ::InitializeSystem( ));
  355. __MPC_EXIT_IF_METHOD_FAILS(hr, CPCHSystemMonitor ::InitializeSystem( ));
  356. __MPC_EXIT_IF_METHOD_FAILS(hr, CSAFReg ::InitializeSystem( ));
  357. hr = S_OK;
  358. __HCP_FUNC_CLEANUP;
  359. __HCP_FUNC_EXIT(hr);
  360. }
  361. static HRESULT CleanAll()
  362. {
  363. CSAFReg ::FinalizeSystem();
  364. CPCHSystemMonitor ::FinalizeSystem();
  365. CPCHSecurity ::FinalizeSystem();
  366. CPCHUserProcess ::FinalizeSystem();
  367. CPCHContentStore ::FinalizeSystem();
  368. HCUpdate::Config::Root ::FinalizeSystem();
  369. OfflineCache::Root ::FinalizeSystem();
  370. Taxonomy::Cache ::FinalizeSystem();
  371. Taxonomy::InstalledInstanceStore::FinalizeSystem();
  372. JetBlue::SessionPool ::FinalizeSystem();
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////
  375. int __cdecl wmain( int argc, WCHAR **argv, WCHAR **envp)
  376. {
  377. HRESULT hr;
  378. if(SUCCEEDED(hr = ::CoInitializeEx( NULL, COINIT_MULTITHREADED )) &&
  379. SUCCEEDED(hr = InitAll ( )) )
  380. {
  381. hr = RunTests( argc, argv );
  382. CleanAll();
  383. ::CoUninitialize();
  384. }
  385. return FAILED(hr) ? 10 : 0;
  386. }