1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
// DbConnectionStringBuilderTest.cs - NUnit Test Cases for Testing the
// DbConnectionStringBuilder class
//
// Author:
// Sureshkumar T (tsureshkumar@novell.com)
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
#region Using directives
using System;
using System.Text;
using System.Data;
using System.Reflection;
using System.Data.Common;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Collections.Generic;
using NUnit.Framework;
#endregion
namespace MonoTests.System.Data.Common
{
[TestFixture]
public class DbConnectionStringBuilderTest
{
private DbConnectionStringBuilder builder = null;
private const string SERVER = "SERVER";
private const string SERVER_VALUE = "localhost";
[SetUp]
public void SetUp ()
{
builder = new DbConnectionStringBuilder ();
}
[Test]
public void AddTest ()
{
builder.Add (SERVER, SERVER_VALUE);
Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
"Adding to connection String failed!");
}
[Test]
public void ClearTest ()
{
builder.Add (SERVER, SERVER_VALUE);
builder.Clear ();
Assert.AreEqual ("", builder.ConnectionString,
"Clearing connection String failed!");
}
[Test]
public void AddDuplicateTest ()
{
builder.Add (SERVER, SERVER_VALUE);
builder.Add (SERVER, SERVER_VALUE);
// should allow duplicate addition. rather, it should re-assign
Assert.AreEqual (SERVER + "=" + SERVER_VALUE, builder.ConnectionString,
"Duplicates addition does not change the value!");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void InvalidKeyTest ()
{
builder.Add (SERVER, SERVER_VALUE);
string value = builder ["###"].ToString (); // some invalid key values
Assert.Fail ("Should have thrown exception!");
}
[Test]
public void RemoveTest ()
{
builder.Add (SERVER, SERVER_VALUE);
builder.Remove (SERVER);
Assert.AreEqual ("", builder.ConnectionString, "Remove does not work!");
}
[Test]
public void ContainsKeyTest ()
{
builder.Add (SERVER, SERVER_VALUE);
bool value = builder.ContainsKey (SERVER);
Assert.IsTrue (value, "Contains does not work!");
}
[Test]
public void EquivalentToTest ()
{
builder.Add (SERVER, SERVER_VALUE);
DbConnectionStringBuilder sb2 = new DbConnectionStringBuilder ();
sb2.Add (SERVER, SERVER_VALUE);
bool value = builder.EquivalentTo (sb2);
Assert.IsTrue (value, "builder comparision does not work!");
// negative tests
sb2.Add (SERVER + "1", SERVER_VALUE);
value = builder.EquivalentTo (sb2);
Assert.IsFalse (value, "builder comparision does not work for not equivalent strings!");
}
[Test]
public void AppendKeyValuePairTest ()
{
StringBuilder sb = new StringBuilder ();
DbConnectionStringBuilder.AppendKeyValuePair (sb, SERVER, SERVER_VALUE);
Assert.AreEqual (SERVER + "=" + SERVER_VALUE, sb.ToString (),
"adding key value pair to existing string builder fails!");
}
[Test]
public void ToStringTest ()
{
builder.Add (SERVER, SERVER_VALUE);
string str = builder.ToString ();
string value = builder.ConnectionString;
Assert.AreEqual (value, str,
"ToString shoud return ConnectionString!");
}
[Test]
public void ItemTest ()
{
builder.Add (SERVER, SERVER_VALUE);
string value = (string) builder [SERVER];
Assert.AreEqual (SERVER_VALUE, value,
"Item indexor does not retrun correct value!");
}
[Test, Ignore ("FIXME : commented for a missing feature in gmcs, (CopyTo)")]
public void IDictionaryCopyToTest ()
{
KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [2];
builder.Add (SERVER, SERVER_VALUE);
builder.Add (SERVER + "1", SERVER_VALUE + "1");
IDictionary<string, object> s = builder;
//FIXME : s.CopyTo (dict, 0);
Assert.AreEqual (SERVER, dict [0].Key, "not equal");
Assert.AreEqual (SERVER_VALUE, dict [0].Value, "not equal");
Assert.AreEqual (SERVER + "1", dict [1].Key, "not equal");
Assert.AreEqual (SERVER_VALUE + "1", dict [1].Value, "not equal");
}
[Test, Ignore ("FIXME: commented for a missing feature in gmcs (CopyTo)")]
[ExpectedException (typeof (ArgumentException))]
public void NegIDictionaryCopyToTest ()
{
KeyValuePair<string, object> [] dict = new KeyValuePair<string, object> [1];
builder.Add (SERVER, SERVER_VALUE);
builder.Add (SERVER + "1", SERVER_VALUE + "1");
IDictionary<string, object> s = builder;
//FIXME : s.CopyTo (dict, 0);
Assert.Fail ("Exception Destination Array not enough is not thrown!");
}
[Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+ " non generic type")]
public void ICollectionCopyToTest ()
{
/*
KeyValuePair <string, object> [] arr = new KeyValuePair <string, object> [2];
builder.Add (SERVER, SERVER_VALUE);
builder.Add (SERVER + "1", SERVER_VALUE + "1");
System.Collections.ICollection s = builder;
s.CopyTo ((Array) arr, 0);
Assert.AreEqual (SERVER, arr [0].Key, "not equal");
Assert.AreEqual (SERVER_VALUE, arr [0].Value, "not equal");
Assert.AreEqual (SERVER + "1", arr [1].Key, "not equal");
Assert.AreEqual (SERVER_VALUE + "1", arr [1].Value, "not equal");
*/
}
[Test, Ignore ("FIXME : currently mono is not supporting casting from generic type to"+ " non generic type")]
[ExpectedException (typeof (ArgumentException))]
public void NegICollectionCopyToTest ()
{
/*
string [] arr = new string [2];
builder.Add (SERVER, SERVER_VALUE);
builder.Add (SERVER + "1", SERVER_VALUE + "1");
System.Collections.ICollection s = builder;
s.CopyTo ((Array) arr, 0);
*/
}
[Test]
public void TryGetValueTest ()
{
builder.Add (SERVER, SERVER_VALUE);
object value = "";
bool result = builder.TryGetValue (SERVER, out value);
Assert.AreEqual (SERVER_VALUE, (string) value,
"TryGetValue does not return correct value in out parameter!");
Assert.IsTrue (result, "TryGetValue does not return true for existant key!");
result = builder.TryGetValue ("@@@@", out value);
Assert.IsFalse (result, "TryGetValue does not return false for non-existant key!");
Assert.IsNull ((string) value,
"TryGetValue does not return correct value in out parameter for non existant key!");
}
[Test]
public void ICTD_GetClassNameTest ()
{
ICustomTypeDescriptor ictd = (ICustomTypeDescriptor) builder;
string className = ictd.GetClassName ();
Assert.AreEqual (builder.GetType ().ToString (), className, "Should return class name!");
AttributeCollection collection = ictd.GetAttributes ();
Assert.AreEqual (2, collection.Count);
object [] attr = builder.GetType ().GetCustomAttributes (typeof (DefaultMemberAttribute), false);
if (attr.Length > 0) {
DefaultMemberAttribute defAtt = (DefaultMemberAttribute) attr [0];
Assert.AreEqual ("Item", defAtt.MemberName, "default memeber attribute is not set!");
} else
Assert.Fail ("DbConnectionStringBuilder class does not implement DefaultMember attribute");
string compName = ictd.GetComponentName ();
Assert.IsNull (compName, "");
TypeConverter converter = ictd.GetConverter ();
Assert.AreEqual (typeof (CollectionConverter), converter.GetType (), "");
EventDescriptor evtDesc = ictd.GetDefaultEvent ();
Assert.IsNull (evtDesc, "");
PropertyDescriptor property = ictd.GetDefaultProperty ();
Assert.IsNull (property, "");
}
}
}
#endif // NET_2_0
|