Merge pull request #347 from JamesB7/master
[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 #if NET_2_0
29 using System;
30 using System.Data;
31 using System.Data.Common;
32 using System.Globalization;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Data.Common
37 {
38         [TestFixture]
39         public class DbCommandBuilderTest
40         {
41                 [Test]
42                 public void CatalogLocationTest ()
43                 {
44                         MyCommandBuilder cb = new MyCommandBuilder ();
45                         Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
46                         cb.CatalogLocation = CatalogLocation.End;
47                         Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#2");
48                 }
49
50                 [Test]
51                 public void CatalogLocation_Value_Invalid ()
52                 {
53                         MyCommandBuilder cb = new MyCommandBuilder ();
54                         cb.CatalogLocation = CatalogLocation.End;
55                         try {
56                                 cb.CatalogLocation = (CatalogLocation) 666;
57                                 Assert.Fail ("#1");
58                         } catch (ArgumentOutOfRangeException ex) {
59                                 // The CatalogLocation enumeration value, 666, is invalid
60                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
61                                 Assert.IsNull (ex.InnerException, "#3");
62                                 Assert.IsNotNull (ex.Message, "#4");
63                                 Assert.IsTrue (ex.Message.IndexOf ("CatalogLocation") != -1, "#5:" + ex.Message);
64                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
65                                 Assert.AreEqual ("CatalogLocation", ex.ParamName, "#7");
66                         }
67                         Assert.AreEqual (CatalogLocation.End, cb.CatalogLocation, "#8");
68                 }
69
70                 [Test]
71                 public void CatalogSeparator ()
72                 {
73                         MyCommandBuilder cb = new MyCommandBuilder ();
74                         Assert.AreEqual (".", cb.CatalogSeparator, "#1");
75                         cb.CatalogSeparator = "a";
76                         Assert.AreEqual ("a", cb.CatalogSeparator, "#2");
77                         cb.CatalogSeparator = null;
78                         Assert.AreEqual (".", cb.CatalogSeparator, "#3");
79                         cb.CatalogSeparator = "b";
80                         Assert.AreEqual ("b", cb.CatalogSeparator, "#4");
81                         cb.CatalogSeparator = string.Empty;
82                         Assert.AreEqual (".", cb.CatalogSeparator, "#5");
83                         cb.CatalogSeparator = " ";
84                         Assert.AreEqual (" ", cb.CatalogSeparator, "#6");
85                 }
86
87                 [Test]
88                 public void ConflictOptionTest ()
89                 {
90                         MyCommandBuilder cb = new MyCommandBuilder ();
91                         Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictOption, "#1");
92                         cb.ConflictOption = ConflictOption.CompareRowVersion;
93                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#2");
94                 }
95
96                 [Test]
97                 public void ConflictOption_Value_Invalid ()
98                 {
99                         MyCommandBuilder cb = new MyCommandBuilder ();
100                         cb.ConflictOption = ConflictOption.CompareRowVersion;
101                         try {
102                                 cb.ConflictOption = (ConflictOption) 666;
103                                 Assert.Fail ("#1");
104                         } catch (ArgumentOutOfRangeException ex) {
105                                 // The ConflictOption enumeration value, 666, is invalid
106                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
107                                 Assert.IsNull (ex.InnerException, "#3");
108                                 Assert.IsNotNull (ex.Message, "#4");
109                                 Assert.IsTrue (ex.Message.IndexOf ("ConflictOption") != -1, "#5:" + ex.Message);
110                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#6:" + ex.Message);
111                                 Assert.AreEqual ("ConflictOption", ex.ParamName, "#7");
112                         }
113                         Assert.AreEqual (ConflictOption.CompareRowVersion, cb.ConflictOption, "#8");
114                 }
115
116                 [Test] // QuoteIdentifier (String)
117                 public void QuoteIdentifier ()
118                 {
119                         MyCommandBuilder cb = new MyCommandBuilder ();
120                         try {
121                                 cb.QuoteIdentifier ((string) null);
122                                 Assert.Fail ("#A1");
123                         } catch (NotSupportedException ex) {
124                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
125                                 Assert.IsNull (ex.InnerException, "#A3");
126                                 Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#A4");
127                         }
128
129                         try {
130                                 cb.QuoteIdentifier ("mono");
131                                 Assert.Fail ("#B1");
132                         } catch (NotSupportedException ex) {
133                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
134                                 Assert.IsNull (ex.InnerException, "#B3");
135                                 Assert.AreEqual ((new NotSupportedException ()).Message, ex.Message, "#B4");
136                         }
137                 }
138
139                 [Test]
140                 public void QuotePrefix ()
141                 {
142                         MyCommandBuilder cb = new MyCommandBuilder ();
143                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
144                         cb.QuotePrefix = "mono";
145                         Assert.AreEqual ("mono", cb.QuotePrefix, "#2");
146                         cb.QuotePrefix = null;
147                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#3");
148                         cb.QuotePrefix = "'\"";
149                         Assert.AreEqual ("'\"", cb.QuotePrefix, "#4");
150                         cb.QuotePrefix = string.Empty;
151                         Assert.AreEqual (string.Empty, cb.QuotePrefix, "#5");
152                         cb.QuotePrefix = " ";
153                         Assert.AreEqual (" ", cb.QuotePrefix, "#6");
154                 }
155
156                 [Test]
157                 public void QuoteSuffix ()
158                 {
159                         MyCommandBuilder cb = new MyCommandBuilder ();
160                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
161                         cb.QuoteSuffix = "mono";
162                         Assert.AreEqual ("mono", cb.QuoteSuffix, "#2");
163                         cb.QuoteSuffix = null;
164                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#3");
165                         cb.QuoteSuffix = "'\"";
166                         Assert.AreEqual ("'\"", cb.QuoteSuffix, "#4");
167                         cb.QuoteSuffix = string.Empty;
168                         Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#5");
169                         cb.QuoteSuffix = " ";
170                         Assert.AreEqual (" ", cb.QuoteSuffix, "#6");
171                 }
172
173                 [Test]
174                 public void SchemaSeparator ()
175                 {
176                         MyCommandBuilder cb = new MyCommandBuilder ();
177                         Assert.AreEqual (".", cb.SchemaSeparator, "#1");
178                         cb.SchemaSeparator = "a";
179                         Assert.AreEqual ("a", cb.SchemaSeparator, "#2");
180                         cb.SchemaSeparator = null;
181                         Assert.AreEqual (".", cb.SchemaSeparator, "#3");
182                         cb.SchemaSeparator = "b";
183                         Assert.AreEqual ("b", cb.SchemaSeparator, "#4");
184                         cb.SchemaSeparator = string.Empty;
185                         Assert.AreEqual (".", cb.SchemaSeparator, "#5");
186                         cb.SchemaSeparator = " ";
187                         Assert.AreEqual (" ", cb.SchemaSeparator, "#6");
188                 }
189
190                 private class MyCommandBuilder : DbCommandBuilder
191                 {
192                         protected override string GetParameterPlaceholder (int parameterOrdinal)
193                         {
194                                 return string.Format (CultureInfo.InvariantCulture,
195                                         "@PH:{0}@", parameterOrdinal);
196                         }
197
198                         protected override string GetParameterName (string parameterName)
199                         {
200                                 return string.Format (CultureInfo.InvariantCulture,
201                                         "@NAME:{0}@", parameterName);
202                         }
203
204                         protected override string GetParameterName (int parameterOrdinal)
205                         {
206                                 return string.Format (CultureInfo.InvariantCulture,
207                                         "@NAME:{0}@", parameterOrdinal);
208                         }
209
210                         protected override void ApplyParameterInfo (DbParameter parameter, DataRow row, StatementType statementType, bool whereClause)
211                         {
212                         }
213
214                         protected override void SetRowUpdatingHandler (DbDataAdapter adapter)
215                         {
216                         }
217                 }
218         }
219 }
220 #endif // NET_2_0