SqlConnectionTest.cs 39 KB

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