SqlConnectionTest.cs 39 KB

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