2
0

OdbcConnectionTest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // OdbcConnectionTest.cs - NUnit Test Cases for testing the
  3. // OdbcConnectionTest class
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // Copyright (c) 2007 Gert Driesen
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Data;
  30. using System.Data.Odbc;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Data.Odbc
  33. {
  34. [TestFixture]
  35. public class OdbcConnectionTest
  36. {
  37. const string CONNECTION_STRING = "Driver={SQL Server};Server=SQLSRV;Database=Mono;";
  38. [Test] // OdbcConnection ()
  39. public void Constructor1 ()
  40. {
  41. OdbcConnection cn = new OdbcConnection ();
  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. Assert.AreEqual (string.Empty, cn.Driver, "#6");
  48. Assert.IsNull (cn.Site, "#7");
  49. Assert.AreEqual (ConnectionState.Closed, cn.State, "#8");
  50. }
  51. [Test] // OdbcConnection (string)
  52. public void Constructor2 ()
  53. {
  54. OdbcConnection cn = new OdbcConnection (CONNECTION_STRING);
  55. Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#A1");
  56. Assert.AreEqual (15, cn.ConnectionTimeout, "#A2");
  57. Assert.IsNull (cn.Container, "#A3");
  58. Assert.AreEqual (string.Empty, cn.Database, "#A4");
  59. Assert.AreEqual (string.Empty, cn.DataSource, "#A5");
  60. Assert.AreEqual (string.Empty, cn.Driver, "#A6");
  61. Assert.IsNull (cn.Site, "#A7");
  62. Assert.AreEqual (ConnectionState.Closed, cn.State, "#A8");
  63. cn = new OdbcConnection ((string) null);
  64. Assert.AreEqual (string.Empty, cn.ConnectionString, "#B1");
  65. Assert.AreEqual (15, cn.ConnectionTimeout, "#B2");
  66. Assert.IsNull (cn.Container, "#B3");
  67. Assert.AreEqual (string.Empty, cn.Database, "#B4");
  68. Assert.AreEqual (string.Empty, cn.DataSource, "#B5");
  69. Assert.AreEqual (string.Empty, cn.Driver, "#B6");
  70. Assert.IsNull (cn.Site, "#B7");
  71. Assert.AreEqual (ConnectionState.Closed, cn.State, "#B8");
  72. }
  73. [Test]
  74. public void BeginTransaction_Connection_Closed ()
  75. {
  76. OdbcConnection cn = new OdbcConnection ();
  77. try {
  78. cn.BeginTransaction ();
  79. Assert.Fail ("#A1");
  80. } catch (InvalidOperationException ex) {
  81. // Invalid operation. The connection is closed
  82. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  83. Assert.IsNull (ex.InnerException, "#A3");
  84. Assert.IsNotNull (ex.Message, "#A4");
  85. }
  86. try {
  87. cn.BeginTransaction ((IsolationLevel) 666);
  88. Assert.Fail ("#B1");
  89. } catch (InvalidOperationException ex) {
  90. // Invalid operation. The connection is closed
  91. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  92. Assert.IsNull (ex.InnerException, "#B3");
  93. Assert.IsNotNull (ex.Message, "#B4");
  94. }
  95. try {
  96. cn.BeginTransaction (IsolationLevel.Serializable);
  97. Assert.Fail ("#C1");
  98. } catch (InvalidOperationException ex) {
  99. // Invalid operation. The connection is closed
  100. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  101. Assert.IsNull (ex.InnerException, "#C3");
  102. Assert.IsNotNull (ex.Message, "#C4");
  103. }
  104. }
  105. [Test]
  106. public void ConnectionString ()
  107. {
  108. OdbcConnection cn = new OdbcConnection ();
  109. cn.ConnectionString = CONNECTION_STRING;
  110. Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#1");
  111. cn.ConnectionString = null;
  112. Assert.AreEqual (string.Empty, cn.ConnectionString, "#2");
  113. cn.ConnectionString = CONNECTION_STRING;
  114. Assert.AreEqual (CONNECTION_STRING, cn.ConnectionString, "#3");
  115. cn.ConnectionString = string.Empty;
  116. Assert.AreEqual (string.Empty, cn.ConnectionString, "#4");
  117. }
  118. #if NET_2_0
  119. [Test]
  120. public void GetSchema_Connection_Closed ()
  121. {
  122. OdbcConnection cn = new OdbcConnection ();
  123. try {
  124. cn.GetSchema ();
  125. Assert.Fail ("#A1");
  126. } catch (InvalidOperationException ex) {
  127. // Invalid operation. The connection is closed
  128. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  129. Assert.IsNull (ex.InnerException, "#B3");
  130. Assert.IsNotNull (ex.Message, "#B4");
  131. }
  132. try {
  133. cn.GetSchema ("Tables");
  134. Assert.Fail ("#B1");
  135. } catch (InvalidOperationException ex) {
  136. // Invalid operation. The connection is closed
  137. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  138. Assert.IsNull (ex.InnerException, "#B3");
  139. Assert.IsNotNull (ex.Message, "#B4");
  140. }
  141. try {
  142. cn.GetSchema ((string) null);
  143. Assert.Fail ("#C1");
  144. } catch (InvalidOperationException ex) {
  145. // Invalid operation. The connection is closed
  146. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  147. Assert.IsNull (ex.InnerException, "#C3");
  148. Assert.IsNotNull (ex.Message, "#C4");
  149. }
  150. try {
  151. cn.GetSchema ("Tables", new string [] { "master" });
  152. Assert.Fail ("#D1");
  153. } catch (InvalidOperationException ex) {
  154. // Invalid operation. The connection is closed
  155. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  156. Assert.IsNull (ex.InnerException, "#D3");
  157. Assert.IsNotNull (ex.Message, "#D4");
  158. }
  159. try {
  160. cn.GetSchema ((string) null, new string [] { "master" });
  161. Assert.Fail ("#E1");
  162. } catch (InvalidOperationException ex) {
  163. // Invalid operation. The connection is closed
  164. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
  165. Assert.IsNull (ex.InnerException, "#E3");
  166. Assert.IsNotNull (ex.Message, "#E4");
  167. }
  168. try {
  169. cn.GetSchema ("Tables", (string []) null);
  170. Assert.Fail ("#F1");
  171. } catch (InvalidOperationException ex) {
  172. // Invalid operation. The connection is closed
  173. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
  174. Assert.IsNull (ex.InnerException, "#F3");
  175. Assert.IsNotNull (ex.Message, "#F4");
  176. }
  177. try {
  178. cn.GetSchema ((string) null, (string []) null);
  179. Assert.Fail ("#G1");
  180. } catch (InvalidOperationException ex) {
  181. // Invalid operation. The connection is closed
  182. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
  183. Assert.IsNull (ex.InnerException, "#G3");
  184. Assert.IsNotNull (ex.Message, "#G4");
  185. }
  186. }
  187. #endif
  188. [Test]
  189. public void ServerVersion_Connection_Closed ()
  190. {
  191. OdbcConnection cn = new OdbcConnection ();
  192. try {
  193. Assert.Fail ("#A1:" + cn.ServerVersion);
  194. } catch (InvalidOperationException ex) {
  195. // Invalid operation. The connection is closed
  196. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  197. Assert.IsNull (ex.InnerException, "#A3");
  198. Assert.IsNotNull (ex.Message, "#A4");
  199. }
  200. cn = new OdbcConnection (CONNECTION_STRING);
  201. try {
  202. Assert.Fail ("#B1:" + cn.ServerVersion);
  203. } catch (InvalidOperationException ex) {
  204. // Invalid operation. The connection is closed
  205. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  206. Assert.IsNull (ex.InnerException, "#B3");
  207. Assert.IsNotNull (ex.Message, "#B4");
  208. }
  209. }
  210. }
  211. }