Merge pull request #1222 from LogosBible/uri-trycreate
[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 using System.Data;
30 using System.Data.Common;
31 using System.Data.SqlClient;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Data.Odbc
36 {
37         [TestFixture]
38         public class SqlCommandBuilderTest
39         {
40                 [Test]
41                 public void CatalogLocationTest ()
42                 {
43                         SqlCommandBuilder cb = new SqlCommandBuilder ();
44                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
45                         cb.CatalogLocation = CatalogLocation.Start;
46                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#2");
47                 }
48
49                 [Test]
50                 public void CatalogLocation_Value_Invalid ()
51                 {
52                         SqlCommandBuilder cb = new SqlCommandBuilder ();
53                         try {
54                                 cb.CatalogLocation = (CatalogLocation) 666;
55                                 Assert.Fail ("#A1");
56                         } catch (ArgumentException ex) {
57                                 // The only acceptable value for the property
58                                 // 'CatalogLocation' is 'Start'
59                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
60                                 Assert.IsNull (ex.InnerException, "#A3");
61                                 Assert.IsNotNull (ex.Message, "#A4");
62                                 Assert.IsTrue (ex.Message.IndexOf ("'CatalogLocation'") != -1, "#A5:" + ex.Message);
63                                 Assert.IsTrue (ex.Message.IndexOf ("'Start'") != -1, "#A6:" + ex.Message);
64                                 Assert.IsNull (ex.ParamName, "#A7");
65                         }
66                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#A7");
67
68                         try {
69                                 cb.CatalogLocation = CatalogLocation.End;
70                                 Assert.Fail ("#B1");
71                         } catch (ArgumentException ex) {
72                                 // The only acceptable value for the property
73                                 // 'CatalogLocation' is 'Start'
74                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
75                                 Assert.IsNull (ex.InnerException, "#B3");
76                                 Assert.IsNotNull (ex.Message, "#B4");
77                                 Assert.IsTrue (ex.Message.IndexOf ("'CatalogLocation'") != -1, "#B5:" + ex.Message);
78                                 Assert.IsTrue (ex.Message.IndexOf ("'Start'") != -1, "#B6:" + ex.Message);
79                                 Assert.IsNull (ex.ParamName, "#B7");
80                         }
81                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#B8");
82                 }
83
84                 [Test]
85                 public void CatalogSeparator ()
86                 {
87                         SqlCommandBuilder cb = new SqlCommandBuilder ();
88                         Assert.AreEqual (".", cb.CatalogSeparator, "#1");
89                 }
90
91                 [Test]
92                 public void CatalogSeparator_Value_Invalid ()
93                 {
94                         string [] separators = new string [] {
95                                 "x",
96                                 "'",
97                                 "[x",
98                                 string.Empty,
99                                 null
100                                 };
101
102                         SqlCommandBuilder cb = new SqlCommandBuilder ();
103                         for (int i = 0; i < separators.Length; i++) {
104                                 try {
105                                         cb.CatalogSeparator = separators [i];
106                                         Assert.Fail ("#1:" + i);
107                                 } catch (ArgumentException ex) {
108                                         // The acceptable value for the property
109                                         // 'CatalogSeparator' is '.'
110                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
111                                         Assert.IsNull (ex.InnerException, "#3:" + i);
112                                         Assert.IsNotNull (ex.Message, "#4:" + i);
113                                         Assert.IsTrue (ex.Message.IndexOf ("'CatalogSeparator'") != -1, "#5:" + ex.Message);
114                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#6:" + ex.Message);
115                                         Assert.IsNull (ex.ParamName, "#7:" + i);
116                                 }
117                         }
118                 }
119
120                 [Test]
121                 public void ConflictOptionTest ()
122                 {
123                         SqlCommandBuilder cb = new SqlCommandBuilder ();
124                         Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
125                         cb.ConflictOption = ConflictOption.CompareRowVersion;
126                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
127                 }
128
129                 [Test]
130                 public void ConflictOption_Value_Invalid ()
131                 {
132                         SqlCommandBuilder cb = new SqlCommandBuilder ();
133                         cb.ConflictOption = ConflictOption.CompareRowVersion;
134                         try {
135                                 cb.ConflictOption = (ConflictOption) 666;
136                                 Assert.Fail ("#1");
137                         } catch (ArgumentOutOfRangeException ex) {
138                                 // The ConflictOption enumeration value, 666, is invalid
139                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
140                                 Assert.IsNull (ex.InnerException, "#3");
141                                 Assert.IsNotNull (ex.Message, "#4");
142                                 Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
143                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
144                                 Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
145                         }
146                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
147                 }
148
149                 [Test] // QuoteIdentifier (String)
150                 public void QuoteIdentifier ()
151                 {
152                         SqlCommandBuilder cb;
153                 
154                         cb = new SqlCommandBuilder ();
155                         Assert.AreEqual ("[mono]", cb.QuoteIdentifier ("mono"), "#A1");
156                         Assert.AreEqual ("[]", cb.QuoteIdentifier (string.Empty), "#A2");
157                         Assert.AreEqual ("[Z]", cb.QuoteIdentifier ("Z"), "#A3");
158                         Assert.AreEqual ("[[]", cb.QuoteIdentifier ("["), "#A4");
159                         Assert.AreEqual ("[A[C]", cb.QuoteIdentifier ("A[C"), "#A5");
160                         Assert.AreEqual ("[]]]", cb.QuoteIdentifier ("]"), "#A6");
161                         Assert.AreEqual ("[A]]C]", cb.QuoteIdentifier ("A]C"), "#A7");
162                         Assert.AreEqual ("[[]]]", cb.QuoteIdentifier ("[]"), "#A8");
163                         Assert.AreEqual ("[A[]]C]", cb.QuoteIdentifier ("A[]C"), "#A9");
164
165                         cb = new SqlCommandBuilder ();
166                         cb.QuotePrefix = "\"";
167                         cb.QuoteSuffix = "\"";
168                         Assert.AreEqual ("\"mono\"", cb.QuoteIdentifier ("mono"), "#B1");
169                         Assert.AreEqual ("\"\"", cb.QuoteIdentifier (string.Empty), "#B2");
170                         Assert.AreEqual ("\"Z\"", cb.QuoteIdentifier ("Z"), "#B3");
171                         Assert.AreEqual ("\"\"\"\"", cb.QuoteIdentifier ("\""), "#B4");
172                         Assert.AreEqual ("\"A\"\"C\"", cb.QuoteIdentifier ("A\"C"), "#B5");
173                 }
174
175                 [Test]
176                 public void QuoteIdentifier_PrefixSuffix_NoMatch ()
177                 {
178                         SqlCommandBuilder cb;
179                 
180                         cb = new SqlCommandBuilder ();
181                         cb.QuoteSuffix = "\"";
182                         try {
183                                 cb.QuoteIdentifier ("mono");
184                                 Assert.Fail ("#A1");
185                         } catch (ArgumentException ex) {
186                                 // Specified QuotePrefix and QuoteSuffix values
187                                 // do not match
188                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
189                                 Assert.IsNull (ex.InnerException, "#A3");
190                                 Assert.IsNotNull (ex.Message, "#A4");
191                                 Assert.IsTrue (ex.Message.IndexOf ("QuotePrefix") != -1, "#A5:" + ex.Message);
192                                 Assert.IsTrue (ex.Message.IndexOf ("QuoteSuffix") != -1, "#A6:" + ex.Message);
193                                 Assert.IsNull (ex.ParamName, "#A7");
194                         }
195
196                         cb = new SqlCommandBuilder ();
197                         cb.QuotePrefix = "\"";
198                         try {
199                                 cb.QuoteIdentifier ("mono");
200                                 Assert.Fail ("#B1");
201                         } catch (ArgumentException ex) {
202                                 // Specified QuotePrefix and QuoteSuffix values
203                                 // do not match
204                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
205                                 Assert.IsNull (ex.InnerException, "#B3");
206                                 Assert.IsNotNull (ex.Message, "#B4");
207                                 Assert.IsTrue (ex.Message.IndexOf ("QuotePrefix") != -1, "#B5:" + ex.Message);
208                                 Assert.IsTrue (ex.Message.IndexOf ("QuoteSuffix") != -1, "#B6:" + ex.Message);
209                                 Assert.IsNull (ex.ParamName, "#B7");
210                         }
211                 }
212
213                 [Test] // QuoteIdentifier (String)
214                 public void QuoteIdentifier_UnquotedIdentifier_Null ()
215                 {
216                         SqlCommandBuilder cb = new SqlCommandBuilder ();
217                         try {
218                                 cb.QuoteIdentifier ((string) null);
219                                 Assert.Fail ("#1");
220                         } catch (ArgumentNullException ex) {
221                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
222                                 Assert.IsNull (ex.InnerException, "#3");
223                                 Assert.IsNotNull (ex.Message, "#4");
224                                 Assert.AreEqual ("unquotedIdentifier", ex.ParamName, "#5");
225                         }
226                 }
227
228                 [Test]
229                 public void QuotePrefix ()
230                 {
231                         SqlCommandBuilder cb = new SqlCommandBuilder ();
232                         Assert.AreEqual ("[", cb.QuotePrefix, "#A1");
233                         Assert.AreEqual ("]", cb.QuoteSuffix, "#A2");
234                         cb.QuotePrefix = "\"";
235                         Assert.AreEqual ("\"", cb.QuotePrefix, "#B1");
236                         Assert.AreEqual ("]", cb.QuoteSuffix, "#B2");
237                         cb.QuotePrefix = "[";
238                         Assert.AreEqual ("[", cb.QuotePrefix, "#C1");
239                         Assert.AreEqual ("]", cb.QuoteSuffix, "#C2");
240                 }
241
242                 [Test]
243                 public void QuotePrefix_Value_Invalid ()
244                 {
245                         string [] prefixes = new string [] {
246                                 "x",
247                                 "'",
248                                 "[x",
249                                 string.Empty,
250                                 null,
251                                 "]"
252                                 };
253
254                         SqlCommandBuilder cb = new SqlCommandBuilder ();
255                         for (int i = 0; i < prefixes.Length; i++) {
256                                 try {
257                                         cb.QuotePrefix = prefixes [i];
258                                         Assert.Fail ("#1:" + i);
259                                 } catch (ArgumentException ex) {
260                                         // The acceptable values for the property
261                                         // 'QuoteSuffix' are ']' or '"'
262                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
263                                         Assert.IsNull (ex.InnerException, "#3:" + i);
264                                         Assert.IsNotNull (ex.Message, "#4:" + i);
265                                         Assert.IsNull (ex.ParamName, "#5:" + i);
266                                 }
267                         }
268                 }
269
270                 [Test]
271                 public void QuoteSuffix ()
272                 {
273                         SqlCommandBuilder cb = new SqlCommandBuilder ();
274                         Assert.AreEqual ("[", cb.QuotePrefix, "#A1");
275                         Assert.AreEqual ("]", cb.QuoteSuffix, "#A2");
276                         cb.QuoteSuffix = "\"";
277                         Assert.AreEqual ("[", cb.QuotePrefix, "#B1");
278                         Assert.AreEqual ("\"", cb.QuoteSuffix, "#B2");
279                         cb.QuoteSuffix = "]";
280                         Assert.AreEqual ("[", cb.QuotePrefix, "#C1");
281                         Assert.AreEqual ("]", cb.QuoteSuffix, "#C2");
282                 }
283
284                 [Test]
285                 public void QuoteSuffix_Value_Invalid ()
286                 {
287                         string [] suffixes = new string [] {
288                                 "x",
289                                 "'",
290                                 "[x",
291                                 string.Empty,
292                                 null,
293                                 "["
294                                 };
295
296                         SqlCommandBuilder cb = new SqlCommandBuilder ();
297                         for (int i = 0; i < suffixes.Length; i++) {
298                                 try {
299                                         cb.QuoteSuffix = suffixes [i];
300                                         Assert.Fail ("#1:" + i);
301                                 } catch (ArgumentException ex) {
302                                         // The acceptable values for the property
303                                         // 'QuoteSuffix' are ']' or '"'
304                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
305                                         Assert.IsNull (ex.InnerException, "#3:" + i);
306                                         Assert.IsNotNull (ex.Message, "#4:" + i);
307                                         Assert.IsNull (ex.ParamName, "#5:" + i);
308                                 }
309                         }
310                 }
311
312                 [Test]
313                 public void SchemaSeparator ()
314                 {
315                         SqlCommandBuilder cb = new SqlCommandBuilder ();
316                         Assert.AreEqual (".", cb.SchemaSeparator, "#1");
317                         cb.SchemaSeparator = ".";
318                         Assert.AreEqual (".", cb.SchemaSeparator, "#2");
319                 }
320
321                 [Test]
322                 public void SchemaSeparator_Value_Invalid ()
323                 {
324                         string [] separators = new string [] {
325                                 "x",
326                                 "'",
327                                 "[x",
328                                 string.Empty,
329                                 null
330                                 };
331
332                         SqlCommandBuilder cb = new SqlCommandBuilder ();
333                         for (int i = 0; i < separators.Length; i++) {
334                                 try {
335                                         cb.SchemaSeparator = separators [i];
336                                         Assert.Fail ("#1:" + i);
337                                 } catch (ArgumentException ex) {
338                                         // The acceptable value for the property
339                                         // 'SchemaSeparator' is '.'
340                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + i);
341                                         Assert.IsNull (ex.InnerException, "#3:" + i);
342                                         Assert.IsNotNull (ex.Message, "#4:" + i);
343                                         Assert.IsTrue (ex.Message.IndexOf ("'SchemaSeparator'") != -1, "#5:" + ex.Message);
344                                         Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#6:" + ex.Message);
345                                         Assert.IsNull (ex.ParamName, "#7:" + i);
346                                 }
347                         }
348                 }
349         }
350 }