SqlConnectionTest.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. //
  2. // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
  3. // SqlDataAdapter class
  4. // Author:
  5. // Senganal T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed
  8. // on the ChangeLog entries.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Data;
  31. using System.Data.SqlClient;
  32. using System.Net;
  33. using NUnit.Framework;
  34. using System.Collections;
  35. namespace MonoTests.System.Data
  36. {
  37. [TestFixture]
  38. [Category ("sqlserver")]
  39. public class SqlConnectionTest
  40. {
  41. SqlConnection conn = null ;
  42. String connectionString = ConnectionManager.Singleton.ConnectionString;
  43. ArrayList invalidConnectionStrings = null;
  44. int stateChangeEventCount = 0;
  45. int disposedEventCount = 0;
  46. int infoMessageEventCount = 0;
  47. void populateTestData ()
  48. {
  49. invalidConnectionStrings = new ArrayList ();
  50. // shud be got from a config file ..
  51. //list of invalid and valid conn strings;
  52. invalidConnectionStrings.Add ("InvalidConnectionString");
  53. invalidConnectionStrings.Add ("invalidKeyword=10");
  54. invalidConnectionStrings.Add ("Packet Size=511");
  55. invalidConnectionStrings.Add ("Packet Size=32768");
  56. invalidConnectionStrings.Add ("Connect Timeout=-1");
  57. invalidConnectionStrings.Add ("Max Pool Size=-1");
  58. invalidConnectionStrings.Add ("Min Pool Size=-1");
  59. }
  60. [SetUp]
  61. public void SetUp ()
  62. {
  63. }
  64. [TearDown]
  65. public void TearDown ()
  66. {
  67. if (conn != null)
  68. conn.Dispose ();
  69. }
  70. [Test]
  71. public void DefaultConstructorTest ()
  72. {
  73. SqlConnection conn = new SqlConnection ();
  74. Assert.AreEqual ("", conn.ConnectionString,
  75. "#1 Default Connection String should be empty");
  76. Assert.AreEqual (15, conn.ConnectionTimeout,
  77. "#2 Default ConnectionTimeout should be 15" );
  78. Assert.AreEqual ("", conn.Database,
  79. "#3 Default Database should be empty");
  80. Assert.AreEqual ("", conn.DataSource,
  81. "#4 Default DataSource should be empty");
  82. Assert.AreEqual (8192, conn.PacketSize,"#5 Default Packet Size is 8192");
  83. Assert.AreEqual (Dns.GetHostName().ToUpper (), conn.WorkstationId.ToUpper (),
  84. "#6 Default Workstationid shud be hostname");
  85. Assert.AreEqual (ConnectionState.Closed, conn.State,
  86. "#7 Connection State shud be closed by default");
  87. }
  88. [Test]
  89. public void OverloadedConstructorTest ()
  90. {
  91. // Test Exceptions are thrown for Invalid Connection Strings
  92. int count=0 ;
  93. populateTestData ();
  94. foreach (String invalidConnString in invalidConnectionStrings) {
  95. count++;
  96. try {
  97. conn = new SqlConnection ((string)invalidConnString);
  98. Assert.Fail ("#1 Exception must be thrown");
  99. }catch (AssertionException e) {
  100. throw e;
  101. }catch (Exception e) {
  102. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  103. "Incorrect Exception" + e.StackTrace);
  104. }
  105. }
  106. //check synonyms..
  107. //do i need to check for all the synonyms..
  108. conn = new SqlConnection (
  109. "Timeout=10;Connect Timeout=20;Connection Timeout=30");
  110. Assert.AreEqual (30, conn.ConnectionTimeout,
  111. "## The last set value shud be taken");
  112. conn = new SqlConnection (
  113. "Connect Timeout=100;Connection Timeout=200;Timeout=300");
  114. Assert.AreEqual (300, conn.ConnectionTimeout,
  115. "## The last set value shud be taken");
  116. conn = new SqlConnection (
  117. "Connection Timeout=1000;Timeout=2000;Connect Timeout=3000");
  118. Assert.AreEqual (3000, conn.ConnectionTimeout,
  119. "## The last set value shud be taken");
  120. // Test if properties are set correctly
  121. //'==' doesent work correctly in both msdotnet and mono
  122. /*
  123. conn = new SqlConnection ("server=local==host;database=tmp;");
  124. Assert.AreEqual ("local==host", conn.DataSource,
  125. "# Datasource name is set incorrectly");
  126. */
  127. string connStr = "Server='loca\"lhost';Database='''Db'; packet Size=\"512\";";
  128. connStr += "connect Timeout=20;Workstation Id=\"'\"\"desktop\";";
  129. conn = new SqlConnection (connStr);
  130. Assert.AreEqual (connStr , conn.ConnectionString , "#1");
  131. Assert.AreEqual ("loca\"lhost" , conn.DataSource , "#2");
  132. Assert.AreEqual ("'Db" , conn.Database , "#3");
  133. Assert.AreEqual (512 , conn.PacketSize , "#4");
  134. Assert.AreEqual (20 , conn.ConnectionTimeout , "#5");
  135. Assert.AreEqual ("'\"desktop" , conn.WorkstationId , "#6");
  136. Assert.AreEqual (ConnectionState.Closed , conn.State , "#7");
  137. }
  138. [Test]
  139. public void OpenTest ()
  140. {
  141. conn = new SqlConnection (connectionString);
  142. ArrayList validIncorrectConnStrings = new ArrayList();
  143. string validConnString = connectionString;
  144. validIncorrectConnStrings.Add (
  145. validConnString+"user id=invalidLogin");
  146. validIncorrectConnStrings.Add (
  147. validConnString+"database=invalidDB");
  148. validIncorrectConnStrings.Add (
  149. validConnString+";password=invalidPassword");
  150. validIncorrectConnStrings.Add (
  151. validConnString+";server=invalidServerName");
  152. int count=0;
  153. foreach (string connString in validIncorrectConnStrings) {
  154. count++;
  155. try {
  156. conn.ConnectionString = connString;
  157. conn.Open();
  158. Assert.Fail (String.Format (
  159. "#1_{0} Incorrect Connection String",count));
  160. }catch (AssertionException e) {
  161. throw e;
  162. }catch (Exception e) {
  163. Assert.AreEqual (typeof (SqlException), e.GetType (),
  164. "#2 Incorrect Exception" + e.StackTrace);
  165. }
  166. }
  167. // Test connection is Opened for a valid Connection String
  168. conn.ConnectionString = connectionString;
  169. conn.Open ();
  170. Assert.AreEqual (ConnectionState.Open, conn.State,
  171. "#3 Connection State Should be OPEN");
  172. // Test Exception is thrown on opening an OPEN Connection
  173. try {
  174. conn.Open ();
  175. Assert.AreEqual (typeof (InvalidOperationException), null,
  176. "#1 Connection is Already Open");
  177. }catch (AssertionException e) {
  178. throw e;
  179. }catch (Exception e) {
  180. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  181. "#2 Incorect Exception.");
  182. }
  183. conn.Close();
  184. /*
  185. // Test if localhost is assumed when servername is empty/missing
  186. // NOTE : msdotnet contradicts doc
  187. Assumes the server is localhost .. need to test this with mono on windows
  188. conn.ConnectionString = connectionString + "server=;";
  189. try {
  190. conn.Open ();
  191. }catch (Exception e) {
  192. Assert.Fail ("## If server name is not given or empty ,localhost shud be tried");
  193. }
  194. ex = null;
  195. conn.Close ();
  196. */
  197. }
  198. [Test]
  199. public void OpenTest_1 ()
  200. {
  201. SqlConnection conn = new SqlConnection ();
  202. conn.ConnectionString = "";
  203. try {
  204. conn.Open ();
  205. Assert.Fail ("#1 Should throw ArgumentException and not SqlException");
  206. } catch (InvalidOperationException) {
  207. }
  208. conn.ConnectionString = " ";
  209. try {
  210. conn.Open ();
  211. Assert.Fail ("#2 Should throw ArgumentException and not SqlException");
  212. } catch (InvalidOperationException) {
  213. }
  214. }
  215. [Test]
  216. public void CreateCommandTest ()
  217. {
  218. conn = new SqlConnection (connectionString);
  219. IDbCommand cmd = conn.CreateCommand ();
  220. Assert.AreSame (conn, cmd.Connection,
  221. "#1 Connection instance should be the same");
  222. }
  223. [Test]
  224. public void CloseTest ()
  225. {
  226. conn = new SqlConnection (connectionString);
  227. conn.Open ();
  228. Assert.AreEqual (ConnectionState.Open, conn.State,
  229. "#1 Connection State should be : Open");
  230. conn.Close ();
  231. Assert.AreEqual (ConnectionState.Closed, conn.State,
  232. "#1 Connection State Should : Closed");
  233. // Test Closing an already closed connection is Valid..
  234. conn.Close ();
  235. }
  236. [Test]
  237. public void DisposeTest ()
  238. {
  239. SqlConnection conn = new SqlConnection (connectionString);
  240. conn.Dispose ();
  241. Assert.AreEqual ("", conn.ConnectionString,
  242. "#1 Dispose shud make the Connection String empty");
  243. Assert.AreEqual (15, conn.ConnectionTimeout,
  244. "#2 Default ConnectionTimeout : 15" );
  245. Assert.AreEqual ("", conn.Database,
  246. "#3 Default Database : empty");
  247. Assert.AreEqual ("", conn.DataSource,
  248. "#4 Default DataSource : empty");
  249. Assert.AreEqual (8192, conn.PacketSize,
  250. "#5 Default Packet Size : 8192");
  251. Assert.AreEqual (Dns.GetHostName().ToUpper (), conn.WorkstationId.ToUpper (),
  252. "#6 Default Workstationid : hostname");
  253. Assert.AreEqual (ConnectionState.Closed, conn.State,
  254. "#7 Default State : CLOSED ");
  255. conn = new SqlConnection ();
  256. //shud not throw exception
  257. conn.Dispose ();
  258. }
  259. [Test]
  260. public void ChangeDatabaseTest ()
  261. {
  262. conn = new SqlConnection (connectionString);
  263. String database = conn.Database;
  264. //Test if exception is thrown if connection is closed
  265. try {
  266. conn.ChangeDatabase ("database");
  267. Assert.AreEqual (typeof (InvalidOperationException), null,
  268. "#1 Connection is Closed");
  269. }catch (AssertionException e){
  270. throw e;
  271. }catch (Exception e) {
  272. Assert.AreEqual (typeof (InvalidOperationException), e.GetType(),
  273. "#2 Incorrect Exception : " + e.StackTrace);
  274. }
  275. //Test if exception is thrown for invalid Database Names
  276. //need to add more to the list
  277. conn.Open ();
  278. String[] InvalidDatabaseNames = {"", null, " "};
  279. for (int i = 0; i < InvalidDatabaseNames.Length ; ++i) {
  280. try {
  281. conn.ChangeDatabase (InvalidDatabaseNames[i]);
  282. Assert.AreEqual (typeof (ArgumentException), null,
  283. string.Format ("#3_{0} Exception not thrown",i));
  284. }catch (AssertionException e) {
  285. throw e;
  286. }catch (Exception e) {
  287. Assert.AreEqual (typeof(ArgumentException), e.GetType (),
  288. string.Format( "#4_{0} Incorrect Exception : {1}",
  289. i, e.StackTrace));
  290. }
  291. Assert.AreEqual (database, conn.Database,
  292. "#4 The Database shouldnt get changed if Operation Failed");
  293. }
  294. //Test if exception is thrown if database name is non-existent
  295. try {
  296. conn.ChangeDatabase ("invalidDB");
  297. Assert.Fail ("#5 Exception must be thrown if database doesent exist");
  298. }catch (AssertionException e) {
  299. throw e;
  300. }catch (Exception e) {
  301. Assert.AreEqual (typeof(SqlException), e.GetType (),
  302. "#6 Incorrect Exception" + e.StackTrace);
  303. }
  304. conn.Close ();
  305. //Test if '-' is a valid character in a database name
  306. //TODO : Check for database names that have more special Characters..
  307. conn.ConnectionString = connectionString;
  308. conn.Open ();
  309. try {
  310. conn.ChangeDatabase ("mono-test");
  311. Assert.AreEqual ("mono-test", conn.Database,
  312. "#7 Database name should be mono-test");
  313. }catch (AssertionException e) {
  314. throw e;
  315. }catch (Exception e){
  316. Assert.Fail ("#8 Unexpected Exception : DB Name can have a '-' : "
  317. + e);
  318. }
  319. }
  320. [Test]
  321. public void InterfaceTransactionTest ()
  322. {
  323. conn = new SqlConnection (connectionString);
  324. conn.Open ();
  325. IDbCommand idbCommand = new SqlCommand ("use [mono-test]", conn);
  326. idbCommand.Connection = null;
  327. Assert.AreEqual (null, idbCommand.Connection, "Connection should be null");
  328. idbCommand.Transaction = null;
  329. Assert.AreEqual (null, idbCommand.Transaction, "Transaction should be null");
  330. conn.Close ();
  331. }
  332. [Test]
  333. public void BeginTransactionTest()
  334. {
  335. conn = new SqlConnection (connectionString);
  336. SqlTransaction trans = null ;
  337. try {
  338. trans = conn.BeginTransaction ();
  339. Assert.Fail ("#1 Connection must be Open to Begin a Transaction");
  340. }catch (AssertionException e) {
  341. throw e;
  342. }catch (Exception e) {
  343. Assert.AreEqual (typeof (InvalidOperationException), e.GetType(),
  344. "#2 Incorrect Exception" + e.StackTrace);
  345. }
  346. conn.Open ();
  347. trans = conn.BeginTransaction ();
  348. Assert.AreSame (conn, trans.Connection,
  349. "#3 Transaction should reference the same connection");
  350. Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel,
  351. "#4 Isolation Level shud be ReadCommitted");
  352. trans.Rollback ();
  353. try {
  354. trans = conn.BeginTransaction ();
  355. trans = conn.BeginTransaction ();
  356. conn.BeginTransaction ();
  357. Assert.Fail ("#5 Parallel Transactions are not supported");
  358. }catch (AssertionException e) {
  359. throw e;
  360. }catch (Exception e) {
  361. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  362. "#6 Incorrect Exception" + e.StackTrace);
  363. }finally {
  364. trans.Rollback();
  365. }
  366. try {
  367. trans = conn.BeginTransaction ();
  368. trans.Rollback ();
  369. trans = conn.BeginTransaction ();
  370. trans.Commit();
  371. trans = conn.BeginTransaction ();
  372. }catch {
  373. Assert.Fail ("#7 Transaction can be opened after a rollback/commit");
  374. }finally {
  375. trans.Rollback ();
  376. }
  377. }
  378. [Test]
  379. public void ConnectionStringPropertyTest ()
  380. {
  381. conn = new SqlConnection (connectionString) ;
  382. // Test Repeated Keyoword ..Should take the latest value
  383. conn.ConnectionString = conn.ConnectionString + ";server=RepeatedServer;" ;
  384. Assert.AreEqual ("RepeatedServer", ((SqlConnection)conn).DataSource,
  385. "#1 if keyword is repeated, the latest value should be taken");
  386. conn.ConnectionString += ";database=gen;Initial Catalog=gen1";
  387. Assert.AreEqual ("gen1", conn.Database,
  388. "#2 database and initial catalog are synonyms .. ");
  389. // Test if properties are set correctly
  390. string str = "server=localhost1;database=db;user id=user;";
  391. str += "password=pwd;Workstation ID=workstation;Packet Size=512;";
  392. str += "Connect Timeout=10";
  393. conn.ConnectionString = str;
  394. Assert.AreEqual ("localhost1", conn.DataSource,
  395. "#3 DataSource name should be same as passed");
  396. Assert.AreEqual ("db", conn.Database,
  397. "#4 Database name shud be same as passed");
  398. Assert.AreEqual (ConnectionState.Closed, conn.State,
  399. "#5 Connection shud be in closed state");
  400. Assert.AreEqual ("workstation", conn.WorkstationId,
  401. "#6 Workstation Id shud be same as passed");
  402. Assert.AreEqual (512, conn.PacketSize,
  403. "#7 Packetsize shud be same as passed");
  404. Assert.AreEqual (10, conn.ConnectionTimeout,
  405. "#8 ConnectionTimeout shud be same as passed");
  406. // Test if any leftover values exist from previous invocation.
  407. conn.ConnectionString = connectionString;
  408. conn.ConnectionString = "";
  409. Assert.AreEqual ("", conn.DataSource,
  410. "#9 Datasource shud be reset to Default : Empty");
  411. Assert.AreEqual ("", conn.Database,
  412. "#10 Database shud reset to Default : Empty");
  413. Assert.AreEqual (8192, conn.PacketSize,
  414. "#11 Packetsize shud be reset to Default : 8192");
  415. Assert.AreEqual (15, conn.ConnectionTimeout,
  416. "#12 ConnectionTimeour shud be reset to Default : 15");
  417. Assert.AreEqual (Dns.GetHostName ().ToUpper (), conn.WorkstationId.ToUpper (),
  418. "#13 WorkstationId shud be reset to Default : Hostname");
  419. // Test Argument Exception is thrown for Invalid Connection Strings
  420. foreach (string connString in invalidConnectionStrings) {
  421. try {
  422. conn.ConnectionString = connString;
  423. Assert.Fail (
  424. "#14 Exception should be thrown");
  425. }catch (AssertionException e){
  426. throw e;
  427. }catch (Exception e) {
  428. Assert.AreEqual (typeof (ArgumentException), e.GetType(),
  429. "#15 Incorrect Exception" + e.StackTrace);
  430. }
  431. }
  432. // Test if ConnectionString is read-only when Connection is OPEN
  433. conn.ConnectionString = connectionString;
  434. conn.Open() ;
  435. try {
  436. Assert.AreEqual (conn.State, ConnectionState.Open,
  437. "#16 Connection shud be open");
  438. conn.ConnectionString = "server=localhost;database=tmp;" ;
  439. Assert.Fail (
  440. "#17 ConnectionString should Read-Only when Connection is Open");
  441. }catch (AssertionException e){
  442. throw e;
  443. }catch (Exception e) {
  444. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  445. "#18 Incorrect Exception" + e.StackTrace);
  446. }
  447. conn.Close ();
  448. }
  449. [Test]
  450. public void ServerVersionTest ()
  451. {
  452. conn = new SqlConnection (connectionString);
  453. // Test InvalidOperation Exception is thrown if Connection is CLOSED
  454. try {
  455. string s = conn.ServerVersion;
  456. Assert.Fail ("#1 InvalidOperation Exception Must be thrown if conn is closed");
  457. Assert.AreEqual ("", s, "#1a Should be an empty string");
  458. } catch (AssertionException e){
  459. throw e;
  460. } catch (Exception e){
  461. Assert.AreEqual(typeof (InvalidOperationException), e.GetType (),
  462. "#2 Incorrect Exception" + e.StackTrace);
  463. }
  464. // Test if Release Version is as per specification.
  465. conn.Open ();
  466. String[] version = conn.ServerVersion.Split ('.') ;
  467. Assert.AreEqual (2, version[0].Length,
  468. "#2 The Major release shud be exactly 2 characters");
  469. Assert.AreEqual (2, version[1].Length,
  470. "#3 The Minor release shud be exactly 2 characters");
  471. Assert.AreEqual (4, version[2].Length,
  472. "#4 The Release version should be exactly 4 digits");
  473. }
  474. [Test]
  475. public void DatabasePropertyTest ()
  476. {
  477. conn = new SqlConnection (connectionString);
  478. string database = conn.Database ;
  479. // Test if database property is updated when a query changes database
  480. conn.Open ();
  481. SqlCommand cmd = new SqlCommand ("use [mono-test]" , conn);
  482. cmd.ExecuteNonQuery ();
  483. Assert.AreEqual ("mono-test", conn.Database,
  484. "#1 DATABASE name shud change if query changes the db");
  485. conn.Close ();
  486. Assert.AreEqual (database, conn.Database,
  487. "#2 Shud be back to default value");
  488. // Test if the database property is reset on re-opening the connection
  489. conn.ConnectionString = connectionString;
  490. conn.Open ();
  491. Assert.AreEqual (database, conn.Database,
  492. "#3 Shud be back to original value");
  493. conn.Close ();
  494. }
  495. [Test]
  496. public void StateChangeEventTest ()
  497. {
  498. conn = new SqlConnection (connectionString);
  499. conn.StateChange += new StateChangeEventHandler (
  500. StateChangeHandlerTest1);
  501. using (conn) {
  502. conn.Open ();
  503. }
  504. Assert.AreEqual (2, stateChangeEventCount,
  505. "#1 The handler shud be called twice");
  506. stateChangeEventCount =0 ;
  507. conn.StateChange -= new StateChangeEventHandler (
  508. StateChangeHandlerTest1);
  509. // NOTE : Need to check the behavior if an exception is raised
  510. // in a handler
  511. }
  512. [Test]
  513. public void DisposedEventTest ()
  514. {
  515. conn = new SqlConnection (connectionString);
  516. conn.Disposed += new EventHandler (DisposedEventHandlerTest1);
  517. conn.Dispose ();
  518. Assert.AreEqual (1, disposedEventCount,
  519. "#1 Disposed eventhandler shud be called");
  520. }
  521. void StateChangeHandlerTest1 (object sender , StateChangeEventArgs e)
  522. {
  523. Assert.IsTrue ((e.CurrentState != e.OriginalState),
  524. "#1 Current and Original state shud be different");
  525. Assert.AreEqual (e.CurrentState, conn.State,
  526. "The conn state and the arg received in event shud be same");
  527. stateChangeEventCount++ ;
  528. }
  529. void DisposedEventHandlerTest1 (object sender , EventArgs e)
  530. {
  531. disposedEventCount++;
  532. }
  533. #if NET_2_0
  534. [Test]
  535. public void FireInfoMessageEventOnUserErrorsTest ()
  536. {
  537. conn = new SqlConnection ();
  538. Assert.AreEqual(false, conn.FireInfoMessageEventOnUserErrors, "#1 The default value should be false");
  539. conn.FireInfoMessageEventOnUserErrors = true;
  540. Assert.AreEqual(true, conn.FireInfoMessageEventOnUserErrors, "#1 The value should be true after setting the property to true");
  541. }
  542. [Test]
  543. public void StatisticsEnabledTest ()
  544. {
  545. conn = new SqlConnection ();
  546. Assert.AreEqual(false, conn.StatisticsEnabled, "#1 The default value should be false");
  547. conn.StatisticsEnabled = true;
  548. Assert.AreEqual(true, conn.StatisticsEnabled, "#1 The value should be true after setting the property to true");
  549. }
  550. [Test]
  551. public void ChangePasswordTest ()
  552. {
  553. string tmpPassword = "modifiedbymonosqlclient";
  554. SqlConnection.ChangePassword (connectionString, tmpPassword);
  555. SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder (connectionString);
  556. string oldPassword = connBuilder.Password;
  557. connBuilder.Password = tmpPassword;
  558. SqlConnection.ChangePassword (connBuilder.ConnectionString, oldPassword); // Modify to the original password
  559. }
  560. [Test]
  561. [ExpectedException (typeof (ArgumentNullException))]
  562. public void ChangePasswordNullConnStringTest ()
  563. {
  564. conn = new SqlConnection (connectionString);
  565. SqlConnection.ChangePassword (null, "mono");
  566. }
  567. [Test]
  568. [ExpectedException (typeof (ArgumentNullException))]
  569. public void ChangePasswordNullPasswordTest ()
  570. {
  571. conn = new SqlConnection (connectionString);
  572. SqlConnection.ChangePassword (connectionString, null);
  573. }
  574. [Test]
  575. [ExpectedException (typeof (ArgumentNullException))]
  576. public void ChangePasswordEmptyPasswordTest ()
  577. {
  578. conn = new SqlConnection (connectionString);
  579. SqlConnection.ChangePassword (connectionString, "");
  580. }
  581. [Test]
  582. [ExpectedException (typeof (ArgumentException))]
  583. public void ChangePasswordExceedPasswordTest ()
  584. {
  585. conn = new SqlConnection (connectionString);
  586. SqlConnection.ChangePassword (connectionString,"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
  587. }
  588. #endif
  589. }
  590. #if NET_2_0
  591. [TestFixture]
  592. [Category ("sqlserver")]
  593. public class GetSchemaTest
  594. {
  595. SqlConnection conn = null;
  596. String connectionString = ConnectionManager.Singleton.ConnectionString;
  597. [SetUp]
  598. public void Setup()
  599. {
  600. conn = new SqlConnection(connectionString);
  601. conn.Open();
  602. }
  603. [TearDown]
  604. public void TearDown()
  605. {
  606. conn.Close();
  607. }
  608. [Test]
  609. public void GetSchemaTest1()
  610. {
  611. bool flag = false;
  612. DataTable tab1 = conn.GetSchema("databases");
  613. foreach (DataRow row in tab1.Rows)
  614. {
  615. foreach (DataColumn col in tab1.Columns)
  616. {
  617. if (col.ColumnName.ToString() == "database_name" && row[col].ToString() == "monotest")
  618. {
  619. flag = true;
  620. break;
  621. }
  622. }
  623. if (flag)
  624. break;
  625. }
  626. Assert.AreEqual(true, flag, "#GS1 failed");
  627. }
  628. [Test]
  629. [ExpectedException(typeof(ArgumentException))]
  630. public void GetSchemaTest2()
  631. {
  632. conn.GetSchema(null);
  633. }
  634. [Test]
  635. public void GetSchemaTest3()
  636. {
  637. bool flag = false;
  638. DataTable tab1 = conn.GetSchema("ForeignKeys");
  639. foreach (DataRow row in tab1.Rows)
  640. {
  641. foreach (DataColumn col in tab1.Columns)
  642. {
  643. /*
  644. * We need to consider multiple values
  645. */
  646. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "tmptable1")
  647. {
  648. flag = true;
  649. break;
  650. }
  651. }
  652. if (flag)
  653. break;
  654. }
  655. Assert.AreEqual(true, flag, "#GS3 failed");
  656. }
  657. [Test]
  658. public void GetSchemaTest4()
  659. {
  660. bool flag = false;
  661. DataTable tab1 = conn.GetSchema("Indexes");
  662. foreach (DataRow row in tab1.Rows)
  663. {
  664. foreach (DataColumn col in tab1.Columns)
  665. {
  666. /*
  667. * We need to consider multiple values
  668. */
  669. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  670. {
  671. flag = true;
  672. break;
  673. }
  674. }
  675. if (flag)
  676. break;
  677. }
  678. Assert.AreEqual(true, flag, "#GS4 failed");
  679. }
  680. [Test]
  681. public void GetSchemaTest5()
  682. {
  683. bool flag = false;
  684. DataTable tab1 = conn.GetSchema("IndexColumns");
  685. foreach (DataRow row in tab1.Rows)
  686. {
  687. foreach (DataColumn col in tab1.Columns)
  688. {
  689. /*
  690. * We need to consider multiple values
  691. */
  692. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  693. {
  694. flag = true;
  695. break;
  696. }
  697. }
  698. if (flag)
  699. break;
  700. }
  701. Assert.AreEqual(true, flag, "#GS5 failed");
  702. }
  703. [Test]
  704. public void GetSchemaTest6()
  705. {
  706. bool flag = false;
  707. DataTable tab1 = conn.GetSchema("Procedures");
  708. foreach (DataRow row in tab1.Rows)
  709. {
  710. foreach (DataColumn col in tab1.Columns)
  711. {
  712. /*
  713. * We need to consider multiple values
  714. */
  715. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  716. {
  717. flag = true;
  718. break;
  719. }
  720. }
  721. if (flag)
  722. break;
  723. }
  724. Assert.AreEqual(true, flag, "#GS6 failed");
  725. }
  726. [Test]
  727. public void GetSchemaTest7()
  728. {
  729. bool flag = false;
  730. DataTable tab1 = conn.GetSchema("ProcedureParameters");
  731. foreach (DataRow row in tab1.Rows)
  732. {
  733. foreach (DataColumn col in tab1.Columns)
  734. {
  735. /*
  736. * We need to consider multiple values
  737. */
  738. if (col.ColumnName.ToString() == "SPECIFIC_NAME" && row[col].ToString() == "sp_get_age")
  739. {
  740. flag = true;
  741. break;
  742. }
  743. }
  744. if (flag)
  745. break;
  746. }
  747. Assert.AreEqual(true, flag, "#GS7 failed");
  748. }
  749. [Test]
  750. public void GetSchemaTest8()
  751. {
  752. bool flag = false;
  753. DataTable tab1 = conn.GetSchema("Tables");
  754. foreach (DataRow row in tab1.Rows)
  755. {
  756. foreach (DataColumn col in tab1.Columns)
  757. {
  758. /*
  759. * We need to consider multiple values
  760. */
  761. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  762. {
  763. flag = true;
  764. break;
  765. }
  766. }
  767. if (flag)
  768. break;
  769. }
  770. Assert.AreEqual(true, flag, "#GS8 failed");
  771. }
  772. [Test]
  773. public void GetSchemaTest9()
  774. {
  775. bool flag = false;
  776. DataTable tab1 = conn.GetSchema("Columns");
  777. foreach (DataRow row in tab1.Rows)
  778. {
  779. foreach (DataColumn col in tab1.Columns)
  780. {
  781. /*
  782. * We need to consider multiple values
  783. */
  784. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  785. {
  786. flag = true;
  787. break;
  788. }
  789. }
  790. if (flag)
  791. break;
  792. }
  793. Assert.AreEqual(true, flag, "#GS9 failed");
  794. }
  795. public void GetSchemaTest10()
  796. {
  797. bool flag = false;
  798. DataTable tab1 = conn.GetSchema("Users");
  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() == "user_name" && row[col].ToString() == "public")
  807. {
  808. flag = true;
  809. break;
  810. }
  811. }
  812. if (flag)
  813. break;
  814. }
  815. Assert.AreEqual(true, flag, "#GS10 failed");
  816. }
  817. public void GetSchemaTest11()
  818. {
  819. bool flag = false;
  820. DataTable tab1 = conn.GetSchema("Views");
  821. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  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() == "user_name" && row[col].ToString() == "public")
  830. {
  831. flag = true;
  832. break;
  833. }
  834. }
  835. if (flag)
  836. break;
  837. }
  838. Assert.AreEqual(true, flag, "#GS11 failed");
  839. }
  840. public void GetSchemaTest12()
  841. {
  842. bool flag = false;
  843. DataTable tab1 = conn.GetSchema("ViewColumns");
  844. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  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() == "user_name" && row[col].ToString() == "public")
  853. {
  854. flag = true;
  855. break;
  856. }
  857. }
  858. if (flag)
  859. break;
  860. }
  861. Assert.AreEqual(true, flag, "#GS12 failed");
  862. }
  863. public void GetSchemaTest13()
  864. {
  865. bool flag = false;
  866. DataTable tab1 = conn.GetSchema("UserDefineTypes");
  867. flag = true; // FIXME: Currently MS-SQL 2005 returns empty table. Remove this flag ASAP.
  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() == "user_name" && row[col].ToString() == "public")
  876. {
  877. flag = true;
  878. break;
  879. }
  880. }
  881. if (flag)
  882. break;
  883. }
  884. Assert.AreEqual(true, flag, "#GS13 failed");
  885. }
  886. [Test]
  887. public void GetSchemaTest14()
  888. {
  889. bool flag = false;
  890. string [] restrictions = new string[4];
  891. restrictions[0] = "monotest";
  892. restrictions[1] = "dbo";
  893. restrictions[2] = null;
  894. restrictions[3] = "BASE TABLE";
  895. DataTable tab1 = conn.GetSchema("Tables", restrictions);
  896. foreach (DataRow row in tab1.Rows)
  897. {
  898. foreach (DataColumn col in tab1.Columns)
  899. {
  900. /*
  901. * We need to consider multiple values
  902. */
  903. if (col.ColumnName.ToString() == "TABLE_NAME" && row[col].ToString() == "binary_family")
  904. {
  905. flag = true;
  906. break;
  907. }
  908. }
  909. if (flag)
  910. break;
  911. }
  912. Assert.AreEqual(true, flag, "#GS14 failed");
  913. }
  914. [Test]
  915. public void GetSchemaTest15()
  916. {
  917. bool flag = false;
  918. string [] restrictions = new string[4];
  919. restrictions[0] = "monotest";
  920. restrictions[1] = null;
  921. restrictions[2] = "binary_family";
  922. restrictions[3] = null;
  923. DataTable tab1 = conn.GetSchema("IndexColumns", restrictions);
  924. foreach (DataRow row in tab1.Rows)
  925. {
  926. foreach (DataColumn col in tab1.Columns)
  927. {
  928. /*
  929. * We need to consider multiple values
  930. */
  931. if (col.ColumnName.ToString() == "table_name" && row[col].ToString() == "binary_family")
  932. {
  933. flag = true;
  934. break;
  935. }
  936. }
  937. if (flag)
  938. break;
  939. }
  940. Assert.AreEqual(true, flag, "#GS15 failed");
  941. }
  942. [Test]
  943. public void GetSchemaTest16()
  944. {
  945. bool flag = false;
  946. string [] restrictions = new string[4];
  947. restrictions[0] = "monotest";
  948. restrictions[1] = null;
  949. restrictions[2] = "sp_get_age";
  950. restrictions[3] = null;
  951. DataTable tab1 = conn.GetSchema("Procedures", restrictions);
  952. foreach (DataRow row in tab1.Rows)
  953. {
  954. foreach (DataColumn col in tab1.Columns)
  955. {
  956. /*
  957. * We need to consider multiple values
  958. */
  959. if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "sp_get_age")
  960. {
  961. flag = true;
  962. break;
  963. }
  964. }
  965. if (flag)
  966. break;
  967. }
  968. Assert.AreEqual(true, flag, "#GS16 failed");
  969. }
  970. [Test]
  971. public void GetSchemaTest17()
  972. {
  973. bool flag = false;
  974. DataTable tab1 = conn.GetSchema();
  975. foreach (DataRow row in tab1.Rows)
  976. {
  977. foreach (DataColumn col in tab1.Columns)
  978. {
  979. /*
  980. * We need to consider multiple values
  981. */
  982. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  983. {
  984. flag = true;
  985. break;
  986. }
  987. }
  988. if (flag)
  989. break;
  990. }
  991. Assert.AreEqual(true, flag, "#GS17 failed");
  992. }
  993. [Test]
  994. public void GetSchemaTest18()
  995. {
  996. bool flag = false;
  997. DataTable tab1 = conn.GetSchema("RESTRICTIONS");
  998. foreach (DataRow row in tab1.Rows)
  999. {
  1000. foreach (DataColumn col in tab1.Columns)
  1001. {
  1002. /*
  1003. * We need to consider multiple values
  1004. */
  1005. if (col.ColumnName.ToString() == "RestrictionDefault" && row[col].ToString() == "CONSTRAINT_NAME")
  1006. {
  1007. flag = true;
  1008. break;
  1009. }
  1010. }
  1011. if (flag)
  1012. break;
  1013. }
  1014. Assert.AreEqual(true, flag, "#GS18 failed");
  1015. }
  1016. [Test]
  1017. [ExpectedException (typeof (ArgumentException))]
  1018. public void GetSchemaTest19 ()
  1019. {
  1020. String [] restrictions = new String[1];
  1021. conn.GetSchema("RESTRICTIONS", restrictions);
  1022. }
  1023. [Test]
  1024. public void GetSchemaTest20 ()
  1025. {
  1026. bool flag = false;
  1027. DataTable tab1 = conn.GetSchema("DataTypes");
  1028. foreach (DataRow row in tab1.Rows)
  1029. {
  1030. foreach (DataColumn col in tab1.Columns)
  1031. {
  1032. /*
  1033. * We need to consider multiple values
  1034. */
  1035. if (col.ColumnName.ToString() == "TypeName" && row[col].ToString() == "uniqueidentifier")
  1036. {
  1037. flag = true;
  1038. break;
  1039. }
  1040. }
  1041. if (flag)
  1042. break;
  1043. }
  1044. Assert.AreEqual(true, flag, "#GS20 failed");
  1045. }
  1046. [Test]
  1047. public void GetSchemaTest21()
  1048. {
  1049. bool flag = false;
  1050. DataTable tab1 = conn.GetSchema();
  1051. foreach (DataRow row in tab1.Rows)
  1052. {
  1053. foreach (DataColumn col in tab1.Columns)
  1054. {
  1055. /*
  1056. * We need to consider multiple values
  1057. */
  1058. if (col.ColumnName.ToString() == "CollectionName" && row[col].ToString() == "UserDefinedTypes")
  1059. {
  1060. flag = true;
  1061. break;
  1062. }
  1063. }
  1064. if (flag)
  1065. break;
  1066. }
  1067. Assert.AreEqual(true, flag, "#GS21 failed");
  1068. }
  1069. [Test]
  1070. public void GetSchemaTest22()
  1071. {
  1072. bool flag = false;
  1073. DataTable tab1 = conn.GetSchema("ReservedWords");
  1074. foreach (DataRow row in tab1.Rows)
  1075. {
  1076. foreach (DataColumn col in tab1.Columns)
  1077. {
  1078. /*
  1079. * We need to consider multiple values
  1080. */
  1081. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "UPPER")
  1082. {
  1083. flag = true;
  1084. break;
  1085. }
  1086. }
  1087. if (flag)
  1088. break;
  1089. }
  1090. Assert.AreEqual(true, flag, "#GS22 failed");
  1091. }
  1092. [Test]
  1093. public void GetSchemaTest23()
  1094. {
  1095. bool flag = false;
  1096. DataTable tab1 = conn.GetSchema("ReservedWords");
  1097. foreach (DataRow row in tab1.Rows)
  1098. {
  1099. foreach (DataColumn col in tab1.Columns)
  1100. {
  1101. /*
  1102. * We need to consider multiple values
  1103. */
  1104. if (col.ColumnName.ToString() == "ReservedWord" && row[col].ToString() == "upper")
  1105. {
  1106. flag = true;
  1107. break;
  1108. }
  1109. }
  1110. if (flag)
  1111. break;
  1112. }
  1113. Assert.AreEqual(false, flag, "#GS23 failed");
  1114. }
  1115. [Test]
  1116. public void GetSchemaTest24()
  1117. {
  1118. bool flag = false;
  1119. string [] restrictions = new string[4];
  1120. restrictions[0] = "monotest";
  1121. restrictions[1] = null;
  1122. restrictions[2] = "sp_get_age";
  1123. restrictions[3] = null;
  1124. DataTable tab1 = conn.GetSchema("Procedures", restrictions);
  1125. foreach (DataRow row in tab1.Rows)
  1126. {
  1127. foreach (DataColumn col in tab1.Columns)
  1128. {
  1129. /*
  1130. * We need to consider multiple values
  1131. */
  1132. if (col.ColumnName.ToString() == "ROUTINE_NAME" && row[col].ToString() == "mono")
  1133. {
  1134. flag = true;
  1135. break;
  1136. }
  1137. }
  1138. if (flag)
  1139. break;
  1140. }
  1141. Assert.AreEqual(false, flag, "#GS24 failed");
  1142. }
  1143. [Test]
  1144. [ExpectedException (typeof (ArgumentException))]
  1145. public void GetSchemaTest25 ()
  1146. {
  1147. String [] restrictions = new String [1];
  1148. conn.GetSchema ("Mono", restrictions);
  1149. }
  1150. }
  1151. #endif
  1152. }