BasicDataServiceTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using log4net.Config;
  5. using NUnit.Framework;
  6. using NUnit.Framework.Constraints;
  7. using OpenMetaverse;
  8. using OpenSim.Framework;
  9. using log4net;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Reflection;
  13. namespace OpenSim.Data.Tests
  14. {
  15. /// <summary>This is a base class for testing any Data service for any DBMS.
  16. /// Requires NUnit 2.5 or better (to support the generics).
  17. /// </summary>
  18. /// <typeparam name="TConn"></typeparam>
  19. /// <typeparam name="TService"></typeparam>
  20. public class BasicDataServiceTest<TConn, TService>
  21. where TConn : DbConnection, new()
  22. where TService : class, new()
  23. {
  24. protected string m_connStr;
  25. private TService m_service;
  26. private string m_file;
  27. // TODO: Is this in the right place here?
  28. // Later: apparently it's not, but does it matter here?
  29. // protected static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  30. protected ILog m_log; // doesn't matter here that it's not static, init to correct type in instance .ctor
  31. public BasicDataServiceTest()
  32. : this("")
  33. {
  34. }
  35. public BasicDataServiceTest(string conn)
  36. {
  37. m_connStr = !String.IsNullOrEmpty(conn) ? conn : DefaultTestConns.Get(typeof(TConn));
  38. m_log = LogManager.GetLogger(this.GetType());
  39. OpenSim.Tests.Common.TestLogging.LogToConsole(); // TODO: Is that right?
  40. }
  41. /// <summary>
  42. /// To be overridden in derived classes. Do whatever init with the m_service, like setting the conn string to it.
  43. /// You'd probably want to to cast the 'service' to a more specific type and store it in a member var.
  44. /// This framework takes care of disposing it, if it's disposable.
  45. /// </summary>
  46. /// <param name="service">The service being tested</param>
  47. protected virtual void InitService(object service)
  48. {
  49. }
  50. [TestFixtureSetUp]
  51. public void Init()
  52. {
  53. // Sorry, some SQLite-specific stuff goes here (not a big deal, as its just some file ops)
  54. if (typeof(TConn).Name.StartsWith("Sqlite"))
  55. {
  56. // SQLite doesn't work on power or z linux
  57. if (Directory.Exists("/proc/ppc64") || Directory.Exists("/proc/dasd"))
  58. Assert.Ignore();
  59. // for SQLite, if no explicit conn string is specified, use a temp file
  60. if (String.IsNullOrEmpty(m_connStr))
  61. {
  62. m_file = Path.GetTempFileName() + ".db";
  63. m_connStr = "URI=file:" + m_file + ",version=3";
  64. }
  65. }
  66. if (String.IsNullOrEmpty(m_connStr))
  67. {
  68. string msg = String.Format("Connection string for {0} is not defined, ignoring tests", typeof(TConn).Name);
  69. m_log.Warn(msg);
  70. Assert.Ignore(msg);
  71. }
  72. // Try the connection, ignore tests if Open() fails
  73. using (TConn conn = new TConn())
  74. {
  75. conn.ConnectionString = m_connStr;
  76. try
  77. {
  78. conn.Open();
  79. conn.Close();
  80. }
  81. catch
  82. {
  83. string msg = String.Format("{0} is unable to connect to the database, ignoring tests", typeof(TConn).Name);
  84. m_log.Warn(msg);
  85. Assert.Ignore(msg);
  86. }
  87. }
  88. // If we manage to connect to the database with the user
  89. // and password above it is our test database, and run
  90. // these tests. If anything goes wrong, ignore these
  91. // tests.
  92. try
  93. {
  94. m_service = new TService();
  95. InitService(m_service);
  96. }
  97. catch (Exception e)
  98. {
  99. m_log.Error(e.ToString());
  100. Assert.Ignore();
  101. }
  102. }
  103. [TestFixtureTearDown]
  104. public void Cleanup()
  105. {
  106. if (m_service != null)
  107. {
  108. if( m_service is IDisposable)
  109. ((IDisposable)m_service).Dispose();
  110. m_service = null;
  111. }
  112. if( !String.IsNullOrEmpty(m_file) && File.Exists(m_file) )
  113. File.Delete(m_file);
  114. }
  115. protected virtual DbConnection Connect()
  116. {
  117. DbConnection cnn = new TConn();
  118. cnn.ConnectionString = m_connStr;
  119. cnn.Open();
  120. return cnn;
  121. }
  122. protected virtual void ExecuteSql(string sql)
  123. {
  124. using (DbConnection dbcon = Connect())
  125. {
  126. using (DbCommand cmd = dbcon.CreateCommand())
  127. {
  128. cmd.CommandText = sql;
  129. cmd.ExecuteNonQuery();
  130. }
  131. }
  132. }
  133. protected delegate bool ProcessRow(IDataReader reader);
  134. protected virtual int ExecQuery(string sql, bool bSingleRow, ProcessRow action)
  135. {
  136. int nRecs = 0;
  137. using (DbConnection dbcon = Connect())
  138. {
  139. using (DbCommand cmd = dbcon.CreateCommand())
  140. {
  141. cmd.CommandText = sql;
  142. CommandBehavior cb = bSingleRow ? CommandBehavior.SingleRow : CommandBehavior.Default;
  143. using (DbDataReader rdr = cmd.ExecuteReader(cb))
  144. {
  145. while (rdr.Read())
  146. {
  147. nRecs++;
  148. if (!action(rdr))
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. return nRecs;
  155. }
  156. /// <summary>Drop tables (listed as parameters). There is no "DROP IF EXISTS" syntax common for all
  157. /// databases, so we just DROP and ignore an exception.
  158. /// </summary>
  159. /// <param name="tables"></param>
  160. protected virtual void DropTables(params string[] tables)
  161. {
  162. foreach (string tbl in tables)
  163. {
  164. try
  165. {
  166. ExecuteSql("DROP TABLE " + tbl + ";");
  167. }catch
  168. {
  169. }
  170. }
  171. }
  172. /// <summary>Clear tables listed as parameters (without dropping them).
  173. /// </summary>
  174. /// <param name="tables"></param>
  175. protected virtual void ResetMigrations(params string[] stores)
  176. {
  177. string lst = "";
  178. foreach (string store in stores)
  179. {
  180. string s = "'" + store + "'";
  181. if (lst == "")
  182. lst = s;
  183. else
  184. lst += ", " + s;
  185. }
  186. string sCond = stores.Length > 1 ? ("in (" + lst + ")") : ("=" + lst);
  187. try
  188. {
  189. ExecuteSql("DELETE FROM migrations where name " + sCond);
  190. }
  191. catch
  192. {
  193. }
  194. }
  195. /// <summary>Clear tables listed as parameters (without dropping them).
  196. /// </summary>
  197. /// <param name="tables"></param>
  198. protected virtual void ClearTables(params string[] tables)
  199. {
  200. foreach (string tbl in tables)
  201. {
  202. try
  203. {
  204. ExecuteSql("DELETE FROM " + tbl + ";");
  205. }
  206. catch
  207. {
  208. }
  209. }
  210. }
  211. }
  212. }