Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlClient / SqlCommandBuilderTest.cs
1 // SqlCommandBuilderTest.cs - NUnit Test Cases for testing SqlCommandBuilder.
2 //
3 // Authors:
4 //      Gert Driesen (drieseng@users.sourceforge.net)
5 // 
6 // Copyright (c) 2008 Gert Driesen
7 //
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:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
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
26 // SOFTWARE.
27
28 using System;
29 #if NET_2_0
30 using System.Data;
31 using System.Data.Common;
32 #endif
33 using System.Data.SqlClient;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Data.Odbc
38 {
39         [TestFixture]
40         public class SqlCommandBuilderTest
41         {
42 #if NET_2_0
43                 [Test]
44                 public void CatalogLocationTest ()
45                 {
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");
50                 }
51
52                 [Test]
53                 public void CatalogLocation_Value_Invalid ()
54                 {
55                         SqlCommandBuilder cb = new SqlCommandBuilder ();
56                         try {
57                                 cb.CatalogLocation = (CatalogLocation) 666;
58                                 Assert.Fail ("#A1");
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");
68                         }
69                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#A7");
70
71                         try {
72                                 cb.CatalogLocation = CatalogLocation.End;
73                                 Assert.Fail ("#B1");
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");
83                         }
84                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#B8");
85                 }
86
87                 [Test]
88                 public void CatalogSeparator ()
89                 {
90                         SqlCommandBuilder cb = new SqlCommandBuilder ();
91                         Assert.AreEqual (".", cb.CatalogSeparator, "#1");
92                 }
93
94                 [Test]
95                 public void CatalogSeparator_Value_Invalid ()
96                 {
97                         string [] separators = new string [] {
98                                 "x",
99                                 "'",
100                                 "[x",
101                                 string.Empty,
102                                 null
103                                 };
104
105                         SqlCommandBuilder cb = new SqlCommandBuilder ();
106                         for (int i = 0; i < separators.Length; i++) {
107                                 try {
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);
119                                 }
120                         }
121                 }
122
123                 [Test]
124                 public void ConflictOptionTest ()
125                 {
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");
130                 }
131
132                 [Test]
133                 public void ConflictOption_Value_Invalid ()
134                 {
135                         SqlCommandBuilder cb = new SqlCommandBuilder ();
136                         cb.ConflictOption = ConflictOption.CompareRowVersion;
137                         try {
138                                 cb.ConflictOption = (ConflictOption) 666;
139                                 Assert.Fail ("#1");
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");
148                         }
149                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
150                 }
151
152                 [Test] // QuoteIdentifier (String)
153                 public void QuoteIdentifier ()
154                 {
155                         SqlCommandBuilder cb;
156                 
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");
167
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");
176                 }
177
178                 [Test]
179                 public void QuoteIdentifier_PrefixSuffix_NoMatch ()
180                 {
181                         SqlCommandBuilder cb;
182                 
183                         cb = new SqlCommandBuilder ();
184                         cb.QuoteSuffix = "\"";
185                         try {
186                                 cb.QuoteIdentifier ("mono");
187                                 Assert.Fail ("#A1");
188                         } catch (ArgumentException ex) {
189                                 // Specified QuotePrefix and QuoteSuffix values
190                                 // do not match
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");
197                         }
198
199                         cb = new SqlCommandBuilder ();
200                         cb.QuotePrefix = "\"";
201                         try {
202                                 cb.QuoteIdentifier ("mono");
203                                 Assert.Fail ("#B1");
204                         } catch (ArgumentException ex) {
205                                 // Specified QuotePrefix and QuoteSuffix values
206                                 // do not match
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");
213                         }
214                 }
215
216                 [Test] // QuoteIdentifier (String)
217                 public void QuoteIdentifier_UnquotedIdentifier_Null ()
218                 {
219                         SqlCommandBuilder cb = new SqlCommandBuilder ();
220                         try {
221                                 cb.QuoteIdentifier ((string) null);
222                                 Assert.Fail ("#1");
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");
228                         }
229                 }
230 #endif
231
232                 [Test]
233                 public void QuotePrefix ()
234                 {
235                         SqlCommandBuilder cb = new SqlCommandBuilder ();
236 #if NET_2_0
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");
245 #else
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");
266 #endif
267                 }
268
269 #if NET_2_0
270                 [Test]
271                 public void QuotePrefix_Value_Invalid ()
272                 {
273                         string [] prefixes = new string [] {
274                                 "x",
275                                 "'",
276                                 "[x",
277                                 string.Empty,
278                                 null,
279                                 "]"
280                                 };
281
282                         SqlCommandBuilder cb = new SqlCommandBuilder ();
283                         for (int i = 0; i < prefixes.Length; i++) {
284                                 try {
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);
294                                 }
295                         }
296                 }
297 #endif
298
299                 [Test]
300                 public void QuoteSuffix ()
301                 {
302                         SqlCommandBuilder cb = new SqlCommandBuilder ();
303 #if NET_2_0
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");
312 #else
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");
333 #endif
334                 }
335
336 #if NET_2_0
337                 [Test]
338                 public void QuoteSuffix_Value_Invalid ()
339                 {
340                         string [] suffixes = new string [] {
341                                 "x",
342                                 "'",
343                                 "[x",
344                                 string.Empty,
345                                 null,
346                                 "["
347                                 };
348
349                         SqlCommandBuilder cb = new SqlCommandBuilder ();
350                         for (int i = 0; i < suffixes.Length; i++) {
351                                 try {
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);
361                                 }
362                         }
363                 }
364
365                 [Test]
366                 public void SchemaSeparator ()
367                 {
368                         SqlCommandBuilder cb = new SqlCommandBuilder ();
369                         Assert.AreEqual (".", cb.SchemaSeparator, "#1");
370                         cb.SchemaSeparator = ".";
371                         Assert.AreEqual (".", cb.SchemaSeparator, "#2");
372                 }
373
374                 [Test]
375                 public void SchemaSeparator_Value_Invalid ()
376                 {
377                         string [] separators = new string [] {
378                                 "x",
379                                 "'",
380                                 "[x",
381                                 string.Empty,
382                                 null
383                                 };
384
385                         SqlCommandBuilder cb = new SqlCommandBuilder ();
386                         for (int i = 0; i < separators.Length; i++) {
387                                 try {
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);
399                                 }
400                         }
401                 }
402 #endif
403         }
404 }