ConnectionConfig.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if !NO_CONFIGURATION
  30. using System;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.Globalization;
  34. using System.Xml;
  35. namespace MonoTests.System.Data.Connected
  36. {
  37. internal sealed class ConnectionConfig
  38. {
  39. private readonly string name;
  40. private readonly string factory;
  41. private readonly string connectionString;
  42. private readonly EngineConfig engine;
  43. private ConnectionConfig (string name, string factory, string connectionString, EngineConfig engine)
  44. {
  45. this.name = name;
  46. this.factory = factory;
  47. this.connectionString = connectionString;
  48. this.engine = engine;
  49. }
  50. internal static ConnectionConfig FromXml (XmlNode connNode, Hashtable engines)
  51. {
  52. return new ConnectionConfig (
  53. GetAttribValue (connNode, "name", true),
  54. GetAttribValue (connNode, "factory", true),
  55. GetAttribValue (connNode, "connectionString", true),
  56. GetEngine (connNode, engines));
  57. }
  58. public string Name {
  59. get { return name; }
  60. }
  61. public string Factory {
  62. get { return factory; }
  63. }
  64. public string ConnectionString {
  65. get { return connectionString; }
  66. }
  67. public EngineConfig Engine {
  68. get { return engine; }
  69. }
  70. static string GetAttribValue (XmlNode node, string name, bool required)
  71. {
  72. XmlAttribute attr = node.Attributes [name];
  73. if (attr == null) {
  74. if (required)
  75. throw CreateAttributeMissingException (name, node);
  76. return null;
  77. }
  78. return attr.Value;
  79. }
  80. static EngineConfig GetEngine (XmlNode connNode, Hashtable engines)
  81. {
  82. XmlAttribute engineAttr = connNode.Attributes ["engine"];
  83. if (engineAttr == null)
  84. throw CreateAttributeMissingException ("engine", connNode);
  85. string engineName = engineAttr.Value;
  86. EngineConfig engine = (EngineConfig) engines [engineName];
  87. if (engine == null) {
  88. string msg = string.Format (CultureInfo.InvariantCulture,
  89. "Engine '{0}' does not exist.", engineName);
  90. throw new ConfigurationErrorsException (msg, engineAttr);
  91. }
  92. return engine;
  93. }
  94. static Exception CreateAttributeMissingException (string name, XmlNode node)
  95. {
  96. string msg = string.Format (CultureInfo.InvariantCulture,
  97. "Missing '{0}' attribute.", name);
  98. throw new ConfigurationErrorsException (msg, node);
  99. }
  100. }
  101. }
  102. #endif