// // DataProviderBaseTest.cs : A base class that provides the common // functionality of : // 1) Reading a config file containing the // database connection parameters, different // tables and their description, Values that // the tables are populated with. // 2) Retrieves data from these tables; // 3) Compares the retrieved values against the ones // contained in the config file. // // A class specific to each database (and ODBC) are derived from this class. // These classes contain code specific to different databases (like establishing // a connection, comparing date values, etc). // // Author: // Satya Sudha K (ksathyasudha@novell.com) // // // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections; using System.IO; using System.Xml; using System.Xml.XPath; using System.Data; using System.Configuration; using System.Text.RegularExpressions; namespace MonoTests.System.Data { public class BaseRetrieve { public IDbConnection con; public IDbCommand cmd; public IDataReader rdr; protected XmlNode configDoc; public BaseRetrieve (string database) { con = null; cmd = null; rdr = null; configDoc = (XmlNode) ConfigurationSettings.GetConfig (database); } void CreateCommand () { if (con == null) return; cmd = con.CreateCommand (); } // Method that actually runs the entire test : Connects to a database, // retrieves values from different tables, and compares them against // the values that we had entered public void RunTest () { GetConnection (); if (con == null) return; CreateCommand (); if (cmd == null) return; string noOfTables = null; string tableName = null; int [] columnNos = null; try { noOfTables = ConfigClass.GetElement (configDoc, "tables", "numTables"); short numTables = Convert.ToInt16 (noOfTables); string noOfQueries = ConfigClass.GetElement (configDoc, "queries", "numQueries"); Console.WriteLine ("**** Running Queries ****"); if (noOfQueries != null) { int numQueries = Convert.ToInt32 (noOfQueries); for (int index = 1; index <= numQueries; index++) { string queryStr = ConfigClass.GetElement (configDoc, "queries", "query" + index); int tableNum = 0; rdr = RunQuery (queryStr, ref columnNos, ref tableNum); if (rdr == null) continue; CompareData (rdr, configDoc, columnNos, tableNum); rdr.Close (); } } string storedProc = null; try { storedProc = ConfigClass.GetElement (configDoc, "StoredProcExists"); } catch (Exception e) { return; } if (storedProc.Equals ("Y")) { Console.WriteLine ("\n**** Running tests for stored procedures *****\n"); int numStoredProc = Convert.ToInt32 (ConfigClass.GetElement(configDoc, "StoredProc", "NumStoredProc")); for (int index = 1; index <= numStoredProc; index++) { string storedProcTag = "StoredProc" + index; string type = ConfigClass.GetElement (configDoc, "StoredProc", storedProcTag, "type"); string nameTemplate = ConfigClass.GetElement (configDoc, "StoredProc", storedProcTag, "name"); if (type.Equals("generic")) { // There is stored proc correspoding to each table // Run all such stored proc for (short i = 1; i <= numTables; i++) { try { tableName = ConfigClass.GetElement (configDoc, "tables", "table"+i, "name"); } catch (XPathException e) { Console.WriteLine (e.Message); continue; // need not return here; try with the next one } string storedProcName = nameTemplate.Replace ("{{TABLE}}", tableName); rdr = QueryUsingStoredProc (cmd, storedProcName, null); if (rdr == null) continue; CompareData (rdr, configDoc, null, i); rdr.Close (); } } } } } catch (Exception e) { Console.WriteLine ("ERROR : " + e.Message); Console.WriteLine ("STACKTRACE : " + e.StackTrace); } finally { con.Close (); con = null; } } public virtual IDataReader QueryUsingStoredProc (IDbCommand cmd, string storedProcName, string paramName) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storedProcName; IDataReader rdr = null; try { rdr = cmd.ExecuteReader (); } catch (Exception e) { Console.WriteLine ("Could not execute command : " + cmd.CommandText); Console.WriteLine ("ERROR : " + e.Message); Console.WriteLine ("STACKTRACE : " + e.StackTrace); return null; } return rdr; } IDataReader RunQuery (string queryStr, ref int [] columnNos, ref int tableNum) { string regexp = "\\b(Select|select) (?(COLUMNS|((COLUMN\\d+,)*(COLUMN\\d+)))) from (?TABLE\\d+)( order by (?COLUMN\\d+))*"; Match m = Regex.Match (queryStr, regexp, RegexOptions.ExplicitCapture); if (!m.Success) { Console.WriteLine ("Incorrect query format!!!"); return null; } columnNos = null; while (m.Success) { string tableTag = m.Result ("${tableName}"); tableNum = Convert.ToInt32 (tableTag.Replace ("TABLE", "")); string tableName = ConfigClass.GetElement (configDoc, "tables", tableTag.ToLower (), "name"); queryStr = queryStr.Replace (tableTag, tableName); for (int i = 0; i