1 // OdbcCommandBuilderTest.cs - NUnit Test Cases for testing the
2 // OdbcCommandBuilder Test.
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (c) 2008 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person
10 // obtaining a copy of this software and associated documentation
11 // files (the "Software"), to deal in the Software without
12 // restriction, including without limitation the rights to use, copy,
13 // modify, merge, publish, distribute, sublicense, and/or sell copies
14 // of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 using System.Data.Common;
34 using System.Data.Odbc;
36 using NUnit.Framework;
38 namespace MonoTests.System.Data.Odbc
41 public class OdbcCommandBuilderTest
44 public void CatalogLocationTest ()
46 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
47 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
48 cb.CatalogLocation = CatalogLocation.End;
49 Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
53 public void CatalogLocation_Value_Invalid ()
55 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
56 cb.CatalogLocation = CatalogLocation.End;
58 cb.CatalogLocation = (CatalogLocation) 666;
60 } catch (ArgumentOutOfRangeException ex) {
61 // The CatalogLocation enumeration value, 666, is invalid
62 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
63 Assert.IsNull (ex.InnerException, "#3");
64 Assert.IsNotNull (ex.Message, "#4");
65 Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
66 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
68 Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#6");
72 public void CatalogSeparator ()
74 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
75 Assert.AreEqual (".", cb.CatalogSeparator, "#1");
76 cb.CatalogSeparator = "a";
77 Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
78 cb.CatalogSeparator = null;
79 Assert.AreEqual (".", cb.CatalogSeparator, "#3");
80 cb.CatalogSeparator = "b";
81 Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
82 cb.CatalogSeparator = string.Empty;
83 Assert.AreEqual (".", cb.CatalogSeparator, "#5");
84 cb.CatalogSeparator = " ";
85 Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
89 public void ConflictOptionTest ()
91 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
92 Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
93 cb.ConflictOption = ConflictOption.CompareRowVersion;
94 Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
98 public void ConflictOption_Value_Invalid ()
100 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
101 cb.ConflictOption = ConflictOption.CompareRowVersion;
103 cb.ConflictOption = (ConflictOption) 666;
105 } catch (ArgumentOutOfRangeException ex) {
106 // The ConflictOption enumeration value, 666, is invalid
107 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
108 Assert.IsNull (ex.InnerException, "#3");
109 Assert.IsNotNull (ex.Message, "#4");
110 Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
111 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
112 Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
114 Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
118 public void QuotePrefix ()
120 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
121 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
122 cb.QuotePrefix = "mono";
123 Assert.AreEqual ("mono", cb.QuotePrefix, "#2");
124 cb.QuotePrefix = null;
125 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#3");
126 cb.QuotePrefix = "'\"";
127 Assert.AreEqual ("'\"", cb.QuotePrefix, "#4");
128 cb.QuotePrefix = string.Empty;
129 Assert.AreEqual (string.Empty, cb.QuotePrefix, "#5");
130 cb.QuotePrefix = " ";
131 Assert.AreEqual (" ", cb.QuotePrefix, "#6");
135 public void QuoteSuffix ()
137 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
138 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
139 cb.QuoteSuffix = "mono";
140 Assert.AreEqual ("mono", cb.QuoteSuffix, "#2");
141 cb.QuoteSuffix = null;
142 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#3");
143 cb.QuoteSuffix = "'\"";
144 Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
145 cb.QuoteSuffix = string.Empty;
146 Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
147 cb.QuoteSuffix = " ";
148 Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
151 [Test] // QuoteIdentifier (String)
152 public void QuoteIdentifier1 ()
154 OdbcCommandBuilder cb;
156 cb = new OdbcCommandBuilder ();
157 cb.QuotePrefix = "aBc";
158 Assert.AreEqual ("aBcmoAbCno", cb.QuoteIdentifier ("moAbCno"), "#A1");
159 Assert.AreEqual ("aBc", cb.QuoteIdentifier (string.Empty), "#A2");
160 Assert.AreEqual ("aBcZ", cb.QuoteIdentifier ("Z"), "#A3");
161 Assert.AreEqual ("aBcabc", cb.QuoteIdentifier ("abc"), "#A4");
162 cb.QuoteSuffix = "deF";
163 Assert.AreEqual ("aBcmodEfnodeF", cb.QuoteIdentifier ("modEfno"), "#A5");
164 Assert.AreEqual ("aBcdeF", cb.QuoteIdentifier (string.Empty), "#A6");
165 Assert.AreEqual ("aBcZdeF", cb.QuoteIdentifier ("Z"), "#A7");
166 Assert.AreEqual ("aBcabcdeF", cb.QuoteIdentifier ("abc"), "#A8");
168 cb = new OdbcCommandBuilder ();
169 cb.QuotePrefix = "X";
170 Assert.AreEqual ("XmoXno", cb.QuoteIdentifier ("moXno"), "#B1");
171 Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty), "#B2");
172 Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z"), "#B3");
173 Assert.AreEqual ("XX", cb.QuoteIdentifier ("X"), "#B4");
174 cb.QuoteSuffix = " ";
175 Assert.AreEqual ("Xmo no ", cb.QuoteIdentifier ("mo no"), "#B5");
176 Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty), "#B6");
177 Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z"), "#B7");
178 Assert.AreEqual ("X ", cb.QuoteIdentifier (" "), "#B8");
180 cb = new OdbcCommandBuilder ();
181 cb.QuotePrefix = " ";
182 Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono"), "#C1");
183 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty), "#C2");
184 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z"), "#C3");
185 cb.QuoteSuffix = "dEf";
186 Assert.AreEqual ("modefno", cb.QuoteIdentifier ("modefno"), "#C4");
187 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty), "#C5");
188 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z"), "#C6");
190 cb = new OdbcCommandBuilder ();
191 cb.QuotePrefix = " ";
192 Assert.AreEqual (" mono", cb.QuoteIdentifier ("mono"), "#D1");
193 Assert.AreEqual (" ", cb.QuoteIdentifier (string.Empty), "#D2");
194 Assert.AreEqual (" Z", cb.QuoteIdentifier ("Z"), "#D3");
195 cb.QuoteSuffix = "dEf";
196 Assert.AreEqual (" moDeFnodEf", cb.QuoteIdentifier ("moDeFno"), "#D4");
197 Assert.AreEqual (" modEfdEfnodEf", cb.QuoteIdentifier ("modEfno"), "#D5");
198 Assert.AreEqual (" dEf", cb.QuoteIdentifier (string.Empty), "#D6");
199 Assert.AreEqual (" ZdEf", cb.QuoteIdentifier ("Z"), "#D7");
202 [Test] // QuoteIdentifier (String)
203 public void QuoteIdentifier1_QuotePrefix_Empty ()
205 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
207 cb.QuoteIdentifier ("mono");
209 } catch (InvalidOperationException ex) {
210 // QuoteIdentifier requires open connection when
211 // the quote prefix has not been set
212 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
213 Assert.IsNull (ex.InnerException, "#3");
214 Assert.IsNotNull (ex.Message, "#4");
218 [Test] // QuoteIdentifier (String)
219 public void QuoteIdentifier1_UnquotedIdentifier_Null ()
221 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
223 cb.QuoteIdentifier ((string) null);
225 } catch (ArgumentNullException ex) {
226 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
227 Assert.IsNull (ex.InnerException, "#3");
228 Assert.IsNotNull (ex.Message, "#4");
229 Assert.AreEqual ("unquotedIdentifier", ex.ParamName, "#5");
233 [Test] // QuoteIdentifier (String, OdbcConnection)
234 public void QuoteIdentifier2_Connection_Null ()
236 OdbcCommandBuilder cb;
237 OdbcConnection conn = null;
239 cb = new OdbcCommandBuilder ();
241 cb.QuoteIdentifier ("mono", conn);
243 } catch (InvalidOperationException ex) {
244 // QuoteIdentifier requires open connection when
245 // the quote prefix has not been set
246 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
247 Assert.IsNull (ex.InnerException, "#A3");
248 Assert.IsNotNull (ex.Message, "#A4");
251 cb = new OdbcCommandBuilder ();
252 cb.QuotePrefix = "abc";
253 Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
254 Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
255 Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
256 Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
257 Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
258 Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
259 cb.QuoteSuffix = "def";
260 Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
261 Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
262 Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
263 Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
264 Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
265 Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
267 cb.QuotePrefix = string.Empty;
269 cb.QuoteIdentifier ("mono", conn);
271 } catch (InvalidOperationException ex) {
272 // QuoteIdentifier requires open connection when
273 // the quote prefix has not been set
274 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
275 Assert.IsNull (ex.InnerException, "#C3");
276 Assert.IsNotNull (ex.Message, "#C4");
279 cb = new OdbcCommandBuilder ();
280 cb.QuotePrefix = "X";
281 Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono", conn), "#D1");
282 Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z", conn), "#D2");
283 Assert.AreEqual ("XX", cb.QuoteIdentifier ("X", conn), "#D3");
284 Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
285 Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
286 Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
287 cb.QuoteSuffix = " ";
288 Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono", conn), "#D7");
289 Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z", conn), "#D8");
290 Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X", conn), "#D9");
291 Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
292 Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
293 Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
295 cb = new OdbcCommandBuilder ();
296 cb.QuotePrefix = " ";
297 Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
298 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
299 Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
300 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
301 Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
302 Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
303 cb.QuoteSuffix = "def";
304 Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
305 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
306 Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
307 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
308 Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
309 Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
312 [Test] // QuoteIdentifier (String, OdbcConnection)
313 public void QuoteIdentifier2_Connection_Closed ()
315 OdbcCommandBuilder cb;
316 OdbcConnection conn = new OdbcConnection ();
318 cb = new OdbcCommandBuilder ();
320 cb.QuoteIdentifier ("mono", conn);
322 } catch (InvalidOperationException ex) {
323 // QuoteIdentifier requires an open and available
324 // Connection. The connection's current state is
326 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
327 Assert.IsNull (ex.InnerException, "#A3");
328 Assert.IsNotNull (ex.Message, "#A4");
331 cb = new OdbcCommandBuilder ();
332 cb.QuotePrefix = "abc";
333 Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
334 Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
335 Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
336 Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
337 Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
338 Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
339 cb.QuoteSuffix = "def";
340 Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
341 Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
342 Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
343 Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
344 Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
345 Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
347 cb.QuotePrefix = string.Empty;
349 cb.QuoteIdentifier ("mono");
351 } catch (InvalidOperationException ex) {
352 // QuoteIdentifier requires an open and available
353 // Connection. The connection's current state is
355 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
356 Assert.IsNull (ex.InnerException, "#C3");
357 Assert.IsNotNull (ex.Message, "#C4");
360 cb = new OdbcCommandBuilder ();
361 cb.QuotePrefix = "X";
362 Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono"), "#D1");
363 Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z"), "#D2");
364 Assert.AreEqual ("XX", cb.QuoteIdentifier ("X"), "#D3");
365 Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
366 Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
367 Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
368 cb.QuoteSuffix = " ";
369 Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono"), "#D7");
370 Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z"), "#D8");
371 Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X"), "#D9");
372 Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
373 Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
374 Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
376 cb = new OdbcCommandBuilder ();
377 cb.QuotePrefix = " ";
378 Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
379 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
380 Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
381 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
382 Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
383 Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
384 cb.QuoteSuffix = "def";
385 Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
386 Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
387 Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
388 Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
389 Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
390 Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
394 public void SchemaSeparator ()
396 OdbcCommandBuilder cb = new OdbcCommandBuilder ();
397 Assert.AreEqual (".", cb.SchemaSeparator, "#1");
398 cb.SchemaSeparator = "a";
399 Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
400 cb.SchemaSeparator = null;
401 Assert.AreEqual (".", cb.SchemaSeparator, "#3");
402 cb.SchemaSeparator = "b";
403 Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
404 cb.SchemaSeparator = string.Empty;
405 Assert.AreEqual (".", cb.SchemaSeparator, "#5");
406 cb.SchemaSeparator = " ";
407 Assert.AreEqual (" ", cb.SchemaSeparator, "#6");