OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
DefaultTestConns.cs
Go to the documentation of this file.
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.Collections.Generic;
30 using System.Linq;
31 using System.Text;
32 using System.Reflection;
33 using System.IO;
34 using Nini.Config;
35 
36 namespace OpenSim.Data.Tests
37 {
51 
52  static class DefaultTestConns
53  {
54  private static Dictionary<Type, string> conns = new Dictionary<Type, string>();
55 
56  public static string Get(Type connType)
57  {
58  string sConn;
59 
60  if (conns.TryGetValue(connType, out sConn))
61  return sConn;
62 
63  Assembly asm = Assembly.GetExecutingAssembly();
64  string sType = connType.Name;
65 
66  // Note: when running from NUnit, the DLL is located in some temp dir, so how do we get
67  // to the INI file? Ok, so put it into the resources!
68  // string iniName = Path.Combine(Path.GetDirectoryName(asm.Location), "TestDataConnections.ini");
69 
70  string[] allres = asm.GetManifestResourceNames();
71  string sResFile = Array.Find(allres, s => s.Contains("TestDataConnections.ini"));
72 
73  if (String.IsNullOrEmpty(sResFile))
74  throw new Exception(String.Format("Please add resource TestDataConnections.ini, with section [TestConnections] and settings like {0}=\"...\"",
75  sType));
76 
77  using (Stream resource = asm.GetManifestResourceStream(sResFile))
78  {
79  IConfigSource source = new IniConfigSource(resource);
80  var cfg = source.Configs["TestConnections"];
81  sConn = cfg.Get(sType, "");
82  }
83 
84  if (!String.IsNullOrEmpty(sConn))
85  conns[connType] = sConn;
86 
87  return sConn;
88  }
89  }
90 }