New test.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest.cs
1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable 
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 // 
7 // (C) Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using NUnit.Framework;
35 using System;
36 using System.Data;
37 using System.Data.SqlTypes;
38 using System.Globalization;
39 using System.IO;
40 using System.Runtime.Serialization.Formatters.Binary;
41 using System.Xml;
42
43 namespace MonoTests.System.Data
44 {
45         [TestFixture]
46         public class DataTableTest : Assertion
47         {
48
49                 [Test]
50                 public void Ctor()
51                 {
52                         DataTable dt = new DataTable();
53
54                         AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
55                         Assert("Col",dt.Columns != null);
56                         //Assert(dt.ChildRelations != null);
57                         Assert("Const", dt.Constraints != null);
58                         Assert("ds", dt.DataSet == null); 
59                         Assert("dv", dt.DefaultView != null);
60                         Assert("de", dt.DisplayExpression == "");
61                         Assert("ep", dt.ExtendedProperties != null);
62                         Assert("he", dt.HasErrors == false);
63                         Assert("lc", dt.Locale != null);
64                         Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
65                         Assert("ns", dt.Namespace == "");
66                         //Assert(dt.ParentRelations != null);
67                         Assert("pf", dt.Prefix == "");
68                         Assert("pk", dt.PrimaryKey != null);
69                         Assert("rows", dt.Rows != null);
70                         Assert("Site", dt.Site == null);
71                         Assert("tname", dt.TableName == "");
72                         
73                 }
74
75                 [Test]
76                 public void Select ()
77                 {
78                         DataSet Set = new DataSet ();
79                         DataTable Mom = new DataTable ("Mom");
80                         DataTable Child = new DataTable ("Child");                      
81                         Set.Tables.Add (Mom);
82                         Set.Tables.Add (Child);
83                         
84                         DataColumn Col = new DataColumn ("Name");
85                         DataColumn Col2 = new DataColumn ("ChildName");
86                         Mom.Columns.Add (Col);
87                         Mom.Columns.Add (Col2);
88                         
89                         DataColumn Col3 = new DataColumn ("Name");
90                         DataColumn Col4 = new DataColumn ("Age");
91                         Col4.DataType = Type.GetType ("System.Int16");
92                         Child.Columns.Add (Col3);
93                         Child.Columns.Add (Col4);
94                         
95                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
96                         Set.Relations.Add (Relation);
97
98                         DataRow Row = Mom.NewRow ();
99                         Row [0] = "Laura";
100                         Row [1] = "Nick";
101                         Mom.Rows.Add (Row);
102                         
103                         Row = Mom.NewRow ();
104                         Row [0] = "Laura";
105                         Row [1] = "Dick";
106                         Mom.Rows.Add (Row);
107                         
108                         Row = Mom.NewRow ();
109                         Row [0] = "Laura";
110                         Row [1] = "Mick";
111                         Mom.Rows.Add (Row);
112
113                         Row = Mom.NewRow ();
114                         Row [0] = "Teresa";
115                         Row [1] = "Jack";
116                         Mom.Rows.Add (Row);
117                         
118                         Row = Mom.NewRow ();
119                         Row [0] = "Teresa";
120                         Row [1] = "Mack";
121                         Mom.Rows.Add (Row);
122
123                         Row = Mom.NewRow ();
124                         Row [0] = "'Jhon O'' Collenal'";
125                         Row [1] = "Pack";
126                         Mom.Rows.Add (Row);
127                         
128                         Row = Child.NewRow ();
129                         Row [0] = "Nick";
130                         Row [1] = 15;
131                         Child.Rows.Add (Row);
132                         
133                         Row = Child.NewRow ();
134                         Row [0] = "Dick";
135                         Row [1] = 25;
136                         Child.Rows.Add (Row);
137                         
138                         Row = Child.NewRow ();
139                         Row [0] = "Mick";
140                         Row [1] = 35;
141                         Child.Rows.Add (Row);
142                         
143                         Row = Child.NewRow ();
144                         Row [0] = "Jack";
145                         Row [1] = 10;
146                         Child.Rows.Add (Row);
147                         
148                         Row = Child.NewRow ();
149                         Row [0] = "Mack";
150                         Row [1] = 19;
151                         Child.Rows.Add (Row);
152                 
153                         Row = Child.NewRow ();
154                         Row [0] = "Mack";
155                         Row [1] = 99;
156                         Child.Rows.Add (Row);
157
158                         Row = Child.NewRow ();
159                         Row [0] = "Pack";
160                         Row [1] = 66;
161                         Child.Rows.Add (Row);
162                         
163                         DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
164                         AssertEquals ("test#01", 2, Rows.Length);
165
166                         // test with apos escaped
167                         Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
168                         AssertEquals ("test#01.1", 1, Rows.Length);
169                         
170                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
171                         AssertEquals ("test#02", 0, Rows.Length);
172
173                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
174                         AssertEquals ("test#03", 1, Rows.Length);
175
176                         Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
177                         AssertEquals ("test#04", "Mack", Rows [0] [1]);
178                         
179                         Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
180                         AssertEquals ("test#05", 6, Rows.Length);
181                         
182                         Rows = Child.Select ("age = 20 - 1");
183                         AssertEquals ("test#06", 1, Rows.Length);
184                         
185                         Rows = Child.Select ("age <= 20");
186                         AssertEquals ("test#07", 3, Rows.Length);
187                         
188                         Rows = Child.Select ("age >= 20");
189                         AssertEquals ("test#08", 4, Rows.Length);
190                         
191                         Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
192                         AssertEquals ("test#09", 2, Rows.Length);
193
194                         Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
195                         AssertEquals ("test#10", 1, Rows.Length);
196                         AssertEquals ("test#11", "Mack", Rows [0] [0]);
197                         
198                         Rows = Child.Select ("not (Name = 'Jack')");
199                         AssertEquals ("test#12", 6, Rows.Length);
200                 }
201                 
202                 [Test]
203                 public void Select2 ()
204                 {
205                         DataSet Set = new DataSet ();
206                         DataTable Child = new DataTable ("Child");
207
208                         Set.Tables.Add (Child);
209                                                 
210                         DataColumn Col3 = new DataColumn ("Name");
211                         DataColumn Col4 = new DataColumn ("Age");
212                         Col4.DataType = Type.GetType ("System.Int16");
213                         Child.Columns.Add (Col3);
214                         Child.Columns.Add (Col4);
215                         
216                         DataRow Row = Child.NewRow ();
217                         Row [0] = "Nick";
218                         Row [1] = 15;
219                         Child.Rows.Add (Row);
220                         
221                         Row = Child.NewRow ();
222                         Row [0] = "Dick";
223                         Row [1] = 25;
224                         Child.Rows.Add (Row);
225                         
226                         Row = Child.NewRow ();
227                         Row [0] = "Mick";
228                         Row [1] = 35;
229                         Child.Rows.Add (Row);
230                         
231                         Row = Child.NewRow ();
232                         Row [0] = "Jack";
233                         Row [1] = 10;
234                         Child.Rows.Add (Row);
235                         
236                         Row = Child.NewRow ();
237                         Row [0] = "Mack";
238                         Row [1] = 19;
239                         Child.Rows.Add (Row);
240                 
241                         Row = Child.NewRow ();
242                         Row [0] = "Mack";
243                         Row [1] = 99;
244                         Child.Rows.Add (Row);
245
246                         DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
247                         AssertEquals ("test#01", 3, Rows.Length);
248                         AssertEquals ("test#02", "Mack", Rows [0] [0]);
249                         AssertEquals ("test#03", "Mick", Rows [1] [0]);                 
250                         AssertEquals ("test#04", "Dick", Rows [2] [0]);                 
251                         
252                         Rows = Child.Select ("age >= 20", "age asc");
253                         AssertEquals ("test#05", 3, Rows.Length);
254                         AssertEquals ("test#06", "Dick", Rows [0] [0]);
255                         AssertEquals ("test#07", "Mick", Rows [1] [0]);                 
256                         AssertEquals ("test#08", "Mack", Rows [2] [0]);                 
257                 
258                         Rows = Child.Select ("age >= 20", "name asc");
259                         AssertEquals ("test#09", 3, Rows.Length);
260                         AssertEquals ("test#10", "Dick", Rows [0] [0]);
261                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
262                         AssertEquals ("test#12", "Mick", Rows [2] [0]);                 
263
264                         Rows = Child.Select ("age >= 20", "name desc");
265                         AssertEquals ("test#09", 3, Rows.Length);
266                         AssertEquals ("test#10", "Mick", Rows [0] [0]);
267                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
268                         AssertEquals ("test#12", "Dick", Rows [2] [0]);                 
269
270                 }
271
272                 [Test]
273                 public void SelectParsing ()
274                 {
275                         DataTable T = new DataTable ("test");
276                         DataColumn C = new DataColumn ("name");
277                         T.Columns.Add (C);
278                         C = new DataColumn ("age");
279                         C.DataType = typeof (int);
280                         T.Columns.Add (C);
281                         C = new DataColumn ("id");
282                         T.Columns.Add (C);
283                         
284                         DataSet Set = new DataSet ("TestSet");
285                         Set.Tables.Add (T);
286                         
287                         DataRow Row = null;
288                         for (int i = 0; i < 100; i++) {
289                                 Row = T.NewRow ();
290                                 Row [0] = "human" + i;
291                                 Row [1] = i;
292                                 Row [2] = i;
293                                 T.Rows.Add (Row);
294                         }
295                         
296                         Row = T.NewRow ();
297                         Row [0] = "h*an";
298                         Row [1] = 1;
299                         Row [2] = 1;
300                         T.Rows.Add (Row);
301
302                         AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
303                         
304                         AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
305
306                         try {
307                                 T.Select ("name = 1human ");
308                                 Fail ("test#03");
309                         } catch (Exception e) {
310                                 
311                                 // missing operand after 'human' operand 
312                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());                          
313                         }
314                         
315                         try {                   
316                                 T.Select ("name = 1");
317                                 Fail ("test#05");
318                         } catch (Exception e) {
319                                 
320                                 // Cannot perform '=' operation between string and Int32
321                                 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
322                         }
323                         
324                         AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
325
326                 }
327                 
328                 [Test]
329                 public void SelectEscaping () {
330                         DataTable dt = new DataTable ();
331                         dt.Columns.Add ("SomeCol");
332                         dt.Rows.Add (new object [] {"\t"});
333                         dt.Rows.Add (new object [] {"\\"});
334                         
335                         AssertEquals ("test#01", 1, dt.Select (@"SomeCol='\t'").Length);
336                         AssertEquals ("test#02", 1, dt.Select (@"SomeCol='\\'").Length);
337                         
338                         try {
339                                 dt.Select (@"SomeCol='\x'");
340                                 Fail("test#03");
341                         } catch (SyntaxErrorException) {}
342                 }
343
344                 [Test]
345                 public void SelectOperators ()
346                 {
347                         DataTable T = new DataTable ("test");
348                         DataColumn C = new DataColumn ("name");
349                         T.Columns.Add (C);
350                         C = new DataColumn ("age");
351                         C.DataType = typeof (int);
352                         T.Columns.Add (C);
353                         C = new DataColumn ("id");
354                         T.Columns.Add (C);
355                         
356                         DataSet Set = new DataSet ("TestSet");
357                         Set.Tables.Add (T);
358                         
359                         DataRow Row = null;
360                         for (int i = 0; i < 100; i++) {
361                                 Row = T.NewRow ();
362                                 Row [0] = "human" + i;
363                                 Row [1] = i;
364                                 Row [2] = i;
365                                 T.Rows.Add (Row);
366                         }
367                         
368                         Row = T.NewRow ();
369                         Row [0] = "h*an";
370                         Row [1] = 1;
371                         Row [2] = 1;
372                         T.Rows.Add (Row);
373                         
374                         AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
375                         AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);                    
376                         AssertEquals ("test#03", 12, T.Select ("age< =10").Length);                     
377                         AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
378                         AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);                    
379                         AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
380                         AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
381                         AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
382                         // FIXME: Somebody explain how this can be possible.
383                         // it seems that it is no matter between 10 - 30. The
384                         // result is allways 25 :-P
385                         //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
386                         
387                 }
388
389                 [Test]
390                 public void SelectExceptions ()
391                 {
392                         DataTable T = new DataTable ("test");
393                         DataColumn C = new DataColumn ("name");
394                         T.Columns.Add (C);
395                         C = new DataColumn ("age");
396                         C.DataType = typeof (int);
397                         T.Columns.Add (C);
398                         C = new DataColumn ("id");
399                         T.Columns.Add (C);
400                         
401                         for (int i = 0; i < 100; i++) {
402                                 DataRow Row = T.NewRow ();
403                                 Row [0] = "human" + i;
404                                 Row [1] = i;
405                                 Row [2] = i;
406                                 T.Rows.Add (Row);
407                         }
408                         
409                         try {
410                                 T.Select ("name = human1");
411                                 Fail ("test#01");
412                         } catch (Exception e) {
413                                 
414                                 // column name human not found
415                                 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
416                         }
417                         
418                         AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
419                         AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
420                         
421                         try {
422                                 T.Select ("id = 1k3");
423                                 Fail ("test#06");
424                         } catch (Exception e) {
425                                 
426                                 // no operands after k3 operator
427                                 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
428                         }                                               
429                 }
430                 
431                 [Test]
432                 public void SelectStringOperators ()
433                 {
434                         DataTable T = new DataTable ("test");
435                         DataColumn C = new DataColumn ("name");
436                         T.Columns.Add (C);
437                         C = new DataColumn ("age");
438                         C.DataType = typeof (int);
439                         T.Columns.Add (C);
440                         C = new DataColumn ("id");
441                         T.Columns.Add (C);
442                         
443                         DataSet Set = new DataSet ("TestSet");
444                         Set.Tables.Add (T);
445                         
446                         DataRow Row = null;
447                         for (int i = 0; i < 100; i++) {
448                                 Row = T.NewRow ();
449                                 Row [0] = "human" + i;
450                                 Row [1] = i;
451                                 Row [2] = i;
452                                 T.Rows.Add (Row);
453                         }
454                         Row = T.NewRow ();
455                         Row [0] = "h*an";
456                         Row [1] = 1;
457                         Row [2] = 1;
458                         T.Rows.Add (Row);
459                                         
460                         AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
461                         
462                         AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);                       
463                         AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
464                         AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);                     
465                         AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
466                         AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
467                         
468                         AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
469                         
470                         Set.CaseSensitive = true;
471                         AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
472                         
473                         T.CaseSensitive = false;
474                         AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
475                         
476                         T.CaseSensitive = true;
477                         AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
478                         
479                         Set.CaseSensitive = false;
480                         AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
481                         
482                         T.CaseSensitive = false;
483                         AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
484                         
485                         AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
486                         AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
487                         AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
488                         
489                         try {
490                                 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
491                                 Fail ("test#16");
492                         } catch (Exception e) {
493                                 
494                                 // 'h*an1' is invalid
495                                 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
496                         }
497                         
498                         try {
499                                 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
500                                 Fail ("test#19");
501                         } catch (Exception e) {
502                                 
503                                 // 'h%an1' is invalid
504                                 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
505                         }
506                         
507                         AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
508                         AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
509                         
510                 }
511
512                 [Test]
513                 public void SelectAggregates ()
514                 {
515                         DataTable T = new DataTable ("test");
516                         DataColumn C = new DataColumn ("name");
517                         T.Columns.Add (C);
518                         C = new DataColumn ("age");
519                         C.DataType = typeof (int);
520                         T.Columns.Add (C);
521                         C = new DataColumn ("id");
522                         T.Columns.Add (C);
523                         DataRow Row = null;
524                         
525                         for (int i = 0; i < 1000; i++) {
526                                 Row = T.NewRow ();
527                                 Row [0] = "human" + i;
528                                 Row [1] = i;
529                                 Row [2] = i;
530                                 T.Rows.Add (Row);
531                         }
532                         
533                         AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
534                         AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
535                         AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
536                         AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
537                         AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
538                         AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
539                         AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
540                 }
541                 
542                 [Test]
543                 public void SelectFunctions ()
544                 {
545                         DataTable T = new DataTable ("test");
546                         DataColumn C = new DataColumn ("name");
547                         T.Columns.Add (C);
548                         C = new DataColumn ("age");
549                         C.DataType = typeof (int);
550                         T.Columns.Add (C);
551                         C = new DataColumn ("id");
552                         T.Columns.Add (C);
553                         DataRow Row = null;
554                         
555                         for (int i = 0; i < 1000; i++) {
556                                 Row = T.NewRow ();
557                                 Row [0] = "human" + i;
558                                 Row [1] = i;
559                                 Row [2] = i;
560                                 T.Rows.Add (Row);
561                         }
562                         
563                         Row = T.NewRow ();
564                         Row [0] = "human" + "test";
565                         Row [1] = DBNull.Value;
566                         Row [2] = DBNull.Value;
567                         T.Rows.Add (Row);
568
569                         //TODO: How to test Convert-function
570                         AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);                        
571                         AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
572                         AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
573                         AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);                   
574                         AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);                  
575                         AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
576                         AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
577                         AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
578                         AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
579                         AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
580                         
581                 }
582
583                 [Test]
584                 public void SelectRelations ()
585                 {
586                         DataSet Set = new DataSet ();
587                         DataTable Mom = new DataTable ("Mom");
588                         DataTable Child = new DataTable ("Child");
589
590                         Set.Tables.Add (Mom);
591                         Set.Tables.Add (Child);
592                         
593                         DataColumn Col = new DataColumn ("Name");
594                         DataColumn Col2 = new DataColumn ("ChildName");
595                         Mom.Columns.Add (Col);
596                         Mom.Columns.Add (Col2);
597                         
598                         DataColumn Col3 = new DataColumn ("Name");
599                         DataColumn Col4 = new DataColumn ("Age");
600                         Col4.DataType = Type.GetType ("System.Int16");
601                         Child.Columns.Add (Col3);
602                         Child.Columns.Add (Col4);
603                         
604                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
605                         Set.Relations.Add (Relation);
606
607                         DataRow Row = Mom.NewRow ();
608                         Row [0] = "Laura";
609                         Row [1] = "Nick";
610                         Mom.Rows.Add (Row);
611                         
612                         Row = Mom.NewRow ();
613                         Row [0] = "Laura";
614                         Row [1] = "Dick";
615                         Mom.Rows.Add (Row);
616                         
617                         Row = Mom.NewRow ();
618                         Row [0] = "Laura";
619                         Row [1] = "Mick";
620                         Mom.Rows.Add (Row);
621
622                         Row = Mom.NewRow ();
623                         Row [0] = "Teresa";
624                         Row [1] = "Jack";
625                         Mom.Rows.Add (Row);
626                         
627                         Row = Mom.NewRow ();
628                         Row [0] = "Teresa";
629                         Row [1] = "Mack";
630                         Mom.Rows.Add (Row);
631                         
632                         Row = Child.NewRow ();
633                         Row [0] = "Nick";
634                         Row [1] = 15;
635                         Child.Rows.Add (Row);
636                         
637                         Row = Child.NewRow ();
638                         Row [0] = "Dick";
639                         Row [1] = 25;
640                         Child.Rows.Add (Row);
641                         
642                         Row = Child.NewRow ();
643                         Row [0] = "Mick";
644                         Row [1] = 35;
645                         Child.Rows.Add (Row);
646                         
647                         Row = Child.NewRow ();
648                         Row [0] = "Jack";
649                         Row [1] = 10;
650                         Child.Rows.Add (Row);
651                         
652                         Row = Child.NewRow ();
653                         Row [0] = "Mack";
654                         Row [1] = 19;
655                         Child.Rows.Add (Row);
656                 
657                         Row = Child.NewRow ();
658                         Row [0] = "Mack";
659                         Row [1] = 99;
660                         Child.Rows.Add (Row);
661                         
662                         DataRow [] Rows = Child.Select ("name = Parent.Childname");
663                         AssertEquals ("test#01", 6, Rows.Length);
664                         Rows = Child.Select ("Parent.childname = 'Jack'");
665                         AssertEquals ("test#02", 1, Rows.Length);
666                         
667                         /*
668                         try {
669                                 // FIXME: LAMESPEC: Why the exception is thrown why... why... 
670                                 Mom.Select ("Child.Name = 'Jack'");
671                                 Fail ("test#03");
672                         } catch (Exception e) {
673                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
674                                 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
675                         }
676                         */
677                         
678                         Rows = Child.Select ("Parent.name = 'Laura'");
679                         AssertEquals ("test#06", 3, Rows.Length);
680                         
681                         DataTable Parent2 = new DataTable ("Parent2");
682                         Col = new DataColumn ("Name");
683                         Col2 = new DataColumn ("ChildName");
684
685                         Parent2.Columns.Add (Col);
686                         Parent2.Columns.Add (Col2);
687                         Set.Tables.Add (Parent2);
688                         
689                         Row = Parent2.NewRow ();
690                         Row [0] = "Laura";
691                         Row [1] = "Nick";
692                         Parent2.Rows.Add (Row);
693                         
694                         Row = Parent2.NewRow ();
695                         Row [0] = "Laura";
696                         Row [1] = "Dick";
697                         Parent2.Rows.Add (Row);
698                         
699                         Row = Parent2.NewRow ();
700                         Row [0] = "Laura";
701                         Row [1] = "Mick";
702                         Parent2.Rows.Add (Row);
703
704                         Row = Parent2.NewRow ();
705                         Row [0] = "Teresa";
706                         Row [1] = "Jack";
707                         Parent2.Rows.Add (Row);
708                         
709                         Row = Parent2.NewRow ();
710                         Row [0] = "Teresa";
711                         Row [1] = "Mack";
712                         Parent2.Rows.Add (Row);
713
714                         Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
715                         Set.Relations.Add (Relation);
716                         
717                         try {
718                                 Rows = Child.Select ("Parent.ChildName = 'Jack'");
719                                 Fail ("test#07");
720                         } catch (Exception e) {
721                                 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
722                                 //AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
723                         }
724                         
725                         Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
726                         AssertEquals ("test#10", 1, Rows.Length);
727
728                         Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
729                         AssertEquals ("test#10", 1, Rows.Length);
730                         
731                         try {
732                                 Mom.Select ("Parent.name  = 'John'");
733                         } catch (Exception e) {
734                                 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
735                                 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
736                         }
737                         
738                 }
739
740                 [Test]
741                 public void SelectRowState()
742                 {
743                         DataTable d = new DataTable();
744                         d.Columns.Add (new DataColumn ("aaa"));
745                         DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
746                         AssertEquals(0, rows.Length);
747                         d.Rows.Add (new object [] {"bbb"});
748                         d.Rows.Add (new object [] {"bbb"});
749                         rows = d.Select (null, null, DataViewRowState.Deleted);
750                         AssertEquals(0, rows.Length);
751                 }
752
753                 [Test]
754                 public void ToStringTest()
755                 {
756                         DataTable dt = new DataTable();
757                         dt.Columns.Add("Col1",typeof(int));
758                         
759                         dt.TableName = "Mytable";
760                         dt.DisplayExpression = "Col1";
761                         
762                         
763                         string cmpr = dt.TableName + " + " + dt.DisplayExpression;
764                         AssertEquals(cmpr,dt.ToString());
765                 }
766
767                 [Test]
768                 public void PrimaryKey ()
769                 {
770                         DataTable dt = new DataTable ();
771                         DataColumn Col = new DataColumn ();
772                         Col.AllowDBNull = false;
773                         Col.DataType = typeof (int);
774                         dt.Columns.Add (Col);
775                         dt.Columns.Add ();
776                         dt.Columns.Add ();
777                         dt.Columns.Add ();
778                         
779                         AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
780                         
781                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
782                         AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
783                         AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
784                         
785                         dt.PrimaryKey = null;
786                         AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
787                         
788                         Col = new DataColumn ("failed");
789                         
790                         try {
791                                 dt.PrimaryKey = new DataColumn [] {Col};
792                                 Fail ("test#05");                                       
793                         } catch (Exception e) {
794                                 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
795                                 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
796                         }
797                         
798                         DataTable dt2 = new DataTable ();
799                         dt2.Columns.Add ();
800                         
801                         try {
802                                 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
803                                 Fail ("test#08");
804                         } catch (Exception e) {
805                                 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
806                                 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
807                         }
808                         
809                         
810                         AssertEquals ("test#11", 0, dt.Constraints.Count);
811                         
812                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
813                         AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
814                         AssertEquals ("test#13", 1, dt.Constraints.Count);
815                         AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
816                         AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
817                         AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
818                         
819                 }
820                 
821                 [Test]
822                 public void PropertyExceptions ()
823                 {
824                         DataSet set = new DataSet ();
825                         DataTable table = new DataTable ();
826                         DataTable table1 =  new DataTable ();
827                         set.Tables.Add (table);
828                         set.Tables.Add (table1);
829
830                         DataColumn col = new DataColumn ();
831                         col.ColumnName = "Id";
832                         col.DataType = Type.GetType ("System.Int32");
833                         table.Columns.Add (col);
834                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
835                         table.Constraints.Add (uc);
836                         table.CaseSensitive = false;
837                                                                                                                            
838                         col = new DataColumn ();
839                         col.ColumnName = "Name";
840                         col.DataType = Type.GetType ("System.String");
841                         table.Columns.Add (col);
842                         
843                         col = new DataColumn ();
844                         col.ColumnName = "Id";
845                         col.DataType = Type.GetType ("System.Int32");
846                         table1.Columns.Add (col);
847                         col = new DataColumn ();
848                         col.ColumnName = "Name";
849                         col.DataType = Type.GetType ("System.String");
850                         table1.Columns.Add (col);
851
852                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
853                         set.Relations.Add (dr);
854
855                         try {
856                                 table.CaseSensitive = true;
857                                 table1.CaseSensitive = true;
858                                 Fail ("#A01");
859                         }
860                         catch (Exception e) {
861                                 if (e.GetType () != typeof (AssertionException))
862                                         AssertEquals ("#A02", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
863                                 else
864                                         Console.WriteLine (e);
865                         }
866                         try {
867                                 CultureInfo cultureInfo = new CultureInfo ("en-gb");
868                                 table.Locale = cultureInfo;
869                                 table1.Locale = cultureInfo;
870                                 Fail ("#A03");
871                         }
872                         catch (Exception e) {
873                                  if (e.GetType () != typeof (AssertionException))
874                                         AssertEquals ("#A04", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
875                                 else
876                                         Console.WriteLine (e);
877                         }
878                         try {
879                                 table.Prefix = "Prefix#1";
880                                 Fail ("#A05");
881                         }
882                         catch (Exception e){
883                                 if (e.GetType () != typeof (AssertionException))
884                                         AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
885                                 else
886                                         Console.WriteLine (e);
887
888                         }
889                 }
890
891                 [Test]
892                 public void GetErrors ()
893                 {
894                         DataTable table = new DataTable ();
895
896                         DataColumn col = new DataColumn ();
897                         col.ColumnName = "Id";
898                         col.DataType = Type.GetType ("System.Int32");
899                         table.Columns.Add (col);
900                                                                                                                              
901                         col = new DataColumn ();
902                         col.ColumnName = "Name";
903                         col.DataType = Type.GetType ("System.String");
904                         table.Columns.Add (col);
905                         
906                         DataRow row = table.NewRow ();
907                         row ["Id"] = 147;
908                         row ["name"] = "Abc";
909                         row.RowError = "Error#1";
910                         table.Rows.Add (row);
911
912                         AssertEquals ("#A01", 1, table.GetErrors ().Length);
913                         AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
914                 }
915                 [Test]
916                 public void CloneCopyTest ()
917                 {
918                         DataTable table = new DataTable ();
919                         table.TableName = "Table#1";
920                         DataTable table1 = new DataTable ();
921                         table1.TableName = "Table#2";
922                 
923                         table.AcceptChanges ();
924                         
925                         DataSet set = new DataSet ("Data Set#1");
926                         set.DataSetName = "Dataset#1";
927                         set.Tables.Add (table);
928                         set.Tables.Add (table1);
929
930                         DataColumn col = new DataColumn ();
931                         col.ColumnName = "Id";
932                         col.DataType = Type.GetType ("System.Int32");
933                         table.Columns.Add (col);
934                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
935                         table.Constraints.Add (uc);
936                 
937                         col = new DataColumn ();
938                         col.ColumnName = "Id";
939                         col.DataType = Type.GetType ("System.Int32");
940                         table1.Columns.Add (col);
941                                                                                                                              
942                         col = new DataColumn ();
943                         col.ColumnName = "Name";
944                         col.DataType = Type.GetType ("System.String");
945                         table.Columns.Add (col);
946                         
947                         col = new DataColumn ();
948                         col.ColumnName = "Name";
949                         col.DataType = Type.GetType ("System.String");
950                         table1.Columns.Add (col);
951                         DataRow row = table.NewRow ();
952                         row ["Id"] = 147;
953                         row ["name"] = "Abc";
954                         row.RowError = "Error#1";
955                         table.Rows.Add (row);
956                                                                                                                              
957                         row = table.NewRow ();
958                         row ["Id"] = 47;
959                         row ["name"] = "Efg";
960                         table.Rows.Add (row);
961                         table.AcceptChanges ();
962                                                                                                                              
963                         table.CaseSensitive = true;
964                         table1.CaseSensitive = true;
965                         table.MinimumCapacity = 100;
966                         table.Prefix = "PrefixNo:1";
967                         table.Namespace = "Namespace#1";
968                         table.DisplayExpression = "Id / Name + (Id * Id)";
969                         DataColumn[] colArray = {table.Columns[0]};
970                         table.PrimaryKey = colArray;
971                         table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
972 #if NET_1_1 // This prevents further tests after .NET 1.1.
973 #else
974                         CultureInfo cultureInfo = new CultureInfo ("en-gb");
975                         table.Locale = cultureInfo;
976 #endif
977
978                         row = table1.NewRow ();
979                         row ["Name"] = "Abc";
980                         row ["Id"] = 147;
981                         table1.Rows.Add (row);
982
983                         row = table1.NewRow ();
984                         row ["Id"] = 47;
985                         row ["Name"] = "Efg";
986                         table1.Rows.Add (row);
987                 
988                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
989                         set.Relations.Add (dr);
990
991                         //Testing properties of clone
992                         DataTable cloneTable = table.Clone ();
993                         AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
994                         AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
995                         AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
996                         AssertEquals ("#A04", 2,  cloneTable.Columns.Count);
997                         AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
998                         AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
999                         AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
1000                         AssertEquals ("#A08", false ,cloneTable.HasErrors);
1001 #if NET_1_1
1002 #else
1003                         AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
1004 #endif
1005                         AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
1006                         AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
1007                         AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
1008                         AssertEquals ("#A13", "Id",  cloneTable.PrimaryKey[0].ColumnName);
1009                         AssertEquals ("#A14",0 , cloneTable.Rows.Count );
1010                         AssertEquals ("#A15", "Table#1", cloneTable.TableName);
1011
1012                         //Testing properties of copy
1013                         DataTable copyTable = table.Copy ();
1014                         AssertEquals ("#A16",true ,copyTable.CaseSensitive);
1015                         AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
1016                         AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
1017                         AssertEquals ("#A19", 2,  copyTable.Columns.Count);
1018                         AssertEquals ("#A20", 1, copyTable.Constraints.Count);
1019                         AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
1020                         AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
1021                         AssertEquals ("#A23", true ,copyTable.HasErrors);
1022 #if NET_1_1
1023 #else
1024                         AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
1025 #endif
1026                         AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
1027                         AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
1028                         AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
1029                         AssertEquals ("#A28", "Id",  copyTable.PrimaryKey[0].ColumnName);
1030                         AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1031                         AssertEquals ("#A30", "Table#1", copyTable.TableName);
1032                 }
1033
1034                 [Test]
1035                 public void LoadDataException ()
1036                 {
1037                         DataTable table = new DataTable ();
1038                         DataColumn col = new DataColumn ();
1039                         col.ColumnName = "Id";
1040                         col.DataType = Type.GetType ("System.Int32");
1041                         col.DefaultValue = 47;
1042                         table.Columns.Add (col);
1043                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1044                         table.Constraints.Add (uc);
1045                 
1046                         col = new DataColumn ();
1047                         col.ColumnName = "Name";
1048                         col.DataType = Type.GetType ("System.String");
1049                         col.DefaultValue = "Hello";
1050                         table.Columns.Add (col);
1051                 
1052                         table.BeginLoadData();
1053                         object[] row = {147, "Abc"};
1054                         DataRow newRow = table.LoadDataRow (row, true);
1055                 
1056                         object[] row1 = {147, "Efg"};
1057                         DataRow newRow1 = table.LoadDataRow (row1, true);
1058                                                                                                                              
1059                         object[] row2 = {143, "Hij"};
1060                         DataRow newRow2 = table.LoadDataRow (row2, true);
1061                                                                                                                              
1062                         try {
1063                                 table.EndLoadData ();
1064                                 Fail ("#A01");
1065                         }
1066                         catch (ConstraintException) {
1067                         }
1068                 }
1069                 [Test]
1070                 public void Changes () //To test GetChanges and RejectChanges
1071                 {
1072                         DataTable table = new DataTable ();
1073
1074                         DataColumn col = new DataColumn ();
1075                         col.ColumnName = "Id";
1076                         col.DataType = Type.GetType ("System.Int32");
1077                         table.Columns.Add (col);
1078                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1079                         table.Constraints.Add (uc);
1080                                                                                                                              
1081                         col = new DataColumn ();
1082                         col.ColumnName = "Name";
1083                         col.DataType = Type.GetType ("System.String");
1084                         table.Columns.Add (col);                        
1085
1086                         DataRow row = table.NewRow ();
1087                         row ["Id"] = 147;
1088                         row ["name"] = "Abc";
1089                         table.Rows.Add (row);
1090                         table.AcceptChanges ();
1091                         
1092                         row = table.NewRow ();
1093                         row ["Id"] = 47;
1094                         row ["name"] = "Efg";
1095                         table.Rows.Add (row);
1096
1097                         //Testing GetChanges
1098                         DataTable changesTable = table.GetChanges ();
1099                         AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1100                         AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);                      
1101                         table.AcceptChanges ();
1102                         changesTable = table.GetChanges ();
1103                         try {
1104                                 int cnt = changesTable.Rows.Count;
1105                         }
1106                         catch(Exception e) {
1107                                 if (e.GetType () != typeof (AssertionException))
1108                                         AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1109                                 else
1110                                         Console.WriteLine (e);
1111                         }
1112                         
1113                         //Testing RejectChanges
1114                         row = table.NewRow ();
1115                         row ["Id"] = 247;
1116                         row ["name"] = "Hij";
1117                         table.Rows.Add (row);
1118
1119                         (table.Rows [0])["Name"] = "AaBbCc";
1120                         table.RejectChanges ();
1121                         AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1122                         AssertEquals ("#A04", 2, table.Rows.Count);
1123                 }
1124                 
1125                 [Test]
1126                 public void ImportRowTest ()
1127                 {
1128                         // build source table
1129                         DataTable src = new DataTable ();
1130                         src.Columns.Add ("id", typeof (int));
1131                         src.Columns.Add ("name", typeof (string));
1132
1133                         src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1134
1135                         src.Rows.Add (new object [] { 1, "mono 1" });
1136                         src.Rows.Add (new object [] { 2, "mono 2" });
1137                         src.Rows.Add (new object [] { 3, "mono 3" });
1138                         src.AcceptChanges ();
1139
1140                         src.Rows [0] [1] = "mono changed 1";  // modify 1st row
1141                         src.Rows [1].Delete ();              // delete 2nd row
1142                         // 3rd row is unchanged
1143                         src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1144
1145                         // build target table
1146                         DataTable target = new DataTable ();
1147                         target.Columns.Add ("id", typeof (int));
1148                         target.Columns.Add ("name", typeof (string));
1149
1150                         target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1151
1152                         // import all rows
1153                         target.ImportRow (src.Rows [0]);     // import 1st row
1154                         target.ImportRow (src.Rows [1]);     // import 2nd row
1155                         target.ImportRow (src.Rows [2]);     // import 3rd row
1156                         target.ImportRow (src.Rows [3]);     // import 4th row
1157
1158                         try {
1159                                 target.ImportRow (src.Rows [2]); // import 3rd row again
1160                                 Fail ("#AA1 Should have thrown exception violativ PK");
1161                         } catch (ConstraintException e) {}
1162
1163                         // check row states
1164                         AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1165                         AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1166                         AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1167                         AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1168
1169                         // check for modified row (1st row)
1170                         AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1171                         AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1172                         AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1173                         AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1174                         AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1175
1176                         // check for deleted row (2nd row)
1177                         AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1178
1179                         // check for unchanged row (3rd row)
1180                         AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1181                         AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1182                         AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1183                         AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1184
1185                         // check for newly added row (4th row)
1186                         AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1187                         AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1188                         AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1189                 }
1190
1191                 [Test]
1192                 public void ImportRowDetachedTest ()
1193                 {
1194                         DataTable table = new DataTable ();
1195                         DataColumn col = new DataColumn ();
1196                         col.ColumnName = "Id";
1197                         col.DataType = Type.GetType ("System.Int32");
1198                         table.Columns.Add (col);
1199
1200                         table.PrimaryKey = new DataColumn [] {col};
1201
1202                         col = new DataColumn ();
1203                         col.ColumnName = "Name";
1204                         col.DataType = Type.GetType ("System.String");
1205                         table.Columns.Add (col);
1206                         
1207                         DataRow row = table.NewRow ();
1208                         row ["Id"] = 147;
1209                         row ["name"] = "Abc";
1210
1211                         // keep silent as ms.net ;-), though this is not useful.
1212                         table.ImportRow (row);
1213
1214                         //if RowState is detached, then dont import the row.
1215                         AssertEquals ("#1", 0, table.Rows.Count);
1216                 }
1217
1218                 [Test]
1219                 public void ImportRowDeletedTest ()
1220                 {
1221                         DataTable table = new DataTable ();
1222                         table.Columns.Add ("col", typeof (int));
1223                         table.Columns.Add ("col1", typeof (int));
1224
1225                         DataRow row = table.Rows.Add (new object[] {1,2});
1226                         table.PrimaryKey = new DataColumn[] {table.Columns[0]};
1227                         table.AcceptChanges ();
1228
1229                         // If row is in Deleted state, then ImportRow loads the
1230                         // row.
1231                         row.Delete ();
1232                         table.ImportRow (row);
1233                         AssertEquals ("#1", 2, table.Rows.Count);
1234
1235                         // Both the deleted rows shud be now gone
1236                         table.AcceptChanges ();
1237                         AssertEquals ("#2", 0, table.Rows.Count);
1238
1239                         //just add another row
1240                         row = table.Rows.Add (new object[] {1,2});
1241                         // no exception shud be thrown
1242                         table.AcceptChanges ();
1243
1244                         // If row is in Deleted state, then ImportRow loads the
1245                         // row and validate only on RejectChanges
1246                         row.Delete ();
1247                         table.ImportRow (row);
1248                         AssertEquals ("#3", 2, table.Rows.Count);
1249                         AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
1250
1251                         try {
1252                                 table.RejectChanges ();
1253                                 Fail ("#5");
1254                         } catch (ConstraintException e) {
1255                         }
1256                 }
1257
1258                 [Test]
1259                 public void ClearReset () //To test Clear and Reset methods
1260                 {
1261                         DataTable table = new DataTable ("table");
1262                         DataTable table1 = new DataTable ("table1");
1263                 
1264                         DataSet set = new DataSet ();
1265                         set.Tables.Add (table);
1266                         set.Tables.Add (table1);
1267
1268                         table.Columns.Add ("Id", typeof (int));
1269                         table.Columns.Add ("Name", typeof (string));
1270                         table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1271                         table.CaseSensitive = false;
1272                         
1273                         table1.Columns.Add ("Id", typeof (int));
1274                         table1.Columns.Add ("Name", typeof (string));
1275
1276                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1277                         set.Relations.Add (dr);
1278                 
1279                         DataRow row = table.NewRow ();
1280                         row ["Id"] = 147;
1281                         row ["name"] = "Roopa";
1282                         table.Rows.Add (row);
1283                 
1284                         row = table.NewRow ();
1285                         row ["Id"] = 47;
1286                         row ["Name"] = "roopa";
1287                         table.Rows.Add (row);
1288                 
1289                         AssertEquals (2, table.Rows.Count);
1290                         AssertEquals (1, table.ChildRelations.Count);
1291                         try {
1292                                 table.Reset ();
1293                                 Fail ("#A01, should have thrown ArgumentException");
1294                         }
1295                         catch (ArgumentException) {
1296                         }
1297                         AssertEquals ("#CT01", 0, table.Rows.Count);
1298                         AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1299                         AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1300                         AssertEquals ("#CT04", 0, table.Constraints.Count);
1301
1302                         table1.Reset ();
1303                         AssertEquals ("#A05", 0, table1.Rows.Count);
1304                         AssertEquals ("#A06", 0, table1.Constraints.Count);
1305                         AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1306                 
1307                         // clear test
1308                         table.Clear ();
1309                         AssertEquals ("#A08", 0, table.Rows.Count);
1310 #if NET_1_1
1311                         AssertEquals ("#A09", 0, table.Constraints.Count);
1312 #else
1313                         AssertEquals ("#A09", 1, table.Constraints.Count);
1314 #endif
1315                         AssertEquals ("#A10", 0, table.ChildRelations.Count);
1316
1317                 }
1318
1319                 [Test]
1320                 public void ClearTest ()
1321                 {
1322                         DataTable table = new DataTable ("test");
1323                         table.Columns.Add ("id", typeof (int));
1324                         table.Columns.Add ("name", typeof (string));
1325                         
1326                         table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1327                         
1328                         table.Rows.Add (new object [] { 1, "mono 1" });
1329                         table.Rows.Add (new object [] { 2, "mono 2" });
1330                         table.Rows.Add (new object [] { 3, "mono 3" });
1331                         table.Rows.Add (new object [] { 4, "mono 4" });
1332
1333                         table.AcceptChanges ();
1334 #if NET_2_0
1335                         _tableClearedEventFired = false;
1336                         table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1337 #endif // NET_2_0
1338                         
1339                         table.Clear ();
1340 #if NET_2_0
1341                         AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1342 #endif // NET_2_0
1343                         
1344                         DataRow r = table.Rows.Find (1);
1345                         AssertEquals ("#1 should have cleared", true, r == null);
1346
1347                         // try adding new row. indexes should have cleared
1348                         table.Rows.Add (new object [] { 2, "mono 2" });
1349                         AssertEquals ("#2 should add row", 1, table.Rows.Count);
1350                 }
1351 #if NET_2_0
1352                 private bool _tableClearedEventFired = false;
1353                 private void OnTableCleared (object src, DataTableClearEventArgs args)
1354                 {
1355                         _tableClearedEventFired = true;
1356                 }
1357 #endif // NET_2_0
1358                 
1359
1360                 [Test]
1361                 public void Serialize ()
1362                 {
1363                         MemoryStream fs = new MemoryStream ();
1364                         
1365                         // Construct a BinaryFormatter and use it 
1366                         // to serialize the data to the stream.
1367                         BinaryFormatter formatter = new BinaryFormatter();
1368                 
1369                         // Create an array with multiple elements refering to 
1370                         // the one Singleton object.
1371                         DataTable dt = new DataTable();
1372                 
1373                 
1374                         dt.Columns.Add(new DataColumn("Id", typeof(string)));
1375                         dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1376                         dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1377                         dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1378                         dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1379                 
1380                         DataRow loRowToAdd;
1381                         loRowToAdd = dt.NewRow();
1382                         loRowToAdd[0] = "a";
1383                         loRowToAdd[1] = "b";
1384                         loRowToAdd[2] = "c";
1385                         loRowToAdd[3] = "d";
1386                         loRowToAdd[4] = "e";
1387                                                 
1388                         dt.Rows.Add(loRowToAdd);
1389                 
1390                         DataTable[] dtarr = new DataTable[] {dt}; 
1391                 
1392                         // Serialize the array elements.
1393                         formatter.Serialize(fs, dtarr);
1394                 
1395                         // Deserialize the array elements.
1396                         fs.Position = 0;
1397                         DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1398                 
1399                         DataSet ds = new DataSet();
1400                         ds.Tables.Add(a2[0]);
1401                 
1402                         StringWriter sw = new StringWriter ();
1403                         ds.WriteXml(sw);
1404                         XmlDocument doc = new XmlDocument ();
1405                         doc.LoadXml (sw.ToString ());
1406                         AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1407                 }
1408
1409                 [Test]
1410                 [ExpectedException (typeof (DataException))]
1411                 public void SetPrimaryKeyAssertsNonNull ()
1412                 {
1413                         DataTable dt = new DataTable ("table");
1414                         dt.Columns.Add ("col1");
1415                         dt.Columns.Add ("col2");
1416                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1417                         dt.Rows.Add (new object [] {1, 3});
1418                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1419
1420                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1421                 }
1422
1423                 [Test]
1424                 [ExpectedException (typeof (NoNullAllowedException))]
1425                 public void PrimaryKeyColumnChecksNonNull ()
1426                 {
1427                         DataTable dt = new DataTable ("table");
1428                         dt.Columns.Add ("col1");
1429                         dt.Columns.Add ("col2");
1430                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1431                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1432                         dt.Rows.Add (new object [] {1, 3});
1433                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1434                 }
1435
1436                 [Test]
1437                 public void PrimaryKey_CheckSetsAllowDBNull ()
1438                 {
1439                         DataTable table = new DataTable ();
1440                         DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1441                         DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1442         
1443                         AssertEquals ("#1" , true, col1.AllowDBNull);
1444                         AssertEquals ("#2" , true, col2.AllowDBNull);
1445                         AssertEquals ("#3" , false, col2.Unique);
1446                         AssertEquals ("#4" , false, col2.Unique);
1447
1448                         table.PrimaryKey = new DataColumn[] {col1,col2};
1449                         AssertEquals ("#5" , false, col1.AllowDBNull);
1450                         AssertEquals ("#6" , false, col2.AllowDBNull);
1451                         // LAMESPEC or bug ?? 
1452                         AssertEquals ("#7" , false, col1.Unique);
1453                         AssertEquals ("#8" , false, col2.Unique);
1454                 }
1455
1456                 void RowChanging (object o, DataRowChangeEventArgs e)
1457                 {
1458                         AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1459                         rowChangingRowChanging = true;
1460                 }
1461
1462                 void RowChanged (object o, DataRowChangeEventArgs e)
1463                 {
1464                         AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1465                         rowChangingRowChanged = true;
1466                 }
1467
1468                 bool rowChangingRowChanging, rowChangingRowChanged;
1469                 DataRowAction rowChangingExpectedAction;
1470
1471                 [Test]
1472                 public void RowChanging ()
1473                 {
1474                         DataTable dt = new DataTable ("table");
1475                         dt.Columns.Add ("col1");
1476                         dt.Columns.Add ("col2");
1477                         dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1478                         dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1479                         rowChangingExpectedAction = DataRowAction.Add;
1480                         dt.Rows.Add (new object [] {1, 2});
1481                         Assert ("changing,Added", rowChangingRowChanging);
1482                         Assert ("changed,Added", rowChangingRowChanged);
1483                         rowChangingExpectedAction = DataRowAction.Change;
1484                         dt.Rows [0] [0] = 2;
1485                         Assert ("changing,Changed", rowChangingRowChanging);
1486                         Assert ("changed,Changed", rowChangingRowChanged);
1487                 }
1488
1489                  [Test]
1490                 public void CloneSubClassTest()
1491                 {
1492                         MyDataTable dt1 = new MyDataTable();
1493                         MyDataTable dt = (MyDataTable)(dt1.Clone());
1494                         AssertEquals("A#01",2,MyDataTable.count);
1495                 }
1496
1497                 DataRowAction rowActionChanging = DataRowAction.Nothing;
1498                 DataRowAction rowActionChanged  = DataRowAction.Nothing;
1499                 [Test]
1500                 public void AcceptChangesTest ()
1501                 {
1502                         DataTable dt = new DataTable ("test");
1503                         dt.Columns.Add ("id", typeof (int));
1504                         dt.Columns.Add ("name", typeof (string));
1505                         
1506                         dt.Rows.Add (new object [] { 1, "mono 1" });
1507
1508                         dt.RowChanged  += new DataRowChangeEventHandler (OnRowChanged);
1509                         dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1510
1511                         try {
1512                                 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1513                                 dt.AcceptChanges ();
1514
1515                                 AssertEquals ("#1 should have fired event and set action to commit",
1516                                               DataRowAction.Commit, rowActionChanging);
1517                                 AssertEquals ("#2 should have fired event and set action to commit",
1518                                               DataRowAction.Commit, rowActionChanged);
1519
1520                         } finally {
1521                                 dt.RowChanged  -= new DataRowChangeEventHandler (OnRowChanged);
1522                                 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1523
1524                         }
1525                 }
1526
1527                                 [Test]
1528                                 public void ColumnObjectTypeTest() {
1529                                         DataTable dt = new DataTable();
1530                                         dt.Columns.Add("Series Label", typeof(SqlInt32));
1531                                         dt.Rows.Add(new object[] {"sss"});
1532                                         AssertEquals(1, dt.Rows.Count);
1533                                 }
1534
1535                 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1536                 {
1537                         rowActionChanging = args.Action;
1538                 }
1539                 
1540                 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1541                 {
1542                         rowActionChanged = args.Action;
1543                 }
1544
1545
1546 #if NET_2_0
1547                 #region DataTable.CreateDataReader Tests and DataTable.Load Tests
1548
1549                 private DataTable dt;
1550
1551                 private void localSetup () {
1552                         dt = new DataTable ("test");
1553                         dt.Columns.Add ("id", typeof (int));
1554                         dt.Columns.Add ("name", typeof (string));
1555                         dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
1556
1557                         dt.Rows.Add (new object[] { 1, "mono 1" });
1558                         dt.Rows.Add (new object[] { 2, "mono 2" });
1559                         dt.Rows.Add (new object[] { 3, "mono 3" });
1560
1561                         dt.AcceptChanges ();
1562                 }
1563
1564                 [Test]
1565                 public void CreateDataReader1 () {
1566                         localSetup ();
1567                         DataTableReader dtr = dt.CreateDataReader ();
1568                         Assert ("HasRows", dtr.HasRows);
1569                         AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1570                         int ri = 0;
1571                         while (dtr.Read ()) {
1572                                 for (int i = 0; i < dtr.FieldCount; i++) {
1573                                         AssertEquals ("RowData-" + ri + "-" + i, dt.Rows[ri][i],
1574                                                 dtr[i]);
1575                                 }
1576                                 ri++;
1577                         }
1578                 }
1579
1580                 [Test]
1581                 public void CreateDataReader2 () {
1582                         localSetup ();
1583                         DataTableReader dtr = dt.CreateDataReader ();
1584                         Assert ("HasRows", dtr.HasRows);
1585                         AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1586                         dtr.Read ();
1587                         AssertEquals ("RowData0-0", 1, dtr[0]);
1588                         AssertEquals ("RowData0-1", "mono 1", dtr[1]);
1589                         dtr.Read ();
1590                         AssertEquals ("RowData1-0", 2, dtr[0]);
1591                         AssertEquals ("RowData1-1", "mono 2", dtr[1]);
1592                         dtr.Read ();
1593                         AssertEquals ("RowData2-0", 3, dtr[0]);
1594                         AssertEquals ("RowData2-1", "mono 3", dtr[1]);
1595                 }
1596
1597                 [Test]
1598                 public void Load_NoSchema () {
1599                         localSetup ();
1600                         DataTable dtLoad = new DataTable ("LoadNoSchema");
1601                         DataTableReader dtr = dt.CreateDataReader ();
1602                         dtLoad.Load (dtr);
1603                         TableAssert (dtLoad);
1604                 }
1605
1606                 [Test]
1607                 [ExpectedException (typeof (ArgumentException))]
1608                 public void Load_Incompatible () {
1609                         localSetup ();
1610                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1611                         dtLoad.Columns.Add ("name", typeof (double));
1612                         DataTableReader dtr = dt.CreateDataReader ();
1613                         dtLoad.Load (dtr);
1614                 }
1615
1616                 [Test]
1617                 public void Load_ExtraColsEqualVal () {
1618                         localSetup ();
1619                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1620                         dtLoad.Columns.Add ("id", typeof (int));
1621                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1622                         dtLoad.Rows.Add (new object[] { 1 });
1623                         dtLoad.Rows.Add (new object[] { 2 });
1624                         dtLoad.Rows.Add (new object[] { 3 });
1625                         dtLoad.AcceptChanges ();
1626                         DataTableReader dtr = dt.CreateDataReader ();
1627                         dtLoad.Load (dtr);
1628                         TableAssert (dtLoad);
1629                 }
1630
1631                 [Test]
1632                 public void Load_ExtraColsNonEqualVal () {
1633                         localSetup ();
1634                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1635                         dtLoad.Columns.Add ("id", typeof (int));
1636                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1637                         dtLoad.Rows.Add (new object[] { 4 });
1638                         dtLoad.Rows.Add (new object[] { 5 });
1639                         dtLoad.Rows.Add (new object[] { 6 });
1640                         dtLoad.AcceptChanges ();
1641                         DataTableReader dtr = dt.CreateDataReader ();
1642                         dtLoad.Load (dtr);
1643                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1644                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1645                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1646                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1647                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1648                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1649                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1650                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1651                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1652                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1653                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1654                 }
1655
1656                 [Test]
1657                 [ExpectedException (typeof (ConstraintException))]
1658                 public void Load_MissingColsNonNullable () {
1659                         localSetup ();
1660                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1661                         dtLoad.Columns.Add ("id", typeof (int));
1662                         dtLoad.Columns.Add ("name", typeof (string));
1663                         dtLoad.Columns.Add ("missing", typeof (string));
1664                         dtLoad.Columns["missing"].AllowDBNull = false;
1665                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1666                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1667                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1668                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1669                         dtLoad.AcceptChanges ();
1670                         DataTableReader dtr = dt.CreateDataReader ();
1671                         dtLoad.Load (dtr);
1672                 }
1673
1674                 [Test]
1675                 public void Load_MissingColsDefault () {
1676                         localSetup ();
1677                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1678                         dtLoad.Columns.Add ("id", typeof (int));
1679                         dtLoad.Columns.Add ("name", typeof (string));
1680                         dtLoad.Columns.Add ("missing", typeof (string));
1681                         dtLoad.Columns["missing"].AllowDBNull = false;
1682                         dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1683                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1684                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1685                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1686                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1687                         dtLoad.AcceptChanges ();
1688                         DataTableReader dtr = dt.CreateDataReader ();
1689                         dtLoad.Load (dtr);
1690                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1691                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1692                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1693                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1694                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1695                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1696                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1697                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1698                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1699                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1700                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1701                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1702                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1703                         AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1704                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1705                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1706                         AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1707                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1708                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1709                         AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1710                 }
1711
1712                 [Test]
1713                 public void Load_MissingColsNullable () {
1714                         localSetup ();
1715                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1716                         dtLoad.Columns.Add ("id", typeof (int));
1717                         dtLoad.Columns.Add ("name", typeof (string));
1718                         dtLoad.Columns.Add ("missing", typeof (string));
1719                         dtLoad.Columns["missing"].AllowDBNull = true;
1720                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1721                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1722                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1723                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1724                         dtLoad.AcceptChanges ();
1725                         DataTableReader dtr = dt.CreateDataReader ();
1726                         dtLoad.Load (dtr);
1727                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1728                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1729                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1730                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1731                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1732                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1733                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1734                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1735                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1736                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1737                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1738                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1739                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1740                         //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1741                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1742                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1743                         //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1744                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1745                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1746                         //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1747                 }
1748
1749                 [Test]
1750                 [Category ("NotWorking")]
1751                 public void Load_RowStateChanges () {
1752                         localSetup ();
1753                         dt.Rows.Add (new object[] { 4, "mono 4" });
1754                         dt.AcceptChanges ();
1755                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1756                         dtLoad.Columns.Add ("id", typeof (int));
1757                         dtLoad.Columns.Add ("name", typeof (string));
1758                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1759                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1760                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1761                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1762                         dtLoad.AcceptChanges ();
1763                         // Update Table with following changes:
1764                         // Row0 unmodified, Row1 modified, Row2 deleted, Row3 added.
1765                         dtLoad.Rows[1]["name"] = "Modify 2";
1766                         dtLoad.Rows[2].Delete ();
1767                         DataRow row = dtLoad.NewRow ();
1768                         row["id"] = 4;
1769                         row["name"] = "Add 4";
1770                         dtLoad.Rows.Add (row);
1771                         DataTableReader dtr = dt.CreateDataReader ();
1772                         dtLoad.Load (dtr);
1773                         // asserting Unchanged Row0
1774                         AssertEquals ("RowData0-C", "mono 1",
1775                                 dtLoad.Rows[0][1,DataRowVersion.Current]);
1776                         AssertEquals ("RowData0-O", "mono 1",
1777                                 dtLoad.Rows[0][1,DataRowVersion.Original]);
1778                         AssertEquals ("RowState0", DataRowState.Unchanged,
1779                                 dtLoad.Rows[0].RowState);
1780                         // asserting Modified Row1
1781                         AssertEquals ("RowData1-C", "Modify 2",
1782                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
1783                         AssertEquals ("RowData1-O", "mono 2",
1784                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
1785                         AssertEquals ("RowState1", DataRowState.Modified,
1786                                 dtLoad.Rows[1].RowState);
1787                         // asserting Deleted Row2
1788                         AssertEquals ("RowData1-O", "mono 3",
1789                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
1790                         AssertEquals ("RowState2", DataRowState.Deleted,
1791                                 dtLoad.Rows[2].RowState);
1792                         // asserting Added Row3
1793                         AssertEquals ("RowData3-C", "Add 4",
1794                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
1795                         AssertEquals ("RowData3-O", "mono 4",
1796                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
1797                         AssertEquals ("RowState3", DataRowState.Modified,
1798                                 dtLoad.Rows[3].RowState);
1799                 }
1800
1801                 [Test]
1802                 [ExpectedException (typeof (VersionNotFoundException))]
1803                 [Category ("NotWorking")]
1804                 public void Load_RowStateChangeDelete () {
1805                         localSetup ();
1806                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1807                         dtLoad.Columns.Add ("id", typeof (int));
1808                         dtLoad.Columns.Add ("name", typeof (string));
1809                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1810                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1811                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1812                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1813                         dtLoad.AcceptChanges ();
1814                         dtLoad.Rows[2].Delete ();
1815                         DataTableReader dtr = dt.CreateDataReader ();
1816                         dtLoad.Load (dtr);
1817                         AssertEquals ("RowData1-O", " ",
1818                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
1819                 }
1820
1821                 private void TableAssert (DataTable dtAssert) {
1822                         AssertEquals ("NColumns", 2, dtAssert.Columns.Count);
1823                         AssertEquals ("NRows", 3, dtAssert.Rows.Count);
1824                         AssertEquals ("RowData0-0", 1, dtAssert.Rows[0][0]);
1825                         AssertEquals ("RowData0-1", "mono 1", dtAssert.Rows[0][1]);
1826                         AssertEquals ("RowData1-0", 2, dtAssert.Rows[1][0]);
1827                         AssertEquals ("RowData1-1", "mono 2", dtAssert.Rows[1][1]);
1828                         AssertEquals ("RowData2-0", 3, dtAssert.Rows[2][0]);
1829                         AssertEquals ("RowData2-1", "mono 3", dtAssert.Rows[2][1]);
1830                 }
1831
1832                 #endregion // DataTable.CreateDataReader Tests and DataTable.Load Tests
1833 #endif // NET_2_0
1834
1835         }
1836                                                                                                     
1837                                                                                                     
1838          public  class MyDataTable:DataTable {
1839                                                                                                     
1840              public static int count = 0;
1841                                                                                                     
1842              public MyDataTable() {
1843                                                                                                     
1844                     count++;
1845              }
1846                                                                                                     
1847          }
1848
1849         [Serializable]
1850         [TestFixture]
1851         public class AppDomainsAndFormatInfo
1852         {
1853                 public void Remote ()
1854                 {
1855                         int n = (int) Convert.ChangeType ("5", typeof (int));
1856                         Assertion.AssertEquals ("n", 5, n);
1857                 }
1858 #if !TARGET_JVM
1859                 [Test]
1860                 public void NFIFromBug55978 ()
1861                 {
1862                         AppDomain domain = AppDomain.CreateDomain ("testdomain");
1863                         AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
1864                         test.Remote ();
1865                         domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
1866                         AppDomain.Unload (domain);
1867                 }
1868 #endif
1869
1870                 [Test]
1871                 public void Bug55978 ()
1872                 {
1873                         DataTable dt = new DataTable ();
1874                         dt.Columns.Add ("StartDate", typeof (DateTime));
1875          
1876                         DataRow dr;
1877                         DateTime date = DateTime.Now;
1878          
1879                         for (int i = 0; i < 10; i++) {
1880                                 dr = dt.NewRow ();
1881                                 dr ["StartDate"] = date.AddDays (i);
1882                                 dt.Rows.Add (dr);
1883                         }
1884          
1885                         DataView dv = dt.DefaultView;
1886                         dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
1887                         Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
1888                         Assertion.AssertEquals ("View", 2, dv.Count);
1889                 }
1890         }
1891 }