| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448 |
- //
- // SqlConnectionTest.cs - NUnit Test Cases for testing the
- // SqlConnection class
- // Author:
- // Senganal T ([email protected])
- //
- // Copyright (c) 2004 Novell Inc., and the individuals listed
- // on the ChangeLog entries.
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.Data;
- using System.Data.SqlClient;
- using System.Net;
- using System.Threading;
- using NUnit.Framework;
- namespace MonoTests.System.Data.Connected.SqlClient
- {
- [TestFixture]
- [Category ("sqlserver")]
- public class SqlConnectionTest
- {
- SqlConnection conn;
- String connectionString;
- ArrayList events;
- EngineConfig engine;
- [SetUp]
- public void SetUp ()
- {
- events = new ArrayList ();
- connectionString = ConnectionManager.Instance.Sql.ConnectionString;
- engine = ConnectionManager.Instance.Sql.EngineConfig;
- }
- [TearDown]
- public void TearDown ()
- {
- if (conn != null)
- conn.Dispose ();
- if (connectionString != null)
- SqlConnection.ClearAllPools ();
- }
- [Test]
- public void OverloadedConstructorTest ()
- {
- //check synonyms.
- //do i need to check for all the synonyms..
- conn = new SqlConnection ("Timeout=10;Connect Timeout=20;Connection Timeout=30");
- Assert.AreEqual (30, conn.ConnectionTimeout, "#A1");
- conn = new SqlConnection ("Connect Timeout=100;Connection Timeout=200;Timeout=300");
- Assert.AreEqual (300, conn.ConnectionTimeout, "#A2");
- conn = new SqlConnection ("Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
- Assert.AreEqual (3000, conn.ConnectionTimeout, "#A3");
- //'==' doesent work correctly in both msdotnet and mono
- /*
- conn = new SqlConnection ("server=local==host;database=tmp;");
- Assert.AreEqual ("local==host", conn.DataSource,
- "# Datasource name is set incorrectly");
- */
- string connStr = "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";"
- + "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
- conn = new SqlConnection (connStr);
- Assert.AreEqual (connStr , conn.ConnectionString , "#B1");
- Assert.AreEqual ("loca\"lhost" , conn.DataSource , "#B2");
- Assert.AreEqual ("'Db" , conn.Database , "#B3");
- Assert.AreEqual (512 , conn.PacketSize , "#B4");
- Assert.AreEqual (20 , conn.ConnectionTimeout , "#B5");
- Assert.AreEqual ("'\"desktop" , conn.WorkstationId , "#B6");
- }
- [Test]
- public void Open ()
- {
- conn = new SqlConnection (connectionString);
- conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
- conn.Open ();
- Assert.AreEqual (ConnectionState.Open, conn.State, "#1");
- Assert.AreEqual (1, events.Count, "#2");
- StateChangeEventArgs args = events [0] as StateChangeEventArgs;
- Assert.IsNotNull (args, "#3");
- Assert.AreEqual (ConnectionState.Closed, args.OriginalState, "#4");
- Assert.AreEqual (ConnectionState.Open, args.CurrentState, "#5");
- conn.Close ();
- }
- [Test]
- public void Open_Connection_Open ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- try {
- conn.Open ();
- Assert.Fail ("#1");
- } catch (InvalidOperationException ex) {
- // The connection was not closed. The connection's
- // current state is open
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void Open_ConnectionString_LoginInvalid ()
- {
- // login invalid
- conn = new SqlConnection (connectionString + "user id=invalidLogin");
- try {
- conn.Open ();
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // Login failed for user 'invalidLogin'
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 14, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (18456, ex.Number, "#7");
- Assert.AreEqual ((byte) 1, ex.State, "#8");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void Open_ConnectionString_DatabaseInvalid ()
- {
- conn = new SqlConnection (connectionString + "database=invalidDB");
- try {
- conn.Open ();
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // Cannot open database "invalidDB" requested
- // by the login. The login failed
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 11, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (4060, ex.Number, "#7");
- Assert.AreEqual ((byte) 1, ex.State, "#8");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void Open_ConnectionString_PasswordInvalid ()
- {
- // password invalid
- conn = new SqlConnection (connectionString + ";password=invalidPassword");
- try {
- conn.Open ();
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // Login failed for user '...'
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 14, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (18456, ex.Number, "#6");
- Assert.AreEqual ((byte) 1, ex.State, "#7");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void Open_ConnectionString_ServerInvalid ()
- {
- Assert.Ignore ("Long running");
- // server invalid
- conn = new SqlConnection (connectionString + ";server=invalidServerName");
- try {
- conn.Open ();
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // An error has occurred while establishing a
- // connection to the server...
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 20, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.AreEqual (53, ex.Number, "#6");
- Assert.AreEqual ((byte) 0, ex.State, "#7");
- } finally {
- conn.Close ();
- }
- }
- [Test] // bug #383061
- [Category("NotWorking")]
- public void Open_MaxPoolSize_Reached ()
- {
- connectionString += ";Pooling=true;Connection Lifetime=6;Connect Timeout=3;Max Pool Size=2";
- SqlConnection conn1 = new SqlConnection (connectionString);
- conn1.Open ();
- SqlConnection conn2 = new SqlConnection (connectionString);
- conn2.Open ();
- DateTime start = DateTime.Now;
- try {
- using (SqlConnection sqlConnection = new SqlConnection (connectionString)) {
- sqlConnection.Open ();
- }
- Assert.Fail ("#A1");
- } catch (InvalidOperationException ex) {
- // System.InvalidOperationException: Timeout expired.
- // The timeout period elapsed prior to obtaining a
- // connection from the pool. This may have occurred
- // because all pooled connections were in use and max
- // pool size was reached.
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- }
- TimeSpan elapsed = DateTime.Now - start;
- Assert.IsTrue (elapsed.TotalSeconds >= 3, "#B1:" + elapsed.TotalSeconds);
- Assert.IsTrue (elapsed.TotalSeconds < 4, "#B2:" + elapsed.TotalSeconds);
- conn2.Close ();
- // as the second connection is closed, we should now be
- // able to open a new connection (which essentially
- // uses the pooled connection from conn2)
- SqlConnection conn3 = new SqlConnection (connectionString);
- conn3.Open ();
- conn3.Close ();
- conn1.Close ();
- }
- [Test] // bug #412574
- public void Close ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
- conn.Close ();
- Assert.AreEqual (ConnectionState.Closed, conn.State, "#1");
- Assert.AreEqual (1, events.Count, "#2");
- StateChangeEventArgs args = events [0] as StateChangeEventArgs;
- Assert.IsNotNull (args, "#3");
- Assert.AreEqual (ConnectionState.Open, args.OriginalState, "#4");
- Assert.AreEqual (ConnectionState.Closed, args.CurrentState, "5");
- conn.Close ();
- Assert.AreEqual (1, events.Count, "#6");
- }
- [Test]
- public void ChangeDatabase ()
- {
- conn = new SqlConnection(connectionString);
- conn.Open();
- if (ConnectionManager.Instance.Sql.IsAzure)
- {
- var exc = Assert.Throws<SqlException>(() => conn.ChangeDatabase("master"));
- Assert.Equals(40508, exc.Number); //USE statement is not supported to switch between databases (Azure).
- }
- else
- {
- conn.ChangeDatabase("master");
- Assert.AreEqual("master", conn.Database);
- }
- }
- [Test]
- public void ChangeDatabase_DatabaseName_DoesNotExist ()
- {
- if (ConnectionManager.Instance.Sql.IsAzure)
- Assert.Ignore("SQL Azure doesn't support 'ChangeDatabase'");
- conn = new SqlConnection (connectionString);
- conn.Open ();
- String database = conn.Database;
- try {
- conn.ChangeDatabase ("doesnotexist");
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // Could not locate entry in sysdatabases for
- // database 'doesnotexist'. No entry found with
- // that name. Make sure that the name is entered
- // correctly.
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 16, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.IsTrue (ex.Message.IndexOf ("'doesnotexist'") != -1, "#6");
- Assert.AreEqual (911, ex.Number, "#7");
- Assert.AreEqual ((byte) 1, ex.State, "#8");
- Assert.AreEqual (database, conn.Database, "#9");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void ChangeDatabase_DatabaseName_Empty ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- try {
- conn.ChangeDatabase (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Database cannot be null, the empty string,
- // or string of only whitespace
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName);
- }
- }
- [Test]
- public void ChangeDatabase_DatabaseName_Null ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- try {
- conn.ChangeDatabase ((string) null);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Database cannot be null, the empty string,
- // or string of only whitespace
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName);
- }
- }
- [Test] // bug #412581
- public void ChangeDatabase_DatabaseName_Whitespace ()
- {
- Assert.Ignore ("bug #412581");
- conn = new SqlConnection (connectionString);
- conn.Open ();
- try {
- conn.ChangeDatabase (" ");
- Assert.Fail ("#1");
- } catch (SqlException ex) {
- // Could not locate entry in sysdatabases for
- // database ' '. No entry found with that name.
- // Make sure that the name is entered correctly
- Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
- Assert.AreEqual ((byte) 16, ex.Class, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- Assert.IsTrue (ex.Message.IndexOf ("' '") != -1, "#6");
- Assert.AreEqual (911, ex.Number, "#7");
- Assert.AreEqual ((byte) 1, ex.State, "#8");
- }
- }
- [Test]
- [Category("NotWorking")]
- public void ClearAllPools ()
- {
- SqlConnection conn1 = new SqlConnection (connectionString + ";Pooling=false");
- conn1.Open ();
- int initial_connection_count = GetConnectionCount (conn1);
- SqlConnection conn2 = new SqlConnection (connectionString + ";App=A");
- conn2.Open ();
- conn2.Close ();
- SqlConnection conn3 = new SqlConnection (connectionString + ";App=B");
- conn3.Open ();
- conn3.Close ();
- Assert.AreEqual (initial_connection_count + 2, GetConnectionCount (conn1), "#1");
- SqlConnection.ClearAllPools ();
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn1), "#2");
- conn1.Close ();
- }
- [Test] // bug #443131
- [Category("NotWorking")]
- public void ClearPool ()
- {
- SqlConnection conn1 = new SqlConnection (connectionString);
- conn1.Open ();
- int initial_connection_count = GetConnectionCount (conn1);
- SqlConnection conn2 = new SqlConnection (connectionString);
- conn2.Open ();
- SqlConnection conn3 = new SqlConnection (connectionString);
- conn3.Open ();
- conn3.Close ();
- Assert.AreEqual (initial_connection_count + 2, GetConnectionCount (conn1), "#1");
- SqlConnection.ClearPool (conn1);
- // check if pooled connections that were not in use are
- // actually closed
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn1), "#2");
- conn2.Close ();
- // check if connections that were in use when the pool
- // was cleared will not be returned to the pool when
- // closed (and are closed instead)
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn1), "#3");
- SqlConnection conn4 = new SqlConnection (connectionString);
- conn4.Open ();
- SqlConnection conn5 = new SqlConnection (connectionString);
- conn5.Open ();
- SqlConnection conn6 = new SqlConnection (connectionString);
- conn6.Open ();
- Assert.AreEqual (initial_connection_count + 3, GetConnectionCount (conn1), "#4");
- conn5.Close ();
- conn6.Close ();
- // check if new connections are stored in the pool again
- Assert.AreEqual (initial_connection_count + 3, GetConnectionCount (conn1), "#5");
- conn1.Close ();
- Assert.AreEqual (initial_connection_count + 2, GetConnectionCount (conn4), "#6");
- SqlConnection.ClearPool (conn3);
- // the connection passed to ClearPool does not have to
- // be open
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn4), "#7");
- SqlConnection conn7 = new SqlConnection (connectionString);
- conn7.Open ();
- conn7.Close ();
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn4), "#8");
- conn3.ConnectionString += ";App=B";
- SqlConnection.ClearPool (conn3);
- // check if a pool is identified by its connection string
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn4), "#9");
- SqlConnection conn8 = new SqlConnection (connectionString);
- SqlConnection.ClearPool (conn8);
- // connection should not have been opened before to
- // clear the corresponding pool
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn4), "#10");
- SqlConnection conn9 = new SqlConnection (connectionString);
- conn9.Open ();
- conn9.Close ();
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn4), "#11");
- conn3.ConnectionString = connectionString;
- SqlConnection.ClearPool (conn3);
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn4), "#12");
- SqlConnection conn10 = new SqlConnection (connectionString);
- conn10.Open ();
- SqlConnection conn11 = new SqlConnection (connectionString + ";App=B");
- conn11.Open ();
- SqlConnection conn12 = new SqlConnection (connectionString + ";App=B");
- conn12.Open ();
- SqlConnection conn13 = new SqlConnection (connectionString + ";App=B");
- conn13.Open ();
- conn10.Close ();
- conn11.Close ();
- conn12.Close ();
- conn13.Close ();
- Assert.AreEqual (initial_connection_count + 4, GetConnectionCount (conn4), "#13");
- // check that other connection pools are not affected
- SqlConnection.ClearPool (conn13);
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn4), "#14");
- SqlConnection conn14 = new SqlConnection (connectionString);
- conn14.Open ();
- conn14.Dispose ();
- // a disposed connection cannot be used to clear a pool
- SqlConnection.ClearPool (conn14);
- Assert.AreEqual (initial_connection_count + 1, GetConnectionCount (conn4), "#15");
- SqlConnection.ClearPool (conn4);
- Assert.AreEqual (initial_connection_count, GetConnectionCount (conn4), "#16");
- conn4.Close ();
- }
- [Test]
- public void InterfaceTransactionTest ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- IDbCommand idbCommand = new SqlCommand ("use [mono-test]", conn);
- idbCommand.Connection = null;
- Assert.AreEqual (null, idbCommand.Connection, "Connection should be null");
- idbCommand.Transaction = null;
- Assert.AreEqual (null, idbCommand.Transaction, "Transaction should be null");
- conn.Close ();
- }
- [Test]
- public void BeginTransaction ()
- {
- conn = new SqlConnection (connectionString);
- conn.Open ();
- SqlTransaction trans = conn.BeginTransaction ();
- Assert.AreSame (conn, trans.Connection, "#A1");
- Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel, "#A2");
- trans.Rollback ();
- trans = conn.BeginTransaction ();
- try {
- conn.BeginTransaction ();
- Assert.Fail ("#B1");
- } catch (InvalidOperationException ex) {
- // SqlConnection does not support parallel transactions
- Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- } finally {
- trans.Rollback();
- }
- try {
- trans = conn.BeginTransaction ();
- trans.Rollback ();
- trans = conn.BeginTransaction ();
- trans.Commit ();
- trans = conn.BeginTransaction ();
- } finally {
- trans.Rollback ();
- }
- }
- [Test]
- public void ConnectionString ()
- {
- conn = new SqlConnection (connectionString);
- // Test Repeated Keyoword should take the latest value
- conn.ConnectionString = conn.ConnectionString + ";server=RepeatedServer;";
- Assert.AreEqual ("RepeatedServer", ((SqlConnection)conn).DataSource, "#A1");
- conn.ConnectionString += ";database=gen;Initial Catalog=gen1";
- Assert.AreEqual ("gen1", conn.Database, "#A2");
- // Test if properties are set correctly
- string str = "server=localhost1;database=db;user id=user;";
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Not a secret.")]
- str += "password=pwd;Workstation ID=workstation;Packet Size=512;";
- str += "Connect Timeout=10";
- conn.ConnectionString = str;
- Assert.AreEqual ("localhost1", conn.DataSource, "#B1");
- Assert.AreEqual ("db", conn.Database, "#B2");
- Assert.AreEqual (ConnectionState.Closed, conn.State, "#B3");
- Assert.AreEqual ("workstation", conn.WorkstationId, "#B4");
- Assert.AreEqual (512, conn.PacketSize, "#B5");
- Assert.AreEqual (10, conn.ConnectionTimeout, "#B6");
-
- // Test if any leftover values exist from previous invocation
- conn.ConnectionString = connectionString;
- conn.ConnectionString = string.Empty;
- Assert.AreEqual (string.Empty, conn.DataSource, "#C1");
- Assert.AreEqual ("", conn.Database, "#C2");
- Assert.AreEqual (8000, conn.PacketSize, "#C3");
- Assert.AreEqual (15, conn.ConnectionTimeout, "#C4");
- }
- [Test]
- public void ConnectionString_Connection_Open ()
- {
- conn = new SqlConnection (connectionString);
- conn.ConnectionString = connectionString;
- conn.Open ();
- try {
- conn.ConnectionString = "server=localhost;database=tmp;";
- Assert.Fail ("#1");
- } catch (InvalidOperationException ex) {
- // Not allowed to change the 'ConnectionString'
- // property. The connection's current state is open
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- } finally {
- conn.Close ();
- }
- }
- [Test]
- public void ServerVersionTest ()
- {
- conn = new SqlConnection (connectionString);
- // Test InvalidOperation Exception is thrown if Connection is CLOSED
- try {
- string s = conn.ServerVersion;
- Assert.Fail ("#A1:" + s);
- } catch (InvalidOperationException ex) {
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- }
-
- // Test if Release Version is as per specification.
- conn.Open ();
- String [] version = conn.ServerVersion.Split ('.');
- Assert.AreEqual (2, version[0].Length,
- "#B1 The Major release shud be exactly 2 characters");
- Assert.AreEqual (2, version[1].Length,
- "#B2 The Minor release shud be exactly 2 characters");
- Assert.AreEqual (4, version[2].Length,
- "#B3 The Release version should be exactly 4 digits");
- }
- [Test]
- public void Database ()
- {
- if (ConnectionManager.Instance.Sql.IsAzure)
- Assert.Ignore("SQL Azure doesn't support 'use [db]'");
- conn = new SqlConnection (connectionString);
- string database = conn.Database;
- SqlCommand cmd;
- // Test if database property is updated when a query changes database
- conn.Open ();
- cmd = new SqlCommand ("use [master]" , conn);
- cmd.ExecuteNonQuery ();
- Assert.AreEqual ("master", conn.Database, "#1");
- // ensure we're really in the expected database
- if (ClientVersion == 7)
- cmd.CommandText = "SELECT name FROM sysdatabases WHERE name = 'master'";
- else
- cmd.CommandText = "SELECT name FROM sys.databases WHERE name = 'master'";
- using (SqlDataReader dr = cmd.ExecuteReader ()) {
- Assert.IsTrue (dr.Read (), "#2");
- }
- conn.Close ();
- Assert.AreEqual (database, conn.Database, "#3");
- // Test if the database property is reset on re-opening the connection
- conn.ConnectionString = connectionString;
- conn.Open ();
- Assert.AreEqual (database, conn.Database, "#4");
- // ensure we're really in the expected database
- cmd.CommandText = "SELECT fname FROM employee WHERE id = 2";
- using (SqlDataReader dr = cmd.ExecuteReader ()) {
- Assert.IsTrue (dr.Read (), "#5");
- Assert.AreEqual ("ramesh", dr.GetValue (0), "#6");
- }
- conn.Close ();
- }
- [Test]
- [Category("NotWorking")] //https://github.com/dotnet/corefx/issues/22871
- public void WorkstationId()
- {
- var connection1 = new SqlConnection (connectionString + ";Workstation Id=Desktop");
- var connection2 = new SqlConnection (connectionString);
- connection1.Dispose();
- Assert.AreEqual (Environment.MachineName, connection1.WorkstationId);
- Assert.AreEqual (Environment.MachineName, connection2.WorkstationId);
- }
- [Test] // bug #412571
- public void Dispose ()
- {
- StateChangeEventArgs stateChangeArgs;
- EventArgs disposedArgs;
- conn = new SqlConnection (connectionString + ";Connection Timeout=30;Packet Size=512;Workstation Id=Desktop");
- conn.Disposed += new EventHandler (Connection_Disposed);
- conn.Open ();
- conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
- Assert.AreEqual (0, events.Count, "#A1");
- conn.Dispose ();
- Assert.AreEqual (string.Empty, conn.ConnectionString, "#A2");
- Assert.AreEqual (15, conn.ConnectionTimeout, "#A3");
- Assert.AreEqual (string.Empty, conn.Database, "#A4");
- Assert.AreEqual (string.Empty, conn.DataSource, "#A5");
- Assert.AreEqual (8000, conn.PacketSize, "#A6");
- Assert.AreEqual (ConnectionState.Closed, conn.State, "#A7");
- Assert.AreEqual (2, events.Count, "#A9");
- stateChangeArgs = events [0] as StateChangeEventArgs;
- Assert.IsNotNull (stateChangeArgs, "#B1");
- Assert.AreEqual (typeof (StateChangeEventArgs), stateChangeArgs.GetType (), "#B2");
- Assert.AreEqual (ConnectionState.Open, stateChangeArgs.OriginalState, "#B3");
- Assert.AreEqual (ConnectionState.Closed, stateChangeArgs.CurrentState, "B4");
- disposedArgs = events [1] as EventArgs;
- Assert.IsNotNull (disposedArgs, "#C1");
- Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#C2");
- conn.Dispose ();
- Assert.AreEqual (ConnectionState.Closed, conn.State, "#D1");
- Assert.AreEqual (3, events.Count, "#D2");
- disposedArgs = events [2] as EventArgs;
- Assert.IsNotNull (disposedArgs, "#E1");
- Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#E2");
- }
- void Connection_StateChange (object sender , StateChangeEventArgs e)
- {
- events.Add (e);
- }
- void Connection_Disposed (object sender , EventArgs e)
- {
- events.Add (e);
- }
- [Test]
- public void FireInfoMessageEventOnUserErrorsTest ()
- {
- conn = new SqlConnection ();
- Assert.AreEqual(false, conn.FireInfoMessageEventOnUserErrors, "#1 The default value should be false");
- conn.FireInfoMessageEventOnUserErrors = true;
- Assert.AreEqual(true, conn.FireInfoMessageEventOnUserErrors, "#1 The value should be true after setting the property to true");
- }
- [Test]
- public void StatisticsEnabledTest ()
- {
- conn = new SqlConnection ();
- Assert.AreEqual(false, conn.StatisticsEnabled, "#1 The default value should be false");
- conn.StatisticsEnabled = true;
- Assert.AreEqual(true, conn.StatisticsEnabled, "#1 The value should be true after setting the property to true");
- }
- [Test]
- [Category("NotWorking")]
- public void ChangePasswordTest ()
- {
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Not a secret.")]
- string tmpPassword = "modifiedbymonosqlclient";
- SqlConnection.ChangePassword (connectionString, tmpPassword);
- SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder (connectionString);
- string oldPassword = connBuilder.Password;
- connBuilder.Password = tmpPassword;
- SqlConnection.ChangePassword (connBuilder.ConnectionString, oldPassword); // Modify to the original password
- }
- static int GetConnectionCount (SqlConnection conn)
- {
- Thread.Sleep (200);
- SqlCommand cmd = conn.CreateCommand ();
- cmd.CommandText = "select count(*) from master..sysprocesses where db_name(dbid) = @dbname";
- cmd.Parameters.Add (new SqlParameter ("@dbname", conn.Database));
- int connection_count = (int) cmd.ExecuteScalar ();
- cmd.Dispose ();
- return connection_count;
- }
- int ClientVersion {
- get {
- return (engine.ClientVersion);
- }
- }
- }
- [TestFixture]
- [Category ("sqlserver")]
- public class GetSchemaTest
- {
- SqlConnection conn = null;
- String connectionString = ConnectionManager.Instance.Sql.ConnectionString;
- [SetUp]
- public void SetUp()
- {
- conn = new SqlConnection(connectionString);
- conn.Open();
- }
- [TearDown]
- public void TearDown()
- {
- conn?.Close();
- }
- [Test]
- public void GetSchemaTest1()
- {
- if (ConnectionManager.Instance.Sql.IsAzure)
- Assert.Ignore("SQL Azure - Not supported'");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("databases");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- if (col.ColumnName.ToString() == "database_name" && row[col].ToString() == ConnectionManager.Instance.DatabaseName)
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS1 failed");
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void GetSchemaTest2()
- {
- conn.GetSchema(null);
- }
- [Test]
- public void GetSchemaTest3 ()
- {
- Assert.Ignore ("We currently have no foreign keys defined in the test database");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("ForeignKeys");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "tmptable1")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS3 failed");
- }
- [Test]
- public void GetSchemaTest4()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Indexes");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS4 failed");
- }
- [Test]
- public void GetSchemaTest5()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("IndexColumns");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS5 failed");
- }
- [Test]
- public void GetSchemaTest6()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Procedures");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS6 failed");
- }
- [Test]
- public void GetSchemaTest7()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("ProcedureParameters");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS7 failed");
- }
- [Test]
- public void GetSchemaTest8()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Tables");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS8 failed");
- }
- [Test]
- public void GetSchemaTest9()
- {
- if (ConnectionManager.Instance.Sql.IsAzure)
- Assert.Ignore("SQL Azure - Not supported'");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Columns");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS9 failed");
- }
- [Test]
- public void GetSchemaTest10()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Users");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS10 failed");
- }
- [Test]
- public void GetSchemaTest11 ()
- {
- Assert.Ignore ("Incorrect syntax near 'TABLE_SCHEMA'");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("Views");
- flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values.
- */
- if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS11 failed");
- }
- [Test]
- public void GetSchemaTest12 ()
- {
- Assert.Ignore ("Incorrect syntax near '('");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("ViewColumns");
- flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values.
- */
- if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS12 failed");
- }
- [Test]
- public void GetSchemaTest13 ()
- {
- Assert.Ignore ("The multi-part identifier \"assportemblies.name\" could not be bound");
- bool flag = false;
- DataTable tab1 = conn.GetSchema("UserDefinedTypes");
- flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values.
- */
- if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS13 failed");
- }
- [Test]
- [Ignore("TODO: fix restrictions")]
- public void GetSchemaTest14()
- {
- bool flag = false;
- string [] restrictions = new string[4];
- restrictions[0] = ConnectionManager.Instance.DatabaseName;
- restrictions[1] = "dbo";
- restrictions[2] = null;
- restrictions[3] = "BASE TABLE";
- DataTable tab1 = conn.GetSchema("Tables", restrictions);
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS14 failed");
- }
- [Test]
- [Ignore("TODO: fix restrictions")]
- public void GetSchemaTest15()
- {
- bool flag = false;
- string [] restrictions = new string[4];
- restrictions[0] = ConnectionManager.Instance.DatabaseName;
- restrictions[1] = null;
- restrictions[2] = "binary_family";
- restrictions[3] = null;
- DataTable tab1 = conn.GetSchema("IndexColumns", restrictions);
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS15 failed");
- }
- [Test]
- [Ignore("TODO: fix restrictions")]
- public void GetSchemaTest16()
- {
- bool flag = false;
- string [] restrictions = new string[4];
- restrictions[0] = ConnectionManager.Instance.DatabaseName;
- restrictions[1] = null;
- restrictions[2] = "sp_get_age";
- restrictions[3] = null;
- DataTable tab1 = conn.GetSchema("Procedures", restrictions);
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "sp_get_age")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS16 failed");
- }
- [Test]
- public void GetSchemaTest17()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema();
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS17 failed");
- }
- [Test]
- public void GetSchemaTest18()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("RESTRICTIONS");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "RestrictionDefault" && row[col].ToString() == "CONSTRAINT_NAME")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS18 failed");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void GetSchemaTest19 ()
- {
- String [] restrictions = new String[1];
- conn.GetSchema("RESTRICTIONS", restrictions);
- }
- [Test]
- public void GetSchemaTest20 ()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("DataTypes");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "TypeName" && row[col].ToString() == "uniqueidentifier")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS20 failed");
- }
- [Test]
- public void GetSchemaTest21()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema();
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS21 failed");
- }
- [Test]
- public void GetSchemaTest22()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("ReservedWords");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "UPPER")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(true, flag, "#GS22 failed");
- }
- [Test]
- public void GetSchemaTest23()
- {
- bool flag = false;
- DataTable tab1 = conn.GetSchema("ReservedWords");
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "upper")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(false, flag, "#GS23 failed");
- }
- [Test]
- public void GetSchemaTest24()
- {
- bool flag = false;
- string [] restrictions = new string[4];
- restrictions[0] = ConnectionManager.Instance.DatabaseName;
- restrictions[1] = null;
- restrictions[2] = "sp_get_age";
- restrictions[3] = null;
- DataTable tab1 = conn.GetSchema("Procedures", restrictions);
- foreach (DataRow row in tab1.Rows)
- {
- foreach (DataColumn col in tab1.Columns)
- {
- /*
- * We need to consider multiple values
- */
- if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "mono")
- {
- flag = true;
- break;
- }
- }
- if (flag)
- break;
- }
- Assert.AreEqual(false, flag, "#GS24 failed");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void GetSchemaTest25 ()
- {
- String [] restrictions = new String [1];
- conn.GetSchema ("Mono", restrictions);
- }
- }
- }
|