[system] Part of fix for bug #683339. NameValueCollection.Add (NameValueCollection...
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / NameValueCollectionTest.cs
1 // created on 7/21/2001 at 2:36 PM
2 //
3 // Authors:
4 //      Martin Willemoes Hansen (mwh@sysrq.dk)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Martin Willemoes Hansen
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Collections.Specialized;
15 using System.Text;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Collections.Specialized {
20
21         [TestFixture]
22         public class NameValueCollectionTest {
23
24                 [Test]
25                 public void GetValues ()
26                 {
27                         NameValueCollection col = new NameValueCollection ();
28                         col.Add ("foo1", "bar1");
29                         Assert.AreEqual (null, col.GetValues (null), "#1");
30                         Assert.AreEqual (null, col.GetValues (""), "#2");
31                         Assert.AreEqual (null, col.GetValues ("NotExistent"), "#3");
32                         Assert.AreEqual (1, col.GetValues (0).Length, "#4");
33                 }
34
35                 [Test]
36                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
37                 public void GetValues_OutOfRange ()
38                 {
39                         NameValueCollection c = new NameValueCollection ();
40                         c.Add ("foo1", "bar1");
41                         Assert.AreEqual (null, c.GetValues (1), "#5");
42                 }
43
44                 [Test]
45                 public void Get ()
46                 {
47                         NameValueCollection col = new NameValueCollection (5);
48                         col.Add ("foo1", "bar1");
49                         Assert.AreEqual (null, col.Get (null), "#1");
50                         Assert.AreEqual (null, col.Get (""), "#2");
51                         Assert.AreEqual (null, col.Get ("NotExistent"), "#3");
52                         Assert.AreEqual ("bar1", col.Get ("foo1"), "#4");
53                         Assert.AreEqual ("bar1", col.Get (0), "#5");
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
58                 public void Get_OutOfRange ()
59                 {
60                         NameValueCollection c = new NameValueCollection ();
61                         c.Add ("foo1", "bar1");
62                         Assert.AreEqual (null, c.Get (1), "#6");
63                 }
64
65                 [Test]
66                 public void GetKey ()
67                 {
68                         NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
69                         c.Add ("foo1", "bar1");
70                         Assert.AreEqual ("foo1", c.GetKey (0), "#1");
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
75                 public void GetKey_OutOfRange ()
76                 {
77                         NameValueCollection c = new NameValueCollection ();
78                         c.Add ("foo1", "bar1");
79                         Assert.AreEqual (null, c.GetKey (1), "#2");
80                 }
81
82                 [Test]
83                 public void HasKeys ()
84                 {
85                         NameValueCollection c = new NameValueCollection (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
86                         Assert.IsTrue (!c.HasKeys (), "#1");
87                         c.Add ("foo1", "bar1");
88                         Assert.IsTrue (c.HasKeys (), "#2");
89                 }
90
91                 [Test]
92                 public void Clear ()
93                 {
94                         NameValueCollection c = new NameValueCollection ();
95                         Assert.AreEqual (0, c.Count, "#1");
96                         c.Add ("foo1", "bar1");
97                         Assert.AreEqual (1, c.Count, "#2");
98                         c.Clear ();
99                         Assert.AreEqual (0, c.Count, "#3");
100                 }
101
102                 [Test]
103                 public void Add ()
104                 {
105                         NameValueCollection c = new NameValueCollection ();
106                         c.Add ("mono", "mono");
107                         c.Add ("!mono", null);
108                         c.Add (null, "mono!");
109                         Assert.AreEqual (3, c.Count, "Count");
110                         Assert.AreEqual ("mono", c ["mono"], "mono");
111                         Assert.IsNull (c ["!mono"], "!mono");
112                         Assert.AreEqual ("mono!", c [null], "mono!");
113                 }
114
115                 [Test]
116                 public void Add_Calls ()
117                 {
118                         var nvc1 = new MyNVC ();
119                         var nvc2 = new MyNVC ();
120                         nvc1.Add ("one", "1");
121                         nvc1.Add ("one", "one");
122                         nvc1.Add ("two", null);
123                         nvc2.Add (nvc1);
124
125                         string[] values;
126                         Assert.AreEqual (8, nvc1.Log.Count, "#A1-1");
127                         Assert.AreEqual ("Add (string, string)", nvc1.Log [0], "#A1-2");
128                         Assert.AreEqual ("Add (string, string)", nvc1.Log [1], "#A1-3");
129                         Assert.AreEqual ("Add (string, string)", nvc1.Log [2], "#A1-4");
130                         Assert.AreEqual ("get_Count", nvc1.Log [3], "#A1-5");
131                         Assert.AreEqual ("GetKey (int)", nvc1.Log [4], "#A1-6");
132                         Assert.AreEqual ("GetValues (int)", nvc1.Log [5], "#A1-7");
133                         Assert.AreEqual ("GetKey (int)", nvc1.Log [6], "#A1-8");
134                         Assert.AreEqual ("GetValues (int)", nvc1.Log [7], "#A1-9");
135
136                         Assert.AreEqual (2, nvc1.Count, "#A2-1");
137                         values = nvc1.GetValues (0);
138                         Assert.AreEqual ("one", nvc1.GetKey (0), "#A2-2");
139                         Assert.AreEqual ("1", values [0], "#A2-3");
140                         Assert.AreEqual ("one", values [1], "#A2-4");
141                         values = nvc1.GetValues (1);
142                         Assert.AreEqual ("two", nvc1.GetKey (1), "#A2-5");
143                         Assert.IsTrue (values == null, "#A2-6");
144
145                         Assert.AreEqual (3, nvc2.Log.Count, "#B1-1");
146                         Assert.AreEqual ("Add (string, string)", nvc2.Log [0], "#B1-2");
147                         Assert.AreEqual ("Add (string, string)", nvc2.Log [1], "#B1-3");
148                         Assert.AreEqual ("Add (string, string)", nvc2.Log [2], "#B1-4");
149
150                         Assert.AreEqual (2, nvc2.Count, "#B2-1");
151                         values = nvc2.GetValues (0);
152                         Assert.AreEqual ("one", nvc2.GetKey (0), "#B2-2");
153                         Assert.AreEqual ("1", values [0], "#B2-3");
154                         Assert.AreEqual ("one", values [1], "#B2-4");
155                         values = nvc2.GetValues (1);
156                         Assert.AreEqual ("two", nvc2.GetKey (1), "#B2-5");
157                         Assert.IsTrue (values == null, "#B2-6");
158                 }
159                 
160                 [Test]
161                 public void Add_Multiples ()
162                 {
163                         NameValueCollection c = new NameValueCollection ();
164                         c.Add ("mono", "mono");
165                         c.Add ("mono", "mono");
166                         c.Add ("mono", "mono");
167                         Assert.AreEqual (1, c.Count, "Count");
168                         Assert.AreEqual ("mono,mono,mono", c ["mono"], "mono");
169                 }
170
171                 [Test]
172                 public void Add_Multiples_Null ()
173                 {
174                         NameValueCollection c = new NameValueCollection ();
175                         c.Add ("mono", "mono");
176                         c.Add ("mono", null);
177                         c.Add ("mono", "mono");
178                         Assert.AreEqual (1, c.Count, "Count");
179                         Assert.AreEqual ("mono,mono", c ["mono"], "mono");
180                 }
181
182                 [Test]
183                 public void Add_NVC ()
184                 {
185                         NameValueCollection c1 = new NameValueCollection ();
186                         NameValueCollection c2 = new NameValueCollection (c1);
187
188                         c2.Add (c1);
189                         Assert.AreEqual (0, c1.Count, "c1.Count");
190                         Assert.AreEqual (0, c2.Count, "c2.Count");
191
192                         c1.Add ("foo", "bar");
193                         c2.Add ("bar", "foo");
194
195                         Assert.AreEqual (1, c1.Count, "c1.Count");
196                         Assert.AreEqual (1, c2.Count, "c2.Count");
197
198                         c2.Add (c1);
199                         Assert.AreEqual (1, c1.Count, "c1.Count");
200                         Assert.AreEqual (2, c2.Count, "c2.Count");
201                 }
202
203                 [Test]
204 #if NET_2_0
205                 [ExpectedException (typeof (ArgumentNullException))]
206 #else
207                 [ExpectedException (typeof (NullReferenceException))]
208 #endif
209                 public void Add_NVC_Null ()
210                 {
211                         new NameValueCollection ().Add (null);
212                 }
213
214                 [Test]
215                 public void Add_NVC_Null2 ()
216                 {
217                         NameValueCollection a = new NameValueCollection ();
218                         NameValueCollection b = new NameValueCollection ();
219
220                         b.Add ("Test", null);
221                         a.Add (b);
222                         Assert.AreEqual (1, a.Count, "Count");
223                 }
224
225                 [Test]
226                 public void Set_New ()
227                 {
228                         NameValueCollection c = new NameValueCollection ();
229                         c.Set ("mono", "mono");
230                         c.Set ("!mono", null);
231                         c.Set (null, "mono!");
232                         Assert.AreEqual (3, c.Count, "Count");
233                         Assert.AreEqual ("mono", c ["mono"], "mono");
234                         Assert.IsNull (c ["!mono"], "!mono");
235                         Assert.AreEqual ("mono!", c [null], "mono!");
236                 }
237
238                 [Test]
239                 public void Set_Replace ()
240                 {
241                         NameValueCollection c = new NameValueCollection ();
242                         c.Add ("mono", "mono");
243                         c.Add ("!mono", "!mono");
244                         c.Add ("mono!", "mono!");
245                         Assert.AreEqual (3, c.Count, "Count");
246                         Assert.AreEqual ("mono", c ["mono"], "mono");
247                         Assert.AreEqual ("!mono", c ["!mono"], "!mono");
248                         Assert.AreEqual ("mono!", c ["mono!"], "mono!");
249
250                         c.Set ("mono", "nomo");
251                         c.Set ("!mono", null);
252                         c.Set (null, "mono!");
253                         Assert.AreEqual (4, c.Count, "Count"); // mono! isn't removed
254                         Assert.AreEqual ("nomo", c ["mono"], "mono");
255                         Assert.IsNull (c ["!mono"], "!mono");
256                         Assert.AreEqual ("mono!", c ["mono!"], "mono!1");
257                         Assert.AreEqual ("mono!", c [null], "mono!2");
258                 }
259
260                 [Test]
261                 public void CaseInsensitive () 
262                 {
263                         // default constructor is case insensitive
264                         NameValueCollection c = new NameValueCollection ();
265                         c.Add ("mono", "mono");
266                         c.Add ("MoNo", "MoNo");
267                         c.Add ("mOnO", "mOnO");
268                         c.Add ("MONO", "MONO");
269                         Assert.AreEqual (1, c.Count, "Count");
270                 }
271
272                 [Test]
273                 public void CopyTo () 
274                 {
275                         string [] array = new string [4];
276                         NameValueCollection c = new NameValueCollection ();
277                         c.Add ("1", "mono");
278                         c.Add ("2", "MoNo");
279                         c.Add ("3", "mOnO");
280                         c.Add ("4", "MONO");
281                         c.CopyTo (array, 0);
282                 }
283
284                 [Test]
285                 [ExpectedException (typeof (ArgumentNullException))]
286                 public void CopyTo_Null () 
287                 {
288                         NameValueCollection c = new NameValueCollection ();
289                         c.CopyTo (null, 0);
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
294                 public void CopyTo_NegativeIndex () 
295                 {
296                         string [] array = new string [4];
297                         NameValueCollection c = new NameValueCollection ();
298                         c.Add ("1", "mono");
299                         c.Add ("2", "MoNo");
300                         c.Add ("3", "mOnO");
301                         c.Add ("4", "MONO");
302                         c.CopyTo (array, -1);
303                 }
304
305                 [Test]
306                 [ExpectedException (typeof (ArgumentException))]
307                 public void CopyTo_NotEnoughSpace () 
308                 {
309                         string [] array = new string [4];
310                         NameValueCollection c = new NameValueCollection ();
311                         c.Add ("1", "mono");
312                         c.Add ("2", "MoNo");
313                         c.Add ("3", "mOnO");
314                         c.Add ("4", "MONO");
315                         c.CopyTo (array, 2);
316                 }
317
318                 [Test]
319                 // Note: not a RankException
320                 [ExpectedException (typeof (ArgumentException))]
321                 public void CopyTo_MultipleDimensionStringArray () 
322                 {
323                         string [,,] matrix = new string [2,3,4];
324                         NameValueCollection c = new NameValueCollection ();
325                         c.Add ("1", "mono");
326                         c.Add ("2", "MoNo");
327                         c.Add ("3", "mOnO");
328                         c.Add ("4", "MONO");
329                         c.CopyTo (matrix, 0);
330                 }
331
332                 [Test]
333                 // Note: not a RankException
334                 [ExpectedException (typeof (ArgumentException))]
335                 public void CopyTo_MultipleDimensionArray () 
336                 {
337                         Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
338                         NameValueCollection c = new NameValueCollection ();
339                         c.CopyTo (a, 0);
340                 }
341                 
342                 [Test]
343 #if NET_2_0
344                 [ExpectedException (typeof (InvalidCastException))]
345 #else           
346                 [ExpectedException (typeof (ArrayTypeMismatchException))]
347 #endif
348                 public void CopyTo_WrongTypeArray ()
349                 {
350                         Array a = Array.CreateInstance (typeof (DateTime), 3);
351                         NameValueCollection c = new NameValueCollection ();
352                         for (int i = 0; i < 3; i++)
353                                 c.Add(i.ToString(), i.ToString());
354                         c.CopyTo(a, 0);
355                 }
356
357                 [Test]
358                 public void Remove () 
359                 {
360                         string[] items = { "mono", "MoNo", "mOnO", "MONO" };
361                         // default constructor is case insensitive
362                         NameValueCollection c = new NameValueCollection ();
363                         for (int i=0; i < items.Length; i++) {
364                                 string add = "Add-" + i.ToString () + "-Count";
365
366                                 c.Add (items [i], add);
367                                 Assert.AreEqual (1, c.Count, add);
368                                 c.Remove (items [0]);
369                                 Assert.AreEqual (0, c.Count, "Remove-0-Count");
370
371                                 c.Add (items [i], add);
372                                 Assert.AreEqual (1, c.Count, add);
373                                 c.Remove (items [1]);
374                                 Assert.AreEqual (0, c.Count, "Remove-1-Count");
375
376                                 c.Add (items [i], add);
377                                 Assert.AreEqual (1, c.Count, add);
378                                 c.Remove (items [2]);
379                                 Assert.AreEqual (0, c.Count, "Remove-2-Count");
380
381                                 c.Add (items [i], add);
382                                 Assert.AreEqual (1, c.Count, add);
383                                 c.Remove (items [3]);
384                                 Assert.AreEqual (0, c.Count, "Remove-3-Count");
385                         }
386                 }
387                 [Test]
388 #if NET_2_0
389                 [ExpectedException (typeof (ArgumentNullException))]
390 #else
391                 [ExpectedException (typeof (NullReferenceException))]
392 #endif          
393                 public void Constructor_Null_NVC ()
394                 {
395                         NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
396                 }
397                 
398                 [Test]
399 #if NET_2_0
400                 [ExpectedException (typeof (ArgumentNullException))]
401 #else
402                 [ExpectedException (typeof (NullReferenceException))]
403 #endif          
404                 public void Constructor_Capacity_Null_NVC ()
405                 {
406                         NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
407                 }
408
409 #if NET_2_0
410                 [Test]
411                 public void Constructor_IEqualityComparer ()
412                 {
413                         NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
414                         coll.Add ("a", "1");
415                         Assert.AreEqual (1, coll.Count, "#1");
416                 }
417
418                 [Test]
419                 public void Constructor_Int_IEqualityComparer ()
420                 {
421                         NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
422                         coll.Add ("a", "1");
423                         Assert.AreEqual (1, coll.Count, "#1");
424                 }
425
426                 [Test]
427                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
428                 public void Constructor_IntNegative_IEqualityComparer ()
429                 {
430                         new NameValueCollection (-1, new EqualityComparer ());
431                 }
432
433                 [Test]
434                 public void Constructor_IEqualityComparer_Null ()
435                 {
436                         NameValueCollection c1 = new NameValueCollection ((IEqualityComparer)null);
437                         c1.Add ("key", "value");
438                         Assert.AreEqual (c1.Get ("KEY"), "value", "Constructor_IEqualityComparer_Null");
439                         c1.Remove ("key");
440                 }
441
442                 [Test]
443                 public void Constructor_NameValueCollection ()
444                 {
445                         NameValueCollection c1 = new NameValueCollection (StringComparer.InvariantCultureIgnoreCase);
446                         c1.Add ("key", "value");
447                         NameValueCollection c2 = new NameValueCollection (c1);
448                         Assert.AreEqual (c2.Get ("KEY"), "value", "Constructor_NameValueCollection");
449                         c2.Remove ("key");
450                 }
451 #endif
452                 class MyNVC : NameValueCollection
453                 {
454                         List<string> log;
455
456                         public List<string> Log {
457                                 get {
458                                         if (log == null)
459                                                 log = new List<string> ();
460                                         return log;
461                                 }
462                         }
463
464                         public override KeysCollection Keys {
465                                 get {
466                                         Log.Add ("get_Keys");
467                                         return base.Keys;
468                                 }
469                         }
470
471                         public override int Count {
472                                 get {
473                                         Log.Add ("get_Count");
474                                         return base.Count;
475                                 }
476                         }
477
478                         public override string[] AllKeys {
479                                 get {
480                                         Log.Add ("get_AllKeys");
481                                         return base.AllKeys;
482                                 }
483                         }
484                         
485                         public override string Get (int index)
486                         {
487                                 Log.Add ("Get (int)");
488                                 return base.Get (index);
489                         }
490
491                         public override string Get (string name)
492                         {
493                                 Log.Add ("Get (string)");
494                                 return base.Get (name);
495                         }
496
497                         public override string GetKey (int index)
498                         {
499                                 Log.Add ("GetKey (int)");
500                                 return base.GetKey (index);
501                         }
502
503                         public override string[] GetValues (int index)
504                         {
505                                 Log.Add ("GetValues (int)");
506                                 return base.GetValues (index);
507                         }
508
509                         public override string[] GetValues (string name)
510                         {
511                                 Log.Add ("GetValues (string)");
512                                 return base.GetValues (name);
513                         }
514
515                         public override void Add (string name, string value)
516                         {
517                                 Log.Add ("Add (string, string)");
518                                 base.Add (name, value);
519                         }
520
521                         public override void Set (string name, string value)
522                         {
523                                 Log.Add ("Set (string, string)");
524                                 base.Set (name, value);
525                         }
526                 }
527         }
528 }