Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbCommandBuilderTest.cs
1 // DbCommandBuilderTest.cs - NUnit Test Cases for DbCommandBuilder class
2 //
3 // Author: 
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 obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // 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 BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28 using System;
29 using System.Data;
30 using System.Data.Common;
31 using System.Globalization;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Data.Common
36 {
37         [TestFixture]
38         public class DbCommandBuilderTest
39         {
40                 [Test]
41                 public void CatalogLocationTest ()
42                 {
43                         MyCommandBuilder cb = new MyCommandBuilder ();
44                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
45                         cb.CatalogLocation = CatalogLocation.End;
46                         Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
47                 }
48
49                 [Test]
50                 public void CatalogLocation_Value_Invalid ()
51                 {
52                         MyCommandBuilder cb = new MyCommandBuilder ();
53                         cb.CatalogLocation = CatalogLocation.End;
54                         try {
55                                 cb.CatalogLocation = (CatalogLocation) 666;
56                                 Assert.Fail ("#1");
57                         } catch (ArgumentOutOfRangeException ex) {
58                                 // The CatalogLocation enumeration value, 666, is invalid
59                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
60                                 Assert.IsNull (ex.InnerException, "#3");
61                                 Assert.IsNotNull (ex.Message, "#4");
62                                 Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
63                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
64                                 Assert.AreEqual ("CatalogLocation", ex.ParamName, "#7");
65                         }
66                         Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#8");
67                 }
68
69                 [Test]
70                 public void CatalogSeparator ()
71                 {
72                         MyCommandBuilder cb = new MyCommandBuilder ();
73                         Assert.AreEqual (".", cb.CatalogSeparator, "#1");
74                         cb.CatalogSeparator = "a";
75                         Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
76                         cb.CatalogSeparator = null;
77                         Assert.AreEqual (".", cb.CatalogSeparator, "#3");
78                         cb.CatalogSeparator = "b";
79                         Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
80                         cb.CatalogSeparator = string.Empty;
81                         Assert.AreEqual (".", cb.CatalogSeparator, "#5");
82                         cb.CatalogSeparator = " ";
83                         Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
84                 }
85
86                 [Test]
87                 public void ConflictOptionTest ()
88                 {
89                         MyCommandBuilder cb = new MyCommandBuilder ();
90                         Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
91                         cb.ConflictOption = ConflictOption.CompareRowVersion;
92                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
93                 }
94
95                 [Test]
96                 public void ConflictOption_Value_Invalid ()
97                 {
98                         MyCommandBuilder cb = new MyCommandBuilder ();
99                         cb.ConflictOption = ConflictOption.CompareRowVersion;
100                         try {
101                                 cb.ConflictOption = (ConflictOption) 666;
102                                 Assert.Fail ("#1");
103                         } catch (ArgumentOutOfRangeException ex) {
104                                 // The ConflictOption enumeration value, 666, is invalid
105                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
106                                 Assert.IsNull (ex.InnerException, "#3");
107                                 Assert.IsNotNull (ex.Message, "#4");
108                                 Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
109                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
110                                 Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
111                         }
112                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
113                 }
114
115                 [Test] // QuoteIdentifier (String)
116                 public void QuoteIdentifier ()
117                 {
118                         MyCommandBuilder cb = new MyCommandBuilder ();
119                         try {
120                                 cb.QuoteIdentifier ((string) null);
121                                 Assert.Fail ("#A1");
122                         } catch (NotSupportedException ex) {
123                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
124                                 Assert.IsNull (ex.InnerException, "#A3");
125                                 Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#A4");
126                         }
127
128                         try {
129                                 cb.QuoteIdentifier ("mono");
130                                 Assert.Fail ("#B1");
131                         } catch (NotSupportedException ex) {
132                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
133                                 Assert.IsNull (ex.InnerException, "#B3");
134                                 Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#B4");
135                         }
136                 }
137
138                 [Test]
139                 public void QuotePrefix ()
140                 {
141                         MyCommandBuilder cb = new MyCommandBuilder ();
142                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
143                         cb.QuotePrefix = "mono";
144                         Assert.AreEqual ("mono", cb.QuotePrefix, "#2");
145                         cb.QuotePrefix = null;
146                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#3");
147                         cb.QuotePrefix = "'\"";
148                         Assert.AreEqual ("'\"", cb.QuotePrefix, "#4");
149                         cb.QuotePrefix = string.Empty;
150                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#5");
151                         cb.QuotePrefix = " ";
152                         Assert.AreEqual (" ", cb.QuotePrefix, "#6");
153                 }
154
155                 [Test]
156                 public void QuoteSuffix ()
157                 {
158                         MyCommandBuilder cb = new MyCommandBuilder ();
159                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
160                         cb.QuoteSuffix = "mono";
161                         Assert.AreEqual ("mono", cb.QuoteSuffix, "#2");
162                         cb.QuoteSuffix = null;
163                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#3");
164                         cb.QuoteSuffix = "'\"";
165                         Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
166                         cb.QuoteSuffix = string.Empty;
167                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
168                         cb.QuoteSuffix = " ";
169                         Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
170                 }
171
172                 [Test]
173                 public void SchemaSeparator ()
174                 {
175                         MyCommandBuilder cb = new MyCommandBuilder ();
176                         Assert.AreEqual (".", cb.SchemaSeparator, "#1");
177                         cb.SchemaSeparator = "a";
178                         Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
179                         cb.SchemaSeparator = null;
180                         Assert.AreEqual (".", cb.SchemaSeparator, "#3");
181                         cb.SchemaSeparator = "b";
182                         Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
183                         cb.SchemaSeparator = string.Empty;
184                         Assert.AreEqual (".", cb.SchemaSeparator, "#5");
185                         cb.SchemaSeparator = " ";
186                         Assert.AreEqual (" ", cb.SchemaSeparator, "#6");
187                 }
188
189                 private class MyCommandBuilder : DbCommandBuilder
190                 {
191                         protected override string GetParameterPlaceholder (int parameterOrdinal)
192                         {
193                                 return string.Format (CultureInfo.InvariantCulture,
194                                         "@PH:{0}@", parameterOrdinal);
195                         }
196
197                         protected override string GetParameterName (string parameterName)
198                         {
199                                 return string.Format (CultureInfo.InvariantCulture,
200                                         "@NAME:{0}@", parameterName);
201                         }
202
203                         protected override string GetParameterName (int parameterOrdinal)
204                         {
205                                 return string.Format (CultureInfo.InvariantCulture,
206                                         "@NAME:{0}@", parameterOrdinal);
207                         }
208
209                         protected override void ApplyParameterInfo (DbParameter parameter, DataRow row, StatementType statementType, bool whereClause)
210                         {
211                         }
212
213                         protected override void SetRowUpdatingHandler (DbDataAdapter adapter)
214                         {
215                         }
216                 }
217         }
218 }