DataReaderBaseTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //
  2. // DataProviderBaseTest.cs : A base class that provides the common
  3. // functionality of :
  4. // 1) Reading a config file containing the
  5. // database connection parameters, different
  6. // tables and their description, Values that
  7. // the tables are populated with.
  8. // 2) Retrieves data from these tables;
  9. // 3) Compares the retrieved values against the ones
  10. // contained in the config file.
  11. //
  12. // A class specific to each database (and ODBC) are derived from this class.
  13. // These classes contain code specific to different databases (like establishing
  14. // a connection, comparing date values, etc).
  15. //
  16. // Author:
  17. // Satya Sudha K ([email protected])
  18. //
  19. //
  20. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  21. //
  22. // Permission is hereby granted, free of charge, to any person obtaining
  23. // a copy of this software and associated documentation files (the
  24. // "Software"), to deal in the Software without restriction, including
  25. // without limitation the rights to use, copy, modify, merge, publish,
  26. // distribute, sublicense, and/or sell copies of the Software, and to
  27. // permit persons to whom the Software is furnished to do so, subject to
  28. // the following conditions:
  29. //
  30. // The above copyright notice and this permission notice shall be
  31. // included in all copies or substantial portions of the Software.
  32. //
  33. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  34. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  35. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  36. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  37. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  38. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  39. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  40. //
  41. using System;
  42. using System.Collections;
  43. using System.IO;
  44. using System.Xml;
  45. using System.Xml.XPath;
  46. using System.Data;
  47. using System.Configuration;
  48. using System.Text.RegularExpressions;
  49. namespace MonoTests.System.Data {
  50. public class BaseRetrieve {
  51. public IDbConnection con;
  52. public IDbCommand cmd;
  53. public IDataReader rdr;
  54. protected XmlNode configDoc;
  55. public BaseRetrieve (string database)
  56. {
  57. con = null;
  58. cmd = null;
  59. rdr = null;
  60. configDoc = (XmlNode) ConfigurationSettings.GetConfig (database);
  61. }
  62. void CreateCommand ()
  63. {
  64. if (con == null)
  65. return;
  66. cmd = con.CreateCommand ();
  67. }
  68. // Method that actually runs the entire test : Connects to a database,
  69. // retrieves values from different tables, and compares them against
  70. // the values that we had entered
  71. public void RunTest ()
  72. {
  73. GetConnection ();
  74. if (con == null)
  75. return;
  76. CreateCommand ();
  77. if (cmd == null)
  78. return;
  79. string noOfTables = null;
  80. string tableName = null;
  81. int [] columnNos = null;
  82. try {
  83. noOfTables = ConfigClass.GetElement (configDoc, "tables", "numTables");
  84. short numTables = Convert.ToInt16 (noOfTables);
  85. string noOfQueries = ConfigClass.GetElement (configDoc, "queries", "numQueries");
  86. Console.WriteLine ("**** Running Queries ****");
  87. if (noOfQueries != null) {
  88. int numQueries = Convert.ToInt32 (noOfQueries);
  89. for (int index = 1; index <= numQueries; index++) {
  90. string queryStr = ConfigClass.GetElement (configDoc, "queries", "query" + index);
  91. int tableNum = 0;
  92. rdr = RunQuery (queryStr, ref columnNos, ref tableNum);
  93. if (rdr == null)
  94. continue;
  95. CompareData (rdr, configDoc, columnNos, tableNum);
  96. rdr.Close ();
  97. }
  98. }
  99. string storedProc = null;
  100. try {
  101. storedProc = ConfigClass.GetElement (configDoc, "StoredProcExists");
  102. } catch (Exception e) {
  103. return;
  104. }
  105. if (storedProc.Equals ("Y")) {
  106. Console.WriteLine ("\n**** Running tests for stored procedures *****\n");
  107. int numStoredProc = Convert.ToInt32 (ConfigClass.GetElement(configDoc,
  108. "StoredProc", "NumStoredProc"));
  109. for (int index = 1; index <= numStoredProc; index++) {
  110. string storedProcTag = "StoredProc" + index;
  111. string type = ConfigClass.GetElement (configDoc, "StoredProc",
  112. storedProcTag, "type");
  113. string nameTemplate = ConfigClass.GetElement (configDoc, "StoredProc",
  114. storedProcTag, "name");
  115. if (type.Equals("generic")) {
  116. // There is stored proc correspoding to each table
  117. // Run all such stored proc
  118. for (short i = 1; i <= numTables; i++) {
  119. try {
  120. tableName = ConfigClass.GetElement (configDoc, "tables",
  121. "table"+i, "name");
  122. } catch (XPathException e) {
  123. Console.WriteLine (e.Message);
  124. continue; // need not return here; try with the next one
  125. }
  126. string storedProcName = nameTemplate.Replace ("{{TABLE}}", tableName);
  127. rdr = QueryUsingStoredProc (cmd, storedProcName, null);
  128. if (rdr == null)
  129. continue;
  130. CompareData (rdr, configDoc, null, i);
  131. rdr.Close ();
  132. }
  133. }
  134. }
  135. }
  136. } catch (Exception e) {
  137. Console.WriteLine ("ERROR : " + e.Message);
  138. Console.WriteLine ("STACKTRACE : " + e.StackTrace);
  139. } finally {
  140. con.Close ();
  141. con = null;
  142. }
  143. }
  144. public virtual IDataReader QueryUsingStoredProc (IDbCommand cmd,
  145. string storedProcName,
  146. string paramName)
  147. {
  148. cmd.CommandType = CommandType.StoredProcedure;
  149. cmd.CommandText = storedProcName;
  150. IDataReader rdr = null;
  151. try {
  152. rdr = cmd.ExecuteReader ();
  153. } catch (Exception e) {
  154. Console.WriteLine ("Could not execute command : " + cmd.CommandText);
  155. Console.WriteLine ("ERROR : " + e.Message);
  156. Console.WriteLine ("STACKTRACE : " + e.StackTrace);
  157. return null;
  158. }
  159. return rdr;
  160. }
  161. IDataReader RunQuery (string queryStr, ref int [] columnNos, ref int tableNum)
  162. {
  163. string regexp = "\\b(Select|select) (?<columnList>(COLUMNS|((COLUMN\\d+,)*(COLUMN\\d+)))) from (?<tableName>TABLE\\d+)( order by (?<OrderBy>COLUMN\\d+))*";
  164. Match m = Regex.Match (queryStr, regexp, RegexOptions.ExplicitCapture);
  165. if (!m.Success) {
  166. Console.WriteLine ("Incorrect query format!!!");
  167. return null;
  168. }
  169. columnNos = null;
  170. while (m.Success) {
  171. string tableTag = m.Result ("${tableName}");
  172. tableNum = Convert.ToInt32 (tableTag.Replace ("TABLE", ""));
  173. string tableName = ConfigClass.GetElement (configDoc, "tables", tableTag.ToLower (), "name");
  174. queryStr = queryStr.Replace (tableTag, tableName);
  175. for (int i = 0; i<m.Groups.Count; i++) {
  176. Group g = m.Groups [i];
  177. CaptureCollection cc = g.Captures;
  178. for (int j = 0; j < cc.Count; j++) {
  179. string matchedVal = cc [j].Value;
  180. if (matchedVal.Equals ("COLUMNS")) {
  181. string [] columnNames = ConfigClass.GetColumnNames (configDoc, tableNum);
  182. queryStr = queryStr.Replace ("COLUMNS", String.Join (",", columnNames));
  183. columnNos = new int [columnNames.Length];
  184. for (int index = 1; index <= columnNos.Length; index++)
  185. columnNos [index - 1] = index;
  186. } else if (matchedVal.StartsWith ("COLUMN")) {
  187. // May be a column name or a comma
  188. // separated list of columns
  189. string [] listOfColumns = matchedVal.Split (',');
  190. if (columnNos == null) {
  191. columnNos = new int [listOfColumns.Length];
  192. int colIndex = 0;
  193. foreach (string str in listOfColumns) {
  194. int columnNo = Convert.ToInt32 (str.Replace("COLUMN", ""));
  195. columnNos [colIndex ++] = columnNo;
  196. }
  197. }
  198. foreach (string str in listOfColumns) {
  199. string columnName = ConfigClass.GetElement (configDoc, "tables",
  200. tableTag.ToLower (), str.ToLower (), "name");
  201. queryStr = queryStr.Replace (str, columnName);
  202. }
  203. }
  204. }
  205. }
  206. m = m.NextMatch ();
  207. }
  208. IDataReader rdr = null;
  209. cmd.CommandText = queryStr;
  210. try {
  211. rdr = cmd.ExecuteReader ();
  212. } catch (Exception e) {
  213. Console.WriteLine ("ERROR : " + e.Message);
  214. Console.WriteLine ("\nSTACKTRACE : " + e.StackTrace);
  215. return null;
  216. }
  217. return rdr;
  218. }
  219. void CompareData (IDataReader rdr,
  220. XmlNode doc,
  221. int [] columnNos,
  222. int numTable)
  223. {
  224. int rowNum = 0;
  225. string errorMsg = "";
  226. string tableName = null;
  227. try {
  228. tableName = ConfigClass.GetElement (doc, "tables", "table"+numTable, "name");
  229. } catch (Exception e) {
  230. Console.WriteLine ("ERROR : " + e.Message );
  231. Console.WriteLine ("STACKTRACE : " + e.StackTrace );
  232. return;
  233. }
  234. while (rdr.Read()) {
  235. rowNum ++;
  236. string columnValue = null;
  237. for (int i = 0; i < rdr.FieldCount; i++) {
  238. errorMsg = "";
  239. int columnNum = 0;
  240. try {
  241. if (columnNos == null)
  242. columnNum = i+1;
  243. else
  244. columnNum = columnNos [i];
  245. columnValue = ConfigClass.GetElement (doc, "values", "table" + numTable,
  246. "row" + rowNum, "column" + columnNum);
  247. } catch (Exception e) {
  248. Console.WriteLine ("ERROR : " + e.Message);
  249. Console.WriteLine ("STACKTRACE : " + e.StackTrace);
  250. }
  251. object obj = null;
  252. Console.Write ("Table : {0} : ROW: {1} COL: {2}", tableName, rowNum, columnNum);
  253. try {
  254. obj = GetValue (rdr, i);
  255. } catch (Exception e) {
  256. Console.WriteLine ("...FAIL");
  257. errorMsg = "ERROR : " + e.Message;
  258. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  259. errorMsg += "\nProbably the 'GetFieldType()' method returned a wrong type!!";
  260. Console.WriteLine (errorMsg);
  261. obj = null;
  262. continue;
  263. }
  264. if (AreEqual (obj, columnValue, ref errorMsg)) {
  265. Console.WriteLine ("...OK");
  266. } else {
  267. Console.WriteLine ("...FAIL");
  268. if (!errorMsg.Equals ("")) {
  269. // There was some exception
  270. Console.WriteLine (errorMsg);
  271. } else {
  272. // Comparison failed
  273. Console.WriteLine ("Expected : "+columnValue+" Got : "+obj);
  274. }
  275. }
  276. }
  277. Console.WriteLine ("======================");
  278. }
  279. }
  280. public virtual object GetValue (IDataReader rdr, int columnIndex)
  281. {
  282. object value = null;
  283. if (rdr.IsDBNull (columnIndex)) {
  284. return null;
  285. }
  286. Type type = rdr.GetFieldType (columnIndex);
  287. switch (type.Name.ToLower ()) {
  288. case "byte" : value = rdr.GetByte (columnIndex);
  289. break;
  290. case "sbyte" : value = rdr.GetInt16 (columnIndex);
  291. break;
  292. case "boolean" : value = rdr.GetBoolean (columnIndex);
  293. break;
  294. case "int16" : value = rdr.GetInt16 (columnIndex);
  295. break;
  296. case "uint16" :
  297. case "int32" : value = rdr.GetInt32 (columnIndex);
  298. break;
  299. case "uint32" :
  300. case "int64" : value = rdr.GetInt64 (columnIndex);
  301. break;
  302. case "single" : value = rdr.GetFloat (columnIndex);
  303. break;
  304. case "double" : value = rdr.GetDouble (columnIndex);
  305. break;
  306. case "uint64" :
  307. case "decimal" : value = rdr.GetDecimal (columnIndex);
  308. break;
  309. case "datetime": value = rdr.GetDateTime (columnIndex);
  310. break;
  311. case "string": value = rdr.GetString (columnIndex);
  312. break;
  313. default : value = rdr.GetValue (columnIndex);
  314. break;
  315. }
  316. return value;
  317. }
  318. public virtual Boolean AreEqual (object obj, string value, ref string errorMsg)
  319. {
  320. if ((obj == null) || (value.Equals("null"))) {
  321. if (obj == null && value.Equals ("null"))
  322. return true;
  323. return false;
  324. }
  325. object valObj = ConvertToValueType (obj.GetType (), value, ref errorMsg);
  326. if (valObj == null) {
  327. errorMsg = "Could not convert values!!\n" + errorMsg;
  328. return false;
  329. }
  330. return valObj.Equals (obj);
  331. }
  332. public virtual object ConvertToValueType (Type objType, string value, ref string errorMsg)
  333. {
  334. value = value.Trim ('\'');
  335. value = value.Trim ('\"');
  336. switch (Type.GetTypeCode (objType)) {
  337. case TypeCode.Int16 :
  338. return ConvertToInt16 (objType, value, ref errorMsg);
  339. case TypeCode.Int32 :
  340. return ConvertToInt32 (objType, value, ref errorMsg);
  341. case TypeCode.Int64 :
  342. return ConvertToInt64 (objType, value, ref errorMsg);
  343. case TypeCode.Boolean :
  344. return ConvertToBoolean (objType, value, ref errorMsg);
  345. case TypeCode.Byte :
  346. return ConvertToByte (objType, value, ref errorMsg);
  347. case TypeCode.DateTime :
  348. return ConvertToDateTime (objType, value, ref errorMsg);
  349. case TypeCode.Decimal :
  350. return ConvertToDecimal (objType, value, ref errorMsg);
  351. case TypeCode.Double :
  352. return ConvertToDouble (objType, value, ref errorMsg);
  353. case TypeCode.Single :
  354. return ConvertToSingle (objType, value, ref errorMsg);
  355. }
  356. if ( objType.ToString () == "System.TimeSpan")
  357. return ConvertToTimespan (objType, value, ref errorMsg);
  358. return ConvertValue (objType, value, ref errorMsg);
  359. }
  360. public virtual object ConvertValue (Type type, string value, ref string errorMsg)
  361. {
  362. object valObj = null;
  363. try {
  364. valObj = Convert.ChangeType (value, type);
  365. } catch (InvalidCastException e) {
  366. errorMsg = "Cant compare values!! \n";
  367. errorMsg += "ERROR : " + e.Message;
  368. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  369. return false;
  370. } catch (Exception e) {
  371. errorMsg = "ERROR : " + e.Message;
  372. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  373. return null;
  374. }
  375. return valObj;
  376. }
  377. public virtual object ConvertToInt16 (Type type, string value, ref string errorMsg)
  378. {
  379. return ConvertValue (type, value, ref errorMsg);
  380. }
  381. public virtual object ConvertToInt32 (Type type, string value, ref string errorMsg)
  382. {
  383. return ConvertValue (type, value, ref errorMsg);
  384. }
  385. public virtual object ConvertToInt64 (Type type, string value, ref string errorMsg)
  386. {
  387. return ConvertValue (type, value, ref errorMsg);
  388. }
  389. public virtual object ConvertToBoolean (Type type, string value, ref string errorMsg)
  390. {
  391. return ConvertValue (type, value, ref errorMsg);
  392. }
  393. public virtual object ConvertToByte (Type type, string value, ref string errorMsg)
  394. {
  395. return ConvertValue (type, value, ref errorMsg);
  396. }
  397. public virtual object ConvertToDateTime (Type type, string value, ref string errorMsg)
  398. {
  399. return ConvertValue (type, value, ref errorMsg);
  400. }
  401. public virtual object ConvertToDecimal (Type type, string value, ref string errorMsg)
  402. {
  403. return ConvertValue (type, value, ref errorMsg);
  404. }
  405. public virtual object ConvertToDouble (Type type, string value, ref string errorMsg)
  406. {
  407. return ConvertValue (type, value, ref errorMsg);
  408. }
  409. public virtual object ConvertToSingle (Type type, string value, ref string errorMsg)
  410. {
  411. return ConvertValue (type, value, ref errorMsg);
  412. }
  413. public virtual object ConvertToTimespan (Type type, string value, ref string errorMsg)
  414. {
  415. return ConvertValue (type, value, ref errorMsg);
  416. }
  417. public virtual void GetConnection()
  418. {
  419. }
  420. }
  421. }