Added a bulk of tests from Mainsoft repository.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest2.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 DataTableTest2
37         {
38                 private bool _EventTriggered = false;
39                 private bool EventRaised = false;
40                 private bool EventValues = false;
41
42                 class ProtectedTestClass : DataTable
43                 {
44                         public ProtectedTestClass() : base()
45                         {
46                                 this.Columns.Add("Id",typeof(int));
47                                 this.Columns.Add("Value",typeof(string));
48                                 this.Rows.Add(new object[] {1,"one"});
49                                 this.Rows.Add(new object[] {2,"two"});
50                                 this.AcceptChanges();
51                         }
52
53                         public void OnColumnChanged_Test()
54                         {
55                                 OnColumnChanged(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue")); 
56                         }
57
58                         public void OnColumnChanging_Test()
59                         {
60                                 OnColumnChanging(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue")); 
61                         }
62
63                         public void OnRemoveColumn_Test()
64                         {
65                                 OnRemoveColumn(this.Columns[0]); 
66                         }
67
68                         public DataTable CreateInstance_Test()
69                         {
70                                 return CreateInstance();
71                         }
72
73                         public void OnRowChanged_Test(DataRowAction drAction )\r
74                         {\r
75                                 base.OnRowChanged(new DataRowChangeEventArgs(this.Rows[0],drAction )); \r
76                         }
77
78                         public void OnRowChanging_Test(DataRowAction drAction )\r
79                         {\r
80                                 base.OnRowChanging(new DataRowChangeEventArgs(this.Rows[0],drAction )); \r
81                         }
82
83                         public void OnRowDeleted_Test(DataRowAction drAction )\r
84                         {\r
85                                 base.OnRowDeleted(new DataRowChangeEventArgs(this.Rows[0],drAction )); \r
86                         }
87
88                         public void OnRowDeleting_Test(DataRowAction drAction )\r
89                         {\r
90                                 base.OnRowDeleting(new DataRowChangeEventArgs(this.Rows[0],drAction )); \r
91                         }
92                 }
93
94                 [Test] public void AcceptChanges()
95                 {
96                         String sNewValue = "NewValue";
97                         DataRow drModified,drDeleted,drAdded;
98                         DataTable dt = DataProvider.CreateParentDataTable();
99
100                         drModified = dt.Rows[0];
101                         drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
102
103                         drDeleted = dt.Rows[1];
104                         drDeleted.Delete();             //DataRowState =  Deleted
105
106                         drAdded = dt.NewRow();                  
107                         dt.Rows.Add(drAdded);   //DataRowState =  Added
108
109                         dt.AcceptChanges();
110
111                         // AcceptChanges - Unchanged1
112                         Assert.AreEqual(DataRowState.Unchanged , drModified.RowState  , "DT1");
113
114                         // AcceptChanges - Current
115                         Assert.AreEqual(sNewValue , drModified[1,DataRowVersion.Current] , "DT2");
116
117                         // AcceptChanges - Unchanged2
118                         Assert.AreEqual(DataRowState.Unchanged , drAdded.RowState  , "DT3");
119
120                         // AcceptChanges - Detached
121                         Assert.AreEqual(DataRowState.Detached  , drDeleted.RowState  , "DT4");
122                 }
123
124                 [Test] public void ChildRelations()
125                 {
126                         DataTable dtChild,dtParent;
127                         DataSet ds = new DataSet();
128                         //Create tables
129                         dtChild = DataProvider.CreateChildDataTable();
130                         dtParent= DataProvider.CreateParentDataTable(); 
131                         //Add tables to dataset
132                         ds.Tables.Add(dtChild);
133                         ds.Tables.Add(dtParent);
134
135                         DataRelationCollection drlCollection;
136                         DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
137
138                         // Checking ChildRelations - default value
139                         //Check default
140                         drlCollection = dtParent.ChildRelations; 
141                         Assert.AreEqual(0, drlCollection.Count  , "DT5");
142
143                         ds.Relations.Add(drl);
144                         drlCollection = dtParent.ChildRelations; 
145
146                         // Checking ChildRelations Count
147                         Assert.AreEqual(1, drlCollection.Count  , "DT6");
148
149                         // Checking ChildRelations Value
150                         Assert.AreEqual(drl, drlCollection[0] , "DT7");
151                 }
152
153                 [Test] public void Clear()
154                 {
155                         DataTable dt = DataProvider.CreateParentDataTable();
156                         dt.Clear();
157                         // Clear
158                         Assert.AreEqual(0, dt.Rows.Count  , "DT8");
159                 }
160
161                 [Test] public void Clone()
162                 {
163                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
164                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
165                         dt2.Columns[0].DefaultValue=7;
166
167                         dt1 = dt2.Clone();
168
169                         for (int i=0; i<dt2.Constraints.Count; i++)
170                         {
171                                 // Clone - Constraints[{0}],i)
172                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT9");
173                         }
174
175                         for (int i=0; i<dt2.Columns.Count; i++)
176                         {
177                                 // Clone - Columns[{0}].ColumnName,i)
178                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT10");
179
180                                 // Clone - Columns[{0}].DataType,i)
181                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT11");
182                         }
183                 }
184
185                 [Test] public void ColumnChanged()
186                 {
187                         DataTable dt = DataProvider.CreateParentDataTable();
188
189                         dt.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
190
191                         _EventTriggered=false;
192                         // ColumnChanged - EventTriggered
193                         dt.Rows[0][1] = "NewValue";
194                         Assert.AreEqual(true , _EventTriggered , "DT12");
195
196                         _EventTriggered=false;
197                         dt.ColumnChanged -= new DataColumnChangeEventHandler( Column_Changed );
198                         // ColumnChanged - NO EventTriggered
199                         dt.Rows[0][1] = "VeryNewValue";
200                         Assert.AreEqual(false , _EventTriggered , "DT13");
201                 }
202
203                 private void Column_Changed( object sender, DataColumnChangeEventArgs e )
204                 {
205                         _EventTriggered = true;
206                 }
207
208                 [Test] public void ColumnChanging()
209                 {
210                         DataTable dt = DataProvider.CreateParentDataTable();
211
212                         dt.ColumnChanging  += new DataColumnChangeEventHandler( Column_Changeding );
213
214                         _EventTriggered=false;
215                         // ColumnChanged - EventTriggered
216                         dt.Rows[0][1] = "NewValue";
217                         Assert.AreEqual(true , _EventTriggered , "DT14");
218
219                         _EventTriggered=false;
220                         dt.ColumnChanging  -= new DataColumnChangeEventHandler( Column_Changeding );
221                         // ColumnChanged - NO EventTriggered
222                         dt.Rows[0][1] = "VeryNewValue";
223                         Assert.AreEqual(false , _EventTriggered , "DT15");
224                 }
225
226                 private void Column_Changeding( object sender, DataColumnChangeEventArgs e )
227                 {
228                         _EventTriggered = true;
229                 }
230
231                 [Test] public void Columns()
232                 {
233                         DataTable dtParent;
234                         DataColumnCollection dcl;
235                         dtParent= DataProvider.CreateParentDataTable(); 
236
237                         dcl = dtParent.Columns; 
238
239                         // Checking ColumnsCollection != null 
240                         Assert.AreEqual(false, dcl == null  , "DT16");
241
242                         // Checking ColumnCollection Count  1
243                         Assert.AreEqual(6, dcl.Count  , "DT17");
244
245                         // Checking ColumnCollection Count 2
246                         dtParent.Columns.Add(new DataColumn("Test"));
247                         Assert.AreEqual(7, dcl.Count  , "DT18");
248
249                         // Checking ColumnCollection - get columnn by different case
250                         DataColumn tmp = dtParent.Columns["TEST"];
251                         Assert.AreEqual(dtParent.Columns["Test"], tmp  , "DT19");
252
253                         // Checking ColumnCollection colummn name case sensetive
254                         dtParent.Columns.Add(new DataColumn("test"));
255                         Assert.AreEqual(8, dcl.Count  , "DT20");
256
257                         // Checking ColumnCollection - get columnn by different case,ArgumentException
258                         try
259                         {
260                                 DataColumn tmp1 = dtParent.Columns["TEST"];
261                                 Assert.Fail("DT21: indexer Failed to throw ArgumentException");
262                         }
263                         catch (ArgumentException) {}
264                         catch (AssertionException exc) {throw  exc;}
265                         catch (Exception exc)
266                         {
267                                 Assert.Fail("DT22: Indexer. Wrong exception type. Got:" + exc);
268                         }
269                 }
270
271                 [Test] public void Compute()
272                 {
273                         DataTable dt = DataProvider.CreateChildDataTable();
274
275                         //Get expected
276                         DataRow[] drArr = dt.Select("ParentId=1");
277                         Int64 iExSum = 0;
278                         foreach (DataRow dr in drArr)
279                         {
280                                 iExSum += (int)dr["ChildId"];
281                         }
282                         object objCompute=null;
283                         // Compute - sum values
284                         objCompute = dt.Compute("Sum(ChildId)","ParentId=1");
285                         Assert.AreEqual(Int64.Parse(objCompute.ToString()) , Int64.Parse(iExSum.ToString()), "DT23");
286
287                         // Compute - sum type
288                         Assert.AreEqual(typeof(Int64).FullName , objCompute.GetType().FullName, "DT24");
289
290                         //get expected
291                         double iExAvg = 0;
292                         drArr = dt.Select("ParentId=5");
293                         foreach (DataRow dr in drArr)
294                         {
295                                 iExAvg += (double)dr["ChildDouble"];
296                         }
297                         iExAvg = iExAvg / drArr.Length;
298
299                         // Compute - Avg value
300                         objCompute = dt.Compute("Avg(ChildDouble)","ParentId=5");
301                         Assert.AreEqual(double.Parse(objCompute.ToString()) , double.Parse(iExAvg.ToString()), "DT25");
302
303                         // Compute - Avg type
304                         Assert.AreEqual(typeof(double).FullName , objCompute.GetType().FullName, "DT26");
305                 }
306
307                 [Test] public void Constraints()
308                 {
309                         DataTable dtParent;
310                         ConstraintCollection consColl;
311                         dtParent= DataProvider.CreateParentDataTable(); 
312
313                         consColl = dtParent.Constraints;
314                         // Checking Constraints  != null 
315                         Assert.AreEqual(false, consColl == null  , "DT27");
316
317                         // Checking Constraints Count
318                         Assert.AreEqual(0, consColl.Count  , "DT28");
319
320                         // Checking Constraints Count
321                         //Add primary key
322                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
323                         Assert.AreEqual(1, consColl.Count   , "DT29");
324                 }
325
326                 [Test] public void Copy()
327                 {
328                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
329                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
330                         dt2.Columns[0].DefaultValue=7;
331
332                         dt1 = dt2.Copy();
333
334                         for (int i=0; i<dt2.Constraints.Count; i++)
335                         {
336                                 // Copy - Constraints[{0}],i)
337                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT30");
338                         }
339
340                         for (int i=0; i<dt2.Columns.Count; i++)
341                         {
342                                 // Copy - Columns[{0}].ColumnName,i)
343                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT31");
344
345                                 // Copy - Columns[{0}].DataType,i)
346                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT32");
347                         }
348
349                         DataRow[] drArr1,drArr2;
350                         drArr1 = dt1.Select("");
351                         drArr2 = dt2.Select("");
352                         for (int i=0; i<drArr1.Length ; i++)
353                         {
354                                 // Copy - Data [ParentId]{0} ,i)
355                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
356                                 // Copy - Data [String1]{0} ,i)
357                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
358                                 // Copy - Data [String2]{0} ,i)
359                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
360                         }
361                 }
362
363                 [Test] public void CreateInstance()
364                 {
365                         // CreateInstance
366                         ProtectedTestClass C = new ProtectedTestClass();
367                         DataTable dt = C.CreateInstance_Test();
368                         Assert.AreEqual(true , dt != null , "DT36");
369                 }
370
371                 [Test] public void DataSet()
372                 {
373                         DataTable dtParent;
374                         DataSet ds;
375                         dtParent= DataProvider.CreateParentDataTable(); 
376
377                         ds = dtParent.DataSet; 
378
379                         // Checking DataSet == null
380                         Assert.AreEqual(null, ds, "DT37");
381
382                         // Checking DataSet != null
383                         ds = new DataSet("MyDataSet");
384                         ds.Tables.Add(dtParent);
385                         Assert.AreEqual(true, dtParent.DataSet != null , "DT38");
386
387                         // Checking DataSet Name
388                         Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName  , "DT39");
389                 }
390
391                 [Test] public void DefaultView()
392                 {
393                         DataTable dtParent;
394                         DataView dv;
395                         dtParent= DataProvider.CreateParentDataTable(); 
396                         dv = dtParent.DefaultView ;
397
398                         // Checking DataView != null
399                         Assert.AreEqual(true, dv != null, "DT40");
400                 }
401
402                 [Test] public void EndLoadData()
403                 {
404                         DataTable dt = DataProvider.CreateParentDataTable();
405                         dt.Columns[0].AllowDBNull = false;
406
407                         // EndLoadData
408                         dt.BeginLoadData();
409                         dt.LoadDataRow(new object[] {null,"A","B"},false);
410
411                         try
412                         {
413                                 //ConstraintException will be throw
414                                 dt.EndLoadData();
415                                 Assert.Fail("DT41: EndLoadData Failed to throw ConstraintException");
416                         }
417                         catch (ConstraintException) {}
418                         catch (AssertionException exc) {throw  exc;}
419                         catch (Exception exc)
420                         {
421                                 Assert.Fail("DT42: EndLoadData Wrong exception type. Got:" + exc);
422                         }
423                 }
424
425                 [Test] public void GetChanges()
426                 {
427                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
428                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
429                         dt2.Columns[0].DefaultValue=7;
430
431                         //make some changes
432                         dt2.Rows[0].Delete();
433                         dt2.Rows[1].Delete();
434                         dt2.Rows[2].Delete();
435                         dt2.Rows[3].Delete();
436
437                         dt1 = dt2.GetChanges();
438
439                         for (int i=0; i<dt2.Constraints.Count; i++)
440                         {
441                                 // GetChanges - Constraints[{0}],i)
442                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT43");
443                         }
444
445                         for (int i=0; i<dt2.Columns.Count; i++)
446                         {
447                                 // GetChanges - Columns[{0}].ColumnName,i)
448                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT44");
449
450                                 // GetChanges - Columns[{0}].DataType,i)
451                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT45");
452                         }
453
454                         DataRow[] drArr1,drArr2;
455
456                         drArr1 = dt1.Select("","",DataViewRowState.Deleted );
457                         drArr2 = dt2.Select("","",DataViewRowState.Deleted );
458
459                         for (int i=0; i<drArr1.Length ; i++)
460                         {
461                                 // GetChanges - Data [ParentId]{0} ,i)
462                                 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
463                                 // GetChanges - Data [String1]{0} ,i)
464                                 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original],  "DT47");
465                                 // GetChanges - Data [String2]{0} ,i)
466                                 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original],  "DT48");
467                         }
468                 }
469
470                 [Test] public void GetChanges_ByDataRowState()
471                 {
472                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
473                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
474                         dt2.Columns[0].DefaultValue=7;
475
476                         //make some changes
477                         dt2.Rows[0].Delete(); //DataRowState.Deleted 
478                         dt2.Rows[1].Delete(); //DataRowState.Deleted 
479                         dt2.Rows[2].BeginEdit();
480                         dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified 
481                         dt2.Rows[2].EndEdit();
482
483                         dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added 
484
485                         // *********** Checking GetChanges - DataRowState.Deleted ************
486                         dt1=null;
487                         dt1 = dt2.GetChanges(DataRowState.Deleted);
488                         CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
489                         DataRow[] drArr1,drArr2;
490                         drArr1 = dt1.Select("","",DataViewRowState.Deleted );
491                         drArr2 = dt2.Select("","",DataViewRowState.Deleted );
492
493                         for (int i=0; i<drArr1.Length ; i++)
494                         {
495                                 // GetChanges(Deleted) - Data [ParentId]{0} ,i)
496                                 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original],  "DT49");
497                                 // GetChanges(Deleted) - Data [String1]{0} ,i)
498                                 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original],  "DT50");
499                                 // GetChanges(Deleted) - Data [String2]{0} ,i)
500                                 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original],  "DT51");
501                         }
502
503                         // *********** Checking GetChanges - DataRowState.Modified ************
504                         dt1=null;
505                         dt1 = dt2.GetChanges(DataRowState.Modified);
506                         CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
507                         drArr1 = dt1.Select("","");
508                         drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
509
510                         for (int i=0; i<drArr1.Length ; i++)
511                         {
512                                 // GetChanges(Modified) - Data [ParentId]{0} ,i)
513                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT52");
514                                 // GetChanges(Modified) - Data [String1]{0} ,i)
515                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT53");
516                                 // GetChanges(Modified) - Data [String2]{0} ,i)
517                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT54");
518                         }
519
520                         // *********** Checking GetChanges - DataRowState.Added ************
521                         dt1=null;
522                         dt1 = dt2.GetChanges(DataRowState.Added);
523                         CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
524                         drArr1 = dt1.Select("","");
525                         drArr2 = dt2.Select("","",DataViewRowState.Added );
526
527                         for (int i=0; i<drArr1.Length ; i++)
528                         {
529                                 // GetChanges(Added) - Data [ParentId]{0} ,i)
530                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT55");
531                                 // GetChanges(Added) - Data [String1]{0} ,i)
532                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT56");
533                                 // GetChanges(Added) - Data [String2]{0} ,i)
534                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT57");
535                         }
536
537                         // *********** Checking GetChanges - DataRowState.Unchanged  ************
538                         dt1=null;
539                         dt1 = dt2.GetChanges(DataRowState.Unchanged);
540                         CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
541                         drArr1 = dt1.Select("","");
542                         drArr2 = dt2.Select("","",DataViewRowState.Unchanged  );
543
544                         for (int i=0; i<drArr1.Length ; i++)
545                         {
546                                 // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
547                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT58");
548                                 // GetChanges(Unchanged) - Data [String1]{0} ,i)
549                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT59");
550                                 // GetChanges(Unchanged) - Data [String2]{0} ,i)
551                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT60");
552                         }
553                 }
554
555                 private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
556                 {
557                         for (int i=0; i<dt2.Constraints.Count; i++)
558                         {
559                                 // GetChanges - Constraints[{0}] - {1},i,Description)
560                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT61");
561                         }
562
563                         for (int i=0; i<dt2.Columns.Count; i++)
564                         {
565                                 // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
566                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT62");
567
568                                 // GetChanges - Columns[{0}].DataType {1},i,Description)
569                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT63");
570                         }
571                 }
572
573                 [Test] public void GetErrors()
574                 {
575                         DataTable dt = DataProvider.CreateParentDataTable();
576                         DataRow[] drArr = new DataRow[3];
577                         drArr[0] = dt.Rows[0];
578                         drArr[1] = dt.Rows[2];
579                         drArr[2] = dt.Rows[5];
580
581                         drArr[0].RowError = "Error1";
582                         drArr[1].RowError = "Error2";
583                         drArr[2].RowError = "Error3";
584
585                         // GetErrors
586                         Assert.AreEqual(dt.GetErrors(), drArr , "DT64");
587                 }
588
589                 [Test] public new void GetHashCode()
590                 {
591                         DataTable dt = DataProvider.CreateParentDataTable();
592                         int iHashCode;
593                         iHashCode = dt.GetHashCode();        
594
595                         for (int i=0; i<10 ;i++)
596                         {
597                                 // HashCode - i= + i.ToString()
598                                 Assert.AreEqual(dt.GetHashCode()  , iHashCode , "DT65");
599                         }
600                 }
601
602                 [Test] public new void GetType()
603                 {
604                         DataTable dt = DataProvider.CreateParentDataTable();
605                         Type tmpType = typeof(DataTable);
606
607                         // GetType
608                         Assert.AreEqual(tmpType,  dt.GetType() , "DT66");
609                 }
610
611                 [Test] public void HasErrors()
612                 {
613                         DataTable dtParent;
614                         dtParent= DataProvider.CreateParentDataTable(); 
615
616                         // Checking HasErrors default 
617                         Assert.AreEqual(false, dtParent.HasErrors , "DT67");
618
619                         // Checking HasErrors Get 
620                         dtParent.Rows[0].RowError = "Error on row 0";
621                         dtParent.Rows[2].RowError = "Error on row 2";
622                         Assert.AreEqual(true, dtParent.HasErrors , "DT68");
623                 }
624
625                 [Test] public void ImportRow()
626                 {
627                         DataTable dt1,dt2;
628                         dt1 = DataProvider.CreateParentDataTable();
629                         dt2 = DataProvider.CreateParentDataTable();
630                         DataRow dr = dt2.NewRow();
631                         dr.ItemArray = new object[] {99,"",""};
632                         dt2.Rows.Add(dr);
633
634                         // ImportRow - Values
635                         dt1.ImportRow(dr);
636                         Assert.AreEqual(dr.ItemArray,  dt1.Rows[dt1.Rows.Count-1].ItemArray  , "DT69");
637
638                         // ImportRow - DataRowState
639                         Assert.AreEqual(dr.RowState ,  dt1.Rows[dt1.Rows.Count-1].RowState , "DT70");
640                 }
641
642                 [Test] public void LoadDataRow()
643                 {
644                         DataTable dt;
645                         DataRow dr;
646                         dt = DataProvider.CreateParentDataTable();
647                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};        //add ParentId as Primary Key
648                         dt.Columns["String1"].DefaultValue = "Default";
649
650                         dr = dt.Select("ParentId=1")[0];
651
652                         //Update existing row without accept changes
653                         dt.BeginLoadData();
654                         dt.LoadDataRow(new object[] {1,null,"Changed"},false);
655                         dt.EndLoadData();
656
657                         // LoadDataRow(update1) - check column String1
658                         Assert.AreEqual(dr["String1"], dt.Columns["String1"].DefaultValue   , "DT71");
659
660                         // LoadDataRow(update1) - check column String2
661                         Assert.AreEqual(dr["String2"], "Changed", "DT72");
662
663                         // LoadDataRow(update1) - check row state
664                         Assert.AreEqual(DataRowState.Modified , dr.RowState , "DT73");
665
666                         //Update existing row with accept changes
667                         dr = dt.Select("ParentId=2")[0];
668
669                         dt.BeginLoadData();
670                         dt.LoadDataRow(new object[] {2,null,"Changed"},true);
671                         dt.EndLoadData();
672
673                         // LoadDataRow(update2) - check row state
674                         Assert.AreEqual(DataRowState.Unchanged  , dr.RowState , "DT74");
675
676                         //Add New row without accept changes
677                         dt.BeginLoadData();
678                         dt.LoadDataRow(new object[] {99,null,"Changed"},false);
679                         dt.EndLoadData();
680
681                         // LoadDataRow(insert1) - check column String2
682                         dr = dt.Select("ParentId=99")[0];
683                         Assert.AreEqual("Changed" , dr["String2"] , "DT75");
684
685                         // LoadDataRow(insert1) - check row state
686                         Assert.AreEqual(DataRowState.Added   , dr.RowState , "DT76");
687
688                         //Add New row with accept changes
689                         dt.BeginLoadData();
690                         dt.LoadDataRow(new object[] {100,null,"Changed"},true);
691                         dt.EndLoadData();
692
693                         // LoadDataRow(insert2) - check row and values
694                         dr = dt.Select("ParentId=100")[0];
695                         Assert.AreEqual("Changed" , dr["String2"] , "DT77");
696
697                         // LoadDataRow(insert2) - check row state
698                         Assert.AreEqual(DataRowState.Unchanged    , dr.RowState , "DT78");
699                 }
700
701                 [Test] public void Locale()
702                 {
703                         DataTable dtParent;
704                         DataSet ds = new DataSet("MyDataSet");
705
706                         dtParent= DataProvider.CreateParentDataTable(); 
707                         ds.Tables.Add(dtParent);
708                         System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
709
710                         // Checking Locale default from system 
711                         Assert.AreEqual(culInfo, dtParent.Locale  , "DT79");
712
713                         // Checking Locale default from dataset 
714                         culInfo = new System.Globalization.CultureInfo("fr"); // = French
715                         ds.Locale = culInfo;
716                         Assert.AreEqual(culInfo , dtParent.Locale , "DT80");
717
718                         // Checking Locale get/set 
719                         culInfo = new System.Globalization.CultureInfo("fr"); // = French
720                         dtParent.Locale = culInfo ;
721                         Assert.AreEqual(culInfo , dtParent.Locale , "DT81");
722                 }
723
724                 [Test] public void MinimumCapacity()
725                 {
726                         //                              i get default=50, according to MSDN the value should be 25 
727                         //                              // Checking MinimumCapacity default = 25 
728                         //                              Assert.AreEqual(25, dtParent.MinimumCapacity , "DT82");
729                         //                              EndCase(null);
730                         DataTable dt = new DataTable();
731
732                         // Checking MinimumCapacity get/set int.MaxValue 
733                         dt.MinimumCapacity = int.MaxValue; 
734                         Assert.AreEqual(int.MaxValue, dt.MinimumCapacity  , "DT83");
735
736                         // Checking MinimumCapacity get/set 0
737                         dt.MinimumCapacity = 0; 
738                         Assert.AreEqual(0, dt.MinimumCapacity  , "DT84");
739
740                         //                              // Checking MinimumCapacity get/set int.MinValue 
741                         //                              dtParent.MinimumCapacity = int.MinValue ; 
742                         //                              Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity  , "DT85");
743                         //                              EndCase(null);
744                 }
745
746                 [Test] public void Namespace()
747                 {
748                         DataTable dtParent = new DataTable();
749
750                         // Checking Namespace default
751                         Assert.AreEqual(String.Empty, dtParent.Namespace, "DT86");
752
753                         // Checking Namespace set/get
754                         String s = "MyNamespace";
755                         dtParent.Namespace=s;
756                         Assert.AreEqual(s, dtParent.Namespace, "DT87");
757                 }
758
759                 [Test] public void NewRow()
760                 {
761                         DataTable dt;
762                         DataRow dr;
763                         dt = DataProvider.CreateParentDataTable();
764
765                         // NewRow
766                         dr = dt.NewRow();
767                         Assert.AreEqual(true , dr != null , "DT88");
768                 }
769
770                 [Test] public void OnColumnChanged()
771                 {
772                         ProtectedTestClass dt = new ProtectedTestClass(); 
773
774                         EventRaised = false;
775                         dt.OnColumnChanged_Test();
776                         // OnColumnChanged Event 1
777                         Assert.AreEqual(false , EventRaised , "DT89");
778                         EventRaised = false;
779                         EventValues = false;
780                         dt.ColumnChanged += new DataColumnChangeEventHandler(OnColumnChanged_Handler);
781                         dt.OnColumnChanged_Test();
782                         // OnColumnChanged Event 2
783                         Assert.AreEqual(true , EventRaised , "DT90");
784                         // OnColumnChanged Values
785                         Assert.AreEqual(true , EventValues , "DT91");
786                         dt.ColumnChanged -= new DataColumnChangeEventHandler(OnColumnChanged_Handler);
787                 }
788
789                 private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
790                 {
791                         DataTable dt = (DataTable)sender;
792                         if ( (e.Column.Equals(dt.Columns["Value"])      )       &&
793                                 (e.Row.Equals(dt.Rows[0])                               )       &&
794                                 (e.ProposedValue.Equals("NewValue"))    )
795                         {
796                                 EventValues = true;
797                         }
798                         else
799                         {
800                                 EventValues = false;
801                         }
802                         EventRaised = true;
803                 }
804
805                 [Test] public void OnColumnChanging()
806                 {
807                         ProtectedTestClass dt = new ProtectedTestClass(); 
808
809                         EventRaised = false;
810                         dt.OnColumnChanging_Test();
811                         // OnColumnChanging Event 1
812                         Assert.AreEqual(false , EventRaised , "DT92");
813                         EventRaised = false;
814                         EventValues = false;
815                         dt.ColumnChanging  += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
816                         dt.OnColumnChanging_Test();
817                         // OnColumnChanging Event 2
818                         Assert.AreEqual(true , EventRaised , "DT93");
819                         // OnColumnChanging Values
820                         Assert.AreEqual(true , EventValues , "DT94");
821                         dt.ColumnChanging -= new DataColumnChangeEventHandler(OnColumnChanging_Handler);
822                 }
823
824                 private void OnColumnChanging_Handler(Object sender,DataColumnChangeEventArgs e)
825                 {
826                         DataTable dt = (DataTable)sender;
827                         if ( (e.Column.Equals(dt.Columns["Value"])      )       &&
828                                 (e.Row.Equals(dt.Rows[0])                               )       &&
829                                 (e.ProposedValue.Equals("NewValue"))    )
830                         {
831                                 EventValues = true;
832                         }
833                         else
834                         {
835                                 EventValues = false;
836                         }
837                         EventRaised = true;
838                 }
839
840                 [Test] public void OnRemoveColumn()
841                 {
842                         ProtectedTestClass dt = new ProtectedTestClass(); 
843
844                         // OnRemoveColumn 
845                         dt.OnRemoveColumn_Test();
846                 }
847
848                 [Test] public void ParentRelations()
849                 {
850                         DataTable dtChild,dtParent;
851                         DataSet ds = new DataSet();
852                         //Create tables
853                         dtChild = DataProvider.CreateChildDataTable();
854                         dtParent= DataProvider.CreateParentDataTable(); 
855                         //Add tables to dataset
856                         ds.Tables.Add(dtChild);
857                         ds.Tables.Add(dtParent);
858
859                         DataRelationCollection drlCollection;
860                         DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
861
862                         // Checking ParentRelations - default value
863                         //Check default
864                         drlCollection = dtChild.ParentRelations; 
865                         Assert.AreEqual(0, drlCollection.Count  , "DT96");
866
867                         ds.Relations.Add(drl);
868                         drlCollection = dtChild.ParentRelations; 
869
870                         // Checking ParentRelations Count
871                         Assert.AreEqual(1, drlCollection.Count  , "DT97");
872
873                         // Checking ParentRelations Value
874                         Assert.AreEqual(drl, drlCollection[0] , "DT98");
875                 }
876
877                 [Test] public void Prefix()
878                 {
879                         DataTable dtParent = new DataTable();
880
881                         // Checking Prefix default
882                         Assert.AreEqual(String.Empty, dtParent.Prefix , "DT99");
883
884                         // Checking Prefix set/get
885                         String s = "MyPrefix";
886                         dtParent.Prefix=s;
887                         Assert.AreEqual(s, dtParent.Prefix, "DT100");
888                 }
889
890                 [Test] public void RejectChanges()
891                 {
892                         String sNewValue = "NewValue";
893                         DataRow drModified,drDeleted,drAdded;
894                         DataTable dt = DataProvider.CreateParentDataTable();
895
896                         drModified = dt.Rows[0];
897                         drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
898
899                         drDeleted = dt.Rows[1];
900                         drDeleted.Delete();             //DataRowState =  Deleted
901
902                         drAdded = dt.NewRow();                  
903                         dt.Rows.Add(drAdded);   //DataRowState =  Added
904
905                         dt.RejectChanges();
906
907                         // RejectChanges - Unchanged1
908                         Assert.AreEqual(DataRowState.Unchanged , drModified.RowState  , "DT101");
909
910                         // RejectChanges - Unchanged2
911                         Assert.AreEqual(DataRowState.Detached , drAdded.RowState  , "DT102");
912
913                         // RejectChanges - Detached
914                         Assert.AreEqual(DataRowState.Unchanged   , drDeleted.RowState  , "DT103");
915                 }                       
916
917                 [Test] public void Reset()
918                 {
919                         DataTable dt1 = DataProvider.CreateParentDataTable();
920                         DataTable dt2 = DataProvider.CreateChildDataTable();
921                         dt1.PrimaryKey  = new DataColumn[] {dt1.Columns[0]};
922                         dt2.PrimaryKey  = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
923                         DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
924                         DataSet ds = new DataSet();
925                         ds.Tables.AddRange(new DataTable[] {dt1,dt2});
926                         ds.Relations.Add(rel);
927
928                         dt2.Reset();
929
930                         // Reset - ParentRelations
931                         Assert.AreEqual(0 , dt2.ParentRelations.Count  , "DT104");
932                         // Reset - Constraints
933                         Assert.AreEqual(0 , dt2.Constraints.Count  , "DT105");
934                         // Reset - Rows
935                         Assert.AreEqual(0 , dt2.Rows.Count  , "DT106");
936                         // Reset - Columns
937                         Assert.AreEqual(0 , dt2.Columns.Count  , "DT107");
938                 }
939
940                 [Test] public void RowChanged()
941                 {
942                         DataTable dt = DataProvider.CreateParentDataTable();
943
944                         dt.RowChanged += new DataRowChangeEventHandler ( Row_Changed );
945
946                         _EventTriggered=false;
947                         // RowChanged - 1
948                         dt.Rows[0][1] = "NewValue";
949                         Assert.AreEqual(true , _EventTriggered , "DT108");
950
951                         _EventTriggered=false;
952                         // RowChanged - 2
953                         dt.Rows[0].BeginEdit(); 
954                         dt.Rows[0][1] = "NewValue";
955                         Assert.AreEqual(false , _EventTriggered , "DT109");
956
957                         _EventTriggered=false;
958                         // RowChanged - 3
959                         dt.Rows[0].EndEdit();
960                         Assert.AreEqual(true , _EventTriggered , "DT110");
961
962                         _EventTriggered=false;
963                         dt.RowChanged -= new DataRowChangeEventHandler  ( Row_Changed );
964                         // RowChanged - 4
965                         dt.Rows[0][1] = "NewValue A";
966                         Assert.AreEqual(false , _EventTriggered , "DT111");
967                 }
968
969                 private void Row_Changed( object sender, DataRowChangeEventArgs e )
970                 {
971                         _EventTriggered = true;
972                 }
973
974                 [Test] public void RowChanging()
975                 {
976                         DataTable dt = DataProvider.CreateParentDataTable();
977
978                         dt.RowChanging  += new DataRowChangeEventHandler ( Row_Changing );
979
980                         _EventTriggered=false;
981                         // RowChanging - 1
982                         dt.Rows[0][1] = "NewValue";
983                         Assert.AreEqual(true , _EventTriggered , "DT112");
984
985                         _EventTriggered=false;
986                         // RowChanging - 2
987                         dt.Rows[0].BeginEdit(); 
988                         dt.Rows[0][1] = "NewValue";
989                         Assert.AreEqual(false , _EventTriggered , "DT113");
990
991                         _EventTriggered=false;
992                         // RowChanging - 3
993                         dt.Rows[0].EndEdit();
994                         Assert.AreEqual(true , _EventTriggered , "DT114");
995
996                         _EventTriggered=false;
997                         dt.RowChanging  -= new DataRowChangeEventHandler ( Row_Changing );
998                         // RowChanging - 4
999                         dt.Rows[0][1] = "NewValue A";
1000                         Assert.AreEqual(false , _EventTriggered , "DT115");
1001                 }
1002
1003                 private void Row_Changing( object sender, DataRowChangeEventArgs e )
1004                 {
1005                         _EventTriggered = true;
1006                 }
1007
1008                 [Test] public void RowDeleted()
1009                 {
1010                         DataTable dt = DataProvider.CreateParentDataTable();
1011
1012                         dt.RowDeleted += new DataRowChangeEventHandler ( Row_Deleted );
1013
1014                         _EventTriggered=false;
1015                         // RowDeleted - 1
1016                         dt.Rows[0].Delete();
1017                         Assert.AreEqual(true , _EventTriggered , "DT116");
1018
1019                         _EventTriggered=false;
1020                         dt.RowDeleted -= new DataRowChangeEventHandler ( Row_Deleted );
1021                         // RowDeleted - 2
1022                         dt.Rows[1].Delete();
1023                         Assert.AreEqual(false , _EventTriggered , "DT117");
1024                 }
1025
1026                 private void Row_Deleted( object sender, DataRowChangeEventArgs e )
1027                 {
1028                         _EventTriggered = true;
1029                 }
1030
1031                 [Test] public void RowDeleting()
1032                 {
1033                         DataTable dt = DataProvider.CreateParentDataTable();
1034
1035                         dt.RowDeleting  += new DataRowChangeEventHandler ( Row_Deleting );
1036
1037                         _EventTriggered=false;
1038                         // RowDeleting - 1
1039                         dt.Rows[0].Delete();
1040                         Assert.AreEqual(true , _EventTriggered , "DT118");
1041
1042                         _EventTriggered=false;
1043                         dt.RowDeleting  -= new DataRowChangeEventHandler ( Row_Deleting );
1044                         // RowDeleting - 2
1045                         dt.Rows[1].Delete();
1046                         Assert.AreEqual(false , _EventTriggered , "DT119");
1047                 }
1048
1049                 private void Row_Deleting( object sender, DataRowChangeEventArgs e )
1050                 {
1051                         _EventTriggered = true;
1052                 }
1053
1054                 [Test] public void Rows()
1055                 {
1056                         DataTable dtParent;
1057                         dtParent = DataProvider.CreateParentDataTable();
1058
1059                         // Checking Rows
1060                         Assert.AreEqual(true, dtParent.Rows != null, "DT120");
1061
1062                         // Checking rows count
1063                         Assert.AreEqual(true, dtParent.Rows.Count > 0 , "DT121");
1064                 }
1065
1066                 [Test] public void Select()
1067                 {
1068                         DataTable dt = DataProvider.CreateParentDataTable();
1069
1070                         DataRow[] drSelect = dt.Select();
1071                         DataRow[] drResult = new DataRow[dt.Rows.Count] ;
1072                         dt.Rows.CopyTo(drResult,0);
1073
1074                         // Select
1075
1076                         Assert.AreEqual(drResult, drSelect , "DT122");
1077                 }
1078
1079                 [Test] public void Select_ByFilter()
1080                 {
1081                         DataSet ds = new DataSet();
1082                         ds.Tables.Add(DataProvider.CreateParentDataTable());
1083
1084                         DataTable dt = DataProvider.CreateChildDataTable();
1085                         ds.Tables.Add(dt);
1086                         DataRow[] drSelect = null;
1087                         System.Collections.ArrayList al = new System.Collections.ArrayList();
1088
1089                         //add column with special name
1090                         DataColumn dc = new DataColumn("Column#",typeof(int));
1091                         dc.DefaultValue=-1;
1092                         dt.Columns.Add(dc);
1093                         //put some values
1094                         dt.Rows[0][dc] = 100;
1095                         dt.Rows[1][dc] = 200;
1096                         dt.Rows[2][dc] = 300;
1097                         dt.Rows[4][dc] = -400;
1098
1099                         //for trim function
1100                         dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + "   \t\n ";
1101                         dt.Rows[0]["String1"] = "   \t\n " + dt.Rows[0]["String1"] ;
1102                         dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + "    ";
1103
1104                         ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
1105                         ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
1106                         ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
1107
1108                         //-------------------------------------------------------------
1109                         al.Clear();
1110                         foreach (DataRow dr in dt.Rows ) 
1111                         {
1112                                 if ((int)dr["ChildId"] == 1)
1113                                 {
1114                                         al.Add(dr);
1115                                 }
1116                         }
1117                         // Select_S - ChildId=1
1118                         drSelect = dt.Select("ChildId=1");
1119                         Assert.AreEqual(al.ToArray(), drSelect , "DT123");
1120
1121                         //-------------------------------------------------------------
1122                         al.Clear();
1123                         foreach (DataRow dr in dt.Rows ) 
1124                         {
1125                                 if ((int)dr["ChildId"] == 1)
1126                                 {
1127                                         al.Add(dr);
1128                                 }
1129                         }
1130                         // Select_S - ChildId='1'
1131                         drSelect = dt.Select("ChildId='1'");
1132                         Assert.AreEqual(al.ToArray(), drSelect , "DT124");
1133                         //-------------------------------------------------------------
1134                         // Select_S - ChildId= '1'  (whitespace in filter string.
1135                         drSelect = dt.Select("ChildId= '1'");
1136                         Assert.AreEqual(al.ToArray(), drSelect , "DT125");
1137                         //-------------------------------------------------------------
1138                         al.Clear();
1139                         foreach (DataRow dr in dt.Rows ) if (dr["String1"].ToString() == "1-String1") al.Add(dr);
1140                         // Select_S - String1='1-String1'
1141                         drSelect = dt.Select("String1='1-String1'");
1142                         Assert.AreEqual(al.ToArray(), drSelect , "DT126");
1143
1144                         //-------------------------------------------------------------
1145                         al.Clear();
1146                         foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) al.Add(dr);
1147                         // Select_S - ChildId=1 and String1='1-String1'
1148                         drSelect = dt.Select("ChildId=1 and String1='1-String1'");
1149                         Assert.AreEqual(al.ToArray(), drSelect , "DT127");
1150
1151                         //-------------------------------------------------------------
1152                         al.Clear();
1153                         foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4 ) al.Add(dr);
1154                         // Select_S - ChildId+ParentId >= 4
1155                         drSelect = dt.Select("ChildId+ParentId >= 4");
1156                         CompareUnSorted(drSelect ,al.ToArray());
1157
1158                         //-------------------------------------------------------------
1159                         al.Clear();
1160                         foreach (DataRow dr in dt.Rows ) 
1161                         {
1162                                 if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0 )
1163                                 {
1164                                         al.Add(dr);
1165                                 }
1166                         }
1167                         // Select_S - ChildId-ParentId) * -1  <> 0
1168                         drSelect = dt.Select("(ChildId-ParentId) * -1  <> 0");
1169                         CompareUnSorted(drSelect ,al.ToArray());
1170
1171                         //-------------------------------------------------------------
1172                         al.Clear();
1173                         foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4 ) al.Add(dr);
1174                         // Select_S - ChildDouble < ParentId % 4
1175                         drSelect = dt.Select("ChildDouble < ParentId % 4");
1176                         CompareUnSorted(drSelect ,al.ToArray());
1177
1178                         //-------------------------------------------------------------
1179                         al.Clear();
1180                         foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25 ) al.Add(dr);
1181                         // Select_S - ChildDouble in (10,20,25)
1182                         drSelect = dt.Select("ChildDouble in (10,20,25)");
1183                         CompareUnSorted(drSelect ,al.ToArray());
1184
1185                         //-------------------------------------------------------------
1186                         al.Clear();
1187                         foreach (DataRow dr in dt.Rows ) if ( dr["String2"].ToString().IndexOf("1-S") >= 0 ) al.Add(dr);
1188                         // Select_S - String2 like '%1-S%'
1189                         drSelect = dt.Select("String2 like '%1-S%'");
1190                         Assert.AreEqual(al.ToArray(), drSelect , "DT128");
1191
1192                         //-------------------------------------------------------------
1193                         //If a column name contains one of the above characters,(ex. #\/=><+-*%&|^'" and so on) the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write "[Column#]":
1194                         al.Clear();
1195                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1196                         // Select_S - [Column#] <= 0 
1197                         drSelect = dt.Select("[Column#] <= 0 ");
1198                         CompareUnSorted(drSelect ,al.ToArray());
1199                         //-------------------------------------------------------------
1200                         al.Clear();
1201                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1202                         // Select_S - [Column#] <= 0
1203                         drSelect = dt.Select("[Column#] <= 0");
1204                         CompareUnSorted(drSelect ,al.ToArray());
1205
1206                         //-------------------------------------------------------------
1207                         al.Clear();
1208                         foreach (DataRow dr in dt.Rows ) if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0  ) al.Add(dr);
1209                         // Select_S - ChildDateTime > #12/12/2000# 
1210                         drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
1211                         CompareUnSorted(drSelect ,al.ToArray());
1212
1213                         //-------------------------------------------------------------
1214
1215                         al.Clear();
1216                         foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0  ) al.Add(dr);
1217                         // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#  
1218                         drSelect = dt.Select("ChildDateTime > #1/12/1999 12:06:30 PM#  ");
1219                         CompareUnSorted(drSelect ,al.ToArray());
1220
1221                         //-------------------------------------------------------------
1222
1223                         al.Clear();
1224                         foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2005,12,03,17,06,30)) >= 0  || ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1980,11,03)) <= 0 ) al.Add(dr);
1225                         // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#  
1226                         drSelect = dt.Select("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#  ");
1227                         CompareUnSorted(drSelect ,al.ToArray());
1228
1229 #if LATER
1230                         //-------------------------------------------------------------
1231                         al.Clear();
1232                         foreach (DataRow dr in dt.Rows ) if ( dr["ChildDouble"].ToString().Length > 10) al.Add(dr);
1233                                 // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
1234                                 drSelect = dt.Select("Len(Convert(ChildDouble,'System.String')) > 10");
1235                                 Assert.AreEqual(al.ToArray(), drSelect , "DT129");
1236 #endif
1237                         //-------------------------------------------------------------
1238                         al.Clear();
1239                         foreach (DataRow dr in dt.Rows ) if ( dr["String1"].ToString().Trim().Substring(0,2) == "1-") al.Add(dr);
1240                         // Select_S - SubString(Trim(String1),1,2) = '1-'
1241                         drSelect = dt.Select("SubString(Trim(String1),1,2) = '1-'");
1242                         Assert.AreEqual(al.ToArray(), drSelect , "DT130");
1243                         //-------------------------------------------------------------
1244                         /*
1245                         al.Clear();
1246                         foreach (DataRow dr in ds.Tables[0].Rows ) if ( dr.IsNull("ParentBool") || (bool)dr["ParentBool"]) al.Add(dr);
1247                                 // Select_S - IsNull(ParentBool,true)
1248                                 drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
1249                                 Assert.AreEqual(al.ToArray(), drSelect , "DT131");
1250                         */
1251                         //-------------------------------------------------------------
1252                         al.Clear();
1253                         // Select_S - Relation not exists, Exception
1254                         try
1255                         {
1256                                 drSelect = dt.Select("Parent.ParentId = ChildId");
1257                                 Assert.Fail("DT132: Select Failed to throw IndexOutOfRangeException");
1258                         }
1259                         catch (IndexOutOfRangeException) {}
1260                         catch (AssertionException exc) {throw  exc;}
1261                         catch (Exception exc)
1262                         {
1263                                 Assert.Fail("DT133: Select. Wrong exception type. Got:" + exc);
1264                         }
1265                         //-------------------------------------------------------------
1266                         al.Clear();
1267                         ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1268                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"]) al.Add(dr);
1269                         // Select_S - Parent.ParentId = ChildId
1270                         drSelect = dt.Select("Parent.ParentId = ChildId");
1271                         Assert.AreEqual(al.ToArray(), drSelect , "DT134");
1272                 }
1273
1274                 private void CompareUnSorted(Array a, Array b)
1275                 {
1276                         string msg = string.Format("Failed while comparing(Array a ={0} ({1}), Array b = {2} ({3}))]", a.ToString(), a.GetType().FullName, b.ToString(), b.GetType().FullName);
1277                         foreach (object item in a)
1278                         {
1279                                 if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
1280                                 {
1281                                         Assert.Fail(msg);
1282                                 }
1283                         }
1284
1285                         foreach (object item in b)
1286                         {
1287                                 if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
1288                                 {
1289                                         Assert.Fail(msg);
1290                                         return;
1291                                 }
1292                         }
1293                 }
1294
1295                 [Test] public void Select_ByFilterDataViewRowState()
1296                 {
1297                         DataTable dt = DataProvider.CreateParentDataTable();
1298                         DataRow[] drSelect, drResult;
1299
1300                         dt.Rows[0].Delete();
1301                         dt.Rows[1]["ParentId"] = 1;
1302                         dt.Rows[2]["ParentId"] = 1;
1303                         dt.Rows[3].Delete();
1304                         dt.Rows.Add(new object[] {1,"A","B"});
1305                         dt.Rows.Add(new object[] {1,"C","D"});
1306                         dt.Rows.Add(new object[] {1,"E","F"});
1307
1308                         drSelect = dt.Select("ParentId=1","",DataViewRowState.Added );
1309                         drResult = GetResultRows(dt,DataRowState.Added);
1310                         // Select_SSD DataViewRowState.Added
1311                         Assert.AreEqual(drResult, drSelect , "DT135");
1312
1313                         drSelect = dt.Select("ParentId=1","",DataViewRowState.CurrentRows  );
1314                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added  | DataRowState.Modified );
1315                         // Select_SSD DataViewRowState.CurrentRows
1316                         Assert.AreEqual(drResult, drSelect , "DT136");
1317
1318                         drSelect = dt.Select("ParentId=1","",DataViewRowState.Deleted  );
1319                         drResult = GetResultRows(dt,DataRowState.Deleted );
1320                         // Select_SSD DataViewRowState.Deleted
1321                         Assert.AreEqual(drResult, drSelect , "DT137");
1322
1323                         drSelect = dt.Select("ParentId=1","",DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal  );
1324                         drResult = GetResultRows(dt,DataRowState.Modified );
1325                         // Select_SSD ModifiedCurrent or ModifiedOriginal
1326                         Assert.AreEqual(drResult, drSelect , "DT138");
1327                 }
1328
1329                 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
1330                 {
1331                         System.Collections.ArrayList al = new System.Collections.ArrayList();
1332                         DataRowVersion drVer = DataRowVersion.Current;
1333
1334                         //From MSDN -   The row the default version for the current DataRowState.
1335                         //                              For a DataRowState value of Added, Modified or Current, 
1336                         //                              the default version is Current. 
1337                         //                              For a DataRowState of Deleted, the version is Original.
1338                         //                              For a DataRowState value of Detached, the version is Proposed.
1339
1340                         if (    ((State & DataRowState.Added)           > 0)  
1341                                 | ((State & DataRowState.Modified)      > 0)  
1342                                 | ((State & DataRowState.Unchanged)     > 0) ) 
1343                                 drVer = DataRowVersion.Current;
1344                         if ( (State & DataRowState.Deleted)             > 0
1345                                 | (State & DataRowState.Detached)       > 0 )  
1346                                 drVer = DataRowVersion.Original; 
1347
1348                         foreach (DataRow dr in dt.Rows )
1349                         {
1350                                 if ( dr.HasVersion(drVer) 
1351                                         && ((int)dr["ParentId", drVer] == 1) 
1352                                         && ((dr.RowState & State) > 0 ) 
1353                                         )
1354                                         al.Add(dr);
1355                         }
1356                         DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
1357                         return result; 
1358                 }       
1359
1360                 [Test] public void TableName()
1361                 {
1362                         DataTable dtParent = new DataTable();
1363
1364                         // Checking TableName default
1365                         Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
1366
1367                         // Checking TableName set/get
1368                         String s = "MyTable";
1369                         dtParent.TableName=s;
1370                         Assert.AreEqual(s, dtParent.TableName, "DT140");
1371                 }
1372
1373                 [Test] public new void  ToString() 
1374                 {
1375                         DataTable dt = DataProvider.CreateParentDataTable();
1376                         dt.DisplayExpression = dt.Columns[0].ColumnName ;
1377
1378                         string sToString = dt.TableName + " + " + dt.DisplayExpression;
1379                         // ToString
1380                         Assert.AreEqual(sToString , dt.ToString() , "DT141");
1381                 }
1382
1383                 [Test] public void caseSensitive()
1384                 {
1385                         DataTable dtParent = new DataTable();
1386
1387                         // Checking default
1388                         Assert.AreEqual(false, dtParent.CaseSensitive  , "DT142");
1389
1390                         // Checking set/get
1391                         dtParent.CaseSensitive = true;
1392                         Assert.AreEqual(true, dtParent.CaseSensitive , "DT143");
1393                 }
1394
1395                 [Test] public void ctor()
1396                 {
1397                         DataTable dt;
1398                         dt = new DataTable();
1399
1400                         // ctor
1401                         Assert.AreEqual(false, dt == null, "DT144");
1402                 }
1403
1404                 [Test] public void ctor_ByName()
1405                 {
1406                         DataTable dt;
1407                         string sName = "MyName";
1408
1409                         dt = new DataTable(sName);
1410
1411                         // Ctor
1412                         Assert.AreEqual(false , dt == null , "DT145");
1413
1414                         // Ctor TableName
1415                         Assert.AreEqual(sName , dt.TableName , "DT146");
1416                 }
1417
1418                 [Test] public void displayExpression()
1419                 {
1420                         DataTable dtParent;
1421                         dtParent= DataProvider.CreateParentDataTable(); 
1422
1423                         // Checking DisplayExpression default 
1424                         Assert.AreEqual(String.Empty , dtParent.DisplayExpression , "DT147");
1425
1426                         // Checking DisplayExpression Set/Get 
1427                         dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
1428                         Assert.AreEqual(dtParent.Columns[0].ColumnName, dtParent.DisplayExpression  , "DT148");
1429                 }
1430
1431                 [Test] public void extendedProperties()
1432                 {
1433                         DataTable dtParent;
1434                         PropertyCollection pc;
1435                         dtParent= DataProvider.CreateParentDataTable(); 
1436
1437                         pc = dtParent.ExtendedProperties ;
1438
1439                         // Checking ExtendedProperties default 
1440                         Assert.AreEqual(true, pc != null, "DT149");
1441
1442                         // Checking ExtendedProperties count 
1443                         Assert.AreEqual(0, pc.Count , "DT150");
1444                 }
1445
1446                 [Test] public void primaryKey()
1447                 {
1448                         DataTable dtParent;
1449                         dtParent = DataProvider.CreateParentDataTable();
1450
1451                         // Checking PrimaryKey default
1452                         Assert.AreEqual(0, dtParent.PrimaryKey.Length , "DT151");
1453
1454                         // Checking PrimaryKey set/get
1455                         DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]}; 
1456                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
1457                         Assert.AreEqual(dcArr, dtParent.PrimaryKey  , "DT152");
1458
1459                         dtParent.PrimaryKey=null;
1460                         DataSet ds = new DataSet();
1461                         DataRow dr = null;
1462                         ds.Tables.Add(dtParent);
1463
1464                         //check primary key - ColumnType String, ds.CaseSensitive = false;
1465                         ds.CaseSensitive = false;
1466                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
1467                         // check primary key - ColumnType String, ds.CaseSensitive = false;
1468                         dr = dtParent.NewRow();
1469                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1470                         dr["String1"] = dr["String1"].ToString().ToUpper();  
1471                         try
1472                         {
1473                                 dtParent.Rows.Add(dr);
1474                                 Assert.Fail("DT153: Rows.Add Failed to throw ConstraintException");
1475                         }
1476                         catch (ConstraintException) {}
1477                         catch (AssertionException exc) {throw  exc;}
1478                         catch (Exception exc)
1479                         {
1480                                 Assert.Fail("DT154: Rows.Add. Wrong exception type. Got:" + exc);
1481                         }
1482                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1483
1484                         //check primary key - ColumnType String, ds.CaseSensitive = true;               
1485                         ds.CaseSensitive = true;
1486                         // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
1487                         dr = dtParent.NewRow();
1488                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1489                         dr["String1"] = dr["String1"].ToString();  
1490                         try
1491                         {
1492                                 dtParent.Rows.Add(dr);
1493                                 Assert.Fail("DT155: Rows.Add Failed to throw ConstraintException");
1494                         }
1495                         catch (ConstraintException) {}
1496                         catch (AssertionException exc) {throw  exc;}
1497                         catch (Exception exc)
1498                         {
1499                                 Assert.Fail("DT156: Rows.Add. Wrong exception type. Got:" + exc);
1500                         }
1501                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1502
1503                         //check primary key - ColumnType String, ds.CaseSensitive = true;               
1504                         ds.CaseSensitive = true;
1505
1506                         // check primary key - ColumnType String, ds.CaseSensitive = true;
1507                         dr = dtParent.NewRow();
1508                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1509                         dr["String1"] = dr["String1"].ToString().ToUpper() ;  
1510                         dtParent.Rows.Add(dr);
1511                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
1512
1513                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1514
1515                         dtParent.PrimaryKey=null;
1516                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
1517                         // check primary key - ColumnType DateTime
1518                         dr = dtParent.NewRow();
1519                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1520                         dr["ParentDateTime"] = DateTime.Now;
1521                         dtParent.Rows.Add(dr);
1522                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
1523                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1524
1525                         // check primary key ConstraintException- ColumnType DateTime
1526                         dr = dtParent.NewRow();
1527                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1528                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1529                         try
1530                         {
1531                                 dtParent.Rows.Add(dr);
1532                                 Assert.Fail("DT159: Rows.Add Failed to throw ConstraintException");
1533                         }
1534                         catch (ConstraintException) {}
1535                         catch (AssertionException exc) {throw  exc;}
1536                         catch (Exception exc)
1537                         {
1538                                 Assert.Fail("DT160: Rows.Add. Wrong exception type. Got:" + exc);
1539                         }
1540                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1541
1542                         dtParent.PrimaryKey=null;
1543                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
1544                         // check primary key - ColumnType ParentDouble, value=Epsilon
1545                         dr = dtParent.NewRow();
1546                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1547                         dr["ParentDouble"] = Double.Epsilon ;
1548                         dtParent.Rows.Add(dr);
1549                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
1550                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1551
1552                         // check primary key ConstraintException - ColumnType ParentDouble
1553                         dr = dtParent.NewRow();
1554                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1555                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1556                         try
1557                         {
1558                                 dtParent.Rows.Add(dr);
1559                                 Assert.Fail("DT162: Rows.Add Failed to throw ConstraintException");
1560                         }
1561                         catch (ConstraintException) {}
1562                         catch (AssertionException exc) {throw  exc;}
1563                         catch (Exception exc)
1564                         {
1565                                 Assert.Fail("DT163: Rows.Add. Wrong exception type. Got:" + exc);
1566                         }
1567                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1568
1569                         //
1570                         // SubTest
1571                         //
1572                         dtParent.PrimaryKey=null;
1573                         // check primary key ConstraintException - ColumnType ParentBool 
1574                         try
1575                         {
1576                                 //ParentBool is not unique, will raise exception
1577                                 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
1578                                 Assert.Fail("DT164: PrimaryKey Failed to throw ArgumentException");
1579                         }
1580                         catch (ArgumentException) {}
1581                         catch (AssertionException exc) {throw  exc;}
1582                         catch (Exception exc)
1583                         {
1584                                 Assert.Fail("DT165: PrimaryKey. Wrong exception type. Got:" + exc);
1585                         }
1586                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1587
1588                         //
1589                         // SubTest
1590                         //
1591                         dtParent.PrimaryKey=null;
1592                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
1593                         // check primary key - ColumnType Double,DateTime test1
1594                         dr = dtParent.NewRow();
1595                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1596                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1597                         dr["ParentDateTime"] = DateTime.Now;
1598                         dtParent.Rows.Add(dr);
1599                         Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
1600                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1601
1602                         // check primary key - ColumnType Double,DateTime test2
1603                         dr = dtParent.NewRow();
1604                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1605                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1606                         dr["ParentDouble"] = 99.399;
1607                         dtParent.Rows.Add(dr);
1608                         Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
1609                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1610
1611                         // check primary key ConstraintException - ColumnType Double,DateTime 
1612                         dr = dtParent.NewRow();
1613                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1614                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1615                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1616                         try
1617                         {
1618                                 dtParent.Rows.Add(dr);
1619                                 Assert.Fail("DT168: Rows.Add Failed to throw ConstraintException");
1620                         }
1621                         catch (ConstraintException) {}
1622                         catch (AssertionException exc) {throw  exc;}
1623                         catch (Exception exc)
1624                         {
1625                                 Assert.Fail("DT169: Rows.Add. Wrong exception type. Got:" + exc);
1626                         }
1627                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1628
1629                         DataTable dtChild = DataProvider.CreateChildDataTable();
1630                         ds.Tables.Add(dtChild);
1631                         dtParent.PrimaryKey = null;
1632                         //this test was addedd to check java exception: 
1633                         //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
1634                         // check add primary key with relation 
1635                         ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1636                         //the following line will cause java to fail
1637                         ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]}; 
1638                         Assert.AreEqual(2, ds.Tables[0].PrimaryKey.Length , "DT170");
1639                 }
1640         
1641                 // Test for bug #76213
1642                 [Test]
1643                 public void Compute_WithoutSchemaData_Test()
1644                 {
1645                         DataSet ds = new DataSet ("TestData");
1646                         DataTable table = ds.Tables.Add ("TestTable");
1647
1648                         table.Columns.Add ("Id");
1649                         table.Columns.Add ("Value");
1650
1651                         table.Rows.Add (new object[] {"1","4.5"});
1652                         table.Rows.Add (new object[] {"2","7.5"});
1653                         table.Rows.Add (new object[] {"3","2.5"});
1654                         table.Rows.Add (new object[] {"4","3.5"});
1655                         
1656                         Assert.AreEqual ("1",
1657                                  table.Compute ("Min(Id)",String.Empty),"#1");  
1658                         Assert.AreEqual ("4",
1659                                  table.Compute ("Max(Id)",String.Empty),"#2");  
1660                         Assert.AreEqual ("2.5",
1661                                  table.Compute ("Min(Value)",String.Empty),"#3");       
1662                         Assert.AreEqual ("7.5",
1663                                  table.Compute ("Max(Value)",String.Empty),"#4");       
1664                 }
1665
1666                 [Test]
1667                 public void BeginLoadData()\r
1668                 {\r
1669                         DataTable dt = DataProvider.CreateParentDataTable();\r
1670                         dt.Columns[0].AllowDBNull = false;\r
1671                                \r
1672                         //dt.BeginLoadData();\r
1673                         try\r
1674                         {\r
1675                                 //if BeginLoadData has not been called, an exception will be throw\r
1676                                 dt.LoadDataRow(new object[] {null,"A","B"},false);\r
1677                                 Assert.Fail("DT170: Failed to throw NoNullAllowedException");
1678                         }
1679                         catch (NoNullAllowedException) {}
1680                         catch (AssertionException exc) {throw  exc;}
1681                         catch (Exception exc)
1682                         {
1683                                 Assert.Fail("DT171: Wrong exception type. Got:" + exc);
1684                         }\r
1685 \r
1686 \r
1687                         DataTable dt1 = DataProvider.CreateUniqueConstraint();\r
1688 \r
1689                         bool excptionOccurd = false;\r
1690 \r
1691                         try\r
1692                         {\r
1693                                 dt1.BeginLoadData();\r
1694 \r
1695                                 DataRow  dr = dt1.NewRow();\r
1696                                 dr[0] = 3;\r
1697                                 dt1.Rows.Add(dr);\r
1698                                 dt1.EndLoadData(); // constraint violation\r
1699 \r
1700                                 Assert.Fail("DT172: Failed to throw ConstraintException");
1701                         }
1702                         catch (ConstraintException) 
1703                         {
1704                                 Assert.AreEqual(2,dt1.GetErrors().Length,"DT173");\r
1705                                 Assert.AreEqual(true,dt1.GetErrors()[0].RowError.Length > 10,"DT174");\r
1706                                 Assert.AreEqual(true,dt1.GetErrors()[1].RowError.Length > 10,"DT175");
1707                         }
1708                         catch (AssertionException exc) {throw  exc;}
1709                         catch (Exception exc)
1710                         {
1711                                 Assert.Fail("DT176: Wrong exception type. Got:" + exc);
1712                         }\r
1713                         \r
1714 \r
1715                         DataSet ds=null;\r
1716                         excptionOccurd = false;\r
1717                         try\r
1718                         {\r
1719                                 ds= DataProvider.CreateForigenConstraint();\r
1720                                 ds.Tables[0].BeginLoadData();\r
1721                                 ds.Tables[0].Rows[0][0] = 10; //Forigen constraint violation\r
1722                                 //ds.Tables[0].AcceptChanges();\r
1723                                 ds.Tables[0].EndLoadData();\r
1724 \r
1725                                 Assert.Fail("DT177: Failed to throw ConstraintException");
1726                         }
1727                         catch (ConstraintException) 
1728                         {
1729                                 Assert.AreEqual(3,ds.Tables[1].GetErrors().Length,"DT178");\r
1730                                 for(int index=0;index<3;index++)\r
1731                                 {\r
1732                                         Assert.AreEqual(true,ds.Tables[1].GetErrors()[index].RowError.Length > 10,"DT179");\r
1733                                 }
1734                         }
1735                         catch (AssertionException exc) {throw  exc;}
1736                         catch (Exception exc)
1737                         {
1738                                 Assert.Fail("DT180: Wrong exception type. Got:" + exc);
1739                         }\r
1740                 }
1741 \r
1742                 private DataRowAction drExpectedAction;\r
1743                 \r
1744                 public void OnRowChanged()\r
1745                 {\r
1746                         ProtectedTestClass dt = new ProtectedTestClass(); \r
1747 \r
1748                         EventRaised = false;\r
1749                         dt.OnRowChanged_Test(DataRowAction.Nothing );\r
1750                         \r
1751                         Assert.IsFalse(EventRaised ,"DT181" );\r
1752 \r
1753                         dt.RowChanged += new DataRowChangeEventHandler(OnRowChanged_Handler);\r
1754                         foreach (int i in Enum.GetValues(typeof(DataRowAction)))\r
1755                         {\r
1756                                 EventRaised = false;\r
1757                                 EventValues = false;\r
1758                                 drExpectedAction = (DataRowAction)i;\r
1759                                 dt.OnRowChanged_Test(drExpectedAction);\r
1760                     \r
1761                                 Assert.IsTrue(EventRaised ,"DT182" );\r
1762 \r
1763                                 Assert.IsTrue(EventValues  ,"DT183" );\r
1764                         }\r
1765                         dt.RowChanged -= new DataRowChangeEventHandler(OnRowChanged_Handler);\r
1766                 }\r
1767 \r
1768                 private void OnRowChanged_Handler(Object sender,DataRowChangeEventArgs e)\r
1769                 {\r
1770                         \r
1771                         DataTable dt = (DataTable)sender;\r
1772                         if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)\r
1773                                 EventValues = true;\r
1774                         EventRaised = true;\r
1775                 }
1776
1777                 [Test]
1778                 public void OnRowChanging()\r
1779                 {\r
1780                         ProtectedTestClass dt = new ProtectedTestClass(); \r
1781 \r
1782                         EventRaised = false;\r
1783                         dt.OnRowChanging_Test(DataRowAction.Nothing );\r
1784                         \r
1785                         Assert.IsFalse(EventRaised ,"DT184" );\r
1786 \r
1787                         dt.RowChanging += new DataRowChangeEventHandler(OnRowChanging_Handler);\r
1788                         foreach (int i in Enum.GetValues(typeof(DataRowAction)))\r
1789                         {\r
1790                                 EventRaised = false;\r
1791                                 EventValues = false;\r
1792                                 drExpectedAction = (DataRowAction)i;\r
1793                                 dt.OnRowChanging_Test(drExpectedAction);\r
1794                     \r
1795                                 Assert.IsTrue(EventRaised ,"DT185" );\r
1796                                 \r
1797                                 Assert.IsTrue(EventValues  ,"DT186" );\r
1798                         }\r
1799                         dt.RowChanging -= new DataRowChangeEventHandler(OnRowChanging_Handler);\r
1800                 }\r
1801 \r
1802                 private void OnRowChanging_Handler(Object sender,DataRowChangeEventArgs e)\r
1803                 {\r
1804                         \r
1805                         DataTable dt = (DataTable)sender;\r
1806                         if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)\r
1807                                 EventValues = true;\r
1808                         EventRaised = true;\r
1809                 }
1810
1811                 [Test]
1812                 public void OnRowDeleted()\r
1813                 {\r
1814                         ProtectedTestClass dt = new ProtectedTestClass(); \r
1815 \r
1816                         EventRaised = false;\r
1817                         dt.OnRowDeleted_Test(DataRowAction.Nothing );\r
1818 \r
1819                         Assert.IsFalse(EventRaised ,"DT187" );\r
1820 \r
1821                         dt.RowDeleted += new DataRowChangeEventHandler(OnRowDeleted_Handler);\r
1822                         foreach (int i in Enum.GetValues(typeof(DataRowAction)))\r
1823                         {\r
1824                                 EventRaised = false;\r
1825                                 EventValues = false;\r
1826                                 drExpectedAction = (DataRowAction)i;\r
1827                                 dt.OnRowDeleted_Test(drExpectedAction);\r
1828 \r
1829                                 Assert.IsTrue(EventRaised ,"DT188" );\r
1830                                 \r
1831                                 Assert.IsTrue(EventValues  ,"DT189" );\r
1832                         }\r
1833                         dt.RowDeleted -= new DataRowChangeEventHandler(OnRowDeleted_Handler);\r
1834                 }\r
1835 \r
1836 \r
1837                 private void OnRowDeleted_Handler(Object sender,DataRowChangeEventArgs e)\r
1838                 {\r
1839                         DataTable dt = (DataTable)sender;\r
1840                         if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)\r
1841                                 EventValues = true;\r
1842                         EventRaised = true;\r
1843                 }
1844
1845                 public void OnRowDeleting()\r
1846                 {\r
1847                         ProtectedTestClass dt = new ProtectedTestClass(); \r
1848 \r
1849                         EventRaised = false;\r
1850                         dt.OnRowDeleting_Test(DataRowAction.Nothing );\r
1851 \r
1852                         Assert.IsFalse(EventRaised ,"DT190" );\r
1853 \r
1854                         dt.RowDeleting += new DataRowChangeEventHandler(OnRowDeleting_Handler);\r
1855                         foreach (int i in Enum.GetValues(typeof(DataRowAction)))\r
1856                         {\r
1857                                 EventRaised = false;\r
1858                                 EventValues = false;\r
1859                                 drExpectedAction = (DataRowAction)i;\r
1860                                 dt.OnRowDeleting_Test(drExpectedAction);\r
1861                     \r
1862                                 Assert.IsTrue(EventRaised ,"DT191" );\r
1863                                 \r
1864                                 Assert.IsTrue(EventValues  ,"DT192" );\r
1865                         }\r
1866                         dt.RowDeleting -= new DataRowChangeEventHandler(OnRowDeleting_Handler);\r
1867                 }\r
1868 \r
1869                 private void OnRowDeleting_Handler(Object sender,DataRowChangeEventArgs e)\r
1870                 {\r
1871                         \r
1872                         DataTable dt = (DataTable)sender;\r
1873                         if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)\r
1874                                 EventValues = true;\r
1875                         EventRaised = true;\r
1876                 }
1877
1878                 [Test]
1879 #if !TARGET_JVM\r
1880                 [Category ("NotWorking")]\r
1881 #endif
1882                 public void Select_StringString()\r
1883                 {\r
1884                 DataTable dt = DataProvider.CreateChildDataTable();\r
1885                 \r
1886                         DataRow[] drSelect;\r
1887                         System.Collections.ArrayList al;\r
1888 \r
1889                         //add some rows\r
1890                         dt.Rows.Add(new object[] {99,88,"bla","wowww"});\r
1891                         dt.Rows.Add(new object[] {999,888,"","woowww"});\r
1892 \r
1893                         //get excepted resault\r
1894                         al = new System.Collections.ArrayList();\r
1895                         foreach (DataRow dr in dt.Rows )\r
1896                         {\r
1897                                 if ((int)dr["ChildId"] == 1)\r
1898                                         al.Add(dr);\r
1899                         }\r
1900                         //al.Reverse();\r
1901                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1902 \r
1903                         drSelect = dt.Select("ChildId=1","ParentId Desc");\r
1904                         Assert.AreEqual(al.ToArray(),drSelect ,"DT193");\r
1905 \r
1906 \r
1907                         //get excepted resault\r
1908                         al = new System.Collections.ArrayList();\r
1909                         foreach (DataRow dr in dt.Rows )\r
1910                         {\r
1911                                 if (dr["String1"].ToString() == "1-String1")\r
1912                                         al.Add(dr);\r
1913                         }\r
1914                         //al.Reverse();\r
1915                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1916 \r
1917                         drSelect = dt.Select("String1='1-String1'","ParentId Desc");\r
1918                         Assert.AreEqual(al.ToArray(),drSelect ,"DT194");\r
1919                         \r
1920 \r
1921                         //get excepted resault\r
1922                         al = new System.Collections.ArrayList();\r
1923                         foreach (DataRow dr in dt.Rows )\r
1924                         {\r
1925                                 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")\r
1926                                         al.Add(dr);\r
1927                         }\r
1928                         //al.Reverse();\r
1929                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1930 \r
1931                         drSelect = dt.Select("ChildId=1 and String1='1-String1'","ParentId Desc");\r
1932                         Assert.AreEqual(al.ToArray(),drSelect ,"DT195");\r
1933                         \r
1934 \r
1935                         //get excepted resault\r
1936                         al = new System.Collections.ArrayList();\r
1937                         foreach (DataRow dr in dt.Rows )\r
1938                         {\r
1939                                 if (dr["String1"].ToString().Length < 4 )\r
1940                                         al.Add(dr);\r
1941                         }\r
1942                         //al.Reverse();\r
1943                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1944 \r
1945                         drSelect = dt.Select("Len(String1) < 4 ","ParentId Desc");\r
1946                         Assert.AreEqual(al.ToArray(),drSelect ,"DT196");\r
1947                         \r
1948 \r
1949                         //get excepted resault\r
1950                         al = new System.Collections.ArrayList();\r
1951                         foreach (DataRow dr in dt.Rows )\r
1952                         {\r
1953                                 if ( dr["String1"].ToString().IndexOf("String") > 0 )\r
1954                                         al.Add(dr);\r
1955                         }\r
1956                         //al.Reverse();\r
1957                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1958 \r
1959                         drSelect = dt.Select("String1 like '%%String*'  ","ParentId Desc");\r
1960                         Assert.AreEqual(al.ToArray(),drSelect ,"DT197");\r
1961                         \r
1962 \r
1963                         //get excepted resault\r
1964                         al = new System.Collections.ArrayList();\r
1965                         foreach (DataRow dr in dt.Rows )\r
1966                         {\r
1967                                 if (((int)dr["ChildId"] == 2) || ((int)dr["ChildId"] == 3))\r
1968                                         al.Add(dr);\r
1969                         }\r
1970                         //al.Reverse();\r
1971                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1972 \r
1973                         drSelect = dt.Select("ChildId in (2,3)  ","ParentId Desc");\r
1974                         Assert.AreEqual(al.ToArray(),drSelect ,"DT198");\r
1975                         \r
1976 \r
1977                         //get excepted resault\r
1978                         al = new System.Collections.ArrayList();\r
1979                         foreach (DataRow dr in dt.Rows )\r
1980                         {\r
1981                                 if ((((int)dr["ChildId"] * (int)dr["ParentId"]) > 5 ))\r
1982                                         al.Add(dr);\r
1983                         }\r
1984                         al.Sort(new DataRowsComparer("ChildId", "Asc"));\r
1985 \r
1986                         drSelect = dt.Select("ChildId * ParentId > 5 ","ChildId Asc");\r
1987                         Assert.AreEqual(al.ToArray(),drSelect ,"DT199");\r
1988                         \r
1989 \r
1990                         //get excepted resault\r
1991                         al = new System.Collections.ArrayList();\r
1992                         foreach (DataRow dr in dt.Rows )\r
1993                         {\r
1994                                 if (dr["String2"].ToString().Substring(2,3) == "Str" )\r
1995                                         al.Add(dr);\r
1996                         }\r
1997                         al.Sort(new DataRowsComparer("ParentId", "Desc"));\r
1998 \r
1999                         drSelect = dt.Select("SubString(String2,3,3) like 'Str' ","ParentId Desc");\r
2000                         Assert.AreEqual(al.ToArray(),drSelect ,"DT200");\r
2001                 }\r
2002 \r
2003                 [Test]\r
2004                 [ExpectedException(typeof(ArgumentException))]\r
2005 #if !TARGET_JVM\r
2006                 [Category ("NotWorking")]\r
2007 #endif\r
2008                 public void Select_StringString_2()\r
2009                 {\r
2010                         DataTable dt = DataProvider.CreateParentDataTable();\r
2011                         //Checking the parsing of the sort string\r
2012                         dt.Select(dt.Columns[0].ColumnName + "=1","[x");\r
2013                 }\r
2014 \r
2015                 [Test]\r
2016                 [ExpectedException(typeof(IndexOutOfRangeException))]\r
2017                 public void Select_StringString_3()\r
2018                 {       \r
2019                         DataTable dt = DataProvider.CreateParentDataTable();\r
2020                         //Select - parse sort string checking 1");\r
2021                         dt.Select(dt.Columns[0].ColumnName,dt.Columns[0].ColumnName + "1");\r
2022                 }
2023
2024                 internal class DataRowsComparer : System.Collections.IComparer\r
2025                 {\r
2026                         #region Memebers\r
2027                         private string _columnName;\r
2028                         private string _direction;\r
2029                         #endregion\r
2030 \r
2031                         #region Constructors\r
2032                         public DataRowsComparer(string columnName, string direction)\r
2033                         {\r
2034                                 _columnName = columnName;\r
2035                                 if (direction.ToLower() != "asc" && direction.ToLower() != "desc")\r
2036                                 {\r
2037                                         throw new ArgumentException("Direction can only be one of: 'asc' or 'desc'");\r
2038                                 }\r
2039                                 _direction = direction;\r
2040                         }\r
2041                         #endregion\r
2042 \r
2043                         #region IComparer Members\r
2044 \r
2045                         public int Compare(object x, object y)\r
2046                         {\r
2047                                 DataRow drX = (DataRow)x;\r
2048                                 DataRow drY = (DataRow)y;\r
2049 \r
2050                                 object objX = drX[_columnName];\r
2051                                 object objY = drY[_columnName];\r
2052 \r
2053                                 int compareResult = System.Collections.Comparer.Default.Compare(objX, objY);\r
2054 \r
2055                                 //If we are comparing desc we need to reverse the result.\r
2056                                 if (_direction.ToLower() == "desc")\r
2057                                 {\r
2058                                         compareResult = -compareResult;\r
2059                                 }\r
2060 \r
2061                                 return compareResult;\r
2062 \r
2063                         }\r
2064 \r
2065                         #endregion\r
2066 \r
2067                 }
2068         }
2069 }