diff options
author | AlexRa | 2010-05-17 12:37:49 +0300 |
---|---|---|
committer | AlexRa | 2010-05-23 11:47:36 +0300 |
commit | 91ad1f4ee71dc8e945e9be7b85f7d4e0ddcf4156 (patch) | |
tree | 20c64a9e438bbc476b77f10e4628ed419689e030 /OpenSim/Data/Tests/DefaultTestConns.cs | |
parent | Apply http://opensimulator.org/mantis/view.php?id=4632 (diff) | |
download | opensim-SC_OLD-91ad1f4ee71dc8e945e9be7b85f7d4e0ddcf4156.zip opensim-SC_OLD-91ad1f4ee71dc8e945e9be7b85f7d4e0ddcf4156.tar.gz opensim-SC_OLD-91ad1f4ee71dc8e945e9be7b85f7d4e0ddcf4156.tar.bz2 opensim-SC_OLD-91ad1f4ee71dc8e945e9be7b85f7d4e0ddcf4156.tar.xz |
Added generic base classes for testing database services
These are some generic classes that simplify writing tests
for any of the data connectors and databases. Among other
things, configuring the connection strings is done once,
in a separate resource file.
Tests based on the new BasicDataServiceTest class require
NUnit 2.5 or better.
Diffstat (limited to 'OpenSim/Data/Tests/DefaultTestConns.cs')
-rw-r--r-- | OpenSim/Data/Tests/DefaultTestConns.cs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/OpenSim/Data/Tests/DefaultTestConns.cs b/OpenSim/Data/Tests/DefaultTestConns.cs new file mode 100644 index 0000000..7b52af5 --- /dev/null +++ b/OpenSim/Data/Tests/DefaultTestConns.cs | |||
@@ -0,0 +1,63 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Linq; | ||
4 | using System.Text; | ||
5 | using System.Reflection; | ||
6 | using System.IO; | ||
7 | using Nini.Config; | ||
8 | |||
9 | namespace OpenSim.Data.Tests | ||
10 | { | ||
11 | /// <summary>This static class looks for TestDataConnections.ini file in the /bin directory to obtain | ||
12 | /// a connection string for testing one of the supported databases. | ||
13 | /// The connections must be in the section [TestConnections] with names matching the connection class | ||
14 | /// name for the specific database, e.g.: | ||
15 | /// | ||
16 | /// [TestConnections] | ||
17 | /// MySqlConnection="..." | ||
18 | /// SqlConnection="..." | ||
19 | /// SqliteConnection="..." | ||
20 | /// | ||
21 | /// Note that the conn string may also be set explicitly in the [TestCase()] attribute of test classes | ||
22 | /// based on BasicDataServiceTest.cs. | ||
23 | /// </summary> | ||
24 | |||
25 | static class DefaultTestConns | ||
26 | { | ||
27 | private static Dictionary<Type, string> conns = new Dictionary<Type, string>(); | ||
28 | |||
29 | public static string Get(Type connType) | ||
30 | { | ||
31 | string sConn; | ||
32 | |||
33 | if (conns.TryGetValue(connType, out sConn)) | ||
34 | return sConn; | ||
35 | |||
36 | Assembly asm = Assembly.GetExecutingAssembly(); | ||
37 | string sType = connType.Name; | ||
38 | |||
39 | // Note: when running from NUnit, the DLL is located in some temp dir, so how do we get | ||
40 | // to the INI file? Ok, so put it into the resources! | ||
41 | // string iniName = Path.Combine(Path.GetDirectoryName(asm.Location), "TestDataConnections.ini"); | ||
42 | |||
43 | string[] allres = asm.GetManifestResourceNames(); | ||
44 | string sResFile = Array.Find(allres, s => s.Contains("TestDataConnections.ini")); | ||
45 | |||
46 | if (String.IsNullOrEmpty(sResFile)) | ||
47 | throw new Exception(String.Format("Please add resource TestDataConnections.ini, with section [TestConnections] and settings like {0}=\"...\"", | ||
48 | sType)); | ||
49 | |||
50 | using (Stream resource = asm.GetManifestResourceStream(sResFile)) | ||
51 | { | ||
52 | IConfigSource source = new IniConfigSource(resource); | ||
53 | var cfg = source.Configs["TestConnections"]; | ||
54 | sConn = cfg.Get(sType, ""); | ||
55 | } | ||
56 | |||
57 | if (!String.IsNullOrEmpty(sConn)) | ||
58 | conns[connType] = sConn; | ||
59 | |||
60 | return sConn; | ||
61 | } | ||
62 | } | ||
63 | } | ||