OdbcConnectionStringBuilderTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // OdbcConnectionStringBuilderTest.cs - NUnit Test Cases for testing the
  2. // OdbcConnectionStringBuilder Class.
  3. //
  4. // Authors:
  5. // Nidhi Rawal ([email protected])
  6. //
  7. // Copyright (c) 2007 Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. #if NET_2_0
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.Odbc;
  35. using Mono.Data;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Data
  38. {
  39. [TestFixture]
  40. [Category ("odbc")]
  41. public class OdbcConnectionStringBuilderTest
  42. {
  43. [Test]
  44. public void IndexerTest ()
  45. {
  46. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  47. builder ["Dbq"] = "C:\\Data.xls";
  48. builder ["DriverID"] = "790";
  49. builder ["DefaultDir"] = "C:\\";
  50. Assert.AreEqual ("790", builder ["DriverID"], "#1 The value of the key DriverID is not as expected");
  51. Assert.AreEqual ("C:\\Data.xls", builder ["Dbq"], "#2 The value of the key Dbq is not as expected");
  52. Assert.AreEqual ("C:\\", builder ["DefaultDir"], "#3 The value of the key DefaultDir is not as expected");
  53. }
  54. [Test]
  55. public void ConnectionStringConstructorTest ()
  56. {
  57. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ("Driver={SQL Server};Server=(local);Database=AdventureWorks;Uid=ab;Pwd=pass@word1");
  58. Assert.AreEqual ("AdventureWorks", builder ["Database"],"#1 The value of the key AdventureWorks does not match with the one set in the constructor");
  59. Assert.AreEqual ("pass@word1", builder ["Pwd"], "#2 The value of the key Pwd does not match with the one set in the constructor");
  60. Assert.AreEqual ("ab", builder ["Uid"], "#3 The value of the key Uid does not match with the one set in the constructor");
  61. Assert.AreEqual ("{SQL Server}", builder ["Driver"], "#4 The value of the key Driver does not match with the one set in the constructor");
  62. Assert.AreEqual ("(local)", builder ["Server"],"#5 The value of the key Server does not match with the one set in the constructor");
  63. OdbcConnectionStringBuilder oDriver = new OdbcConnectionStringBuilder ("Driver=");
  64. Assert.AreEqual ("", oDriver.ConnectionString, "#6 It should not add keyword without value");
  65. OdbcConnectionStringBuilder oDsn = new OdbcConnectionStringBuilder ("Dsn=");
  66. Assert.AreEqual ("", oDsn.ConnectionString, "#6 It should not add keyword without value");
  67. }
  68. // Test case failing
  69. [Test]
  70. public void ConnectionStringTest ()
  71. {
  72. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Jet OLEDB:System Database=|DataDirectory|\System.mdw;");
  73. Assert.AreEqual ("Microsoft.Jet.OLEDB.4.0", builder ["Provider"], "#1 ");
  74. Assert.AreEqual (@"|DataDirectory|\Northwind.mdb", builder ["Data Source"], "#2 ");
  75. Assert.AreEqual (@"|DataDirectory|\System.mdw", builder ["Jet OLEDB:System Database"], "#3 ");
  76. OdbcConnectionStringBuilder builder1 = new OdbcConnectionStringBuilder ();
  77. builder1 ["Data Source"] = "(local)";
  78. builder1 ["Integrated Security"] = true;
  79. builder1 ["Initial Catalog"] = "AdventureWorks;NewValue=Bad";
  80. Assert.AreEqual ("Integrated Security=True;Initial Catalog={AdventureWorks;NewValue=Bad};Data Source=(local)",
  81. builder1.ConnectionString, "#4 "); // Not in same sequence as MS.NET
  82. OdbcConnectionStringBuilder builder2 = new OdbcConnectionStringBuilder (@"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls");
  83. Assert.AreEqual ("{Microsoft Excel Driver (*.xls)}", builder2["Driver"], "#5 ");
  84. Assert.AreEqual (@"c:\bin\book1.xls", builder2["DBQ"], "#6 ");
  85. OdbcConnectionStringBuilder builder3 = new OdbcConnectionStringBuilder (@"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin");
  86. Assert.AreEqual ("{Microsoft Text Driver (*.txt; *.csv)}", builder3["Driver"], "#7 "); // Not giving correct value
  87. Assert.AreEqual (@"c:\bin", builder3["DBQ"], "#8 ");
  88. }
  89. [Test]
  90. public void ConnectionStringConstructorNullTest ()
  91. {
  92. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder (null);
  93. Assert.AreEqual ("", builder.ConnectionString, "#1");
  94. OdbcConnectionStringBuilder oc = new OdbcConnectionStringBuilder ("");
  95. Assert.AreEqual ("", oc.ConnectionString, "#2");
  96. }
  97. [Test]
  98. public void DuplicateKeyTest ()
  99. {
  100. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  101. builder ["DriverID"] = "120";
  102. builder ["DriverID"] = "790";
  103. Assert.AreEqual ("DriverID=790", builder.ConnectionString, "#1 The connection string should take most recent value");
  104. }
  105. [Test]
  106. [ExpectedException (typeof (ArgumentException))]
  107. public void IndexerValueNullTest ()
  108. {
  109. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  110. builder ["DriverID"] = null;
  111. Assert.AreEqual (null, builder ["DriverID"], "#1");
  112. }
  113. [Test]
  114. [ExpectedException (typeof (ArgumentNullException))]
  115. public void IndexerKeywordNullTest ()
  116. {
  117. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  118. builder [null] = "abc";
  119. Assert.AreEqual ("abc", builder [null], "#1");
  120. }
  121. [Test]
  122. public void DriverTest ()
  123. {
  124. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  125. builder.Driver = "SQL Server";
  126. Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#1 The connection string should contain the value for the driver that is being set by the property");
  127. Assert.AreEqual ("SQL Server", builder.Driver, "#2 The property should return the value that is being set");
  128. builder.Clear ();
  129. builder ["Driver"] = "SQL Server";
  130. Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#3 The connection string should contain the value for the driver that is being set by assigning the value to key");
  131. Assert.AreEqual ("SQL Server", builder.Driver, "#4 The property should return the value that is being set by assigning the value to the key");
  132. builder.Clear ();
  133. builder ["Driver"] = "{SQL Server";
  134. Assert.AreEqual ("Driver={{SQL Server}", builder.ConnectionString, "#5 ");
  135. Assert.AreEqual ("{SQL Server", builder.Driver, "#6 ");
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentNullException))]
  139. public void DriverNullTest ()
  140. {
  141. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  142. builder.Driver = null;
  143. }
  144. [Test]
  145. public void IndexerDriverNullTest ()
  146. {
  147. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  148. builder ["Driver"] = null;
  149. Assert.AreEqual ("", builder.Driver, "#1 ");
  150. Assert.AreEqual ("", builder ["Driver"], "#2 ");
  151. }
  152. [Test]
  153. public void DsnTest ()
  154. {
  155. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  156. builder.Dsn = "myDsn";
  157. Assert.AreEqual ("Dsn=myDsn", builder.ConnectionString, "#1 The connection string should contain the value for the dsn that is being set by the property");
  158. Assert.AreEqual ("myDsn", builder.Dsn, "#2 The property should return the value that is being set");
  159. builder.Clear ();
  160. builder ["Dsn"] = "myDsn";
  161. Assert.AreEqual ("Dsn=myDsn", builder.ConnectionString, "#3 The connection string should contain the value for the dsn that is being set by assigning the value to key");
  162. Assert.AreEqual ("myDsn", builder.Dsn, "#4 The property should return the value that is being set by assigning the value to the key");
  163. }
  164. [Test]
  165. [ExpectedException (typeof (ArgumentNullException))]
  166. public void DsnNullTest ()
  167. {
  168. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  169. builder.Dsn = null;
  170. }
  171. [Test]
  172. public void IndexerDsnNullTest ()
  173. {
  174. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  175. builder ["Dsn"] = null;
  176. Assert.AreEqual ("", builder.Dsn, "#1 ");
  177. Assert.AreEqual ("", builder ["Dsn"], "#1 ");
  178. }
  179. [Test]
  180. public void ClearTest ()
  181. {
  182. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  183. builder ["Dbq"] = "C:\\Data.xls";
  184. builder ["DriverID"] = "790";
  185. builder ["DefaultDir"] = "C:\\";
  186. builder.Clear ();
  187. Assert.AreEqual ("", builder.ConnectionString, "#1 The connection string should be null");
  188. Assert.AreEqual (false, builder.ContainsKey ("Dbq"), "#2 ");
  189. }
  190. [Test]
  191. public void ContainsKeyTest ()
  192. {
  193. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  194. builder ["SourceType"] = "DBC";
  195. Assert.AreEqual (true, builder.ContainsKey ("SourceType"), "#1 Should be true for explicitly added key");
  196. Assert.AreEqual (true, builder.ContainsKey ("Dsn"), "#2 Should return true for the key that is implicitly added");
  197. Assert.AreEqual (true, builder.ContainsKey ("Driver"), "#3 Should return true for the key that is implicitly added");
  198. Assert.AreEqual (false, builder.ContainsKey ("xyz"), "#4 Should return false for the non-existant key");
  199. builder.Dsn = "myDsn";
  200. Assert.AreEqual (true, builder.ContainsKey ("Dsn"), "#5 Should return true as the key Dsn is now explicitly added");
  201. builder.Driver = "SQL Server";
  202. Assert.AreEqual (true, builder.ContainsKey ("Driver"), "#6 Should return true as the key Driver is now explicitly added");
  203. builder ["Dsn"] = "myDsn";
  204. Assert.AreEqual (true, builder.ContainsKey ("Dsn"), "#5 Should return true as the key Dsn is now explicitly added");
  205. builder ["Driver"] = "SQL Server";
  206. Assert.AreEqual (true, builder.ContainsKey ("Driver"), "#6 Should return true as the key Driver is now explicitly added");
  207. builder ["abc"] = "pqr";
  208. Assert.AreEqual (true, builder.ContainsKey ("ABC"), "#7 Should return true as it should not be case sensitive");
  209. }
  210. [Test]
  211. [ExpectedException (typeof (ArgumentNullException))]
  212. public void ContainsKeyNullArgumentTest ()
  213. {
  214. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  215. builder ["SourceType"] = "DBC";
  216. builder.ContainsKey (null);
  217. }
  218. [Test]
  219. public void RemoveTest ()
  220. {
  221. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  222. builder ["DriverID"] = "790";
  223. builder ["DefaultDir"] = "C:\\";
  224. Assert.AreEqual (false, builder.Remove ("Dsn"), "#1 Should return false for the key that is not explicitly added");
  225. Assert.AreEqual (false, builder.Remove ("Driver"), "#2 Should return false for the key that is not explicitly added");
  226. Assert.AreEqual (true, builder.Remove ("DriverID"), "#3 It should remove the explicitly added key and return true");
  227. Assert.AreEqual (false, builder.Remove ("userid"), "#4 It should return false as there is no such key");
  228. Assert.AreEqual (false, builder.Remove ("DriverID"), "#5 It should not find the key that is previously removed and should return false");
  229. builder.Dsn = "myDsn";
  230. Assert.AreEqual (true, builder.Remove ("Dsn"), "#6 Should return true as the key Dsn is now explicitly added");
  231. builder.Driver = "SQL Server";
  232. Assert.AreEqual (true, builder.Remove ("Driver"), "#7 Should return true as the key Driver is now explicitly added");
  233. builder ["Dsn"] = "myDsn";
  234. Assert.AreEqual (true, builder.Remove ("Dsn"), "#8 Should return true as the key Dsn is now explicitly added");
  235. Assert.AreEqual (false, builder.Remove ("Dsn"), "#9 Should return false as the key is already removed");
  236. builder ["Driver"] = "SQL Server";
  237. Assert.AreEqual (true, builder.Remove ("Driver"), "#10 Should return true as the key Driver is now explicitly added");
  238. Assert.AreEqual (false, builder.Remove ("Driver"), "#11 Should return false as the key is already removed");
  239. }
  240. [Test]
  241. [ExpectedException (typeof (ArgumentNullException))]
  242. public void RemoveNullArgumentTest ()
  243. {
  244. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  245. builder.Remove (null);
  246. }
  247. [Test]
  248. public void TryGetValueTest ()
  249. {
  250. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  251. object value = null;
  252. builder ["DriverID"] = "790";
  253. builder ["Server"] = "C:\\";
  254. Assert.AreEqual (true, builder.TryGetValue ("DriverID", out value), "#1 It should find the key and return true");
  255. Assert.AreEqual (true, builder.TryGetValue ("SERVER", out value), "#2 It should return true as it is not case-sensitive");
  256. }
  257. [Test]
  258. [ExpectedException (typeof (ArgumentNullException))]
  259. public void TryGetValueNullArgumentTest ()
  260. {
  261. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  262. object value = null;
  263. builder.TryGetValue (null, out value);
  264. }
  265. [Test]
  266. public void AddTest ()
  267. {
  268. OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
  269. builder.Add ("driverid", "420");
  270. builder.Add ("DriverID", "840");
  271. Assert.AreEqual ("840", builder ["driverid"], "#1 it should overrite the previous value of driverid as its case-insensitive");
  272. builder.Add ("Driver", "OdbcDriver");
  273. Assert.AreEqual ("OdbcDriver", builder.Driver, "#2 The value of driver should be as per the added value");
  274. builder.Add ("Driver", "{OdbcDriver");
  275. Assert.AreEqual ("{OdbcDriver", builder.Driver, "#3 The value of driver should be as per the added value");
  276. builder.Add ("Dsn", "MyDsn");
  277. Assert.AreEqual ("MyDsn", builder.Dsn, "#4 The value of dsn should be as per the added value");
  278. }
  279. }
  280. }
  281. #endif // NET_2_0 using