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