1 // SqlCommandBuilderTest.cs - NUnit Test Cases for testing SqlCommandBuilder.
4 // Gert Driesen (drieseng@users.sourceforge.net)
6 // Copyright (c) 2008 Gert Driesen
8 // Permission is hereby granted, free of charge, to any person
9 // obtaining a copy of this software and associated documentation
10 // files (the "Software"), to deal in the Software without
11 // restriction, including without limitation the rights to use, copy,
12 // modify, merge, publish, distribute, sublicense, and/or sell copies
13 // of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 using System.Data.Common;
33 using System.Data.SqlClient;
35 using NUnit.Framework;
37 namespace MonoTests.System.Data.Odbc
40 public class SqlCommandBuilderTest
44 public void CatalogLocationTest ()
46 SqlCommandBuilder cb = new SqlCommandBuilder ();
47 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
48 cb.CatalogLocation = CatalogLocation.Start;
49 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#2");
53 public void CatalogLocation_Value_Invalid ()
55 SqlCommandBuilder cb = new SqlCommandBuilder ();
57 cb.CatalogLocation = (CatalogLocation) 666;
59 } catch (ArgumentException ex) {
60 // The only acceptable value for the property
61 // 'CatalogLocation' is 'Start'
62 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
63 Assert.IsNull (ex.InnerException, "#A3");
64 Assert.IsNotNull (ex.Message, "#A4");
65 Assert.IsTrue (ex.Message.IndexOf ("'CatalogLocation'") != -1, "#A5:" + ex.Message);
66 Assert.IsTrue (ex.Message.IndexOf ("'Start'") != -1, "#A6:" + ex.Message);
67 Assert.IsNull (ex.ParamName, "#A7");
69 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#A7");
72 cb.CatalogLocation = CatalogLocation.End;
74 } catch (ArgumentException ex) {
75 // The only acceptable value for the property
76 // 'CatalogLocation' is 'Start'
77 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
78 Assert.IsNull (ex.InnerException, "#B3");
79 Assert.IsNotNull (ex.Message, "#B4");
80 Assert.IsTrue (ex.Message.IndexOf ("'CatalogLocation'") != -1, "#B5:" + ex.Message);
81 Assert.IsTrue (ex.Message.IndexOf ("'Start'") != -1, "#B6:" + ex.Message);
82 Assert.IsNull (ex.ParamName, "#B7");
84 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#B8");
88 public void CatalogSeparator ()
90 SqlCommandBuilder cb = new SqlCommandBuilder ();
91 Assert.AreEqual (".", cb.CatalogSeparator, "#1");
95 public void CatalogSeparator_Value_Invalid ()
97 string [] separators = new string [] {
105 SqlCommandBuilder cb = new SqlCommandBuilder ();
106 for (int i = 0; i < separators.Length; i++) {
108 cb.CatalogSeparator = separators [i];
109 Assert.Fail ("#1:" + i);
110 } catch (ArgumentException ex) {
111 // The acceptable value for the property
112 // 'CatalogSeparator' is '.'
113 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
114 Assert.IsNull (ex.InnerException, "#3:" + i);
115 Assert.IsNotNull (ex.Message, "#4:" + i);
116 Assert.IsTrue (ex.Message.IndexOf ("'CatalogSeparator'") != -1, "#5:" + ex.Message);
117 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#6:" + ex.Message);
118 Assert.IsNull (ex.ParamName, "#7:" + i);
124 public void ConflictOptionTest ()
126 SqlCommandBuilder cb = new SqlCommandBuilder ();
127 Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
128 cb.ConflictOption = ConflictOption.CompareRowVersion;
129 Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
133 public void ConflictOption_Value_Invalid ()
135 SqlCommandBuilder cb = new SqlCommandBuilder ();
136 cb.ConflictOption = ConflictOption.CompareRowVersion;
138 cb.ConflictOption = (ConflictOption) 666;
140 } catch (ArgumentOutOfRangeException ex) {
141 // The ConflictOption enumeration value, 666, is invalid
142 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
143 Assert.IsNull (ex.InnerException, "#3");
144 Assert.IsNotNull (ex.Message, "#4");
145 Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
146 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
147 Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
149 Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
152 [Test] // QuoteIdentifier (String)
153 public void QuoteIdentifier ()
155 SqlCommandBuilder cb;
157 cb = new SqlCommandBuilder ();
158 Assert.AreEqual ("[mono]", cb.QuoteIdentifier ("mono"), "#A1");
159 Assert.AreEqual ("[]", cb.QuoteIdentifier (string.Empty), "#A2");
160 Assert.AreEqual ("[Z]", cb.QuoteIdentifier ("Z"), "#A3");
161 Assert.AreEqual ("[[]", cb.QuoteIdentifier ("["), "#A4");
162 Assert.AreEqual ("[A[C]", cb.QuoteIdentifier ("A[C"), "#A5");
163 Assert.AreEqual ("[]]]", cb.QuoteIdentifier ("]"), "#A6");
164 Assert.AreEqual ("[A]]C]", cb.QuoteIdentifier ("A]C"), "#A7");
165 Assert.AreEqual ("[[]]]", cb.QuoteIdentifier ("[]"), "#A8");
166 Assert.AreEqual ("[A[]]C]", cb.QuoteIdentifier ("A[]C"), "#A9");
168 cb = new SqlCommandBuilder ();
169 cb.QuotePrefix = "\"";
170 cb.QuoteSuffix = "\"";
171 Assert.AreEqual ("\"mono\"", cb.QuoteIdentifier ("mono"), "#B1");
172 Assert.AreEqual ("\"\"", cb.QuoteIdentifier (string.Empty), "#B2");
173 Assert.AreEqual ("\"Z\"", cb.QuoteIdentifier ("Z"), "#B3");
174 Assert.AreEqual ("\"\"\"\"", cb.QuoteIdentifier ("\""), "#B4");
175 Assert.AreEqual ("\"A\"\"C\"", cb.QuoteIdentifier ("A\"C"), "#B5");
179 public void QuoteIdentifier_PrefixSuffix_NoMatch ()
181 SqlCommandBuilder cb;
183 cb = new SqlCommandBuilder ();
184 cb.QuoteSuffix = "\"";
186 cb.QuoteIdentifier ("mono");
188 } catch (ArgumentException ex) {
189 // Specified QuotePrefix and QuoteSuffix values
191 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
192 Assert.IsNull (ex.InnerException, "#A3");
193 Assert.IsNotNull (ex.Message, "#A4");
194 Assert.IsTrue (ex.Message.IndexOf ("QuotePrefix") != -1, "#A5:" + ex.Message);
195 Assert.IsTrue (ex.Message.IndexOf ("QuoteSuffix") != -1, "#A6:" + ex.Message);
196 Assert.IsNull (ex.ParamName, "#A7");
199 cb = new SqlCommandBuilder ();
200 cb.QuotePrefix = "\"";
202 cb.QuoteIdentifier ("mono");
204 } catch (ArgumentException ex) {
205 // Specified QuotePrefix and QuoteSuffix values
207 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
208 Assert.IsNull (ex.InnerException, "#B3");
209 Assert.IsNotNull (ex.Message, "#B4");
210 Assert.IsTrue (ex.Message.IndexOf ("QuotePrefix") != -1, "#B5:" + ex.Message);
211 Assert.IsTrue (ex.Message.IndexOf ("QuoteSuffix") != -1, "#B6:" + ex.Message);
212 Assert.IsNull (ex.ParamName, "#B7");
216 [Test] // QuoteIdentifier (String)
217 public void QuoteIdentifier_UnquotedIdentifier_Null ()
219 SqlCommandBuilder cb = new SqlCommandBuilder ();
221 cb.QuoteIdentifier ((string) null);
223 } catch (ArgumentNullException ex) {
224 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
225 Assert.IsNull (ex.InnerException, "#3");
226 Assert.IsNotNull (ex.Message, "#4");
227 Assert.AreEqual ("unquotedIdentifier", ex.ParamName, "#5");
233 public void QuotePrefix ()
235 SqlCommandBuilder cb = new SqlCommandBuilder ();
237 Assert.AreEqual ("[", cb.QuotePrefix, "#A1");
238 Assert.AreEqual ("]", cb.QuoteSuffix, "#A2");
239 cb.QuotePrefix = "\"";
240 Assert.AreEqual ("\"", cb.QuotePrefix, "#B1");
241 Assert.AreEqual ("]", cb.QuoteSuffix, "#B2");
242 cb.QuotePrefix = "[";
243 Assert.AreEqual ("[", cb.QuotePrefix, "#C1");
244 Assert.AreEqual ("]", cb.QuoteSuffix, "#C2");
246 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#A1");
247 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#A2");
248 cb.QuotePrefix = "\"";
249 Assert.AreEqual ("\"", cb.QuotePrefix, "#B1");
250 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#B2");
251 cb.QuotePrefix = string.Empty;
252 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#C1");
253 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#C2");
254 cb.QuotePrefix = "x";
255 Assert.AreEqual ("x", cb.QuotePrefix, "#D1");
256 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#D2");
257 cb.QuotePrefix = null;
258 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#E1");
259 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#E2");
260 cb.QuotePrefix = "mono";
261 Assert.AreEqual ("mono", cb.QuotePrefix, "#F1");
262 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#F2");
263 cb.QuotePrefix = " ";
264 Assert.AreEqual (" ", cb.QuotePrefix, "#G1");
265 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#G2");
271 public void QuotePrefix_Value_Invalid ()
273 string [] prefixes = new string [] {
282 SqlCommandBuilder cb = new SqlCommandBuilder ();
283 for (int i = 0; i < prefixes.Length; i++) {
285 cb.QuotePrefix = prefixes [i];
286 Assert.Fail ("#1:" + i);
287 } catch (ArgumentException ex) {
288 // The acceptable values for the property
289 // 'QuoteSuffix' are ']' or '"'
290 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
291 Assert.IsNull (ex.InnerException, "#3:" + i);
292 Assert.IsNotNull (ex.Message, "#4:" + i);
293 Assert.IsNull (ex.ParamName, "#5:" + i);
300 public void QuoteSuffix ()
302 SqlCommandBuilder cb = new SqlCommandBuilder ();
304 Assert.AreEqual ("[", cb.QuotePrefix, "#A1");
305 Assert.AreEqual ("]", cb.QuoteSuffix, "#A2");
306 cb.QuoteSuffix = "\"";
307 Assert.AreEqual ("[", cb.QuotePrefix, "#B1");
308 Assert.AreEqual ("\"", cb.QuoteSuffix, "#B2");
309 cb.QuoteSuffix = "]";
310 Assert.AreEqual ("[", cb.QuotePrefix, "#C1");
311 Assert.AreEqual ("]", cb.QuoteSuffix, "#C2");
313 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#A1");
314 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#A2");
315 cb.QuoteSuffix = "\"";
316 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#B1");
317 Assert.AreEqual ("\"", cb.QuoteSuffix, "#B2");
318 cb.QuoteSuffix = string.Empty;
319 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#C1");
320 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#C2");
321 cb.QuoteSuffix = "x";
322 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#D1");
323 Assert.AreEqual ("x", cb.QuoteSuffix, "#D2");
324 cb.QuoteSuffix = null;
325 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#E1");
326 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#E2");
327 cb.QuoteSuffix = "mono";
328 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#F1");
329 Assert.AreEqual ("mono", cb.QuoteSuffix, "#F2");
330 cb.QuoteSuffix = " ";
331 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#G1");
332 Assert.AreEqual (" ", cb.QuoteSuffix, "#G2");
338 public void QuoteSuffix_Value_Invalid ()
340 string [] suffixes = new string [] {
349 SqlCommandBuilder cb = new SqlCommandBuilder ();
350 for (int i = 0; i < suffixes.Length; i++) {
352 cb.QuoteSuffix = suffixes [i];
353 Assert.Fail ("#1:" + i);
354 } catch (ArgumentException ex) {
355 // The acceptable values for the property
356 // 'QuoteSuffix' are ']' or '"'
357 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
358 Assert.IsNull (ex.InnerException, "#3:" + i);
359 Assert.IsNotNull (ex.Message, "#4:" + i);
360 Assert.IsNull (ex.ParamName, "#5:" + i);
366 public void SchemaSeparator ()
368 SqlCommandBuilder cb = new SqlCommandBuilder ();
369 Assert.AreEqual (".", cb.SchemaSeparator, "#1");
370 cb.SchemaSeparator = ".";
371 Assert.AreEqual (".", cb.SchemaSeparator, "#2");
375 public void SchemaSeparator_Value_Invalid ()
377 string [] separators = new string [] {
385 SqlCommandBuilder cb = new SqlCommandBuilder ();
386 for (int i = 0; i < separators.Length; i++) {
388 cb.SchemaSeparator = separators [i];
389 Assert.Fail ("#1:" + i);
390 } catch (ArgumentException ex) {
391 // The acceptable value for the property
392 // 'SchemaSeparator' is '.'
393 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
394 Assert.IsNull (ex.InnerException, "#3:" + i);
395 Assert.IsNotNull (ex.Message, "#4:" + i);
396 Assert.IsTrue (ex.Message.IndexOf ("'SchemaSeparator'") != -1, "#5:" + ex.Message);
397 Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#6:" + ex.Message);
398 Assert.IsNull (ex.ParamName, "#7:" + i);