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
30 using System.Data.Common;
31 using System.Data.SqlClient;
33 using NUnit.Framework;
35 namespace MonoTests.System.Data.Odbc
38 public class SqlCommandBuilderTest
41 public void CatalogLocationTest ()
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");
50 public void CatalogLocation_Value_Invalid ()
52 SqlCommandBuilder cb = new SqlCommandBuilder ();
54 cb.CatalogLocation = (CatalogLocation) 666;
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");
66 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#A7");
69 cb.CatalogLocation = CatalogLocation.End;
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");
81 Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#B8");
85 public void CatalogSeparator ()
87 SqlCommandBuilder cb = new SqlCommandBuilder ();
88 Assert.AreEqual (".", cb.CatalogSeparator, "#1");
92 public void CatalogSeparator_Value_Invalid ()
94 string [] separators = new string [] {
102 SqlCommandBuilder cb = new SqlCommandBuilder ();
103 for (int i = 0; i < separators.Length; i++) {
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);
121 public void ConflictOptionTest ()
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");
130 public void ConflictOption_Value_Invalid ()
132 SqlCommandBuilder cb = new SqlCommandBuilder ();
133 cb.ConflictOption = ConflictOption.CompareRowVersion;
135 cb.ConflictOption = (ConflictOption) 666;
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");
146 Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
149 [Test] // QuoteIdentifier (String)
150 public void QuoteIdentifier ()
152 SqlCommandBuilder cb;
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");
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");
176 public void QuoteIdentifier_PrefixSuffix_NoMatch ()
178 SqlCommandBuilder cb;
180 cb = new SqlCommandBuilder ();
181 cb.QuoteSuffix = "\"";
183 cb.QuoteIdentifier ("mono");
185 } catch (ArgumentException ex) {
186 // Specified QuotePrefix and QuoteSuffix values
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");
196 cb = new SqlCommandBuilder ();
197 cb.QuotePrefix = "\"";
199 cb.QuoteIdentifier ("mono");
201 } catch (ArgumentException ex) {
202 // Specified QuotePrefix and QuoteSuffix values
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");
213 [Test] // QuoteIdentifier (String)
214 public void QuoteIdentifier_UnquotedIdentifier_Null ()
216 SqlCommandBuilder cb = new SqlCommandBuilder ();
218 cb.QuoteIdentifier ((string) null);
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");
229 public void QuotePrefix ()
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");
243 public void QuotePrefix_Value_Invalid ()
245 string [] prefixes = new string [] {
254 SqlCommandBuilder cb = new SqlCommandBuilder ();
255 for (int i = 0; i < prefixes.Length; i++) {
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);
271 public void QuoteSuffix ()
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");
285 public void QuoteSuffix_Value_Invalid ()
287 string [] suffixes = new string [] {
296 SqlCommandBuilder cb = new SqlCommandBuilder ();
297 for (int i = 0; i < suffixes.Length; i++) {
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);
313 public void SchemaSeparator ()
315 SqlCommandBuilder cb = new SqlCommandBuilder ();
316 Assert.AreEqual (".", cb.SchemaSeparator, "#1");
317 cb.SchemaSeparator = ".";
318 Assert.AreEqual (".", cb.SchemaSeparator, "#2");
322 public void SchemaSeparator_Value_Invalid ()
324 string [] separators = new string [] {
332 SqlCommandBuilder cb = new SqlCommandBuilder ();
333 for (int i = 0; i < separators.Length; i++) {
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);