Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableCollectionTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Data;
34 using MonoTests.System.Data.Utils;
35
36 namespace MonoTests.System.Data
37 {
38         [TestFixture] public class DataTableCollectionTest2
39         {
40                 private int counter = 0;
41
42                 [Test] public void Add()
43                 {
44                         // Adding computed column to a data set
45                         DataSet ds = new DataSet();
46                         ds.Tables.Add(new DataTable("Table"));
47                         ds.Tables[0].Columns.Add(new DataColumn("EmployeeNo", typeof(string)));
48                         ds.Tables[0].Rows.Add(new object[] {"Maciek"});
49                         ds.Tables[0].Columns.Add("ComputedColumn", typeof(object), "EmployeeNo");
50
51                         Assert.AreEqual("EmployeeNo", ds.Tables[0].Columns["ComputedColumn"].Expression, "DTC1");
52                 }
53
54                 [Test]
55                 public void AddTwoTables()
56                 {
57                         DataSet ds = new DataSet();
58                         ds.Tables.Add();
59                         Assert.AreEqual("Table1", ds.Tables[0].TableName , "DTC2");
60                         //Assert.AreEqual(ds.Tables[0].TableName,"Table1");
61                         ds.Tables.Add();
62                         Assert.AreEqual("Table2", ds.Tables[1].TableName , "DTC3");
63                         //Assert.AreEqual(ds.Tables[1].TableName,"Table2");
64                 }
65
66                 [Test]
67                 public void AddRange()
68                 {
69                         DataSet ds = new DataSet();
70
71                         DataTable[] arr = new DataTable[2];
72
73                         arr[0] = new DataTable("NewTable1");
74                         arr[1] = new DataTable("NewTable2");
75
76                         ds.Tables.AddRange(arr);
77                         Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC4");
78                         Assert.AreEqual("NewTable2", ds.Tables[1].TableName, "DTC5");
79                 }
80                 [Test]
81                 public void AddRange_NullValue()
82                 {
83                         DataSet ds = new DataSet();
84                         ds.Tables.AddRange(null);
85                 }
86
87                 [Test]
88                 public void AddRange_ArrayWithNull()
89                 {
90                         DataSet ds = new DataSet();
91                         DataTable[] arr = new DataTable[2];
92                         arr[0] = new DataTable("NewTable1");
93                         arr[1] = (DataTable)null ;
94                         ds.Tables.AddRange(arr);
95                         Assert.AreEqual("NewTable1", ds.Tables[0].TableName, "DTC6");
96                         Assert.AreEqual(1, ds.Tables.Count, "DTC7");
97                 }
98
99                 [Test]
100                 public void CanRemove()
101                 {
102                         DataSet ds = new DataSet();
103                         ds.Tables.Add();
104                         Assert.AreEqual(true, ds.Tables.CanRemove(ds.Tables[0]), "DTC8"); 
105                 }
106
107                 [Test]
108                 public void CanRemove_NullValue()
109                 {
110                         DataSet ds = new DataSet();
111                         Assert.AreEqual(false, ds.Tables.CanRemove(null), "DTC9");
112                 }
113
114                 [Test]
115                 public void CanRemove_TableDoesntBelong()
116                 {
117                         DataSet ds = new DataSet();
118                         DataSet ds1 = new DataSet();
119                         ds1.Tables.Add();
120                         Assert.AreEqual(false, ds.Tables.CanRemove(ds1.Tables[0]), "DTC10");
121                 }
122
123                 [Test]
124                 public void CanRemove_PartOfRelation()
125                 {
126                         DataSet ds = new DataSet();
127                         ds.Tables.Add(DataProvider.CreateParentDataTable());
128                         ds.Tables.Add(DataProvider.CreateChildDataTable());
129
130                         ds.Relations.Add("rel",ds.Tables[0].Columns["ParentId"],ds.Tables[1].Columns["ParentId"],false);
131
132                         Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC11");
133                         Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC12");
134                 }
135                 [Test]
136                 public void CanRemove_PartOfConstraint()
137                 {
138                         DataSet ds = DataProvider.CreateForigenConstraint();
139                         Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[0]), "DTC13"); //Unique 
140                         Assert.AreEqual(false, ds.Tables.CanRemove(ds.Tables[1]), "DTC14"); //Foreign 
141                 }
142
143                 [Test]
144                 public void CollectionChanged()
145                 {
146                         counter = 0;
147                         DataSet ds = new DataSet();
148                         ds.Tables.CollectionChanged+=new CollectionChangeEventHandler(Tables_CollectionChanged);
149                         ds.Tables.Add();
150                         ds.Tables.Add();
151                         Assert.AreEqual(2, counter, "DTC15");
152
153                         ds.Tables.Remove(ds.Tables[0]);
154                         ds.Tables.Remove(ds.Tables[0]);
155                         Assert.AreEqual(4, counter, "DTC16");
156                 }
157
158                 private void Tables_CollectionChanged(object sender, CollectionChangeEventArgs e)
159                 {
160                         counter++;
161                 }
162
163                 [Test]
164                 public void CollectionChanging()
165                 {
166                         counter = 0;
167                         DataSet ds = new DataSet();
168                         ds.Tables.CollectionChanging+=new CollectionChangeEventHandler(Tables_CollectionChanging);
169                         ds.Tables.Add();
170                         ds.Tables.Add();
171                         Assert.AreEqual(2, counter, "DTC17");
172
173                         ds.Tables.Remove(ds.Tables[0]);
174                         ds.Tables.Remove(ds.Tables[0]);
175                         Assert.AreEqual(4, counter, "DTC18");
176                 }
177
178                 private void Tables_CollectionChanging(object sender, CollectionChangeEventArgs e)
179                 {
180                         counter++;
181                 }
182
183                 [Test]
184                 public void Contains()
185                 {
186                         DataSet ds = new DataSet();
187                         ds.Tables.Add("NewTable1");
188                         ds.Tables.Add("NewTable2");
189
190                         Assert.AreEqual(true, ds.Tables.Contains("NewTable1"), "DTC19");
191                         Assert.AreEqual(true, ds.Tables.Contains("NewTable2"), "DTC20");
192                         Assert.AreEqual(false, ds.Tables.Contains("NewTable3"), "DTC21");
193
194                         ds.Tables["NewTable1"].TableName = "Tbl1";
195                         Assert.AreEqual(false, ds.Tables.Contains("NewTable1"), "DTC22");
196                         Assert.AreEqual(true, ds.Tables.Contains("Tbl1"), "DTC23");
197                 }
198
199                 [Test]
200                 public void CopyTo()
201                 {
202                         DataSet ds = new DataSet();
203                         ds.Tables.Add();
204                         ds.Tables.Add();
205                         DataTable[] arr = new DataTable[2];
206                         ds.Tables.CopyTo(arr,0);
207                         Assert.AreEqual("Table1", ((DataTable)arr[0]).TableName, "DTC24");
208                         Assert.AreEqual("Table2", ((DataTable)arr[1]).TableName, "DTC25");
209                 }
210
211                 [Test]
212                 public void Count()
213                 {
214                         DataSet ds = new DataSet();
215                         Assert.AreEqual(0, ds.Tables.Count, "DTC26");
216
217                         ds.Tables.Add();
218                         Assert.AreEqual(1, ds.Tables.Count, "DTC27");
219
220                         ds.Tables.Add();
221                         Assert.AreEqual(2, ds.Tables.Count, "DTC28");
222
223                         ds.Tables.Remove("Table1");
224                         Assert.AreEqual(1, ds.Tables.Count, "DTC29");
225
226                         ds.Tables.Remove("Table2");
227                         Assert.AreEqual(0, ds.Tables.Count, "DTC30");
228                 }
229
230                 [Test]
231                 public void GetEnumerator()
232                 {
233                         DataSet ds = new DataSet();
234                         ds.Tables.Add();
235                         ds.Tables.Add();
236                         int count=0;
237
238                         IEnumerator myEnumerator = ds.Tables.GetEnumerator();
239
240                         while (myEnumerator.MoveNext())
241                         {
242                                 Assert.AreEqual("Table",  ((DataTable) myEnumerator.Current).TableName.Substring(0,5), "DTC31");
243                                 count++;
244                         }
245                         Assert.AreEqual(2, count, "DTC32");
246                 }
247                 public void IndexOf_ByDataTable()
248                 {
249                         DataSet ds = new DataSet();
250                         DataTable dt = new DataTable("NewTable1");
251                         DataTable dt1 = new DataTable("NewTable2");
252                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
253
254                         Assert.AreEqual(0, ds.Tables.IndexOf(dt), "DTC33");
255                         Assert.AreEqual(1, ds.Tables.IndexOf(dt1), "DTC34");
256
257                         ds.Tables.IndexOf((DataTable)null);
258
259                         DataTable dt2 = new DataTable("NewTable2");
260
261                         Assert.AreEqual(-1, ds.Tables.IndexOf(dt2), "DTC35");
262                 }
263
264                 public void IndexOf_ByName()
265                 {
266                         DataSet ds = new DataSet();
267                         DataTable dt = new DataTable("NewTable1");
268                         DataTable dt1 = new DataTable("NewTable2");
269                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
270
271                         Assert.AreEqual(0, ds.Tables.IndexOf("NewTable1"), "DTC36");
272                         Assert.AreEqual(1, ds.Tables.IndexOf("NewTable2"), "DTC37");
273
274                         ds.Tables.IndexOf((string)null);
275
276                         Assert.AreEqual(-1, ds.Tables.IndexOf("NewTable3"), "DTC38");
277                 }
278
279                 [Test]
280                 public void Item()
281                 {
282                         DataSet ds = new DataSet();
283                         DataTable dt = new DataTable("NewTable1");
284                         DataTable dt1 = new DataTable("NewTable2");
285                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
286
287                         Assert.AreEqual(dt, ds.Tables[0], "DTC39");
288                         Assert.AreEqual(dt1, ds.Tables[1], "DTC40");
289                         Assert.AreEqual(dt, ds.Tables["NewTable1"], "DTC41");
290                         Assert.AreEqual(dt1, ds.Tables["NewTable2"], "DTC42");
291                 }
292
293                 [Test]
294                 public void DataTableCollection_Add_D1()
295                 {
296                         DataSet ds = new DataSet();
297                         DataTable dt = new DataTable("NewTable1");
298                         ds.Tables.Add(dt);
299                         Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC43");
300                 }
301
302                 [Test]
303                 [ExpectedException(typeof(ArgumentNullException))]
304                 public void DataTableCollection_Add_D2()
305                 {
306                         DataSet ds = new DataSet();
307
308                         ds.Tables.Add((DataTable)null);
309                 }
310
311                 [Test]
312                 [ExpectedException(typeof(ArgumentException))]
313                 public void DataTableCollection_Add_D3()
314                 {
315                         DataSet ds = new DataSet();
316                         DataSet ds1 = new DataSet();
317                         ds1.Tables.Add();
318
319                         ds.Tables.Add(ds1.Tables[0]);
320                 }
321
322                 [Test]
323                 [ExpectedException(typeof(DuplicateNameException))]
324                 public void DataTableCollection_Add_D4()
325                 {
326                         DataSet ds = new DataSet();
327                         ds.Tables.Add();
328
329                         DataTable dt = new DataTable("Table1");
330                         ds.Tables.Add(dt);
331                 }
332
333                 [Test]
334                 public void DataTableCollection_Add_S1()
335                 {
336                         DataSet ds = new DataSet();
337                         ds.Tables.Add("NewTable1");
338                         Assert.AreEqual("NewTable1",ds.Tables[0].TableName,"DTC44");
339                         ds.Tables.Add("NewTable2");
340                         Assert.AreEqual("NewTable2",ds.Tables[1].TableName,"DTC45");
341                 }
342
343                 [Test]
344                 [ExpectedException(typeof(DuplicateNameException))]
345                 public void DataTableCollection_Add_S2()
346                 {
347                         DataSet ds = new DataSet();
348                         ds.Tables.Add("NewTable1");
349
350                         ds.Tables.Add("NewTable1");
351                 }
352
353                 [Test]
354                 public void DataTableCollection_Clear1()
355                 {
356                         DataSet ds = new DataSet();
357                         ds.Tables.Add();
358                         ds.Tables.Add();
359                         ds.Tables.Clear();
360                         Assert.AreEqual(0,ds.Tables.Count,"DTC46");
361
362                 }
363
364                 [Test]
365                 [ExpectedException(typeof(IndexOutOfRangeException))]
366                 public void DataTableCollection_Clear2()
367                 {
368                         DataSet ds = new DataSet();
369                         ds.Tables.Add();
370                         ds.Tables.Add();
371                         ds.Tables.Clear();
372
373                         ds.Tables[0].TableName = "Error";
374                 }
375
376                 [Test]
377                 public void DataTableCollection_Remove_D1()
378                 {
379                         DataSet ds = new DataSet();
380                         DataTable dt = new DataTable("NewTable1");
381                         DataTable dt1 = new DataTable("NewTable2");
382                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
383
384                         ds.Tables.Remove(dt);
385                         Assert.AreEqual(1,ds.Tables.Count,"DTC47");
386                         Assert.AreEqual(dt1,ds.Tables[0],"DTC48");
387                         ds.Tables.Remove(dt1);
388                         Assert.AreEqual(0,ds.Tables.Count,"DTC49");
389                 }
390
391                 [Test]
392                 [ExpectedException(typeof(ArgumentException))]
393                 public void DataTableCollection_Remove_D2()
394                 {
395                         DataSet ds = new DataSet();
396                         DataTable dt = new DataTable("NewTable1");
397
398                         ds.Tables.Remove(dt);
399                 }
400
401                 [Test]
402                 [ExpectedException(typeof(ArgumentNullException))]
403                 public void DataTableCollection_Remove_D3()
404                 {
405                         DataSet ds = new DataSet();
406
407                         ds.Tables.Remove((DataTable)null);
408                 }
409
410                 [Test]
411                 public void DataTableCollection_Remove_S1()
412                 {
413                         DataSet ds = new DataSet();
414                         DataTable dt = new DataTable("NewTable1");
415                         DataTable dt1 = new DataTable("NewTable2");
416                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
417
418                         ds.Tables.Remove("NewTable1");
419                         Assert.AreEqual(1,ds.Tables.Count,"DTC50");
420                         Assert.AreEqual(dt1,ds.Tables[0],"DTC51");
421                         ds.Tables.Remove("NewTable2");
422                         Assert.AreEqual(0,ds.Tables.Count,"DTC52");     
423                 }
424
425                 [Test]
426                 [ExpectedException(typeof(ArgumentException))]
427                 public void DataTableCollection_Remove_S2()
428                 {
429                         DataSet ds = new DataSet();
430
431                         ds.Tables.Remove("NewTable2");
432                 }
433
434                 [Test]
435                 [ExpectedException(typeof(ArgumentException))]
436                 public void DataTableCollection_Remove_S3()
437                 {
438                         DataSet ds = new DataSet();
439
440                         ds.Tables.Remove((string)null);
441                 }
442
443                 [Test]
444                 public void DataTableCollection_RemoveAt_I1()
445                 {
446                         DataSet ds = new DataSet();
447                         DataTable dt = new DataTable("NewTable1");
448                         DataTable dt1 = new DataTable("NewTable2");
449                         ds.Tables.AddRange(new DataTable[] {dt,dt1});
450
451                         ds.Tables.RemoveAt(1);
452                         Assert.AreEqual(dt,ds.Tables[0],"DTC53");
453                         ds.Tables.RemoveAt(0);
454                         Assert.AreEqual(0,ds.Tables.Count,"DTC54");
455                 }
456
457                 [Test]
458                 [ExpectedException(typeof(IndexOutOfRangeException))]
459                 public void DataTableCollection_RemoveAt_I2()
460                 {
461                         DataSet ds = new DataSet();
462
463                         ds.Tables.RemoveAt(-1);
464                 }
465
466                 [Test]
467                 [ExpectedException(typeof(ArgumentException))]
468                 public void DataTableCollection_RemoveAt_I3()
469                 {
470                         DataSet ds = DataProvider.CreateForigenConstraint();
471
472                         ds.Tables.RemoveAt(0); //Parent table
473                 }
474
475 #if NET_2_0
476                 [Test]
477                 public void AddTable_DiffNamespaceTest ()
478                 {
479                         DataSet ds = new DataSet ();
480                         ds.Tables.Add ("table", "namespace1");
481                         ds.Tables.Add ("table", "namespace2");
482                         Assert.AreEqual (2, ds.Tables.Count, "#1");
483
484                         try {
485                                 ds.Tables.Add ("table", "namespace1");
486                                 Assert.Fail ("#2");
487                         } catch (DuplicateNameException e) { }
488
489                         ds.Tables.Add ("table");
490                         try {
491                                 ds.Tables.Add ("table", null);
492                                 Assert.Fail ("#4");
493                         } catch (DuplicateNameException e) { }
494                 }
495
496                 [Test]
497                 public void Contains_DiffNamespaceTest ()
498                 {
499                         DataSet ds = new DataSet ();
500                         ds.Tables.Add ("table");
501                         Assert.IsTrue (ds.Tables.Contains ("table"), "#1");
502
503                         ds.Tables.Add ("table", "namespace1");
504                         ds.Tables.Add ("table", "namespace2");
505
506                         // Should fail if it cannot be resolved to a single table
507                         Assert.IsFalse (ds.Tables.Contains ("table"));
508
509                         try {
510                                 ds.Tables.Contains ("table", null);
511                                 Assert.Fail ("#2");
512                         } catch (ArgumentNullException e) { }
513
514                         Assert.IsTrue (ds.Tables.Contains ("table", "namespace1"), "#4");
515                         Assert.IsFalse (ds.Tables.Contains ("table", "namespace3"), "#5");
516                 }
517
518                 [Test]
519                 public void IndexOf_DiffNamespaceTest ()
520                 {
521                         DataSet ds = new DataSet ();
522                         ds.Tables.Add ("table");
523                         Assert.AreEqual (0, ds.Tables.IndexOf ("table"), "#1");
524                         ds.Tables.Add ("table", "namespace1");
525                         ds.Tables.Add ("table", "namespace2");
526                         Assert.AreEqual (-1, ds.Tables.IndexOf ("table"), "#2");
527                         Assert.AreEqual (2, ds.Tables.IndexOf ("table", "namespace2"), "#3");
528                         Assert.AreEqual (1, ds.Tables.IndexOf ("table", "namespace1"), "#4");
529                 }
530
531                 [Test]
532                 public void Remove_DiffNamespaceTest ()
533                 {
534                         DataSet ds = new DataSet ();
535                         ds.Tables.Add ("table");
536                         ds.Tables.Add ("table", "namespace1");
537                         ds.Tables.Add ("table", "namespace2");
538
539                         try {
540                                 ds.Tables.Remove ("table");
541                                 Assert.Fail ("#1");
542                         } catch (ArgumentException e) { }
543
544                         ds.Tables.Remove ("table", "namespace2");
545                         Assert.AreEqual (2, ds.Tables.Count, "#3");
546                         Assert.AreEqual ("namespace1", ds.Tables [1].Namespace, "#4");
547
548                         try {
549                                 ds.Tables.Remove ("table", "namespace2");
550                                 Assert.Fail ("#5");
551                         } catch (ArgumentException e) { }
552                 }
553 #endif
554         }
555 }