2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbConnectionStringBuilderTest.cs
1 // DbConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the 
2 // DbConnectionStringBuilder class
3 //
4 // Author: 
5 //      Sureshkumar T (tsureshkumar@novell.com)
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
30 #if NET_2_0
31
32 #region Using directives
33
34 using System;
35 using System.Text;
36
37 using System.Data;
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;
44
45 using NUnit.Framework;
46
47 #endregion
48
49 namespace MonoTests.System.Data.Common
50 {
51
52         [TestFixture]
53         public class DbConnectionStringBuilderTest
54         {
55                 private DbConnectionStringBuilder builder = null;
56                 private const string SERVER = "SERVER";
57                 private const string SERVER_VALUE = "localhost";
58
59                 [SetUp]
60                 public void SetUp ()
61                 {
62                         builder = new DbConnectionStringBuilder ();
63                 }
64
65                 [Test]
66                 public void AddTest ()
67                 {
68                         builder.Add (SERVER, SERVER_VALUE);
69                         Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
70                                          "Adding to connection String failed!");
71                 }
72
73                 [Test]
74                 public void ClearTest ()
75                 {
76                         builder.Add (SERVER, SERVER_VALUE);
77                         builder.Clear ();
78                         Assert.AreEqual ("", builder.ConnectionString,
79                                          "Clearing connection String failed!");
80                 }
81
82                 [Test]
83                 public void AddDuplicateTest ()
84                 {
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!");
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void InvalidKeyTest ()
95                 {
96                         builder.Add (SERVER, SERVER_VALUE);
97                         string value = builder ["###"].ToString (); // some invalid key values
98                         Assert.Fail ("Should have thrown exception!");
99                 }
100
101                 [Test]
102                 public void RemoveTest ()
103                 {
104                         builder.Add (SERVER, SERVER_VALUE);
105                         builder.Remove (SERVER);
106                         Assert.AreEqual ("", builder.ConnectionString, "Remove does not work!");
107                 }
108
109                 [Test]
110                 public void ContainsKeyTest ()
111                 {
112                         builder.Add (SERVER, SERVER_VALUE);
113                         bool value = builder.ContainsKey (SERVER);
114                         Assert.IsTrue (value, "Contains does not work!");
115                 }
116
117                 [Test]
118                 public void EquivalentToTest ()
119                 {
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!");
125
126                         // negative tests
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!");
130                 }
131
132                 [Test]
133                 public void AppendKeyValuePairTest ()
134                 {
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!");
139                 }
140
141                 [Test]
142                 public void ToStringTest ()
143                 {
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!");
149                 }
150
151                 [Test]
152                 public void ItemTest ()
153                 {
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!");
158                 }
159
160                 [Test, Ignore ("FIXME : commented for a missing feature in gmcs, (CopyTo)")]
161                 public void IDictionaryCopyToTest ()
162                 {
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");
172                 }
173
174                 [Test, Ignore ("FIXME: commented for a missing feature in gmcs (CopyTo)")]
175                 [ExpectedException (typeof (ArgumentException))]
176                 public void NegIDictionaryCopyToTest ()
177                 {
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!");
184                 }
185
186                 [Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+
187                  " non generic type")]
188                 public void ICollectionCopyToTest ()
189                 {
190
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");
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ArgumentException))]
204                 public void NegICollectionCopyToTest ()
205                 {
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);
211                 }
212
213                 [Test]
214                 public void TryGetValueTest ()
215                 {
216                         builder.Add (SERVER, SERVER_VALUE);
217                         object 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!");
222
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!");
227                 }
228
229                 [Test]
230                 public void ICTD_GetClassNameTest ()
231                 {
232                         ICustomTypeDescriptor ictd = (ICustomTypeDescriptor) builder;
233                         string className = ictd.GetClassName ();
234                         Assert.AreEqual (builder.GetType ().ToString (), className, "Should return class name!");
235
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!");
242                         } else
243                                 Assert.Fail ("DbConnectionStringBuilder class does not implement DefaultMember attribute");
244
245                         string compName = ictd.GetComponentName ();
246                         Assert.IsNull (compName, "");
247
248                         TypeConverter converter = ictd.GetConverter ();
249                         Assert.AreEqual (typeof (CollectionConverter), converter.GetType (), "");
250
251                         EventDescriptor evtDesc = ictd.GetDefaultEvent ();
252                         Assert.IsNull (evtDesc, "");
253
254                         PropertyDescriptor property = ictd.GetDefaultProperty ();
255                         Assert.IsNull (property, "");
256
257                 }
258         }
259 }
260
261 #endif // NET_2_0