BasicDataServiceTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. using log4net.Config;
  31. using NUnit.Framework;
  32. using NUnit.Framework.Constraints;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Tests.Common;
  36. using log4net;
  37. using System.Data;
  38. using System.Data.Common;
  39. using System.Reflection;
  40. namespace OpenSim.Data.Tests
  41. {
  42. /// <summary>This is a base class for testing any Data service for any DBMS.
  43. /// Requires NUnit 2.5 or better (to support the generics).
  44. /// </summary>
  45. /// <remarks>
  46. /// FIXME: Should extend OpenSimTestCase but compile on mono 2.4.3 currently fails with
  47. /// AssetTests`2 : System.MemberAccessException : Cannot create an instance of OpenSim.Data.Tests.AssetTests`2[TConn,TAssetData] because Type.ContainsGenericParameters is true.
  48. /// and similar on EstateTests, InventoryTests and RegionTests.
  49. /// Runs fine with mono 2.10.8.1, so easiest thing is to wait until min Mono version uplifts.
  50. /// </remarks>
  51. /// <typeparam name="TConn"></typeparam>
  52. /// <typeparam name="TService"></typeparam>
  53. public class BasicDataServiceTest<TConn, TService>
  54. where TConn : DbConnection, new()
  55. where TService : class, new()
  56. {
  57. protected string m_connStr;
  58. private TService m_service;
  59. private string m_file;
  60. // TODO: Is this in the right place here?
  61. // Later: apparently it's not, but does it matter here?
  62. // protected static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  63. protected ILog m_log; // doesn't matter here that it's not static, init to correct type in instance .ctor
  64. public BasicDataServiceTest()
  65. : this("")
  66. {
  67. }
  68. public BasicDataServiceTest(string conn)
  69. {
  70. m_connStr = !String.IsNullOrEmpty(conn) ? conn : DefaultTestConns.Get(typeof(TConn));
  71. m_log = LogManager.GetLogger(this.GetType());
  72. OpenSim.Tests.Common.TestLogging.LogToConsole(); // TODO: Is that right?
  73. }
  74. /// <summary>
  75. /// To be overridden in derived classes. Do whatever init with the m_service, like setting the conn string to it.
  76. /// You'd probably want to to cast the 'service' to a more specific type and store it in a member var.
  77. /// This framework takes care of disposing it, if it's disposable.
  78. /// </summary>
  79. /// <param name="service">The service being tested</param>
  80. protected virtual void InitService(object service)
  81. {
  82. }
  83. [TestFixtureSetUp]
  84. public void Init()
  85. {
  86. // Sorry, some SQLite-specific stuff goes here (not a big deal, as its just some file ops)
  87. if (typeof(TConn).Name.StartsWith("Sqlite"))
  88. {
  89. // SQLite doesn't work on power or z linux
  90. if (Directory.Exists("/proc/ppc64") || Directory.Exists("/proc/dasd"))
  91. Assert.Ignore();
  92. if (Util.IsWindows())
  93. Util.LoadArchSpecificWindowsDll("sqlite3.dll");
  94. // for SQLite, if no explicit conn string is specified, use a temp file
  95. if (String.IsNullOrEmpty(m_connStr))
  96. {
  97. m_file = Path.GetTempFileName() + ".db";
  98. m_connStr = "URI=file:" + m_file + ",version=3";
  99. }
  100. }
  101. if (String.IsNullOrEmpty(m_connStr))
  102. {
  103. string msg = String.Format("Connection string for {0} is not defined, ignoring tests", typeof(TConn).Name);
  104. m_log.Warn(msg);
  105. Assert.Ignore(msg);
  106. }
  107. // Try the connection, ignore tests if Open() fails
  108. using (TConn conn = new TConn())
  109. {
  110. conn.ConnectionString = m_connStr;
  111. try
  112. {
  113. conn.Open();
  114. conn.Close();
  115. }
  116. catch
  117. {
  118. string msg = String.Format("{0} is unable to connect to the database, ignoring tests", typeof(TConn).Name);
  119. m_log.Warn(msg);
  120. Assert.Ignore(msg);
  121. }
  122. }
  123. // If we manage to connect to the database with the user
  124. // and password above it is our test database, and run
  125. // these tests. If anything goes wrong, ignore these
  126. // tests.
  127. try
  128. {
  129. m_service = new TService();
  130. InitService(m_service);
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.Error(e.ToString());
  135. Assert.Ignore();
  136. }
  137. }
  138. [TestFixtureTearDown]
  139. public void Cleanup()
  140. {
  141. if (m_service != null)
  142. {
  143. if (m_service is IDisposable)
  144. ((IDisposable)m_service).Dispose();
  145. m_service = null;
  146. }
  147. if (!String.IsNullOrEmpty(m_file) && File.Exists(m_file))
  148. File.Delete(m_file);
  149. }
  150. protected virtual DbConnection Connect()
  151. {
  152. DbConnection cnn = new TConn();
  153. cnn.ConnectionString = m_connStr;
  154. cnn.Open();
  155. return cnn;
  156. }
  157. protected virtual void ExecuteSql(string sql)
  158. {
  159. using (DbConnection dbcon = Connect())
  160. {
  161. using (DbCommand cmd = dbcon.CreateCommand())
  162. {
  163. cmd.CommandText = sql;
  164. cmd.ExecuteNonQuery();
  165. }
  166. }
  167. }
  168. protected delegate bool ProcessRow(IDataReader reader);
  169. protected virtual int ExecQuery(string sql, bool bSingleRow, ProcessRow action)
  170. {
  171. int nRecs = 0;
  172. using (DbConnection dbcon = Connect())
  173. {
  174. using (DbCommand cmd = dbcon.CreateCommand())
  175. {
  176. cmd.CommandText = sql;
  177. CommandBehavior cb = bSingleRow ? CommandBehavior.SingleRow : CommandBehavior.Default;
  178. using (DbDataReader rdr = cmd.ExecuteReader(cb))
  179. {
  180. while (rdr.Read())
  181. {
  182. nRecs++;
  183. if (!action(rdr))
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. return nRecs;
  190. }
  191. /// <summary>Drop tables (listed as parameters). There is no "DROP IF EXISTS" syntax common for all
  192. /// databases, so we just DROP and ignore an exception.
  193. /// </summary>
  194. /// <param name="tables"></param>
  195. protected virtual void DropTables(params string[] tables)
  196. {
  197. foreach (string tbl in tables)
  198. {
  199. try
  200. {
  201. ExecuteSql("DROP TABLE " + tbl + ";");
  202. }catch
  203. {
  204. }
  205. }
  206. }
  207. /// <summary>Clear tables listed as parameters (without dropping them).
  208. /// </summary>
  209. /// <param name="tables"></param>
  210. protected virtual void ResetMigrations(params string[] stores)
  211. {
  212. string lst = "";
  213. foreach (string store in stores)
  214. {
  215. string s = "'" + store + "'";
  216. if (lst == "")
  217. lst = s;
  218. else
  219. lst += ", " + s;
  220. }
  221. string sCond = stores.Length > 1 ? ("in (" + lst + ")") : ("=" + lst);
  222. try
  223. {
  224. ExecuteSql("DELETE FROM migrations where name " + sCond);
  225. }
  226. catch
  227. {
  228. }
  229. }
  230. /// <summary>Clear tables listed as parameters (without dropping them).
  231. /// </summary>
  232. /// <param name="tables"></param>
  233. protected virtual void ClearTables(params string[] tables)
  234. {
  235. foreach (string tbl in tables)
  236. {
  237. try
  238. {
  239. ExecuteSql("DELETE FROM " + tbl + ";");
  240. }
  241. catch
  242. {
  243. }
  244. }
  245. }
  246. }
  247. }