SqlConnectionTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // SqlConnectionTest.cs - NUnit Test Cases for testing the
  3. // SqlConnection class
  4. // Author:
  5. // Gert Driesen ([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 NUnit.Framework;
  33. namespace MonoTests.System.Data.SqlClient
  34. {
  35. [TestFixture]
  36. public class SqlConnectionTest
  37. {
  38. [Test] // SqlConnection ()
  39. public void Constructor1 ()
  40. {
  41. SqlConnection cn = new SqlConnection ();
  42. Assert.AreEqual (string.Empty, cn.ConnectionString, "#1");
  43. Assert.AreEqual (15, cn.ConnectionTimeout, "#2");
  44. Assert.IsNull (cn.Container, "#3");
  45. Assert.AreEqual (string.Empty, cn.Database, "#4");
  46. Assert.AreEqual (string.Empty, cn.DataSource, "#5");
  47. #if NET_2_0
  48. Assert.IsFalse (cn.FireInfoMessageEventOnUserErrors, "#6");
  49. Assert.AreEqual (8000, cn.PacketSize, "#7");
  50. #else
  51. Assert.AreEqual (8192, cn.PacketSize, "#7");
  52. #endif
  53. Assert.IsNull (cn.Site, "#8");
  54. Assert.AreEqual (ConnectionState.Closed, cn.State, "#9");
  55. #if NET_2_0
  56. Assert.IsFalse (cn.StatisticsEnabled, "#10");
  57. #endif
  58. Assert.AreEqual (Environment.MachineName, cn.WorkstationId, "#11");
  59. }
  60. [Test] // SqlConnection (string)
  61. public void Constructor2 ()
  62. {
  63. string connectionString = "server=SQLSRV; database=Mono;";
  64. SqlConnection cn = new SqlConnection (connectionString);
  65. Assert.AreEqual (connectionString, cn.ConnectionString, "#A1");
  66. Assert.AreEqual (15, cn.ConnectionTimeout, "#A2");
  67. Assert.IsNull (cn.Container, "#A3");
  68. Assert.AreEqual ("Mono", cn.Database, "#A4");
  69. Assert.AreEqual ("SQLSRV", cn.DataSource, "#A5");
  70. #if NET_2_0
  71. Assert.IsFalse (cn.FireInfoMessageEventOnUserErrors, "#A6");
  72. Assert.AreEqual (8000, cn.PacketSize, "#A7");
  73. #else
  74. Assert.AreEqual (8192, cn.PacketSize, "#A7");
  75. #endif
  76. Assert.IsNull (cn.Site, "#A8");
  77. Assert.AreEqual (ConnectionState.Closed, cn.State, "#A9");
  78. #if NET_2_0
  79. Assert.IsFalse (cn.StatisticsEnabled, "#A10");
  80. #endif
  81. Assert.AreEqual (Environment.MachineName, cn.WorkstationId, "#A11");
  82. cn = new SqlConnection ((string) null);
  83. Assert.AreEqual (string.Empty, cn.ConnectionString, "#B1");
  84. Assert.AreEqual (15, cn.ConnectionTimeout, "#B2");
  85. Assert.IsNull (cn.Container, "#B3");
  86. Assert.AreEqual (string.Empty, cn.Database, "#B4");
  87. Assert.AreEqual (string.Empty, cn.DataSource, "#B5");
  88. #if NET_2_0
  89. Assert.IsFalse (cn.FireInfoMessageEventOnUserErrors, "#B6");
  90. Assert.AreEqual (8000, cn.PacketSize, "#B7");
  91. #else
  92. Assert.AreEqual (8192, cn.PacketSize, "#B7");
  93. #endif
  94. Assert.IsNull (cn.Site, "#B8");
  95. Assert.AreEqual (ConnectionState.Closed, cn.State, "#B9");
  96. #if NET_2_0
  97. Assert.IsFalse (cn.StatisticsEnabled, "#B10");
  98. #endif
  99. Assert.AreEqual (Environment.MachineName, cn.WorkstationId, "#B11");
  100. }
  101. [Test]
  102. public void BeginTransaction_Connection_Closed ()
  103. {
  104. SqlConnection cn = new SqlConnection ();
  105. try {
  106. cn.BeginTransaction ();
  107. Assert.Fail ("#A1");
  108. } catch (InvalidOperationException ex) {
  109. // Invalid operation. The connection is closed
  110. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  111. Assert.IsNull (ex.InnerException, "#A3");
  112. Assert.IsNotNull (ex.Message, "#A4");
  113. }
  114. try {
  115. cn.BeginTransaction ((IsolationLevel) 666);
  116. Assert.Fail ("#B1");
  117. } catch (InvalidOperationException ex) {
  118. // Invalid operation. The connection is closed
  119. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  120. Assert.IsNull (ex.InnerException, "#B3");
  121. Assert.IsNotNull (ex.Message, "#B4");
  122. }
  123. try {
  124. cn.BeginTransaction (IsolationLevel.Serializable);
  125. Assert.Fail ("#C1");
  126. } catch (InvalidOperationException ex) {
  127. // Invalid operation. The connection is closed
  128. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  129. Assert.IsNull (ex.InnerException, "#C3");
  130. Assert.IsNotNull (ex.Message, "#C4");
  131. }
  132. try {
  133. cn.BeginTransaction ("trans");
  134. Assert.Fail ("#D1");
  135. } catch (InvalidOperationException ex) {
  136. // Invalid operation. The connection is closed
  137. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  138. Assert.IsNull (ex.InnerException, "#D3");
  139. Assert.IsNotNull (ex.Message, "#D4");
  140. }
  141. try {
  142. cn.BeginTransaction ((IsolationLevel) 666, "trans");
  143. Assert.Fail ("#E1");
  144. } catch (InvalidOperationException ex) {
  145. // Invalid operation. The connection is closed
  146. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
  147. Assert.IsNull (ex.InnerException, "#E3");
  148. Assert.IsNotNull (ex.Message, "#E4");
  149. }
  150. try {
  151. cn.BeginTransaction (IsolationLevel.Serializable, "trans");
  152. Assert.Fail ("#F1");
  153. } catch (InvalidOperationException ex) {
  154. // Invalid operation. The connection is closed
  155. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
  156. Assert.IsNull (ex.InnerException, "#F3");
  157. Assert.IsNotNull (ex.Message, "#F4");
  158. }
  159. }
  160. [Test]
  161. public void ConnectionString ()
  162. {
  163. SqlConnection cn = new SqlConnection ();
  164. cn.ConnectionString = "server=SQLSRV";
  165. Assert.AreEqual ("server=SQLSRV", cn.ConnectionString, "#1");
  166. cn.ConnectionString = null;
  167. Assert.AreEqual (string.Empty, cn.ConnectionString, "#2");
  168. cn.ConnectionString = "server=SQLSRV";
  169. Assert.AreEqual ("server=SQLSRV", cn.ConnectionString, "#3");
  170. cn.ConnectionString = string.Empty;
  171. Assert.AreEqual (string.Empty, cn.ConnectionString, "#4");
  172. }
  173. [Test]
  174. public void ConnectionTimeoutSynonyms()
  175. {
  176. SqlConnection cn = null;
  177. cn = new SqlConnection ();
  178. cn.ConnectionString = "Connection Timeout=25";
  179. Assert.AreEqual (25, cn.ConnectionTimeout);
  180. cn = new SqlConnection ();
  181. cn.ConnectionString = "Connect Timeout=25";
  182. Assert.AreEqual (25, cn.ConnectionTimeout);
  183. cn = new SqlConnection ();
  184. cn.ConnectionString = "Timeout=25";
  185. Assert.AreEqual (25, cn.ConnectionTimeout);
  186. }
  187. #if NET_2_0
  188. [Test]
  189. public void GetSchema_Connection_Closed ()
  190. {
  191. SqlConnection cn = new SqlConnection ();
  192. try {
  193. cn.GetSchema ();
  194. Assert.Fail ("#A1");
  195. } catch (InvalidOperationException ex) {
  196. // Invalid operation. The connection is closed
  197. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  198. Assert.IsNull (ex.InnerException, "#B3");
  199. Assert.IsNotNull (ex.Message, "#B4");
  200. }
  201. try {
  202. cn.GetSchema ("Tables");
  203. Assert.Fail ("#B1");
  204. } catch (InvalidOperationException ex) {
  205. // Invalid operation. The connection is closed
  206. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  207. Assert.IsNull (ex.InnerException, "#B3");
  208. Assert.IsNotNull (ex.Message, "#B4");
  209. }
  210. try {
  211. cn.GetSchema ((string) null);
  212. Assert.Fail ("#C1");
  213. } catch (InvalidOperationException ex) {
  214. // Invalid operation. The connection is closed
  215. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  216. Assert.IsNull (ex.InnerException, "#C3");
  217. Assert.IsNotNull (ex.Message, "#C4");
  218. }
  219. try {
  220. cn.GetSchema ("Tables", new string [] { "master" });
  221. Assert.Fail ("#D1");
  222. } catch (InvalidOperationException ex) {
  223. // Invalid operation. The connection is closed
  224. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  225. Assert.IsNull (ex.InnerException, "#D3");
  226. Assert.IsNotNull (ex.Message, "#D4");
  227. }
  228. try {
  229. cn.GetSchema ((string) null, new string [] { "master" });
  230. Assert.Fail ("#E1");
  231. } catch (InvalidOperationException ex) {
  232. // Invalid operation. The connection is closed
  233. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
  234. Assert.IsNull (ex.InnerException, "#E3");
  235. Assert.IsNotNull (ex.Message, "#E4");
  236. }
  237. try {
  238. cn.GetSchema ("Tables", (string []) null);
  239. Assert.Fail ("#F1");
  240. } catch (InvalidOperationException ex) {
  241. // Invalid operation. The connection is closed
  242. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
  243. Assert.IsNull (ex.InnerException, "#F3");
  244. Assert.IsNotNull (ex.Message, "#F4");
  245. }
  246. try {
  247. cn.GetSchema ((string) null, (string []) null);
  248. Assert.Fail ("#G1");
  249. } catch (InvalidOperationException ex) {
  250. // Invalid operation. The connection is closed
  251. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
  252. Assert.IsNull (ex.InnerException, "#G3");
  253. Assert.IsNotNull (ex.Message, "#G4");
  254. }
  255. }
  256. #endif
  257. [Test]
  258. public void NetworkLibrarySynonyms()
  259. {
  260. SqlConnection cn = new SqlConnection ();
  261. cn.ConnectionString = "Net=DBMSSOCN";
  262. cn.ConnectionString = "Network=DBMSSOCN";
  263. cn.ConnectionString = "Network library=DBMSSOCN";
  264. }
  265. [Test]
  266. public void DatabaseSynonyms()
  267. {
  268. SqlConnection cn = null;
  269. cn = new SqlConnection ();
  270. cn.ConnectionString = "Initial Catalog=db";
  271. Assert.AreEqual ("db", cn.Database);
  272. cn = new SqlConnection ();
  273. cn.ConnectionString = "Database=db";
  274. Assert.AreEqual ("db", cn.Database);
  275. }
  276. [Test]
  277. public void DataSourceSynonyms()
  278. {
  279. SqlConnection cn = null;
  280. cn = new SqlConnection ();
  281. cn.ConnectionString = "Data Source=server";
  282. Assert.AreEqual ("server", cn.DataSource);
  283. cn = new SqlConnection ();
  284. cn.ConnectionString = "addr=server";
  285. Assert.AreEqual ("server", cn.DataSource);
  286. cn = new SqlConnection ();
  287. cn.ConnectionString = "address=server";
  288. Assert.AreEqual ("server", cn.DataSource);
  289. cn = new SqlConnection ();
  290. cn.ConnectionString = "network address=server";
  291. Assert.AreEqual ("server", cn.DataSource);
  292. cn = new SqlConnection ();
  293. cn.ConnectionString = "server=server";
  294. Assert.AreEqual ("server", cn.DataSource);
  295. }
  296. [Test]
  297. public void OtherConnectionStringKeywords()
  298. {
  299. SqlConnection cn = new SqlConnection ();
  300. cn.ConnectionString = "Application Name=test";
  301. cn.ConnectionString = "App=test";
  302. cn.ConnectionString = "Connection LifeTime=1000";
  303. cn.ConnectionString = "Connection Reset=true";
  304. cn.ConnectionString = "Current Language=test";
  305. cn.ConnectionString = "Language=test";
  306. cn.ConnectionString = "Encrypt=false";
  307. cn.ConnectionString = "Enlist=true";
  308. cn.ConnectionString = "Integrated Security=true";
  309. cn.ConnectionString = "Trusted_connection=true";
  310. cn.ConnectionString = "Max Pool Size=10";
  311. cn.ConnectionString = "Min Pool Size=10";
  312. cn.ConnectionString = "Password=scrambled";
  313. cn.ConnectionString = "Pwd=scrambled";
  314. cn.ConnectionString = "Pooling=true";
  315. cn.ConnectionString = "User Id=test";
  316. cn.ConnectionString = "User=test";
  317. cn.ConnectionString = "Uid=test";
  318. /*
  319. * NOT IMPLEMENTED YET
  320. */
  321. /*
  322. cn.ConnectionString = "Persist Security Info=true";
  323. cn.ConnectionString = "PersistSecurityInfo=true";
  324. cn.ConnectionString = "Encrypt=true";
  325. cn.ConnectionString = "Enlist=false";
  326. cn.ConnectionString = "attachdbfilename=dunno";
  327. cn.ConnectionString = "extended properties=dunno";
  328. cn.ConnectionString = "initial file name=dunno";
  329. */
  330. }
  331. [Test]
  332. public void ServerVersion_Connection_Closed ()
  333. {
  334. SqlConnection cn = new SqlConnection ();
  335. try {
  336. Assert.Fail ("#A1:" + cn.ServerVersion);
  337. } catch (InvalidOperationException ex) {
  338. // Invalid operation. The connection is closed
  339. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  340. Assert.IsNull (ex.InnerException, "#A3");
  341. Assert.IsNotNull (ex.Message, "#A4");
  342. }
  343. cn = new SqlConnection ("server=SQLSRV; database=Mono;");
  344. try {
  345. Assert.Fail ("#B1:" + cn.ServerVersion);
  346. } catch (InvalidOperationException ex) {
  347. // Invalid operation. The connection is closed
  348. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  349. Assert.IsNull (ex.InnerException, "#B3");
  350. Assert.IsNotNull (ex.Message, "#B4");
  351. }
  352. }
  353. }
  354. }