SqlConnectionTest.cs 33 KB

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