[System.Data] Fixes tests build with mobile profiles
[mono.git] / mcs / class / System.Data / Test / ProviderTests / Common / EngineConfig.cs
1 //
2 // EngineConfig.cs  - Holds information on the capabilities and behavior of an
3 // RDBMS engine.
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 using System;
31 using System.Globalization;
32 using System.Xml;
33
34 namespace MonoTests.System.Data.Connected
35 {
36         public sealed class EngineConfig
37         {
38                 private string name;
39                 private string quoteCharacter;
40                 private bool removesTrailingSpaces;
41                 private bool emptyBinaryAsNull;
42                 private bool supportsMicroseconds;
43                 private bool supportsUniqueIdentifier;
44                 private bool supportsDate;
45                 private bool supportsTime;
46                 private bool supportsTimestamp;
47                 private EngineType type;
48                 private int clientVersion;
49
50                 private EngineConfig ()
51                 {
52                 }
53
54                 public string Name {
55                         get { return name; }
56                 }
57
58                 /// <summary>
59                 /// Returns the character(s) for quoting identifiers.
60                 /// </summary>
61                 public string QuoteCharacter {
62                         get { return quoteCharacter; }
63                 }
64
65                 public EngineType Type {
66                         get { return type; }
67                 }
68
69                 public bool RemovesTrailingSpaces {
70                         get { return removesTrailingSpaces; }
71                 }
72
73                 public bool EmptyBinaryAsNull {
74                         get { return emptyBinaryAsNull; }
75                 }
76
77                 public bool SupportsMicroseconds {
78                         get { return supportsMicroseconds; }
79                 }
80
81                 public bool SupportsUniqueIdentifier {
82                         get { return supportsUniqueIdentifier; }
83                 }
84
85                 public bool SupportsDate {
86                         get { return supportsDate; }
87                 }
88
89                 public bool SupportsTime {
90                         get { return supportsTime; }
91                 }
92
93                 public bool SupportsTimestamp {
94                         get { return supportsTimestamp; }
95                 }
96
97                 public int ClientVersion {
98                        get { return clientVersion; }
99                 }
100
101                 public static EngineConfig FromXml (XmlNode config)
102                 {
103                         EngineConfig engine = new EngineConfig ();
104                         engine.name = GetAttribValue (config, "name", true);
105                         engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
106                         engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
107                         engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
108                         engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
109                         engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
110                         engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
111                         engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
112                         engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
113                         engine.type = ParseEngineType (config, "type");
114                         engine.clientVersion = ParseClientVersion (config, "clientversion");
115                         return engine;
116                 }
117
118                 static string GetAttribValue (XmlNode node, string name, bool required)
119                 {
120                         XmlAttribute attr = node.Attributes [name];
121                         if (attr == null) {
122                                 if (required)
123                                         throw CreateAttributeMissingException (name, node);
124                                 return null;
125                         }
126                         return attr.Value;
127                 }
128
129                 static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
130                 {
131                         XmlAttribute attr = config.Attributes [attrName];
132                         if (attr == null) {
133                                 if (required)
134                                         throw CreateAttributeMissingException (attrName, config);
135                                 return defaultValue;
136                         }
137
138                         string value = attr.Value;
139
140                         try {
141                                 return bool.Parse (value);
142                         } catch (Exception ex) {
143                                 throw CreateInvalidValueException (attrName,
144                                         value, attr, ex);
145                         }
146                 }
147
148                 static EngineType ParseEngineType (XmlNode config, string attrName)
149                 {
150                         XmlAttribute attr = config.Attributes [attrName];
151                         if (attr == null)
152                                 throw CreateAttributeMissingException (attrName, config);
153
154                         string value = attr.Value;
155
156                         try {
157                                 return (EngineType) Enum.Parse (typeof (EngineType), value);
158                         } catch (Exception ex) {
159                                 throw CreateInvalidValueException (attrName,
160                                         value, attr, ex);
161                         }
162                 }
163
164                 static int ParseClientVersion (XmlNode config, string attrName)
165                 {
166                         XmlAttribute attr = config.Attributes [attrName];
167                         if (attr == null)
168                                 return -1;
169
170                         string value = attr.Value;
171
172                         try {
173                                 return Int32.Parse (value);
174                         } catch (Exception ex) {
175                                 throw CreateInvalidValueException (attrName,
176                                         value, attr, ex);
177                         }                       
178                 }
179
180                 static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
181                 {
182                         string msg = string.Format (CultureInfo.InvariantCulture,
183                                         "Invalid value '{0}' for attribute {1}.",
184                                         value, name);
185                         throw new ArgumentOutOfRangeException (msg, cause);
186                 }
187
188                 static Exception CreateAttributeMissingException (string name, XmlNode node)
189                 {
190                         string msg = string.Format (CultureInfo.InvariantCulture,
191                                 "Missing '{0}' attribute.", name);
192                         throw new ArgumentException (msg);
193                 }
194         }
195 }