1 // DbConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the
2 // DbConnectionStringBuilder class
5 // Sureshkumar T (tsureshkumar@novell.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 #region Using directives
38 using System.Reflection;
39 using System.Data.Common;
40 using System.ComponentModel;
41 using System.Data.SqlClient;
42 using System.Collections.Specialized;
43 using System.Collections.Generic;
45 using NUnit.Framework;
49 namespace MonoTests.System.Data.Common
53 public class DbConnectionStringBuilderTest
55 private DbConnectionStringBuilder builder = null;
56 private const string SERVER = "SERVER";
57 private const string SERVER_VALUE = "localhost";
62 builder = new DbConnectionStringBuilder ();
66 public void AddTest ()
68 builder.Add (SERVER, SERVER_VALUE);
69 Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
70 "Adding to connection String failed!");
74 public void ClearTest ()
76 builder.Add (SERVER, SERVER_VALUE);
78 Assert.AreEqual ("", builder.ConnectionString,
79 "Clearing connection String failed!");
83 public void AddDuplicateTest ()
85 builder.Add (SERVER, SERVER_VALUE);
86 builder.Add (SERVER, SERVER_VALUE);
87 // should allow duplicate addition. rather, it should re-assign
88 Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
89 "Duplicates addition does not change the value!");
93 [ExpectedException (typeof (ArgumentException))]
94 public void InvalidKeyTest ()
96 builder.Add (SERVER, SERVER_VALUE);
97 string value = builder ["###"].ToString (); // some invalid key values
98 Assert.Fail ("Should have thrown exception!");
102 public void RemoveTest ()
104 builder.Add (SERVER, SERVER_VALUE);
105 builder.Remove (SERVER);
106 Assert.AreEqual ("", builder.ConnectionString, "Remove does not work!");
110 public void ContainsKeyTest ()
112 builder.Add (SERVER, SERVER_VALUE);
113 bool value = builder.ContainsKey (SERVER);
114 Assert.IsTrue (value, "Contains does not work!");
118 public void EquivalentToTest ()
120 builder.Add (SERVER, SERVER_VALUE);
121 DbConnectionStringBuilder sb2 = new DbConnectionStringBuilder ();
122 sb2.Add (SERVER, SERVER_VALUE);
123 bool value = builder.EquivalentTo (sb2);
124 Assert.IsTrue (value, "builder comparision does not work!");
127 sb2.Add (SERVER + "1", SERVER_VALUE);
128 value = builder.EquivalentTo (sb2);
129 Assert.IsFalse (value, "builder comparision does not work for not equivalent strings!");
133 public void AppendKeyValuePairTest ()
135 StringBuilder sb = new StringBuilder ();
136 DbConnectionStringBuilder.AppendKeyValuePair (sb, SERVER, SERVER_VALUE);
137 Assert.AreEqual (SERVER + "=" + SERVER_VALUE, sb.ToString (),
138 "adding key value pair to existing string builder fails!");
142 public void ToStringTest ()
144 builder.Add (SERVER, SERVER_VALUE);
145 string str = builder.ToString ();
146 string value = builder.ConnectionString;
147 Assert.AreEqual (value, str,
148 "ToString shoud return ConnectionString!");
152 public void ItemTest ()
154 builder.Add (SERVER, SERVER_VALUE);
155 string value = (string) builder [SERVER];
156 Assert.AreEqual (SERVER_VALUE, value,
157 "Item indexor does not retrun correct value!");
160 [Test, Ignore ("FIXME : commented for a missing feature in gmcs, (CopyTo)")]
161 public void IDictionaryCopyToTest ()
163 KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [2];
164 builder.Add (SERVER, SERVER_VALUE);
165 builder.Add (SERVER + "1", SERVER_VALUE + "1");
166 IDictionary<string, object> s = builder;
167 //FIXME : s.CopyTo (dict, 0);
168 Assert.AreEqual (SERVER, dict [0].Key, "not equal");
169 Assert.AreEqual (SERVER_VALUE, dict [0].Value, "not equal");
170 Assert.AreEqual (SERVER + "1", dict [1].Key, "not equal");
171 Assert.AreEqual (SERVER_VALUE + "1", dict [1].Value, "not equal");
174 [Test, Ignore ("FIXME: commented for a missing feature in gmcs (CopyTo)")]
175 [ExpectedException (typeof (ArgumentException))]
176 public void NegIDictionaryCopyToTest ()
178 KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [1];
179 builder.Add (SERVER, SERVER_VALUE);
180 builder.Add (SERVER + "1", SERVER_VALUE + "1");
181 IDictionary<string, object> s = builder;
182 //FIXME : s.CopyTo (dict, 0);
183 Assert.Fail ("Exception Destination Array not enough is not thrown!");
186 [Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+
187 " non generic type")]
188 public void ICollectionCopyToTest ()
191 KeyValuePair <string, object> [] arr = new KeyValuePair <string, object> [2];
192 builder.Add (SERVER, SERVER_VALUE);
193 builder.Add (SERVER + "1", SERVER_VALUE + "1");
194 System.Collections.ICollection s = builder;
195 s.CopyTo ((Array) arr, 0);
196 Assert.AreEqual (SERVER, arr [0].Key, "not equal");
197 Assert.AreEqual (SERVER_VALUE, arr [0].Value, "not equal");
198 Assert.AreEqual (SERVER + "1", arr [1].Key, "not equal");
199 Assert.AreEqual (SERVER_VALUE + "1", arr [1].Value, "not equal");
203 [ExpectedException (typeof (ArgumentException))]
204 public void NegICollectionCopyToTest ()
206 string [] arr = new string [2];
207 builder.Add (SERVER, SERVER_VALUE);
208 builder.Add (SERVER + "1", SERVER_VALUE + "1");
209 System.Collections.ICollection s = builder;
210 s.CopyTo ((Array) arr, 0);
214 public void TryGetValueTest ()
216 builder.Add (SERVER, SERVER_VALUE);
218 bool result = builder.TryGetValue (SERVER, out value);
219 Assert.AreEqual (SERVER_VALUE, (string) value,
220 "TryGetValue does not return correct value in out parameter!");
221 Assert.IsTrue (result, "TryGetValue does not return true for existant key!");
223 result = builder.TryGetValue ("@@@@", out value);
224 Assert.IsFalse (result, "TryGetValue does not return false for non-existant key!");
225 Assert.IsNull ((string) value,
226 "TryGetValue does not return correct value in out parameter for non existant key!");
230 public void ICTD_GetClassNameTest ()
232 ICustomTypeDescriptor ictd = (ICustomTypeDescriptor) builder;
233 string className = ictd.GetClassName ();
234 Assert.AreEqual (builder.GetType ().ToString (), className, "Should return class name!");
236 AttributeCollection collection = ictd.GetAttributes ();
237 Assert.AreEqual (2, collection.Count);
238 object [] attr = builder.GetType ().GetCustomAttributes (typeof (DefaultMemberAttribute), false);
239 if (attr.Length > 0) {
240 DefaultMemberAttribute defAtt = (DefaultMemberAttribute) attr [0];
241 Assert.AreEqual ("Item", defAtt.MemberName, "default memeber attribute is not set!");
243 Assert.Fail ("DbConnectionStringBuilder class does not implement DefaultMember attribute");
245 string compName = ictd.GetComponentName ();
246 Assert.IsNull (compName, "");
248 TypeConverter converter = ictd.GetConverter ();
249 Assert.AreEqual (typeof (CollectionConverter), converter.GetType (), "");
251 EventDescriptor evtDesc = ictd.GetDefaultEvent ();
252 Assert.IsNull (evtDesc, "");
254 PropertyDescriptor property = ictd.GetDefaultProperty ();
255 Assert.IsNull (property, "");