[runtime] Updates comments.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataViewTest2.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.IO;
34 using System.Data;
35 using MonoTests.System.Data.Utils;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture] public class DataViewTest2
40         {
41                 private EventProperties evProp = null;
42
43                 class EventProperties  //hold the event properties to be checked
44                 {
45                         public ListChangedType lstType ;
46                         public int NewIndex;
47                         public int OldIndex;
48                 }
49
50                 [Test] public void AddNew()
51                 {
52                         //create the source datatable
53                         DataTable dt = DataProvider.CreateChildDataTable();
54
55                         //create the dataview for the table
56                         DataView dv = new DataView(dt);
57
58                         int CountView = dv.Count ;
59                         int CountTable= dt.Rows.Count ;
60
61                         DataRowView drv = null;
62
63                         // AddNew - DataView Row Count
64                         drv = dv.AddNew();
65                         Assert.AreEqual(dv.Count , CountView+1, "DV1");
66
67                         // AddNew - Table Row Count 
68                         Assert.AreEqual(dt.Rows.Count , CountTable, "DV2");
69
70                         // AddNew - new row in DataTable
71                         drv.EndEdit();
72                         Assert.AreEqual(dt.Rows.Count , CountTable+1, "DV3");
73
74                         // AddNew - new row != null
75                         Assert.AreEqual(true, drv!=null, "DV4");
76
77                         // AddNew - check table
78                         Assert.AreEqual(dt, drv.Row.Table, "DV5");
79                 }
80
81                 [Test] public void AllowDelete()
82                 {
83                         DataTable dt = DataProvider.CreateParentDataTable();
84                         DataView dv = new DataView(dt);
85
86                         // AllowDelete - default value
87                         Assert.AreEqual(true , dv.AllowDelete , "DV6");
88
89                         // AllowDelete - true
90                         dv.AllowDelete = true;
91                         Assert.AreEqual(true, dv.AllowDelete , "DV7");
92
93                         // AllowDelete - false
94                         dv.AllowDelete = false;
95                         Assert.AreEqual(false, dv.AllowDelete , "DV8");
96
97                         dv.AllowDelete = false;
98                         // AllowDelete false- Exception
99                         try 
100                         {
101                                 dv.Delete(0);
102                                 Assert.Fail("DV9: Delete Failed to throw DataException");
103                         }
104                         catch (DataException) {}
105                         catch (AssertionException exc) {throw  exc;}
106                         catch (Exception exc)
107                         {
108                                 Assert.Fail("DV10: Delete. Wrong exception type. Got:" + exc);
109                         }
110
111                         dv.AllowDelete = true;
112                         int RowsCount = dv.Count ;
113                         // AllowDelete true- Exception
114                         dv.Delete(0);
115                         Assert.AreEqual(RowsCount-1, dv.Count , "DV11");
116                 }
117
118                 [Test] public void AllowEdit()
119                 {
120                         DataTable dt = DataProvider.CreateParentDataTable();
121                         DataView dv = new DataView(dt);
122
123                         // AllowEdit - default value
124                         Assert.AreEqual(true , dv.AllowEdit , "DV12");
125
126                         // AllowEdit - true
127                         dv.AllowEdit = true;
128                         Assert.AreEqual(true, dv.AllowEdit , "DV13");
129
130                         // AllowEdit - false
131                         dv.AllowEdit = false;
132                         Assert.AreEqual(false, dv.AllowEdit , "DV14");
133
134                         dv.AllowEdit=false;
135
136                         // AllowEdit false - exception
137                         try 
138                         {
139                                 dv[0][2] = "aaa";
140                                 Assert.Fail("DV15: Indexer Failed to throw DataException");
141                         }
142                         catch (DataException) {}
143                         catch (AssertionException exc) {throw  exc;}
144                         catch (Exception exc)
145                         {
146                                 Assert.Fail("DV16: Indexer. Wrong exception type. Got:" + exc);
147                         }
148
149                         dv.AllowEdit=true;
150
151                         // AllowEdit true- exception
152                         dv[0][2] = "aaa";
153                         Assert.AreEqual("aaa", dv[0][2] , "DV17");
154                 }
155
156                 [Test] public void AllowNew()
157                 {
158                         DataTable dt = DataProvider.CreateParentDataTable();
159                         DataView dv = new DataView(dt);
160
161                         // AllowNew - default value
162                         Assert.AreEqual(true , dv.AllowNew , "DV18");
163
164                         // AllowNew - true
165                         dv.AllowNew = true;
166                         Assert.AreEqual(true, dv.AllowNew , "DV19");
167
168                         // AllowNew - false
169                         dv.AllowNew = false;
170                         Assert.AreEqual(false, dv.AllowNew , "DV20");
171
172                         // AllowNew - exception
173                         try 
174                         {
175                                 dv.AddNew();
176                                 Assert.Fail("DV21: AddNew Failed to throw DataException");
177                         }
178                         catch (DataException) {}
179                         catch (AssertionException exc) {throw  exc;}
180                         catch (Exception exc)
181                         {
182                                 Assert.Fail("DV22: AddNew. Wrong exception type. Got:" + exc);
183                         }
184
185                         dv.AllowNew=true;
186                         int RowsCount = dv.Count ;
187
188                         // AllowNew - exception
189                         dv.AddNew();
190                         Assert.AreEqual(RowsCount+1, dv.Count , "DV23");
191                 }
192
193                 [Test] public void ApplyDefaultSort()
194                 {
195                         DataTable dt = DataProvider.CreateParentDataTable();
196                         DataView dv = new DataView(dt);
197
198                         // ApplyDefaultSort - default value
199                         Assert.AreEqual(false , dv.ApplyDefaultSort , "DV24");
200
201                         // ApplyDefaultSort - true
202                         dv.ApplyDefaultSort = true;
203                         Assert.AreEqual(true, dv.ApplyDefaultSort , "DV25");
204
205                         // ApplyDefaultSort - false
206                         dv.ApplyDefaultSort = false;
207                         Assert.AreEqual(false, dv.ApplyDefaultSort , "DV26");
208                 }
209
210                 [Test] public void CopyTo()
211                 {
212                         //create the source datatable
213                         DataTable dt = DataProvider.CreateChildDataTable();
214
215                         //create the dataview for the table
216                         DataView dv = new DataView(dt);
217
218                         DataRowView[] drvExpected = null;
219                         DataRowView[] drvResult = null;
220
221                         // ------- Copy from Index=0
222                         drvExpected = new DataRowView[dv.Count];
223                         for (int i=0; i < dv.Count ;i++)
224                         {
225                                 drvExpected[i] = dv[i];
226                         }
227
228                         drvResult = new DataRowView[dv.Count];
229                         // CopyTo from index 0
230                         dv.CopyTo(drvResult,0);
231                         Assert.AreEqual(drvResult, drvExpected , "DV27");
232
233                         // ------- Copy from Index=3
234                         drvExpected = new DataRowView[dv.Count+3];
235                         for (int i=0; i < dv.Count ;i++)
236                         {
237                                 drvExpected[i+3] = dv[i];
238                         }
239
240                         drvResult = new DataRowView[dv.Count+3];
241                         // CopyTo from index 3
242                         dv.CopyTo(drvResult,3);
243                         Assert.AreEqual(drvResult , drvExpected , "DV28");
244
245                         // ------- Copy from Index=3,larger array
246                         drvExpected = new DataRowView[dv.Count+9];
247                         for (int i=0; i < dv.Count ;i++)
248                         {
249                                 drvExpected[i+3] = dv[i];
250                         }
251
252                         drvResult = new DataRowView[dv.Count+9];
253                         // CopyTo from index 3,larger array
254                         dv.CopyTo(drvResult,3);
255                         Assert.AreEqual(drvResult, drvExpected , "DV29");
256
257                         // ------- CopyTo smaller array, check exception
258                         drvResult = new DataRowView[dv.Count-1];
259
260                         // CopyTo smaller array, check exception
261                         try 
262                         {
263                                 dv.CopyTo(drvResult,0);
264                                 Assert.Fail("DV30: CopyTo Failed to throw IndexOutOfRangeException");
265                         }
266                         catch (IndexOutOfRangeException) {}
267                         catch (AssertionException exc) {throw  exc;}
268                         catch (Exception exc)
269                         {
270                                 Assert.Fail("DV31: CopyTo. Wrong exception type. Got:" + exc);
271                         }
272                 }
273
274                 [Test] public void Delete()
275                 {
276                         //create the source datatable
277                         DataTable dt = DataProvider.CreateChildDataTable();
278
279                         //create the dataview for the table
280                         DataView dv = new DataView(dt);
281
282                         int CountView = dv.Count ;
283                         int CountTable= dt.Rows.Count ;
284
285                         DataRowView drv = dv[0];
286
287                         // Delete - DataView Row Count
288                         dv.Delete(0);
289                         Assert.AreEqual(dv.Count , CountView-1, "DV32");
290
291                         // Delete - Table Row Count 
292                         Assert.AreEqual(dt.Rows.Count , CountTable, "DV33");
293
294                         // Delete - check table
295                         Assert.AreEqual(dt, drv.Row.Table, "DV34");
296                 }
297
298                 [Test] public void FindRows_ByKey()
299                 {
300                         DataRowView[] dvArr = null;
301
302                         //create the source datatable
303                         DataTable dt = DataProvider.CreateChildDataTable();
304
305                         //create the dataview for the table
306                         DataView dv = new DataView(dt);
307
308                         // FindRows ,no sort - exception
309                         try 
310                         {
311                                 dvArr = dv.FindRows(3);
312                                 Assert.Fail("DV35: FindRows Failed to throw ArgumentException");
313                         }
314                         catch (ArgumentException) {}
315                         catch (AssertionException exc) {throw  exc;}
316                         catch (Exception exc)
317                         {
318                                 Assert.Fail("DV36: FindRows. Wrong exception type. Got:" + exc);
319                         }
320
321                         dv.Sort = "String1";
322                         // Find = wrong sort, can not find
323                         dvArr = dv.FindRows(3);
324                         Assert.AreEqual(0, dvArr.Length  , "DV37");
325
326                         dv.Sort = "ChildId";
327
328                         //get expected results
329                         DataRow[] drExpected = dt.Select("ChildId=3");
330
331                         // FindRows - check count
332                         dvArr = dv.FindRows(3);
333                         Assert.AreEqual(drExpected.Length , dvArr.Length, "DV38");
334
335                         // FindRows - check data
336
337                         //check that result is ok
338                         bool Succeed = true;
339                         for (int i=0; i<dvArr.Length ; i++)
340                         {
341                                 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
342                                 if (!Succeed) break;
343                         }
344                         Assert.AreEqual(true, Succeed , "DV39");
345                 }
346
347                 [Test] public void FindRows_ByKeys()
348                 {
349                         DataRowView[] dvArr = null;
350
351                         //create the source datatable
352                         DataTable dt = DataProvider.CreateChildDataTable();
353
354                         //create the dataview for the table
355                         DataView dv = new DataView(dt);
356
357                         // FindRows ,no sort - exception
358                         try 
359                         {
360                                 dvArr = dv.FindRows(new object[] {"3","3-String1"});
361                                 Assert.Fail("DV40: FindRows Failed to throw ArgumentException");
362                         }
363                         catch (ArgumentException) {}
364                         catch (AssertionException exc) {throw  exc;}
365                         catch (Exception exc)
366                         {
367                                 Assert.Fail("DV41: FindRows. Wrong exception type. Got:" + exc);
368                         }
369
370                         dv.Sort = "String1,ChildId";
371                         // Find = wrong sort, can not find
372                         dvArr = dv.FindRows(new object[] {"3","3-String1"});
373                         Assert.AreEqual(0, dvArr.Length  , "DV42");
374
375                         dv.Sort = "ChildId,String1";
376
377                         //get expected results
378                         DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
379
380                         // FindRows - check count
381                         dvArr = dv.FindRows(new object[] {"3","3-String1"});
382                         Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
383
384                         // FindRows - check data
385
386                         //check that result is ok
387                         bool Succeed = true;
388                         for (int i=0; i<dvArr.Length ; i++)
389                         {
390                                 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
391                                 if (!Succeed) break;
392                         }
393                         Assert.AreEqual(true, Succeed , "DV44");
394                 }
395
396                 //Activate This Construntor to log All To Standard output
397                 //public TestClass():base(true){}
398
399                 //Activate this constructor to log Failures to a log file
400                 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
401
402                 //Activate this constructor to log All to a log file
403                 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
404
405                 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
406
407                 [Test] public void Find_ByObject()
408                 {
409                         int FindResult,ExpectedResult=-1;
410
411                         //create the source datatable
412                         DataTable dt = DataProvider.CreateParentDataTable();
413
414                         //create the dataview for the table
415                         DataView dv = new DataView(dt);
416
417                         for (int i=0; i<dt.Rows.Count ; i++)
418                         {
419                                 if ((int)dt.Rows[i]["ParentId"] == 3)
420                                 {
421                                         ExpectedResult = i;
422                                         break;
423                                 }
424                         }
425
426                         // Find ,no sort - exception
427                         try 
428                         {
429                                 FindResult = dv.Find("3");
430                                 Assert.Fail("DV45: Find Failed to throw ArgumentException");
431                         }
432                         catch (ArgumentException) {}
433                         catch (AssertionException exc) {throw  exc;}
434                         catch (Exception exc)
435                         {
436                                 Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
437                         }
438
439                         dv.Sort = "String1";
440                         // Find = wrong sort, can not find
441                         FindResult = dv.Find("3");
442                         Assert.AreEqual(-1, FindResult , "DV47");
443
444                         dv.Sort = "ParentId";
445                         // Find 
446                         FindResult = dv.Find("3");
447                         Assert.AreEqual(ExpectedResult, FindResult , "DV48");
448                 }
449
450                 [Test] public void Find_ByArray()
451                 {
452                         int FindResult,ExpectedResult=-1;
453
454                         //create the source datatable
455                         DataTable dt = DataProvider.CreateParentDataTable();
456
457                         //create the dataview for the table
458                         DataView dv = new DataView(dt);
459
460                         for (int i=0; i<dt.Rows.Count ; i++)
461                         {
462                                 if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
463                                 {
464                                         ExpectedResult = i;
465                                         break;
466                                 }
467                         }
468
469                         // Find ,no sort - exception
470                         try 
471                         {
472                                 FindResult = dv.Find(new object[] {"3","3-String1"});
473                                 Assert.Fail("DV49: Find Failed to throw ArgumentException");
474                         }
475                         catch (ArgumentException) {}
476                         catch (AssertionException exc) {throw  exc;}
477                         catch (Exception exc)
478                         {
479                                 Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
480                         }
481
482                         dv.Sort = "String1,ParentId";
483                         // Find = wrong sort, can not find
484                         FindResult = dv.Find(new object[] {"3","3-String1"});
485                         Assert.AreEqual(-1, FindResult , "DV51");
486
487                         dv.Sort = "ParentId,String1";
488                         // Find 
489                         FindResult = dv.Find(new object[] {"3","3-String1"});
490                         Assert.AreEqual(ExpectedResult, FindResult , "DV52");
491                 }
492
493                 //Activate This Construntor to log All To Standard output
494                 //public TestClass():base(true){}
495
496                 //Activate this constructor to log Failures to a log file
497                 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
498
499                 //Activate this constructor to log All to a log file
500                 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
501
502                 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
503
504                 [Test] public void GetEnumerator()
505                 {
506                         //create the source datatable
507                         DataTable dt = DataProvider.CreateChildDataTable();
508
509                         //create the dataview for the table
510                         DataView dv = new DataView(dt);
511
512                         IEnumerator ienm = null;
513
514                         // GetEnumerator != null
515                         ienm = dv.GetEnumerator();
516                         Assert.AreEqual(true, ienm != null, "DV53");
517
518                         int i=0;
519                         while (ienm.MoveNext() )
520                         {
521                                 // Check item i
522                                 Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
523                                 i++;
524                         }
525                 }
526
527                 [Test] public void Item()
528                 {
529                         //create the source datatable
530                         DataTable dt = DataProvider.CreateParentDataTable();
531
532                         //create the dataview for the table
533                         DataView dv = new DataView(dt);
534
535                         // DataView Item 0
536                         Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
537
538                         // DataView Item 4
539                         Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
540
541                         dv.RowFilter="ParentId in (1,3,6)";
542
543                         // DataView Item 0,DataTable with filter
544                         Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
545                 }
546
547                 [Test] public void ListChanged()
548                 {
549                         DataTable dt = DataProvider.CreateParentDataTable();
550                         DataView dv = new DataView(dt);
551
552                         //add event handler
553                         dv.ListChanged +=new ListChangedEventHandler(dv_ListChanged);
554
555                         // ----- Change Value ---------
556                         evProp = null;
557                         // change value - Event raised
558                         dv[1]["String1"] = "something";
559                         Assert.AreEqual(true , evProp!=null , "DV58");
560                         // change value - ListChangedType
561                         Assert.AreEqual(ListChangedType.ItemChanged, evProp.lstType , "DV59");
562                         // change value - NewIndex
563                         Assert.AreEqual(1, evProp.NewIndex, "DV60");
564                         // change value - OldIndex
565                         Assert.AreEqual(-1, evProp.OldIndex , "DV61");
566
567                         // ----- Add New ---------
568                         evProp = null;
569                         // Add New  - Event raised
570                         dv.AddNew();
571                         Assert.AreEqual(true , evProp!=null , "DV62");
572                         // Add New  - ListChangedType
573                         Assert.AreEqual(ListChangedType.ItemAdded , evProp.lstType , "DV63");
574                         // Add New  - NewIndex
575                         Assert.AreEqual(6, evProp.NewIndex, "DV64");
576                         // Add New  - OldIndex
577                         Assert.AreEqual(-1, evProp.OldIndex , "DV65");
578
579                         // ----- Sort ---------
580                         evProp = null;
581                         // sort  - Event raised
582                         dv.Sort = "ParentId Desc";
583                         Assert.AreEqual(true , evProp!=null , "DV66");
584                         // sort - ListChangedType
585                         Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV67");
586                         // sort - NewIndex
587                         Assert.AreEqual(-1, evProp.NewIndex, "DV68");
588                         // sort - OldIndex
589                         Assert.AreEqual(-1, evProp.OldIndex , "DV69");
590
591                         //ListChangedType - this was not checked
592                         //Move
593                         //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema. 
594                         //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema. 
595                         //PropertyDescriptorDeleted 
596                 }
597
598                 [Test]
599                 public void AcceptChanges ()
600                 {
601                         evProp = null;
602                         DataTable dt = new DataTable ();
603                         IBindingList list = dt.DefaultView;
604                         list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
605                         dt.Columns.Add ("test", typeof (int));
606                         dt.Rows.Add (new object[] { 10 });
607                         dt.Rows.Add (new object[] { 20 });
608                         // ListChangedType.Reset
609                         dt.AcceptChanges ();
610
611                         Assert.AreEqual(true , evProp != null , "DV166");
612                         // AcceptChanges - should emit ListChangedType.Reset
613                         Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
614                 }
615
616                 [Test]
617                 public void ClearTable ()
618                 {
619                         evProp = null;
620                         DataTable dt = new DataTable ();
621                         IBindingList list = dt.DefaultView;
622                         list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
623                         dt.Columns.Add ("test", typeof (int));
624                         dt.Rows.Add (new object[] { 10 });
625                         dt.Rows.Add (new object[] { 20 });
626                         // Clears DataTable
627                         dt.Clear ();
628
629                         Assert.AreEqual(true , evProp != null , "DV168");
630                         // Clear DataTable - should emit ListChangedType.Reset
631                         Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV169");
632                         // Clear DataTable - should clear view count
633                         Assert.AreEqual(0, dt.DefaultView.Count , "DV169");
634                 }
635
636                 private void dv_ListChanged(object sender, ListChangedEventArgs e)
637                 {
638                         evProp = new EventProperties(); 
639                         evProp.lstType = e.ListChangedType;
640                         evProp.NewIndex = e.NewIndex;
641                         evProp.OldIndex = e.OldIndex; 
642                 }
643
644                 [Test] public void RowFilter()
645                 {
646                         //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
647                         // this test also check DataView.Count property
648
649                         DataRowView[] drvResult = null;
650                         ArrayList al = new ArrayList();
651
652                         //create the source datatable
653                         DataTable dt = DataProvider.CreateChildDataTable();
654
655                         //create the dataview for the table
656                         DataView dv = new DataView(dt);
657
658                         //-------------------------------------------------------------
659                         //Get excpected result 
660                         al.Clear();
661                         foreach (DataRow dr in dt.Rows ) 
662                         {
663                                 if ((int)dr["ChildId"] == 1)
664                                 {
665                                         al.Add(dr);
666                                 }
667                         }
668
669                         // RowFilter = 'ChildId=1', check count
670                         dv.RowFilter = "ChildId=1";
671                         Assert.AreEqual(al.Count , dv.Count , "DV70");
672
673                         // RowFilter = 'ChildId=1', check rows
674                         drvResult = new DataRowView[dv.Count];
675                         dv.CopyTo(drvResult,0);
676                         //check that the filterd rows exists
677                         bool Succeed = true;
678                         for (int i=0; i<drvResult.Length ; i++)
679                         {
680                                 Succeed = al.Contains(drvResult[i].Row);
681                                 if (!Succeed) break;
682                         }
683                         Assert.AreEqual(true, Succeed , "DV71");
684                         //-------------------------------------------------------------
685
686                         //-------------------------------------------------------------
687                         //Get excpected result 
688                         al.Clear();
689                         foreach (DataRow dr in dt.Rows ) 
690                                 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) 
691                                         al.Add(dr);
692
693                         // RowFilter - ChildId=1 and String1='1-String1'
694                         dv.RowFilter = "ChildId=1 and String1='1-String1'";
695                         Assert.AreEqual(al.Count , dv.Count , "DV72");
696
697                         // RowFilter = ChildId=1 and String1='1-String1', check rows
698                         drvResult = new DataRowView[dv.Count];
699                         dv.CopyTo(drvResult,0);
700                         //check that the filterd rows exists
701                         Succeed = true;
702                         for (int i=0; i<drvResult.Length ; i++)
703                         {
704                                 Succeed = al.Contains(drvResult[i].Row);
705                                 if (!Succeed) break;
706                         }
707                         Assert.AreEqual(true, Succeed , "DV73");
708                         //-------------------------------------------------------------
709
710                         //EvaluateException
711                         // RowFilter - check EvaluateException
712                         try 
713                         {
714                                 dv.RowFilter = "Col=1";
715                                 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
716                         }
717                         catch (EvaluateException) {}
718                         catch (AssertionException exc) {throw  exc;}
719                         catch (Exception exc)
720                         {
721                                 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
722                         }
723
724                         //SyntaxErrorException 1
725                         // RowFilter - check SyntaxErrorException 1
726                         try 
727                         {
728                                 dv.RowFilter = "sum('something')";
729                                 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
730                         }
731                         catch (SyntaxErrorException) {}
732                         catch (AssertionException exc) {throw  exc;}
733                         catch (Exception exc)
734                         {
735                                 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
736                         }
737
738                         //SyntaxErrorException 2
739                         // RowFilter - check SyntaxErrorException 2
740                         try 
741                         {
742                                 dv.RowFilter = "HH**!";
743                                 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
744                         }
745                         catch (SyntaxErrorException) {}
746                         catch (AssertionException exc) {throw  exc;}
747                         catch (Exception exc)
748                         {
749                                 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
750                         }
751                 }
752
753                 [Test] public void RowStateFilter()
754                 {
755                         /*
756                                 Added                   A new row. 4 
757                                 CurrentRows             Current rows including unchanged, new, and modified rows. 22 
758                                 Deleted                 A deleted row. 8 
759                                 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16 
760                                 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32 
761                                 None                    None. 0 
762                                 OriginalRows    Original rows including unchanged and deleted rows. 42 
763                                 Unchanged               An unchanged row. 2 
764                          */
765
766                         //DataRowView[] drvResult = null;
767                         ArrayList al = new ArrayList();
768
769                         DataTable dt = DataProvider.CreateParentDataTable();
770
771                         //create the dataview for the table
772                         DataView dv = new DataView(dt);
773
774                         DataRow[]  drResult;
775
776                         dt.Rows[0].Delete();
777                         dt.Rows[1]["ParentId"] = 1;
778                         dt.Rows[2]["ParentId"] = 1;
779                         dt.Rows[3].Delete();
780                         dt.Rows.Add(new object[] {1,"A","B"});
781                         dt.Rows.Add(new object[] {1,"C","D"});
782                         dt.Rows.Add(new object[] {1,"E","F"});
783
784                         //---------- Added -------- 
785                         dv.RowStateFilter = DataViewRowState.Added ;
786                         drResult = GetResultRows(dt,DataRowState.Added);
787                         // Added
788                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
789
790                         //---------- CurrentRows -------- 
791                         dv.RowStateFilter = DataViewRowState.CurrentRows ;
792                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added  | DataRowState.Modified );
793                         // CurrentRows
794                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
795
796                         //---------- ModifiedCurrent -------- 
797                         dv.RowStateFilter = DataViewRowState.ModifiedCurrent  ;
798                         drResult = GetResultRows(dt,DataRowState.Modified );
799                         // ModifiedCurrent
800                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
801
802                         //---------- ModifiedOriginal -------- 
803                         dv.RowStateFilter = DataViewRowState.ModifiedOriginal   ;
804                         drResult = GetResultRows(dt,DataRowState.Modified );
805                         // ModifiedOriginal
806                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
807
808                         //---------- Deleted -------- 
809                         dv.RowStateFilter = DataViewRowState.Deleted ;
810                         drResult = GetResultRows(dt,DataRowState.Deleted );
811                         // Deleted
812                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
813                         /*
814                                         //---------- OriginalRows -------- 
815                                         dv.RowStateFilter = DataViewRowState.OriginalRows ;
816                                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
817                                                 // OriginalRows
818                                                 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
819                         */
820                 }
821
822                 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
823                 {
824                         //get expected rows
825                         ArrayList al = new ArrayList();
826                         DataRowVersion drVer = DataRowVersion.Current;
827
828                         //From MSDN -   The row the default version for the current DataRowState.
829                         //                              For a DataRowState value of Added, Modified or Current, 
830                         //                              the default version is Current. 
831                         //                              For a DataRowState of Deleted, the version is Original.
832                         //                              For a DataRowState value of Detached, the version is Proposed.
833
834                         if (    ((State & DataRowState.Added)           > 0)  
835                                 | ((State & DataRowState.Modified)      > 0)  
836                                 | ((State & DataRowState.Unchanged)     > 0) ) 
837                                 drVer = DataRowVersion.Current;
838                         if ( (State & DataRowState.Deleted)             > 0
839                                 | (State & DataRowState.Detached)       > 0 )  
840                                 drVer = DataRowVersion.Original; 
841
842                         foreach (DataRow dr in dt.Rows )
843                         {
844                                 if ( dr.HasVersion(drVer) 
845                                         //&& ((int)dr["ParentId", drVer] == 1) 
846                                         && ((dr.RowState & State) > 0 ) 
847                                         )
848                                         al.Add(dr);
849                         }
850                         DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
851                         return result; 
852                 }
853
854                 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
855                 {
856                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
857
858                         //comparing the rows by using columns ParentId and ChildId
859                         if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
860                         {
861                                 for (int i=0; i<dv.Count ; i++)
862                                 {
863                                         if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString()) 
864                                                 return false;
865                                 }
866                         }
867                         else
868                         {
869                                 for (int i=0; i<dv.Count ; i++)
870                                 {
871                                         if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()) 
872                                                 return false;
873                                 }
874                         }
875                         return true;
876                 }
877
878                 [Test] public void Sort()
879                 {
880                         DataRow[] drArrTable;
881
882                         //create the source datatable
883                         DataTable dt = DataProvider.CreateChildDataTable();
884
885                         //create the dataview for the table
886                         DataView dv = new DataView(dt);
887
888                         dv.Sort = "ParentId";
889                         drArrTable = dt.Select("","ParentId");
890                         // sort = ParentId
891                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
892
893                         dv.Sort = "ChildId";
894                         drArrTable = dt.Select("","ChildId");
895                         // sort = ChildId
896                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
897
898                         dv.Sort = "ParentId Desc, ChildId";
899                         drArrTable = dt.Select("","ParentId Desc, ChildId");
900                         // sort = ParentId Desc, ChildId
901                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
902
903                         dv.Sort = "ChildId Asc, ParentId";
904                         drArrTable = dt.Select("","ChildId Asc, ParentId");
905                         // sort = ChildId Asc, ParentId
906                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
907
908                         dv.Sort = "ChildId Asc, ChildId Desc";
909                         drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
910                         // sort = ChildId Asc, ChildId Desc
911                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
912
913                         // IndexOutOfRangeException - 1
914                         try 
915                         {
916                                 dv.Sort = "something";
917                                 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
918                         }
919                         catch (IndexOutOfRangeException) {}
920                         catch (AssertionException exc) {throw  exc;}
921                         catch (Exception exc)
922                         {
923                                 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
924                         }
925
926                         // IndexOutOfRangeException - 2
927                         try 
928                         {
929                                 dv.Sort = "ColumnId Desc Asc";
930                                 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
931                         }
932                         catch (IndexOutOfRangeException) {}
933                         catch (AssertionException exc) {throw  exc;}
934                         catch (Exception exc)
935                         {
936                                 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
937                         }
938
939                         // IndexOutOfRangeException - 3
940                         try 
941                         {
942                                 dv.Sort = "ColumnId blabla";
943                                 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
944                         }
945                         catch (IndexOutOfRangeException) {}
946                         catch (AssertionException exc) {throw  exc;}
947                         catch (Exception exc)
948                         {
949                                 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
950                         }
951                 }
952
953                 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
954                 {
955                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
956
957                         //comparing the rows by using columns ParentId and ChildId
958                         for (int i=0; i<dv.Count ; i++)
959                         {
960                                 if (    dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString() 
961                                         && 
962                                         dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
963                                         return false;
964                         }
965                         return true;
966                 }
967
968                 [Test] public void Table()
969                 {
970                         DataTable dt = new DataTable();
971                         DataView dv = new DataView();
972
973                         // DataTable=null
974                         Assert.AreEqual(null , dv.Table , "DV97");
975
976                         // DataException - bind to table with no name
977                         try 
978                         {
979                                 dv.Table = dt;
980                                 Assert.Fail("DV98: Table Failed to throw DataException");
981                         }
982                         catch (DataException) {}
983                         catch (AssertionException exc) {throw  exc;}
984                         catch (Exception exc)
985                         {
986                                 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
987                         }
988
989                         dt.TableName = "myTable";
990                         // DataTable!=null
991                         dv.Table = dt;
992                         Assert.AreEqual(dt, dv.Table , "DV100");
993
994                         // assign null to DataTable
995                         dv.Table = null; 
996                         Assert.AreEqual(null, dv.Table , "DV101");
997                 }
998
999                 [Test] public void ctor_Empty()
1000                 {
1001                         DataView dv; 
1002                         dv = new DataView();
1003
1004                         // ctor
1005                         Assert.AreEqual(false, dv == null, "DV102");
1006                 }
1007
1008                 [Test] public void ctor_DataTable()
1009                 {
1010                         DataView dv = null; 
1011                         DataTable dt = new DataTable("myTable");
1012
1013                         // ctor
1014                         dv = new DataView(dt);
1015                         Assert.AreEqual(false, dv == null, "DV103");
1016
1017                         // ctor - table
1018                         Assert.AreEqual(dt , dv.Table  , "DV104");
1019                 }
1020
1021                 [Test] public void ctor_ExpectedExceptions()
1022                 {
1023                         DataView dv = null; 
1024                         DataTable dt = new DataTable("myTable");
1025
1026                         // ctor - missing column CutomerID Exception
1027                         try 
1028                         {
1029                                 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
1030                                 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1031                                 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
1032                         }
1033                         catch (EvaluateException) {}
1034                         catch (IndexOutOfRangeException) {}
1035                         catch (AssertionException exc) {throw  exc;}
1036                         catch (Exception exc)
1037                         {
1038                                 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1039                         }
1040
1041                         dt.Columns.Add(new DataColumn("CustomerId"));
1042
1043                         // ctor - missing column Age Exception
1044                         try 
1045                         {
1046                                 //exception: System.Data.EvaluateException: Cannot find column [Age]
1047                                 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1048                                 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1049                         }
1050                         catch (IndexOutOfRangeException) {}
1051                         catch (AssertionException exc) {throw  exc;}
1052                         catch (Exception exc)
1053                         {
1054                                 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1055                         }
1056                 }
1057
1058                 [Test] public void ctor_Complex()
1059                 {
1060                         DataView dv = null; 
1061                         DataTable dt = new DataTable("myTable");
1062
1063                         dt.Columns.Add(new DataColumn("CustomerId"));
1064                         dt.Columns.Add(new DataColumn("Age"));
1065
1066                         // ctor
1067                         dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1068                         Assert.AreEqual(false , dv == null  , "DV109");
1069
1070                         // ctor - table
1071                         Assert.AreEqual(dt , dv.Table  , "DV110");
1072
1073                         // ctor - RowFilter
1074                         Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1075
1076                         // ctor - Sort
1077                         Assert.AreEqual("Age" , dv.Sort, "DV112");
1078
1079                         // ctor - RowStateFilter 
1080                         Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1081                 }
1082
1083                 [Test]
1084                 public void DataViewManager()
1085                 {
1086                         DataView dv = null; 
1087                         DataViewManager dvm = null;
1088                         DataSet ds = new DataSet();
1089                         DataTable dt = new DataTable("myTable");
1090                         ds.Tables.Add(dt);
1091
1092                         dv = dt.DefaultView;
1093
1094                         //      public DataViewManager DataViewManager {get;} - The DataViewManager that created this view. 
1095                         //      If this is the default DataView for a DataTable, the DataViewManager property returns the default DataViewManager for the DataSet.
1096                         //      Otherwise, if the DataView was created without a DataViewManager, this property is a null reference (Nothing in Visual Basic).
1097
1098                         dvm = dv.DataViewManager;
1099                         Assert.AreSame (ds.DefaultViewManager, dvm, "DV114");
1100
1101                         dv = new DataView(dt);
1102                         dvm = dv.DataViewManager;
1103                         Assert.IsNull (dvm, "DV115");
1104                         
1105                         dv = ds.DefaultViewManager.CreateDataView(dt);
1106                         Assert.AreSame (ds.DefaultViewManager, dv.DataViewManager , "DV116");
1107                 }
1108
1109                 [Test]
1110                 public void DataView_ListChangedEventTest ()
1111                 {
1112                         // Test DataView generates events, when datatable is directly modified
1113
1114                         DataTable table = new DataTable ("test");
1115                         table.Columns.Add ("col1", typeof(int));
1116                         
1117                         DataView view = new DataView (table);
1118                         
1119                         view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
1120                         
1121                         evProp = null;
1122                         table.Rows.Add (new object[] {1});
1123                         Assert.AreEqual (0, evProp.NewIndex, "#1");
1124                         Assert.AreEqual (-1, evProp.OldIndex, "#2");
1125                         Assert.AreEqual (ListChangedType.ItemAdded, evProp.lstType, "#3");
1126
1127                         evProp = null;
1128                         table.Rows[0][0] = 5;
1129                         Assert.AreEqual (0, evProp.NewIndex, "#4");
1130                         Assert.AreEqual (-1, evProp.OldIndex, "#5");
1131                         Assert.AreEqual (ListChangedType.ItemChanged, evProp.lstType, "#6");
1132
1133                         evProp = null;
1134                         table.Rows.RemoveAt (0);
1135                         Assert.AreEqual (0, evProp.NewIndex, "#7");
1136                         Assert.AreEqual (-1, evProp.OldIndex, "#8");
1137                         Assert.AreEqual (ListChangedType.ItemDeleted, evProp.lstType, "#9");
1138
1139                         table.Rows.Clear();
1140                         Assert.AreEqual (-1, evProp.NewIndex, "#10");
1141                         Assert.AreEqual (-1, evProp.OldIndex, "#11");
1142                         Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
1143                 }
1144
1145                 [Test]
1146                 public void TestDefaultValues()
1147                 {
1148                         DataView view = new DataView();
1149                         Assert.IsFalse(view.ApplyDefaultSort, "#1");
1150                         Assert.AreEqual ("", view.Sort, "#2");
1151                         Assert.AreEqual("", view.RowFilter, "#3");
1152                         Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1153                         Assert.IsTrue(view.AllowDelete, "#5");
1154                         Assert.IsTrue(view.AllowEdit, "#6");
1155                         Assert.IsTrue(view.AllowNew, "#7");
1156                 }
1157                 
1158                 [Test]
1159                 public void TestTableProperty()
1160                 {
1161                         DataTable table = new DataTable("table");
1162                         DataView view = new DataView();
1163                         view.Table = table;
1164                         Assert.AreEqual("", view.Sort, "#1");
1165                         Assert.AreEqual("", view.RowFilter, "#2");
1166                         Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1167                 }
1168
1169                 [Test]
1170                 public void TestEquals_SameTableDiffViewProp()
1171                 {
1172
1173                         DataTable table = new DataTable("table");
1174                         table.Columns.Add("col1", typeof(int));
1175                         table.Columns.Add("col2", typeof(int));
1176                         for (int i = 0; i < 5; ++i)
1177                                 table.Rows.Add(new object[] { i, 100 + i });
1178
1179                         DataView view1 = new DataView(table);
1180                         DataView view2 = new DataView(table);
1181
1182                         object obj2 = (object)view2;
1183                         Assert.IsFalse(view1.Equals(obj2), "#1");
1184
1185                         Assert.IsTrue(view1.Equals(view1), "#2");
1186                         Assert.IsTrue(view2.Equals(view1), "#3");
1187
1188                         view1.Sort = "col1 ASC";
1189                         Assert.IsFalse(view1.Equals(view2), "#4");
1190                         view2.Sort = "col1 ASC";
1191                         Assert.IsTrue(view1.Equals(view2), "#5");
1192
1193                         view1.RowFilter = "col1 > 100";
1194                         Assert.IsFalse(view1.Equals(view2), "#6");
1195                         view1.RowFilter = "";
1196                         Assert.IsTrue(view1.Equals(view2), "#7");
1197
1198                         view1.RowStateFilter = DataViewRowState.Added;
1199                         Assert.IsFalse(view1.Equals(view2), "#8");
1200                         view1.RowStateFilter = DataViewRowState.CurrentRows;
1201                         Assert.IsTrue(view1.Equals(view2), "#9");
1202
1203                         view1.AllowDelete = !view2.AllowDelete;
1204                         Assert.IsFalse(view1.Equals(view2), "#10");
1205                         view1.AllowDelete = view2.AllowDelete;
1206                         Assert.IsTrue(view1.Equals(view2), "#11");
1207
1208                         view1.AllowEdit = !view2.AllowEdit;
1209                         Assert.IsFalse(view1.Equals(view2), "#12");
1210                         view1.AllowEdit = view2.AllowEdit;
1211                         Assert.IsTrue(view1.Equals(view2), "#13");
1212
1213                         view1.AllowNew = !view2.AllowNew;
1214                         Assert.IsFalse(view1.Equals(view2), "#14");
1215                         view1.AllowNew = view2.AllowNew;
1216                         Assert.IsTrue(view1.Equals(view2), "#15");
1217
1218                         //ApplyDefaultSort doesnet affect the comparision
1219                         view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
1220                         Assert.IsTrue(view1.Equals(view2), "#16");
1221
1222                         DataTable table2 = table.Copy();
1223                         view1.Table = table2;
1224                         Assert.IsFalse(view1.Equals(view2), "#17");
1225
1226                         view1.Table = table;
1227                         //well.. sort is set to null when Table is assigned..
1228                         view1.Sort = view2.Sort;          
1229                         Assert.IsTrue(view1.Equals(view2), "#18"); 
1230                 }
1231
1232                 [Test]
1233                 public void ToTable_SimpleTest()
1234                 {
1235                         DataSet ds = new DataSet();
1236                         ds.Tables.Add("table");
1237                         ds.Tables[0].Columns.Add("col1", typeof(int));
1238                         ds.Tables[0].Columns.Add("col2", typeof(int), "sum(col1)");
1239                         ds.Tables[0].Columns.Add("col3", typeof(int));
1240                         ds.Tables[0].Columns[2].AutoIncrement = true;
1241
1242                         ds.Tables[0].Rows.Add(new object[] { 1 });
1243                         ds.Tables[0].Rows.Add(new object[] { 2 });
1244
1245                         ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0] };
1246                         
1247                         DataView view = new DataView(ds.Tables[0]);
1248                         DataTable table = view.ToTable();
1249                         
1250                         // The rule seems to be : Copy any col property that doesent
1251                         // involve/depend on other columns..
1252                         // Constraints and PrimaryKey info not copied over
1253                         Assert.AreEqual(0, table.PrimaryKey.Length, "#1");
1254                         Assert.AreEqual(0, table.Constraints.Count, "#2");
1255                         // AllowDBNull state is maintained by ms.net
1256                         Assert.IsFalse(table.Columns[0].AllowDBNull, "#3");
1257                         Assert.IsTrue(table.Columns[2].AllowDBNull, "#4");
1258                         // Expression is not copied over by ms.net
1259                         Assert.AreEqual("", table.Columns[1].Expression, "#5");
1260                         // AutoIncrement state is maintained by ms.net
1261                         Assert.IsTrue(table.Columns[2].AutoIncrement, "#6");
1262                         
1263                         Assert.IsFalse (ds.Tables[0] == table, "#7");
1264
1265                         Assert.AreEqual(ds.Tables [0].TableName, table.TableName, "#8");
1266                         Assert.AreEqual(ds.Tables [0].Columns.Count, table.Columns.Count, "#9 Col Count");
1267                         Assert.AreEqual(ds.Tables [0].Rows.Count, table.Rows.Count, "#10");
1268                         
1269                         for (int i = 0; i < table.Columns.Count; ++i) {
1270                                 Assert.AreEqual(ds.Tables [0].Columns[i].ColumnName, table.Columns[i].ColumnName, "10_"+i);
1271                                 Assert.AreEqual(ds.Tables [0].Columns[i].DataType, table.Columns[i].DataType, "11_"+i);
1272                                 for (int j = 0; j < table.Rows.Count; ++j)
1273                                         Assert.AreEqual(ds.Tables [0].Rows[j][i], table.Rows[j][i], "#12_"+i+"_"+j);
1274                         }
1275                         
1276                         DataTable table1 = view.ToTable("newtable");
1277                         Assert.AreEqual("newtable", table1.TableName, "#13");
1278                 }
1279                 
1280                 [Test]
1281                 public void ToTableTest_DataValidity ()
1282                 {              
1283                         DataTable table = new DataTable();
1284                         table.Columns.Add("col1", typeof(int));
1285                         table.Columns.Add("col2", typeof(int));
1286                         table.Columns.Add("col3", typeof(int));
1287                         
1288                         for (int i = 0; i < 5; ++i) {
1289                                 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1290                                 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1291                         }
1292                         
1293                         table.AcceptChanges();
1294                         DataView view = new DataView(table);
1295                         try {
1296                                 DataTable newTable = view.ToTable (false, null);
1297                         } catch (ArgumentNullException e) {
1298                                 // Never premise English.
1299                                 //Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine + 
1300                                 //              "Parameter name: columnNames", e.Message, "#1");
1301                         }
1302                         DataTable newTable1 = view.ToTable(false, new string[] { });
1303                         Assert.AreEqual(10, newTable1.Rows.Count, "#3");
1304
1305                         newTable1 = view.ToTable(true, new string[] {});
1306                         Assert.AreEqual(3, newTable1.Columns.Count, "#4");
1307                         Assert.AreEqual(5, newTable1.Rows.Count, "#5");
1308                         
1309                         table.Rows.Add(new object[] { 1, 100, 100 });
1310
1311                         newTable1 = view.ToTable(true, new string[] {});
1312                         Assert.AreEqual(3, newTable1.Columns.Count, "#6");
1313                         Assert.AreEqual(6, newTable1.Rows.Count, "#7");
1314                         
1315                         newTable1 = view.ToTable(true, new string[] {"col1"});
1316                         Assert.AreEqual(1, newTable1.Columns.Count, "#8");
1317                         Assert.AreEqual(5, newTable1.Rows.Count, "#9");
1318                         
1319                         newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
1320                         Assert.AreEqual(2, newTable1.Columns.Count, "#10");
1321                         Assert.AreEqual(6, newTable1.Rows.Count, "#11");
1322                         
1323                         for (int i = 0; i < newTable1.Rows.Count; ++i)
1324                                 Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
1325
1326                         view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
1327                         Assert.AreEqual (0, view.Count, "#12");
1328
1329                         newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
1330                         Assert.AreEqual (0, newTable1.Rows.Count, "#13");
1331                         
1332                         table.Rows.Add (new object[] {10, 1, 1});
1333                         table.Rows.Add (new object[] {10, 1, 3});
1334                         table.Rows.Add (new object[] {10, 1, 2});
1335
1336                         Assert.AreEqual (3, view.Count, "#14");
1337                         view.Sort = "col1 asc, col2 asc, col3 desc";
1338                         newTable1 =  view.ToTable (true, new string[] {"col1", "col3"});
1339                         Assert.AreEqual (3, newTable1.Rows.Count, "#14");
1340                         Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
1341                         Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
1342                         Assert.AreEqual (1, newTable1.Rows [2][1], "#17");
1343                 }
1344         }
1345 }