DbConnectionStringBuilderTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // DbConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the
  2. // DbConnectionStringBuilder class
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  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. #if NET_2_0
  30. #region Using directives
  31. using System;
  32. using System.Text;
  33. using System.Data;
  34. using System.Reflection;
  35. using System.Data.Common;
  36. using System.ComponentModel;
  37. using System.Data.SqlClient;
  38. using System.Collections.Specialized;
  39. using System.Collections.Generic;
  40. using NUnit.Framework;
  41. #endregion
  42. namespace MonoTests.System.Data.Common
  43. {
  44. [TestFixture]
  45. public class DbConnectionStringBuilderTest
  46. {
  47. private DbConnectionStringBuilder builder = null;
  48. private const string SERVER = "SERVER";
  49. private const string SERVER_VALUE = "localhost";
  50. [SetUp]
  51. public void SetUp ()
  52. {
  53. builder = new DbConnectionStringBuilder ();
  54. }
  55. [Test]
  56. public void AddTest ()
  57. {
  58. builder.Add (SERVER, SERVER_VALUE);
  59. Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
  60. "Adding to connection String failed!");
  61. }
  62. [Test]
  63. public void ClearTest ()
  64. {
  65. builder.Add (SERVER, SERVER_VALUE);
  66. builder.Clear ();
  67. Assert.AreEqual ("", builder.ConnectionString,
  68. "Clearing connection String failed!");
  69. }
  70. [Test]
  71. public void AddDuplicateTest ()
  72. {
  73. builder.Add (SERVER, SERVER_VALUE);
  74. builder.Add (SERVER, SERVER_VALUE);
  75. // should allow duplicate addition. rather, it should re-assign
  76. Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
  77. "Duplicates addition does not change the value!");
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void InvalidKeyTest ()
  82. {
  83. builder.Add (SERVER, SERVER_VALUE);
  84. string value = builder ["###"].ToString (); // some invalid key values
  85. Assert.Fail ("Should have thrown exception!");
  86. }
  87. [Test]
  88. public void RemoveTest ()
  89. {
  90. builder.Add (SERVER, SERVER_VALUE);
  91. builder.Remove (SERVER);
  92. Assert.AreEqual ("", builder.ConnectionString, "Remove does not work!");
  93. }
  94. [Test]
  95. public void ContainsKeyTest ()
  96. {
  97. builder.Add (SERVER, SERVER_VALUE);
  98. bool value = builder.ContainsKey (SERVER);
  99. Assert.IsTrue (value, "Contains does not work!");
  100. }
  101. [Test]
  102. public void EquivalentToTest ()
  103. {
  104. builder.Add (SERVER, SERVER_VALUE);
  105. DbConnectionStringBuilder sb2 = new DbConnectionStringBuilder ();
  106. sb2.Add (SERVER, SERVER_VALUE);
  107. bool value = builder.EquivalentTo (sb2);
  108. Assert.IsTrue (value, "builder comparision does not work!");
  109. // negative tests
  110. sb2.Add (SERVER + "1", SERVER_VALUE);
  111. value = builder.EquivalentTo (sb2);
  112. Assert.IsFalse (value, "builder comparision does not work for not equivalent strings!");
  113. }
  114. [Test]
  115. public void AppendKeyValuePairTest ()
  116. {
  117. StringBuilder sb = new StringBuilder ();
  118. DbConnectionStringBuilder.AppendKeyValuePair (sb, SERVER, SERVER_VALUE);
  119. Assert.AreEqual (SERVER + "=" + SERVER_VALUE, sb.ToString (),
  120. "adding key value pair to existing string builder fails!");
  121. }
  122. [Test]
  123. public void ToStringTest ()
  124. {
  125. builder.Add (SERVER, SERVER_VALUE);
  126. string str = builder.ToString ();
  127. string value = builder.ConnectionString;
  128. Assert.AreEqual (value, str,
  129. "ToString shoud return ConnectionString!");
  130. }
  131. [Test]
  132. public void ItemTest ()
  133. {
  134. builder.Add (SERVER, SERVER_VALUE);
  135. string value = (string) builder [SERVER];
  136. Assert.AreEqual (SERVER_VALUE, value,
  137. "Item indexor does not retrun correct value!");
  138. }
  139. [Test, Ignore ("FIXME : commented for a missing feature in gmcs, (CopyTo)")]
  140. public void IDictionaryCopyToTest ()
  141. {
  142. KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [2];
  143. builder.Add (SERVER, SERVER_VALUE);
  144. builder.Add (SERVER + "1", SERVER_VALUE + "1");
  145. IDictionary<string, object> s = builder;
  146. //FIXME : s.CopyTo (dict, 0);
  147. Assert.AreEqual (SERVER, dict [0].Key, "not equal");
  148. Assert.AreEqual (SERVER_VALUE, dict [0].Value, "not equal");
  149. Assert.AreEqual (SERVER + "1", dict [1].Key, "not equal");
  150. Assert.AreEqual (SERVER_VALUE + "1", dict [1].Value, "not equal");
  151. }
  152. [Test, Ignore ("FIXME: commented for a missing feature in gmcs (CopyTo)")]
  153. [ExpectedException (typeof (ArgumentException))]
  154. public void NegIDictionaryCopyToTest ()
  155. {
  156. KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [1];
  157. builder.Add (SERVER, SERVER_VALUE);
  158. builder.Add (SERVER + "1", SERVER_VALUE + "1");
  159. IDictionary<string, object> s = builder;
  160. //FIXME : s.CopyTo (dict, 0);
  161. Assert.Fail ("Exception Destination Array not enough is not thrown!");
  162. }
  163. [Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+ " non generic type")]
  164. public void ICollectionCopyToTest ()
  165. {
  166. /*
  167. KeyValuePair <string, object> [] arr = new KeyValuePair <string, object> [2];
  168. builder.Add (SERVER, SERVER_VALUE);
  169. builder.Add (SERVER + "1", SERVER_VALUE + "1");
  170. System.Collections.ICollection s = builder;
  171. s.CopyTo ((Array) arr, 0);
  172. Assert.AreEqual (SERVER, arr [0].Key, "not equal");
  173. Assert.AreEqual (SERVER_VALUE, arr [0].Value, "not equal");
  174. Assert.AreEqual (SERVER + "1", arr [1].Key, "not equal");
  175. Assert.AreEqual (SERVER_VALUE + "1", arr [1].Value, "not equal");
  176. */
  177. }
  178. [Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+ " non generic type")]
  179. [ExpectedException (typeof (ArgumentException))]
  180. public void NegICollectionCopyToTest ()
  181. {
  182. /*
  183. string [] arr = new string [2];
  184. builder.Add (SERVER, SERVER_VALUE);
  185. builder.Add (SERVER + "1", SERVER_VALUE + "1");
  186. System.Collections.ICollection s = builder;
  187. s.CopyTo ((Array) arr, 0);
  188. */
  189. }
  190. [Test]
  191. public void TryGetValueTest ()
  192. {
  193. builder.Add (SERVER, SERVER_VALUE);
  194. object value = "";
  195. bool result = builder.TryGetValue (SERVER, out value);
  196. Assert.AreEqual (SERVER_VALUE, (string) value,
  197. "TryGetValue does not return correct value in out parameter!");
  198. Assert.IsTrue (result, "TryGetValue does not return true for existant key!");
  199. result = builder.TryGetValue ("@@@@", out value);
  200. Assert.IsFalse (result, "TryGetValue does not return false for non-existant key!");
  201. Assert.IsNull ((string) value,
  202. "TryGetValue does not return correct value in out parameter for non existant key!");
  203. }
  204. [Test]
  205. public void ICTD_GetClassNameTest ()
  206. {
  207. ICustomTypeDescriptor ictd = (ICustomTypeDescriptor) builder;
  208. string className = ictd.GetClassName ();
  209. Assert.AreEqual (builder.GetType ().ToString (), className, "Should return class name!");
  210. AttributeCollection collection = ictd.GetAttributes ();
  211. Assert.AreEqual (2, collection.Count);
  212. object [] attr = builder.GetType ().GetCustomAttributes (typeof (DefaultMemberAttribute), false);
  213. if (attr.Length > 0) {
  214. DefaultMemberAttribute defAtt = (DefaultMemberAttribute) attr [0];
  215. Assert.AreEqual ("Item", defAtt.MemberName, "default memeber attribute is not set!");
  216. } else
  217. Assert.Fail ("DbConnectionStringBuilder class does not implement DefaultMember attribute");
  218. string compName = ictd.GetComponentName ();
  219. Assert.IsNull (compName, "");
  220. TypeConverter converter = ictd.GetConverter ();
  221. Assert.AreEqual (typeof (CollectionConverter), converter.GetType (), "");
  222. EventDescriptor evtDesc = ictd.GetDefaultEvent ();
  223. Assert.IsNull (evtDesc, "");
  224. PropertyDescriptor property = ictd.GetDefaultProperty ();
  225. Assert.IsNull (property, "");
  226. }
  227. }
  228. }
  229. #endif // NET_2_0