SqlConnectionTest.cs 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 (Exception e) {
  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 (
  445. "#1 InvalidOperation Exception Must be thrown if conn is closed");
  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. }
  522. #if NET_2_0
  523. [TestFixture]
  524. [Category ("sqlserver")]
  525. public class GetSchemaTest
  526. {
  527. SqlConnection conn = null;
  528. String connectionString = ConnectionManager.Singleton.ConnectionString;
  529. [SetUp]
  530. public void Setup()
  531. {
  532. conn = new SqlConnection(connectionString);
  533. conn.Open();
  534. }
  535. [TearDown]
  536. public void TearDown()
  537. {
  538. conn.Close();
  539. }
  540. [Test]
  541. public void GetSchemaTest1()
  542. {
  543. bool flag = false;
  544. DataTable tab1 = conn.GetSchema("databases");
  545. foreach (DataRow row in tab1.Rows)
  546. {
  547. foreach (DataColumn col in tab1.Columns)
  548. {
  549. if (col.ColumnName.ToString() == "database_name" && row[col].ToString() == "monotest")
  550. {
  551. flag = true;
  552. break;
  553. }
  554. }
  555. if (flag)
  556. break;
  557. }
  558. Assert.AreEqual(true, flag, "#GS1 failed");
  559. }
  560. [Test]
  561. [ExpectedException(typeof(ArgumentException))]
  562. public void GetSchemaTest2()
  563. {
  564. conn.GetSchema(null);
  565. }
  566. [Test]
  567. public void GetSchemaTest3()
  568. {
  569. bool flag = false;
  570. DataTable tab1 = conn.GetSchema("ForeignKeys");
  571. foreach (DataRow row in tab1.Rows)
  572. {
  573. foreach (DataColumn col in tab1.Columns)
  574. {
  575. /*
  576. * We need to consider multiple values
  577. */
  578. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "tmptable1")
  579. {
  580. flag = true;
  581. break;
  582. }
  583. }
  584. if (flag)
  585. break;
  586. }
  587. Assert.AreEqual(true, flag, "#GS3 failed");
  588. }
  589. [Test]
  590. public void GetSchemaTest4()
  591. {
  592. bool flag = false;
  593. DataTable tab1 = conn.GetSchema("Indexes");
  594. foreach (DataRow row in tab1.Rows)
  595. {
  596. foreach (DataColumn col in tab1.Columns)
  597. {
  598. /*
  599. * We need to consider multiple values
  600. */
  601. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  602. {
  603. flag = true;
  604. break;
  605. }
  606. }
  607. if (flag)
  608. break;
  609. }
  610. Assert.AreEqual(true, flag, "#GS4 failed");
  611. }
  612. [Test]
  613. public void GetSchemaTest5()
  614. {
  615. bool flag = false;
  616. DataTable tab1 = conn.GetSchema("IndexColumns");
  617. foreach (DataRow row in tab1.Rows)
  618. {
  619. foreach (DataColumn col in tab1.Columns)
  620. {
  621. /*
  622. * We need to consider multiple values
  623. */
  624. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  625. {
  626. flag = true;
  627. break;
  628. }
  629. }
  630. if (flag)
  631. break;
  632. }
  633. Assert.AreEqual(true, flag, "#GS5 failed");
  634. }
  635. [Test]
  636. public void GetSchemaTest6()
  637. {
  638. bool flag = false;
  639. DataTable tab1 = conn.GetSchema("Procedures");
  640. foreach (DataRow row in tab1.Rows)
  641. {
  642. foreach (DataColumn col in tab1.Columns)
  643. {
  644. /*
  645. * We need to consider multiple values
  646. */
  647. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  648. {
  649. flag = true;
  650. break;
  651. }
  652. }
  653. if (flag)
  654. break;
  655. }
  656. Assert.AreEqual(true, flag, "#GS6 failed");
  657. }
  658. [Test]
  659. public void GetSchemaTest7()
  660. {
  661. bool flag = false;
  662. DataTable tab1 = conn.GetSchema("ProcedureParameters");
  663. foreach (DataRow row in tab1.Rows)
  664. {
  665. foreach (DataColumn col in tab1.Columns)
  666. {
  667. /*
  668. * We need to consider multiple values
  669. */
  670. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  671. {
  672. flag = true;
  673. break;
  674. }
  675. }
  676. if (flag)
  677. break;
  678. }
  679. Assert.AreEqual(true, flag, "#GS7 failed");
  680. }
  681. [Test]
  682. public void GetSchemaTest8()
  683. {
  684. bool flag = false;
  685. DataTable tab1 = conn.GetSchema("Tables");
  686. foreach (DataRow row in tab1.Rows)
  687. {
  688. foreach (DataColumn col in tab1.Columns)
  689. {
  690. /*
  691. * We need to consider multiple values
  692. */
  693. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  694. {
  695. flag = true;
  696. break;
  697. }
  698. }
  699. if (flag)
  700. break;
  701. }
  702. Assert.AreEqual(true, flag, "#GS8 failed");
  703. }
  704. [Test]
  705. public void GetSchemaTest9()
  706. {
  707. bool flag = false;
  708. DataTable tab1 = conn.GetSchema("Columns");
  709. foreach (DataRow row in tab1.Rows)
  710. {
  711. foreach (DataColumn col in tab1.Columns)
  712. {
  713. /*
  714. * We need to consider multiple values
  715. */
  716. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  717. {
  718. flag = true;
  719. break;
  720. }
  721. }
  722. if (flag)
  723. break;
  724. }
  725. Assert.AreEqual(true, flag, "#GS9 failed");
  726. }
  727. public void GetSchemaTest10()
  728. {
  729. bool flag = false;
  730. DataTable tab1 = conn.GetSchema("Users");
  731. foreach (DataRow row in tab1.Rows)
  732. {
  733. foreach (DataColumn col in tab1.Columns)
  734. {
  735. /*
  736. * We need to consider multiple values
  737. */
  738. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  739. {
  740. flag = true;
  741. break;
  742. }
  743. }
  744. if (flag)
  745. break;
  746. }
  747. Assert.AreEqual(true, flag, "#GS10 failed");
  748. }
  749. public void GetSchemaTest11()
  750. {
  751. bool flag = false;
  752. DataTable tab1 = conn.GetSchema("Views");
  753. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  754. foreach (DataRow row in tab1.Rows)
  755. {
  756. foreach (DataColumn col in tab1.Columns)
  757. {
  758. /*
  759. * We need to consider multiple values.
  760. */
  761. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  762. {
  763. flag = true;
  764. break;
  765. }
  766. }
  767. if (flag)
  768. break;
  769. }
  770. Assert.AreEqual(true, flag, "#GS11 failed");
  771. }
  772. public void GetSchemaTest12()
  773. {
  774. bool flag = false;
  775. DataTable tab1 = conn.GetSchema("ViewColumns");
  776. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  777. foreach (DataRow row in tab1.Rows)
  778. {
  779. foreach (DataColumn col in tab1.Columns)
  780. {
  781. /*
  782. * We need to consider multiple values.
  783. */
  784. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  785. {
  786. flag = true;
  787. break;
  788. }
  789. }
  790. if (flag)
  791. break;
  792. }
  793. Assert.AreEqual(true, flag, "#GS12 failed");
  794. }
  795. public void GetSchemaTest13()
  796. {
  797. bool flag = false;
  798. DataTable tab1 = conn.GetSchema("UserDefineTypes");
  799. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  800. foreach (DataRow row in tab1.Rows)
  801. {
  802. foreach (DataColumn col in tab1.Columns)
  803. {
  804. /*
  805. * We need to consider multiple values.
  806. */
  807. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  808. {
  809. flag = true;
  810. break;
  811. }
  812. }
  813. if (flag)
  814. break;
  815. }
  816. Assert.AreEqual(true, flag, "#GS13 failed");
  817. }
  818. [Test]
  819. public void GetSchemaTest14()
  820. {
  821. bool flag = false;
  822. string [] restrictions = new string[4];
  823. restrictions[0] = "monotest";
  824. restrictions[1] = "dbo";
  825. restrictions[2] = null;
  826. restrictions[3] = "BASE TABLE";
  827. DataTable tab1 = conn.GetSchema("Tables", restrictions);
  828. foreach (DataRow row in tab1.Rows)
  829. {
  830. foreach (DataColumn col in tab1.Columns)
  831. {
  832. /*
  833. * We need to consider multiple values
  834. */
  835. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  836. {
  837. flag = true;
  838. break;
  839. }
  840. }
  841. if (flag)
  842. break;
  843. }
  844. Assert.AreEqual(true, flag, "#GS14 failed");
  845. }
  846. [Test]
  847. public void GetSchemaTest15()
  848. {
  849. bool flag = false;
  850. string [] restrictions = new string[4];
  851. restrictions[0] = "monotest";
  852. restrictions[1] = null;
  853. restrictions[2] = "binary_family";
  854. restrictions[3] = null;
  855. DataTable tab1 = conn.GetSchema("IndexColumns", restrictions);
  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() == "table_name" && row[col].ToString() == "binary_family")
  864. {
  865. flag = true;
  866. break;
  867. }
  868. }
  869. if (flag)
  870. break;
  871. }
  872. Assert.AreEqual(true, flag, "#GS15 failed");
  873. }
  874. [Test]
  875. public void GetSchemaTest16()
  876. {
  877. bool flag = false;
  878. string [] restrictions = new string[4];
  879. restrictions[0] = "monotest";
  880. restrictions[1] = null;
  881. restrictions[2] = "sp_get_age";
  882. restrictions[3] = null;
  883. DataTable tab1 = conn.GetSchema("Procedures", 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() == "ROUTINE_NAME" && row[col].ToString() == "sp_get_age")
  892. {
  893. flag = true;
  894. break;
  895. }
  896. }
  897. if (flag)
  898. break;
  899. }
  900. Assert.AreEqual(true, flag, "#GS16 failed");
  901. }
  902. [Test]
  903. public void GetSchemaTest17()
  904. {
  905. bool flag = false;
  906. DataTable tab1 = conn.GetSchema();
  907. foreach (DataRow row in tab1.Rows)
  908. {
  909. foreach (DataColumn col in tab1.Columns)
  910. {
  911. /*
  912. * We need to consider multiple values
  913. */
  914. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  915. {
  916. flag = true;
  917. break;
  918. }
  919. }
  920. if (flag)
  921. break;
  922. }
  923. Assert.AreEqual(true, flag, "#GS17 failed");
  924. }
  925. [Test]
  926. public void GetSchemaTest18()
  927. {
  928. bool flag = false;
  929. DataTable tab1 = conn.GetSchema("RESTRICTIONS");
  930. foreach (DataRow row in tab1.Rows)
  931. {
  932. foreach (DataColumn col in tab1.Columns)
  933. {
  934. /*
  935. * We need to consider multiple values
  936. */
  937. if (col.ColumnName.ToString() == "RestrictionDefault" && row[col].ToString() == "CONSTRAINT_NAME")
  938. {
  939. flag = true;
  940. break;
  941. }
  942. }
  943. if (flag)
  944. break;
  945. }
  946. Assert.AreEqual(true, flag, "#GS18 failed");
  947. }
  948. [Test]
  949. [ExpectedException(typeof(ArgumentException))]
  950. public void GetSchemaTest19()
  951. {
  952. String [] restrictions = new String[1];
  953. DataTable tab1 = conn.GetSchema("RESTRICTIONS", restrictions);
  954. }
  955. [Test]
  956. public void GetSchemaTest20()
  957. {
  958. bool flag = false;
  959. DataTable tab1 = conn.GetSchema("DataTypes");
  960. foreach (DataRow row in tab1.Rows)
  961. {
  962. foreach (DataColumn col in tab1.Columns)
  963. {
  964. /*
  965. * We need to consider multiple values
  966. */
  967. if (col.ColumnName.ToString() == "TypeName" && row[col].ToString() == "uniqueidentifier")
  968. {
  969. flag = true;
  970. break;
  971. }
  972. }
  973. if (flag)
  974. break;
  975. }
  976. Assert.AreEqual(true, flag, "#GS20 failed");
  977. }
  978. [Test]
  979. public void GetSchemaTest21()
  980. {
  981. bool flag = false;
  982. DataTable tab1 = conn.GetSchema();
  983. foreach (DataRow row in tab1.Rows)
  984. {
  985. foreach (DataColumn col in tab1.Columns)
  986. {
  987. /*
  988. * We need to consider multiple values
  989. */
  990. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  991. {
  992. flag = true;
  993. break;
  994. }
  995. }
  996. if (flag)
  997. break;
  998. }
  999. Assert.AreEqual(true, flag, "#GS21 failed");
  1000. }
  1001. [Test]
  1002. public void GetSchemaTest22()
  1003. {
  1004. bool flag = false;
  1005. DataTable tab1 = conn.GetSchema("ReservedWords");
  1006. foreach (DataRow row in tab1.Rows)
  1007. {
  1008. foreach (DataColumn col in tab1.Columns)
  1009. {
  1010. /*
  1011. * We need to consider multiple values
  1012. */
  1013. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "UPPER")
  1014. {
  1015. flag = true;
  1016. break;
  1017. }
  1018. }
  1019. if (flag)
  1020. break;
  1021. }
  1022. Assert.AreEqual(true, flag, "#GS22 failed");
  1023. }
  1024. [Test]
  1025. public void GetSchemaTest23()
  1026. {
  1027. bool flag = false;
  1028. DataTable tab1 = conn.GetSchema("ReservedWords");
  1029. foreach (DataRow row in tab1.Rows)
  1030. {
  1031. foreach (DataColumn col in tab1.Columns)
  1032. {
  1033. /*
  1034. * We need to consider multiple values
  1035. */
  1036. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "upper")
  1037. {
  1038. flag = true;
  1039. break;
  1040. }
  1041. }
  1042. if (flag)
  1043. break;
  1044. }
  1045. Assert.AreEqual(false, flag, "#GS23 failed");
  1046. }
  1047. [Test]
  1048. public void GetSchemaTest24()
  1049. {
  1050. bool flag = false;
  1051. string [] restrictions = new string[4];
  1052. restrictions[0] = "monotest";
  1053. restrictions[1] = null;
  1054. restrictions[2] = "sp_get_age";
  1055. restrictions[3] = null;
  1056. DataTable tab1 = conn.GetSchema("Procedures", restrictions);
  1057. foreach (DataRow row in tab1.Rows)
  1058. {
  1059. foreach (DataColumn col in tab1.Columns)
  1060. {
  1061. /*
  1062. * We need to consider multiple values
  1063. */
  1064. if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "mono")
  1065. {
  1066. flag = true;
  1067. break;
  1068. }
  1069. }
  1070. if (flag)
  1071. break;
  1072. }
  1073. Assert.AreEqual(false, flag, "#GS24 failed");
  1074. }
  1075. [Test]
  1076. [ExpectedException(typeof(ArgumentException))]
  1077. public void GetSchemaTest25()
  1078. {
  1079. String [] restrictions = new String[1];
  1080. DataTable tab1 = conn.GetSchema("Mono", restrictions);
  1081. }
  1082. }
  1083. #endif
  1084. }