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