Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / System.Data / Test / ProviderTests / Common / ConnectionConfig.cs
1 //
2 // ConnectionConfig.cs  - Holds information on a specific connection and
3 // corresponding engine to test.
4 //
5 // Author:
6 //      Gert Driesen (drieseng@users.sourceforge.net
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
30 #if !NO_CONFIGURATION
31
32 using System;
33 using System.Collections;
34 using System.Configuration;
35 using System.Globalization;
36 using System.Xml;
37
38 namespace MonoTests.System.Data.Connected
39 {
40         internal sealed class ConnectionConfig
41         {
42                 private readonly string name;
43                 private readonly string factory;
44                 private readonly string connectionString;
45                 private readonly EngineConfig engine;
46
47                 private ConnectionConfig (string name, string factory, string connectionString, EngineConfig engine)
48                 {
49                         this.name = name;
50                         this.factory = factory;
51                         this.connectionString = connectionString;
52                         this.engine = engine;
53                 }
54
55                 internal static ConnectionConfig FromXml (XmlNode connNode, Hashtable engines)
56                 {
57                         return new ConnectionConfig (
58                                 GetAttribValue (connNode, "name", true),
59                                 GetAttribValue (connNode, "factory", true),
60                                 GetAttribValue (connNode, "connectionString", true),
61                                 GetEngine (connNode, engines));
62                 }
63
64                 public string Name {
65                         get { return name; }
66                 }
67
68                 public string Factory {
69                         get { return factory; }
70                 }
71
72                 public string ConnectionString {
73                         get { return connectionString; }
74                 }
75
76                 public EngineConfig Engine {
77                         get { return engine; }
78                 }
79
80                 static string GetAttribValue (XmlNode node, string name, bool required)
81                 {
82                         XmlAttribute attr = node.Attributes [name];
83                         if (attr == null) {
84                                 if (required)
85                                         throw CreateAttributeMissingException (name, node);
86                                 return null;
87                         }
88                         return attr.Value;
89                 }
90
91                 static EngineConfig GetEngine (XmlNode connNode, Hashtable engines)
92                 {
93                         XmlAttribute engineAttr = connNode.Attributes ["engine"];
94                         if (engineAttr == null)
95                                 throw CreateAttributeMissingException ("engine", connNode);
96
97                         string engineName = engineAttr.Value;
98                         EngineConfig engine = (EngineConfig) engines [engineName];
99                         if (engine == null) {
100                                 string msg = string.Format (CultureInfo.InvariantCulture,
101                                         "Engine '{0}' does not exist.", engineName);
102                                 throw new ConfigurationErrorsException (msg, engineAttr);
103                         }
104                         return engine;
105                 }
106
107                 static Exception CreateAttributeMissingException (string name, XmlNode node)
108                 {
109                         string msg = string.Format (CultureInfo.InvariantCulture,
110                                 "Missing '{0}' attribute.", name);
111                         throw new ConfigurationErrorsException (msg, node);
112                 }
113         }
114 }
115
116 #endif