DefaultTestConns.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. namespace OpenSim.Data.Tests
  9. {
  10. /// <summary>This static class looks for TestDataConnections.ini file in the /bin directory to obtain
  11. /// a connection string for testing one of the supported databases.
  12. /// The connections must be in the section [TestConnections] with names matching the connection class
  13. /// name for the specific database, e.g.:
  14. ///
  15. /// [TestConnections]
  16. /// MySqlConnection="..."
  17. /// SqlConnection="..."
  18. /// SqliteConnection="..."
  19. ///
  20. /// Note that the conn string may also be set explicitly in the [TestCase()] attribute of test classes
  21. /// based on BasicDataServiceTest.cs.
  22. /// </summary>
  23. static class DefaultTestConns
  24. {
  25. private static Dictionary<Type, string> conns = new Dictionary<Type, string>();
  26. public static string Get(Type connType)
  27. {
  28. string sConn;
  29. if (conns.TryGetValue(connType, out sConn))
  30. return sConn;
  31. Assembly asm = Assembly.GetExecutingAssembly();
  32. string sType = connType.Name;
  33. // Note: when running from NUnit, the DLL is located in some temp dir, so how do we get
  34. // to the INI file? Ok, so put it into the resources!
  35. // string iniName = Path.Combine(Path.GetDirectoryName(asm.Location), "TestDataConnections.ini");
  36. string[] allres = asm.GetManifestResourceNames();
  37. string sResFile = Array.Find(allres, s => s.Contains("TestDataConnections.ini"));
  38. if (String.IsNullOrEmpty(sResFile))
  39. throw new Exception(String.Format("Please add resource TestDataConnections.ini, with section [TestConnections] and settings like {0}=\"...\"",
  40. sType));
  41. using (Stream resource = asm.GetManifestResourceStream(sResFile))
  42. {
  43. IConfigSource source = new IniConfigSource(resource);
  44. var cfg = source.Configs["TestConnections"];
  45. sConn = cfg.Get(sType, "");
  46. }
  47. if (!String.IsNullOrEmpty(sConn))
  48. conns[connType] = sConn;
  49. return sConn;
  50. }
  51. }
  52. }