2 // SqlConnectionTest.cs - NUnit Test Cases for testing the
5 // Senganal T (tsenganal@novell.com)
7 // Copyright (c) 2004 Novell Inc., and the individuals listed
8 // on the ChangeLog entries.
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Collections
;
33 using System
.Data
.SqlClient
;
35 using System
.Threading
;
37 using NUnit
.Framework
;
39 namespace MonoTests
.System
.Data
.Connected
.SqlClient
42 [Category ("sqlserver")]
43 public class SqlConnectionTest
46 String connectionString
;
53 events
= new ArrayList ();
54 connectionString
= ConnectionManager
.Instance
.Sql
.ConnectionString
;
55 engine
= ConnectionManager
.Instance
.Sql
.EngineConfig
;
59 public void TearDown ()
63 if (connectionString
!= null)
64 SqlConnection
.ClearAllPools ();
68 public void OverloadedConstructorTest ()
71 //do i need to check for all the synonyms..
72 conn
= new SqlConnection ("Timeout=10;Connect Timeout=20;Connection Timeout=30");
73 Assert
.AreEqual (30, conn
.ConnectionTimeout
, "#A1");
74 conn
= new SqlConnection ("Connect Timeout=100;Connection Timeout=200;Timeout=300");
75 Assert
.AreEqual (300, conn
.ConnectionTimeout
, "#A2");
76 conn
= new SqlConnection ("Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
77 Assert
.AreEqual (3000, conn
.ConnectionTimeout
, "#A3");
79 //'==' doesent work correctly in both msdotnet and mono
81 conn = new SqlConnection ("server=local==host;database=tmp;");
82 Assert.AreEqual ("local==host", conn.DataSource,
83 "# Datasource name is set incorrectly");
85 string connStr
= "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";"
86 + "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
87 conn
= new SqlConnection (connStr
);
88 Assert
.AreEqual (connStr
, conn
.ConnectionString
, "#B1");
89 Assert
.AreEqual ("loca\"lhost" , conn
.DataSource
, "#B2");
90 Assert
.AreEqual ("'Db" , conn
.Database
, "#B3");
91 Assert
.AreEqual (512 , conn
.PacketSize
, "#B4");
92 Assert
.AreEqual (20 , conn
.ConnectionTimeout
, "#B5");
93 Assert
.AreEqual ("'\"desktop" , conn
.WorkstationId
, "#B6");
99 conn
= new SqlConnection (connectionString
);
100 conn
.StateChange
+= new StateChangeEventHandler (Connection_StateChange
);
103 Assert
.AreEqual (ConnectionState
.Open
, conn
.State
, "#1");
104 Assert
.AreEqual (1, events
.Count
, "#2");
105 StateChangeEventArgs args
= events
[0] as StateChangeEventArgs
;
106 Assert
.IsNotNull (args
, "#3");
107 Assert
.AreEqual (ConnectionState
.Closed
, args
.OriginalState
, "#4");
108 Assert
.AreEqual (ConnectionState
.Open
, args
.CurrentState
, "#5");
114 public void Open_Connection_Open ()
116 conn
= new SqlConnection (connectionString
);
122 } catch (InvalidOperationException ex
) {
123 // The connection was not closed. The connection's
124 // current state is open
125 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
126 Assert
.IsNull (ex
.InnerException
, "#3");
127 Assert
.IsNotNull (ex
.Message
, "#4");
134 public void Open_ConnectionString_LoginInvalid ()
137 conn
= new SqlConnection (connectionString
+ "user id=invalidLogin");
141 } catch (SqlException ex
) {
142 // Login failed for user 'invalidLogin'
143 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
144 Assert
.AreEqual ((byte) 14, ex
.Class
, "#3");
145 Assert
.IsNull (ex
.InnerException
, "#4");
146 Assert
.IsNotNull (ex
.Message
, "#5");
147 Assert
.AreEqual (18456, ex
.Number
, "#7");
148 Assert
.AreEqual ((byte) 1, ex
.State
, "#8");
155 public void Open_ConnectionString_DatabaseInvalid ()
157 conn
= new SqlConnection (connectionString
+ "database=invalidDB");
161 } catch (SqlException ex
) {
162 // Cannot open database "invalidDB" requested
163 // by the login. The login failed
164 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
165 Assert
.AreEqual ((byte) 11, ex
.Class
, "#3");
166 Assert
.IsNull (ex
.InnerException
, "#4");
167 Assert
.IsNotNull (ex
.Message
, "#5");
168 Assert
.AreEqual (4060, ex
.Number
, "#7");
169 Assert
.AreEqual ((byte) 1, ex
.State
, "#8");
177 public void Open_ConnectionString_PasswordInvalid ()
180 conn
= new SqlConnection (connectionString
+ ";password=invalidPassword");
184 } catch (SqlException ex
) {
185 // Login failed for user '...'
186 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
187 Assert
.AreEqual ((byte) 14, ex
.Class
, "#3");
188 Assert
.IsNull (ex
.InnerException
, "#4");
189 Assert
.IsNotNull (ex
.Message
, "#5");
190 Assert
.AreEqual (18456, ex
.Number
, "#6");
191 Assert
.AreEqual ((byte) 1, ex
.State
, "#7");
198 public void Open_ConnectionString_ServerInvalid ()
200 Assert
.Ignore ("Long running");
203 conn
= new SqlConnection (connectionString
+ ";server=invalidServerName");
207 } catch (SqlException ex
) {
208 // An error has occurred while establishing a
209 // connection to the server...
210 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
211 Assert
.AreEqual ((byte) 20, ex
.Class
, "#3");
212 Assert
.IsNull (ex
.InnerException
, "#4");
213 Assert
.IsNotNull (ex
.Message
, "#5");
214 Assert
.AreEqual (53, ex
.Number
, "#6");
215 Assert
.AreEqual ((byte) 0, ex
.State
, "#7");
221 [Test
] // bug #383061
222 [Category("NotWorking")]
223 public void Open_MaxPoolSize_Reached ()
225 connectionString
+= ";Pooling=true;Connection Lifetime=6;Connect Timeout=3;Max Pool Size=2";
227 SqlConnection conn1
= new SqlConnection (connectionString
);
230 SqlConnection conn2
= new SqlConnection (connectionString
);
233 DateTime start
= DateTime
.Now
;
236 using (SqlConnection sqlConnection
= new SqlConnection (connectionString
)) {
237 sqlConnection
.Open ();
240 } catch (InvalidOperationException ex
) {
241 // System.InvalidOperationException: Timeout expired.
242 // The timeout period elapsed prior to obtaining a
243 // connection from the pool. This may have occurred
244 // because all pooled connections were in use and max
245 // pool size was reached.
246 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#A2");
247 Assert
.IsNull (ex
.InnerException
, "#A3");
248 Assert
.IsNotNull (ex
.Message
, "#A4");
251 TimeSpan elapsed
= DateTime
.Now
- start
;
253 Assert
.IsTrue (elapsed
.TotalSeconds
>= 3, "#B1:" + elapsed
.TotalSeconds
);
254 Assert
.IsTrue (elapsed
.TotalSeconds
< 4, "#B2:" + elapsed
.TotalSeconds
);
258 // as the second connection is closed, we should now be
259 // able to open a new connection (which essentially
260 // uses the pooled connection from conn2)
261 SqlConnection conn3
= new SqlConnection (connectionString
);
268 [Test
] // bug #412574
271 conn
= new SqlConnection (connectionString
);
273 conn
.StateChange
+= new StateChangeEventHandler (Connection_StateChange
);
276 Assert
.AreEqual (ConnectionState
.Closed
, conn
.State
, "#1");
277 Assert
.AreEqual (1, events
.Count
, "#2");
278 StateChangeEventArgs args
= events
[0] as StateChangeEventArgs
;
279 Assert
.IsNotNull (args
, "#3");
280 Assert
.AreEqual (ConnectionState
.Open
, args
.OriginalState
, "#4");
281 Assert
.AreEqual (ConnectionState
.Closed
, args
.CurrentState
, "5");
285 Assert
.AreEqual (1, events
.Count
, "#6");
289 public void ChangeDatabase ()
291 conn
= new SqlConnection(connectionString
);
294 if (ConnectionManager
.Instance
.Sql
.IsAzure
)
296 var exc
= Assert
.Throws
<SqlException
>(() => conn
.ChangeDatabase("master"));
297 Assert
.Equals(40508, exc
.Number
); //USE statement is not supported to switch between databases (Azure).
301 conn
.ChangeDatabase("master");
302 Assert
.AreEqual("master", conn
.Database
);
307 public void ChangeDatabase_DatabaseName_DoesNotExist ()
309 if (ConnectionManager
.Instance
.Sql
.IsAzure
)
310 Assert
.Ignore("SQL Azure doesn't support 'ChangeDatabase'");
312 conn
= new SqlConnection (connectionString
);
315 String database
= conn
.Database
;
318 conn
.ChangeDatabase ("doesnotexist");
320 } catch (SqlException ex
) {
321 // Could not locate entry in sysdatabases for
322 // database 'doesnotexist'. No entry found with
323 // that name. Make sure that the name is entered
325 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
326 Assert
.AreEqual ((byte) 16, ex
.Class
, "#3");
327 Assert
.IsNull (ex
.InnerException
, "#4");
328 Assert
.IsNotNull (ex
.Message
, "#5");
329 Assert
.IsTrue (ex
.Message
.IndexOf ("'doesnotexist'") != -1, "#6");
330 Assert
.AreEqual (911, ex
.Number
, "#7");
331 Assert
.AreEqual ((byte) 1, ex
.State
, "#8");
333 Assert
.AreEqual (database
, conn
.Database
, "#9");
340 public void ChangeDatabase_DatabaseName_Empty ()
342 conn
= new SqlConnection (connectionString
);
345 conn
.ChangeDatabase (string.Empty
);
347 } catch (ArgumentException ex
) {
348 // Database cannot be null, the empty string,
349 // or string of only whitespace
350 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
351 Assert
.IsNull (ex
.InnerException
, "#3");
352 Assert
.IsNotNull (ex
.Message
, "#4");
353 Assert
.IsNull (ex
.ParamName
);
358 public void ChangeDatabase_DatabaseName_Null ()
360 conn
= new SqlConnection (connectionString
);
363 conn
.ChangeDatabase ((string) null);
365 } catch (ArgumentException ex
) {
366 // Database cannot be null, the empty string,
367 // or string of only whitespace
368 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
369 Assert
.IsNull (ex
.InnerException
, "#3");
370 Assert
.IsNotNull (ex
.Message
, "#4");
371 Assert
.IsNull (ex
.ParamName
);
375 [Test
] // bug #412581
376 public void ChangeDatabase_DatabaseName_Whitespace ()
378 Assert
.Ignore ("bug #412581");
380 conn
= new SqlConnection (connectionString
);
383 conn
.ChangeDatabase (" ");
385 } catch (SqlException ex
) {
386 // Could not locate entry in sysdatabases for
387 // database ' '. No entry found with that name.
388 // Make sure that the name is entered correctly
389 Assert
.AreEqual (typeof (SqlException
), ex
.GetType (), "#2");
390 Assert
.AreEqual ((byte) 16, ex
.Class
, "#3");
391 Assert
.IsNull (ex
.InnerException
, "#4");
392 Assert
.IsNotNull (ex
.Message
, "#5");
393 Assert
.IsTrue (ex
.Message
.IndexOf ("' '") != -1, "#6");
394 Assert
.AreEqual (911, ex
.Number
, "#7");
395 Assert
.AreEqual ((byte) 1, ex
.State
, "#8");
400 [Category("NotWorking")]
401 public void ClearAllPools ()
403 SqlConnection conn1
= new SqlConnection (connectionString
+ ";Pooling=false");
406 int initial_connection_count
= GetConnectionCount (conn1
);
408 SqlConnection conn2
= new SqlConnection (connectionString
+ ";App=A");
412 SqlConnection conn3
= new SqlConnection (connectionString
+ ";App=B");
416 Assert
.AreEqual (initial_connection_count
+ 2, GetConnectionCount (conn1
), "#1");
418 SqlConnection
.ClearAllPools ();
420 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn1
), "#2");
424 [Test
] // bug #443131
425 [Category("NotWorking")]
426 public void ClearPool ()
428 SqlConnection conn1
= new SqlConnection (connectionString
);
431 int initial_connection_count
= GetConnectionCount (conn1
);
433 SqlConnection conn2
= new SqlConnection (connectionString
);
436 SqlConnection conn3
= new SqlConnection (connectionString
);
440 Assert
.AreEqual (initial_connection_count
+ 2, GetConnectionCount (conn1
), "#1");
442 SqlConnection
.ClearPool (conn1
);
444 // check if pooled connections that were not in use are
446 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn1
), "#2");
450 // check if connections that were in use when the pool
451 // was cleared will not be returned to the pool when
452 // closed (and are closed instead)
453 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn1
), "#3");
455 SqlConnection conn4
= new SqlConnection (connectionString
);
458 SqlConnection conn5
= new SqlConnection (connectionString
);
461 SqlConnection conn6
= new SqlConnection (connectionString
);
464 Assert
.AreEqual (initial_connection_count
+ 3, GetConnectionCount (conn1
), "#4");
469 // check if new connections are stored in the pool again
470 Assert
.AreEqual (initial_connection_count
+ 3, GetConnectionCount (conn1
), "#5");
474 Assert
.AreEqual (initial_connection_count
+ 2, GetConnectionCount (conn4
), "#6");
476 SqlConnection
.ClearPool (conn3
);
478 // the connection passed to ClearPool does not have to
480 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn4
), "#7");
482 SqlConnection conn7
= new SqlConnection (connectionString
);
486 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn4
), "#8");
488 conn3
.ConnectionString
+= ";App=B";
489 SqlConnection
.ClearPool (conn3
);
491 // check if a pool is identified by its connection string
492 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn4
), "#9");
494 SqlConnection conn8
= new SqlConnection (connectionString
);
495 SqlConnection
.ClearPool (conn8
);
497 // connection should not have been opened before to
498 // clear the corresponding pool
499 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn4
), "#10");
501 SqlConnection conn9
= new SqlConnection (connectionString
);
505 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn4
), "#11");
507 conn3
.ConnectionString
= connectionString
;
508 SqlConnection
.ClearPool (conn3
);
510 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn4
), "#12");
512 SqlConnection conn10
= new SqlConnection (connectionString
);
515 SqlConnection conn11
= new SqlConnection (connectionString
+ ";App=B");
518 SqlConnection conn12
= new SqlConnection (connectionString
+ ";App=B");
521 SqlConnection conn13
= new SqlConnection (connectionString
+ ";App=B");
529 Assert
.AreEqual (initial_connection_count
+ 4, GetConnectionCount (conn4
), "#13");
531 // check that other connection pools are not affected
532 SqlConnection
.ClearPool (conn13
);
534 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn4
), "#14");
536 SqlConnection conn14
= new SqlConnection (connectionString
);
540 // a disposed connection cannot be used to clear a pool
541 SqlConnection
.ClearPool (conn14
);
543 Assert
.AreEqual (initial_connection_count
+ 1, GetConnectionCount (conn4
), "#15");
545 SqlConnection
.ClearPool (conn4
);
547 Assert
.AreEqual (initial_connection_count
, GetConnectionCount (conn4
), "#16");
553 public void InterfaceTransactionTest ()
555 conn
= new SqlConnection (connectionString
);
557 IDbCommand idbCommand
= new SqlCommand ("use [mono-test]", conn
);
558 idbCommand
.Connection
= null;
559 Assert
.AreEqual (null, idbCommand
.Connection
, "Connection should be null");
560 idbCommand
.Transaction
= null;
561 Assert
.AreEqual (null, idbCommand
.Transaction
, "Transaction should be null");
567 public void BeginTransaction ()
569 conn
= new SqlConnection (connectionString
);
572 SqlTransaction trans
= conn
.BeginTransaction ();
573 Assert
.AreSame (conn
, trans
.Connection
, "#A1");
574 Assert
.AreEqual (IsolationLevel
.ReadCommitted
, trans
.IsolationLevel
, "#A2");
577 trans
= conn
.BeginTransaction ();
580 conn
.BeginTransaction ();
582 } catch (InvalidOperationException ex
) {
583 // SqlConnection does not support parallel transactions
584 Assert
.AreEqual (typeof(InvalidOperationException
), ex
.GetType(), "#B2");
585 Assert
.IsNull (ex
.InnerException
, "#B3");
586 Assert
.IsNotNull (ex
.Message
, "#B4");
592 trans
= conn
.BeginTransaction ();
594 trans
= conn
.BeginTransaction ();
596 trans
= conn
.BeginTransaction ();
603 public void ConnectionString ()
605 conn
= new SqlConnection (connectionString
);
606 // Test Repeated Keyoword should take the latest value
607 conn
.ConnectionString
= conn
.ConnectionString
+ ";server=RepeatedServer;";
608 Assert
.AreEqual ("RepeatedServer", ((SqlConnection
)conn
).DataSource
, "#A1");
609 conn
.ConnectionString
+= ";database=gen;Initial Catalog=gen1";
610 Assert
.AreEqual ("gen1", conn
.Database
, "#A2");
612 // Test if properties are set correctly
613 string str
= "server=localhost1;database=db;user id=user;";
614 str
+= "password=pwd;Workstation ID=workstation;Packet Size=512;";
615 str
+= "Connect Timeout=10";
616 conn
.ConnectionString
= str
;
618 Assert
.AreEqual ("localhost1", conn
.DataSource
, "#B1");
619 Assert
.AreEqual ("db", conn
.Database
, "#B2");
620 Assert
.AreEqual (ConnectionState
.Closed
, conn
.State
, "#B3");
621 Assert
.AreEqual ("workstation", conn
.WorkstationId
, "#B4");
622 Assert
.AreEqual (512, conn
.PacketSize
, "#B5");
623 Assert
.AreEqual (10, conn
.ConnectionTimeout
, "#B6");
625 // Test if any leftover values exist from previous invocation
626 conn
.ConnectionString
= connectionString
;
627 conn
.ConnectionString
= string.Empty
;
628 Assert
.AreEqual (string.Empty
, conn
.DataSource
, "#C1");
629 Assert
.AreEqual ("", conn
.Database
, "#C2");
630 Assert
.AreEqual (8000, conn
.PacketSize
, "#C3");
631 Assert
.AreEqual (15, conn
.ConnectionTimeout
, "#C4");
635 public void ConnectionString_Connection_Open ()
637 conn
= new SqlConnection (connectionString
);
638 conn
.ConnectionString
= connectionString
;
641 conn
.ConnectionString
= "server=localhost;database=tmp;";
643 } catch (InvalidOperationException ex
) {
644 // Not allowed to change the 'ConnectionString'
645 // property. The connection's current state is open
646 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#2");
647 Assert
.IsNull (ex
.InnerException
, "#3");
648 Assert
.IsNotNull (ex
.Message
, "#4");
655 public void ServerVersionTest ()
657 conn
= new SqlConnection (connectionString
);
659 // Test InvalidOperation Exception is thrown if Connection is CLOSED
661 string s
= conn
.ServerVersion
;
662 Assert
.Fail ("#A1:" + s
);
663 } catch (InvalidOperationException ex
) {
664 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#A2");
665 Assert
.IsNull (ex
.InnerException
, "#A3");
666 Assert
.IsNotNull (ex
.Message
, "#A4");
669 // Test if Release Version is as per specification.
671 String
[] version
= conn
.ServerVersion
.Split ('.');
672 Assert
.AreEqual (2, version
[0].Length
,
673 "#B1 The Major release shud be exactly 2 characters");
674 Assert
.AreEqual (2, version
[1].Length
,
675 "#B2 The Minor release shud be exactly 2 characters");
676 Assert
.AreEqual (4, version
[2].Length
,
677 "#B3 The Release version should be exactly 4 digits");
681 public void Database ()
683 if (ConnectionManager
.Instance
.Sql
.IsAzure
)
684 Assert
.Ignore("SQL Azure doesn't support 'use [db]'");
686 conn
= new SqlConnection (connectionString
);
687 string database
= conn
.Database
;
691 // Test if database property is updated when a query changes database
693 cmd
= new SqlCommand ("use [master]" , conn
);
694 cmd
.ExecuteNonQuery ();
695 Assert
.AreEqual ("master", conn
.Database
, "#1");
697 // ensure we're really in the expected database
698 if (ClientVersion
== 7)
699 cmd
.CommandText
= "SELECT name FROM sysdatabases WHERE name = 'master'";
701 cmd
.CommandText
= "SELECT name FROM sys.databases WHERE name = 'master'";
702 using (SqlDataReader dr
= cmd
.ExecuteReader ()) {
703 Assert
.IsTrue (dr
.Read (), "#2");
707 Assert
.AreEqual (database
, conn
.Database
, "#3");
709 // Test if the database property is reset on re-opening the connection
710 conn
.ConnectionString
= connectionString
;
712 Assert
.AreEqual (database
, conn
.Database
, "#4");
714 // ensure we're really in the expected database
715 cmd
.CommandText
= "SELECT fname FROM employee WHERE id = 2";
716 using (SqlDataReader dr
= cmd
.ExecuteReader ()) {
717 Assert
.IsTrue (dr
.Read (), "#5");
718 Assert
.AreEqual ("ramesh", dr
.GetValue (0), "#6");
725 [Category("NotWorking")] //https://github.com/dotnet/corefx/issues/22871
726 public void WorkstationId()
728 var connection1
= new SqlConnection (connectionString
+ ";Workstation Id=Desktop");
729 var connection2
= new SqlConnection (connectionString
);
730 connection1
.Dispose();
731 Assert
.AreEqual (Environment
.MachineName
, connection1
.WorkstationId
);
732 Assert
.AreEqual (Environment
.MachineName
, connection2
.WorkstationId
);
735 [Test
] // bug #412571
736 public void Dispose ()
738 StateChangeEventArgs stateChangeArgs
;
739 EventArgs disposedArgs
;
741 conn
= new SqlConnection (connectionString
+ ";Connection Timeout=30;Packet Size=512;Workstation Id=Desktop");
742 conn
.Disposed
+= new EventHandler (Connection_Disposed
);
744 conn
.StateChange
+= new StateChangeEventHandler (Connection_StateChange
);
745 Assert
.AreEqual (0, events
.Count
, "#A1");
747 Assert
.AreEqual (string.Empty
, conn
.ConnectionString
, "#A2");
748 Assert
.AreEqual (15, conn
.ConnectionTimeout
, "#A3");
749 Assert
.AreEqual (string.Empty
, conn
.Database
, "#A4");
750 Assert
.AreEqual (string.Empty
, conn
.DataSource
, "#A5");
751 Assert
.AreEqual (8000, conn
.PacketSize
, "#A6");
752 Assert
.AreEqual (ConnectionState
.Closed
, conn
.State
, "#A7");
753 Assert
.AreEqual (2, events
.Count
, "#A9");
755 stateChangeArgs
= events
[0] as StateChangeEventArgs
;
756 Assert
.IsNotNull (stateChangeArgs
, "#B1");
757 Assert
.AreEqual (typeof (StateChangeEventArgs
), stateChangeArgs
.GetType (), "#B2");
758 Assert
.AreEqual (ConnectionState
.Open
, stateChangeArgs
.OriginalState
, "#B3");
759 Assert
.AreEqual (ConnectionState
.Closed
, stateChangeArgs
.CurrentState
, "B4");
761 disposedArgs
= events
[1] as EventArgs
;
762 Assert
.IsNotNull (disposedArgs
, "#C1");
763 Assert
.AreEqual (typeof (EventArgs
), disposedArgs
.GetType (), "#C2");
767 Assert
.AreEqual (ConnectionState
.Closed
, conn
.State
, "#D1");
768 Assert
.AreEqual (3, events
.Count
, "#D2");
770 disposedArgs
= events
[2] as EventArgs
;
771 Assert
.IsNotNull (disposedArgs
, "#E1");
772 Assert
.AreEqual (typeof (EventArgs
), disposedArgs
.GetType (), "#E2");
775 void Connection_StateChange (object sender
, StateChangeEventArgs e
)
780 void Connection_Disposed (object sender
, EventArgs e
)
786 public void FireInfoMessageEventOnUserErrorsTest ()
788 conn
= new SqlConnection ();
789 Assert
.AreEqual(false, conn
.FireInfoMessageEventOnUserErrors
, "#1 The default value should be false");
790 conn
.FireInfoMessageEventOnUserErrors
= true;
791 Assert
.AreEqual(true, conn
.FireInfoMessageEventOnUserErrors
, "#1 The value should be true after setting the property to true");
795 public void StatisticsEnabledTest ()
797 conn
= new SqlConnection ();
798 Assert
.AreEqual(false, conn
.StatisticsEnabled
, "#1 The default value should be false");
799 conn
.StatisticsEnabled
= true;
800 Assert
.AreEqual(true, conn
.StatisticsEnabled
, "#1 The value should be true after setting the property to true");
804 [Category("NotWorking")]
805 public void ChangePasswordTest ()
807 string tmpPassword
= "modifiedbymonosqlclient";
808 SqlConnection
.ChangePassword (connectionString
, tmpPassword
);
809 SqlConnectionStringBuilder connBuilder
= new SqlConnectionStringBuilder (connectionString
);
810 string oldPassword
= connBuilder
.Password
;
811 connBuilder
.Password
= tmpPassword
;
812 SqlConnection
.ChangePassword (connBuilder
.ConnectionString
, oldPassword
); // Modify to the original password
815 static int GetConnectionCount (SqlConnection conn
)
819 SqlCommand cmd
= conn
.CreateCommand ();
820 cmd
.CommandText
= "select count(*) from master..sysprocesses where db_name(dbid) = @dbname";
821 cmd
.Parameters
.Add (new SqlParameter ("@dbname", conn
.Database
));
822 int connection_count
= (int) cmd
.ExecuteScalar ();
825 return connection_count
;
830 return (engine
.ClientVersion
);
836 [Category ("sqlserver")]
837 public class GetSchemaTest
839 SqlConnection conn
= null;
840 String connectionString
= ConnectionManager
.Instance
.Sql
.ConnectionString
;
845 conn
= new SqlConnection(connectionString
);
850 public void TearDown()
856 public void GetSchemaTest1()
858 if (ConnectionManager
.Instance
.Sql
.IsAzure
)
859 Assert
.Ignore("SQL Azure - Not supported'");
862 DataTable tab1
= conn
.GetSchema("databases");
863 foreach (DataRow row
in tab1
.Rows
)
865 foreach (DataColumn col
in tab1
.Columns
)
867 if (col
.ColumnName
.ToString() == "database_name" && row
[col
].ToString() == ConnectionManager
.Instance
.DatabaseName
)
876 Assert
.AreEqual(true, flag
, "#GS1 failed");
880 [ExpectedException(typeof(ArgumentException
))]
881 public void GetSchemaTest2()
883 conn
.GetSchema(null);
887 public void GetSchemaTest3 ()
889 Assert
.Ignore ("We currently have no foreign keys defined in the test database");
892 DataTable tab1
= conn
.GetSchema("ForeignKeys");
893 foreach (DataRow row
in tab1
.Rows
)
895 foreach (DataColumn col
in tab1
.Columns
)
898 * We need to consider multiple values
900 if (col
.ColumnName
.ToString() == "TABLE_NAME" && row
[col
].ToString() == "tmptable1")
909 Assert
.AreEqual(true, flag
, "#GS3 failed");
913 public void GetSchemaTest4()
916 DataTable tab1
= conn
.GetSchema("Indexes");
917 foreach (DataRow row
in tab1
.Rows
)
919 foreach (DataColumn col
in tab1
.Columns
)
922 * We need to consider multiple values
924 if (col
.ColumnName
.ToString() == "table_name" && row
[col
].ToString() == "binary_family")
933 Assert
.AreEqual(true, flag
, "#GS4 failed");
937 public void GetSchemaTest5()
940 DataTable tab1
= conn
.GetSchema("IndexColumns");
941 foreach (DataRow row
in tab1
.Rows
)
943 foreach (DataColumn col
in tab1
.Columns
)
946 * We need to consider multiple values
948 if (col
.ColumnName
.ToString() == "table_name" && row
[col
].ToString() == "binary_family")
957 Assert
.AreEqual(true, flag
, "#GS5 failed");
961 public void GetSchemaTest6()
964 DataTable tab1
= conn
.GetSchema("Procedures");
965 foreach (DataRow row
in tab1
.Rows
)
967 foreach (DataColumn col
in tab1
.Columns
)
970 * We need to consider multiple values
972 if (col
.ColumnName
.ToString() == "SPECIFIC_NAME" && row
[col
].ToString() == "sp_get_age")
981 Assert
.AreEqual(true, flag
, "#GS6 failed");
985 public void GetSchemaTest7()
988 DataTable tab1
= conn
.GetSchema("ProcedureParameters");
989 foreach (DataRow row
in tab1
.Rows
)
991 foreach (DataColumn col
in tab1
.Columns
)
994 * We need to consider multiple values
996 if (col
.ColumnName
.ToString() == "SPECIFIC_NAME" && row
[col
].ToString() == "sp_get_age")
1005 Assert
.AreEqual(true, flag
, "#GS7 failed");
1009 public void GetSchemaTest8()
1012 DataTable tab1
= conn
.GetSchema("Tables");
1013 foreach (DataRow row
in tab1
.Rows
)
1015 foreach (DataColumn col
in tab1
.Columns
)
1018 * We need to consider multiple values
1020 if (col
.ColumnName
.ToString() == "TABLE_NAME" && row
[col
].ToString() == "binary_family")
1029 Assert
.AreEqual(true, flag
, "#GS8 failed");
1033 public void GetSchemaTest9()
1035 if (ConnectionManager
.Instance
.Sql
.IsAzure
)
1036 Assert
.Ignore("SQL Azure - Not supported'");
1039 DataTable tab1
= conn
.GetSchema("Columns");
1040 foreach (DataRow row
in tab1
.Rows
)
1042 foreach (DataColumn col
in tab1
.Columns
)
1045 * We need to consider multiple values
1047 if (col
.ColumnName
.ToString() == "TABLE_NAME" && row
[col
].ToString() == "binary_family")
1056 Assert
.AreEqual(true, flag
, "#GS9 failed");
1060 public void GetSchemaTest10()
1063 DataTable tab1
= conn
.GetSchema("Users");
1064 foreach (DataRow row
in tab1
.Rows
)
1066 foreach (DataColumn col
in tab1
.Columns
)
1069 * We need to consider multiple values
1071 if (col
.ColumnName
.ToString() == "user_name" && row
[col
].ToString() == "public")
1080 Assert
.AreEqual(true, flag
, "#GS10 failed");
1084 public void GetSchemaTest11 ()
1086 Assert
.Ignore ("Incorrect syntax near 'TABLE_SCHEMA'");
1089 DataTable tab1
= conn
.GetSchema("Views");
1090 flag
= true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
1091 foreach (DataRow row
in tab1
.Rows
)
1093 foreach (DataColumn col
in tab1
.Columns
)
1096 * We need to consider multiple values.
1098 if (col
.ColumnName
.ToString() == "user_name" && row
[col
].ToString() == "public")
1107 Assert
.AreEqual(true, flag
, "#GS11 failed");
1111 public void GetSchemaTest12 ()
1113 Assert
.Ignore ("Incorrect syntax near '('");
1116 DataTable tab1
= conn
.GetSchema("ViewColumns");
1117 flag
= true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
1118 foreach (DataRow row
in tab1
.Rows
)
1120 foreach (DataColumn col
in tab1
.Columns
)
1123 * We need to consider multiple values.
1125 if (col
.ColumnName
.ToString() == "user_name" && row
[col
].ToString() == "public")
1134 Assert
.AreEqual(true, flag
, "#GS12 failed");
1138 public void GetSchemaTest13 ()
1140 Assert
.Ignore ("The multi-part identifier \"assportemblies.name\" could not be bound");
1143 DataTable tab1
= conn
.GetSchema("UserDefinedTypes");
1144 flag
= true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
1145 foreach (DataRow row
in tab1
.Rows
)
1147 foreach (DataColumn col
in tab1
.Columns
)
1150 * We need to consider multiple values.
1152 if (col
.ColumnName
.ToString() == "user_name" && row
[col
].ToString() == "public")
1161 Assert
.AreEqual(true, flag
, "#GS13 failed");
1165 [Ignore("TODO: fix restrictions")]
1166 public void GetSchemaTest14()
1169 string [] restrictions
= new string[4];
1171 restrictions
[0] = ConnectionManager
.Instance
.DatabaseName
;
1172 restrictions
[1] = "dbo";
1173 restrictions
[2] = null;
1174 restrictions
[3] = "BASE TABLE";
1175 DataTable tab1
= conn
.GetSchema("Tables", restrictions
);
1176 foreach (DataRow row
in tab1
.Rows
)
1178 foreach (DataColumn col
in tab1
.Columns
)
1181 * We need to consider multiple values
1183 if (col
.ColumnName
.ToString() == "TABLE_NAME" && row
[col
].ToString() == "binary_family")
1192 Assert
.AreEqual(true, flag
, "#GS14 failed");
1196 [Ignore("TODO: fix restrictions")]
1197 public void GetSchemaTest15()
1200 string [] restrictions
= new string[4];
1202 restrictions
[0] = ConnectionManager
.Instance
.DatabaseName
;
1203 restrictions
[1] = null;
1204 restrictions
[2] = "binary_family";
1205 restrictions
[3] = null;
1206 DataTable tab1
= conn
.GetSchema("IndexColumns", restrictions
);
1207 foreach (DataRow row
in tab1
.Rows
)
1209 foreach (DataColumn col
in tab1
.Columns
)
1212 * We need to consider multiple values
1214 if (col
.ColumnName
.ToString() == "table_name" && row
[col
].ToString() == "binary_family")
1223 Assert
.AreEqual(true, flag
, "#GS15 failed");
1227 [Ignore("TODO: fix restrictions")]
1228 public void GetSchemaTest16()
1231 string [] restrictions
= new string[4];
1233 restrictions
[0] = ConnectionManager
.Instance
.DatabaseName
;
1234 restrictions
[1] = null;
1235 restrictions
[2] = "sp_get_age";
1236 restrictions
[3] = null;
1237 DataTable tab1
= conn
.GetSchema("Procedures", restrictions
);
1238 foreach (DataRow row
in tab1
.Rows
)
1240 foreach (DataColumn col
in tab1
.Columns
)
1243 * We need to consider multiple values
1245 if (col
.ColumnName
.ToString() == "ROUTINE_NAME" && row
[col
].ToString() == "sp_get_age")
1254 Assert
.AreEqual(true, flag
, "#GS16 failed");
1258 public void GetSchemaTest17()
1261 DataTable tab1
= conn
.GetSchema();
1262 foreach (DataRow row
in tab1
.Rows
)
1264 foreach (DataColumn col
in tab1
.Columns
)
1267 * We need to consider multiple values
1269 if (col
.ColumnName
.ToString() == "CollectionName" && row
[col
].ToString() == "UserDefinedTypes")
1278 Assert
.AreEqual(true, flag
, "#GS17 failed");
1282 public void GetSchemaTest18()
1285 DataTable tab1
= conn
.GetSchema("RESTRICTIONS");
1286 foreach (DataRow row
in tab1
.Rows
)
1288 foreach (DataColumn col
in tab1
.Columns
)
1291 * We need to consider multiple values
1293 if (col
.ColumnName
.ToString() == "RestrictionDefault" && row
[col
].ToString() == "CONSTRAINT_NAME")
1302 Assert
.AreEqual(true, flag
, "#GS18 failed");
1306 [ExpectedException (typeof (ArgumentException
))]
1307 public void GetSchemaTest19 ()
1309 String
[] restrictions
= new String
[1];
1310 conn
.GetSchema("RESTRICTIONS", restrictions
);
1314 public void GetSchemaTest20 ()
1317 DataTable tab1
= conn
.GetSchema("DataTypes");
1318 foreach (DataRow row
in tab1
.Rows
)
1320 foreach (DataColumn col
in tab1
.Columns
)
1323 * We need to consider multiple values
1325 if (col
.ColumnName
.ToString() == "TypeName" && row
[col
].ToString() == "uniqueidentifier")
1334 Assert
.AreEqual(true, flag
, "#GS20 failed");
1338 public void GetSchemaTest21()
1341 DataTable tab1
= conn
.GetSchema();
1342 foreach (DataRow row
in tab1
.Rows
)
1344 foreach (DataColumn col
in tab1
.Columns
)
1347 * We need to consider multiple values
1349 if (col
.ColumnName
.ToString() == "CollectionName" && row
[col
].ToString() == "UserDefinedTypes")
1358 Assert
.AreEqual(true, flag
, "#GS21 failed");
1361 public void GetSchemaTest22()
1364 DataTable tab1
= conn
.GetSchema("ReservedWords");
1365 foreach (DataRow row
in tab1
.Rows
)
1367 foreach (DataColumn col
in tab1
.Columns
)
1370 * We need to consider multiple values
1372 if (col
.ColumnName
.ToString() == "ReservedWord" && row
[col
].ToString() == "UPPER")
1381 Assert
.AreEqual(true, flag
, "#GS22 failed");
1385 public void GetSchemaTest23()
1388 DataTable tab1
= conn
.GetSchema("ReservedWords");
1389 foreach (DataRow row
in tab1
.Rows
)
1391 foreach (DataColumn col
in tab1
.Columns
)
1394 * We need to consider multiple values
1396 if (col
.ColumnName
.ToString() == "ReservedWord" && row
[col
].ToString() == "upper")
1405 Assert
.AreEqual(false, flag
, "#GS23 failed");
1409 public void GetSchemaTest24()
1412 string [] restrictions
= new string[4];
1414 restrictions
[0] = ConnectionManager
.Instance
.DatabaseName
;
1415 restrictions
[1] = null;
1416 restrictions
[2] = "sp_get_age";
1417 restrictions
[3] = null;
1418 DataTable tab1
= conn
.GetSchema("Procedures", restrictions
);
1419 foreach (DataRow row
in tab1
.Rows
)
1421 foreach (DataColumn col
in tab1
.Columns
)
1424 * We need to consider multiple values
1426 if (col
.ColumnName
.ToString() == "ROUTINE_NAME" && row
[col
].ToString() == "mono")
1435 Assert
.AreEqual(false, flag
, "#GS24 failed");
1439 [ExpectedException (typeof (ArgumentException
))]
1440 public void GetSchemaTest25 ()
1442 String
[] restrictions
= new String
[1];
1443 conn
.GetSchema ("Mono", restrictions
);