update readme (#21797)
[mono-project.git] / mcs / class / System.Data / Test / System.Data.Odbc / OdbcCommandTest.cs
blob21e149e2ac718cd9a9a2bd00b1f53cea27ca7943
1 //
2 // OdbcCommandTest.cs - NUnit Test Cases for testing
3 // System.Data.Odbc.OdbcCommand
4 //
5 // Author:
6 // Gert Driesen (drieseng@users.sourceforge.net)
7 //
8 // Copyright (c) 2007 Gert Driesen
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #if !NO_ODBC
32 using System;
33 using System.Data;
34 using System.Data.Odbc;
36 using NUnit.Framework;
38 namespace MonoTests.System.Data.Odbc
40 [TestFixture]
41 public class OdbcCommandTest
43 const string COMMAND_TEXT = "SELECT * FROM Authors";
45 [Test] // OdbcCommand ()
46 public void Constructor1 ()
48 OdbcCommand cmd = new OdbcCommand ();
49 Assert.AreEqual (string.Empty, cmd.CommandText, "#1");
50 Assert.AreEqual (30, cmd.CommandTimeout, "#2");
51 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#3");
52 Assert.IsNull (cmd.Connection, "#4");
53 Assert.IsNull (cmd.Container, "#5");
54 Assert.IsTrue (cmd.DesignTimeVisible, "#6");
55 Assert.IsNotNull (cmd.Parameters, "#7");
56 Assert.AreEqual (0, cmd.Parameters.Count, "#8");
57 Assert.IsNull (cmd.Site, "#9");
58 Assert.IsNull (cmd.Transaction, "#10");
59 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#11");
62 [Test] // OdbcCommand (String)
63 public void Constructor2 ()
65 OdbcCommand cmd = new OdbcCommand (COMMAND_TEXT);
66 Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
67 Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
68 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
69 Assert.IsNull (cmd.Connection, "#A4");
70 Assert.IsNull (cmd.Container, "#A5");
71 Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
72 Assert.IsNotNull (cmd.Parameters, "#A7");
73 Assert.AreEqual (0, cmd.Parameters.Count, "#A8");
74 Assert.IsNull (cmd.Site, "#A9");
75 Assert.IsNull (cmd.Transaction, "#A10");
76 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A11");
78 cmd = new OdbcCommand ((string) null);
79 Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
80 Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
81 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
82 Assert.IsNull (cmd.Connection, "#B4");
83 Assert.IsNull (cmd.Container, "#B5");
84 Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
85 Assert.IsNotNull (cmd.Parameters, "#B7");
86 Assert.AreEqual (0, cmd.Parameters.Count, "#B8");
87 Assert.IsNull (cmd.Site, "#B9");
88 Assert.IsNull (cmd.Transaction, "#B10");
89 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B11");
92 [Test] // OdbcCommand (String, OdbcConnection)
93 public void Constructor3 ()
95 OdbcConnection conn = new OdbcConnection ();
96 OdbcCommand cmd;
98 cmd = new OdbcCommand (COMMAND_TEXT, conn);
99 Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
100 Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
101 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
102 Assert.AreSame (conn, cmd.Connection, "#A4");
103 Assert.IsNull (cmd.Container, "#A5");
104 Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
105 Assert.IsNotNull (cmd.Parameters, "#A7");
106 Assert.AreEqual (0, cmd.Parameters.Count, "#A8");
107 Assert.IsNull (cmd.Site, "#A9");
108 Assert.IsNull (cmd.Transaction, "#A10");
109 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A11");
111 cmd = new OdbcCommand ((string) null, conn);
112 Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
113 Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
114 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
115 Assert.AreSame (conn, cmd.Connection, "#B4");
116 Assert.IsNull (cmd.Container, "#B5");
117 Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
118 Assert.IsNotNull (cmd.Parameters, "#B7");
119 Assert.AreEqual (0, cmd.Parameters.Count, "#B8");
120 Assert.IsNull (cmd.Site, "#B9");
121 Assert.IsNull (cmd.Transaction, "#B10");
122 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B11");
124 cmd = new OdbcCommand (COMMAND_TEXT, (OdbcConnection) null);
125 Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
126 Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
127 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
128 Assert.IsNull (cmd.Connection, "#C4");
129 Assert.IsNull (cmd.Container, "#C5");
130 Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
131 Assert.IsNotNull (cmd.Parameters, "#C7");
132 Assert.AreEqual (0, cmd.Parameters.Count, "#C8");
133 Assert.IsNull (cmd.Site, "#C9");
134 Assert.IsNull (cmd.Transaction, "#C10");
135 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C11");
138 [Test] // OdbcCommand (String, OdbcConnection, OdbcTransaction)
139 public void Constructor4 ()
141 OdbcConnection conn = new OdbcConnection ();
142 OdbcCommand cmd;
144 cmd = new OdbcCommand (COMMAND_TEXT, conn, (OdbcTransaction) null);
145 Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
146 Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
147 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
148 Assert.AreSame (conn, cmd.Connection, "#A4");
149 Assert.IsNull (cmd.Container, "#A5");
150 Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
151 Assert.IsNotNull (cmd.Parameters, "#A7");
152 Assert.AreEqual (0, cmd.Parameters.Count, "#A8");
153 Assert.IsNull (cmd.Site, "#A9");
154 Assert.IsNull (cmd.Transaction, "#A10");
155 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A11");
157 cmd = new OdbcCommand ((string) null, conn, (OdbcTransaction) null);
158 Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
159 Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
160 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
161 Assert.AreSame (conn, cmd.Connection, "#B4");
162 Assert.IsNull (cmd.Container, "#B5");
163 Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
164 Assert.IsNotNull (cmd.Parameters, "#B7");
165 Assert.AreEqual (0, cmd.Parameters.Count, "#B8");
166 Assert.IsNull (cmd.Site, "#B9");
167 Assert.IsNull (cmd.Transaction, "#B10");
168 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B11");
170 cmd = new OdbcCommand (COMMAND_TEXT, (OdbcConnection) null, (OdbcTransaction) null);
171 Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
172 Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
173 Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
174 Assert.IsNull (cmd.Connection, "#C4");
175 Assert.IsNull (cmd.Container, "#C5");
176 Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
177 Assert.IsNotNull (cmd.Parameters, "#C7");
178 Assert.AreEqual (0, cmd.Parameters.Count, "#C8");
179 Assert.IsNull (cmd.Site, "#C9");
180 Assert.IsNull (cmd.Transaction, "#C10");
181 Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C11");
184 [Test]
185 public void CommandText ()
187 OdbcCommand cmd = new OdbcCommand ();
188 cmd.CommandText = COMMAND_TEXT;
189 Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#1");
190 cmd.CommandText = null;
191 Assert.AreEqual (string.Empty, cmd.CommandText, "#2");
192 cmd.CommandText = COMMAND_TEXT;
193 Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#3");
194 cmd.CommandText = string.Empty;
195 Assert.AreEqual (string.Empty, cmd.CommandText, "#4");
198 [Test]
199 public void CommandTimeout ()
201 OdbcCommand cmd = new OdbcCommand ();
202 cmd.CommandTimeout = 10;
203 Assert.AreEqual (10, cmd.CommandTimeout, "#1");
204 cmd.CommandTimeout = 25;
205 Assert.AreEqual (25, cmd.CommandTimeout, "#2");
206 cmd.CommandTimeout = 0;
207 Assert.AreEqual (0, cmd.CommandTimeout, "#3");
210 [Test]
211 public void CommandTimeout_Value_Negative ()
213 OdbcCommand cmd = new OdbcCommand ();
214 try {
215 cmd.CommandTimeout = -1;
216 Assert.Fail ("#1");
217 } catch (ArgumentException ex) {
218 // Invalid CommandTimeout value -1; the value must be >= 0
219 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
220 Assert.IsNull (ex.InnerException, "#3");
221 Assert.IsNotNull (ex.Message, "#4");
222 Assert.AreEqual ("CommandTimeout", ex.ParamName, "#5");
226 [Test]
227 public void CommandType_Value_Invalid ()
229 OdbcCommand cmd = new OdbcCommand ();
230 try {
231 cmd.CommandType = (CommandType) (666);
232 Assert.Fail ("#1");
233 } catch (ArgumentOutOfRangeException ex) {
234 // The CommandType enumeration value, 666, is invalid
235 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
236 Assert.IsNull (ex.InnerException, "#3");
237 Assert.IsNotNull (ex.Message, "#4");
238 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5:" + ex.Message);
239 Assert.AreEqual ("CommandType", ex.ParamName, "#6");
243 [Test]
244 public void Dispose ()
246 OdbcConnection conn = new OdbcConnection ();
247 OdbcCommand cmd = null;
249 try {
250 cmd = conn.CreateCommand ();
251 cmd.CommandText = "SELECT 'a'";
252 cmd.CommandTimeout = 67;
253 cmd.CommandType = CommandType.StoredProcedure;
254 cmd.DesignTimeVisible = false;
255 cmd.Parameters.Add (new OdbcParameter ());
256 cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
258 cmd.Dispose ();
260 Assert.AreEqual (string.Empty, cmd.CommandText, "CommandText");
261 Assert.AreEqual (67, cmd.CommandTimeout, "CommandTimeout");
262 Assert.AreEqual (CommandType.StoredProcedure, cmd.CommandType, "CommandType");
263 Assert.IsNull (cmd.Connection, "Connection");
264 Assert.IsFalse (cmd.DesignTimeVisible, "DesignTimeVisible");
265 Assert.IsNotNull (cmd.Parameters, "Parameters#1");
266 Assert.AreEqual (0, cmd.Parameters.Count, "Parameters#2");
267 Assert.IsNull (cmd.Transaction, "Transaction");
268 Assert.AreEqual (UpdateRowSource.OutputParameters, cmd.UpdatedRowSource, "UpdatedRowSource");
269 } finally {
270 if (cmd != null)
271 cmd.Dispose ();
272 conn.Dispose ();
276 [Test]
277 public void ExecuteNonQuery_Connection_Closed ()
279 OdbcConnection cn = new OdbcConnection ();
280 OdbcCommand cmd = new OdbcCommand (COMMAND_TEXT, cn);
281 try {
282 cmd.ExecuteNonQuery ();
283 Assert.Fail ("#1");
284 } catch (InvalidOperationException ex) {
285 // ExecuteNonQuery requires an open and available
286 // Connection. The connection's current state is
287 // closed.
288 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
289 Assert.IsNull (ex.InnerException, "#3");
290 Assert.IsNotNull (ex.Message, "#4");
291 Assert.IsTrue (ex.Message.IndexOf ("ExecuteNonQuery") != -1, "#5:" + ex.Message);
295 [Test]
296 public void ExecuteNonQuery_Connection_Null ()
298 OdbcCommand cmd = new OdbcCommand ();
299 cmd.CommandText = COMMAND_TEXT;
301 try {
302 cmd.ExecuteNonQuery ();
303 Assert.Fail ("#1");
304 } catch (InvalidOperationException ex) {
305 // ExecuteNonQuery: Connection property
306 // has not been initialized
307 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
308 Assert.IsNull (ex.InnerException, "#3");
309 Assert.IsNotNull (ex.Message, "#4");
310 Assert.IsTrue (ex.Message.IndexOf ("ExecuteNonQuery") != -1, "#5:" + ex.Message);
314 [Test]
315 public void ExecuteReader_Connection_Closed ()
317 OdbcConnection cn = new OdbcConnection ();
318 OdbcCommand cmd = new OdbcCommand (COMMAND_TEXT, cn);
319 try {
320 cmd.ExecuteReader ();
321 Assert.Fail ("#1");
322 } catch (InvalidOperationException ex) {
323 // ExecuteNonQuery requires an open and available
324 // Connection. The connection's current state is
325 // closed.
326 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
327 Assert.IsNull (ex.InnerException, "#3");
328 Assert.IsNotNull (ex.Message, "#4");
329 Assert.IsTrue (ex.Message.IndexOf ("ExecuteReader") != -1, "#5:" + ex.Message);
333 [Test]
334 public void ExecuteReader_Connection_Null ()
336 OdbcCommand cmd = new OdbcCommand ();
337 cmd.CommandText = COMMAND_TEXT;
339 try {
340 cmd.ExecuteReader ();
341 Assert.Fail ("#1");
342 } catch (InvalidOperationException ex) {
343 // ExecuteNonQuery: Connection property
344 // has not been initialized
345 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
346 Assert.IsNull (ex.InnerException, "#3");
347 Assert.IsNotNull (ex.Message, "#4");
348 Assert.IsTrue (ex.Message.IndexOf ("ExecuteReader") != -1, "#5:" + ex.Message);
352 [Test]
353 public void ExecuteScalar_Connection_Closed ()
355 OdbcConnection cn = new OdbcConnection ();
356 OdbcCommand cmd = new OdbcCommand (COMMAND_TEXT, cn);
357 try {
358 cmd.ExecuteScalar ();
359 Assert.Fail ("#1");
360 } catch (InvalidOperationException ex) {
361 // ExecuteNonQuery requires an open and available
362 // Connection. The connection's current state is
363 // closed.
364 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
365 Assert.IsNull (ex.InnerException, "#3");
366 Assert.IsNotNull (ex.Message, "#4");
367 Assert.IsTrue (ex.Message.IndexOf ("ExecuteScalar") != -1, "#5:" + ex.Message);
371 [Test]
372 public void ExecuteScalar_Connection_Null ()
374 OdbcCommand cmd = new OdbcCommand ();
375 cmd.CommandText = COMMAND_TEXT;
377 try {
378 cmd.ExecuteScalar ();
379 Assert.Fail ("#1");
380 } catch (InvalidOperationException ex) {
381 // ExecuteNonQuery: Connection property
382 // has not been initialized
383 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
384 Assert.IsNull (ex.InnerException, "#3");
385 Assert.IsNotNull (ex.Message, "#4");
386 Assert.IsTrue (ex.Message.IndexOf ("ExecuteScalar") != -1, "#5:" + ex.Message);
390 [Test]
391 public void ResetCommandTimeout ()
393 OdbcCommand cmd = new OdbcCommand ();
394 cmd.CommandTimeout = 50;
395 Assert.AreEqual (cmd.CommandTimeout, 50, "#1");
396 cmd.ResetCommandTimeout ();
397 Assert.AreEqual (cmd.CommandTimeout, 30, "#2");
400 [Test]
401 public void UpdatedRowSource ()
403 OdbcCommand cmd = new OdbcCommand ();
404 cmd.UpdatedRowSource = UpdateRowSource.None;
405 Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource, "#1");
406 cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
407 Assert.AreEqual (UpdateRowSource.OutputParameters, cmd.UpdatedRowSource, "#2");
410 [Test]
411 public void UpdatedRowSource_Value_Invalid ()
413 OdbcCommand cmd = new OdbcCommand ();
414 try {
415 cmd.UpdatedRowSource = (UpdateRowSource) 666;
416 Assert.Fail ("#1");
417 } catch (ArgumentOutOfRangeException ex) {
418 // The UpdateRowSource enumeration value,666,
419 // is invalid
420 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
421 Assert.IsNull (ex.InnerException, "#3");
422 Assert.IsNotNull (ex.Message, "#4");
423 Assert.AreEqual ("UpdateRowSource", ex.ParamName, "#5");
429 #endif