DbConnectionStringBuilderTest.cs 10 KB

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