[sgen] Fix printf-like format warnings
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlClient / SqlCommandTest.cs
1 //
2 // SqlCommandTest.cs - NUnit Test Cases for testing
3 // System.Data.SqlClient.SqlCommand
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28 //
29
30 using System;
31 using System.Data;
32 using System.Data.Sql;
33 using System.Data.SqlClient;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Data.SqlClient
38 {
39         [TestFixture]
40         public class SqlCommandTest
41         {
42                 const string COMMAND_TEXT = "SELECT * FROM Authors";
43
44                 [Test] // SqlCommand ()
45                 public void Constructor1 ()
46                 {
47                         SqlCommand cmd = new SqlCommand ();
48                         Assert.AreEqual (string.Empty, cmd.CommandText, "#1");
49                         Assert.AreEqual (30, cmd.CommandTimeout, "#2");
50                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#3");
51                         Assert.IsNull (cmd.Connection, "#4");
52                         Assert.IsNull (cmd.Container, "#5");
53                         Assert.IsTrue (cmd.DesignTimeVisible, "#6");
54                         Assert.IsNull (cmd.Notification, "#7");
55                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#8");
56                         Assert.IsNotNull (cmd.Parameters, "#9");
57                         Assert.AreEqual (0, cmd.Parameters.Count, "#10");
58                         Assert.IsNull (cmd.Site, "#11");
59                         Assert.IsNull (cmd.Transaction, "#11");
60                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#12");
61                 }
62
63                 [Test] // SqlCommand (string)
64                 public void Constructor2 ()
65                 {
66                         SqlCommand cmd = new SqlCommand (COMMAND_TEXT);
67                         Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
68                         Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
69                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
70                         Assert.IsNull (cmd.Connection, "#A4");
71                         Assert.IsNull (cmd.Container, "#A5");
72                         Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
73                         Assert.IsNull (cmd.Notification, "#A7");
74                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
75                         Assert.IsNotNull (cmd.Parameters, "#A9");
76                         Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
77                         Assert.IsNull (cmd.Site, "#A11");
78                         Assert.IsNull (cmd.Transaction, "#A12");
79                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
80
81                         cmd = new SqlCommand ((string) null);
82                         Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
83                         Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
84                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
85                         Assert.IsNull (cmd.Connection, "#B4");
86                         Assert.IsNull (cmd.Container, "#B5");
87                         Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
88                         Assert.IsNull (cmd.Notification, "#B7");
89                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
90                         Assert.IsNotNull (cmd.Parameters, "#B9");
91                         Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
92                         Assert.IsNull (cmd.Site, "#B11");
93                         Assert.IsNull (cmd.Transaction, "#B12");
94                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
95                 }
96
97                 [Test] // SqlCommand (string, SqlConnection)
98                 public void Constructor3 ()
99                 {
100                         SqlConnection conn = new SqlConnection ();
101                         SqlCommand cmd;
102
103                         cmd = new SqlCommand (COMMAND_TEXT, conn);
104                         Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
105                         Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
106                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
107                         Assert.AreSame (conn, cmd.Connection, "#A4");
108                         Assert.IsNull (cmd.Container, "#A5");
109                         Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
110                         Assert.IsNull (cmd.Notification, "#A7");
111                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
112                         Assert.IsNotNull (cmd.Parameters, "#A9");
113                         Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
114                         Assert.IsNull (cmd.Site, "#A11");
115                         Assert.IsNull (cmd.Transaction, "#A12");
116                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
117
118                         cmd = new SqlCommand ((string) null, conn);
119                         Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
120                         Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
121                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
122                         Assert.AreSame (conn, cmd.Connection, "#B4");
123                         Assert.IsNull (cmd.Container, "#B5");
124                         Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
125                         Assert.IsNull (cmd.Notification, "#B7");
126                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
127                         Assert.IsNotNull (cmd.Parameters, "#B9");
128                         Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
129                         Assert.IsNull (cmd.Site, "#B11");
130                         Assert.IsNull (cmd.Transaction, "#B12");
131                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
132
133                         cmd = new SqlCommand (COMMAND_TEXT, (SqlConnection) null);
134                         Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
135                         Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
136                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
137                         Assert.IsNull (cmd.Connection, "#C4");
138                         Assert.IsNull (cmd.Container, "#C5");
139                         Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
140                         Assert.IsNull (cmd.Notification, "#C7");
141                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#C8");
142                         Assert.IsNotNull (cmd.Parameters, "#C9");
143                         Assert.AreEqual (0, cmd.Parameters.Count, "#C10");
144                         Assert.IsNull (cmd.Site, "#C11");
145                         Assert.IsNull (cmd.Transaction, "#C12");
146                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C13");
147                 }
148
149                 [Test] // SqlCommand (string, SqlConnection, SqlTransaction)
150                 public void Constructor4 ()
151                 {
152                         SqlConnection conn = new SqlConnection ();
153                         SqlCommand cmd;
154
155                         cmd = new SqlCommand (COMMAND_TEXT, conn, (SqlTransaction) null);
156                         Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
157                         Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
158                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
159                         Assert.AreSame (conn, cmd.Connection, "#A4");
160                         Assert.IsNull (cmd.Container, "#A5");
161                         Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
162                         Assert.IsNull (cmd.Notification, "#A7");
163                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
164                         Assert.IsNotNull (cmd.Parameters, "#A9");
165                         Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
166                         Assert.IsNull (cmd.Site, "#A11");
167                         Assert.IsNull (cmd.Transaction, "#A12");
168                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
169
170                         cmd = new SqlCommand ((string) null, conn, (SqlTransaction) null);
171                         Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
172                         Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
173                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
174                         Assert.AreSame (conn, cmd.Connection, "#B4");
175                         Assert.IsNull (cmd.Container, "#B5");
176                         Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
177                         Assert.IsNull (cmd.Notification, "#B7");
178                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
179                         Assert.IsNotNull (cmd.Parameters, "#B9");
180                         Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
181                         Assert.IsNull (cmd.Site, "#B11");
182                         Assert.IsNull (cmd.Transaction, "#B12");
183                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
184
185                         cmd = new SqlCommand (COMMAND_TEXT, (SqlConnection) null, (SqlTransaction) null);
186                         Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
187                         Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
188                         Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
189                         Assert.IsNull (cmd.Connection, "#C4");
190                         Assert.IsNull (cmd.Container, "#C5");
191                         Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
192                         Assert.IsNull (cmd.Notification, "#C7");
193                         Assert.IsTrue (cmd.NotificationAutoEnlist, "#C8");
194                         Assert.IsNotNull (cmd.Parameters, "#C9");
195                         Assert.AreEqual (0, cmd.Parameters.Count, "#C10");
196                         Assert.IsNull (cmd.Site, "#C11");
197                         Assert.IsNull (cmd.Transaction, "#C12");
198                         Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C13");
199                 }
200
201                 [Test]
202                 public void Clone ()
203                 {
204                         SqlNotificationRequest notificationReq = new SqlNotificationRequest ();
205
206                         SqlCommand cmd = new SqlCommand ();
207                         cmd.CommandText = "sp_insert";
208                         cmd.CommandTimeout = 100;
209                         cmd.CommandType = CommandType.StoredProcedure;
210                         cmd.DesignTimeVisible = false;
211                         cmd.Notification = notificationReq;
212                         cmd.NotificationAutoEnlist = false;
213                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
214                         cmd.Parameters ["@TestPar1"].Value = DBNull.Value;
215                         cmd.Parameters.AddWithValue ("@BirthDate", DateTime.Now);
216                         cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
217
218                         SqlCommand clone = (((ICloneable) (cmd)).Clone ()) as SqlCommand;
219                         Assert.AreEqual ("sp_insert", clone.CommandText, "#1");
220                         Assert.AreEqual (100, clone.CommandTimeout, "#2");
221                         Assert.AreEqual (CommandType.StoredProcedure, clone.CommandType, "#3");
222                         Assert.IsNull (cmd.Connection, "#4");
223                         Assert.IsFalse (cmd.DesignTimeVisible, "#5");
224                         Assert.AreSame (notificationReq, cmd.Notification, "#6");
225                         Assert.IsFalse (cmd.NotificationAutoEnlist, "#7");
226                         Assert.AreEqual (2, clone.Parameters.Count, "#8");
227                         Assert.AreEqual (100, clone.CommandTimeout, "#9");
228                         clone.Parameters.AddWithValue ("@test", DateTime.Now);
229                         clone.Parameters [0].ParameterName = "@ClonePar1";
230                         Assert.AreEqual (3, clone.Parameters.Count, "#10");
231                         Assert.AreEqual (2, cmd.Parameters.Count, "#11");
232                         Assert.AreEqual ("@ClonePar1", clone.Parameters [0].ParameterName, "#12");
233                         Assert.AreEqual ("@TestPar1", cmd.Parameters [0].ParameterName, "#13");
234                         Assert.AreEqual ("@BirthDate", clone.Parameters [1].ParameterName, "#14");
235                         Assert.AreEqual ("@BirthDate", cmd.Parameters [1].ParameterName, "#15");
236                         Assert.IsNull (clone.Transaction, "#16");
237                 }
238
239                 [Test]
240                 public void CommandText ()
241                 {
242                         SqlCommand cmd = new SqlCommand ();
243                         cmd.CommandText = COMMAND_TEXT;
244                         Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#1");
245                         cmd.CommandText = null;
246                         Assert.AreEqual (string.Empty, cmd.CommandText, "#2");
247                         cmd.CommandText = COMMAND_TEXT;
248                         Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#3");
249                         cmd.CommandText = string.Empty;
250                         Assert.AreEqual (string.Empty, cmd.CommandText, "#4");
251                 }
252
253                 [Test]
254                 public void CommandTimeout ()
255                 {
256                         SqlCommand cmd = new SqlCommand ();
257                         cmd.CommandTimeout = 10;
258                         Assert.AreEqual (10, cmd.CommandTimeout, "#1");
259                         cmd.CommandTimeout = 25;
260                         Assert.AreEqual (25, cmd.CommandTimeout, "#2");
261                         cmd.CommandTimeout = 0;
262                         Assert.AreEqual (0, cmd.CommandTimeout, "#3");
263                 }
264
265                 [Test]
266                 public void CommandTimeout_Value_Negative ()
267                 {
268                         SqlCommand cmd = new SqlCommand ();
269                         try {
270                                 cmd.CommandTimeout = -1;
271                                 Assert.Fail ("#1");
272                         } catch (ArgumentException ex) {
273                                 // Invalid CommandTimeout value -1; the value must be >= 0
274                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
275                                 Assert.IsNull (ex.InnerException, "#3");
276                                 Assert.IsNotNull (ex.Message, "#4");
277                                 Assert.AreEqual ("CommandTimeout", ex.ParamName, "#5");
278                         }
279                 }
280
281                 [Test]
282                 public void CommandType_Value_Invalid ()
283                 {
284                         SqlCommand cmd = new SqlCommand ();
285                         try {
286                                 cmd.CommandType = (CommandType) (666);
287                                 Assert.Fail ("#1");
288                         } catch (ArgumentOutOfRangeException ex) {
289                                 // The CommandType enumeration value, 666, is invalid
290                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
291                                 Assert.IsNull (ex.InnerException, "#3");
292                                 Assert.IsNotNull (ex.Message, "#4");
293                                 Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#5");
294                                 Assert.AreEqual ("CommandType", ex.ParamName, "#6");
295                         }
296                 }
297
298                 [Test] // bug #324386
299                 public void Dispose ()
300                 {
301                         string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
302                                 + "Password=d";
303                         SqlConnection connection = new SqlConnection (connectionString);
304                         SqlCommand command = connection.CreateCommand ();
305                         command.Dispose ();
306                         Assert.AreEqual (connectionString, connection.ConnectionString);
307                 }
308
309                 [Test]
310                 public void ExecuteNonQuery_Connection_Closed ()
311                 {
312                         string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
313                                 + "Password=d";
314                         SqlConnection cn = new SqlConnection (connectionString);
315
316                         SqlCommand cmd = new SqlCommand ("delete from whatever", cn);
317                         try {
318                                 cmd.ExecuteNonQuery ();
319                                 Assert.Fail ("#1");
320                         } catch (InvalidOperationException ex) {
321                                 // ExecuteNonQuery requires an open and available
322                                 // Connection. The connection's current state is
323                                 // closed.
324                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
325                                 Assert.IsNull (ex.InnerException, "#3");
326                                 Assert.IsNotNull (ex.Message, "#4");
327                                 Assert.IsTrue (ex.Message.IndexOf ("ExecuteNonQuery") != -1, "#5");
328                         }
329                 }
330
331                 [Test]
332                 public void ExecuteNonQuery_Connection_Null ()
333                 {
334                         SqlCommand cmd = new SqlCommand ("delete from whatever");
335                         try {
336                                 cmd.ExecuteNonQuery ();
337                                 Assert.Fail ("#1");
338                         } catch (InvalidOperationException ex) {
339                                 // ExecuteNonQuery: Connection property has not
340                                 // been initialized
341                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
342                                 Assert.IsNull (ex.InnerException, "#3");
343                                 Assert.IsNotNull (ex.Message, "#4");
344                                 Assert.IsTrue (ex.Message.StartsWith ("ExecuteNonQuery:"), "#5");
345                         }
346                 }
347
348                 [Test]
349                 public void ExecuteReader_Connection_Closed ()
350                 {
351                         string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
352                                 + "Password=d";
353                         SqlConnection cn = new SqlConnection (connectionString);
354
355                         SqlCommand cmd = new SqlCommand ("Select count(*) from whatever", cn);
356                         try {
357                                 cmd.ExecuteReader ();
358                                 Assert.Fail ("#1");
359                         } catch (InvalidOperationException ex) {
360                                 // ExecuteReader requires an open and available
361                                 // Connection. The connection's current state is
362                                 // closed.
363                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
364                                 Assert.IsNull (ex.InnerException, "#3");
365                                 Assert.IsNotNull (ex.Message, "#4");
366                                 Assert.IsTrue (ex.Message.IndexOf ("ExecuteReader") != -1, "#5");
367                         }
368                 }
369
370                 [Test]
371                 public void ExecuteReader_Connection_Null ()
372                 {
373                         SqlCommand cmd = new SqlCommand ("select * from whatever");
374                         try {
375                                 cmd.ExecuteReader ();
376                                 Assert.Fail ("#1");
377                         } catch (InvalidOperationException ex) {
378                                 // ExecuteReader: Connection property has not
379                                 // been initialized
380                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
381                                 Assert.IsNull (ex.InnerException, "#3");
382                                 Assert.IsNotNull (ex.Message, "#4");
383                                 Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader:"), "#5");
384                         }
385                 }
386
387                 [Test]
388                 public void ExecuteScalar_Connection_Closed ()
389                 {
390                         string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
391                                 + "Password=d";
392                         SqlConnection cn = new SqlConnection (connectionString);
393
394                         SqlCommand cmd = new SqlCommand ("Select count(*) from whatever", cn);
395                         try {
396                                 cmd.ExecuteScalar ();
397                                 Assert.Fail ("#1");
398                         } catch (InvalidOperationException ex) {
399                                 // ExecuteScalar requires an open and available
400                                 // Connection. The connection's current state is
401                                 // closed.
402                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
403                                 Assert.IsNull (ex.InnerException, "#3");
404                                 Assert.IsNotNull (ex.Message, "#4");
405                                 Assert.IsTrue (ex.Message.IndexOf ("ExecuteScalar") != -1, "#5");
406                         }
407                 }
408
409                 [Test] // bug #412584
410                 public void ExecuteScalar_Connection_Null ()
411                 {
412                         SqlCommand cmd = new SqlCommand ("select count(*) from whatever");
413                         try {
414                                 cmd.ExecuteScalar ();
415                                 Assert.Fail ("#1");
416                         } catch (InvalidOperationException ex) {
417                                 // ExecuteScalar: Connection property has not
418                                 // been initialized
419                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
420                                 Assert.IsNull (ex.InnerException, "#3");
421                                 Assert.IsNotNull (ex.Message, "#4");
422                                 Assert.IsTrue (ex.Message.StartsWith ("ExecuteScalar:"), "#5");
423                         }
424                 }
425
426                 // FIXME: this actually doesn't match .NET behavior. It shouldn't throw NRE.
427                 [Test]
428                 public void Prepare_Connection_Null ()
429                 {
430                         SqlCommand cmd;
431
432                         // Text, without parameters
433                         cmd = new SqlCommand ("select count(*) from whatever");
434                         try {
435                                 cmd.Prepare ();
436                                 Assert.Fail ("#A1");
437                         } catch (NullReferenceException) {
438                         }
439
440                         // Text, with parameters
441                         cmd = new SqlCommand ("select count(*) from whatever");
442                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
443                         try {
444                                 cmd.Prepare ();
445                                 Assert.Fail ("#B1");
446                         } catch (NullReferenceException) {
447                         }
448
449                         // Text, without parameters
450                         cmd = new SqlCommand ("select count(*) from whatever");
451                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
452                         cmd.Parameters.Clear ();
453                         try {
454                                 cmd.Prepare ();
455                                 Assert.Fail ("#C1");
456                         } catch (NullReferenceException) {
457                         }
458
459                         // StoredProcedure, without parameters
460                         cmd = new SqlCommand ("FindCustomer");
461                         cmd.CommandType = CommandType.StoredProcedure;
462                         try {
463                                 cmd.Prepare ();
464                                 Assert.Fail ("#D1");
465                         } catch (NullReferenceException) {
466                         }
467
468                         // StoredProcedure, with parameters
469                         cmd = new SqlCommand ("FindCustomer");
470                         cmd.CommandType = CommandType.StoredProcedure;
471                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
472                         try {
473                                 cmd.Prepare ();
474                                 Assert.Fail ("#E1");
475                         } catch (NullReferenceException) {
476                         }
477                 }
478                 
479                 [Test] // bug #412586
480                 public void Prepare_Connection_Closed ()
481                 {
482                         string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
483                                 + "Password=d";
484                         SqlConnection cn = new SqlConnection (connectionString);
485
486                         SqlCommand cmd;
487
488                         // Text, without parameters
489                         cmd = new SqlCommand ("select count(*) from whatever", cn);
490                         cmd.Prepare ();
491
492                         // Text, with parameters
493                         cmd = new SqlCommand ("select count(*) from whatever", cn);
494                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
495                         try {
496                                 cmd.Prepare ();
497                                 Assert.Fail ("#A1");
498                         } catch (InvalidOperationException ex) {
499                                 // Prepare requires an open and available
500                                 // Connection. The connection's current state
501                                 // is Closed
502                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
503                                 Assert.IsNull (ex.InnerException, "#A3");
504                                 Assert.IsNotNull (ex.Message, "#A4");
505                                 Assert.IsTrue (ex.Message.IndexOf ("Prepare") != -1, "#A5");
506                         }
507
508                         // Text, parameters cleared
509                         cmd = new SqlCommand ("select count(*) from whatever", cn);
510                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
511                         cmd.Parameters.Clear ();
512                         cmd.Prepare ();
513
514                         // StoredProcedure, without parameters
515                         cmd = new SqlCommand ("FindCustomer", cn);
516                         cmd.CommandType = CommandType.StoredProcedure;
517                         cmd.Prepare ();
518
519                         // StoredProcedure, with parameters
520                         cmd = new SqlCommand ("FindCustomer", cn);
521                         cmd.CommandType = CommandType.StoredProcedure;
522                         cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
523                         cmd.Prepare ();
524
525                         // ensure connection was not implictly opened
526                         Assert.AreEqual (ConnectionState.Closed, cn.State, "#B");
527                 }
528
529                 [Test]
530                 public void ResetCommandTimeout ()
531                 {
532                         SqlCommand cmd = new SqlCommand ();
533                         cmd.CommandTimeout = 50;
534                         Assert.AreEqual (cmd.CommandTimeout, 50, "#1");
535                         cmd.ResetCommandTimeout ();
536                         Assert.AreEqual (cmd.CommandTimeout, 30, "#2");
537                 }
538
539                 [Test]
540                 public void UpdatedRowSource ()
541                 {
542                         SqlCommand cmd = new SqlCommand ();
543                         cmd.UpdatedRowSource = UpdateRowSource.None;
544                         Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource, "#1");
545                         cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
546                         Assert.AreEqual (UpdateRowSource.OutputParameters, cmd.UpdatedRowSource, "#2");
547                 }
548
549                 [Test]
550                 public void UpdatedRowSource_Value_Invalid ()
551                 {
552                         SqlCommand cmd = new SqlCommand ();
553                         try {
554                                 cmd.UpdatedRowSource = (UpdateRowSource) 666;
555                                 Assert.Fail ("#1");
556                         } catch (ArgumentOutOfRangeException ex) {
557                                 // The UpdateRowSource enumeration value,666,
558                                 // is invalid
559                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
560                                 Assert.IsNull (ex.InnerException, "#3");
561                                 Assert.IsNotNull (ex.Message, "#4");
562                                 Assert.AreEqual ("UpdateRowSource", ex.ParamName, "#5");
563                         }
564                 }
565
566
567                 [Test] // bug #381100
568                 public void ParameterCollectionTest ()
569                 {
570                         SqlCommand cmd = new SqlCommand();
571                         cmd.Parameters.AddRange(new SqlParameter[] { });
572                 }
573         }
574 }
575