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