updating to the latest module.
[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.Globalization;
38 using System.IO;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Xml;
41
42 namespace MonoTests.System.Data
43 {
44         [TestFixture]
45         public class DataTableTest : Assertion
46         {
47                 [Test]
48                 public void Ctor()
49                 {
50                         DataTable dt = new DataTable();
51
52                         AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
53                         Assert("Col",dt.Columns != null);
54                         //Assert(dt.ChildRelations != null);
55                         Assert("Const", dt.Constraints != null);
56                         Assert("ds", dt.DataSet == null); 
57                         Assert("dv", dt.DefaultView != null);
58                         Assert("de", dt.DisplayExpression == "");
59                         Assert("ep", dt.ExtendedProperties != null);
60                         Assert("he", dt.HasErrors == false);
61                         Assert("lc", dt.Locale != null);
62                         Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
63                         Assert("ns", dt.Namespace == "");
64                         //Assert(dt.ParentRelations != null);
65                         Assert("pf", dt.Prefix == "");
66                         Assert("pk", dt.PrimaryKey != null);
67                         Assert("rows", dt.Rows != null);
68                         Assert("Site", dt.Site == null);
69                         Assert("tname", dt.TableName == "");
70                         
71                 }
72
73                 [Test]
74                 public void Select ()
75                 {
76                         DataSet Set = new DataSet ();
77                         DataTable Mom = new DataTable ("Mom");
78                         DataTable Child = new DataTable ("Child");                      
79                         Set.Tables.Add (Mom);
80                         Set.Tables.Add (Child);
81                         
82                         DataColumn Col = new DataColumn ("Name");
83                         DataColumn Col2 = new DataColumn ("ChildName");
84                         Mom.Columns.Add (Col);
85                         Mom.Columns.Add (Col2);
86                         
87                         DataColumn Col3 = new DataColumn ("Name");
88                         DataColumn Col4 = new DataColumn ("Age");
89                         Col4.DataType = Type.GetType ("System.Int16");
90                         Child.Columns.Add (Col3);
91                         Child.Columns.Add (Col4);
92                         
93                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
94                         Set.Relations.Add (Relation);
95
96                         DataRow Row = Mom.NewRow ();
97                         Row [0] = "Laura";
98                         Row [1] = "Nick";
99                         Mom.Rows.Add (Row);
100                         
101                         Row = Mom.NewRow ();
102                         Row [0] = "Laura";
103                         Row [1] = "Dick";
104                         Mom.Rows.Add (Row);
105                         
106                         Row = Mom.NewRow ();
107                         Row [0] = "Laura";
108                         Row [1] = "Mick";
109                         Mom.Rows.Add (Row);
110
111                         Row = Mom.NewRow ();
112                         Row [0] = "Teresa";
113                         Row [1] = "Jack";
114                         Mom.Rows.Add (Row);
115                         
116                         Row = Mom.NewRow ();
117                         Row [0] = "Teresa";
118                         Row [1] = "Mack";
119                         Mom.Rows.Add (Row);
120                         
121                         Row = Child.NewRow ();
122                         Row [0] = "Nick";
123                         Row [1] = 15;
124                         Child.Rows.Add (Row);
125                         
126                         Row = Child.NewRow ();
127                         Row [0] = "Dick";
128                         Row [1] = 25;
129                         Child.Rows.Add (Row);
130                         
131                         Row = Child.NewRow ();
132                         Row [0] = "Mick";
133                         Row [1] = 35;
134                         Child.Rows.Add (Row);
135                         
136                         Row = Child.NewRow ();
137                         Row [0] = "Jack";
138                         Row [1] = 10;
139                         Child.Rows.Add (Row);
140                         
141                         Row = Child.NewRow ();
142                         Row [0] = "Mack";
143                         Row [1] = 19;
144                         Child.Rows.Add (Row);
145                 
146                         Row = Child.NewRow ();
147                         Row [0] = "Mack";
148                         Row [1] = 99;
149                         Child.Rows.Add (Row);
150                         
151                         DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
152                         AssertEquals ("test#01", 2, Rows.Length);
153                         
154                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
155                         AssertEquals ("test#02", 0, Rows.Length);
156
157                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
158                         AssertEquals ("test#03", 1, Rows.Length);
159
160                         Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
161                         AssertEquals ("test#04", "Mack", Rows [0] [1]);
162                         
163                         Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
164                         AssertEquals ("test#05", 5, Rows.Length);
165                         
166                         Rows = Child.Select ("age = 20 - 1");
167                         AssertEquals ("test#06", 1, Rows.Length);
168                         
169                         Rows = Child.Select ("age <= 20");
170                         AssertEquals ("test#07", 3, Rows.Length);
171                         
172                         Rows = Child.Select ("age >= 20");
173                         AssertEquals ("test#08", 3, Rows.Length);
174                         
175                         Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
176                         AssertEquals ("test#09", 2, Rows.Length);
177
178                         Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
179                         AssertEquals ("test#10", 1, Rows.Length);
180                         AssertEquals ("test#11", "Mack", Rows [0] [0]);
181                         
182                         Rows = Child.Select ("not (Name = 'Jack')");
183                         AssertEquals ("test#12", 5, Rows.Length);
184                 }
185                 
186                 [Test]
187                 public void Select2 ()
188                 {
189                         DataSet Set = new DataSet ();
190                         DataTable Child = new DataTable ("Child");
191
192                         Set.Tables.Add (Child);
193                                                 
194                         DataColumn Col3 = new DataColumn ("Name");
195                         DataColumn Col4 = new DataColumn ("Age");
196                         Col4.DataType = Type.GetType ("System.Int16");
197                         Child.Columns.Add (Col3);
198                         Child.Columns.Add (Col4);
199                         
200                         DataRow Row = Child.NewRow ();
201                         Row [0] = "Nick";
202                         Row [1] = 15;
203                         Child.Rows.Add (Row);
204                         
205                         Row = Child.NewRow ();
206                         Row [0] = "Dick";
207                         Row [1] = 25;
208                         Child.Rows.Add (Row);
209                         
210                         Row = Child.NewRow ();
211                         Row [0] = "Mick";
212                         Row [1] = 35;
213                         Child.Rows.Add (Row);
214                         
215                         Row = Child.NewRow ();
216                         Row [0] = "Jack";
217                         Row [1] = 10;
218                         Child.Rows.Add (Row);
219                         
220                         Row = Child.NewRow ();
221                         Row [0] = "Mack";
222                         Row [1] = 19;
223                         Child.Rows.Add (Row);
224                 
225                         Row = Child.NewRow ();
226                         Row [0] = "Mack";
227                         Row [1] = 99;
228                         Child.Rows.Add (Row);
229
230                         DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
231                         AssertEquals ("test#01", 3, Rows.Length);
232                         AssertEquals ("test#02", "Mack", Rows [0] [0]);
233                         AssertEquals ("test#03", "Mick", Rows [1] [0]);                 
234                         AssertEquals ("test#04", "Dick", Rows [2] [0]);                 
235                         
236                         Rows = Child.Select ("age >= 20", "age asc");
237                         AssertEquals ("test#05", 3, Rows.Length);
238                         AssertEquals ("test#06", "Dick", Rows [0] [0]);
239                         AssertEquals ("test#07", "Mick", Rows [1] [0]);                 
240                         AssertEquals ("test#08", "Mack", Rows [2] [0]);                 
241                 
242                         Rows = Child.Select ("age >= 20", "name asc");
243                         AssertEquals ("test#09", 3, Rows.Length);
244                         AssertEquals ("test#10", "Dick", Rows [0] [0]);
245                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
246                         AssertEquals ("test#12", "Mick", Rows [2] [0]);                 
247
248                         Rows = Child.Select ("age >= 20", "name desc");
249                         AssertEquals ("test#09", 3, Rows.Length);
250                         AssertEquals ("test#10", "Mick", Rows [0] [0]);
251                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
252                         AssertEquals ("test#12", "Dick", Rows [2] [0]);                 
253
254                 }
255
256                 [Test]
257                 public void SelectParsing ()
258                 {
259                         DataTable T = new DataTable ("test");
260                         DataColumn C = new DataColumn ("name");
261                         T.Columns.Add (C);
262                         C = new DataColumn ("age");
263                         C.DataType = typeof (int);
264                         T.Columns.Add (C);
265                         C = new DataColumn ("id");
266                         T.Columns.Add (C);
267                         
268                         DataSet Set = new DataSet ("TestSet");
269                         Set.Tables.Add (T);
270                         
271                         DataRow Row = null;
272                         for (int i = 0; i < 100; i++) {
273                                 Row = T.NewRow ();
274                                 Row [0] = "human" + i;
275                                 Row [1] = i;
276                                 Row [2] = i;
277                                 T.Rows.Add (Row);
278                         }
279                         
280                         Row = T.NewRow ();
281                         Row [0] = "h*an";
282                         Row [1] = 1;
283                         Row [2] = 1;
284                         T.Rows.Add (Row);
285
286                         AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
287                         
288                         AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
289
290                         try {
291                                 T.Select ("name = 1human ");
292                                 Fail ("test#03");
293                         } catch (Exception e) {
294                                 
295                                 // missing operand after 'human' operand 
296                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());                          
297                         }
298                         
299                         try {                   
300                                 T.Select ("name = 1");
301                                 Fail ("test#05");
302                         } catch (Exception e) {
303                                 
304                                 // Cannot perform '=' operation between string and Int32
305                                 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
306                         }
307                         
308                         AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
309
310                 }
311
312                 [Test]
313                 public void SelectOperators ()
314                 {
315                         DataTable T = new DataTable ("test");
316                         DataColumn C = new DataColumn ("name");
317                         T.Columns.Add (C);
318                         C = new DataColumn ("age");
319                         C.DataType = typeof (int);
320                         T.Columns.Add (C);
321                         C = new DataColumn ("id");
322                         T.Columns.Add (C);
323                         
324                         DataSet Set = new DataSet ("TestSet");
325                         Set.Tables.Add (T);
326                         
327                         DataRow Row = null;
328                         for (int i = 0; i < 100; i++) {
329                                 Row = T.NewRow ();
330                                 Row [0] = "human" + i;
331                                 Row [1] = i;
332                                 Row [2] = i;
333                                 T.Rows.Add (Row);
334                         }
335                         
336                         Row = T.NewRow ();
337                         Row [0] = "h*an";
338                         Row [1] = 1;
339                         Row [2] = 1;
340                         T.Rows.Add (Row);
341                         
342                         AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
343                         AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);                    
344                         AssertEquals ("test#03", 12, T.Select ("age< =10").Length);                     
345                         AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
346                         AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);                    
347                         AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
348                         AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
349                         AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
350                         // FIXME: Somebody explain how this can be possible.
351                         // it seems that it is no matter between 10 - 30. The
352                         // result is allways 25 :-P
353                         //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
354                         
355                 }
356
357                 [Test]
358                 public void SelectExceptions ()
359                 {
360                         DataTable T = new DataTable ("test");
361                         DataColumn C = new DataColumn ("name");
362                         T.Columns.Add (C);
363                         C = new DataColumn ("age");
364                         C.DataType = typeof (int);
365                         T.Columns.Add (C);
366                         C = new DataColumn ("id");
367                         T.Columns.Add (C);
368                         
369                         for (int i = 0; i < 100; i++) {
370                                 DataRow Row = T.NewRow ();
371                                 Row [0] = "human" + i;
372                                 Row [1] = i;
373                                 Row [2] = i;
374                                 T.Rows.Add (Row);
375                         }
376                         
377                         try {
378                                 T.Select ("name = human1");
379                                 Fail ("test#01");
380                         } catch (Exception e) {
381                                 
382                                 // column name human not found
383                                 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
384                         }
385                         
386                         AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
387                         AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
388                         
389                         try {
390                                 T.Select ("id = 1k3");
391                                 Fail ("test#06");
392                         } catch (Exception e) {
393                                 
394                                 // no operands after k3 operator
395                                 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
396                         }                                               
397                 }
398                 
399                 [Test]
400                 public void SelectStringOperators ()
401                 {
402                         DataTable T = new DataTable ("test");
403                         DataColumn C = new DataColumn ("name");
404                         T.Columns.Add (C);
405                         C = new DataColumn ("age");
406                         C.DataType = typeof (int);
407                         T.Columns.Add (C);
408                         C = new DataColumn ("id");
409                         T.Columns.Add (C);
410                         
411                         DataSet Set = new DataSet ("TestSet");
412                         Set.Tables.Add (T);
413                         
414                         DataRow Row = null;
415                         for (int i = 0; i < 100; i++) {
416                                 Row = T.NewRow ();
417                                 Row [0] = "human" + i;
418                                 Row [1] = i;
419                                 Row [2] = i;
420                                 T.Rows.Add (Row);
421                         }
422                         Row = T.NewRow ();
423                         Row [0] = "h*an";
424                         Row [1] = 1;
425                         Row [2] = 1;
426                         T.Rows.Add (Row);
427                                         
428                         AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
429                         
430                         AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);                       
431                         AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
432                         AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);                     
433                         AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
434                         AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
435                         
436                         AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
437                         
438                         Set.CaseSensitive = true;
439                         AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
440                         
441                         T.CaseSensitive = false;
442                         AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
443                         
444                         T.CaseSensitive = true;
445                         AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
446                         
447                         Set.CaseSensitive = false;
448                         AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
449                         
450                         T.CaseSensitive = false;
451                         AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
452                         
453                         AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
454                         AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
455                         AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
456                         
457                         try {
458                                 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
459                                 Fail ("test#16");
460                         } catch (Exception e) {
461                                 
462                                 // 'h*an1' is invalid
463                                 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
464                         }
465                         
466                         try {
467                                 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
468                                 Fail ("test#19");
469                         } catch (Exception e) {
470                                 
471                                 // 'h%an1' is invalid
472                                 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
473                         }
474                         
475                         AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
476                         AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
477                         
478                 }
479
480                 [Test]
481                 public void SelectAggregates ()
482                 {
483                         DataTable T = new DataTable ("test");
484                         DataColumn C = new DataColumn ("name");
485                         T.Columns.Add (C);
486                         C = new DataColumn ("age");
487                         C.DataType = typeof (int);
488                         T.Columns.Add (C);
489                         C = new DataColumn ("id");
490                         T.Columns.Add (C);
491                         DataRow Row = null;
492                         
493                         for (int i = 0; i < 1000; i++) {
494                                 Row = T.NewRow ();
495                                 Row [0] = "human" + i;
496                                 Row [1] = i;
497                                 Row [2] = i;
498                                 T.Rows.Add (Row);
499                         }
500                         
501                         AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
502                         AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
503                         AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
504                         AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
505                         AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
506                         AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
507                         AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
508                 }
509                 
510                 [Test]
511                 public void SelectFunctions ()
512                 {
513                         DataTable T = new DataTable ("test");
514                         DataColumn C = new DataColumn ("name");
515                         T.Columns.Add (C);
516                         C = new DataColumn ("age");
517                         C.DataType = typeof (int);
518                         T.Columns.Add (C);
519                         C = new DataColumn ("id");
520                         T.Columns.Add (C);
521                         DataRow Row = null;
522                         
523                         for (int i = 0; i < 1000; i++) {
524                                 Row = T.NewRow ();
525                                 Row [0] = "human" + i;
526                                 Row [1] = i;
527                                 Row [2] = i;
528                                 T.Rows.Add (Row);
529                         }
530                         
531                         Row = T.NewRow ();
532                         Row [0] = "human" + "test";
533                         Row [1] = DBNull.Value;
534                         Row [2] = DBNull.Value;
535                         T.Rows.Add (Row);
536
537                         //TODO: How to test Convert-function
538                         AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);                        
539                         AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
540                         AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
541                         AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);                   
542                         AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);                  
543                         AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
544                         AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
545                         AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
546                         AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
547                         AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
548                         
549                 }
550
551                 [Test]
552                 public void SelectRelations ()
553                 {
554                         DataSet Set = new DataSet ();
555                         DataTable Mom = new DataTable ("Mom");
556                         DataTable Child = new DataTable ("Child");
557
558                         Set.Tables.Add (Mom);
559                         Set.Tables.Add (Child);
560                         
561                         DataColumn Col = new DataColumn ("Name");
562                         DataColumn Col2 = new DataColumn ("ChildName");
563                         Mom.Columns.Add (Col);
564                         Mom.Columns.Add (Col2);
565                         
566                         DataColumn Col3 = new DataColumn ("Name");
567                         DataColumn Col4 = new DataColumn ("Age");
568                         Col4.DataType = Type.GetType ("System.Int16");
569                         Child.Columns.Add (Col3);
570                         Child.Columns.Add (Col4);
571                         
572                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
573                         Set.Relations.Add (Relation);
574
575                         DataRow Row = Mom.NewRow ();
576                         Row [0] = "Laura";
577                         Row [1] = "Nick";
578                         Mom.Rows.Add (Row);
579                         
580                         Row = Mom.NewRow ();
581                         Row [0] = "Laura";
582                         Row [1] = "Dick";
583                         Mom.Rows.Add (Row);
584                         
585                         Row = Mom.NewRow ();
586                         Row [0] = "Laura";
587                         Row [1] = "Mick";
588                         Mom.Rows.Add (Row);
589
590                         Row = Mom.NewRow ();
591                         Row [0] = "Teresa";
592                         Row [1] = "Jack";
593                         Mom.Rows.Add (Row);
594                         
595                         Row = Mom.NewRow ();
596                         Row [0] = "Teresa";
597                         Row [1] = "Mack";
598                         Mom.Rows.Add (Row);
599                         
600                         Row = Child.NewRow ();
601                         Row [0] = "Nick";
602                         Row [1] = 15;
603                         Child.Rows.Add (Row);
604                         
605                         Row = Child.NewRow ();
606                         Row [0] = "Dick";
607                         Row [1] = 25;
608                         Child.Rows.Add (Row);
609                         
610                         Row = Child.NewRow ();
611                         Row [0] = "Mick";
612                         Row [1] = 35;
613                         Child.Rows.Add (Row);
614                         
615                         Row = Child.NewRow ();
616                         Row [0] = "Jack";
617                         Row [1] = 10;
618                         Child.Rows.Add (Row);
619                         
620                         Row = Child.NewRow ();
621                         Row [0] = "Mack";
622                         Row [1] = 19;
623                         Child.Rows.Add (Row);
624                 
625                         Row = Child.NewRow ();
626                         Row [0] = "Mack";
627                         Row [1] = 99;
628                         Child.Rows.Add (Row);
629                         
630                         DataRow [] Rows = Child.Select ("name = Parent.Childname");
631                         AssertEquals ("test#01", 6, Rows.Length);
632                         Rows = Child.Select ("Parent.childname = 'Jack'");
633                         AssertEquals ("test#02", 1, Rows.Length);
634                         
635                         /*
636                         try {
637                                 // FIXME: LAMESPEC: Why the exception is thrown why... why... 
638                                 Mom.Select ("Child.Name = 'Jack'");
639                                 Fail ("test#03");
640                         } catch (Exception e) {
641                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
642                                 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
643                         }
644                         */
645                         
646                         Rows = Child.Select ("Parent.name = 'Laura'");
647                         AssertEquals ("test#06", 3, Rows.Length);
648                         
649                         DataTable Parent2 = new DataTable ("Parent2");
650                         Col = new DataColumn ("Name");
651                         Col2 = new DataColumn ("ChildName");
652
653                         Parent2.Columns.Add (Col);
654                         Parent2.Columns.Add (Col2);
655                         Set.Tables.Add (Parent2);
656                         
657                         Row = Parent2.NewRow ();
658                         Row [0] = "Laura";
659                         Row [1] = "Nick";
660                         Parent2.Rows.Add (Row);
661                         
662                         Row = Parent2.NewRow ();
663                         Row [0] = "Laura";
664                         Row [1] = "Dick";
665                         Parent2.Rows.Add (Row);
666                         
667                         Row = Parent2.NewRow ();
668                         Row [0] = "Laura";
669                         Row [1] = "Mick";
670                         Parent2.Rows.Add (Row);
671
672                         Row = Parent2.NewRow ();
673                         Row [0] = "Teresa";
674                         Row [1] = "Jack";
675                         Parent2.Rows.Add (Row);
676                         
677                         Row = Parent2.NewRow ();
678                         Row [0] = "Teresa";
679                         Row [1] = "Mack";
680                         Parent2.Rows.Add (Row);
681
682                         Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
683                         Set.Relations.Add (Relation);
684                         
685                         try {
686                                 Rows = Child.Select ("Parent.ChildName = 'Jack'");
687                                 Fail ("test#07");
688                         } catch (Exception e) {
689                                 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
690                                 //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);
691                         }
692                         
693                         Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
694                         AssertEquals ("test#10", 1, Rows.Length);
695
696                         Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
697                         AssertEquals ("test#10", 1, Rows.Length);
698                         
699                         try {
700                                 Mom.Select ("Parent.name  = 'John'");
701                         } catch (Exception e) {
702                                 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
703                                 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
704                         }
705                         
706                 }
707
708                 [Test]
709                 public void ToStringTest()
710                 {
711                         DataTable dt = new DataTable();
712                         dt.Columns.Add("Col1",typeof(int));
713                         
714                         dt.TableName = "Mytable";
715                         dt.DisplayExpression = "Col1";
716                         
717                         
718                         string cmpr = dt.TableName + " + " + dt.DisplayExpression;
719                         AssertEquals(cmpr,dt.ToString());
720                 }
721
722                 [Test]
723                 public void PrimaryKey ()
724                 {
725                         DataTable dt = new DataTable ();
726                         DataColumn Col = new DataColumn ();
727                         Col.AllowDBNull = false;
728                         Col.DataType = typeof (int);
729                         dt.Columns.Add (Col);
730                         dt.Columns.Add ();
731                         dt.Columns.Add ();
732                         dt.Columns.Add ();
733                         
734                         AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
735                         
736                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
737                         AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
738                         AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
739                         
740                         dt.PrimaryKey = null;
741                         AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
742                         
743                         Col = new DataColumn ("failed");
744                         
745                         try {
746                                 dt.PrimaryKey = new DataColumn [] {Col};
747                                 Fail ("test#05");                                       
748                         } catch (Exception e) {
749                                 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
750                                 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
751                         }
752                         
753                         DataTable dt2 = new DataTable ();
754                         dt2.Columns.Add ();
755                         
756                         try {
757                                 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
758                                 Fail ("test#08");
759                         } catch (Exception e) {
760                                 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
761                                 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
762                         }
763                         
764                         
765                         AssertEquals ("test#11", 0, dt.Constraints.Count);
766                         
767                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
768                         AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
769                         AssertEquals ("test#13", 1, dt.Constraints.Count);
770                         AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
771                         AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
772                         AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
773                         
774                 }
775                 
776                 [Test]
777                 public void PropertyExceptions ()
778                 {
779                         DataSet set = new DataSet ();
780                         DataTable table = new DataTable ();
781                         DataTable table1 =  new DataTable ();
782                         set.Tables.Add (table);
783                         set.Tables.Add (table1);
784
785                         DataColumn col = new DataColumn ();
786                         col.ColumnName = "Id";
787                         col.DataType = Type.GetType ("System.Int32");
788                         table.Columns.Add (col);
789                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
790                         table.Constraints.Add (uc);
791                         table.CaseSensitive = false;
792                                                                                                                            
793                         col = new DataColumn ();
794                         col.ColumnName = "Name";
795                         col.DataType = Type.GetType ("System.String");
796                         table.Columns.Add (col);
797                         
798                         col = new DataColumn ();
799                         col.ColumnName = "Id";
800                         col.DataType = Type.GetType ("System.Int32");
801                         table1.Columns.Add (col);
802                         col = new DataColumn ();
803                         col.ColumnName = "Name";
804                         col.DataType = Type.GetType ("System.String");
805                         table1.Columns.Add (col);
806
807                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
808                         set.Relations.Add (dr);
809
810                         try {
811                                 table.CaseSensitive = true;
812                                 table1.CaseSensitive = true;
813                                 Fail ("#A01");
814                         }
815                         catch (Exception e) {
816                                 if (e.GetType () != typeof (AssertionException))
817                                         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);
818                                 else
819                                         Console.WriteLine (e);
820                         }
821                         try {
822                                 CultureInfo cultureInfo = new CultureInfo ("en-gb");
823                                 table.Locale = cultureInfo;
824                                 table1.Locale = cultureInfo;
825                                 Fail ("#A03");
826                         }
827                         catch (Exception e) {
828                                  if (e.GetType () != typeof (AssertionException))
829                                         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);
830                                 else
831                                         Console.WriteLine (e);
832                         }
833                         try {
834                                 table.Prefix = "Prefix#1";
835                                 Fail ("#A05");
836                         }
837                         catch (Exception e){
838                                 if (e.GetType () != typeof (AssertionException))
839                                         AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
840                                 else
841                                         Console.WriteLine (e);
842
843                         }
844                 }
845
846                 [Test]
847                 public void GetErrors ()
848                 {
849                         DataTable table = new DataTable ();
850
851                         DataColumn col = new DataColumn ();
852                         col.ColumnName = "Id";
853                         col.DataType = Type.GetType ("System.Int32");
854                         table.Columns.Add (col);
855                                                                                                                              
856                         col = new DataColumn ();
857                         col.ColumnName = "Name";
858                         col.DataType = Type.GetType ("System.String");
859                         table.Columns.Add (col);
860                         
861                         DataRow row = table.NewRow ();
862                         row ["Id"] = 147;
863                         row ["name"] = "Abc";
864                         row.RowError = "Error#1";
865                         table.Rows.Add (row);
866
867                         AssertEquals ("#A01", 1, table.GetErrors ().Length);
868                         AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
869                 }
870                 [Test]
871                 public void CloneCopyTest ()
872                 {
873                         DataTable table = new DataTable ();
874                         table.TableName = "Table#1";
875                         DataTable table1 = new DataTable ();
876                         table1.TableName = "Table#2";
877                 
878                         table.AcceptChanges ();
879                         
880                         DataSet set = new DataSet ("Data Set#1");
881                         set.DataSetName = "Dataset#1";
882                         set.Tables.Add (table);
883                         set.Tables.Add (table1);
884
885                         DataColumn col = new DataColumn ();
886                         col.ColumnName = "Id";
887                         col.DataType = Type.GetType ("System.Int32");
888                         table.Columns.Add (col);
889                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
890                         table.Constraints.Add (uc);
891                 
892                         col = new DataColumn ();
893                         col.ColumnName = "Id";
894                         col.DataType = Type.GetType ("System.Int32");
895                         table1.Columns.Add (col);
896                                                                                                                              
897                         col = new DataColumn ();
898                         col.ColumnName = "Name";
899                         col.DataType = Type.GetType ("System.String");
900                         table.Columns.Add (col);
901                         
902                         col = new DataColumn ();
903                         col.ColumnName = "Name";
904                         col.DataType = Type.GetType ("System.String");
905                         table1.Columns.Add (col);
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                         row = table.NewRow ();
913                         row ["Id"] = 47;
914                         row ["name"] = "Efg";
915                         table.Rows.Add (row);
916                         table.AcceptChanges ();
917                                                                                                                              
918                         table.CaseSensitive = true;
919                         table1.CaseSensitive = true;
920                         table.MinimumCapacity = 100;
921                         table.Prefix = "PrefixNo:1";
922                         table.Namespace = "Namespace#1";
923                         table.DisplayExpression = "Id / Name + (Id * Id)";
924                         DataColumn[] colArray = {table.Columns[0]};
925                         table.PrimaryKey = colArray;
926                         table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
927 #if NET_1_1 // This prevents further tests after .NET 1.1.
928 #else
929                         CultureInfo cultureInfo = new CultureInfo ("en-gb");
930                         table.Locale = cultureInfo;
931 #endif
932
933                         row = table1.NewRow ();
934                         row ["Name"] = "Abc";
935                         row ["Id"] = 147;
936                         table1.Rows.Add (row);
937
938                         row = table1.NewRow ();
939                         row ["Id"] = 47;
940                         row ["Name"] = "Efg";
941                         table1.Rows.Add (row);
942                 
943                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
944                         set.Relations.Add (dr);
945
946                         //Testing properties of clone
947                         DataTable cloneTable = table.Clone ();
948                         AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
949                         AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
950                         AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
951                         AssertEquals ("#A04", 2,  cloneTable.Columns.Count);
952                         AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
953                         AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
954                         AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
955                         AssertEquals ("#A08", false ,cloneTable.HasErrors);
956 #if NET_1_1
957 #else
958                         AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
959 #endif
960                         AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
961                         AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
962                         AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
963                         AssertEquals ("#A13", "Id",  cloneTable.PrimaryKey[0].ColumnName);
964                         AssertEquals ("#A14",0 , cloneTable.Rows.Count );
965                         AssertEquals ("#A15", "Table#1", cloneTable.TableName);
966
967                         //Testing properties of copy
968                         DataTable copyTable = table.Copy ();
969                         AssertEquals ("#A16",true ,copyTable.CaseSensitive);
970                         AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
971                         AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
972                         AssertEquals ("#A19", 2,  copyTable.Columns.Count);
973                         AssertEquals ("#A20", 1, copyTable.Constraints.Count);
974                         AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
975                         AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
976                         AssertEquals ("#A23", true ,copyTable.HasErrors);
977 #if NET_1_1
978 #else
979                         AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
980 #endif
981                         AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
982                         AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
983                         AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
984                         AssertEquals ("#A28", "Id",  copyTable.PrimaryKey[0].ColumnName);
985                         AssertEquals ("#A29", 2 , copyTable.Rows.Count );
986                         AssertEquals ("#A30", "Table#1", copyTable.TableName);
987                 }
988
989                 [Test]
990                 public void LoadDataException ()
991                 {
992                         DataTable table = new DataTable ();
993                         DataColumn col = new DataColumn ();
994                         col.ColumnName = "Id";
995                         col.DataType = Type.GetType ("System.Int32");
996                         col.DefaultValue = 47;
997                         table.Columns.Add (col);
998                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
999                         table.Constraints.Add (uc);
1000                 
1001                         col = new DataColumn ();
1002                         col.ColumnName = "Name";
1003                         col.DataType = Type.GetType ("System.String");
1004                         col.DefaultValue = "Hello";
1005                         table.Columns.Add (col);
1006                 
1007                         table.BeginLoadData();
1008                         object[] row = {147, "Abc"};
1009                         DataRow newRow = table.LoadDataRow (row, true);
1010                 
1011                         object[] row1 = {147, "Efg"};
1012                         DataRow newRow1 = table.LoadDataRow (row1, true);
1013                                                                                                                              
1014                         object[] row2 = {143, "Hij"};
1015                         DataRow newRow2 = table.LoadDataRow (row2, true);
1016                                                                                                                              
1017                         try {
1018                                 table.EndLoadData ();
1019                                 Fail ("#A01");
1020                         }
1021                         catch (ConstraintException) {
1022                         }
1023                 }
1024                 [Test]
1025                 public void Changes () //To test GetChanges and RejectChanges
1026                 {
1027                         DataTable table = new DataTable ();
1028
1029                         DataColumn col = new DataColumn ();
1030                         col.ColumnName = "Id";
1031                         col.DataType = Type.GetType ("System.Int32");
1032                         table.Columns.Add (col);
1033                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1034                         table.Constraints.Add (uc);
1035                                                                                                                              
1036                         col = new DataColumn ();
1037                         col.ColumnName = "Name";
1038                         col.DataType = Type.GetType ("System.String");
1039                         table.Columns.Add (col);                        
1040
1041                         DataRow row = table.NewRow ();
1042                         row ["Id"] = 147;
1043                         row ["name"] = "Abc";
1044                         table.Rows.Add (row);
1045                         table.AcceptChanges ();
1046                         
1047                         row = table.NewRow ();
1048                         row ["Id"] = 47;
1049                         row ["name"] = "Efg";
1050                         table.Rows.Add (row);
1051
1052                         //Testing GetChanges
1053                         DataTable changesTable = table.GetChanges ();
1054                         AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1055                         AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);                      
1056                         table.AcceptChanges ();
1057                         changesTable = table.GetChanges ();
1058                         try {
1059                                 int cnt = changesTable.Rows.Count;
1060                         }
1061                         catch(Exception e) {
1062                                 if (e.GetType () != typeof (AssertionException))
1063                                         AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1064                                 else
1065                                         Console.WriteLine (e);
1066                         }
1067                         
1068                         //Testing RejectChanges
1069                         row = table.NewRow ();
1070                         row ["Id"] = 247;
1071                         row ["name"] = "Hij";
1072                         table.Rows.Add (row);
1073
1074                         (table.Rows [0])["Name"] = "AaBbCc";
1075                         table.RejectChanges ();
1076                         AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1077                         AssertEquals ("#A04", 2, table.Rows.Count);
1078                 }
1079                 [Test]
1080                 public void ImportRow ()
1081                 {
1082                         DataTable table = new DataTable ();
1083                         DataColumn col = new DataColumn ();
1084                         col.ColumnName = "Id";
1085                         col.DataType = Type.GetType ("System.Int32");
1086                         table.Columns.Add (col);
1087
1088                         col = new DataColumn ();
1089                         col.ColumnName = "Name";
1090                         col.DataType = Type.GetType ("System.String");
1091                         table.Columns.Add (col);
1092                         
1093                         DataRow row = table.NewRow ();
1094                         row ["Id"] = 147;
1095                         row ["name"] = "Abc";
1096                         table.Rows.Add (row);
1097                         table.AcceptChanges ();
1098                                                                                                      
1099                         row = table.NewRow ();
1100                         row ["Id"] = 47;
1101                         row ["name"] = "Efg";
1102                         table.Rows.Add (row);
1103
1104                         (table.Rows [0]) ["Name"] = "AaBbCc";
1105                 
1106                         table.ImportRow (table.Rows [0]);
1107                         table.ImportRow (table.Rows [1]);
1108
1109                         AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
1110                         AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
1111                         AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
1112                         AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
1113                 }
1114
1115                 [Test]
1116                 public void ImportRowDetachedTest ()
1117                 {
1118                         DataTable table = new DataTable ();
1119                         DataColumn col = new DataColumn ();
1120                         col.ColumnName = "Id";
1121                         col.DataType = Type.GetType ("System.Int32");
1122                         table.Columns.Add (col);
1123
1124                         table.PrimaryKey = new DataColumn [] {col};
1125
1126                         col = new DataColumn ();
1127                         col.ColumnName = "Name";
1128                         col.DataType = Type.GetType ("System.String");
1129                         table.Columns.Add (col);
1130                         
1131                         DataRow row = table.NewRow ();
1132                         row ["Id"] = 147;
1133                         row ["name"] = "Abc";
1134
1135                         // keep silent as ms.net ;-), though this is not useful.
1136                         table.ImportRow (row); 
1137                 }
1138
1139                 [Test]
1140                 public void ClearReset () //To test Clear and Reset methods
1141                 {
1142                         DataTable table = new DataTable ("table");
1143                         DataTable table1 = new DataTable ("table1");
1144                 
1145                         DataSet set = new DataSet ();
1146                         set.Tables.Add (table);
1147                         set.Tables.Add (table1);
1148                 
1149                         DataColumn col = new DataColumn ();
1150                         col.ColumnName = "Id";
1151                         col.DataType = Type.GetType ("System.Int32");
1152                         table.Columns.Add (col);
1153                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1154                         table.Constraints.Add (uc);
1155                         table.CaseSensitive = false;
1156                 
1157                         col = new DataColumn ();
1158                         col.ColumnName = "Name";
1159                         col.DataType = Type.GetType ("System.String");
1160                         table.Columns.Add (col);
1161                 
1162                         col = new DataColumn ();
1163                         col.ColumnName = "Id";
1164                         col.DataType = Type.GetType ("System.Int32");
1165                         table1.Columns.Add (col);
1166                 
1167                         col = new DataColumn ();
1168                         col.ColumnName = "Name";
1169                         col.DataType = Type.GetType ("System.String");
1170                         table1.Columns.Add (col);
1171
1172                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1173                         set.Relations.Add (dr);
1174                 
1175                         DataRow row = table.NewRow ();
1176                         row ["Id"] = 147;
1177                         row ["name"] = "Roopa";
1178                         table.Rows.Add (row);
1179                 
1180                         row = table.NewRow ();
1181                         row ["Id"] = 47;
1182                         row ["Name"] = "roopa";
1183                         table.Rows.Add (row);
1184                 
1185                         AssertEquals (2, table.Rows.Count);
1186                         AssertEquals (1, table.ChildRelations.Count);
1187                         try {
1188                                 table.Reset ();
1189                                 Fail ("#A01, should have thrown ArgumentException");
1190                         }
1191                         catch (ArgumentException) {
1192                         }
1193                         AssertEquals ("#CT01", 0, table.Rows.Count);
1194                         AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1195                         AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1196                         AssertEquals ("#CT04", 0, table.Constraints.Count);
1197                         table.Clear ();
1198
1199                         table1.Reset ();
1200                         AssertEquals ("#A05", 0, table1.Rows.Count);
1201                         AssertEquals ("#A06", 0, table1.Constraints.Count);
1202                         AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1203                 
1204                         table.Clear ();
1205                         AssertEquals ("#A08", 0, table.Rows.Count);
1206 #if NET_1_1
1207                         AssertEquals ("#A09", 0, table.Constraints.Count);
1208 #else
1209                         AssertEquals ("#A09", 1, table.Constraints.Count);
1210 #endif
1211                         AssertEquals ("#A10", 0, table.ChildRelations.Count);
1212                 }
1213
1214                 [Test]
1215                 public void Serialize ()
1216                 {
1217                         MemoryStream fs = new MemoryStream ();
1218                         
1219                         // Construct a BinaryFormatter and use it 
1220                         // to serialize the data to the stream.
1221                         BinaryFormatter formatter = new BinaryFormatter();
1222                 
1223                         // Create an array with multiple elements refering to 
1224                         // the one Singleton object.
1225                         DataTable dt = new DataTable();
1226                 
1227                 
1228                         dt.Columns.Add(new DataColumn("Id", typeof(string)));
1229                         dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1230                         dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1231                         dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1232                         dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1233                 
1234                         DataRow loRowToAdd;
1235                         loRowToAdd = dt.NewRow();
1236                         loRowToAdd[0] = "a";
1237                         loRowToAdd[1] = "b";
1238                         loRowToAdd[2] = "c";
1239                         loRowToAdd[3] = "d";
1240                         loRowToAdd[4] = "e";
1241                                                 
1242                         dt.Rows.Add(loRowToAdd);
1243                 
1244                         DataTable[] dtarr = new DataTable[] {dt}; 
1245                 
1246                         // Serialize the array elements.
1247                         formatter.Serialize(fs, dtarr);
1248                 
1249                         // Deserialize the array elements.
1250                         fs.Position = 0;
1251                         DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1252                 
1253                         DataSet ds = new DataSet();
1254                         ds.Tables.Add(a2[0]);
1255                 
1256                         StringWriter sw = new StringWriter ();
1257                         ds.WriteXml(sw);
1258                         XmlDocument doc = new XmlDocument ();
1259                         doc.LoadXml (sw.ToString ());
1260                         AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1261                 }
1262
1263                 [Test]
1264                 [ExpectedException (typeof (DataException))]
1265                 [NUnit.Framework.Category ("NotWorking")]
1266                 public void SetPrimaryKeyAssertsNonNull ()
1267                 {
1268                         DataTable dt = new DataTable ("table");
1269                         dt.Columns.Add ("col1");
1270                         dt.Columns.Add ("col2");
1271                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1272                         dt.Rows.Add (new object [] {1, 3});
1273                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1274
1275                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1276                 }
1277
1278                 [Test]
1279                 [ExpectedException (typeof (NoNullAllowedException))]
1280                 public void PrimaryKeyColumnChecksNonNull ()
1281                 {
1282                         DataTable dt = new DataTable ("table");
1283                         dt.Columns.Add ("col1");
1284                         dt.Columns.Add ("col2");
1285                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1286                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1287                         dt.Rows.Add (new object [] {1, 3});
1288                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1289                 }
1290
1291                 void RowChanging (object o, DataRowChangeEventArgs e)
1292                 {
1293                         AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1294                         rowChangingRowChanging = true;
1295                 }
1296
1297                 void RowChanged (object o, DataRowChangeEventArgs e)
1298                 {
1299                         AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1300                         rowChangingRowChanged = true;
1301                 }
1302
1303                 bool rowChangingRowChanging, rowChangingRowChanged;
1304                 DataRowAction rowChangingExpectedAction;
1305
1306                 [Test]
1307                 public void RowChanging ()
1308                 {
1309                         DataTable dt = new DataTable ("table");
1310                         dt.Columns.Add ("col1");
1311                         dt.Columns.Add ("col2");
1312                         dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1313                         dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1314                         rowChangingExpectedAction = DataRowAction.Add;
1315                         dt.Rows.Add (new object [] {1, 2});
1316                         Assert ("changing,Added", rowChangingRowChanging);
1317                         Assert ("changed,Added", rowChangingRowChanged);
1318                         rowChangingExpectedAction = DataRowAction.Change;
1319                         dt.Rows [0] [0] = 2;
1320                         Assert ("changing,Changed", rowChangingRowChanging);
1321                         Assert ("changed,Changed", rowChangingRowChanged);
1322                 }
1323
1324                  [Test]
1325                 public void CloneSubClassTest()
1326                 {
1327                         MyDataTable dt1 = new MyDataTable();
1328                         MyDataTable dt = (MyDataTable)(dt1.Clone());
1329                         AssertEquals("A#01",2,MyDataTable.count);
1330                 }
1331                                                                                                     
1332         }
1333                                                                                                     
1334                                                                                                     
1335          public  class MyDataTable:DataTable {
1336                                                                                                     
1337              public static int count = 0;
1338                                                                                                     
1339              public MyDataTable() {
1340                                                                                                     
1341                     count++;
1342              }
1343                                                                                                     
1344          }
1345
1346         
1347 }