Standardized Mainsoft System.Data tests except exception tests.
[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.Test.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
74                 [Test] public void AcceptChanges()
75                 {
76                         String sNewValue = "NewValue";
77                         DataRow drModified,drDeleted,drAdded;
78                         DataTable dt = DataProvider.CreateParentDataTable();
79
80                         drModified = dt.Rows[0];
81                         drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
82
83                         drDeleted = dt.Rows[1];
84                         drDeleted.Delete();             //DataRowState =  Deleted
85
86                         drAdded = dt.NewRow();                  
87                         dt.Rows.Add(drAdded);   //DataRowState =  Added
88
89                         dt.AcceptChanges();
90
91                         // AcceptChanges - Unchanged1
92                         Assert.AreEqual(DataRowState.Unchanged , drModified.RowState  , "DT1");
93
94                         // AcceptChanges - Current
95                         Assert.AreEqual(sNewValue , drModified[1,DataRowVersion.Current] , "DT2");
96
97                         // AcceptChanges - Unchanged2
98                         Assert.AreEqual(DataRowState.Unchanged , drAdded.RowState  , "DT3");
99
100                         // AcceptChanges - Detached
101                         Assert.AreEqual(DataRowState.Detached  , drDeleted.RowState  , "DT4");
102                 }
103
104                 [Test] public void ChildRelations()
105                 {
106                         DataTable dtChild,dtParent;
107                         DataSet ds = new DataSet();
108                         //Create tables
109                         dtChild = DataProvider.CreateChildDataTable();
110                         dtParent= DataProvider.CreateParentDataTable(); 
111                         //Add tables to dataset
112                         ds.Tables.Add(dtChild);
113                         ds.Tables.Add(dtParent);
114
115                         DataRelationCollection drlCollection;
116                         DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
117
118                         // Checking ChildRelations - default value
119                         //Check default
120                         drlCollection = dtParent.ChildRelations; 
121                         Assert.AreEqual(0, drlCollection.Count  , "DT5");
122
123                         ds.Relations.Add(drl);
124                         drlCollection = dtParent.ChildRelations; 
125
126                         // Checking ChildRelations Count
127                         Assert.AreEqual(1, drlCollection.Count  , "DT6");
128
129                         // Checking ChildRelations Value
130                         Assert.AreEqual(drl, drlCollection[0] , "DT7");
131                 }
132
133                 [Test] public void Clear()
134                 {
135                         DataTable dt = DataProvider.CreateParentDataTable();
136                         dt.Clear();
137                         // Clear
138                         Assert.AreEqual(0, dt.Rows.Count  , "DT8");
139                 }
140
141                 [Test] public void Clone()
142                 {
143                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
144                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
145                         dt2.Columns[0].DefaultValue=7;
146
147                         dt1 = dt2.Clone();
148
149                         for (int i=0; i<dt2.Constraints.Count; i++)
150                         {
151                                 // Clone - Constraints[{0}],i)
152                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT9");
153                         }
154
155                         for (int i=0; i<dt2.Columns.Count; i++)
156                         {
157                                 // Clone - Columns[{0}].ColumnName,i)
158                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT10");
159
160                                 // Clone - Columns[{0}].DataType,i)
161                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT11");
162                         }
163                 }
164
165                 [Test] public void ColumnChanged()
166                 {
167                         DataTable dt = DataProvider.CreateParentDataTable();
168
169                         dt.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
170
171                         _EventTriggered=false;
172                         // ColumnChanged - EventTriggered
173                         dt.Rows[0][1] = "NewValue";
174                         Assert.AreEqual(true , _EventTriggered , "DT12");
175
176                         _EventTriggered=false;
177                         dt.ColumnChanged -= new DataColumnChangeEventHandler( Column_Changed );
178                         // ColumnChanged - NO EventTriggered
179                         dt.Rows[0][1] = "VeryNewValue";
180                         Assert.AreEqual(false , _EventTriggered , "DT13");
181                 }
182
183                 private void Column_Changed( object sender, DataColumnChangeEventArgs e )
184                 {
185                         _EventTriggered = true;
186                 }
187
188                 [Test] public void ColumnChanging()
189                 {
190                         DataTable dt = DataProvider.CreateParentDataTable();
191
192                         dt.ColumnChanging  += new DataColumnChangeEventHandler( Column_Changeding );
193
194                         _EventTriggered=false;
195                         // ColumnChanged - EventTriggered
196                         dt.Rows[0][1] = "NewValue";
197                         Assert.AreEqual(true , _EventTriggered , "DT14");
198
199                         _EventTriggered=false;
200                         dt.ColumnChanging  -= new DataColumnChangeEventHandler( Column_Changeding );
201                         // ColumnChanged - NO EventTriggered
202                         dt.Rows[0][1] = "VeryNewValue";
203                         Assert.AreEqual(false , _EventTriggered , "DT15");
204                 }
205
206                 private void Column_Changeding( object sender, DataColumnChangeEventArgs e )
207                 {
208                         _EventTriggered = true;
209                 }
210
211                 [Test] public void Columns()
212                 {
213                         DataTable dtParent;
214                         DataColumnCollection dcl;
215                         dtParent= DataProvider.CreateParentDataTable(); 
216
217                         dcl = dtParent.Columns; 
218
219                         // Checking ColumnsCollection != null 
220                         Assert.AreEqual(false, dcl == null  , "DT16");
221
222                         // Checking ColumnCollection Count  1
223                         Assert.AreEqual(6, dcl.Count  , "DT17");
224
225                         // Checking ColumnCollection Count 2
226                         dtParent.Columns.Add(new DataColumn("Test"));
227                         Assert.AreEqual(7, dcl.Count  , "DT18");
228
229                         // Checking ColumnCollection - get columnn by different case
230                         DataColumn tmp = dtParent.Columns["TEST"];
231                         Assert.AreEqual(dtParent.Columns["Test"], tmp  , "DT19");
232
233                         // Checking ColumnCollection colummn name case sensetive
234                         dtParent.Columns.Add(new DataColumn("test"));
235                         Assert.AreEqual(8, dcl.Count  , "DT20");
236
237                         // Checking ColumnCollection - get columnn by different case,ArgumentException
238                         try
239                         {
240                                 DataColumn tmp1 = dtParent.Columns["TEST"];
241                                 Assert.Fail("DT21: indexer Failed to throw ArgumentException");
242                         }
243                         catch (ArgumentException) {}
244                         catch (AssertionException exc) {throw  exc;}
245                         catch (Exception exc)
246                         {
247                                 Assert.Fail("DT22: Indexer. Wrong exception type. Got:" + exc);
248                         }
249                 }
250
251                 [Test] public void Compute()
252                 {
253                         DataTable dt = DataProvider.CreateChildDataTable();
254
255                         //Get expected
256                         DataRow[] drArr = dt.Select("ParentId=1");
257                         Int64 iExSum = 0;
258                         foreach (DataRow dr in drArr)
259                         {
260                                 iExSum += (int)dr["ChildId"];
261                         }
262                         object objCompute=null;
263                         // Compute - sum values
264                         objCompute = dt.Compute("Sum(ChildId)","ParentId=1");
265                         Assert.AreEqual(Int64.Parse(objCompute.ToString()) , Int64.Parse(iExSum.ToString()), "DT23");
266
267                         // Compute - sum type
268                         Assert.AreEqual(typeof(Int64).FullName , objCompute.GetType().FullName, "DT24");
269
270                         //get expected
271                         double iExAvg = 0;
272                         drArr = dt.Select("ParentId=5");
273                         foreach (DataRow dr in drArr)
274                         {
275                                 iExAvg += (double)dr["ChildDouble"];
276                         }
277                         iExAvg = iExAvg / drArr.Length;
278
279                         // Compute - Avg value
280                         objCompute = dt.Compute("Avg(ChildDouble)","ParentId=5");
281                         Assert.AreEqual(double.Parse(objCompute.ToString()) , double.Parse(iExAvg.ToString()), "DT25");
282
283                         // Compute - Avg type
284                         Assert.AreEqual(typeof(double).FullName , objCompute.GetType().FullName, "DT26");
285                 }
286
287                 [Test] public void Constraints()
288                 {
289                         DataTable dtParent;
290                         ConstraintCollection consColl;
291                         dtParent= DataProvider.CreateParentDataTable(); 
292
293                         consColl = dtParent.Constraints;
294                         // Checking Constraints  != null 
295                         Assert.AreEqual(false, consColl == null  , "DT27");
296
297                         // Checking Constraints Count
298                         Assert.AreEqual(0, consColl.Count  , "DT28");
299
300                         // Checking Constraints Count
301                         //Add primary key
302                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
303                         Assert.AreEqual(1, consColl.Count   , "DT29");
304                 }
305
306                 [Test] public void Copy()
307                 {
308                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
309                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
310                         dt2.Columns[0].DefaultValue=7;
311
312                         dt1 = dt2.Copy();
313
314                         for (int i=0; i<dt2.Constraints.Count; i++)
315                         {
316                                 // Copy - Constraints[{0}],i)
317                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT30");
318                         }
319
320                         for (int i=0; i<dt2.Columns.Count; i++)
321                         {
322                                 // Copy - Columns[{0}].ColumnName,i)
323                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT31");
324
325                                 // Copy - Columns[{0}].DataType,i)
326                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT32");
327                         }
328
329                         DataRow[] drArr1,drArr2;
330                         drArr1 = dt1.Select("");
331                         drArr2 = dt2.Select("");
332                         for (int i=0; i<drArr1.Length ; i++)
333                         {
334                                 // Copy - Data [ParentId]{0} ,i)
335                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
336                                 // Copy - Data [String1]{0} ,i)
337                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
338                                 // Copy - Data [String2]{0} ,i)
339                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
340                         }
341                 }
342
343                 [Test] public void CreateInstance()
344                 {
345                         // CreateInstance
346                         ProtectedTestClass C = new ProtectedTestClass();
347                         DataTable dt = C.CreateInstance_Test();
348                         Assert.AreEqual(true , dt != null , "DT36");
349                 }
350
351                 [Test] public void DataSet()
352                 {
353                         DataTable dtParent;
354                         DataSet ds;
355                         dtParent= DataProvider.CreateParentDataTable(); 
356
357                         ds = dtParent.DataSet; 
358
359                         // Checking DataSet == null
360                         Assert.AreEqual(null, ds, "DT37");
361
362                         // Checking DataSet != null
363                         ds = new DataSet("MyDataSet");
364                         ds.Tables.Add(dtParent);
365                         Assert.AreEqual(true, dtParent.DataSet != null , "DT38");
366
367                         // Checking DataSet Name
368                         Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName  , "DT39");
369                 }
370
371                 [Test] public void DefaultView()
372                 {
373                         DataTable dtParent;
374                         DataView dv;
375                         dtParent= DataProvider.CreateParentDataTable(); 
376                         dv = dtParent.DefaultView ;
377
378                         // Checking DataView != null
379                         Assert.AreEqual(true, dv != null, "DT40");
380                 }
381
382                 [Test] public void EndLoadData()
383                 {
384                         DataTable dt = DataProvider.CreateParentDataTable();
385                         dt.Columns[0].AllowDBNull = false;
386
387                         // EndLoadData
388                         dt.BeginLoadData();
389                         dt.LoadDataRow(new object[] {null,"A","B"},false);
390
391                         try
392                         {
393                                 //ConstraintException will be throw
394                                 dt.EndLoadData();
395                                 Assert.Fail("DT41: EndLoadData Failed to throw ConstraintException");
396                         }
397                         catch (ConstraintException) {}
398                         catch (AssertionException exc) {throw  exc;}
399                         catch (Exception exc)
400                         {
401                                 Assert.Fail("DT42: EndLoadData Wrong exception type. Got:" + exc);
402                         }
403                 }
404
405                 [Test] public void GetChanges()
406                 {
407                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
408                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
409                         dt2.Columns[0].DefaultValue=7;
410
411                         //make some changes
412                         dt2.Rows[0].Delete();
413                         dt2.Rows[1].Delete();
414                         dt2.Rows[2].Delete();
415                         dt2.Rows[3].Delete();
416
417                         dt1 = dt2.GetChanges();
418
419                         for (int i=0; i<dt2.Constraints.Count; i++)
420                         {
421                                 // GetChanges - Constraints[{0}],i)
422                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT43");
423                         }
424
425                         for (int i=0; i<dt2.Columns.Count; i++)
426                         {
427                                 // GetChanges - Columns[{0}].ColumnName,i)
428                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT44");
429
430                                 // GetChanges - Columns[{0}].DataType,i)
431                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT45");
432                         }
433
434                         DataRow[] drArr1,drArr2;
435
436                         drArr1 = dt1.Select("","",DataViewRowState.Deleted );
437                         drArr2 = dt2.Select("","",DataViewRowState.Deleted );
438
439                         for (int i=0; i<drArr1.Length ; i++)
440                         {
441                                 // GetChanges - Data [ParentId]{0} ,i)
442                                 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
443                                 // GetChanges - Data [String1]{0} ,i)
444                                 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original],  "DT47");
445                                 // GetChanges - Data [String2]{0} ,i)
446                                 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original],  "DT48");
447                         }
448                 }
449
450                 [Test] public void GetChanges_ByDataRowState()
451                 {
452                         DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
453                         dt2.Constraints.Add("Unique",dt2.Columns[0],true);
454                         dt2.Columns[0].DefaultValue=7;
455
456                         //make some changes
457                         dt2.Rows[0].Delete(); //DataRowState.Deleted 
458                         dt2.Rows[1].Delete(); //DataRowState.Deleted 
459                         dt2.Rows[2].BeginEdit();
460                         dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified 
461                         dt2.Rows[2].EndEdit();
462
463                         dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added 
464
465                         // *********** Checking GetChanges - DataRowState.Deleted ************
466                         dt1=null;
467                         dt1 = dt2.GetChanges(DataRowState.Deleted);
468                         CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
469                         DataRow[] drArr1,drArr2;
470                         drArr1 = dt1.Select("","",DataViewRowState.Deleted );
471                         drArr2 = dt2.Select("","",DataViewRowState.Deleted );
472
473                         for (int i=0; i<drArr1.Length ; i++)
474                         {
475                                 // GetChanges(Deleted) - Data [ParentId]{0} ,i)
476                                 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original],  "DT49");
477                                 // GetChanges(Deleted) - Data [String1]{0} ,i)
478                                 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original],  "DT50");
479                                 // GetChanges(Deleted) - Data [String2]{0} ,i)
480                                 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original],  "DT51");
481                         }
482
483                         // *********** Checking GetChanges - DataRowState.Modified ************
484                         dt1=null;
485                         dt1 = dt2.GetChanges(DataRowState.Modified);
486                         CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
487                         drArr1 = dt1.Select("","");
488                         drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
489
490                         for (int i=0; i<drArr1.Length ; i++)
491                         {
492                                 // GetChanges(Modified) - Data [ParentId]{0} ,i)
493                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT52");
494                                 // GetChanges(Modified) - Data [String1]{0} ,i)
495                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT53");
496                                 // GetChanges(Modified) - Data [String2]{0} ,i)
497                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT54");
498                         }
499
500                         // *********** Checking GetChanges - DataRowState.Added ************
501                         dt1=null;
502                         dt1 = dt2.GetChanges(DataRowState.Added);
503                         CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
504                         drArr1 = dt1.Select("","");
505                         drArr2 = dt2.Select("","",DataViewRowState.Added );
506
507                         for (int i=0; i<drArr1.Length ; i++)
508                         {
509                                 // GetChanges(Added) - Data [ParentId]{0} ,i)
510                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT55");
511                                 // GetChanges(Added) - Data [String1]{0} ,i)
512                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT56");
513                                 // GetChanges(Added) - Data [String2]{0} ,i)
514                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT57");
515                         }
516
517                         // *********** Checking GetChanges - DataRowState.Unchanged  ************
518                         dt1=null;
519                         dt1 = dt2.GetChanges(DataRowState.Unchanged);
520                         CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
521                         drArr1 = dt1.Select("","");
522                         drArr2 = dt2.Select("","",DataViewRowState.Unchanged  );
523
524                         for (int i=0; i<drArr1.Length ; i++)
525                         {
526                                 // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
527                                 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT58");
528                                 // GetChanges(Unchanged) - Data [String1]{0} ,i)
529                                 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT59");
530                                 // GetChanges(Unchanged) - Data [String2]{0} ,i)
531                                 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT60");
532                         }
533                 }
534
535                 private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
536                 {
537                         for (int i=0; i<dt2.Constraints.Count; i++)
538                         {
539                                 // GetChanges - Constraints[{0}] - {1},i,Description)
540                                 Assert.AreEqual(dt2.Constraints[i].ConstraintName ,  dt1.Constraints[i].ConstraintName , "DT61");
541                         }
542
543                         for (int i=0; i<dt2.Columns.Count; i++)
544                         {
545                                 // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
546                                 Assert.AreEqual(dt2.Columns[i].ColumnName ,  dt1.Columns[i].ColumnName  , "DT62");
547
548                                 // GetChanges - Columns[{0}].DataType {1},i,Description)
549                                 Assert.AreEqual(dt2.Columns[i].DataType ,  dt1.Columns[i].DataType , "DT63");
550                         }
551                 }
552
553                 [Test] public void GetErrors()
554                 {
555                         DataTable dt = DataProvider.CreateParentDataTable();
556                         DataRow[] drArr = new DataRow[3];
557                         drArr[0] = dt.Rows[0];
558                         drArr[1] = dt.Rows[2];
559                         drArr[2] = dt.Rows[5];
560
561                         drArr[0].RowError = "Error1";
562                         drArr[1].RowError = "Error2";
563                         drArr[2].RowError = "Error3";
564
565                         // GetErrors
566                         Assert.AreEqual(dt.GetErrors(), drArr , "DT64");
567                 }
568
569                 [Test] public new void GetHashCode()
570                 {
571                         DataTable dt = DataProvider.CreateParentDataTable();
572                         int iHashCode;
573                         iHashCode = dt.GetHashCode();        
574
575                         for (int i=0; i<10 ;i++)
576                         {
577                                 // HashCode - i= + i.ToString()
578                                 Assert.AreEqual(dt.GetHashCode()  , iHashCode , "DT65");
579                         }
580                 }
581
582                 [Test] public new void GetType()
583                 {
584                         DataTable dt = DataProvider.CreateParentDataTable();
585                         Type tmpType = typeof(DataTable);
586
587                         // GetType
588                         Assert.AreEqual(tmpType,  dt.GetType() , "DT66");
589                 }
590
591                 [Test] public void HasErrors()
592                 {
593                         DataTable dtParent;
594                         dtParent= DataProvider.CreateParentDataTable(); 
595
596                         // Checking HasErrors default 
597                         Assert.AreEqual(false, dtParent.HasErrors , "DT67");
598
599                         // Checking HasErrors Get 
600                         dtParent.Rows[0].RowError = "Error on row 0";
601                         dtParent.Rows[2].RowError = "Error on row 2";
602                         Assert.AreEqual(true, dtParent.HasErrors , "DT68");
603                 }
604
605                 [Test] public void ImportRow()
606                 {
607                         DataTable dt1,dt2;
608                         dt1 = DataProvider.CreateParentDataTable();
609                         dt2 = DataProvider.CreateParentDataTable();
610                         DataRow dr = dt2.NewRow();
611                         dr.ItemArray = new object[] {99,"",""};
612                         dt2.Rows.Add(dr);
613
614                         // ImportRow - Values
615                         dt1.ImportRow(dr);
616                         Assert.AreEqual(dr.ItemArray,  dt1.Rows[dt1.Rows.Count-1].ItemArray  , "DT69");
617
618                         // ImportRow - DataRowState
619                         Assert.AreEqual(dr.RowState ,  dt1.Rows[dt1.Rows.Count-1].RowState , "DT70");
620                 }
621
622                 [Test] public void LoadDataRow()
623                 {
624                         DataTable dt;
625                         DataRow dr;
626                         dt = DataProvider.CreateParentDataTable();
627                         dt.PrimaryKey= new DataColumn[] {dt.Columns[0]};        //add ParentId as Primary Key
628                         dt.Columns["String1"].DefaultValue = "Default";
629
630                         dr = dt.Select("ParentId=1")[0];
631
632                         //Update existing row without accept changes
633                         dt.BeginLoadData();
634                         dt.LoadDataRow(new object[] {1,null,"Changed"},false);
635                         dt.EndLoadData();
636
637                         // LoadDataRow(update1) - check column String1
638                         Assert.AreEqual(dr["String1"], dt.Columns["String1"].DefaultValue   , "DT71");
639
640                         // LoadDataRow(update1) - check column String2
641                         Assert.AreEqual(dr["String2"], "Changed", "DT72");
642
643                         // LoadDataRow(update1) - check row state
644                         Assert.AreEqual(DataRowState.Modified , dr.RowState , "DT73");
645
646                         //Update existing row with accept changes
647                         dr = dt.Select("ParentId=2")[0];
648
649                         dt.BeginLoadData();
650                         dt.LoadDataRow(new object[] {2,null,"Changed"},true);
651                         dt.EndLoadData();
652
653                         // LoadDataRow(update2) - check row state
654                         Assert.AreEqual(DataRowState.Unchanged  , dr.RowState , "DT74");
655
656                         //Add New row without accept changes
657                         dt.BeginLoadData();
658                         dt.LoadDataRow(new object[] {99,null,"Changed"},false);
659                         dt.EndLoadData();
660
661                         // LoadDataRow(insert1) - check column String2
662                         dr = dt.Select("ParentId=99")[0];
663                         Assert.AreEqual("Changed" , dr["String2"] , "DT75");
664
665                         // LoadDataRow(insert1) - check row state
666                         Assert.AreEqual(DataRowState.Added   , dr.RowState , "DT76");
667
668                         //Add New row with accept changes
669                         dt.BeginLoadData();
670                         dt.LoadDataRow(new object[] {100,null,"Changed"},true);
671                         dt.EndLoadData();
672
673                         // LoadDataRow(insert2) - check row and values
674                         dr = dt.Select("ParentId=100")[0];
675                         Assert.AreEqual("Changed" , dr["String2"] , "DT77");
676
677                         // LoadDataRow(insert2) - check row state
678                         Assert.AreEqual(DataRowState.Unchanged    , dr.RowState , "DT78");
679                 }
680
681                 [Test] public void Locale()
682                 {
683                         DataTable dtParent;
684                         DataSet ds = new DataSet("MyDataSet");
685
686                         dtParent= DataProvider.CreateParentDataTable(); 
687                         ds.Tables.Add(dtParent);
688                         System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
689
690                         // Checking Locale default from system 
691                         Assert.AreEqual(culInfo, dtParent.Locale  , "DT79");
692
693                         // Checking Locale default from dataset 
694                         culInfo = new System.Globalization.CultureInfo("fr"); // = French
695                         ds.Locale = culInfo;
696                         Assert.AreEqual(culInfo , dtParent.Locale , "DT80");
697
698                         // Checking Locale get/set 
699                         culInfo = new System.Globalization.CultureInfo("fr"); // = French
700                         dtParent.Locale = culInfo ;
701                         Assert.AreEqual(culInfo , dtParent.Locale , "DT81");
702                 }
703
704                 [Test] public void MinimumCapacity()
705                 {
706                         //                              i get default=50, according to MSDN the value should be 25 
707                         //                              // Checking MinimumCapacity default = 25 
708                         //                              Assert.AreEqual(25, dtParent.MinimumCapacity , "DT82");
709                         //                              EndCase(null);
710                         DataTable dt = new DataTable();
711
712                         // Checking MinimumCapacity get/set int.MaxValue 
713                         dt.MinimumCapacity = int.MaxValue; 
714                         Assert.AreEqual(int.MaxValue, dt.MinimumCapacity  , "DT83");
715
716                         // Checking MinimumCapacity get/set 0
717                         dt.MinimumCapacity = 0; 
718                         Assert.AreEqual(0, dt.MinimumCapacity  , "DT84");
719
720                         //                              // Checking MinimumCapacity get/set int.MinValue 
721                         //                              dtParent.MinimumCapacity = int.MinValue ; 
722                         //                              Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity  , "DT85");
723                         //                              EndCase(null);
724                 }
725
726                 [Test] public void Namespace()
727                 {
728                         DataTable dtParent = new DataTable();
729
730                         // Checking Namespace default
731                         Assert.AreEqual(String.Empty, dtParent.Namespace, "DT86");
732
733                         // Checking Namespace set/get
734                         String s = "MyNamespace";
735                         dtParent.Namespace=s;
736                         Assert.AreEqual(s, dtParent.Namespace, "DT87");
737                 }
738
739                 [Test] public void NewRow()
740                 {
741                         DataTable dt;
742                         DataRow dr;
743                         dt = DataProvider.CreateParentDataTable();
744
745                         // NewRow
746                         dr = dt.NewRow();
747                         Assert.AreEqual(true , dr != null , "DT88");
748                 }
749
750                 [Test] public void OnColumnChanged()
751                 {
752                         ProtectedTestClass dt = new ProtectedTestClass(); 
753
754                         EventRaised = false;
755                         dt.OnColumnChanged_Test();
756                         // OnColumnChanged Event 1
757                         Assert.AreEqual(false , EventRaised , "DT89");
758                         EventRaised = false;
759                         EventValues = false;
760                         dt.ColumnChanged += new DataColumnChangeEventHandler(OnColumnChanged_Handler);
761                         dt.OnColumnChanged_Test();
762                         // OnColumnChanged Event 2
763                         Assert.AreEqual(true , EventRaised , "DT90");
764                         // OnColumnChanged Values
765                         Assert.AreEqual(true , EventValues , "DT91");
766                         dt.ColumnChanged -= new DataColumnChangeEventHandler(OnColumnChanged_Handler);
767                 }
768
769                 private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
770                 {
771                         DataTable dt = (DataTable)sender;
772                         if ( (e.Column.Equals(dt.Columns["Value"])      )       &&
773                                 (e.Row.Equals(dt.Rows[0])                               )       &&
774                                 (e.ProposedValue.Equals("NewValue"))    )
775                         {
776                                 EventValues = true;
777                         }
778                         else
779                         {
780                                 EventValues = false;
781                         }
782                         EventRaised = true;
783                 }
784
785                 [Test] public void OnColumnChanging()
786                 {
787                         ProtectedTestClass dt = new ProtectedTestClass(); 
788
789                         EventRaised = false;
790                         dt.OnColumnChanging_Test();
791                         // OnColumnChanging Event 1
792                         Assert.AreEqual(false , EventRaised , "DT92");
793                         EventRaised = false;
794                         EventValues = false;
795                         dt.ColumnChanging  += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
796                         dt.OnColumnChanging_Test();
797                         // OnColumnChanging Event 2
798                         Assert.AreEqual(true , EventRaised , "DT93");
799                         // OnColumnChanging Values
800                         Assert.AreEqual(true , EventValues , "DT94");
801                         dt.ColumnChanging -= new DataColumnChangeEventHandler(OnColumnChanging_Handler);
802                 }
803
804                 private void OnColumnChanging_Handler(Object sender,DataColumnChangeEventArgs e)
805                 {
806                         DataTable dt = (DataTable)sender;
807                         if ( (e.Column.Equals(dt.Columns["Value"])      )       &&
808                                 (e.Row.Equals(dt.Rows[0])                               )       &&
809                                 (e.ProposedValue.Equals("NewValue"))    )
810                         {
811                                 EventValues = true;
812                         }
813                         else
814                         {
815                                 EventValues = false;
816                         }
817                         EventRaised = true;
818                 }
819
820                 [Test] public void OnRemoveColumn()
821                 {
822                         ProtectedTestClass dt = new ProtectedTestClass(); 
823
824                         // OnRemoveColumn 
825                         dt.OnRemoveColumn_Test();
826                 }
827
828                 [Test] public void ParentRelations()
829                 {
830                         DataTable dtChild,dtParent;
831                         DataSet ds = new DataSet();
832                         //Create tables
833                         dtChild = DataProvider.CreateChildDataTable();
834                         dtParent= DataProvider.CreateParentDataTable(); 
835                         //Add tables to dataset
836                         ds.Tables.Add(dtChild);
837                         ds.Tables.Add(dtParent);
838
839                         DataRelationCollection drlCollection;
840                         DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
841
842                         // Checking ParentRelations - default value
843                         //Check default
844                         drlCollection = dtChild.ParentRelations; 
845                         Assert.AreEqual(0, drlCollection.Count  , "DT96");
846
847                         ds.Relations.Add(drl);
848                         drlCollection = dtChild.ParentRelations; 
849
850                         // Checking ParentRelations Count
851                         Assert.AreEqual(1, drlCollection.Count  , "DT97");
852
853                         // Checking ParentRelations Value
854                         Assert.AreEqual(drl, drlCollection[0] , "DT98");
855                 }
856
857                 [Test] public void Prefix()
858                 {
859                         DataTable dtParent = new DataTable();
860
861                         // Checking Prefix default
862                         Assert.AreEqual(String.Empty, dtParent.Prefix , "DT99");
863
864                         // Checking Prefix set/get
865                         String s = "MyPrefix";
866                         dtParent.Prefix=s;
867                         Assert.AreEqual(s, dtParent.Prefix, "DT100");
868                 }
869
870                 [Test] public void RejectChanges()
871                 {
872                         String sNewValue = "NewValue";
873                         DataRow drModified,drDeleted,drAdded;
874                         DataTable dt = DataProvider.CreateParentDataTable();
875
876                         drModified = dt.Rows[0];
877                         drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
878
879                         drDeleted = dt.Rows[1];
880                         drDeleted.Delete();             //DataRowState =  Deleted
881
882                         drAdded = dt.NewRow();                  
883                         dt.Rows.Add(drAdded);   //DataRowState =  Added
884
885                         dt.RejectChanges();
886
887                         // RejectChanges - Unchanged1
888                         Assert.AreEqual(DataRowState.Unchanged , drModified.RowState  , "DT101");
889
890                         // RejectChanges - Unchanged2
891                         Assert.AreEqual(DataRowState.Detached , drAdded.RowState  , "DT102");
892
893                         // RejectChanges - Detached
894                         Assert.AreEqual(DataRowState.Unchanged   , drDeleted.RowState  , "DT103");
895                 }                       
896
897                 [Test] public void Reset()
898                 {
899                         DataTable dt1 = DataProvider.CreateParentDataTable();
900                         DataTable dt2 = DataProvider.CreateChildDataTable();
901                         dt1.PrimaryKey  = new DataColumn[] {dt1.Columns[0]};
902                         dt2.PrimaryKey  = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
903                         DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
904                         DataSet ds = new DataSet();
905                         ds.Tables.AddRange(new DataTable[] {dt1,dt2});
906                         ds.Relations.Add(rel);
907
908                         dt2.Reset();
909
910                         // Reset - ParentRelations
911                         Assert.AreEqual(0 , dt2.ParentRelations.Count  , "DT104");
912                         // Reset - Constraints
913                         Assert.AreEqual(0 , dt2.Constraints.Count  , "DT105");
914                         // Reset - Rows
915                         Assert.AreEqual(0 , dt2.Rows.Count  , "DT106");
916                         // Reset - Columns
917                         Assert.AreEqual(0 , dt2.Columns.Count  , "DT107");
918                 }
919
920                 [Test] public void RowChanged()
921                 {
922                         DataTable dt = DataProvider.CreateParentDataTable();
923
924                         dt.RowChanged += new DataRowChangeEventHandler ( Row_Changed );
925
926                         _EventTriggered=false;
927                         // RowChanged - 1
928                         dt.Rows[0][1] = "NewValue";
929                         Assert.AreEqual(true , _EventTriggered , "DT108");
930
931                         _EventTriggered=false;
932                         // RowChanged - 2
933                         dt.Rows[0].BeginEdit(); 
934                         dt.Rows[0][1] = "NewValue";
935                         Assert.AreEqual(false , _EventTriggered , "DT109");
936
937                         _EventTriggered=false;
938                         // RowChanged - 3
939                         dt.Rows[0].EndEdit();
940                         Assert.AreEqual(true , _EventTriggered , "DT110");
941
942                         _EventTriggered=false;
943                         dt.RowChanged -= new DataRowChangeEventHandler  ( Row_Changed );
944                         // RowChanged - 4
945                         dt.Rows[0][1] = "NewValue A";
946                         Assert.AreEqual(false , _EventTriggered , "DT111");
947                 }
948
949                 private void Row_Changed( object sender, DataRowChangeEventArgs e )
950                 {
951                         _EventTriggered = true;
952                 }
953
954                 [Test] public void RowChanging()
955                 {
956                         DataTable dt = DataProvider.CreateParentDataTable();
957
958                         dt.RowChanging  += new DataRowChangeEventHandler ( Row_Changing );
959
960                         _EventTriggered=false;
961                         // RowChanging - 1
962                         dt.Rows[0][1] = "NewValue";
963                         Assert.AreEqual(true , _EventTriggered , "DT112");
964
965                         _EventTriggered=false;
966                         // RowChanging - 2
967                         dt.Rows[0].BeginEdit(); 
968                         dt.Rows[0][1] = "NewValue";
969                         Assert.AreEqual(false , _EventTriggered , "DT113");
970
971                         _EventTriggered=false;
972                         // RowChanging - 3
973                         dt.Rows[0].EndEdit();
974                         Assert.AreEqual(true , _EventTriggered , "DT114");
975
976                         _EventTriggered=false;
977                         dt.RowChanging  -= new DataRowChangeEventHandler ( Row_Changing );
978                         // RowChanging - 4
979                         dt.Rows[0][1] = "NewValue A";
980                         Assert.AreEqual(false , _EventTriggered , "DT115");
981                 }
982
983                 private void Row_Changing( object sender, DataRowChangeEventArgs e )
984                 {
985                         _EventTriggered = true;
986                 }
987
988                 [Test] public void RowDeleted()
989                 {
990                         DataTable dt = DataProvider.CreateParentDataTable();
991
992                         dt.RowDeleted += new DataRowChangeEventHandler ( Row_Deleted );
993
994                         _EventTriggered=false;
995                         // RowDeleted - 1
996                         dt.Rows[0].Delete();
997                         Assert.AreEqual(true , _EventTriggered , "DT116");
998
999                         _EventTriggered=false;
1000                         dt.RowDeleted -= new DataRowChangeEventHandler ( Row_Deleted );
1001                         // RowDeleted - 2
1002                         dt.Rows[1].Delete();
1003                         Assert.AreEqual(false , _EventTriggered , "DT117");
1004                 }
1005
1006                 private void Row_Deleted( object sender, DataRowChangeEventArgs e )
1007                 {
1008                         _EventTriggered = true;
1009                 }
1010
1011                 [Test] public void RowDeleting()
1012                 {
1013                         DataTable dt = DataProvider.CreateParentDataTable();
1014
1015                         dt.RowDeleting  += new DataRowChangeEventHandler ( Row_Deleting );
1016
1017                         _EventTriggered=false;
1018                         // RowDeleting - 1
1019                         dt.Rows[0].Delete();
1020                         Assert.AreEqual(true , _EventTriggered , "DT118");
1021
1022                         _EventTriggered=false;
1023                         dt.RowDeleting  -= new DataRowChangeEventHandler ( Row_Deleting );
1024                         // RowDeleting - 2
1025                         dt.Rows[1].Delete();
1026                         Assert.AreEqual(false , _EventTriggered , "DT119");
1027                 }
1028
1029                 private void Row_Deleting( object sender, DataRowChangeEventArgs e )
1030                 {
1031                         _EventTriggered = true;
1032                 }
1033
1034                 [Test] public void Rows()
1035                 {
1036                         DataTable dtParent;
1037                         dtParent = DataProvider.CreateParentDataTable();
1038
1039                         // Checking Rows
1040                         Assert.AreEqual(true, dtParent.Rows != null, "DT120");
1041
1042                         // Checking rows count
1043                         Assert.AreEqual(true, dtParent.Rows.Count > 0 , "DT121");
1044                 }
1045
1046                 [Test] public void Select()
1047                 {
1048                         DataTable dt = DataProvider.CreateParentDataTable();
1049
1050                         DataRow[] drSelect = dt.Select();
1051                         DataRow[] drResult = new DataRow[dt.Rows.Count] ;
1052                         dt.Rows.CopyTo(drResult,0);
1053
1054                         // Select
1055
1056                         Assert.AreEqual(drResult, drSelect , "DT122");
1057                 }
1058
1059                 [Test] public void Select_ByFilter()
1060                 {
1061                         DataSet ds = new DataSet();
1062                         ds.Tables.Add(DataProvider.CreateParentDataTable());
1063
1064                         DataTable dt = DataProvider.CreateChildDataTable();
1065                         ds.Tables.Add(dt);
1066                         DataRow[] drSelect = null;
1067                         System.Collections.ArrayList al = new System.Collections.ArrayList();
1068
1069                         //add column with special name
1070                         DataColumn dc = new DataColumn("Column#",typeof(int));
1071                         dc.DefaultValue=-1;
1072                         dt.Columns.Add(dc);
1073                         //put some values
1074                         dt.Rows[0][dc] = 100;
1075                         dt.Rows[1][dc] = 200;
1076                         dt.Rows[2][dc] = 300;
1077                         dt.Rows[4][dc] = -400;
1078
1079                         //for trim function
1080                         dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + "   \t\n ";
1081                         dt.Rows[0]["String1"] = "   \t\n " + dt.Rows[0]["String1"] ;
1082                         dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + "    ";
1083
1084                         ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
1085                         ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
1086                         ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
1087
1088                         //-------------------------------------------------------------
1089                         al.Clear();
1090                         foreach (DataRow dr in dt.Rows ) 
1091                         {
1092                                 if ((int)dr["ChildId"] == 1)
1093                                 {
1094                                         al.Add(dr);
1095                                 }
1096                         }
1097                         // Select_S - ChildId=1
1098                         drSelect = dt.Select("ChildId=1");
1099                         Assert.AreEqual(al.ToArray(), drSelect , "DT123");
1100
1101                         //-------------------------------------------------------------
1102                         al.Clear();
1103                         foreach (DataRow dr in dt.Rows ) 
1104                         {
1105                                 if ((int)dr["ChildId"] == 1)
1106                                 {
1107                                         al.Add(dr);
1108                                 }
1109                         }
1110                         // Select_S - ChildId='1'
1111                         drSelect = dt.Select("ChildId='1'");
1112                         Assert.AreEqual(al.ToArray(), drSelect , "DT124");
1113                         //-------------------------------------------------------------
1114                         // Select_S - ChildId= '1'  (whitespace in filter string.
1115                         drSelect = dt.Select("ChildId= '1'");
1116                         Assert.AreEqual(al.ToArray(), drSelect , "DT125");
1117                         //-------------------------------------------------------------
1118                         al.Clear();
1119                         foreach (DataRow dr in dt.Rows ) if (dr["String1"].ToString() == "1-String1") al.Add(dr);
1120                         // Select_S - String1='1-String1'
1121                         drSelect = dt.Select("String1='1-String1'");
1122                         Assert.AreEqual(al.ToArray(), drSelect , "DT126");
1123
1124                         //-------------------------------------------------------------
1125                         al.Clear();
1126                         foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) al.Add(dr);
1127                         // Select_S - ChildId=1 and String1='1-String1'
1128                         drSelect = dt.Select("ChildId=1 and String1='1-String1'");
1129                         Assert.AreEqual(al.ToArray(), drSelect , "DT127");
1130
1131                         //-------------------------------------------------------------
1132                         al.Clear();
1133                         foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4 ) al.Add(dr);
1134                         // Select_S - ChildId+ParentId >= 4
1135                         drSelect = dt.Select("ChildId+ParentId >= 4");
1136                         CompareUnSorted(drSelect ,al.ToArray());
1137
1138                         //-------------------------------------------------------------
1139                         al.Clear();
1140                         foreach (DataRow dr in dt.Rows ) 
1141                         {
1142                                 if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0 )
1143                                 {
1144                                         al.Add(dr);
1145                                 }
1146                         }
1147                         // Select_S - ChildId-ParentId) * -1  <> 0
1148                         drSelect = dt.Select("(ChildId-ParentId) * -1  <> 0");
1149                         CompareUnSorted(drSelect ,al.ToArray());
1150
1151                         //-------------------------------------------------------------
1152                         al.Clear();
1153                         foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4 ) al.Add(dr);
1154                         // Select_S - ChildDouble < ParentId % 4
1155                         drSelect = dt.Select("ChildDouble < ParentId % 4");
1156                         CompareUnSorted(drSelect ,al.ToArray());
1157
1158                         //-------------------------------------------------------------
1159                         al.Clear();
1160                         foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25 ) al.Add(dr);
1161                         // Select_S - ChildDouble in (10,20,25)
1162                         drSelect = dt.Select("ChildDouble in (10,20,25)");
1163                         CompareUnSorted(drSelect ,al.ToArray());
1164
1165                         //-------------------------------------------------------------
1166                         al.Clear();
1167                         foreach (DataRow dr in dt.Rows ) if ( dr["String2"].ToString().IndexOf("1-S") >= 0 ) al.Add(dr);
1168                         // Select_S - String2 like '%1-S%'
1169                         drSelect = dt.Select("String2 like '%1-S%'");
1170                         Assert.AreEqual(al.ToArray(), drSelect , "DT128");
1171
1172                         //-------------------------------------------------------------
1173                         //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#]":
1174                         al.Clear();
1175                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1176                         // Select_S - [Column#] <= 0 
1177                         drSelect = dt.Select("[Column#] <= 0 ");
1178                         CompareUnSorted(drSelect ,al.ToArray());
1179                         //-------------------------------------------------------------
1180                         al.Clear();
1181                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1182                         // Select_S - [Column#] <= 0
1183                         drSelect = dt.Select("[Column#] <= 0");
1184                         CompareUnSorted(drSelect ,al.ToArray());
1185
1186                         //-------------------------------------------------------------
1187                         al.Clear();
1188                         foreach (DataRow dr in dt.Rows ) if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0  ) al.Add(dr);
1189                         // Select_S - ChildDateTime > #12/12/2000# 
1190                         drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
1191                         CompareUnSorted(drSelect ,al.ToArray());
1192
1193                         //-------------------------------------------------------------
1194
1195                         al.Clear();
1196                         foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0  ) al.Add(dr);
1197                         // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#  
1198                         drSelect = dt.Select("ChildDateTime > #1/12/1999 12:06:30 PM#  ");
1199                         CompareUnSorted(drSelect ,al.ToArray());
1200
1201                         //-------------------------------------------------------------
1202
1203                         al.Clear();
1204                         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);
1205                         // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#  
1206                         drSelect = dt.Select("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#  ");
1207                         CompareUnSorted(drSelect ,al.ToArray());
1208
1209 #if LATER
1210                         //-------------------------------------------------------------
1211                         al.Clear();
1212                         foreach (DataRow dr in dt.Rows ) if ( dr["ChildDouble"].ToString().Length > 10) al.Add(dr);
1213                                 // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
1214                                 drSelect = dt.Select("Len(Convert(ChildDouble,'System.String')) > 10");
1215                                 Assert.AreEqual(al.ToArray(), drSelect , "DT129");
1216 #endif
1217                         //-------------------------------------------------------------
1218                         al.Clear();
1219                         foreach (DataRow dr in dt.Rows ) if ( dr["String1"].ToString().Trim().Substring(0,2) == "1-") al.Add(dr);
1220                         // Select_S - SubString(Trim(String1),1,2) = '1-'
1221                         drSelect = dt.Select("SubString(Trim(String1),1,2) = '1-'");
1222                         Assert.AreEqual(al.ToArray(), drSelect , "DT130");
1223                         //-------------------------------------------------------------
1224                         /*
1225                         al.Clear();
1226                         foreach (DataRow dr in ds.Tables[0].Rows ) if ( dr.IsNull("ParentBool") || (bool)dr["ParentBool"]) al.Add(dr);
1227                                 // Select_S - IsNull(ParentBool,true)
1228                                 drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
1229                                 Assert.AreEqual(al.ToArray(), drSelect , "DT131");
1230                         */
1231                         //-------------------------------------------------------------
1232                         al.Clear();
1233                         // Select_S - Relation not exists, Exception
1234                         try
1235                         {
1236                                 drSelect = dt.Select("Parent.ParentId = ChildId");
1237                                 Assert.Fail("DT132: Select Failed to throw IndexOutOfRangeException");
1238                         }
1239                         catch (IndexOutOfRangeException) {}
1240                         catch (AssertionException exc) {throw  exc;}
1241                         catch (Exception exc)
1242                         {
1243                                 Assert.Fail("DT133: Select. Wrong exception type. Got:" + exc);
1244                         }
1245                         //-------------------------------------------------------------
1246                         al.Clear();
1247                         ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1248                         foreach (DataRow dr in dt.Rows ) if ( (int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"]) al.Add(dr);
1249                         // Select_S - Parent.ParentId = ChildId
1250                         drSelect = dt.Select("Parent.ParentId = ChildId");
1251                         Assert.AreEqual(al.ToArray(), drSelect , "DT134");
1252                 }
1253
1254                 private void CompareUnSorted(Array a, Array b)
1255                 {
1256                         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);
1257                         foreach (object item in a)
1258                         {
1259                                 if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
1260                                 {
1261                                         Assert.Fail(msg);
1262                                 }
1263                         }
1264
1265                         foreach (object item in b)
1266                         {
1267                                 if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
1268                                 {
1269                                         Assert.Fail(msg);
1270                                         return;
1271                                 }
1272                         }
1273                 }
1274
1275                 [Test] public void Select_ByFilterDataViewRowState()
1276                 {
1277                         DataTable dt = DataProvider.CreateParentDataTable();
1278                         DataRow[] drSelect, drResult;
1279
1280                         dt.Rows[0].Delete();
1281                         dt.Rows[1]["ParentId"] = 1;
1282                         dt.Rows[2]["ParentId"] = 1;
1283                         dt.Rows[3].Delete();
1284                         dt.Rows.Add(new object[] {1,"A","B"});
1285                         dt.Rows.Add(new object[] {1,"C","D"});
1286                         dt.Rows.Add(new object[] {1,"E","F"});
1287
1288                         drSelect = dt.Select("ParentId=1","",DataViewRowState.Added );
1289                         drResult = GetResultRows(dt,DataRowState.Added);
1290                         // Select_SSD DataViewRowState.Added
1291                         Assert.AreEqual(drResult, drSelect , "DT135");
1292
1293                         drSelect = dt.Select("ParentId=1","",DataViewRowState.CurrentRows  );
1294                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added  | DataRowState.Modified );
1295                         // Select_SSD DataViewRowState.CurrentRows
1296                         Assert.AreEqual(drResult, drSelect , "DT136");
1297
1298                         drSelect = dt.Select("ParentId=1","",DataViewRowState.Deleted  );
1299                         drResult = GetResultRows(dt,DataRowState.Deleted );
1300                         // Select_SSD DataViewRowState.Deleted
1301                         Assert.AreEqual(drResult, drSelect , "DT137");
1302
1303                         drSelect = dt.Select("ParentId=1","",DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal  );
1304                         drResult = GetResultRows(dt,DataRowState.Modified );
1305                         // Select_SSD ModifiedCurrent or ModifiedOriginal
1306                         Assert.AreEqual(drResult, drSelect , "DT138");
1307                 }
1308
1309                 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
1310                 {
1311                         System.Collections.ArrayList al = new System.Collections.ArrayList();
1312                         DataRowVersion drVer = DataRowVersion.Current;
1313
1314                         //From MSDN -   The row the default version for the current DataRowState.
1315                         //                              For a DataRowState value of Added, Modified or Current, 
1316                         //                              the default version is Current. 
1317                         //                              For a DataRowState of Deleted, the version is Original.
1318                         //                              For a DataRowState value of Detached, the version is Proposed.
1319
1320                         if (    ((State & DataRowState.Added)           > 0)  
1321                                 | ((State & DataRowState.Modified)      > 0)  
1322                                 | ((State & DataRowState.Unchanged)     > 0) ) 
1323                                 drVer = DataRowVersion.Current;
1324                         if ( (State & DataRowState.Deleted)             > 0
1325                                 | (State & DataRowState.Detached)       > 0 )  
1326                                 drVer = DataRowVersion.Original; 
1327
1328                         foreach (DataRow dr in dt.Rows )
1329                         {
1330                                 if ( dr.HasVersion(drVer) 
1331                                         && ((int)dr["ParentId", drVer] == 1) 
1332                                         && ((dr.RowState & State) > 0 ) 
1333                                         )
1334                                         al.Add(dr);
1335                         }
1336                         DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
1337                         return result; 
1338                 }       
1339
1340                 [Test] public void TableName()
1341                 {
1342                         DataTable dtParent = new DataTable();
1343
1344                         // Checking TableName default
1345                         Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
1346
1347                         // Checking TableName set/get
1348                         String s = "MyTable";
1349                         dtParent.TableName=s;
1350                         Assert.AreEqual(s, dtParent.TableName, "DT140");
1351                 }
1352
1353                 [Test] public new void  ToString() 
1354                 {
1355                         DataTable dt = DataProvider.CreateParentDataTable();
1356                         dt.DisplayExpression = dt.Columns[0].ColumnName ;
1357
1358                         string sToString = dt.TableName + " + " + dt.DisplayExpression;
1359                         // ToString
1360                         Assert.AreEqual(sToString , dt.ToString() , "DT141");
1361                 }
1362
1363                 [Test] public void caseSensitive()
1364                 {
1365                         DataTable dtParent = new DataTable();
1366
1367                         // Checking default
1368                         Assert.AreEqual(false, dtParent.CaseSensitive  , "DT142");
1369
1370                         // Checking set/get
1371                         dtParent.CaseSensitive = true;
1372                         Assert.AreEqual(true, dtParent.CaseSensitive , "DT143");
1373                 }
1374
1375                 [Test] public void ctor()
1376                 {
1377                         DataTable dt;
1378                         dt = new DataTable();
1379
1380                         // ctor
1381                         Assert.AreEqual(false, dt == null, "DT144");
1382                 }
1383
1384                 [Test] public void ctor_ByName()
1385                 {
1386                         DataTable dt;
1387                         string sName = "MyName";
1388
1389                         dt = new DataTable(sName);
1390
1391                         // Ctor
1392                         Assert.AreEqual(false , dt == null , "DT145");
1393
1394                         // Ctor TableName
1395                         Assert.AreEqual(sName , dt.TableName , "DT146");
1396                 }
1397
1398                 [Test] public void displayExpression()
1399                 {
1400                         DataTable dtParent;
1401                         dtParent= DataProvider.CreateParentDataTable(); 
1402
1403                         // Checking DisplayExpression default 
1404                         Assert.AreEqual(String.Empty , dtParent.DisplayExpression , "DT147");
1405
1406                         // Checking DisplayExpression Set/Get 
1407                         dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
1408                         Assert.AreEqual(dtParent.Columns[0].ColumnName, dtParent.DisplayExpression  , "DT148");
1409                 }
1410
1411                 [Test] public void extendedProperties()
1412                 {
1413                         DataTable dtParent;
1414                         PropertyCollection pc;
1415                         dtParent= DataProvider.CreateParentDataTable(); 
1416
1417                         pc = dtParent.ExtendedProperties ;
1418
1419                         // Checking ExtendedProperties default 
1420                         Assert.AreEqual(true, pc != null, "DT149");
1421
1422                         // Checking ExtendedProperties count 
1423                         Assert.AreEqual(0, pc.Count , "DT150");
1424                 }
1425
1426                 [Test] public void primaryKey()
1427                 {
1428                         DataTable dtParent;
1429                         dtParent = DataProvider.CreateParentDataTable();
1430
1431                         // Checking PrimaryKey default
1432                         Assert.AreEqual(0, dtParent.PrimaryKey.Length , "DT151");
1433
1434                         // Checking PrimaryKey set/get
1435                         DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]}; 
1436                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
1437                         Assert.AreEqual(dcArr, dtParent.PrimaryKey  , "DT152");
1438
1439                         dtParent.PrimaryKey=null;
1440                         DataSet ds = new DataSet();
1441                         DataRow dr = null;
1442                         ds.Tables.Add(dtParent);
1443
1444                         //check primary key - ColumnType String, ds.CaseSensitive = false;
1445                         ds.CaseSensitive = false;
1446                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
1447                         // check primary key - ColumnType String, ds.CaseSensitive = false;
1448                         dr = dtParent.NewRow();
1449                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1450                         dr["String1"] = dr["String1"].ToString().ToUpper();  
1451                         try
1452                         {
1453                                 dtParent.Rows.Add(dr);
1454                                 Assert.Fail("DT153: Rows.Add Failed to throw ConstraintException");
1455                         }
1456                         catch (ConstraintException) {}
1457                         catch (AssertionException exc) {throw  exc;}
1458                         catch (Exception exc)
1459                         {
1460                                 Assert.Fail("DT154: Rows.Add. Wrong exception type. Got:" + exc);
1461                         }
1462                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1463
1464                         //check primary key - ColumnType String, ds.CaseSensitive = true;               
1465                         ds.CaseSensitive = true;
1466                         // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
1467                         dr = dtParent.NewRow();
1468                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1469                         dr["String1"] = dr["String1"].ToString();  
1470                         try
1471                         {
1472                                 dtParent.Rows.Add(dr);
1473                                 Assert.Fail("DT155: Rows.Add Failed to throw ConstraintException");
1474                         }
1475                         catch (ConstraintException) {}
1476                         catch (AssertionException exc) {throw  exc;}
1477                         catch (Exception exc)
1478                         {
1479                                 Assert.Fail("DT156: Rows.Add. Wrong exception type. Got:" + exc);
1480                         }
1481                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1482
1483                         //check primary key - ColumnType String, ds.CaseSensitive = true;               
1484                         ds.CaseSensitive = true;
1485
1486                         // check primary key - ColumnType String, ds.CaseSensitive = true;
1487                         dr = dtParent.NewRow();
1488                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1489                         dr["String1"] = dr["String1"].ToString().ToUpper() ;  
1490                         dtParent.Rows.Add(dr);
1491                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
1492
1493                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1494
1495                         dtParent.PrimaryKey=null;
1496                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
1497                         // check primary key - ColumnType DateTime
1498                         dr = dtParent.NewRow();
1499                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1500                         dr["ParentDateTime"] = DateTime.Now;
1501                         dtParent.Rows.Add(dr);
1502                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
1503                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1504
1505                         // check primary key ConstraintException- ColumnType DateTime
1506                         dr = dtParent.NewRow();
1507                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1508                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1509                         try
1510                         {
1511                                 dtParent.Rows.Add(dr);
1512                                 Assert.Fail("DT159: Rows.Add Failed to throw ConstraintException");
1513                         }
1514                         catch (ConstraintException) {}
1515                         catch (AssertionException exc) {throw  exc;}
1516                         catch (Exception exc)
1517                         {
1518                                 Assert.Fail("DT160: Rows.Add. Wrong exception type. Got:" + exc);
1519                         }
1520                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1521
1522                         dtParent.PrimaryKey=null;
1523                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
1524                         // check primary key - ColumnType ParentDouble, value=Epsilon
1525                         dr = dtParent.NewRow();
1526                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1527                         dr["ParentDouble"] = Double.Epsilon ;
1528                         dtParent.Rows.Add(dr);
1529                         Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
1530                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1531
1532                         // check primary key ConstraintException - ColumnType ParentDouble
1533                         dr = dtParent.NewRow();
1534                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1535                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1536                         try
1537                         {
1538                                 dtParent.Rows.Add(dr);
1539                                 Assert.Fail("DT162: Rows.Add Failed to throw ConstraintException");
1540                         }
1541                         catch (ConstraintException) {}
1542                         catch (AssertionException exc) {throw  exc;}
1543                         catch (Exception exc)
1544                         {
1545                                 Assert.Fail("DT163: Rows.Add. Wrong exception type. Got:" + exc);
1546                         }
1547                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1548
1549                         //
1550                         // SubTest
1551                         //
1552                         dtParent.PrimaryKey=null;
1553                         // check primary key ConstraintException - ColumnType ParentBool 
1554                         try
1555                         {
1556                                 //ParentBool is not unique, will raise exception
1557                                 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
1558                                 Assert.Fail("DT164: PrimaryKey Failed to throw ArgumentException");
1559                         }
1560                         catch (ArgumentException) {}
1561                         catch (AssertionException exc) {throw  exc;}
1562                         catch (Exception exc)
1563                         {
1564                                 Assert.Fail("DT165: PrimaryKey. Wrong exception type. Got:" + exc);
1565                         }
1566                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1567
1568                         //
1569                         // SubTest
1570                         //
1571                         dtParent.PrimaryKey=null;
1572                         dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
1573                         // check primary key - ColumnType Double,DateTime test1
1574                         dr = dtParent.NewRow();
1575                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1576                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1577                         dr["ParentDateTime"] = DateTime.Now;
1578                         dtParent.Rows.Add(dr);
1579                         Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
1580                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1581
1582                         // check primary key - ColumnType Double,DateTime test2
1583                         dr = dtParent.NewRow();
1584                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1585                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1586                         dr["ParentDouble"] = 99.399;
1587                         dtParent.Rows.Add(dr);
1588                         Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
1589                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1590
1591                         // check primary key ConstraintException - ColumnType Double,DateTime 
1592                         dr = dtParent.NewRow();
1593                         dr.ItemArray = dtParent.Rows[0].ItemArray;
1594                         dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1595                         dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1596                         try
1597                         {
1598                                 dtParent.Rows.Add(dr);
1599                                 Assert.Fail("DT168: Rows.Add Failed to throw ConstraintException");
1600                         }
1601                         catch (ConstraintException) {}
1602                         catch (AssertionException exc) {throw  exc;}
1603                         catch (Exception exc)
1604                         {
1605                                 Assert.Fail("DT169: Rows.Add. Wrong exception type. Got:" + exc);
1606                         }
1607                         if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1608
1609                         DataTable dtChild = DataProvider.CreateChildDataTable();
1610                         ds.Tables.Add(dtChild);
1611                         dtParent.PrimaryKey = null;
1612                         //this test was addedd to check java exception: 
1613                         //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
1614                         // check add primary key with relation 
1615                         ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1616                         //the following line will cause java to fail
1617                         ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]}; 
1618                         Assert.AreEqual(2, ds.Tables[0].PrimaryKey.Length , "DT170");
1619                 }
1620         }
1621 }