SqlConnectionTest.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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) 14, ex.Class, "#3");
  152. Assert.IsNull (ex.InnerException, "#4");
  153. Assert.IsNotNull (ex.Message, "#5");
  154. Assert.AreEqual (18456, 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. str += "password=pwd;Workstation ID=workstation;Packet Size=512;";
  513. str += "Connect Timeout=10";
  514. conn.ConnectionString = str;
  515. Assert.AreEqual ("localhost1", conn.DataSource, "#B1");
  516. Assert.AreEqual ("db", conn.Database, "#B2");
  517. Assert.AreEqual (ConnectionState.Closed, conn.State, "#B3");
  518. Assert.AreEqual ("workstation", conn.WorkstationId, "#B4");
  519. Assert.AreEqual (512, conn.PacketSize, "#B5");
  520. Assert.AreEqual (10, conn.ConnectionTimeout, "#B6");
  521. // Test if any leftover values exist from previous invocation
  522. conn.ConnectionString = connectionString;
  523. conn.ConnectionString = string.Empty;
  524. Assert.AreEqual (string.Empty, conn.DataSource, "#C1");
  525. Assert.AreEqual ("", conn.Database, "#C2");
  526. Assert.AreEqual (8000, conn.PacketSize, "#C3");
  527. Assert.AreEqual (15, conn.ConnectionTimeout, "#C4");
  528. Assert.IsTrue (string.Compare (conn.WorkstationId, Environment.MachineName, true) == 0, "#C5");
  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] // bug #412571
  608. public void Dispose ()
  609. {
  610. StateChangeEventArgs stateChangeArgs;
  611. EventArgs disposedArgs;
  612. conn = new SqlConnection (connectionString + ";Connection Timeout=30;Packet Size=512;Workstation Id=Desktop");
  613. conn.Disposed += new EventHandler (Connection_Disposed);
  614. conn.Open ();
  615. conn.StateChange += new StateChangeEventHandler (Connection_StateChange);
  616. Assert.AreEqual (0, events.Count, "#A1");
  617. conn.Dispose ();
  618. Assert.AreEqual (string.Empty, conn.ConnectionString, "#A2");
  619. Assert.AreEqual (15, conn.ConnectionTimeout, "#A3");
  620. Assert.AreEqual (string.Empty, conn.Database, "#A4");
  621. Assert.AreEqual (string.Empty, conn.DataSource, "#A5");
  622. Assert.AreEqual (8000, conn.PacketSize, "#A6");
  623. Assert.AreEqual (ConnectionState.Closed, conn.State, "#A7");
  624. Assert.IsTrue (string.Compare (conn.WorkstationId, Environment.MachineName, true) == 0, "#A8");
  625. Assert.AreEqual (2, events.Count, "#A9");
  626. stateChangeArgs = events [0] as StateChangeEventArgs;
  627. Assert.IsNotNull (stateChangeArgs, "#B1");
  628. Assert.AreEqual (typeof (StateChangeEventArgs), stateChangeArgs.GetType (), "#B2");
  629. Assert.AreEqual (ConnectionState.Open, stateChangeArgs.OriginalState, "#B3");
  630. Assert.AreEqual (ConnectionState.Closed, stateChangeArgs.CurrentState, "B4");
  631. disposedArgs = events [1] as EventArgs;
  632. Assert.IsNotNull (disposedArgs, "#C1");
  633. Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#C2");
  634. conn.Dispose ();
  635. Assert.AreEqual (ConnectionState.Closed, conn.State, "#D1");
  636. Assert.AreEqual (3, events.Count, "#D2");
  637. disposedArgs = events [2] as EventArgs;
  638. Assert.IsNotNull (disposedArgs, "#E1");
  639. Assert.AreEqual (typeof (EventArgs), disposedArgs.GetType (), "#E2");
  640. }
  641. void Connection_StateChange (object sender , StateChangeEventArgs e)
  642. {
  643. events.Add (e);
  644. }
  645. void Connection_Disposed (object sender , EventArgs e)
  646. {
  647. events.Add (e);
  648. }
  649. [Test]
  650. public void FireInfoMessageEventOnUserErrorsTest ()
  651. {
  652. conn = new SqlConnection ();
  653. Assert.AreEqual(false, conn.FireInfoMessageEventOnUserErrors, "#1 The default value should be false");
  654. conn.FireInfoMessageEventOnUserErrors = true;
  655. Assert.AreEqual(true, conn.FireInfoMessageEventOnUserErrors, "#1 The value should be true after setting the property to true");
  656. }
  657. [Test]
  658. public void StatisticsEnabledTest ()
  659. {
  660. conn = new SqlConnection ();
  661. Assert.AreEqual(false, conn.StatisticsEnabled, "#1 The default value should be false");
  662. conn.StatisticsEnabled = true;
  663. Assert.AreEqual(true, conn.StatisticsEnabled, "#1 The value should be true after setting the property to true");
  664. }
  665. [Test]
  666. [Category("NotWorking")]
  667. public void ChangePasswordTest ()
  668. {
  669. string tmpPassword = "modifiedbymonosqlclient";
  670. SqlConnection.ChangePassword (connectionString, tmpPassword);
  671. SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder (connectionString);
  672. string oldPassword = connBuilder.Password;
  673. connBuilder.Password = tmpPassword;
  674. SqlConnection.ChangePassword (connBuilder.ConnectionString, oldPassword); // Modify to the original password
  675. }
  676. static int GetConnectionCount (SqlConnection conn)
  677. {
  678. Thread.Sleep (200);
  679. SqlCommand cmd = conn.CreateCommand ();
  680. cmd.CommandText = "select count(*) from master..sysprocesses where db_name(dbid) = @dbname";
  681. cmd.Parameters.Add (new SqlParameter ("@dbname", conn.Database));
  682. int connection_count = (int) cmd.ExecuteScalar ();
  683. cmd.Dispose ();
  684. return connection_count;
  685. }
  686. int ClientVersion {
  687. get {
  688. return (engine.ClientVersion);
  689. }
  690. }
  691. }
  692. [TestFixture]
  693. [Category ("sqlserver")]
  694. public class GetSchemaTest
  695. {
  696. SqlConnection conn = null;
  697. String connectionString = ConnectionManager.Instance.Sql.ConnectionString;
  698. [SetUp]
  699. public void SetUp()
  700. {
  701. conn = new SqlConnection(connectionString);
  702. conn.Open();
  703. }
  704. [TearDown]
  705. public void TearDown()
  706. {
  707. conn?.Close();
  708. }
  709. [Test]
  710. public void GetSchemaTest1()
  711. {
  712. if (ConnectionManager.Instance.Sql.IsAzure)
  713. Assert.Ignore("SQL Azure - Not supported'");
  714. bool flag = false;
  715. DataTable tab1 = conn.GetSchema("databases");
  716. foreach (DataRow row in tab1.Rows)
  717. {
  718. foreach (DataColumn col in tab1.Columns)
  719. {
  720. if (col.ColumnName.ToString() == "database_name" && row[col].ToString() == ConnectionManager.Instance.DatabaseName)
  721. {
  722. flag = true;
  723. break;
  724. }
  725. }
  726. if (flag)
  727. break;
  728. }
  729. Assert.AreEqual(true, flag, "#GS1 failed");
  730. }
  731. [Test]
  732. [ExpectedException(typeof(ArgumentException))]
  733. public void GetSchemaTest2()
  734. {
  735. conn.GetSchema(null);
  736. }
  737. [Test]
  738. public void GetSchemaTest3 ()
  739. {
  740. Assert.Ignore ("We currently have no foreign keys defined in the test database");
  741. bool flag = false;
  742. DataTable tab1 = conn.GetSchema("ForeignKeys");
  743. foreach (DataRow row in tab1.Rows)
  744. {
  745. foreach (DataColumn col in tab1.Columns)
  746. {
  747. /*
  748. * We need to consider multiple values
  749. */
  750. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "tmptable1")
  751. {
  752. flag = true;
  753. break;
  754. }
  755. }
  756. if (flag)
  757. break;
  758. }
  759. Assert.AreEqual(true, flag, "#GS3 failed");
  760. }
  761. [Test]
  762. public void GetSchemaTest4()
  763. {
  764. bool flag = false;
  765. DataTable tab1 = conn.GetSchema("Indexes");
  766. foreach (DataRow row in tab1.Rows)
  767. {
  768. foreach (DataColumn col in tab1.Columns)
  769. {
  770. /*
  771. * We need to consider multiple values
  772. */
  773. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  774. {
  775. flag = true;
  776. break;
  777. }
  778. }
  779. if (flag)
  780. break;
  781. }
  782. Assert.AreEqual(true, flag, "#GS4 failed");
  783. }
  784. [Test]
  785. public void GetSchemaTest5()
  786. {
  787. bool flag = false;
  788. DataTable tab1 = conn.GetSchema("IndexColumns");
  789. foreach (DataRow row in tab1.Rows)
  790. {
  791. foreach (DataColumn col in tab1.Columns)
  792. {
  793. /*
  794. * We need to consider multiple values
  795. */
  796. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  797. {
  798. flag = true;
  799. break;
  800. }
  801. }
  802. if (flag)
  803. break;
  804. }
  805. Assert.AreEqual(true, flag, "#GS5 failed");
  806. }
  807. [Test]
  808. public void GetSchemaTest6()
  809. {
  810. bool flag = false;
  811. DataTable tab1 = conn.GetSchema("Procedures");
  812. foreach (DataRow row in tab1.Rows)
  813. {
  814. foreach (DataColumn col in tab1.Columns)
  815. {
  816. /*
  817. * We need to consider multiple values
  818. */
  819. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  820. {
  821. flag = true;
  822. break;
  823. }
  824. }
  825. if (flag)
  826. break;
  827. }
  828. Assert.AreEqual(true, flag, "#GS6 failed");
  829. }
  830. [Test]
  831. public void GetSchemaTest7()
  832. {
  833. bool flag = false;
  834. DataTable tab1 = conn.GetSchema("ProcedureParameters");
  835. foreach (DataRow row in tab1.Rows)
  836. {
  837. foreach (DataColumn col in tab1.Columns)
  838. {
  839. /*
  840. * We need to consider multiple values
  841. */
  842. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  843. {
  844. flag = true;
  845. break;
  846. }
  847. }
  848. if (flag)
  849. break;
  850. }
  851. Assert.AreEqual(true, flag, "#GS7 failed");
  852. }
  853. [Test]
  854. public void GetSchemaTest8()
  855. {
  856. bool flag = false;
  857. DataTable tab1 = conn.GetSchema("Tables");
  858. foreach (DataRow row in tab1.Rows)
  859. {
  860. foreach (DataColumn col in tab1.Columns)
  861. {
  862. /*
  863. * We need to consider multiple values
  864. */
  865. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  866. {
  867. flag = true;
  868. break;
  869. }
  870. }
  871. if (flag)
  872. break;
  873. }
  874. Assert.AreEqual(true, flag, "#GS8 failed");
  875. }
  876. [Test]
  877. public void GetSchemaTest9()
  878. {
  879. if (ConnectionManager.Instance.Sql.IsAzure)
  880. Assert.Ignore("SQL Azure - Not supported'");
  881. bool flag = false;
  882. DataTable tab1 = conn.GetSchema("Columns");
  883. foreach (DataRow row in tab1.Rows)
  884. {
  885. foreach (DataColumn col in tab1.Columns)
  886. {
  887. /*
  888. * We need to consider multiple values
  889. */
  890. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  891. {
  892. flag = true;
  893. break;
  894. }
  895. }
  896. if (flag)
  897. break;
  898. }
  899. Assert.AreEqual(true, flag, "#GS9 failed");
  900. }
  901. [Test]
  902. public void GetSchemaTest10()
  903. {
  904. bool flag = false;
  905. DataTable tab1 = conn.GetSchema("Users");
  906. foreach (DataRow row in tab1.Rows)
  907. {
  908. foreach (DataColumn col in tab1.Columns)
  909. {
  910. /*
  911. * We need to consider multiple values
  912. */
  913. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  914. {
  915. flag = true;
  916. break;
  917. }
  918. }
  919. if (flag)
  920. break;
  921. }
  922. Assert.AreEqual(true, flag, "#GS10 failed");
  923. }
  924. [Test]
  925. public void GetSchemaTest11 ()
  926. {
  927. Assert.Ignore ("Incorrect syntax near 'TABLE_SCHEMA'");
  928. bool flag = false;
  929. DataTable tab1 = conn.GetSchema("Views");
  930. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  931. foreach (DataRow row in tab1.Rows)
  932. {
  933. foreach (DataColumn col in tab1.Columns)
  934. {
  935. /*
  936. * We need to consider multiple values.
  937. */
  938. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  939. {
  940. flag = true;
  941. break;
  942. }
  943. }
  944. if (flag)
  945. break;
  946. }
  947. Assert.AreEqual(true, flag, "#GS11 failed");
  948. }
  949. [Test]
  950. public void GetSchemaTest12 ()
  951. {
  952. Assert.Ignore ("Incorrect syntax near '('");
  953. bool flag = false;
  954. DataTable tab1 = conn.GetSchema("ViewColumns");
  955. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  956. foreach (DataRow row in tab1.Rows)
  957. {
  958. foreach (DataColumn col in tab1.Columns)
  959. {
  960. /*
  961. * We need to consider multiple values.
  962. */
  963. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  964. {
  965. flag = true;
  966. break;
  967. }
  968. }
  969. if (flag)
  970. break;
  971. }
  972. Assert.AreEqual(true, flag, "#GS12 failed");
  973. }
  974. [Test]
  975. public void GetSchemaTest13 ()
  976. {
  977. Assert.Ignore ("The multi-part identifier \"assportemblies.name\" could not be bound");
  978. bool flag = false;
  979. DataTable tab1 = conn.GetSchema("UserDefinedTypes");
  980. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  981. foreach (DataRow row in tab1.Rows)
  982. {
  983. foreach (DataColumn col in tab1.Columns)
  984. {
  985. /*
  986. * We need to consider multiple values.
  987. */
  988. if (col.ColumnName.ToString() == "user_name" && row[col].ToString() == "public")
  989. {
  990. flag = true;
  991. break;
  992. }
  993. }
  994. if (flag)
  995. break;
  996. }
  997. Assert.AreEqual(true, flag, "#GS13 failed");
  998. }
  999. [Test]
  1000. [Ignore("TODO: fix restrictions")]
  1001. public void GetSchemaTest14()
  1002. {
  1003. bool flag = false;
  1004. string [] restrictions = new string[4];
  1005. restrictions[0] = ConnectionManager.Instance.DatabaseName;
  1006. restrictions[1] = "dbo";
  1007. restrictions[2] = null;
  1008. restrictions[3] = "BASE TABLE";
  1009. DataTable tab1 = conn.GetSchema("Tables", restrictions);
  1010. foreach (DataRow row in tab1.Rows)
  1011. {
  1012. foreach (DataColumn col in tab1.Columns)
  1013. {
  1014. /*
  1015. * We need to consider multiple values
  1016. */
  1017. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  1018. {
  1019. flag = true;
  1020. break;
  1021. }
  1022. }
  1023. if (flag)
  1024. break;
  1025. }
  1026. Assert.AreEqual(true, flag, "#GS14 failed");
  1027. }
  1028. [Test]
  1029. [Ignore("TODO: fix restrictions")]
  1030. public void GetSchemaTest15()
  1031. {
  1032. bool flag = false;
  1033. string [] restrictions = new string[4];
  1034. restrictions[0] = ConnectionManager.Instance.DatabaseName;
  1035. restrictions[1] = null;
  1036. restrictions[2] = "binary_family";
  1037. restrictions[3] = null;
  1038. DataTable tab1 = conn.GetSchema("IndexColumns", restrictions);
  1039. foreach (DataRow row in tab1.Rows)
  1040. {
  1041. foreach (DataColumn col in tab1.Columns)
  1042. {
  1043. /*
  1044. * We need to consider multiple values
  1045. */
  1046. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  1047. {
  1048. flag = true;
  1049. break;
  1050. }
  1051. }
  1052. if (flag)
  1053. break;
  1054. }
  1055. Assert.AreEqual(true, flag, "#GS15 failed");
  1056. }
  1057. [Test]
  1058. [Ignore("TODO: fix restrictions")]
  1059. public void GetSchemaTest16()
  1060. {
  1061. bool flag = false;
  1062. string [] restrictions = new string[4];
  1063. restrictions[0] = ConnectionManager.Instance.DatabaseName;
  1064. restrictions[1] = null;
  1065. restrictions[2] = "sp_get_age";
  1066. restrictions[3] = null;
  1067. DataTable tab1 = conn.GetSchema("Procedures", restrictions);
  1068. foreach (DataRow row in tab1.Rows)
  1069. {
  1070. foreach (DataColumn col in tab1.Columns)
  1071. {
  1072. /*
  1073. * We need to consider multiple values
  1074. */
  1075. if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "sp_get_age")
  1076. {
  1077. flag = true;
  1078. break;
  1079. }
  1080. }
  1081. if (flag)
  1082. break;
  1083. }
  1084. Assert.AreEqual(true, flag, "#GS16 failed");
  1085. }
  1086. [Test]
  1087. public void GetSchemaTest17()
  1088. {
  1089. bool flag = false;
  1090. DataTable tab1 = conn.GetSchema();
  1091. foreach (DataRow row in tab1.Rows)
  1092. {
  1093. foreach (DataColumn col in tab1.Columns)
  1094. {
  1095. /*
  1096. * We need to consider multiple values
  1097. */
  1098. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  1099. {
  1100. flag = true;
  1101. break;
  1102. }
  1103. }
  1104. if (flag)
  1105. break;
  1106. }
  1107. Assert.AreEqual(true, flag, "#GS17 failed");
  1108. }
  1109. [Test]
  1110. public void GetSchemaTest18()
  1111. {
  1112. bool flag = false;
  1113. DataTable tab1 = conn.GetSchema("RESTRICTIONS");
  1114. foreach (DataRow row in tab1.Rows)
  1115. {
  1116. foreach (DataColumn col in tab1.Columns)
  1117. {
  1118. /*
  1119. * We need to consider multiple values
  1120. */
  1121. if (col.ColumnName.ToString() == "RestrictionDefault" && row[col].ToString() == "CONSTRAINT_NAME")
  1122. {
  1123. flag = true;
  1124. break;
  1125. }
  1126. }
  1127. if (flag)
  1128. break;
  1129. }
  1130. Assert.AreEqual(true, flag, "#GS18 failed");
  1131. }
  1132. [Test]
  1133. [ExpectedException (typeof (ArgumentException))]
  1134. public void GetSchemaTest19 ()
  1135. {
  1136. String [] restrictions = new String[1];
  1137. conn.GetSchema("RESTRICTIONS", restrictions);
  1138. }
  1139. [Test]
  1140. public void GetSchemaTest20 ()
  1141. {
  1142. bool flag = false;
  1143. DataTable tab1 = conn.GetSchema("DataTypes");
  1144. foreach (DataRow row in tab1.Rows)
  1145. {
  1146. foreach (DataColumn col in tab1.Columns)
  1147. {
  1148. /*
  1149. * We need to consider multiple values
  1150. */
  1151. if (col.ColumnName.ToString() == "TypeName" && row[col].ToString() == "uniqueidentifier")
  1152. {
  1153. flag = true;
  1154. break;
  1155. }
  1156. }
  1157. if (flag)
  1158. break;
  1159. }
  1160. Assert.AreEqual(true, flag, "#GS20 failed");
  1161. }
  1162. [Test]
  1163. public void GetSchemaTest21()
  1164. {
  1165. bool flag = false;
  1166. DataTable tab1 = conn.GetSchema();
  1167. foreach (DataRow row in tab1.Rows)
  1168. {
  1169. foreach (DataColumn col in tab1.Columns)
  1170. {
  1171. /*
  1172. * We need to consider multiple values
  1173. */
  1174. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  1175. {
  1176. flag = true;
  1177. break;
  1178. }
  1179. }
  1180. if (flag)
  1181. break;
  1182. }
  1183. Assert.AreEqual(true, flag, "#GS21 failed");
  1184. }
  1185. [Test]
  1186. public void GetSchemaTest22()
  1187. {
  1188. bool flag = false;
  1189. DataTable tab1 = conn.GetSchema("ReservedWords");
  1190. foreach (DataRow row in tab1.Rows)
  1191. {
  1192. foreach (DataColumn col in tab1.Columns)
  1193. {
  1194. /*
  1195. * We need to consider multiple values
  1196. */
  1197. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "UPPER")
  1198. {
  1199. flag = true;
  1200. break;
  1201. }
  1202. }
  1203. if (flag)
  1204. break;
  1205. }
  1206. Assert.AreEqual(true, flag, "#GS22 failed");
  1207. }
  1208. [Test]
  1209. public void GetSchemaTest23()
  1210. {
  1211. bool flag = false;
  1212. DataTable tab1 = conn.GetSchema("ReservedWords");
  1213. foreach (DataRow row in tab1.Rows)
  1214. {
  1215. foreach (DataColumn col in tab1.Columns)
  1216. {
  1217. /*
  1218. * We need to consider multiple values
  1219. */
  1220. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "upper")
  1221. {
  1222. flag = true;
  1223. break;
  1224. }
  1225. }
  1226. if (flag)
  1227. break;
  1228. }
  1229. Assert.AreEqual(false, flag, "#GS23 failed");
  1230. }
  1231. [Test]
  1232. public void GetSchemaTest24()
  1233. {
  1234. bool flag = false;
  1235. string [] restrictions = new string[4];
  1236. restrictions[0] = ConnectionManager.Instance.DatabaseName;
  1237. restrictions[1] = null;
  1238. restrictions[2] = "sp_get_age";
  1239. restrictions[3] = null;
  1240. DataTable tab1 = conn.GetSchema("Procedures", restrictions);
  1241. foreach (DataRow row in tab1.Rows)
  1242. {
  1243. foreach (DataColumn col in tab1.Columns)
  1244. {
  1245. /*
  1246. * We need to consider multiple values
  1247. */
  1248. if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "mono")
  1249. {
  1250. flag = true;
  1251. break;
  1252. }
  1253. }
  1254. if (flag)
  1255. break;
  1256. }
  1257. Assert.AreEqual(false, flag, "#GS24 failed");
  1258. }
  1259. [Test]
  1260. [ExpectedException (typeof (ArgumentException))]
  1261. public void GetSchemaTest25 ()
  1262. {
  1263. String [] restrictions = new String [1];
  1264. conn.GetSchema ("Mono", restrictions);
  1265. }
  1266. }
  1267. }