SqlConnectionTest.cs 40 KB

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