ConnectionConfig.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // ConnectionConfig.cs - Holds information on a specific connection and
  3. // corresponding engine to test.
  4. //
  5. // Author:
  6. // Gert Driesen ([email protected]
  7. //
  8. // Copyright (c) 2008 Gert Driesen
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.Globalization;
  33. using System.Xml;
  34. namespace MonoTests.System.Data.Connected
  35. {
  36. internal sealed class ConnectionConfig
  37. {
  38. private readonly string name;
  39. private readonly string factory;
  40. private readonly string connectionString;
  41. private readonly EngineConfig engine;
  42. private ConnectionConfig (string name, string factory, string connectionString, EngineConfig engine)
  43. {
  44. this.name = name;
  45. this.factory = factory;
  46. this.connectionString = connectionString;
  47. this.engine = engine;
  48. }
  49. internal static ConnectionConfig FromXml (XmlNode connNode, Hashtable engines)
  50. {
  51. return new ConnectionConfig (
  52. GetAttribValue (connNode, "name", true),
  53. GetAttribValue (connNode, "factory", true),
  54. GetAttribValue (connNode, "connectionString", true),
  55. GetEngine (connNode, engines));
  56. }
  57. public string Name {
  58. get { return name; }
  59. }
  60. public string Factory {
  61. get { return factory; }
  62. }
  63. public string ConnectionString {
  64. get { return connectionString; }
  65. }
  66. public EngineConfig Engine {
  67. get { return engine; }
  68. }
  69. static string GetAttribValue (XmlNode node, string name, bool required)
  70. {
  71. XmlAttribute attr = node.Attributes [name];
  72. if (attr == null) {
  73. if (required)
  74. throw CreateAttributeMissingException (name, node);
  75. return null;
  76. }
  77. return attr.Value;
  78. }
  79. static EngineConfig GetEngine (XmlNode connNode, Hashtable engines)
  80. {
  81. XmlAttribute engineAttr = connNode.Attributes ["engine"];
  82. if (engineAttr == null)
  83. throw CreateAttributeMissingException ("engine", connNode);
  84. string engineName = engineAttr.Value;
  85. EngineConfig engine = (EngineConfig) engines [engineName];
  86. if (engine == null) {
  87. string msg = string.Format (CultureInfo.InvariantCulture,
  88. "Engine '{0}' does not exist.", engineName);
  89. throw new ConfigurationErrorsException (msg, engineAttr);
  90. }
  91. return engine;
  92. }
  93. static Exception CreateAttributeMissingException (string name, XmlNode node)
  94. {
  95. string msg = string.Format (CultureInfo.InvariantCulture,
  96. "Missing '{0}' attribute.", name);
  97. throw new ConfigurationErrorsException (msg, node);
  98. }
  99. }
  100. }