New tests.
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.SqlClient / SqlConnectionTest.cs
1 //
2 // SqlConnectionTest.cs - NUnit Test Cases for testing the
3 //                          SqlConnection class
4 // Author:
5 //      Senganal T (tsenganal@novell.com)
6 //
7 // Copyright (c) 2004 Novell Inc., and the individuals listed
8 // on the ChangeLog entries.
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.SqlClient;
33 using System.Net;
34 using NUnit.Framework;
35 using System.Collections;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture]
40         [Category ("sqlserver")]
41         public class SqlConnectionTest
42         {
43                 SqlConnection conn;
44                 String connectionString = ConnectionManager.Singleton.ConnectionString;
45                 ArrayList events;
46
47                 [SetUp]
48                 public void SetUp ()
49                 {
50                         events = new ArrayList ();
51                 }
52
53                 [TearDown]
54                 public void TearDown ()
55                 {
56                         if (conn != null)
57                                 conn.Dispose ();
58                 }
59
60                 [Test]
61                 public void OverloadedConstructorTest ()
62                 {
63                         //check synonyms.
64                         //do i need to check for all the synonyms.. 
65                         conn = new SqlConnection ("Timeout=10;Connect Timeout=20;Connection Timeout=30");
66                         Assert.AreEqual (30, conn.ConnectionTimeout, "#A1");
67                         conn = new SqlConnection ("Connect Timeout=100;Connection Timeout=200;Timeout=300");
68                         Assert.AreEqual (300, conn.ConnectionTimeout, "#A2");
69                         conn = new SqlConnection ("Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
70                         Assert.AreEqual (3000, conn.ConnectionTimeout, "#A3");
71
72                         //'==' doesent work correctly in both msdotnet and mono
73                         /*
74                         conn = new SqlConnection ("server=local==host;database=tmp;");
75                         Assert.AreEqual ("local==host", conn.DataSource, 
76                                 "# Datasource name is set incorrectly");
77                         */
78                         string connStr = "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";"
79                                 + "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
80                         conn = new SqlConnection (connStr);
81                         Assert.AreEqual (connStr , conn.ConnectionString , "#B1");
82                         Assert.AreEqual ("loca\"lhost" , conn.DataSource , "#B2");
83                         Assert.AreEqual ("'Db" , conn.Database , "#B3");
84                         Assert.AreEqual (512 , conn.PacketSize , "#B4");
85                         Assert.AreEqual (20 , conn.ConnectionTimeout , "#B5");
86                         Assert.AreEqual ("'\"desktop" , conn.WorkstationId , "#B6");
87                 }
88
89                 [Test]
90                 public void Open ()
91                 {
92                         conn = new SqlConnection (connectionString);
93                         conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
94                         conn.Open ();
95
96                         Assert.AreEqual (ConnectionState.Open, conn.State, "#1");
97                         Assert.AreEqual (1, events.Count, "#2");
98                         StateChangeEventArgs args = events [0] as StateChangeEventArgs;
99                         Assert.IsNotNull (args, "#3");
100                         Assert.AreEqual (ConnectionState.Closed, args.OriginalState, "#4");
101                         Assert.AreEqual (ConnectionState.Open, args.CurrentState, "#5");
102
103                         conn.Close ();
104                 }
105
106                 [Test]
107                 public void Open_Connection_Open ()
108                 {
109                         conn = new SqlConnection (connectionString);
110                         conn.Open ();
111
112                         try {
113                                 conn.Open ();
114                                 Assert.Fail ("#1");
115                         } catch (InvalidOperationException ex) {
116                                 // The connection was not closed. The connection's
117                                 // current state is open
118                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
119                                 Assert.IsNull (ex.InnerException, "#3");
120                                 Assert.IsNotNull (ex.Message, "#4");
121                         } finally {
122                                 conn.Close ();
123                         }
124                 }
125
126                 [Test]
127                 public void Open_ConnectionString_Incorrect ()
128                 {
129                         Assert.Ignore ("NotWorking");
130
131                         // login invalid
132                         conn = new SqlConnection (connectionString + "user id=invalidLogin");
133                         try {
134                                 conn.Open ();
135                                 Assert.Fail ("#A1");
136                         } catch (SqlException ex) {
137                                 // Login failed for user 'invalidLogin'
138                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#A2");
139                                 Assert.AreEqual ((byte) 14, ex.Class, "#A3");
140                                 Assert.IsNull (ex.InnerException, "#A4");
141                                 Assert.IsNotNull (ex.Message, "#A5");
142                                 Assert.IsTrue (ex.Message.IndexOf ("'invalidLogin'") != -1, "#A6");
143                                 Assert.AreEqual (18456, ex.Number, "#A7");
144                                 Assert.AreEqual ((byte) 1, ex.State, "#A8");
145                         } finally {
146                                 conn.Close ();
147                         }
148
149                         // database invalid
150                         conn = new SqlConnection (connectionString + "database=invalidDB");
151                         try {
152                                 conn.Open ();
153                                 Assert.Fail ("#B1");
154                         } catch (SqlException ex) {
155                                 // Cannot open database "invalidDB" requested
156                                 // by the login. The login failed
157                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
158                                 Assert.AreEqual ((byte) 11, ex.Class, "#B3");
159                                 Assert.IsNull (ex.InnerException, "#B4");
160                                 Assert.IsNotNull (ex.Message, "#B5");
161                                 Assert.IsTrue (ex.Message.IndexOf ("\"invalidDB\"") != -1, "#B6");
162                                 Assert.AreEqual (4060, ex.Number, "#B7");
163                                 Assert.AreEqual ((byte) 1, ex.State, "#B8");
164                         } finally {
165                                 conn.Close ();
166                         }
167
168                         // password invalid
169                         conn = new SqlConnection (connectionString + ";password=invalidPassword");
170                         try {
171                                 conn.Open ();
172                                 Assert.Fail ("#C1");
173                         } catch (SqlException ex) {
174                                 // Login failed for user '...'
175                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#C2");
176                                 Assert.AreEqual ((byte) 14, ex.Class, "#C3");
177                                 Assert.IsNull (ex.InnerException, "#C4");
178                                 Assert.IsNotNull (ex.Message, "#C5");
179                                 Assert.AreEqual (18456, ex.Number, "#C6");
180                                 Assert.AreEqual ((byte) 1, ex.State, "#C7");
181                         } finally {
182                                 conn.Close ();
183                         }
184
185                         // server invalid
186                         conn = new SqlConnection (connectionString + ";server=invalidServerName");
187                         try {
188                                 conn.Open ();
189                                 Assert.Fail ("#D1");
190                         } catch (SqlException ex) {
191                                 // An error has occurred while establishing a
192                                 // connection to the server...
193                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#D2");
194                                 Assert.AreEqual ((byte) 20, ex.Class, "#D3");
195                                 Assert.IsNull (ex.InnerException, "#D4");
196                                 Assert.IsNotNull (ex.Message, "#D5");
197 #if NET_2_0
198                                 Assert.AreEqual (53, ex.Number, "#D6");
199 #else
200                                 Assert.AreEqual (17, ex.Number, "#D6");
201 #endif
202                                 Assert.AreEqual ((byte) 0, ex.State, "#D7");
203                         } finally {
204                                 conn.Close ();
205                         }
206                 }
207
208                 [Test] // bug #412574
209                 public void Close ()
210                 {
211                         conn = new SqlConnection (connectionString);
212                         conn.Open ();
213                         conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
214                         conn.Close ();
215
216                         Assert.AreEqual (ConnectionState.Closed, conn.State, "#1");
217                         Assert.AreEqual (1, events.Count, "#2");
218                         StateChangeEventArgs args = events [0] as StateChangeEventArgs;
219                         Assert.IsNotNull (args, "#3");
220                         Assert.AreEqual (ConnectionState.Open, args.OriginalState, "#4");
221                         Assert.AreEqual (ConnectionState.Closed, args.CurrentState, "5");
222
223                         conn.Close ();
224
225                         Assert.AreEqual (1, events.Count, "#6");
226                 }
227
228                 [Test]
229                 public void ChangeDatabase ()
230                 {
231                         conn = new SqlConnection (connectionString);
232                         conn.Open ();
233                         conn.ChangeDatabase ("master");
234                         Assert.AreEqual ("master", conn.Database);
235                 }
236
237                 [Test]
238                 public void ChangeDatabase_DatabaseName_DoesNotExist ()
239                 {
240                         conn = new SqlConnection (connectionString);
241                         conn.Open ();
242
243                         String database = conn.Database;
244
245                         try {
246                                 conn.ChangeDatabase ("doesnotexist");
247                                 Assert.Fail ("#1");
248                         } catch (SqlException ex) {
249                                 // Could not locate entry in sysdatabases for
250                                 // database 'doesnotexist'. No entry found with
251                                 // that name. Make sure that the name is entered
252                                 // correctly.
253                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
254                                 Assert.AreEqual ((byte) 16, ex.Class, "#3");
255                                 Assert.IsNull (ex.InnerException, "#4");
256                                 Assert.IsNotNull (ex.Message, "#5");
257                                 Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#6");
258                                 Assert.AreEqual (911, ex.Number, "#7");
259                                 Assert.AreEqual ((byte) 1, ex.State, "#8");
260
261                                 Assert.AreEqual (database, conn.Database, "#9");
262                         } finally {
263                                 conn.Close ();
264                         }
265                 }
266
267                 [Test]
268                 public void ChangeDatabase_DatabaseName_Empty ()
269                 {
270                         conn = new SqlConnection (connectionString);
271                         conn.Open ();
272                         try {
273                                 conn.ChangeDatabase (string.Empty);
274                                 Assert.Fail ("#1");
275                         } catch (ArgumentException ex) {
276                                 // Database cannot be null, the empty string,
277                                 // or string of only whitespace
278                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
279                                 Assert.IsNull (ex.InnerException, "#3");
280                                 Assert.IsNotNull (ex.Message, "#4");
281                                 Assert.IsNull (ex.ParamName);
282                         }
283                 }
284
285                 [Test]
286                 public void ChangeDatabase_DatabaseName_Null ()
287                 {
288                         conn = new SqlConnection (connectionString);
289                         conn.Open ();
290                         try {
291                                 conn.ChangeDatabase ((string) null);
292                                 Assert.Fail ("#1");
293                         } catch (ArgumentException ex) {
294                                 // Database cannot be null, the empty string,
295                                 // or string of only whitespace
296                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
297                                 Assert.IsNull (ex.InnerException, "#3");
298                                 Assert.IsNotNull (ex.Message, "#4");
299                                 Assert.IsNull (ex.ParamName);
300                         }
301                 }
302
303                 [Test] // bug #412581
304                 public void ChangeDatabase_DatabaseName_Whitespace ()
305                 {
306 #if NET_2_0
307                         Assert.Ignore ("bug #412581");
308 #endif
309
310                         conn = new SqlConnection (connectionString);
311                         conn.Open ();
312                         try {
313                                 conn.ChangeDatabase ("   ");
314                                 Assert.Fail ("#1");
315 #if NET_2_0
316                         } catch (SqlException ex) {
317                                 // Could not locate entry in sysdatabases for
318                                 // database '   '. No entry found with that name.
319                                 // Make sure that the name is entered correctly
320                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
321                                 Assert.AreEqual ((byte) 16, ex.Class, "#3");
322                                 Assert.IsNull (ex.InnerException, "#4");
323                                 Assert.IsNotNull (ex.Message, "#5");
324                                 Assert.IsTrue (ex.Message.IndexOf ("'   '") != -1, "#6");
325                                 Assert.AreEqual (911, ex.Number, "#7");
326                                 Assert.AreEqual ((byte) 1, ex.State, "#8");
327                         }
328 #else
329                         } catch (ArgumentException ex) {
330                                 // Database cannot be null, the empty string,
331                                 // or string of only whitespace
332                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
333                                 Assert.IsNull (ex.InnerException, "#3");
334                                 Assert.IsNotNull (ex.Message, "#4");
335                                 Assert.IsNull (ex.ParamName);
336                         }
337 #endif
338                 }
339
340                 [Test]
341                 public void InterfaceTransactionTest ()
342                 {
343                         conn = new SqlConnection (connectionString);
344                         conn.Open ();
345                         IDbCommand idbCommand = new SqlCommand ("use [mono-test]", conn);
346                         idbCommand.Connection = null;
347                         Assert.AreEqual (null, idbCommand.Connection, "Connection should be null");
348                         idbCommand.Transaction = null;
349                         Assert.AreEqual (null, idbCommand.Transaction, "Transaction should be null");
350
351                         conn.Close ();
352                 }
353
354                 [Test]
355                 public void BeginTransaction ()
356                 {
357                         conn = new SqlConnection (connectionString);
358                         conn.Open ();
359
360                         SqlTransaction trans = conn.BeginTransaction ();
361                         Assert.AreSame (conn, trans.Connection, "#A1");
362                         Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel, "#A2");
363                         trans.Rollback ();
364
365                         trans = conn.BeginTransaction ();
366
367                         try {
368                                 conn.BeginTransaction ();
369                                 Assert.Fail ("#B1");
370                         } catch (InvalidOperationException ex) {
371                                 // SqlConnection does not support parallel transactions
372                                 Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(), "#B2");
373                                 Assert.IsNull (ex.InnerException, "#B3");
374                                 Assert.IsNotNull (ex.Message, "#B4");
375                         } finally {
376                                 trans.Rollback();
377                         }
378
379                         try {
380                                 trans = conn.BeginTransaction ();
381                                 trans.Rollback ();
382                                 trans = conn.BeginTransaction ();
383                                 trans.Commit ();
384                                 trans = conn.BeginTransaction ();
385                         } finally {
386                                 trans.Rollback ();
387                         }
388                 }
389
390                 [Test]
391                 public void ConnectionString ()
392                 {
393                         conn = new SqlConnection (connectionString);
394                         // Test Repeated Keyoword should take the latest value
395                         conn.ConnectionString = conn.ConnectionString + ";server=RepeatedServer;";
396                         Assert.AreEqual ("RepeatedServer", ((SqlConnection)conn).DataSource, "#A1");
397                         conn.ConnectionString += ";database=gen;Initial Catalog=gen1";
398                         Assert.AreEqual ("gen1", conn.Database, "#A2");
399
400                         // Test if properties are set correctly
401                         string str = "server=localhost1;database=db;user id=user;";
402                         str += "password=pwd;Workstation ID=workstation;Packet Size=512;";
403                         str += "Connect Timeout=10";
404                         conn.ConnectionString = str;
405
406                         Assert.AreEqual ("localhost1", conn.DataSource, "#B1");
407                         Assert.AreEqual ("db", conn.Database, "#B2");
408                         Assert.AreEqual (ConnectionState.Closed, conn.State, "#B3");
409                         Assert.AreEqual ("workstation", conn.WorkstationId, "#B4");
410                         Assert.AreEqual (512, conn.PacketSize, "#B5");
411                         Assert.AreEqual (10, conn.ConnectionTimeout, "#B6");
412                         
413                         // Test if any leftover values exist from previous invocation
414                         conn.ConnectionString = connectionString;
415                         conn.ConnectionString = string.Empty;
416                         Assert.AreEqual (string.Empty, conn.DataSource, "#C1");
417                         Assert.AreEqual ("", conn.Database, "#C2");
418 #if NET_2_0
419                         Assert.AreEqual (8000, conn.PacketSize, "#C3");
420 #else
421                         Assert.AreEqual (8192, conn.PacketSize, "#C3");
422 #endif
423                         Assert.AreEqual (15, conn.ConnectionTimeout, "#C4");
424                         Assert.IsTrue (string.Compare (conn.WorkstationId, Environment.MachineName, true) == 0, "#C5");
425                 }
426
427                 [Test]
428                 public void ConnectionString_Connection_Open ()
429                 {
430                         conn = new SqlConnection (connectionString);
431                         conn.ConnectionString = connectionString;
432                         conn.Open ();
433                         try {
434                                 conn.ConnectionString = "server=localhost;database=tmp;";
435                                 Assert.Fail ("#1");
436                         } catch (InvalidOperationException ex) {
437                                 // Not allowed to change the 'ConnectionString'
438                                 // property. The connection's current state is open
439                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
440                                 Assert.IsNull (ex.InnerException, "#3");
441                                 Assert.IsNotNull (ex.Message, "#4");
442                         } finally {
443                                 conn.Close ();
444                         }
445                 }
446
447                 [Test]
448                 public void ServerVersionTest ()
449                 {
450                         conn = new SqlConnection (connectionString);
451
452                         // Test InvalidOperation Exception is thrown if Connection is CLOSED
453                         try {
454                                 string s = conn.ServerVersion;
455                                 Assert.Fail ("#A1:" + s);
456                         } catch (InvalidOperationException ex) {
457                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
458                                 Assert.IsNull (ex.InnerException, "#A3");
459                                 Assert.IsNotNull (ex.Message, "#A4");
460                         }
461                         
462                         // Test if Release Version is as per specification.
463                         conn.Open ();
464                         String [] version = conn.ServerVersion.Split ('.');
465                         Assert.AreEqual (2, version[0].Length,
466                                 "#B1 The Major release shud be exactly 2 characters");
467                         Assert.AreEqual (2, version[1].Length,
468                                 "#B2 The Minor release shud be exactly 2 characters");
469                         Assert.AreEqual (4, version[2].Length,
470                                 "#B3 The Release version should be exactly 4 digits");
471                 }
472
473                 [Test]
474                 public void Database ()
475                 {
476                         conn = new SqlConnection (connectionString);
477                         string database = conn.Database;
478
479                         // Test if database property is updated when a query changes database
480                         conn.Open ();
481                         SqlCommand cmd = new SqlCommand ("use [master]" , conn);
482                         cmd.ExecuteNonQuery ();
483                         Assert.AreEqual ("master", conn.Database, "#1");
484                         conn.Close ();
485                         Assert.AreEqual (database, conn.Database, "#2");
486
487                         // Test if the database property is reset on re-opening the connection
488                         conn.ConnectionString = connectionString;
489                         conn.Open ();
490                         Assert.AreEqual (database, conn.Database, "#3");
491                         conn.Close ();
492                 }
493
494                 [Test] // bug #412571
495                 public void Dispose ()
496                 {
497                         StateChangeEventArgs stateChangeArgs;
498                         EventArgs disposedArgs;
499
500                         conn = new SqlConnection (connectionString + ";Connection Timeout=30;Packet Size=512;Workstation Id=Desktop");
501                         conn.Disposed += new EventHandler (Connection_Disposed);
502                         conn.Open ();
503                         conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
504                         Assert.AreEqual (0, events.Count, "#A1");
505                         conn.Dispose ();
506                         Assert.AreEqual (string.Empty, conn.ConnectionString, "#A2");
507                         Assert.AreEqual (15, conn.ConnectionTimeout, "#A3");
508                         Assert.AreEqual (string.Empty, conn.Database, "#A4");
509                         Assert.AreEqual (string.Empty, conn.DataSource, "#A5");
510 #if NET_2_0
511                         Assert.AreEqual (8000, conn.PacketSize, "#A6");
512 #else
513                         Assert.AreEqual (8192, conn.PacketSize, "#A6");
514 #endif
515                         Assert.AreEqual (ConnectionState.Closed, conn.State, "#A7");
516                         Assert.IsTrue (string.Compare (conn.WorkstationId, Environment.MachineName, true) == 0, "#A8");
517                         Assert.AreEqual (2, events.Count, "#A9");
518
519                         stateChangeArgs = events [0] as StateChangeEventArgs;
520                         Assert.IsNotNull (stateChangeArgs, "#B1");
521                         Assert.AreEqual (typeof (StateChangeEventArgs), stateChangeArgs.GetType (), "#B2");
522                         Assert.AreEqual (ConnectionState.Open, stateChangeArgs.OriginalState, "#B3");
523                         Assert.AreEqual (ConnectionState.Closed, stateChangeArgs.CurrentState, "B4");
524
525                         disposedArgs = events [1] as EventArgs;
526                         Assert.IsNotNull (disposedArgs, "#C1");
527                         Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#C2");
528
529                         conn.Dispose ();
530
531                         Assert.AreEqual (ConnectionState.Closed, conn.State, "#D1");
532                         Assert.AreEqual (3, events.Count, "#D2");
533
534                         disposedArgs = events [2] as EventArgs;
535                         Assert.IsNotNull (disposedArgs, "#E1");
536                         Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#E2");
537                 }
538
539                 void Connection_StateChange (object sender , StateChangeEventArgs e)
540                 {
541                         events.Add (e);
542                 }
543
544                 void Connection_Disposed (object sender , EventArgs e)
545                 {
546                         events.Add (e);
547                 }
548
549 #if NET_2_0
550                 [Test]
551                 public void FireInfoMessageEventOnUserErrorsTest ()
552                 {
553                         conn = new SqlConnection ();
554                         Assert.AreEqual(false, conn.FireInfoMessageEventOnUserErrors, "#1 The default value should be false");
555                         conn.FireInfoMessageEventOnUserErrors = true;
556                         Assert.AreEqual(true, conn.FireInfoMessageEventOnUserErrors, "#1 The value should be true after setting the property to true");
557                 }
558
559                 [Test]
560                 public void StatisticsEnabledTest ()
561                 {
562                         conn = new SqlConnection (); 
563                         Assert.AreEqual(false, conn.StatisticsEnabled, "#1 The default value should be false");
564                         conn.StatisticsEnabled = true;
565                         Assert.AreEqual(true, conn.StatisticsEnabled, "#1 The value should be true after setting the property to true");
566                 }
567
568                 [Test]
569                 public void ChangePasswordTest ()
570                 {
571                         string tmpPassword = "modifiedbymonosqlclient";
572                         SqlConnection.ChangePassword (connectionString, tmpPassword);
573                         SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder (connectionString);
574                         string oldPassword = connBuilder.Password;
575                         connBuilder.Password = tmpPassword;
576                         SqlConnection.ChangePassword (connBuilder.ConnectionString, oldPassword); // Modify to the original password
577                 }
578 #endif
579         }
580
581 #if NET_2_0
582         [TestFixture]
583         [Category ("sqlserver")]
584         public class GetSchemaTest
585         {
586                 SqlConnection conn = null;
587                 String connectionString = ConnectionManager.Singleton.ConnectionString;
588
589                 [SetUp]
590                 public void Setup()
591                 {
592                         conn = new SqlConnection(connectionString);
593                         conn.Open();
594                 }
595
596                 [TearDown]
597                 public void TearDown()
598                 {
599                         conn.Close();
600                 }
601
602                 [Test]
603                 public void GetSchemaTest1()
604                 {
605                         bool flag = false;
606                         DataTable tab1 = conn.GetSchema("databases");
607                         foreach (DataRow row in tab1.Rows)
608                         {
609                                 foreach (DataColumn col in tab1.Columns)
610                                 {
611                                         if (col.ColumnName.ToString() == "database_name" && row[col].ToString() == "monotest")
612                                         {
613                                                 flag = true;
614                                                 break;
615                                         }
616                                 }
617                                 if (flag)
618                                         break;
619                         }
620                         Assert.AreEqual(true, flag, "#GS1 failed");
621                 }
622
623                 [Test]
624                 [ExpectedException(typeof(ArgumentException))]
625                 public void GetSchemaTest2()
626                 {
627                         conn.GetSchema(null);
628                 }
629
630                 [Test]
631                 public void GetSchemaTest3 ()
632                 {
633                         Assert.Ignore ("We currently have no foreign keys defined in the test database");
634
635                         bool flag = false;
636                         DataTable tab1 = conn.GetSchema("ForeignKeys");
637                         foreach (DataRow row in tab1.Rows)
638                         {
639                                 foreach (DataColumn col in tab1.Columns)
640                                 {
641                                         /*
642                                          * We need to consider multiple values
643                                          */
644                                         if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "tmptable1")
645                                         {
646                                                 flag = true;
647                                                 break;
648                                         }
649                                 }
650                                 if (flag)
651                                         break;
652                         }
653                         Assert.AreEqual(true, flag, "#GS3 failed");
654                 }
655
656                 [Test]
657                 public void GetSchemaTest4()
658                 {
659                         bool flag = false;
660                         DataTable tab1 = conn.GetSchema("Indexes");
661                         foreach (DataRow row in tab1.Rows)
662                         {
663                                 foreach (DataColumn col in tab1.Columns)
664                                 {
665                                         /*
666                                          * We need to consider multiple values
667                                          */
668                                         if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
669                                         {
670                                                 flag = true;
671                                                 break;
672                                         }
673                                 }
674                                 if (flag)
675                                         break;
676                         }
677                         Assert.AreEqual(true, flag, "#GS4 failed");
678                 }
679
680                 [Test]
681                 public void GetSchemaTest5()
682                 {
683                         bool flag = false;
684                         DataTable tab1 = conn.GetSchema("IndexColumns");
685                         foreach (DataRow row in tab1.Rows)
686                         {
687                                 foreach (DataColumn col in tab1.Columns)
688                                 {
689                                         /*
690                                          * We need to consider multiple values
691                                          */
692                                         if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
693                                         {
694                                                 flag = true;
695                                                 break;
696                                         }
697                                 }
698                                 if (flag)
699                                         break;
700                         }
701                         Assert.AreEqual(true, flag, "#GS5 failed");
702                 }
703
704                 [Test]
705                 public void GetSchemaTest6()
706                 {
707                         bool flag = false;
708                         DataTable tab1 = conn.GetSchema("Procedures");
709                         foreach (DataRow row in tab1.Rows)
710                         {
711                                 foreach (DataColumn col in tab1.Columns)
712                                 {
713                                         /*
714                                          * We need to consider multiple values
715                                          */
716                                         if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
717                                         {
718                                                 flag = true;
719                                                 break;
720                                         }
721                                 }
722                                 if (flag)
723                                         break;
724                         }
725                         Assert.AreEqual(true, flag, "#GS6 failed");
726                 }
727
728                 [Test]
729                 public void GetSchemaTest7()
730                 {
731                         bool flag = false;
732                         DataTable tab1 = conn.GetSchema("ProcedureParameters");
733                         foreach (DataRow row in tab1.Rows)
734                         {
735                                 foreach (DataColumn col in tab1.Columns)
736                                 {
737                                         /*
738                                          * We need to consider multiple values
739                                          */
740                                         if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
741                                         {
742                                                 flag = true;
743                                                 break;
744                                         }
745                                 }
746                                 if (flag)
747                                         break;
748                         }
749                         Assert.AreEqual(true, flag, "#GS7 failed");
750                 }
751
752                 [Test]
753                 public void GetSchemaTest8()
754                 {
755                         bool flag = false;
756                         DataTable tab1 = conn.GetSchema("Tables");
757                         foreach (DataRow row in tab1.Rows)
758                         {
759                                 foreach (DataColumn col in tab1.Columns)
760                                 {
761                                         /*
762                                          * We need to consider multiple values
763                                          */
764                                         if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
765                                         {
766                                                 flag = true;
767                                                 break;
768                                         }
769                                 }
770                                 if (flag)
771                                         break;
772                         }
773                         Assert.AreEqual(true, flag, "#GS8 failed");
774                 }
775
776                 [Test]
777                 public void GetSchemaTest9()
778                 {
779                         bool flag = false;
780                         DataTable tab1 = conn.GetSchema("Columns");
781                         foreach (DataRow row in tab1.Rows)
782                         {
783                                 foreach (DataColumn col in tab1.Columns)
784                                 {
785                                         /*
786                                          * We need to consider multiple values
787                                          */
788                                         if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
789                                         {
790                                                 flag = true;
791                                                 break;
792                                         }
793                                 }
794                                 if (flag)
795                                         break;
796                         }
797                         Assert.AreEqual(true, flag, "#GS9 failed");
798                 }
799
800                 [Test]
801                 public void GetSchemaTest10()
802                 {
803                         bool flag = false;
804                         DataTable tab1 = conn.GetSchema("Users");
805                         foreach (DataRow row in tab1.Rows)
806                         {
807                                 foreach (DataColumn col in tab1.Columns)
808                                 {
809                                         /*
810                                          * We need to consider multiple values
811                                          */
812                                         if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
813                                         {
814                                                 flag = true;
815                                                 break;
816                                         }
817                                 }
818                                 if (flag)
819                                         break;
820                         }
821                         Assert.AreEqual(true, flag, "#GS10 failed");
822                 }
823
824                 [Test]
825                 public void GetSchemaTest11 ()
826                 {
827                         Assert.Ignore ("Incorrect syntax near 'TABLE_SCHEMA'");
828
829                         bool flag = false;
830                         DataTable tab1 = conn.GetSchema("Views");
831                         flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
832                         foreach (DataRow row in tab1.Rows)
833                         {
834                                 foreach (DataColumn col in tab1.Columns)
835                                 {
836                                         /*
837                                          * We need to consider multiple values.
838                                          */
839                                         if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
840                                         {
841                                                 flag = true;
842                                                 break;
843                                         }
844                                 }
845                                 if (flag)
846                                         break;
847                         }
848                         Assert.AreEqual(true, flag, "#GS11 failed");
849                 }
850
851                 [Test]
852                 public void GetSchemaTest12 ()
853                 {
854                         Assert.Ignore ("Incorrect syntax near '('");
855
856                         bool flag = false;
857                         DataTable tab1 = conn.GetSchema("ViewColumns");
858                         flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
859                         foreach (DataRow row in tab1.Rows)
860                         {
861                                 foreach (DataColumn col in tab1.Columns)
862                                 {
863                                         /*
864                                          * We need to consider multiple values.
865                                          */
866                                         if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
867                                         {
868                                                 flag = true;
869                                                 break;
870                                         }
871                                 }
872                                 if (flag)
873                                         break;
874                         }
875                         Assert.AreEqual(true, flag, "#GS12 failed");
876                 }
877
878                 [Test]
879                 public void GetSchemaTest13 ()
880                 {
881                         Assert.Ignore ("The multi-part identifier \"assportemblies.name\" could not be bound");
882
883                         bool flag = false;
884                         DataTable tab1 = conn.GetSchema("UserDefinedTypes");
885                         flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
886                         foreach (DataRow row in tab1.Rows)
887                         {
888                                 foreach (DataColumn col in tab1.Columns)
889                                 {
890                                         /*
891                                          * We need to consider multiple values.
892                                          */
893                                         if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
894                                         {
895                                                 flag = true;
896                                                 break;
897                                         }
898                                 }
899                                 if (flag)
900                                         break;
901                         }
902                         Assert.AreEqual(true, flag, "#GS13 failed");
903                 }
904
905                 [Test]
906                 public void GetSchemaTest14()
907                 {
908                         bool flag = false;
909                         string [] restrictions = new string[4];
910
911                         restrictions[0] = "monotest";
912                         restrictions[1] = "dbo";
913                         restrictions[2] = null;
914                         restrictions[3] = "BASE TABLE";
915                         DataTable tab1 = conn.GetSchema("Tables", restrictions);
916                         foreach (DataRow row in tab1.Rows)
917                         {
918                                 foreach (DataColumn col in tab1.Columns)
919                                 {
920                                         /*
921                                          * We need to consider multiple values
922                                          */
923                                         if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
924                                         {
925                                                 flag = true;
926                                                 break;
927                                         }
928                                 }
929                                 if (flag)
930                                         break;
931                         }
932                         Assert.AreEqual(true, flag, "#GS14 failed");
933                 }
934
935                 [Test]
936                 public void GetSchemaTest15()
937                 {
938                         bool flag = false;
939                         string [] restrictions = new string[4];
940
941                         restrictions[0] = "monotest";
942                         restrictions[1] = null;
943                         restrictions[2] = "binary_family";
944                         restrictions[3] = null;
945                         DataTable tab1 = conn.GetSchema("IndexColumns", restrictions);
946                         foreach (DataRow row in tab1.Rows)
947                         {
948                                 foreach (DataColumn col in tab1.Columns)
949                                 {
950                                         /*
951                                          * We need to consider multiple values
952                                          */
953                                         if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
954                                         {
955                                                 flag = true;
956                                                 break;
957                                         }
958                                 }
959                                 if (flag)
960                                         break;
961                         }
962                         Assert.AreEqual(true, flag, "#GS15 failed");
963                 }
964
965                 [Test]
966                 public void GetSchemaTest16()
967                 {
968                         bool flag = false;
969                         string [] restrictions = new string[4];
970
971                         restrictions[0] = "monotest";
972                         restrictions[1] = null;
973                         restrictions[2] = "sp_get_age";
974                         restrictions[3] = null;
975                         DataTable tab1 = conn.GetSchema("Procedures", restrictions);
976                         foreach (DataRow row in tab1.Rows)
977                         {
978                                 foreach (DataColumn col in tab1.Columns)
979                                 {
980                                         /*
981                                          * We need to consider multiple values
982                                          */
983                                         if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "sp_get_age")
984                                         {
985                                                 flag = true;
986                                                 break;
987                                         }
988                                 }
989                                 if (flag)
990                                         break;
991                         }
992                         Assert.AreEqual(true, flag, "#GS16 failed");
993                 }
994
995                 [Test]
996                 public void GetSchemaTest17()
997                 {
998                         bool flag = false;
999                         DataTable tab1 = conn.GetSchema();
1000                         foreach (DataRow row in tab1.Rows)
1001                         {
1002                                 foreach (DataColumn col in tab1.Columns)
1003                                 {
1004                                         /*
1005                                          * We need to consider multiple values
1006                                          */
1007                                         if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
1008                                         {
1009                                                 flag = true;
1010                                                 break;
1011                                         }
1012                                 }
1013                                 if (flag)
1014                                         break;
1015                         }
1016                         Assert.AreEqual(true, flag, "#GS17 failed");
1017                 }
1018
1019                 [Test]
1020                 public void GetSchemaTest18()
1021                 {
1022                         bool flag = false;
1023                         DataTable tab1 = conn.GetSchema("RESTRICTIONS");
1024                         foreach (DataRow row in tab1.Rows)
1025                         {
1026                                 foreach (DataColumn col in tab1.Columns)
1027                                 {
1028                                         /*
1029                                          * We need to consider multiple values
1030                                          */
1031                                         if (col.ColumnName.ToString() == "RestrictionDefault" && row[col].ToString() == "CONSTRAINT_NAME")
1032                                         {
1033                                                 flag = true;
1034                                                 break;
1035                                         }
1036                                 }
1037                                 if (flag)
1038                                         break;
1039                         }
1040                         Assert.AreEqual(true, flag, "#GS18 failed");
1041                 }
1042
1043                 [Test]
1044                 [ExpectedException (typeof (ArgumentException))]
1045                 public void GetSchemaTest19 ()
1046                 {
1047                         String [] restrictions = new String[1];
1048                         conn.GetSchema("RESTRICTIONS", restrictions);
1049                 }
1050
1051                 [Test]
1052                 public void GetSchemaTest20 ()
1053                 {
1054                         bool flag = false;
1055                         DataTable tab1 = conn.GetSchema("DataTypes");
1056                         foreach (DataRow row in tab1.Rows)
1057                         {
1058                                 foreach (DataColumn col in tab1.Columns)
1059                                 {
1060                                         /*
1061                                          * We need to consider multiple values
1062                                          */
1063                                         if (col.ColumnName.ToString() == "TypeName" && row[col].ToString() == "uniqueidentifier")
1064                                         {
1065                                                 flag = true;
1066                                                 break;
1067                                         }
1068                                 }
1069                                 if (flag)
1070                                         break;
1071                         }
1072                         Assert.AreEqual(true, flag, "#GS20 failed");
1073                 }
1074
1075                 [Test]
1076                 public void GetSchemaTest21()
1077                 {
1078                         bool flag = false;
1079                         DataTable tab1 = conn.GetSchema();
1080                         foreach (DataRow row in tab1.Rows)
1081                         {
1082                                 foreach (DataColumn col in tab1.Columns)
1083                                 {
1084                                         /*
1085                                          * We need to consider multiple values
1086                                          */
1087                                         if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
1088                                         {
1089                                                 flag = true;
1090                                                 break;
1091                                         }
1092                                 }
1093                                 if (flag)
1094                                         break;
1095                         }
1096                         Assert.AreEqual(true, flag, "#GS21 failed");
1097                 }
1098                 [Test]
1099                 public void GetSchemaTest22()
1100                 {
1101                         bool flag = false;
1102                         DataTable tab1 = conn.GetSchema("ReservedWords");
1103                         foreach (DataRow row in tab1.Rows)
1104                         {
1105                                 foreach (DataColumn col in tab1.Columns)
1106                                 {
1107                                         /*
1108                                          * We need to consider multiple values
1109                                          */
1110                                         if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "UPPER")
1111                                         {
1112                                                 flag = true;
1113                                                 break;
1114                                         }
1115                                 }
1116                                 if (flag)
1117                                         break;
1118                         }
1119                         Assert.AreEqual(true, flag, "#GS22 failed");
1120                 }
1121
1122                 [Test]
1123                 public void GetSchemaTest23()
1124                 {
1125                         bool flag = false;
1126                         DataTable tab1 = conn.GetSchema("ReservedWords");
1127                         foreach (DataRow row in tab1.Rows)
1128                         {
1129                                 foreach (DataColumn col in tab1.Columns)
1130                                 {
1131                                         /*
1132                                          * We need to consider multiple values
1133                                          */
1134                                         if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "upper")
1135                                         {
1136                                                 flag = true;
1137                                                 break;
1138                                         }
1139                                 }
1140                                 if (flag)
1141                                         break;
1142                         }
1143                         Assert.AreEqual(false, flag, "#GS23 failed");
1144                 }
1145
1146                 [Test]
1147                 public void GetSchemaTest24()
1148                 {
1149                         bool flag = false;
1150                         string [] restrictions = new string[4];
1151
1152                         restrictions[0] = "monotest";
1153                         restrictions[1] = null;
1154                         restrictions[2] = "sp_get_age";
1155                         restrictions[3] = null;
1156                         DataTable tab1 = conn.GetSchema("Procedures", restrictions);
1157                         foreach (DataRow row in tab1.Rows)
1158                         {
1159                                 foreach (DataColumn col in tab1.Columns)
1160                                 {
1161                                         /*
1162                                          * We need to consider multiple values
1163                                          */
1164                                         if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "mono")
1165                                         {
1166                                                 flag = true;
1167                                                 break;
1168                                         }
1169                                 }
1170                                 if (flag)
1171                                         break;
1172                         }
1173                         Assert.AreEqual(false, flag, "#GS24 failed");
1174                 }
1175
1176                 [Test]
1177                 [ExpectedException (typeof (ArgumentException))]
1178                 public void GetSchemaTest25 ()
1179                 {
1180                         String [] restrictions = new String [1];
1181                         conn.GetSchema ("Mono", restrictions);
1182                 }
1183         }
1184 #endif
1185 }