* DataTable.cs: Fixed LoadDataRow scenarios.
[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 //   Hagit Yidov (hagity@mainsoft.com)
7 // 
8 // (C) Franklin Wise
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using NUnit.Framework;
36 using System;
37 using System.Data;
38 using System.Data.SqlTypes;
39 using System.Globalization;
40 using System.IO;
41 using System.Runtime.Serialization.Formatters.Binary;
42 using System.Xml;
43 using MonoTests.System.Data.Utils;
44 using System.Collections;
45
46 namespace MonoTests.System.Data
47 {
48         [TestFixture]
49         public class DataTableTest :  DataSetAssertion
50         {
51                 string EOL = Environment.NewLine;
52
53                 [Test]
54                 public void Ctor()
55                 {
56                         DataTable dt = new DataTable();
57
58                         AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
59                         Assert("Col",dt.Columns != null);
60                         //Assert(dt.ChildRelations != null);
61                         Assert("Const", dt.Constraints != null);
62                         Assert("ds", dt.DataSet == null); 
63                         Assert("dv", dt.DefaultView != null);
64                         Assert("de", dt.DisplayExpression == "");
65                         Assert("ep", dt.ExtendedProperties != null);
66                         Assert("he", dt.HasErrors == false);
67                         Assert("lc", dt.Locale != null);
68                         Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
69                         Assert("ns", dt.Namespace == "");
70                         //Assert(dt.ParentRelations != null);
71                         Assert("pf", dt.Prefix == "");
72                         Assert("pk", dt.PrimaryKey != null);
73                         Assert("rows", dt.Rows != null);
74                         Assert("Site", dt.Site == null);
75                         Assert("tname", dt.TableName == "");
76                         
77                 }
78
79                 [Test]
80                 public void Select ()
81                 {
82                         DataSet Set = new DataSet ();
83                         DataTable Mom = new DataTable ("Mom");
84                         DataTable Child = new DataTable ("Child");                      
85                         Set.Tables.Add (Mom);
86                         Set.Tables.Add (Child);
87                         
88                         DataColumn Col = new DataColumn ("Name");
89                         DataColumn Col2 = new DataColumn ("ChildName");
90                         Mom.Columns.Add (Col);
91                         Mom.Columns.Add (Col2);
92                         
93                         DataColumn Col3 = new DataColumn ("Name");
94                         DataColumn Col4 = new DataColumn ("Age");
95                         Col4.DataType = Type.GetType ("System.Int16");
96                         Child.Columns.Add (Col3);
97                         Child.Columns.Add (Col4);
98                         
99                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
100                         Set.Relations.Add (Relation);
101
102                         DataRow Row = Mom.NewRow ();
103                         Row [0] = "Laura";
104                         Row [1] = "Nick";
105                         Mom.Rows.Add (Row);
106                         
107                         Row = Mom.NewRow ();
108                         Row [0] = "Laura";
109                         Row [1] = "Dick";
110                         Mom.Rows.Add (Row);
111                         
112                         Row = Mom.NewRow ();
113                         Row [0] = "Laura";
114                         Row [1] = "Mick";
115                         Mom.Rows.Add (Row);
116
117                         Row = Mom.NewRow ();
118                         Row [0] = "Teresa";
119                         Row [1] = "Jack";
120                         Mom.Rows.Add (Row);
121                         
122                         Row = Mom.NewRow ();
123                         Row [0] = "Teresa";
124                         Row [1] = "Mack";
125                         Mom.Rows.Add (Row);
126
127                         Row = Mom.NewRow ();
128                         Row [0] = "'Jhon O'' Collenal'";
129                         Row [1] = "Pack";
130                         Mom.Rows.Add (Row);
131                         
132                         Row = Child.NewRow ();
133                         Row [0] = "Nick";
134                         Row [1] = 15;
135                         Child.Rows.Add (Row);
136                         
137                         Row = Child.NewRow ();
138                         Row [0] = "Dick";
139                         Row [1] = 25;
140                         Child.Rows.Add (Row);
141                         
142                         Row = Child.NewRow ();
143                         Row [0] = "Mick";
144                         Row [1] = 35;
145                         Child.Rows.Add (Row);
146                         
147                         Row = Child.NewRow ();
148                         Row [0] = "Jack";
149                         Row [1] = 10;
150                         Child.Rows.Add (Row);
151                         
152                         Row = Child.NewRow ();
153                         Row [0] = "Mack";
154                         Row [1] = 19;
155                         Child.Rows.Add (Row);
156                 
157                         Row = Child.NewRow ();
158                         Row [0] = "Mack";
159                         Row [1] = 99;
160                         Child.Rows.Add (Row);
161
162                         Row = Child.NewRow ();
163                         Row [0] = "Pack";
164                         Row [1] = 66;
165                         Child.Rows.Add (Row);
166                         
167                         DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
168                         AssertEquals ("test#01", 2, Rows.Length);
169
170                         // test with apos escaped
171                         Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
172                         AssertEquals ("test#01.1", 1, Rows.Length);
173                         
174                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
175                         AssertEquals ("test#02", 0, Rows.Length);
176
177                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
178                         AssertEquals ("test#03", 1, Rows.Length);
179
180                         Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
181                         AssertEquals ("test#04", "Mack", Rows [0] [1]);
182                         
183                         Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
184                         AssertEquals ("test#05", 6, Rows.Length);
185                         
186                         Rows = Child.Select ("age = 20 - 1");
187                         AssertEquals ("test#06", 1, Rows.Length);
188                         
189                         Rows = Child.Select ("age <= 20");
190                         AssertEquals ("test#07", 3, Rows.Length);
191                         
192                         Rows = Child.Select ("age >= 20");
193                         AssertEquals ("test#08", 4, Rows.Length);
194                         
195                         Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
196                         AssertEquals ("test#09", 2, Rows.Length);
197
198                         Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
199                         AssertEquals ("test#10", 1, Rows.Length);
200                         AssertEquals ("test#11", "Mack", Rows [0] [0]);
201                         
202                         Rows = Child.Select ("not (Name = 'Jack')");
203                         AssertEquals ("test#12", 6, Rows.Length);
204                 }
205                 
206                 [Test]
207                 public void Select2 ()
208                 {
209                         DataSet Set = new DataSet ();
210                         DataTable Child = new DataTable ("Child");
211
212                         Set.Tables.Add (Child);
213                                                 
214                         DataColumn Col3 = new DataColumn ("Name");
215                         DataColumn Col4 = new DataColumn ("Age");
216                         Col4.DataType = Type.GetType ("System.Int16");
217                         Child.Columns.Add (Col3);
218                         Child.Columns.Add (Col4);
219                         
220                         DataRow Row = Child.NewRow ();
221                         Row [0] = "Nick";
222                         Row [1] = 15;
223                         Child.Rows.Add (Row);
224                         
225                         Row = Child.NewRow ();
226                         Row [0] = "Dick";
227                         Row [1] = 25;
228                         Child.Rows.Add (Row);
229                         
230                         Row = Child.NewRow ();
231                         Row [0] = "Mick";
232                         Row [1] = 35;
233                         Child.Rows.Add (Row);
234                         
235                         Row = Child.NewRow ();
236                         Row [0] = "Jack";
237                         Row [1] = 10;
238                         Child.Rows.Add (Row);
239                         
240                         Row = Child.NewRow ();
241                         Row [0] = "Mack";
242                         Row [1] = 19;
243                         Child.Rows.Add (Row);
244                 
245                         Row = Child.NewRow ();
246                         Row [0] = "Mack";
247                         Row [1] = 99;
248                         Child.Rows.Add (Row);
249
250                         DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
251                         AssertEquals ("test#01", 3, Rows.Length);
252                         AssertEquals ("test#02", "Mack", Rows [0] [0]);
253                         AssertEquals ("test#03", "Mick", Rows [1] [0]);                 
254                         AssertEquals ("test#04", "Dick", Rows [2] [0]);                 
255                         
256                         Rows = Child.Select ("age >= 20", "age asc");
257                         AssertEquals ("test#05", 3, Rows.Length);
258                         AssertEquals ("test#06", "Dick", Rows [0] [0]);
259                         AssertEquals ("test#07", "Mick", Rows [1] [0]);                 
260                         AssertEquals ("test#08", "Mack", Rows [2] [0]);                 
261                 
262                         Rows = Child.Select ("age >= 20", "name asc");
263                         AssertEquals ("test#09", 3, Rows.Length);
264                         AssertEquals ("test#10", "Dick", Rows [0] [0]);
265                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
266                         AssertEquals ("test#12", "Mick", Rows [2] [0]);                 
267
268                         Rows = Child.Select ("age >= 20", "name desc");
269                         AssertEquals ("test#09", 3, Rows.Length);
270                         AssertEquals ("test#10", "Mick", Rows [0] [0]);
271                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
272                         AssertEquals ("test#12", "Dick", Rows [2] [0]);                 
273
274                 }
275
276                 [Test]
277                 public void SelectParsing ()
278                 {
279                         DataTable T = new DataTable ("test");
280                         DataColumn C = new DataColumn ("name");
281                         T.Columns.Add (C);
282                         C = new DataColumn ("age");
283                         C.DataType = typeof (int);
284                         T.Columns.Add (C);
285                         C = new DataColumn ("id");
286                         T.Columns.Add (C);
287                         
288                         DataSet Set = new DataSet ("TestSet");
289                         Set.Tables.Add (T);
290                         
291                         DataRow Row = null;
292                         for (int i = 0; i < 100; i++) {
293                                 Row = T.NewRow ();
294                                 Row [0] = "human" + i;
295                                 Row [1] = i;
296                                 Row [2] = i;
297                                 T.Rows.Add (Row);
298                         }
299                         
300                         Row = T.NewRow ();
301                         Row [0] = "h*an";
302                         Row [1] = 1;
303                         Row [2] = 1;
304                         T.Rows.Add (Row);
305
306                         AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
307                         
308                         AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
309
310                         try {
311                                 T.Select ("name = 1human ");
312                                 Fail ("test#03");
313                         } catch (Exception e) {
314                                 
315                                 // missing operand after 'human' operand 
316                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());                          
317                         }
318                         
319                         try {                   
320                                 T.Select ("name = 1");
321                                 Fail ("test#05");
322                         } catch (Exception e) {
323                                 
324                                 // Cannot perform '=' operation between string and Int32
325                                 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
326                         }
327                         
328                         AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
329
330                 }
331                 
332                 [Test]
333                 public void SelectEscaping () {
334                         DataTable dt = new DataTable ();
335                         dt.Columns.Add ("SomeCol");
336                         dt.Rows.Add (new object [] {"\t"});
337                         dt.Rows.Add (new object [] {"\\"});
338                         
339                         AssertEquals ("test#01", 1, dt.Select (@"SomeCol='\t'").Length);
340                         AssertEquals ("test#02", 1, dt.Select (@"SomeCol='\\'").Length);
341                         
342                         try {
343                                 dt.Select (@"SomeCol='\x'");
344                                 Fail("test#03");
345                         } catch (SyntaxErrorException) {}
346                 }
347
348                 [Test]
349                 public void SelectOperators ()
350                 {
351                         DataTable T = new DataTable ("test");
352                         DataColumn C = new DataColumn ("name");
353                         T.Columns.Add (C);
354                         C = new DataColumn ("age");
355                         C.DataType = typeof (int);
356                         T.Columns.Add (C);
357                         C = new DataColumn ("id");
358                         T.Columns.Add (C);
359                         
360                         DataSet Set = new DataSet ("TestSet");
361                         Set.Tables.Add (T);
362                         
363                         DataRow Row = null;
364                         for (int i = 0; i < 100; i++) {
365                                 Row = T.NewRow ();
366                                 Row [0] = "human" + i;
367                                 Row [1] = i;
368                                 Row [2] = i;
369                                 T.Rows.Add (Row);
370                         }
371                         
372                         Row = T.NewRow ();
373                         Row [0] = "h*an";
374                         Row [1] = 1;
375                         Row [2] = 1;
376                         T.Rows.Add (Row);
377                         
378                         AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
379                         AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);                    
380                         AssertEquals ("test#03", 12, T.Select ("age< =10").Length);                     
381                         AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
382                         AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);                    
383                         AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
384                         AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
385                         AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
386                         // FIXME: Somebody explain how this can be possible.
387                         // it seems that it is no matter between 10 - 30. The
388                         // result is allways 25 :-P
389                         //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
390                         
391                 }
392
393                 [Test]
394                 public void SelectExceptions ()
395                 {
396                         DataTable T = new DataTable ("test");
397                         DataColumn C = new DataColumn ("name");
398                         T.Columns.Add (C);
399                         C = new DataColumn ("age");
400                         C.DataType = typeof (int);
401                         T.Columns.Add (C);
402                         C = new DataColumn ("id");
403                         T.Columns.Add (C);
404                         
405                         for (int i = 0; i < 100; i++) {
406                                 DataRow Row = T.NewRow ();
407                                 Row [0] = "human" + i;
408                                 Row [1] = i;
409                                 Row [2] = i;
410                                 T.Rows.Add (Row);
411                         }
412                         
413                         try {
414                                 T.Select ("name = human1");
415                                 Fail ("test#01");
416                         } catch (Exception e) {
417                                 
418                                 // column name human not found
419                                 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
420                         }
421                         
422                         AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
423                         AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
424                         
425                         try {
426                                 T.Select ("id = 1k3");
427                                 Fail ("test#06");
428                         } catch (Exception e) {
429                                 
430                                 // no operands after k3 operator
431                                 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
432                         }                                               
433                 }
434                 
435                 [Test]
436                 public void SelectStringOperators ()
437                 {
438                         DataTable T = new DataTable ("test");
439                         DataColumn C = new DataColumn ("name");
440                         T.Columns.Add (C);
441                         C = new DataColumn ("age");
442                         C.DataType = typeof (int);
443                         T.Columns.Add (C);
444                         C = new DataColumn ("id");
445                         T.Columns.Add (C);
446                         
447                         DataSet Set = new DataSet ("TestSet");
448                         Set.Tables.Add (T);
449                         
450                         DataRow Row = null;
451                         for (int i = 0; i < 100; i++) {
452                                 Row = T.NewRow ();
453                                 Row [0] = "human" + i;
454                                 Row [1] = i;
455                                 Row [2] = i;
456                                 T.Rows.Add (Row);
457                         }
458                         Row = T.NewRow ();
459                         Row [0] = "h*an";
460                         Row [1] = 1;
461                         Row [2] = 1;
462                         T.Rows.Add (Row);
463                                         
464                         AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
465                         
466                         AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);                       
467                         AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
468                         AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);                     
469                         AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
470                         AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
471                         
472                         AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
473                         
474                         Set.CaseSensitive = true;
475                         AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
476                         
477                         T.CaseSensitive = false;
478                         AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
479                         
480                         T.CaseSensitive = true;
481                         AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
482                         
483                         Set.CaseSensitive = false;
484                         AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
485                         
486                         T.CaseSensitive = false;
487                         AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
488                         
489                         AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
490                         AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
491                         AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
492                         
493                         try {
494                                 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
495                                 Fail ("test#16");
496                         } catch (Exception e) {
497                                 
498                                 // 'h*an1' is invalid
499                                 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
500                         }
501                         
502                         try {
503                                 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
504                                 Fail ("test#19");
505                         } catch (Exception e) {
506                                 
507                                 // 'h%an1' is invalid
508                                 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
509                         }
510                         
511                         AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
512                         AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
513                         
514                 }
515
516                 [Test]
517                 public void SelectAggregates ()
518                 {
519                         DataTable T = new DataTable ("test");
520                         DataColumn C = new DataColumn ("name");
521                         T.Columns.Add (C);
522                         C = new DataColumn ("age");
523                         C.DataType = typeof (int);
524                         T.Columns.Add (C);
525                         C = new DataColumn ("id");
526                         T.Columns.Add (C);
527                         DataRow Row = null;
528                         
529                         for (int i = 0; i < 1000; i++) {
530                                 Row = T.NewRow ();
531                                 Row [0] = "human" + i;
532                                 Row [1] = i;
533                                 Row [2] = i;
534                                 T.Rows.Add (Row);
535                         }
536                         
537                         AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
538                         AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
539                         AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
540                         AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
541                         AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
542                         AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
543                         AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
544                 }
545                 
546                 [Test]
547                 public void SelectFunctions ()
548                 {
549                         DataTable T = new DataTable ("test");
550                         DataColumn C = new DataColumn ("name");
551                         T.Columns.Add (C);
552                         C = new DataColumn ("age");
553                         C.DataType = typeof (int);
554                         T.Columns.Add (C);
555                         C = new DataColumn ("id");
556                         T.Columns.Add (C);
557                         DataRow Row = null;
558                         
559                         for (int i = 0; i < 1000; i++) {
560                                 Row = T.NewRow ();
561                                 Row [0] = "human" + i;
562                                 Row [1] = i;
563                                 Row [2] = i;
564                                 T.Rows.Add (Row);
565                         }
566                         
567                         Row = T.NewRow ();
568                         Row [0] = "human" + "test";
569                         Row [1] = DBNull.Value;
570                         Row [2] = DBNull.Value;
571                         T.Rows.Add (Row);
572
573                         //TODO: How to test Convert-function
574                         AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);                        
575                         AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
576                         AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
577                         AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);                   
578                         AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);                  
579                         AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
580                         AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
581                         AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
582                         AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
583                         AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
584                         
585                 }
586
587                 [Test]
588                 public void SelectRelations ()
589                 {
590                         DataSet Set = new DataSet ();
591                         DataTable Mom = new DataTable ("Mom");
592                         DataTable Child = new DataTable ("Child");
593
594                         Set.Tables.Add (Mom);
595                         Set.Tables.Add (Child);
596                         
597                         DataColumn Col = new DataColumn ("Name");
598                         DataColumn Col2 = new DataColumn ("ChildName");
599                         Mom.Columns.Add (Col);
600                         Mom.Columns.Add (Col2);
601                         
602                         DataColumn Col3 = new DataColumn ("Name");
603                         DataColumn Col4 = new DataColumn ("Age");
604                         Col4.DataType = Type.GetType ("System.Int16");
605                         Child.Columns.Add (Col3);
606                         Child.Columns.Add (Col4);
607                         
608                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
609                         Set.Relations.Add (Relation);
610
611                         DataRow Row = Mom.NewRow ();
612                         Row [0] = "Laura";
613                         Row [1] = "Nick";
614                         Mom.Rows.Add (Row);
615                         
616                         Row = Mom.NewRow ();
617                         Row [0] = "Laura";
618                         Row [1] = "Dick";
619                         Mom.Rows.Add (Row);
620                         
621                         Row = Mom.NewRow ();
622                         Row [0] = "Laura";
623                         Row [1] = "Mick";
624                         Mom.Rows.Add (Row);
625
626                         Row = Mom.NewRow ();
627                         Row [0] = "Teresa";
628                         Row [1] = "Jack";
629                         Mom.Rows.Add (Row);
630                         
631                         Row = Mom.NewRow ();
632                         Row [0] = "Teresa";
633                         Row [1] = "Mack";
634                         Mom.Rows.Add (Row);
635                         
636                         Row = Child.NewRow ();
637                         Row [0] = "Nick";
638                         Row [1] = 15;
639                         Child.Rows.Add (Row);
640                         
641                         Row = Child.NewRow ();
642                         Row [0] = "Dick";
643                         Row [1] = 25;
644                         Child.Rows.Add (Row);
645                         
646                         Row = Child.NewRow ();
647                         Row [0] = "Mick";
648                         Row [1] = 35;
649                         Child.Rows.Add (Row);
650                         
651                         Row = Child.NewRow ();
652                         Row [0] = "Jack";
653                         Row [1] = 10;
654                         Child.Rows.Add (Row);
655                         
656                         Row = Child.NewRow ();
657                         Row [0] = "Mack";
658                         Row [1] = 19;
659                         Child.Rows.Add (Row);
660                 
661                         Row = Child.NewRow ();
662                         Row [0] = "Mack";
663                         Row [1] = 99;
664                         Child.Rows.Add (Row);
665                         
666                         DataRow [] Rows = Child.Select ("name = Parent.Childname");
667                         AssertEquals ("test#01", 6, Rows.Length);
668                         Rows = Child.Select ("Parent.childname = 'Jack'");
669                         AssertEquals ("test#02", 1, Rows.Length);
670                         
671                         /*
672                         try {
673                                 // FIXME: LAMESPEC: Why the exception is thrown why... why... 
674                                 Mom.Select ("Child.Name = 'Jack'");
675                                 Fail ("test#03");
676                         } catch (Exception e) {
677                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
678                                 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
679                         }
680                         */
681                         
682                         Rows = Child.Select ("Parent.name = 'Laura'");
683                         AssertEquals ("test#06", 3, Rows.Length);
684                         
685                         DataTable Parent2 = new DataTable ("Parent2");
686                         Col = new DataColumn ("Name");
687                         Col2 = new DataColumn ("ChildName");
688
689                         Parent2.Columns.Add (Col);
690                         Parent2.Columns.Add (Col2);
691                         Set.Tables.Add (Parent2);
692                         
693                         Row = Parent2.NewRow ();
694                         Row [0] = "Laura";
695                         Row [1] = "Nick";
696                         Parent2.Rows.Add (Row);
697                         
698                         Row = Parent2.NewRow ();
699                         Row [0] = "Laura";
700                         Row [1] = "Dick";
701                         Parent2.Rows.Add (Row);
702                         
703                         Row = Parent2.NewRow ();
704                         Row [0] = "Laura";
705                         Row [1] = "Mick";
706                         Parent2.Rows.Add (Row);
707
708                         Row = Parent2.NewRow ();
709                         Row [0] = "Teresa";
710                         Row [1] = "Jack";
711                         Parent2.Rows.Add (Row);
712                         
713                         Row = Parent2.NewRow ();
714                         Row [0] = "Teresa";
715                         Row [1] = "Mack";
716                         Parent2.Rows.Add (Row);
717
718                         Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
719                         Set.Relations.Add (Relation);
720                         
721                         try {
722                                 Rows = Child.Select ("Parent.ChildName = 'Jack'");
723                                 Fail ("test#07");
724                         } catch (Exception e) {
725                                 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
726                                 //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);
727                         }
728                         
729                         Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
730                         AssertEquals ("test#10", 1, Rows.Length);
731
732                         Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
733                         AssertEquals ("test#10", 1, Rows.Length);
734                         
735                         try {
736                                 Mom.Select ("Parent.name  = 'John'");
737                         } catch (Exception e) {
738                                 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
739                                 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
740                         }
741                         
742                 }
743
744                 [Test]
745                 public void SelectRowState()
746                 {
747                         DataTable d = new DataTable();
748                         d.Columns.Add (new DataColumn ("aaa"));
749                         DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
750                         AssertEquals(0, rows.Length);
751                         d.Rows.Add (new object [] {"bbb"});
752                         d.Rows.Add (new object [] {"bbb"});
753                         rows = d.Select (null, null, DataViewRowState.Deleted);
754                         AssertEquals(0, rows.Length);
755                 }
756
757                 [Test]
758                 public void ToStringTest()
759                 {
760                         DataTable dt = new DataTable();
761                         dt.Columns.Add("Col1",typeof(int));
762                         
763                         dt.TableName = "Mytable";
764                         dt.DisplayExpression = "Col1";
765                         
766                         
767                         string cmpr = dt.TableName + " + " + dt.DisplayExpression;
768                         AssertEquals(cmpr,dt.ToString());
769                 }
770
771                 [Test]
772                 public void PrimaryKey ()
773                 {
774                         DataTable dt = new DataTable ();
775                         DataColumn Col = new DataColumn ();
776                         Col.AllowDBNull = false;
777                         Col.DataType = typeof (int);
778                         dt.Columns.Add (Col);
779                         dt.Columns.Add ();
780                         dt.Columns.Add ();
781                         dt.Columns.Add ();
782                         
783                         AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
784                         
785                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
786                         AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
787                         AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
788                         
789                         dt.PrimaryKey = null;
790                         AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
791                         
792                         Col = new DataColumn ("failed");
793                         
794                         try {
795                                 dt.PrimaryKey = new DataColumn [] {Col};
796                                 Fail ("test#05");                                       
797                         } catch (Exception e) {
798                                 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
799                                 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
800                         }
801                         
802                         DataTable dt2 = new DataTable ();
803                         dt2.Columns.Add ();
804                         
805                         try {
806                                 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
807                                 Fail ("test#08");
808                         } catch (Exception e) {
809                                 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
810                                 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
811                         }
812                         
813                         
814                         AssertEquals ("test#11", 0, dt.Constraints.Count);
815                         
816                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
817                         AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
818                         AssertEquals ("test#13", 1, dt.Constraints.Count);
819                         AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
820                         AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
821                         AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
822                         
823                 }
824                 
825                 [Test]
826                 public void PropertyExceptions ()
827                 {
828                         DataSet set = new DataSet ();
829                         DataTable table = new DataTable ();
830                         DataTable table1 =  new DataTable ();
831                         set.Tables.Add (table);
832                         set.Tables.Add (table1);
833
834                         DataColumn col = new DataColumn ();
835                         col.ColumnName = "Id";
836                         col.DataType = Type.GetType ("System.Int32");
837                         table.Columns.Add (col);
838                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
839                         table.Constraints.Add (uc);
840                         table.CaseSensitive = false;
841                                                                                                                            
842                         col = new DataColumn ();
843                         col.ColumnName = "Name";
844                         col.DataType = Type.GetType ("System.String");
845                         table.Columns.Add (col);
846                         
847                         col = new DataColumn ();
848                         col.ColumnName = "Id";
849                         col.DataType = Type.GetType ("System.Int32");
850                         table1.Columns.Add (col);
851                         col = new DataColumn ();
852                         col.ColumnName = "Name";
853                         col.DataType = Type.GetType ("System.String");
854                         table1.Columns.Add (col);
855
856                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
857                         set.Relations.Add (dr);
858
859                         try {
860                                 table.CaseSensitive = true;
861                                 table1.CaseSensitive = true;
862                                 Fail ("#A01");
863                         }
864                         catch (Exception e) {
865                                 if (e.GetType () != typeof (AssertionException))
866                                         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);
867                                 else
868                                         Console.WriteLine (e);
869                         }
870                         try {
871                                 CultureInfo cultureInfo = new CultureInfo ("en-gb");
872                                 table.Locale = cultureInfo;
873                                 table1.Locale = cultureInfo;
874                                 Fail ("#A03");
875                         }
876                         catch (Exception e) {
877                                  if (e.GetType () != typeof (AssertionException))
878                                         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);
879                                 else
880                                         Console.WriteLine (e);
881                         }
882                         try {
883                                 table.Prefix = "Prefix#1";
884                                 Fail ("#A05");
885                         }
886                         catch (Exception e){
887                                 if (e.GetType () != typeof (AssertionException))
888                                         AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
889                                 else
890                                         Console.WriteLine (e);
891
892                         }
893                 }
894
895                 [Test]
896                 public void GetErrors ()
897                 {
898                         DataTable table = new DataTable ();
899
900                         DataColumn col = new DataColumn ();
901                         col.ColumnName = "Id";
902                         col.DataType = Type.GetType ("System.Int32");
903                         table.Columns.Add (col);
904                                                                                                                              
905                         col = new DataColumn ();
906                         col.ColumnName = "Name";
907                         col.DataType = Type.GetType ("System.String");
908                         table.Columns.Add (col);
909                         
910                         DataRow row = table.NewRow ();
911                         row ["Id"] = 147;
912                         row ["name"] = "Abc";
913                         row.RowError = "Error#1";
914                         table.Rows.Add (row);
915
916                         AssertEquals ("#A01", 1, table.GetErrors ().Length);
917                         AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
918                 }
919                 [Test]
920                 public void CloneCopyTest ()
921                 {
922                         DataTable table = new DataTable ();
923                         table.TableName = "Table#1";
924                         DataTable table1 = new DataTable ();
925                         table1.TableName = "Table#2";
926                 
927                         table.AcceptChanges ();
928                         
929                         DataSet set = new DataSet ("Data Set#1");
930                         set.DataSetName = "Dataset#1";
931                         set.Tables.Add (table);
932                         set.Tables.Add (table1);
933
934                         DataColumn col = new DataColumn ();
935                         col.ColumnName = "Id";
936                         col.DataType = Type.GetType ("System.Int32");
937                         table.Columns.Add (col);
938                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
939                         table.Constraints.Add (uc);
940                 
941                         col = new DataColumn ();
942                         col.ColumnName = "Id";
943                         col.DataType = Type.GetType ("System.Int32");
944                         table1.Columns.Add (col);
945                                                                                                                              
946                         col = new DataColumn ();
947                         col.ColumnName = "Name";
948                         col.DataType = Type.GetType ("System.String");
949                         table.Columns.Add (col);
950                         
951                         col = new DataColumn ();
952                         col.ColumnName = "Name";
953                         col.DataType = Type.GetType ("System.String");
954                         table1.Columns.Add (col);
955                         DataRow row = table.NewRow ();
956                         row ["Id"] = 147;
957                         row ["name"] = "Abc";
958                         row.RowError = "Error#1";
959                         table.Rows.Add (row);
960                                                                                                                              
961                         row = table.NewRow ();
962                         row ["Id"] = 47;
963                         row ["name"] = "Efg";
964                         table.Rows.Add (row);
965                         table.AcceptChanges ();
966                                                                                                                              
967                         table.CaseSensitive = true;
968                         table1.CaseSensitive = true;
969                         table.MinimumCapacity = 100;
970                         table.Prefix = "PrefixNo:1";
971                         table.Namespace = "Namespace#1";
972                         table.DisplayExpression = "Id / Name + (Id * Id)";
973                         DataColumn[] colArray = {table.Columns[0]};
974                         table.PrimaryKey = colArray;
975                         table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
976 #if NET_1_1 // This prevents further tests after .NET 1.1.
977 #else
978                         CultureInfo cultureInfo = new CultureInfo ("en-gb");
979                         table.Locale = cultureInfo;
980 #endif
981
982                         row = table1.NewRow ();
983                         row ["Name"] = "Abc";
984                         row ["Id"] = 147;
985                         table1.Rows.Add (row);
986
987                         row = table1.NewRow ();
988                         row ["Id"] = 47;
989                         row ["Name"] = "Efg";
990                         table1.Rows.Add (row);
991                 
992                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
993                         set.Relations.Add (dr);
994
995                         //Testing properties of clone
996                         DataTable cloneTable = table.Clone ();
997                         AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
998                         AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
999                         AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
1000                         AssertEquals ("#A04", 2,  cloneTable.Columns.Count);
1001                         AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
1002                         AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
1003                         AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
1004                         AssertEquals ("#A08", false ,cloneTable.HasErrors);
1005 #if NET_1_1
1006 #else
1007                         AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
1008 #endif
1009                         AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
1010                         AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
1011                         AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
1012                         AssertEquals ("#A13", "Id",  cloneTable.PrimaryKey[0].ColumnName);
1013                         AssertEquals ("#A14",0 , cloneTable.Rows.Count );
1014                         AssertEquals ("#A15", "Table#1", cloneTable.TableName);
1015
1016                         //Testing properties of copy
1017                         DataTable copyTable = table.Copy ();
1018                         AssertEquals ("#A16",true ,copyTable.CaseSensitive);
1019                         AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
1020                         AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
1021                         AssertEquals ("#A19", 2,  copyTable.Columns.Count);
1022                         AssertEquals ("#A20", 1, copyTable.Constraints.Count);
1023                         AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
1024                         AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
1025                         AssertEquals ("#A23", true ,copyTable.HasErrors);
1026 #if NET_1_1
1027 #else
1028                         AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
1029 #endif
1030                         AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
1031                         AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
1032                         AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
1033                         AssertEquals ("#A28", "Id",  copyTable.PrimaryKey[0].ColumnName);
1034                         AssertEquals ("#A29", 2 , copyTable.Rows.Count );
1035                         AssertEquals ("#A30", "Table#1", copyTable.TableName);
1036                 }
1037
1038                 [Test]
1039                 public void LoadDataException ()
1040                 {
1041                         DataTable table = new DataTable ();
1042                         DataColumn col = new DataColumn ();
1043                         col.ColumnName = "Id";
1044                         col.DataType = Type.GetType ("System.Int32");
1045                         col.DefaultValue = 47;
1046                         table.Columns.Add (col);
1047                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1048                         table.Constraints.Add (uc);
1049                 
1050                         col = new DataColumn ();
1051                         col.ColumnName = "Name";
1052                         col.DataType = Type.GetType ("System.String");
1053                         col.DefaultValue = "Hello";
1054                         table.Columns.Add (col);
1055                 
1056                         table.BeginLoadData();
1057                         object[] row = {147, "Abc"};
1058                         DataRow newRow = table.LoadDataRow (row, true);
1059                 
1060                         object[] row1 = {147, "Efg"};
1061                         DataRow newRow1 = table.LoadDataRow (row1, true);
1062                                                                                                                              
1063                         object[] row2 = {143, "Hij"};
1064                         DataRow newRow2 = table.LoadDataRow (row2, true);
1065                                                                                                                              
1066                         try {
1067                                 table.EndLoadData ();
1068                                 Fail ("#A01");
1069                         }
1070                         catch (ConstraintException) {
1071                         }
1072                 }
1073                 [Test]
1074                 public void Changes () //To test GetChanges and RejectChanges
1075                 {
1076                         DataTable table = new DataTable ();
1077
1078                         DataColumn col = new DataColumn ();
1079                         col.ColumnName = "Id";
1080                         col.DataType = Type.GetType ("System.Int32");
1081                         table.Columns.Add (col);
1082                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1083                         table.Constraints.Add (uc);
1084                                                                                                                              
1085                         col = new DataColumn ();
1086                         col.ColumnName = "Name";
1087                         col.DataType = Type.GetType ("System.String");
1088                         table.Columns.Add (col);                        
1089
1090                         DataRow row = table.NewRow ();
1091                         row ["Id"] = 147;
1092                         row ["name"] = "Abc";
1093                         table.Rows.Add (row);
1094                         table.AcceptChanges ();
1095                         
1096                         row = table.NewRow ();
1097                         row ["Id"] = 47;
1098                         row ["name"] = "Efg";
1099                         table.Rows.Add (row);
1100
1101                         //Testing GetChanges
1102                         DataTable changesTable = table.GetChanges ();
1103                         AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1104                         AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);                      
1105                         table.AcceptChanges ();
1106                         changesTable = table.GetChanges ();
1107                         try {
1108                                 int cnt = changesTable.Rows.Count;
1109                         }
1110                         catch(Exception e) {
1111                                 if (e.GetType () != typeof (AssertionException))
1112                                         AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1113                                 else
1114                                         Console.WriteLine (e);
1115                         }
1116                         
1117                         //Testing RejectChanges
1118                         row = table.NewRow ();
1119                         row ["Id"] = 247;
1120                         row ["name"] = "Hij";
1121                         table.Rows.Add (row);
1122
1123                         (table.Rows [0])["Name"] = "AaBbCc";
1124                         table.RejectChanges ();
1125                         AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1126                         AssertEquals ("#A04", 2, table.Rows.Count);
1127                 }
1128                 
1129                 [Test]
1130                 public void ImportRowTest ()
1131                 {
1132                         // build source table
1133                         DataTable src = new DataTable ();
1134                         src.Columns.Add ("id", typeof (int));
1135                         src.Columns.Add ("name", typeof (string));
1136
1137                         src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
1138
1139                         src.Rows.Add (new object [] { 1, "mono 1" });
1140                         src.Rows.Add (new object [] { 2, "mono 2" });
1141                         src.Rows.Add (new object [] { 3, "mono 3" });
1142                         src.AcceptChanges ();
1143
1144                         src.Rows [0] [1] = "mono changed 1";  // modify 1st row
1145                         src.Rows [1].Delete ();              // delete 2nd row
1146                         // 3rd row is unchanged
1147                         src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
1148
1149                         // build target table
1150                         DataTable target = new DataTable ();
1151                         target.Columns.Add ("id", typeof (int));
1152                         target.Columns.Add ("name", typeof (string));
1153
1154                         target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
1155
1156                         // import all rows
1157                         target.ImportRow (src.Rows [0]);     // import 1st row
1158                         target.ImportRow (src.Rows [1]);     // import 2nd row
1159                         target.ImportRow (src.Rows [2]);     // import 3rd row
1160                         target.ImportRow (src.Rows [3]);     // import 4th row
1161
1162                         try {
1163                                 target.ImportRow (src.Rows [2]); // import 3rd row again
1164                                 Fail ("#AA1 Should have thrown exception violativ PK");
1165                         } catch (ConstraintException e) {}
1166
1167                         // check row states
1168                         AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
1169                         AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
1170                         AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
1171                         AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
1172
1173                         // check for modified row (1st row)
1174                         AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
1175                         AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
1176                         AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
1177                         AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
1178                         AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
1179
1180                         // check for deleted row (2nd row)
1181                         AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
1182
1183                         // check for unchanged row (3rd row)
1184                         AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
1185                         AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
1186                         AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
1187                         AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
1188
1189                         // check for newly added row (4th row)
1190                         AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
1191                         AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
1192                         AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
1193                 }
1194
1195                 [Test]
1196                 public void ImportRowDetachedTest ()
1197                 {
1198                         DataTable table = new DataTable ();
1199                         DataColumn col = new DataColumn ();
1200                         col.ColumnName = "Id";
1201                         col.DataType = Type.GetType ("System.Int32");
1202                         table.Columns.Add (col);
1203
1204                         table.PrimaryKey = new DataColumn [] {col};
1205
1206                         col = new DataColumn ();
1207                         col.ColumnName = "Name";
1208                         col.DataType = Type.GetType ("System.String");
1209                         table.Columns.Add (col);
1210                         
1211                         DataRow row = table.NewRow ();
1212                         row ["Id"] = 147;
1213                         row ["name"] = "Abc";
1214
1215                         // keep silent as ms.net ;-), though this is not useful.
1216                         table.ImportRow (row);
1217
1218                         //if RowState is detached, then dont import the row.
1219                         AssertEquals ("#1", 0, table.Rows.Count);
1220                 }
1221
1222                 [Test]
1223                 public void ImportRowDeletedTest ()
1224                 {
1225                         DataTable table = new DataTable ();
1226                         table.Columns.Add ("col", typeof (int));
1227                         table.Columns.Add ("col1", typeof (int));
1228
1229                         DataRow row = table.Rows.Add (new object[] {1,2});
1230                         table.PrimaryKey = new DataColumn[] {table.Columns[0]};
1231                         table.AcceptChanges ();
1232
1233                         // If row is in Deleted state, then ImportRow loads the
1234                         // row.
1235                         row.Delete ();
1236                         table.ImportRow (row);
1237                         AssertEquals ("#1", 2, table.Rows.Count);
1238
1239                         // Both the deleted rows shud be now gone
1240                         table.AcceptChanges ();
1241                         AssertEquals ("#2", 0, table.Rows.Count);
1242
1243                         //just add another row
1244                         row = table.Rows.Add (new object[] {1,2});
1245                         // no exception shud be thrown
1246                         table.AcceptChanges ();
1247
1248                         // If row is in Deleted state, then ImportRow loads the
1249                         // row and validate only on RejectChanges
1250                         row.Delete ();
1251                         table.ImportRow (row);
1252                         AssertEquals ("#3", 2, table.Rows.Count);
1253                         AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
1254
1255                         try {
1256                                 table.RejectChanges ();
1257                                 Fail ("#5");
1258                         } catch (ConstraintException e) {
1259                         }
1260                 }
1261
1262                 [Test]
1263                 public void ClearReset () //To test Clear and Reset methods
1264                 {
1265                         DataTable table = new DataTable ("table");
1266                         DataTable table1 = new DataTable ("table1");
1267                 
1268                         DataSet set = new DataSet ();
1269                         set.Tables.Add (table);
1270                         set.Tables.Add (table1);
1271
1272                         table.Columns.Add ("Id", typeof (int));
1273                         table.Columns.Add ("Name", typeof (string));
1274                         table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1275                         table.CaseSensitive = false;
1276                         
1277                         table1.Columns.Add ("Id", typeof (int));
1278                         table1.Columns.Add ("Name", typeof (string));
1279
1280                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1281                         set.Relations.Add (dr);
1282                 
1283                         DataRow row = table.NewRow ();
1284                         row ["Id"] = 147;
1285                         row ["name"] = "Roopa";
1286                         table.Rows.Add (row);
1287                 
1288                         row = table.NewRow ();
1289                         row ["Id"] = 47;
1290                         row ["Name"] = "roopa";
1291                         table.Rows.Add (row);
1292                 
1293                         AssertEquals (2, table.Rows.Count);
1294                         AssertEquals (1, table.ChildRelations.Count);
1295                         try {
1296                                 table.Reset ();
1297                                 Fail ("#A01, should have thrown ArgumentException");
1298                         }
1299                         catch (ArgumentException) {
1300                         }
1301                         AssertEquals ("#CT01", 0, table.Rows.Count);
1302                         AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1303                         AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1304                         AssertEquals ("#CT04", 0, table.Constraints.Count);
1305
1306                         table1.Reset ();
1307                         AssertEquals ("#A05", 0, table1.Rows.Count);
1308                         AssertEquals ("#A06", 0, table1.Constraints.Count);
1309                         AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1310                 
1311                         // clear test
1312                         table.Clear ();
1313                         AssertEquals ("#A08", 0, table.Rows.Count);
1314 #if NET_1_1
1315                         AssertEquals ("#A09", 0, table.Constraints.Count);
1316 #else
1317                         AssertEquals ("#A09", 1, table.Constraints.Count);
1318 #endif
1319                         AssertEquals ("#A10", 0, table.ChildRelations.Count);
1320
1321                 }
1322
1323                 [Test]
1324                 public void ClearTest ()
1325                 {
1326                         DataTable table = new DataTable ("test");
1327                         table.Columns.Add ("id", typeof (int));
1328                         table.Columns.Add ("name", typeof (string));
1329                         
1330                         table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1331                         
1332                         table.Rows.Add (new object [] { 1, "mono 1" });
1333                         table.Rows.Add (new object [] { 2, "mono 2" });
1334                         table.Rows.Add (new object [] { 3, "mono 3" });
1335                         table.Rows.Add (new object [] { 4, "mono 4" });
1336
1337                         table.AcceptChanges ();
1338 #if NET_2_0
1339                         _tableClearedEventFired = false;
1340                         table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1341 #endif // NET_2_0
1342                         
1343                         table.Clear ();
1344 #if NET_2_0
1345                         AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1346 #endif // NET_2_0
1347                         
1348                         DataRow r = table.Rows.Find (1);
1349                         AssertEquals ("#1 should have cleared", true, r == null);
1350
1351                         // try adding new row. indexes should have cleared
1352                         table.Rows.Add (new object [] { 2, "mono 2" });
1353                         AssertEquals ("#2 should add row", 1, table.Rows.Count);
1354                 }
1355 #if NET_2_0
1356                 private bool _tableClearedEventFired = false;
1357                 private void OnTableCleared (object src, DataTableClearEventArgs args)
1358                 {
1359                         _tableClearedEventFired = true;
1360                 }
1361 #endif // NET_2_0
1362                 
1363
1364                 [Test]
1365                 public void Serialize ()
1366                 {
1367                         MemoryStream fs = new MemoryStream ();
1368                         
1369                         // Construct a BinaryFormatter and use it 
1370                         // to serialize the data to the stream.
1371                         BinaryFormatter formatter = new BinaryFormatter();
1372                 
1373                         // Create an array with multiple elements refering to 
1374                         // the one Singleton object.
1375                         DataTable dt = new DataTable();
1376                 
1377                 
1378                         dt.Columns.Add(new DataColumn("Id", typeof(string)));
1379                         dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1380                         dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1381                         dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1382                         dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1383                 
1384                         DataRow loRowToAdd;
1385                         loRowToAdd = dt.NewRow();
1386                         loRowToAdd[0] = "a";
1387                         loRowToAdd[1] = "b";
1388                         loRowToAdd[2] = "c";
1389                         loRowToAdd[3] = "d";
1390                         loRowToAdd[4] = "e";
1391                                                 
1392                         dt.Rows.Add(loRowToAdd);
1393                 
1394                         DataTable[] dtarr = new DataTable[] {dt}; 
1395                 
1396                         // Serialize the array elements.
1397                         formatter.Serialize(fs, dtarr);
1398                 
1399                         // Deserialize the array elements.
1400                         fs.Position = 0;
1401                         DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1402                 
1403                         DataSet ds = new DataSet();
1404                         ds.Tables.Add(a2[0]);
1405                 
1406                         StringWriter sw = new StringWriter ();
1407                         ds.WriteXml(sw);
1408                         XmlDocument doc = new XmlDocument ();
1409                         doc.LoadXml (sw.ToString ());
1410                         AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1411                 }
1412
1413                 [Test]
1414                 [ExpectedException (typeof (DataException))]
1415                 public void SetPrimaryKeyAssertsNonNull ()
1416                 {
1417                         DataTable dt = new DataTable ("table");
1418                         dt.Columns.Add ("col1");
1419                         dt.Columns.Add ("col2");
1420                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1421                         dt.Rows.Add (new object [] {1, 3});
1422                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1423
1424                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1425                 }
1426
1427                 [Test]
1428                 [ExpectedException (typeof (NoNullAllowedException))]
1429                 public void PrimaryKeyColumnChecksNonNull ()
1430                 {
1431                         DataTable dt = new DataTable ("table");
1432                         dt.Columns.Add ("col1");
1433                         dt.Columns.Add ("col2");
1434                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1435                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1436                         dt.Rows.Add (new object [] {1, 3});
1437                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1438                 }
1439
1440                 [Test]
1441                 public void PrimaryKey_CheckSetsAllowDBNull ()
1442                 {
1443                         DataTable table = new DataTable ();
1444                         DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1445                         DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1446         
1447                         AssertEquals ("#1" , true, col1.AllowDBNull);
1448                         AssertEquals ("#2" , true, col2.AllowDBNull);
1449                         AssertEquals ("#3" , false, col2.Unique);
1450                         AssertEquals ("#4" , false, col2.Unique);
1451
1452                         table.PrimaryKey = new DataColumn[] {col1,col2};
1453                         AssertEquals ("#5" , false, col1.AllowDBNull);
1454                         AssertEquals ("#6" , false, col2.AllowDBNull);
1455                         // LAMESPEC or bug ?? 
1456                         AssertEquals ("#7" , false, col1.Unique);
1457                         AssertEquals ("#8" , false, col2.Unique);
1458                 }
1459
1460                 void RowChanging (object o, DataRowChangeEventArgs e)
1461                 {
1462                         AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1463                         rowChangingRowChanging = true;
1464                 }
1465
1466                 void RowChanged (object o, DataRowChangeEventArgs e)
1467                 {
1468                         AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1469                         rowChangingRowChanged = true;
1470                 }
1471
1472                 bool rowChangingRowChanging, rowChangingRowChanged;
1473                 DataRowAction rowChangingExpectedAction;
1474
1475                 [Test]
1476                 public void RowChanging ()
1477                 {
1478                         DataTable dt = new DataTable ("table");
1479                         dt.Columns.Add ("col1");
1480                         dt.Columns.Add ("col2");
1481                         dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1482                         dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1483                         rowChangingExpectedAction = DataRowAction.Add;
1484                         dt.Rows.Add (new object [] {1, 2});
1485                         Assert ("changing,Added", rowChangingRowChanging);
1486                         Assert ("changed,Added", rowChangingRowChanged);
1487                         rowChangingExpectedAction = DataRowAction.Change;
1488                         dt.Rows [0] [0] = 2;
1489                         Assert ("changing,Changed", rowChangingRowChanging);
1490                         Assert ("changed,Changed", rowChangingRowChanged);
1491                 }
1492
1493                  [Test]
1494                 public void CloneSubClassTest()
1495                 {
1496                         MyDataTable dt1 = new MyDataTable();
1497                         MyDataTable dt = (MyDataTable)(dt1.Clone());
1498                         AssertEquals("A#01",2,MyDataTable.count);
1499                 }
1500
1501                 DataRowAction rowActionChanging = DataRowAction.Nothing;
1502                 DataRowAction rowActionChanged  = DataRowAction.Nothing;
1503                 [Test]
1504                 public void AcceptChangesTest ()
1505                 {
1506                         DataTable dt = new DataTable ("test");
1507                         dt.Columns.Add ("id", typeof (int));
1508                         dt.Columns.Add ("name", typeof (string));
1509                         
1510                         dt.Rows.Add (new object [] { 1, "mono 1" });
1511
1512                         dt.RowChanged  += new DataRowChangeEventHandler (OnRowChanged);
1513                         dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1514
1515                         try {
1516                                 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1517                                 dt.AcceptChanges ();
1518
1519                                 AssertEquals ("#1 should have fired event and set action to commit",
1520                                               DataRowAction.Commit, rowActionChanging);
1521                                 AssertEquals ("#2 should have fired event and set action to commit",
1522                                               DataRowAction.Commit, rowActionChanged);
1523
1524                         } finally {
1525                                 dt.RowChanged  -= new DataRowChangeEventHandler (OnRowChanged);
1526                                 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1527
1528                         }
1529                 }
1530
1531                                 [Test]
1532                                 public void ColumnObjectTypeTest() {
1533                                         DataTable dt = new DataTable();
1534                                         dt.Columns.Add("Series Label", typeof(SqlInt32));
1535                                         dt.Rows.Add(new object[] {"sss"});
1536                                         AssertEquals(1, dt.Rows.Count);
1537                                 }
1538
1539                 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1540                 {
1541                         rowActionChanging = args.Action;
1542                 }
1543                 
1544                 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1545                 {
1546                         rowActionChanged = args.Action;
1547                 }
1548
1549
1550 #if NET_2_0
1551                 private DataTable dt;
1552                 private void localSetup () {
1553                         dt = new DataTable ("test");
1554                         dt.Columns.Add ("id", typeof (int));
1555                         dt.Columns.Add ("name", typeof (string));
1556                         dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };
1557
1558                         dt.Rows.Add (new object[] { 1, "mono 1" });
1559                         dt.Rows.Add (new object[] { 2, "mono 2" });
1560                         dt.Rows.Add (new object[] { 3, "mono 3" });
1561
1562                         dt.AcceptChanges ();
1563                 }
1564
1565                 #region DataTable.CreateDataReader Tests
1566
1567                 [Test]
1568                 public void CreateDataReader1 () {
1569                         localSetup ();
1570                         DataTableReader dtr = dt.CreateDataReader ();
1571                         Assert ("HasRows", dtr.HasRows);
1572                         AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1573                         int ri = 0;
1574                         while (dtr.Read ()) {
1575                                 for (int i = 0; i < dtr.FieldCount; i++) {
1576                                         AssertEquals ("RowData-" + ri + "-" + i, dt.Rows[ri][i],
1577                                                 dtr[i]);
1578                                 }
1579                                 ri++;
1580                         }
1581                 }
1582
1583                 [Test]
1584                 public void CreateDataReader2 () {
1585                         localSetup ();
1586                         DataTableReader dtr = dt.CreateDataReader ();
1587                         Assert ("HasRows", dtr.HasRows);
1588                         AssertEquals ("CountCols", dt.Columns.Count, dtr.FieldCount);
1589                         dtr.Read ();
1590                         AssertEquals ("RowData0-0", 1, dtr[0]);
1591                         AssertEquals ("RowData0-1", "mono 1", dtr[1]);
1592                         dtr.Read ();
1593                         AssertEquals ("RowData1-0", 2, dtr[0]);
1594                         AssertEquals ("RowData1-1", "mono 2", dtr[1]);
1595                         dtr.Read ();
1596                         AssertEquals ("RowData2-0", 3, dtr[0]);
1597                         AssertEquals ("RowData2-1", "mono 3", dtr[1]);
1598                 }
1599
1600                 #endregion // DataTable.CreateDataReader Tests
1601
1602                 #region DataTable.Load Tests
1603
1604                 [Test]
1605                 public void Load_Basic () {
1606                         localSetup ();
1607                         DataTable dtLoad = new DataTable ("LoadBasic");
1608                         dtLoad.Columns.Add ("id", typeof (int));
1609                         dtLoad.Columns.Add ("name", typeof (string));
1610                         dtLoad.Columns["id"].ReadOnly = true;
1611                         dtLoad.Columns["name"].ReadOnly = true;
1612                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1613                         dtLoad.Rows.Add (new object[] { 1, "load 1" });
1614                         dtLoad.Rows.Add (new object[] { 2, "load 2" });
1615                         dtLoad.Rows.Add (new object[] { 3, "load 3" });
1616                         dtLoad.AcceptChanges ();
1617                         DataTableReader dtr = dt.CreateDataReader ();
1618                         dtLoad.Load (dtr);
1619                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1620                         AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1621                         AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1622                         AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1623                         AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1624                         AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1625                         AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1626                         AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1627                 }
1628
1629                 [Test]
1630                 public void Load_NoSchema () {
1631                         localSetup ();
1632                         DataTable dtLoad = new DataTable ("LoadNoSchema");
1633                         DataTableReader dtr = dt.CreateDataReader ();
1634                         dtLoad.Load (dtr);
1635                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1636                         AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1637                         AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1638                         AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1639                         AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1640                         AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1641                         AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1642                         AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1643                 }
1644
1645                 internal struct fillErrorStruct {
1646                         internal string error;
1647                         internal string tableName;
1648                         internal int rowKey;
1649                         internal bool contFlag;
1650                         internal void init (string tbl, int row, bool cont, string err) {
1651                                 tableName = tbl;
1652                                 rowKey = row;
1653                                 contFlag = cont;
1654                                 error = err;
1655                         }
1656                 }
1657                 private fillErrorStruct[] fillErr = new fillErrorStruct[3];
1658                 private int fillErrCounter;
1659                 private void fillErrorHandler (object sender, FillErrorEventArgs e) {
1660                         e.Continue = fillErr[fillErrCounter].contFlag;
1661                         AssertEquals ("fillErr-T", fillErr[fillErrCounter].tableName, e.DataTable.TableName);
1662                         AssertEquals ("fillErr-R", fillErr[fillErrCounter].rowKey, e.Values[0]);
1663                         AssertEquals ("fillErr-C", fillErr[fillErrCounter].contFlag, e.Continue);
1664                         AssertEquals ("fillErr-E", fillErr[fillErrCounter].error, e.Errors.Message);
1665                         fillErrCounter++;
1666                 }
1667
1668                 [Test]
1669                 [ExpectedException (typeof (ArgumentException))]
1670                 public void Load_Incompatible () {
1671                         localSetup ();
1672                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1673                         dtLoad.Columns.Add ("name", typeof (double));
1674                         DataTableReader dtr = dt.CreateDataReader ();
1675                         dtLoad.Load (dtr);
1676                 }
1677                 [Test]
1678                 // Load doesn't have a third overload in System.Data
1679                 // and is commented-out below
1680                 public void Load_IncompatibleEHandlerT () {
1681                         fillErrCounter = 0;
1682                         fillErr[0].init ("LoadIncompatible", 1, true,
1683                                 "Input string was not in a correct format.Couldn't store <mono 1> in name Column.  Expected type is Double.");
1684                         fillErr[1].init ("LoadIncompatible", 2, true,
1685                                 "Input string was not in a correct format.Couldn't store <mono 2> in name Column.  Expected type is Double.");
1686                         fillErr[2].init ("LoadIncompatible", 3, true,
1687                                 "Input string was not in a correct format.Couldn't store <mono 3> in name Column.  Expected type is Double.");
1688                         localSetup ();
1689                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1690                         dtLoad.Columns.Add ("name", typeof (double));
1691                         DataTableReader dtr = dt.CreateDataReader ();
1692                         //dtLoad.Load (dtr,LoadOption.PreserveChanges,fillErrorHandler);
1693                 }
1694                 [Test]
1695                 [Category ("NotWorking")]
1696                 // Load doesn't have a third overload in System.Data
1697                 // and is commented-out below
1698                 [ExpectedException (typeof (ArgumentException))]
1699                 public void Load_IncompatibleEHandlerF () {
1700                         fillErrCounter = 0;
1701                         fillErr[0].init ("LoadIncompatible", 1, false,
1702                                 "Input string was not in a correct format.Couldn't store <mono 1> in name Column.  Expected type is Double.");
1703                         localSetup ();
1704                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1705                         dtLoad.Columns.Add ("name", typeof (double));
1706                         DataTableReader dtr = dt.CreateDataReader ();
1707                         //dtLoad.Load (dtr, LoadOption.PreserveChanges, fillErrorHandler);
1708                 }
1709
1710                 [Test]
1711                 public void Load_ExtraColsEqualVal () {
1712                         localSetup ();
1713                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1714                         dtLoad.Columns.Add ("id", typeof (int));
1715                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1716                         dtLoad.Rows.Add (new object[] { 1 });
1717                         dtLoad.Rows.Add (new object[] { 2 });
1718                         dtLoad.Rows.Add (new object[] { 3 });
1719                         dtLoad.AcceptChanges ();
1720                         DataTableReader dtr = dt.CreateDataReader ();
1721                         dtLoad.Load (dtr);
1722                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1723                         AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1724                         AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1725                         AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1726                         AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1727                         AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1728                         AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1729                         AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1730                 }
1731
1732                 [Test]
1733                 public void Load_ExtraColsNonEqualVal () {
1734                         localSetup ();
1735                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1736                         dtLoad.Columns.Add ("id", typeof (int));
1737                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1738                         dtLoad.Rows.Add (new object[] { 4 });
1739                         dtLoad.Rows.Add (new object[] { 5 });
1740                         dtLoad.Rows.Add (new object[] { 6 });
1741                         dtLoad.AcceptChanges ();
1742                         DataTableReader dtr = dt.CreateDataReader ();
1743                         dtLoad.Load (dtr);
1744                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1745                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1746                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1747                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1748                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1749                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1750                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1751                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1752                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1753                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1754                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1755                 }
1756
1757                 [Test]
1758                 [ExpectedException (typeof (ConstraintException))]
1759                 public void Load_MissingColsNonNullable () {
1760                         localSetup ();
1761                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1762                         dtLoad.Columns.Add ("id", typeof (int));
1763                         dtLoad.Columns.Add ("name", typeof (string));
1764                         dtLoad.Columns.Add ("missing", typeof (string));
1765                         dtLoad.Columns["missing"].AllowDBNull = false;
1766                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1767                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1768                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1769                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1770                         dtLoad.AcceptChanges ();
1771                         DataTableReader dtr = dt.CreateDataReader ();
1772                         dtLoad.Load (dtr);
1773                 }
1774
1775                 [Test]
1776                 public void Load_MissingColsDefault () {
1777                         localSetup ();
1778                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1779                         dtLoad.Columns.Add ("id", typeof (int));
1780                         dtLoad.Columns.Add ("name", typeof (string));
1781                         dtLoad.Columns.Add ("missing", typeof (string));
1782                         dtLoad.Columns["missing"].AllowDBNull = false;
1783                         dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1784                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1785                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1786                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1787                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1788                         dtLoad.AcceptChanges ();
1789                         DataTableReader dtr = dt.CreateDataReader ();
1790                         dtLoad.Load (dtr);
1791                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1792                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1793                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1794                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1795                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1796                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1797                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1798                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1799                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1800                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1801                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1802                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1803                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1804                         AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1805                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1806                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1807                         AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1808                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1809                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1810                         AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1811                 }
1812
1813                 [Test]
1814                 public void Load_MissingColsNullable () {
1815                         localSetup ();
1816                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1817                         dtLoad.Columns.Add ("id", typeof (int));
1818                         dtLoad.Columns.Add ("name", typeof (string));
1819                         dtLoad.Columns.Add ("missing", typeof (string));
1820                         dtLoad.Columns["missing"].AllowDBNull = true;
1821                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1822                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1823                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1824                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1825                         dtLoad.AcceptChanges ();
1826                         DataTableReader dtr = dt.CreateDataReader ();
1827                         dtLoad.Load (dtr);
1828                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1829                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1830                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1831                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1832                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1833                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1834                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1835                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1836                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1837                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1838                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1839                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1840                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1841                         //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1842                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1843                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1844                         //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1845                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1846                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1847                         //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1848                 }
1849
1850                 private DataTable setupRowState () {
1851                         DataTable tbl = new DataTable ("LoadRowStateChanges");
1852                         tbl.RowChanged += new DataRowChangeEventHandler (dtLoad_RowChanged);
1853                         tbl.RowChanging += new DataRowChangeEventHandler (dtLoad_RowChanging);
1854                         tbl.Columns.Add ("id", typeof (int));
1855                         tbl.Columns.Add ("name", typeof (string));
1856                         tbl.PrimaryKey = new DataColumn[] { tbl.Columns["id"] };
1857                         tbl.Rows.Add (new object[] { 1, "RowState 1" });
1858                         tbl.Rows.Add (new object[] { 2, "RowState 2" });
1859                         tbl.Rows.Add (new object[] { 3, "RowState 3" });
1860                         tbl.AcceptChanges ();
1861                         // Update Table with following changes: Row0 unmodified, 
1862                         // Row1 modified, Row2 deleted, Row3 added, Row4 not-present.
1863                         tbl.Rows[1]["name"] = "Modify 2";
1864                         tbl.Rows[2].Delete ();
1865                         DataRow row = tbl.NewRow ();
1866                         row["id"] = 4;
1867                         row["name"] = "Add 4";
1868                         tbl.Rows.Add (row);
1869                         return (tbl);
1870                 }
1871
1872                 private DataRowAction[] rowChangeAction = new DataRowAction[5];
1873                 private bool checkAction = false;
1874                 private int rowChagedCounter, rowChangingCounter;
1875                 private void rowActionInit (DataRowAction[] act) {
1876                         checkAction = true;
1877                         rowChagedCounter = 0;
1878                         rowChangingCounter = 0;
1879                         for (int i = 0; i < 5; i++)
1880                                 rowChangeAction[i] = act[i];
1881                 }
1882                 private void rowActionEnd () {
1883                         checkAction = false;
1884                 }
1885                 private void dtLoad_RowChanged (object sender, DataRowChangeEventArgs e) {
1886                         if (checkAction) {
1887                                 AssertEquals ("RowChanged" + rowChagedCounter,
1888                                         rowChangeAction[rowChagedCounter], e.Action);
1889                                 rowChagedCounter++;
1890                         }
1891                 }
1892                 private void dtLoad_RowChanging (object sender, DataRowChangeEventArgs e) {
1893                         if (checkAction) {
1894                                 AssertEquals ("RowChanging" + rowChangingCounter,
1895                                         rowChangeAction[rowChangingCounter], e.Action);
1896                                 rowChangingCounter++;
1897                         }
1898                 }
1899
1900                 [Test]
1901                 public void Load_RowStateChangesDefault () {
1902                         localSetup ();
1903                         dt.Rows.Add (new object[] { 4, "mono 4" });
1904                         dt.Rows.Add (new object[] { 5, "mono 5" });
1905                         dt.AcceptChanges ();
1906                         DataTableReader dtr = dt.CreateDataReader ();
1907                         DataTable dtLoad = setupRowState ();
1908                         DataRowAction[] dra = new DataRowAction[] {
1909                                 DataRowAction.ChangeCurrentAndOriginal,
1910                                 DataRowAction.ChangeOriginal,
1911                                 DataRowAction.ChangeOriginal,
1912                                 DataRowAction.ChangeOriginal,
1913                                 DataRowAction.ChangeCurrentAndOriginal};
1914                         rowActionInit (dra);
1915                         dtLoad.Load (dtr);
1916                         rowActionEnd ();
1917                         // asserting Unchanged Row0
1918                         AssertEquals ("RowData0-C", "mono 1",
1919                                 dtLoad.Rows[0][1,DataRowVersion.Current]);
1920                         AssertEquals ("RowData0-O", "mono 1",
1921                                 dtLoad.Rows[0][1,DataRowVersion.Original]);
1922                         AssertEquals ("RowState0", DataRowState.Unchanged,
1923                                 dtLoad.Rows[0].RowState);
1924                         // asserting Modified Row1
1925                         AssertEquals ("RowData1-C", "Modify 2",
1926                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
1927                         AssertEquals ("RowData1-O", "mono 2",
1928                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
1929                         AssertEquals ("RowState1", DataRowState.Modified,
1930                                 dtLoad.Rows[1].RowState);
1931                         // asserting Deleted Row2
1932                         AssertEquals ("RowData1-O", "mono 3",
1933                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
1934                         AssertEquals ("RowState2", DataRowState.Deleted,
1935                                 dtLoad.Rows[2].RowState);
1936                         // asserting Added Row3
1937                         AssertEquals ("RowData3-C", "Add 4",
1938                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
1939                         AssertEquals ("RowData3-O", "mono 4",
1940                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
1941                         AssertEquals ("RowState3", DataRowState.Modified,
1942                                 dtLoad.Rows[3].RowState);
1943                         // asserting Unpresent Row4
1944                         AssertEquals ("RowData4-C", "mono 5",
1945                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
1946                         AssertEquals ("RowData4-O", "mono 5",
1947                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
1948                         AssertEquals ("RowState4", DataRowState.Unchanged,
1949                                 dtLoad.Rows[4].RowState);
1950                 }
1951
1952                 [Test]
1953                 [ExpectedException (typeof (VersionNotFoundException))]
1954                 public void Load_RowStateChangesDefaultDelete () {
1955                         localSetup ();
1956                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1957                         dtLoad.Columns.Add ("id", typeof (int));
1958                         dtLoad.Columns.Add ("name", typeof (string));
1959                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1960                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1961                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1962                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1963                         dtLoad.AcceptChanges ();
1964                         dtLoad.Rows[2].Delete ();
1965                         DataTableReader dtr = dt.CreateDataReader ();
1966                         dtLoad.Load (dtr);
1967                         AssertEquals ("RowData2-C", " ",
1968                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
1969                 }
1970
1971                 [Test]
1972                 public void Load_RowStatePreserveChanges () {
1973                         localSetup ();
1974                         dt.Rows.Add (new object[] { 4, "mono 4" });
1975                         dt.Rows.Add (new object[] { 5, "mono 5" });
1976                         dt.AcceptChanges ();
1977                         DataTableReader dtr = dt.CreateDataReader ();
1978                         DataTable dtLoad = setupRowState ();
1979                         DataRowAction[] dra = new DataRowAction[] {
1980                                 DataRowAction.ChangeCurrentAndOriginal,
1981                                 DataRowAction.ChangeOriginal,
1982                                 DataRowAction.ChangeOriginal,
1983                                 DataRowAction.ChangeOriginal,
1984                                 DataRowAction.ChangeCurrentAndOriginal};
1985                         rowActionInit (dra);
1986                         dtLoad.Load (dtr, LoadOption.PreserveChanges);
1987                         rowActionEnd ();
1988                         // asserting Unchanged Row0
1989                         AssertEquals ("RowData0-C", "mono 1",
1990                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
1991                         AssertEquals ("RowData0-O", "mono 1",
1992                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
1993                         AssertEquals ("RowState0", DataRowState.Unchanged,
1994                                 dtLoad.Rows[0].RowState);
1995                         // asserting Modified Row1
1996                         AssertEquals ("RowData1-C", "Modify 2",
1997                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
1998                         AssertEquals ("RowData1-O", "mono 2",
1999                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2000                         AssertEquals ("RowState1", DataRowState.Modified,
2001                                 dtLoad.Rows[1].RowState);
2002                         // asserting Deleted Row2
2003                         AssertEquals ("RowData1-O", "mono 3",
2004                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2005                         AssertEquals ("RowState2", DataRowState.Deleted,
2006                                 dtLoad.Rows[2].RowState);
2007                         // asserting Added Row3
2008                         AssertEquals ("RowData3-C", "Add 4",
2009                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2010                         AssertEquals ("RowData3-O", "mono 4",
2011                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2012                         AssertEquals ("RowState3", DataRowState.Modified,
2013                                 dtLoad.Rows[3].RowState);
2014                         // asserting Unpresent Row4
2015                         AssertEquals ("RowData4-C", "mono 5",
2016                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2017                         AssertEquals ("RowData4-O", "mono 5",
2018                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
2019                         AssertEquals ("RowState4", DataRowState.Unchanged,
2020                                 dtLoad.Rows[4].RowState);
2021                 }
2022
2023                 [Test]
2024                 [ExpectedException (typeof (VersionNotFoundException))]
2025                 public void Load_RowStatePreserveChangesDelete () {
2026                         localSetup ();
2027                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2028                         dtLoad.Columns.Add ("id", typeof (int));
2029                         dtLoad.Columns.Add ("name", typeof (string));
2030                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2031                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2032                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2033                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2034                         dtLoad.AcceptChanges ();
2035                         dtLoad.Rows[2].Delete ();
2036                         DataTableReader dtr = dt.CreateDataReader ();
2037                         dtLoad.Load (dtr,LoadOption.PreserveChanges);
2038                         AssertEquals ("RowData2-C", " ",
2039                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2040                 }
2041
2042                 [Test]
2043                 public void Load_RowStateOverwriteChanges () {
2044                         localSetup ();
2045                         dt.Rows.Add (new object[] { 4, "mono 4" });
2046                         dt.Rows.Add (new object[] { 5, "mono 5" });
2047                         dt.AcceptChanges ();
2048                         DataTableReader dtr = dt.CreateDataReader ();
2049                         DataTable dtLoad = setupRowState ();
2050                         DataRowAction[] dra = new DataRowAction[] {
2051                                 DataRowAction.ChangeCurrentAndOriginal,
2052                                 DataRowAction.ChangeCurrentAndOriginal,
2053                                 DataRowAction.ChangeCurrentAndOriginal,
2054                                 DataRowAction.ChangeCurrentAndOriginal,
2055                                 DataRowAction.ChangeCurrentAndOriginal};
2056                         rowActionInit (dra);
2057                         dtLoad.Load (dtr, LoadOption.OverwriteChanges);
2058                         rowActionEnd ();
2059                         // asserting Unchanged Row0
2060                         AssertEquals ("RowData0-C", "mono 1",
2061                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2062                         AssertEquals ("RowData0-O", "mono 1",
2063                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2064                         AssertEquals ("RowState0", DataRowState.Unchanged,
2065                                 dtLoad.Rows[0].RowState);
2066                         // asserting Modified Row1
2067                         AssertEquals ("RowData1-C", "mono 2",
2068                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
2069                         AssertEquals ("RowData1-O", "mono 2",
2070                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2071                         AssertEquals ("RowState1", DataRowState.Unchanged,
2072                                 dtLoad.Rows[1].RowState);
2073                         // asserting Deleted Row2
2074                         AssertEquals ("RowData1-C", "mono 3",
2075                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2076                         AssertEquals ("RowData1-O", "mono 3",
2077                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2078                         AssertEquals ("RowState2", DataRowState.Unchanged,
2079                                 dtLoad.Rows[2].RowState);
2080                         // asserting Added Row3
2081                         AssertEquals ("RowData3-C", "mono 4",
2082                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2083                         AssertEquals ("RowData3-O", "mono 4",
2084                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2085                         AssertEquals ("RowState3", DataRowState.Unchanged,
2086                                 dtLoad.Rows[3].RowState);
2087                         // asserting Unpresent Row4
2088                         AssertEquals ("RowData4-C", "mono 5",
2089                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2090                         AssertEquals ("RowData4-O", "mono 5",
2091                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
2092                         AssertEquals ("RowState4", DataRowState.Unchanged,
2093                                 dtLoad.Rows[4].RowState);
2094                 }
2095
2096                 [Test]
2097                 public void Load_RowStateUpsert () {
2098                         localSetup ();
2099                         dt.Rows.Add (new object[] { 4, "mono 4" });
2100                         dt.Rows.Add (new object[] { 5, "mono 5" });
2101                         dt.AcceptChanges ();
2102                         DataTableReader dtr = dt.CreateDataReader ();
2103                         DataTable dtLoad = setupRowState ();
2104                         // Notice rowChange-Actions only occur 5 times, as number 
2105                         // of actual rows, ignoring row duplication of the deleted row.
2106                         DataRowAction[] dra = new DataRowAction[] {
2107                                 DataRowAction.Change,
2108                                 DataRowAction.Change,
2109                                 DataRowAction.Add,
2110                                 DataRowAction.Change,
2111                                 DataRowAction.Add};
2112                         rowActionInit (dra);
2113                         dtLoad.Load (dtr, LoadOption.Upsert);
2114                         rowActionEnd ();
2115                         // asserting Unchanged Row0
2116                         AssertEquals ("RowData0-C", "mono 1",
2117                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2118                         AssertEquals ("RowData0-O", "RowState 1",
2119                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2120                         AssertEquals ("RowState0", DataRowState.Modified,
2121                                 dtLoad.Rows[0].RowState);
2122                         // asserting Modified Row1
2123                         AssertEquals ("RowData1-C", "mono 2",
2124                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
2125                         AssertEquals ("RowData1-O", "RowState 2",
2126                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2127                         AssertEquals ("RowState1", DataRowState.Modified,
2128                                 dtLoad.Rows[1].RowState);
2129                         // asserting Deleted Row2 and "Deleted-Added" Row4
2130                         AssertEquals ("RowData2-O", "RowState 3",
2131                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2132                         AssertEquals ("RowState2", DataRowState.Deleted,
2133                                 dtLoad.Rows[2].RowState);
2134                         AssertEquals ("RowData4-C", "mono 3",
2135                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2136                         AssertEquals ("RowState4", DataRowState.Added,
2137                                 dtLoad.Rows[4].RowState);
2138                         // asserting Added Row3
2139                         AssertEquals ("RowData3-C", "mono 4",
2140                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2141                         AssertEquals ("RowState3", DataRowState.Added,
2142                                 dtLoad.Rows[3].RowState);
2143                         // asserting Unpresent Row5
2144                         // Notice row4 is used for added row of deleted row2 and so
2145                         // unpresent row4 moves to row5
2146                         AssertEquals ("RowData5-C", "mono 5",
2147                                 dtLoad.Rows[5][1, DataRowVersion.Current]);
2148                         AssertEquals ("RowState5", DataRowState.Added,
2149                                 dtLoad.Rows[5].RowState);
2150                 }
2151
2152                 [Test]
2153                 public void Load_RowStateUpsertDuplicateKey1 () {
2154                         localSetup ();
2155                         dt.Rows.Add (new object[] { 4, "mono 4" });
2156                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2157                         dtLoad.Columns.Add ("id", typeof (int));
2158                         dtLoad.Columns.Add ("name", typeof (string));
2159                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2160                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2161                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2162                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2163                         dtLoad.AcceptChanges ();
2164                         dtLoad.Rows[2].Delete ();
2165                         DataTableReader dtr = dt.CreateDataReader ();
2166                         dtLoad.Load (dtr, LoadOption.Upsert);
2167                         dtLoad.Rows[3][1] = "NEWVAL";
2168                         AssertEquals ("A-RowState2", DataRowState.Deleted,
2169                                 dtLoad.Rows[2].RowState);
2170                         AssertEquals ("A-RowData2-id", 3,
2171                                 dtLoad.Rows[2][0, DataRowVersion.Original]);
2172                         AssertEquals ("A-RowData2-name", "RowState 3",
2173                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2174                         AssertEquals ("A-RowState3", DataRowState.Added,
2175                                 dtLoad.Rows[3].RowState);
2176                         AssertEquals ("A-RowData3-id", 3,
2177                                 dtLoad.Rows[3][0, DataRowVersion.Current]);
2178                         AssertEquals ("A-RowData3-name", "NEWVAL",
2179                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2180                         AssertEquals ("A-RowState4", DataRowState.Added,
2181                                 dtLoad.Rows[4].RowState);
2182                         AssertEquals ("A-RowData4-id", 4,
2183                                 dtLoad.Rows[4][0, DataRowVersion.Current]);
2184                         AssertEquals ("A-RowData4-name", "mono 4",
2185                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2186
2187                         dtLoad.AcceptChanges ();
2188
2189                         AssertEquals ("B-RowState2", DataRowState.Unchanged,
2190                                 dtLoad.Rows[2].RowState);
2191                         AssertEquals ("B-RowData2-id", 3,
2192                                 dtLoad.Rows[2][0, DataRowVersion.Current]);
2193                         AssertEquals ("B-RowData2-name", "NEWVAL",
2194                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2195                         AssertEquals ("B-RowState3", DataRowState.Unchanged,
2196                                 dtLoad.Rows[3].RowState);
2197                         AssertEquals ("B-RowData3-id", 4,
2198                                 dtLoad.Rows[3][0, DataRowVersion.Current]);
2199                         AssertEquals ("B-RowData3-name", "mono 4",
2200                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2201                 }
2202
2203                 [Test]
2204                 [ExpectedException (typeof (IndexOutOfRangeException))]
2205                 public void Load_RowStateUpsertDuplicateKey2 () {
2206                         localSetup ();
2207                         dt.Rows.Add (new object[] { 4, "mono 4" });
2208                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2209                         dtLoad.Columns.Add ("id", typeof (int));
2210                         dtLoad.Columns.Add ("name", typeof (string));
2211                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2212                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2213                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2214                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2215                         dtLoad.AcceptChanges ();
2216                         dtLoad.Rows[2].Delete ();
2217                         DataTableReader dtr = dt.CreateDataReader ();
2218                         dtLoad.Load (dtr, LoadOption.Upsert);
2219                         dtLoad.AcceptChanges ();
2220                         AssertEquals ("RowData4", " ", dtLoad.Rows[4][1]);
2221                 }
2222
2223                 [Test]
2224                 [ExpectedException (typeof (VersionNotFoundException))]
2225                 public void Load_RowStateUpsertDelete1 () {
2226                         localSetup ();
2227                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2228                         dtLoad.Columns.Add ("id", typeof (int));
2229                         dtLoad.Columns.Add ("name", typeof (string));
2230                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2231                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2232                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2233                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2234                         dtLoad.AcceptChanges ();
2235                         dtLoad.Rows[2].Delete ();
2236                         DataTableReader dtr = dt.CreateDataReader ();
2237                         dtLoad.Load (dtr, LoadOption.Upsert);
2238                         AssertEquals ("RowData2-C", " ",
2239                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2240                 }
2241
2242                 [Test]
2243                 [ExpectedException (typeof (VersionNotFoundException))]
2244                 public void Load_RowStateUpsertDelete2 () {
2245                         localSetup ();
2246                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2247                         dtLoad.Columns.Add ("id", typeof (int));
2248                         dtLoad.Columns.Add ("name", typeof (string));
2249                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2250                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2251                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2252                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2253                         dtLoad.AcceptChanges ();
2254                         dtLoad.Rows[2].Delete ();
2255                         DataTableReader dtr = dt.CreateDataReader ();
2256                         dtLoad.Load (dtr, LoadOption.Upsert);
2257                         AssertEquals ("RowData3-O", " ",
2258                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2259                 }
2260
2261                 [Test]
2262                 [ExpectedException (typeof (VersionNotFoundException))]
2263                 public void Load_RowStateUpsertAdd () {
2264                         localSetup ();
2265                         dt.Rows.Add (new object[] { 4, "mono 4" });
2266                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2267                         dtLoad.Columns.Add ("id", typeof (int));
2268                         dtLoad.Columns.Add ("name", typeof (string));
2269                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2270                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2271                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2272                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2273                         dtLoad.AcceptChanges ();
2274                         DataRow row = dtLoad.NewRow ();
2275                         row["id"] = 4;
2276                         row["name"] = "Add 4";
2277                         dtLoad.Rows.Add (row);
2278                         DataTableReader dtr = dt.CreateDataReader ();
2279                         dtLoad.Load (dtr, LoadOption.Upsert);
2280                         AssertEquals ("RowData3-O", " ",
2281                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2282                 }
2283
2284                 [Test]
2285                 [ExpectedException (typeof (VersionNotFoundException))]
2286                 public void Load_RowStateUpsertUnpresent () {
2287                         localSetup ();
2288                         dt.Rows.Add (new object[] { 4, "mono 4" });
2289                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2290                         dtLoad.Columns.Add ("id", typeof (int));
2291                         dtLoad.Columns.Add ("name", typeof (string));
2292                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2293                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2294                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2295                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2296                         dtLoad.AcceptChanges ();
2297                         DataTableReader dtr = dt.CreateDataReader ();
2298                         dtLoad.Load (dtr, LoadOption.Upsert);
2299                         AssertEquals ("RowData3-O", " ",
2300                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2301                 }
2302
2303                 [Test]
2304                 public void Load_RowStateUpsertUnchangedEqualVal () {
2305                         localSetup ();
2306                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2307                         dtLoad.Columns.Add ("id", typeof (int));
2308                         dtLoad.Columns.Add ("name", typeof (string));
2309                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2310                         dtLoad.Rows.Add (new object[] { 1, "mono 1" });
2311                         dtLoad.AcceptChanges ();
2312                         DataTableReader dtr = dt.CreateDataReader ();
2313                         DataRowAction[] dra = new DataRowAction[] {
2314                                 DataRowAction.Nothing,// REAL action
2315                                 DataRowAction.Nothing,// dummy  
2316                                 DataRowAction.Nothing,// dummy  
2317                                 DataRowAction.Nothing,// dummy  
2318                                 DataRowAction.Nothing};// dummy  
2319                         rowActionInit (dra);
2320                         dtLoad.Load (dtr, LoadOption.Upsert);
2321                         rowActionEnd ();
2322                         AssertEquals ("RowData0-C", "mono 1",
2323                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2324                         AssertEquals ("RowData0-O", "mono 1",
2325                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2326                         AssertEquals ("RowState0", DataRowState.Unchanged,
2327                                 dtLoad.Rows[0].RowState);
2328                 }
2329
2330                 [Test]
2331                 public void LoadDataRow_LoadOptions () {
2332                         // LoadDataRow is covered in detail (without LoadOptions) in DataTableTest2
2333                         // LoadOption tests are covered in detail in DataTable.Load().
2334                         // Therefore only minimal tests of LoadDataRow with LoadOptions are covered here.
2335                         DataTable dt;
2336                         DataRow dr;
2337                         dt = CreateDataTableExample ();
2338                         dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };     //add ParentId as Primary Key
2339                         dt.Columns["String1"].DefaultValue = "Default";
2340
2341                         dr = dt.Select ("ParentId=1")[0];
2342
2343                         //Update existing row with LoadOptions = OverwriteChanges
2344                         dt.BeginLoadData ();
2345                         dt.LoadDataRow (new object[] { 1, null, "Changed" },
2346                                 LoadOption.OverwriteChanges);
2347                         dt.EndLoadData ();
2348
2349                         // LoadDataRow(update1) - check column String2
2350                         AssertEquals ("DT72-C", "Changed",
2351                                 dr["String2", DataRowVersion.Current]);
2352                         AssertEquals ("DT72-O", "Changed",
2353                                 dr["String2", DataRowVersion.Original]);
2354
2355                         // LoadDataRow(update1) - check row state
2356                         AssertEquals ("DT73-LO", DataRowState.Unchanged, dr.RowState);
2357
2358                         //Add New row with LoadOptions = Upsert
2359                         dt.BeginLoadData ();
2360                         dt.LoadDataRow (new object[] { 99, null, "Changed" },
2361                                 LoadOption.Upsert);
2362                         dt.EndLoadData ();
2363
2364                         // LoadDataRow(insert1) - check column String2
2365                         dr = dt.Select ("ParentId=99")[0];
2366                         AssertEquals ("DT75-C", "Changed",
2367                                 dr["String2", DataRowVersion.Current]);
2368
2369                         // LoadDataRow(insert1) - check row state
2370                         AssertEquals ("DT76-LO", DataRowState.Added, dr.RowState);
2371                 }
2372
2373                 public static DataTable CreateDataTableExample () {
2374                         DataTable dtParent = new DataTable ("Parent");
2375
2376                         dtParent.Columns.Add ("ParentId", typeof (int));
2377                         dtParent.Columns.Add ("String1", typeof (string));
2378                         dtParent.Columns.Add ("String2", typeof (string));
2379
2380                         dtParent.Columns.Add ("ParentDateTime", typeof (DateTime));
2381                         dtParent.Columns.Add ("ParentDouble", typeof (double));
2382                         dtParent.Columns.Add ("ParentBool", typeof (bool));
2383
2384                         dtParent.Rows.Add (new object[] { 1, "1-String1", "1-String2", new DateTime (2005, 1, 1, 0, 0, 0, 0), 1.534, true });
2385                         dtParent.Rows.Add (new object[] { 2, "2-String1", "2-String2", new DateTime (2004, 1, 1, 0, 0, 0, 1), -1.534, true });
2386                         dtParent.Rows.Add (new object[] { 3, "3-String1", "3-String2", new DateTime (2003, 1, 1, 0, 0, 1, 0), double.MinValue * 10000, false });
2387                         dtParent.Rows.Add (new object[] { 4, "4-String1", "4-String2", new DateTime (2002, 1, 1, 0, 1, 0, 0), double.MaxValue / 10000, true });
2388                         dtParent.Rows.Add (new object[] { 5, "5-String1", "5-String2", new DateTime (2001, 1, 1, 1, 0, 0, 0), 0.755, true });
2389                         dtParent.Rows.Add (new object[] { 6, "6-String1", "6-String2", new DateTime (2000, 1, 1, 0, 0, 0, 0), 0.001, false });
2390                         dtParent.AcceptChanges ();
2391                         return dtParent;
2392                 }
2393
2394                 #endregion // DataTable.Load Tests
2395
2396                 #region Read/Write XML Tests
2397
2398                 [Test]
2399                 [Category ("NotWorking")]
2400                 public void ReadXmlSchema () {
2401                         DataTable Table = new DataTable ();
2402                         Table.ReadXmlSchema ("Test/System.Data/own_schema1.xsd");
2403
2404                         AssertEquals ("test#02", "test_table", Table.TableName);
2405                         AssertEquals ("test#03", "", Table.Namespace);
2406                         AssertEquals ("test#04", 2, Table.Columns.Count);
2407                         AssertEquals ("test#05", 0, Table.Rows.Count);
2408                         AssertEquals ("test#06", false, Table.CaseSensitive);
2409                         AssertEquals ("test#07", 1, Table.Constraints.Count);
2410                         AssertEquals ("test#08", "", Table.Prefix);
2411
2412                         Constraint cons = Table.Constraints[0];
2413                         AssertEquals ("test#09", "Constraint1", cons.ConstraintName.ToString ());
2414                         AssertEquals ("test#10", "Constraint1", cons.ToString ());
2415
2416                         DataColumn column = Table.Columns[0];
2417                         AssertEquals ("test#11", true, column.AllowDBNull);
2418                         AssertEquals ("test#12", false, column.AutoIncrement);
2419                         AssertEquals ("test#13", 0L, column.AutoIncrementSeed);
2420                         AssertEquals ("test#14", 1L, column.AutoIncrementStep);
2421                         AssertEquals ("test#15", "test", column.Caption);
2422                         AssertEquals ("test#16", "Element", column.ColumnMapping.ToString ());
2423                         AssertEquals ("test#17", "first", column.ColumnName);
2424                         AssertEquals ("test#18", "System.String", column.DataType.ToString ());
2425                         AssertEquals ("test#19", "test_default_value", column.DefaultValue.ToString ());
2426                         AssertEquals ("test#20", false, column.DesignMode);
2427                         AssertEquals ("test#21", "", column.Expression);
2428                         AssertEquals ("test#22", 100, column.MaxLength);
2429                         AssertEquals ("test#23", "", column.Namespace);
2430                         AssertEquals ("test#24", 0, column.Ordinal);
2431                         AssertEquals ("test#25", "", column.Prefix);
2432                         AssertEquals ("test#26", false, column.ReadOnly);
2433                         AssertEquals ("test#27", true, column.Unique);
2434
2435                         DataColumn column2 = Table.Columns[1];
2436                         AssertEquals ("test#28", true, column2.AllowDBNull);
2437                         AssertEquals ("test#29", false, column2.AutoIncrement);
2438                         AssertEquals ("test#30", 0L, column2.AutoIncrementSeed);
2439                         AssertEquals ("test#31", 1L, column2.AutoIncrementStep);
2440                         AssertEquals ("test#32", "second", column2.Caption);
2441                         AssertEquals ("test#33", "Element", column2.ColumnMapping.ToString ());
2442                         AssertEquals ("test#34", "second", column2.ColumnName);
2443                         AssertEquals ("test#35", "System.Data.SqlTypes.SqlGuid", column2.DataType.ToString ());
2444                         AssertEquals ("test#36", "Null", column2.DefaultValue.ToString ());
2445                         AssertEquals ("test#37", false, column2.DesignMode);
2446                         AssertEquals ("test#38", "", column2.Expression);
2447                         AssertEquals ("test#39", -1, column2.MaxLength);
2448                         AssertEquals ("test#40", "", column2.Namespace);
2449                         AssertEquals ("test#41", 1, column2.Ordinal);
2450                         AssertEquals ("test#42", "", column2.Prefix);
2451                         AssertEquals ("test#43", false, column2.ReadOnly);
2452                         AssertEquals ("test#44", false, column2.Unique);
2453
2454                         DataTable Table2 = new DataTable ();
2455                         Table2.ReadXmlSchema ("Test/System.Data/own_schema2.xsd");
2456
2457                         AssertEquals ("test#45", "second_test_table", Table2.TableName);
2458                         AssertEquals ("test#46", "", Table2.Namespace);
2459                         AssertEquals ("test#47", 1, Table2.Columns.Count);
2460                         AssertEquals ("test#48", 0, Table2.Rows.Count);
2461                         AssertEquals ("test#49", false, Table2.CaseSensitive);
2462                         AssertEquals ("test#50", 1, Table2.Constraints.Count);
2463                         AssertEquals ("test#51", "", Table2.Prefix);
2464
2465                         DataColumn column3 = Table2.Columns[0];
2466                         AssertEquals ("test#52", true, column3.AllowDBNull);
2467                         AssertEquals ("test#53", false, column3.AutoIncrement);
2468                         AssertEquals ("test#54", 0L, column3.AutoIncrementSeed);
2469                         AssertEquals ("test#55", 1L, column3.AutoIncrementStep);
2470                         AssertEquals ("test#56", "second_first", column3.Caption);
2471                         AssertEquals ("test#57", "Element", column3.ColumnMapping.ToString ());
2472                         AssertEquals ("test#58", "second_first", column3.ColumnName);
2473                         AssertEquals ("test#59", "System.String", column3.DataType.ToString ());
2474                         AssertEquals ("test#60", "default_value", column3.DefaultValue.ToString ());
2475                         AssertEquals ("test#61", false, column3.DesignMode);
2476                         AssertEquals ("test#62", "", column3.Expression);
2477                         AssertEquals ("test#63", 100, column3.MaxLength);
2478                         AssertEquals ("test#64", "", column3.Namespace);
2479                         AssertEquals ("test#65", 0, column3.Ordinal);
2480                         AssertEquals ("test#66", "", column3.Prefix);
2481                         AssertEquals ("test#67", false, column3.ReadOnly);
2482                         AssertEquals ("test#68", true, column3.Unique);
2483                 }
2484
2485                 [Test]
2486                 public void ReadXmlSchema_2 () {
2487                         DataTable dt = new DataTable ();
2488                         string xmlData = string.Empty;
2489                         xmlData += "<?xml version=\"1.0\"?>";
2490                         xmlData += "<xs:schema id=\"SiteConfiguration\" targetNamespace=\"http://tempuri.org/PortalCfg.xsd\" xmlns:mstns=\"http://tempuri.org/PortalCfg.xsd\" xmlns=\"http://tempuri.org/PortalCfg.xsd\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" attributeFormDefault=\"qualified\" elementFormDefault=\"qualified\">";
2491                         xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2492                         xmlData += "<xs:complexType>";
2493                         xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2494                         xmlData += "<xs:element name=\"Tab\">";
2495                         xmlData += "<xs:complexType>";
2496                         xmlData += "<xs:sequence>";
2497                         xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2498                         xmlData += "<xs:complexType>";
2499                         xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2500                         xmlData += "</xs:complexType>";
2501                         xmlData += "</xs:element>";
2502                         xmlData += "</xs:sequence>";
2503                         xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2504                         xmlData += "</xs:complexType>";
2505                         xmlData += "</xs:element>";
2506                         xmlData += "</xs:choice>";
2507                         xmlData += "</xs:complexType>";
2508                         xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2509                         xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2510                         xmlData += "<xs:field xpath=\"@TabId\" />";
2511                         xmlData += "</xs:key>";
2512                         xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2513                         xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2514                         xmlData += "<xs:field xpath=\"@ModuleID\" />";
2515                         xmlData += "</xs:key>";
2516                         xmlData += "</xs:element>";
2517                         xmlData += "</xs:schema>";
2518                         dt.ReadXmlSchema (new StringReader (xmlData));
2519                 }
2520
2521                 [Test]
2522                 public void ReadXmlSchema_ByStream () {
2523                         DataSet ds1 = new DataSet ();
2524                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2525                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2526
2527                         MemoryStream ms1 = new MemoryStream ();
2528                         MemoryStream ms2 = new MemoryStream ();
2529                         //write xml  schema only
2530                         //ds1.WriteXmlSchema (ms);
2531                         ds1.Tables[0].WriteXmlSchema (ms1);
2532                         ds1.Tables[1].WriteXmlSchema (ms2);
2533
2534                         MemoryStream ms11 = new MemoryStream (ms1.GetBuffer ());
2535                         MemoryStream ms22 = new MemoryStream (ms2.GetBuffer ());
2536                         //copy schema
2537                         //DataSet ds2 = new DataSet ();
2538                         DataTable dt1 = new DataTable ();
2539                         DataTable dt2 = new DataTable ();
2540
2541                         //ds2.ReadXmlSchema (ms1);
2542                         dt1.ReadXmlSchema (ms11);
2543                         dt2.ReadXmlSchema (ms22);
2544
2545                         //check xml schema
2546                         // ReadXmlSchema - Tables count
2547                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS269");
2548
2549                         // ReadXmlSchema - Tables 0 Col count
2550                         AssertEquals ("DS270", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2551
2552                         // ReadXmlSchema - Tables 1 Col count
2553                         AssertEquals ("DS271", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2554
2555                         //check some colummns types
2556                         // ReadXmlSchema - Tables 0 Col type
2557                         AssertEquals ("DS272", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2558
2559                         // ReadXmlSchema - Tables 1 Col type
2560                         AssertEquals ("DS273", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2561
2562                         //check that no data exists
2563                         // ReadXmlSchema - Table 1 row count
2564                         AssertEquals ("DS274",0, dt1.Rows.Count);
2565
2566                         // ReadXmlSchema - Table 2 row count
2567                         AssertEquals ("DS275",0, dt2.Rows.Count);
2568                 }
2569
2570                 [Test]
2571                 [Category ("NotWorking")]
2572                 public void ReadWriteXmlSchema_ByFileName () {
2573                         string sTempFileName1 = "tmpDataSet_ReadWriteXml_43899-1.xml";
2574                         string sTempFileName2 = "tmpDataSet_ReadWriteXml_43899-2.xml";
2575
2576                         DataSet ds1 = new DataSet ();
2577                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2578                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2579
2580                         ds1.Tables[0].WriteXmlSchema (sTempFileName1);
2581                         ds1.Tables[1].WriteXmlSchema (sTempFileName2);
2582
2583                         DataTable dt1 = new DataTable ();
2584                         DataTable dt2 = new DataTable ();
2585
2586                         dt1.ReadXmlSchema (sTempFileName1);
2587                         dt2.ReadXmlSchema (sTempFileName2);
2588
2589                         AssertEquals ("DS277", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2590                         AssertEquals ("DS278", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2591                         AssertEquals ("DS279", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2592                         AssertEquals ("DS280", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2593                         AssertEquals ("DS281", 0, dt1.Rows.Count);
2594                         AssertEquals ("DS282", 0, dt2.Rows.Count);
2595
2596                         File.Delete (sTempFileName1);
2597                         File.Delete (sTempFileName2);
2598                 }
2599
2600                 [Test]
2601                 public void ReadXmlSchema_ByTextReader () {
2602                         DataSet ds1 = new DataSet ();
2603                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2604                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2605
2606                         StringWriter sw1 = new StringWriter ();
2607                         StringWriter sw2 = new StringWriter ();
2608                         //write xml file, schema only
2609                         //ds1.WriteXmlSchema (sw);
2610                         ds1.Tables[0].WriteXmlSchema (sw1);
2611                         ds1.Tables[1].WriteXmlSchema (sw2);
2612
2613                         StringReader sr1 = new StringReader (sw1.GetStringBuilder ().ToString ());
2614                         StringReader sr2 = new StringReader (sw2.GetStringBuilder ().ToString ());
2615                         //copy both data and schema
2616                         //DataSet ds2 = new DataSet ();
2617                         DataTable dt1 = new DataTable ();
2618                         DataTable dt2 = new DataTable ();
2619
2620                         //ds2.ReadXmlSchema (sr);
2621                         dt1.ReadXmlSchema (sr1);
2622                         dt2.ReadXmlSchema (sr2);
2623
2624                         //check xml schema
2625                         // ReadXmlSchema - Tables count
2626                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS283");
2627
2628                         // ReadXmlSchema - Tables 0 Col count
2629                         AssertEquals ("DS284", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2630
2631                         // ReadXmlSchema - Tables 1 Col count
2632                         AssertEquals ("DS285", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2633
2634                         //check some colummns types
2635                         // ReadXmlSchema - Tables 0 Col type
2636                         AssertEquals ("DS286", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2637
2638                         // ReadXmlSchema - Tables 1 Col type
2639                         AssertEquals ("DS287", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2640
2641                         //check that no data exists
2642                         // ReadXmlSchema - Table 1 row count
2643                         AssertEquals ("DS288", 0, dt1.Rows.Count);
2644
2645                         // ReadXmlSchema - Table 2 row count
2646                         AssertEquals ("DS289", 0, dt2.Rows.Count);
2647                 }
2648
2649                 [Test]
2650                 public void ReadXmlSchema_ByXmlReader () {
2651                         DataSet ds1 = new DataSet ();
2652                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2653                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2654
2655                         StringWriter sw1 = new StringWriter ();
2656                         XmlTextWriter xmlTW1 = new XmlTextWriter (sw1);
2657                         StringWriter sw2 = new StringWriter ();
2658                         XmlTextWriter xmlTW2 = new XmlTextWriter (sw2);
2659
2660                         //write xml file, schema only
2661                         ds1.Tables[0].WriteXmlSchema (xmlTW1);
2662                         xmlTW1.Flush ();
2663                         ds1.Tables[1].WriteXmlSchema (xmlTW2);
2664                         xmlTW2.Flush ();
2665
2666                         StringReader sr1 = new StringReader (sw1.ToString ());
2667                         XmlTextReader xmlTR1 = new XmlTextReader (sr1);
2668                         StringReader sr2 = new StringReader (sw2.ToString ());
2669                         XmlTextReader xmlTR2 = new XmlTextReader (sr2);
2670
2671                         //copy both data and schema
2672                         //DataSet ds2 = new DataSet ();
2673                         DataTable dt1 = new DataTable ();
2674                         DataTable dt2 = new DataTable ();
2675
2676                         //ds2.ReadXmlSchema (xmlTR);
2677                         dt1.ReadXmlSchema (xmlTR1);
2678                         dt2.ReadXmlSchema (xmlTR2);
2679
2680                         //check xml schema
2681                         // ReadXmlSchema - Tables count
2682                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS290");
2683
2684                         // ReadXmlSchema - Tables 0 Col count
2685                         AssertEquals ("DS291", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2686
2687                         // ReadXmlSchema - Tables 1 Col count
2688                         AssertEquals ("DS292", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2689
2690                         //check some colummns types
2691                         // ReadXmlSchema - Tables 0 Col type
2692                         AssertEquals ("DS293", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2693
2694                         // ReadXmlSchema - Tables 1 Col type
2695                         AssertEquals ("DS294", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2696
2697                         //check that no data exists
2698                         // ReadXmlSchema - Table 1 row count
2699                         AssertEquals ("DS295", 0, dt1.Rows.Count);
2700
2701                         // ReadXmlSchema - Table 2 row count
2702                         AssertEquals ("DS296", 0, dt2.Rows.Count);
2703                 }
2704
2705                 [Test]
2706                 [Category ("NotWorking")]
2707                 public void WriteXmlSchema () {
2708                         DataSet ds = new DataSet ();
2709                         ds.ReadXml ("Test/System.Data/region.xml");
2710                         TextWriter writer = new StringWriter ();
2711                         ds.Tables[0].WriteXmlSchema (writer);
2712
2713
2714                         string TextString = GetNormalizedSchema (writer.ToString ());
2715                         //string TextString = writer.ToString ();
2716
2717                         string substring = TextString.Substring (0, TextString.IndexOf (EOL));
2718                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2719                         AssertEquals ("test#01", "<?xml version=\"1.0\" encoding=\"utf-16\"?>", substring);
2720
2721                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2722                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2723                         AssertEquals ("test#02", "<xs:schema id=\"Root\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">", substring);
2724
2725                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2726                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2727                         AssertEquals ("test#03", "  <xs:element msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Region\" msdata:UseCurrentLocale=\"true\" name=\"Root\">", substring);
2728
2729                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2730                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2731                         AssertEquals ("test#04", "    <xs:complexType>", substring);
2732
2733                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2734                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2735                         AssertEquals ("test#05", "      <xs:choice maxOccurs=\"unbounded\" minOccurs=\"0\">", substring);
2736
2737                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2738                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2739                         AssertEquals ("test#06", "        <xs:element name=\"Region\">", substring);
2740
2741                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2742                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2743                         AssertEquals ("test#07", "          <xs:complexType>", substring);
2744
2745                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2746                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2747                         AssertEquals ("test#08", "            <xs:sequence>", substring);
2748
2749                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2750                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2751                         AssertEquals ("test#09", "              <xs:element minOccurs=\"0\" name=\"RegionID\" type=\"xs:string\" />", substring);
2752
2753                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2754                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2755                         AssertEquals ("test#10", "              <xs:element minOccurs=\"0\" name=\"RegionDescription\" type=\"xs:string\" />", substring);
2756
2757                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2758                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2759                         AssertEquals ("test#11", "            </xs:sequence>", substring);
2760
2761                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2762                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2763                         AssertEquals ("test#12", "          </xs:complexType>", substring);
2764
2765                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2766                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2767                         AssertEquals ("test#13", "        </xs:element>", substring);
2768
2769                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2770                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2771                         AssertEquals ("test#14", "      </xs:choice>", substring);
2772
2773                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2774                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2775                         AssertEquals ("test#15", "    </xs:complexType>", substring);
2776
2777                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2778                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2779                         AssertEquals ("test#16", "  </xs:element>", substring);
2780
2781                         AssertEquals ("test#17", "</xs:schema>", TextString);
2782                 }
2783
2784                 [Test]
2785                 [Category ("NotWorking")]
2786                 public void WriteXmlSchema2 () {
2787                         string xml = @"<myDataSet xmlns='NetFrameWork'><myTable><id>0</id><item>item 0</item></myTable><myTable><id>1</id><item>item 1</item></myTable><myTable><id>2</id><item>item 2</item></myTable><myTable><id>3</id><item>item 3</item></myTable><myTable><id>4</id><item>item 4</item></myTable><myTable><id>5</id><item>item 5</item></myTable><myTable><id>6</id><item>item 6</item></myTable><myTable><id>7</id><item>item 7</item></myTable><myTable><id>8</id><item>item 8</item></myTable><myTable><id>9</id><item>item 9</item></myTable></myDataSet>";
2788                         string schema = @"<?xml version='1.0' encoding='utf-16'?>
2789 <xs:schema id='myDataSet' targetNamespace='NetFrameWork' xmlns:mstns='NetFrameWork' xmlns='NetFrameWork' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified'>
2790   <xs:element name='myDataSet' msdata:IsDataSet='true' msdata:MainDataTable='NetFrameWork_x003A_myTable' msdata:UseCurrentLocale='true'>
2791     <xs:complexType>
2792       <xs:choice minOccurs='0' maxOccurs='unbounded'>
2793         <xs:element name='myTable'>
2794           <xs:complexType>
2795             <xs:sequence>
2796               <xs:element name='id' msdata:AutoIncrement='true' type='xs:int' minOccurs='0' />
2797               <xs:element name='item' type='xs:string' minOccurs='0' />
2798             </xs:sequence>
2799           </xs:complexType>
2800         </xs:element>
2801       </xs:choice>
2802     </xs:complexType>
2803   </xs:element>
2804 </xs:schema>";
2805                         DataSet OriginalDataSet = new DataSet ("myDataSet");
2806                         OriginalDataSet.Namespace = "NetFrameWork";
2807                         DataTable myTable = new DataTable ("myTable");
2808                         DataColumn c1 = new DataColumn ("id", typeof (int));
2809                         c1.AutoIncrement = true;
2810                         DataColumn c2 = new DataColumn ("item");
2811                         myTable.Columns.Add (c1);
2812                         myTable.Columns.Add (c2);
2813                         OriginalDataSet.Tables.Add (myTable);
2814                         // Add ten rows.
2815                         DataRow newRow;
2816                         for (int i = 0; i < 10; i++) {
2817                                 newRow = myTable.NewRow ();
2818                                 newRow["item"] = "item " + i;
2819                                 myTable.Rows.Add (newRow);
2820                         }
2821                         OriginalDataSet.AcceptChanges ();
2822
2823                         StringWriter sw = new StringWriter ();
2824                         XmlTextWriter xtw = new XmlTextWriter (sw);
2825                         xtw.QuoteChar = '\'';
2826                         OriginalDataSet.WriteXml (xtw);
2827                         string result = sw.ToString ();
2828
2829                         AssertEquals (xml, result);
2830
2831                         sw = new StringWriter ();
2832                         xtw = new XmlTextWriter (sw);
2833                         xtw.Formatting = Formatting.Indented;
2834                         OriginalDataSet.Tables[0].WriteXmlSchema (xtw);
2835                         result = sw.ToString ();
2836
2837                         result = result.Replace ("\r\n", "\n").Replace ('"', '\'');
2838                         AssertEquals (schema.Replace ("\r\n", "\n"), result);
2839                 }
2840
2841                 [Test]
2842                 [Category ("NotWorking")]
2843                 public void WriteXmlSchema3 () {
2844                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2845 <xs:schema id=""ExampleDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2846   <xs:element name=""ExampleDataSet"" msdata:IsDataSet=""true"" msdata:MainDataTable=""ExampleDataTable"" msdata:UseCurrentLocale=""true"">
2847     <xs:complexType>
2848       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2849         <xs:element name=""ExampleDataTable"">
2850           <xs:complexType>
2851             <xs:attribute name=""PrimaryKeyColumn"" type=""xs:int"" use=""required"" />
2852           </xs:complexType>
2853         </xs:element>
2854       </xs:choice>
2855     </xs:complexType>
2856     <xs:unique name=""PK_ExampleDataTable"" msdata:PrimaryKey=""true"">
2857       <xs:selector xpath="".//ExampleDataTable"" />
2858       <xs:field xpath=""@PrimaryKeyColumn"" />
2859     </xs:unique>
2860   </xs:element>
2861 </xs:schema>";
2862                         DataSet ds = new DataSet ("ExampleDataSet");
2863
2864                         ds.Tables.Add (new DataTable ("ExampleDataTable"));
2865                         ds.Tables["ExampleDataTable"].Columns.Add (
2866                                 new DataColumn ("PrimaryKeyColumn", typeof (int), "", MappingType.Attribute));
2867                         ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"].AllowDBNull = false;
2868
2869                         ds.Tables["ExampleDataTable"].Constraints.Add (
2870                                 "PK_ExampleDataTable",
2871                                 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"],
2872                                 true);
2873
2874                         ds.AcceptChanges ();
2875                         StringWriter sw = new StringWriter ();
2876                         ds.Tables[0].WriteXmlSchema (sw);
2877
2878                         string result = sw.ToString ();
2879
2880                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2881                         //AssertEquals (xmlschema, result.Replace ("\r\n", "\n"));
2882                 }
2883
2884                 [Test]
2885                 [Category ("NotWorking")]
2886                 public void WriteXmlSchema4 () {
2887                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2888 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2889   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
2890     <xs:complexType>
2891       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2892         <xs:element name=""MyType"">
2893           <xs:complexType>
2894             <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2895             <xs:attribute name=""Desc"" type=""xs:string"" />
2896           </xs:complexType>
2897         </xs:element>
2898       </xs:choice>
2899     </xs:complexType>
2900   </xs:element>
2901 </xs:schema>";
2902                         DataSet ds = new DataSet ("Example");
2903
2904                         // Add MyType DataTable
2905                         DataTable dt = new DataTable ("MyType");
2906                         ds.Tables.Add (dt);
2907
2908                         dt.Columns.Add (new DataColumn ("ID", typeof (int), "",
2909                                 MappingType.Attribute));
2910                         dt.Columns["ID"].AllowDBNull = false;
2911
2912                         dt.Columns.Add (new DataColumn ("Desc", typeof
2913                                 (string), "", MappingType.Attribute));
2914
2915                         ds.AcceptChanges ();
2916
2917                         StringWriter sw = new StringWriter ();
2918                         ds.Tables[0].WriteXmlSchema (sw);
2919
2920                         string result = sw.ToString ();
2921
2922                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2923                 }
2924
2925                 [Test]
2926                 [Category ("NotWorking")]
2927                 public void WriteXmlSchema5 () {
2928                         string xmlschema1 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2929 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2930   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""StandAlone"" msdata:UseCurrentLocale=""true"">
2931     <xs:complexType>
2932       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2933         <xs:element name=""StandAlone"">
2934           <xs:complexType>
2935             <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2936             <xs:attribute name=""Desc"" type=""xs:string"" use=""required"" />
2937           </xs:complexType>
2938         </xs:element>
2939       </xs:choice>
2940     </xs:complexType>
2941   </xs:element>
2942 </xs:schema>";
2943                         string xmlschema2 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2944 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2945   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Dimension"" msdata:UseCurrentLocale=""true"">
2946     <xs:complexType>
2947       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2948         <xs:element name=""Dimension"">
2949           <xs:complexType>
2950             <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2951             <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2952           </xs:complexType>
2953         </xs:element>
2954       </xs:choice>
2955     </xs:complexType>
2956     <xs:unique name=""PK_Dimension"" msdata:PrimaryKey=""true"">
2957       <xs:selector xpath="".//Dimension"" />
2958       <xs:field xpath=""@Number"" />
2959     </xs:unique>
2960   </xs:element>
2961 </xs:schema>";
2962                         string xmlschema3 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2963 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2964   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Element"" msdata:UseCurrentLocale=""true"">
2965     <xs:complexType>
2966       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2967         <xs:element name=""Element"">
2968           <xs:complexType>
2969             <xs:attribute name=""Dimension"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2970             <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2971             <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2972           </xs:complexType>
2973         </xs:element>
2974       </xs:choice>
2975     </xs:complexType>
2976     <xs:unique name=""PK_Element"" msdata:PrimaryKey=""true"">
2977       <xs:selector xpath="".//Element"" />
2978       <xs:field xpath=""@Dimension"" />
2979       <xs:field xpath=""@Number"" />
2980     </xs:unique>
2981   </xs:element>
2982 </xs:schema>";
2983                         DataSet ds = new DataSet ("Example");
2984
2985                         // Add a DataTable with no ReadOnly columns
2986                         DataTable dt1 = new DataTable ("StandAlone");
2987                         ds.Tables.Add (dt1);
2988
2989                         // Add a ReadOnly column
2990                         dt1.Columns.Add (new DataColumn ("ID", typeof (int), "",
2991                                 MappingType.Attribute));
2992                         dt1.Columns["ID"].AllowDBNull = false;
2993
2994                         dt1.Columns.Add (new DataColumn ("Desc", typeof
2995                                 (string), "", MappingType.Attribute));
2996                         dt1.Columns["Desc"].AllowDBNull = false;
2997
2998                         // Add related DataTables with ReadOnly columns
2999                         DataTable dt2 = new DataTable ("Dimension");
3000                         ds.Tables.Add (dt2);
3001                         dt2.Columns.Add (new DataColumn ("Number", typeof
3002                                 (int), "", MappingType.Attribute));
3003                         dt2.Columns["Number"].AllowDBNull = false;
3004                         dt2.Columns["Number"].ReadOnly = true;
3005
3006                         dt2.Columns.Add (new DataColumn ("Title", typeof
3007                                 (string), "", MappingType.Attribute));
3008                         dt2.Columns["Title"].AllowDBNull = false;
3009
3010                         dt2.Constraints.Add ("PK_Dimension", dt2.Columns["Number"], true);
3011
3012                         DataTable dt3 = new DataTable ("Element");
3013                         ds.Tables.Add (dt3);
3014
3015                         dt3.Columns.Add (new DataColumn ("Dimension", typeof
3016                                 (int), "", MappingType.Attribute));
3017                         dt3.Columns["Dimension"].AllowDBNull = false;
3018                         dt3.Columns["Dimension"].ReadOnly = true;
3019
3020                         dt3.Columns.Add (new DataColumn ("Number", typeof
3021                                 (int), "", MappingType.Attribute));
3022                         dt3.Columns["Number"].AllowDBNull = false;
3023                         dt3.Columns["Number"].ReadOnly = true;
3024
3025                         dt3.Columns.Add (new DataColumn ("Title", typeof
3026                                 (string), "", MappingType.Attribute));
3027                         dt3.Columns["Title"].AllowDBNull = false;
3028
3029                         dt3.Constraints.Add ("PK_Element", new DataColumn[] { 
3030                                 dt3.Columns ["Dimension"],
3031                                 dt3.Columns ["Number"] }, true);
3032
3033                         ds.AcceptChanges ();
3034
3035                         StringWriter sw1 = new StringWriter ();
3036                         ds.Tables[0].WriteXmlSchema (sw1);
3037                         string result1 = sw1.ToString ();
3038                         AssertEquals (xmlschema1.Replace ("\r\n", "\n"), result1.Replace ("\r\n", "\n"));
3039
3040                         StringWriter sw2 = new StringWriter ();
3041                         ds.Tables[1].WriteXmlSchema (sw2);
3042                         string result2 = sw2.ToString ();
3043                         AssertEquals (xmlschema2.Replace ("\r\n", "\n"), result2.Replace ("\r\n", "\n"));
3044
3045                         StringWriter sw3 = new StringWriter ();
3046                         ds.Tables[2].WriteXmlSchema (sw3);
3047                         string result3 = sw3.ToString ();
3048                         AssertEquals (xmlschema3.Replace ("\r\n", "\n"), result3.Replace ("\r\n", "\n"));
3049                 }
3050
3051                 [Test]
3052                 [Category ("NotWorking")]
3053                 public void WriteXmlSchema6 () {
3054                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
3055 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
3056   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
3057     <xs:complexType>
3058       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
3059         <xs:element name=""MyType"">
3060           <xs:complexType>
3061             <xs:attribute name=""Desc"">
3062               <xs:simpleType>
3063                 <xs:restriction base=""xs:string"">
3064                   <xs:maxLength value=""32"" />
3065                 </xs:restriction>
3066               </xs:simpleType>
3067             </xs:attribute>
3068           </xs:complexType>
3069         </xs:element>
3070       </xs:choice>
3071     </xs:complexType>
3072   </xs:element>
3073 </xs:schema>";
3074                         DataSet ds = new DataSet ("Example");
3075
3076                         // Add MyType DataTable
3077                         ds.Tables.Add ("MyType");
3078
3079                         ds.Tables["MyType"].Columns.Add (new DataColumn (
3080                                 "Desc", typeof (string), "", MappingType.Attribute));
3081                         ds.Tables["MyType"].Columns["Desc"].MaxLength = 32;
3082
3083                         ds.AcceptChanges ();
3084
3085                         StringWriter sw = new StringWriter ();
3086                         ds.Tables[0].WriteXmlSchema (sw);
3087
3088                         string result = sw.ToString ();
3089
3090                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
3091                 }
3092
3093                 [Test]
3094                 public void WriteXmlSchema7 () {
3095                         DataSet ds = new DataSet ();
3096                         DataTable dt = new DataTable ("table");
3097                         dt.Columns.Add ("col1");
3098                         dt.Columns.Add ("col2");
3099                         ds.Tables.Add (dt);
3100                         dt.Rows.Add (new object[] { "foo", "bar" });
3101                         StringWriter sw = new StringWriter ();
3102                         ds.Tables[0].WriteXmlSchema (sw);
3103                         Assert (sw.ToString ().IndexOf ("xmlns=\"\"") > 0);
3104                 }
3105
3106                 [Test]
3107                 public void WriteXmlSchema_ConstraintNameWithSpaces () {
3108                         DataSet ds = new DataSet ();
3109                         DataTable table1 = ds.Tables.Add ("table1");
3110                         DataTable table2 = ds.Tables.Add ("table2");
3111
3112                         table1.Columns.Add ("col1", typeof (int));
3113                         table2.Columns.Add ("col1", typeof (int));
3114
3115                         table1.Constraints.Add ("uc 1", table1.Columns[0], false);
3116                         table2.Constraints.Add ("fc 1", table1.Columns[0], table2.Columns[0]);
3117
3118                         StringWriter sw1 = new StringWriter ();
3119                         StringWriter sw2 = new StringWriter ();
3120
3121                         //should not throw an exception
3122                         ds.Tables[0].WriteXmlSchema (sw1);
3123                         ds.Tables[1].WriteXmlSchema (sw2);
3124                 }
3125
3126                 [Test]
3127                 public void WriteXmlSchema_ForignKeyConstraint () {
3128                         DataSet ds1 = new DataSet ();
3129
3130                         DataTable table1 = ds1.Tables.Add ();
3131                         DataTable table2 = ds1.Tables.Add ();
3132
3133                         DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
3134                         DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
3135
3136                         table2.Constraints.Add ("fk", col1_1, col2_1);
3137
3138                         StringWriter sw1 = new StringWriter ();
3139                         ds1.Tables[0].WriteXmlSchema (sw1);
3140                         String xml1 = sw1.ToString ();
3141                         Assert ("#1", xml1.IndexOf (@"<xs:unique name=""Constraint1"">") != -1);
3142
3143                         StringWriter sw2 = new StringWriter ();
3144                         ds1.Tables[1].WriteXmlSchema (sw2);
3145                         String xml2 = sw2.ToString ();
3146                         Assert ("#2", xml2.IndexOf (@"<xs:unique name=""Constraint1"">") == -1);
3147                 }
3148
3149                 [Test]
3150                 [Category ("NotWorking")]
3151                 public void WriteXmlSchema_Relations_ForeignKeys () {
3152                         MemoryStream ms1 = null;
3153                         MemoryStream ms2 = null;
3154                         MemoryStream msA = null;
3155                         MemoryStream msB = null;
3156
3157                         DataSet ds1 = new DataSet ();
3158
3159                         DataTable table1 = ds1.Tables.Add ("Table 1");
3160                         DataTable table2 = ds1.Tables.Add ("Table 2");
3161
3162                         DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
3163                         DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
3164                         DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
3165                         DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
3166                         DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
3167                         DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
3168                         DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
3169
3170                         DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
3171                         DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
3172                         DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
3173                         DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
3174                         DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
3175                         DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
3176                         DataColumn col2_7 = table2.Columns.Add ("col 7", typeof (int));
3177
3178                         ds1.Relations.Add ("rel 1",
3179                                 new DataColumn[] { col1_1, col1_2 },
3180                                 new DataColumn[] { col2_1, col2_2 },
3181                                 false);
3182                         ds1.Relations.Add ("rel 2",
3183                                 new DataColumn[] { col1_3, col1_4 },
3184                                 new DataColumn[] { col2_3, col2_4 },
3185                                 true);
3186                         table2.Constraints.Add ("fk 1",
3187                                 new DataColumn[] { col1_5, col1_6 },
3188                                 new DataColumn[] { col2_5, col2_6 });
3189                         table1.Constraints.Add ("fk 2",
3190                                 new DataColumn[] { col2_5, col2_6 },
3191                                 new DataColumn[] { col1_5, col1_6 });
3192
3193                         table1.Constraints.Add ("pk 1", col1_7, true);
3194                         table2.Constraints.Add ("pk 2", col2_7, true);
3195
3196                         ms1 = new MemoryStream ();
3197                         ds1.Tables[0].WriteXmlSchema (ms1);
3198                         ms2 = new MemoryStream ();
3199                         ds1.Tables[1].WriteXmlSchema (ms2);
3200
3201                         msA = new MemoryStream (ms1.GetBuffer ());
3202                         DataTable dtA = new DataTable ();
3203                         dtA.ReadXmlSchema (msA);
3204
3205                         msB = new MemoryStream (ms2.GetBuffer ());
3206                         DataTable dtB = new DataTable ();
3207                         dtB.ReadXmlSchema (msB);
3208
3209                         AssertEquals ("#2", 3, dtA.Constraints.Count);
3210                         AssertEquals ("#3", 2, dtB.Constraints.Count);
3211
3212                         Assert ("#5", dtA.Constraints.Contains ("pk 1"));
3213                         Assert ("#6", dtA.Constraints.Contains ("Constraint1"));
3214                         Assert ("#7", dtA.Constraints.Contains ("Constraint2"));
3215                         Assert ("#9", dtB.Constraints.Contains ("pk 2"));
3216                         Assert ("#10", dtB.Constraints.Contains ("Constraint1"));
3217                 }
3218
3219                 [Test]
3220                 [Category ("NotWorking")]
3221                 public void WriteXmlSchema_DifferentNamespace () {
3222                         string schema = @"<xs:schema id='NewDataSet' targetNamespace='urn:bar' xmlns:mstns='urn:bar' xmlns='urn:bar' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo' msdata:schemafragmentcount='3'>
3223   <xs:import namespace='urn:foo' />
3224   <xs:import namespace='urn:baz' />
3225   <xs:element name='NewDataSet' msdata:IsDataSet='true' msdata:MainDataTable='urn_x003A_foo_x003A_NS1Table' msdata:UseCurrentLocale='true'>
3226     <xs:complexType>
3227       <xs:choice minOccurs='0' maxOccurs='unbounded'>
3228         <xs:element ref='app2:NS1Table' />
3229       </xs:choice>
3230     </xs:complexType>
3231   </xs:element>
3232 </xs:schema>
3233 <xs:schema targetNamespace='urn:baz' xmlns:mstns='urn:bar' xmlns='urn:baz' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app1='urn:baz' xmlns:app2='urn:foo'>
3234   <xs:import namespace='urn:foo' />
3235   <xs:import namespace='urn:bar' />
3236   <xs:element name='column2' type='xs:string' />
3237 </xs:schema>
3238 <xs:schema targetNamespace='urn:foo' xmlns:mstns='urn:bar' xmlns='urn:foo' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' attributeFormDefault='qualified' elementFormDefault='qualified' xmlns:app2='urn:foo' xmlns:app1='urn:baz'>
3239   <xs:import namespace='urn:bar' />
3240   <xs:import namespace='urn:baz' />
3241   <xs:element name='NS1Table'>
3242     <xs:complexType>
3243       <xs:sequence>
3244         <xs:element name='column1' type='xs:string' minOccurs='0' />
3245         <xs:element ref='app1:column2' minOccurs='0' />
3246       </xs:sequence>
3247     </xs:complexType>
3248   </xs:element>
3249 </xs:schema>";
3250                         DataSet ds = new DataSet ();
3251                         DataTable dt = new DataTable ();
3252                         dt.TableName = "NS1Table";
3253                         dt.Namespace = "urn:foo";
3254                         dt.Columns.Add ("column1");
3255                         dt.Columns.Add ("column2");
3256                         dt.Columns[1].Namespace = "urn:baz";
3257                         ds.Tables.Add (dt);
3258                         DataTable dt2 = new DataTable ();
3259                         dt2.TableName = "NS2Table";
3260                         dt2.Namespace = "urn:bar";
3261                         ds.Tables.Add (dt2);
3262                         ds.Namespace = "urn:bar";
3263
3264                         StringWriter sw1 = new StringWriter ();
3265                         XmlTextWriter xw1 = new XmlTextWriter (sw1);
3266                         xw1.Formatting = Formatting.Indented;
3267                         xw1.QuoteChar = '\'';
3268                         ds.Tables[0].WriteXmlSchema (xw1);
3269                         string result1 = sw1.ToString ();
3270                         AssertEquals ("#1", schema, result1);
3271
3272                         StringWriter sw2 = new StringWriter ();
3273                         XmlTextWriter xw2 = new XmlTextWriter (sw2);
3274                         xw2.Formatting = Formatting.Indented;
3275                         xw2.QuoteChar = '\'';
3276                         ds.Tables[0].WriteXmlSchema (xw2);
3277                         string result2 = sw2.ToString ();
3278                         AssertEquals ("#2", schema, result2);
3279                 }
3280
3281                 [Test]
3282                 [Category ("NotWorking")]
3283                 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3284                 // and is commented-out TWICE below
3285                 public void WriteXmlSchema_Hierarchy () {
3286                         DataSet ds = new DataSet ();
3287                         DataTable table1 = new DataTable ();
3288                         DataColumn idColumn = table1.Columns.Add ("ID", typeof (Int32));
3289                         table1.Columns.Add ("Name", typeof (String));
3290                         table1.PrimaryKey = new DataColumn[] { idColumn };
3291                         DataTable table2 = new DataTable ();
3292                         table2.Columns.Add (new DataColumn ("OrderID", typeof (Int32)));
3293                         table2.Columns.Add (new DataColumn ("CustomerID", typeof (Int32)));
3294                         table2.Columns.Add (new DataColumn ("OrderDate", typeof (DateTime)));
3295                         table2.PrimaryKey = new DataColumn[] { table2.Columns[0] };
3296                         ds.Tables.Add (table1);
3297                         ds.Tables.Add (table2);
3298                         ds.Relations.Add ("CustomerOrder",
3299                             new DataColumn[] { table1.Columns[0] },
3300                             new DataColumn[] { table2.Columns[1] }, true);
3301
3302                         StringWriter writer1 = new StringWriter ();
3303                         //table1.WriteXmlSchema (writer1, false);
3304                         string expected1 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n  <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n    <xs:complexType>\r\n      <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n        <xs:element name=\"Table1\">\r\n          <xs:complexType>\r\n            <xs:sequence>\r\n              <xs:element name=\"ID\" type=\"xs:int\" />\r\n              <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n            </xs:sequence>\r\n          </xs:complexType>\r\n        </xs:element>\r\n      </xs:choice>\r\n    </xs:complexType>\r\n    <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n      <xs:selector xpath=\".//Table1\" />\r\n      <xs:field xpath=\"ID\" />\r\n    </xs:unique>\r\n  </xs:element>\r\n</xs:schema>";
3305                         AssertEquals ("#1", expected1, writer1.ToString());
3306
3307                         StringWriter writer2 = new StringWriter ();
3308                         //table1.WriteXmlSchema (writer2, true);
3309                         string expected2 = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n  <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n    <xs:complexType>\r\n      <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\r\n        <xs:element name=\"Table1\">\r\n          <xs:complexType>\r\n            <xs:sequence>\r\n              <xs:element name=\"ID\" type=\"xs:int\" />\r\n              <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\" />\r\n            </xs:sequence>\r\n          </xs:complexType>\r\n        </xs:element>\r\n        <xs:element name=\"Table2\">\r\n          <xs:complexType>\r\n            <xs:sequence>\r\n              <xs:element name=\"OrderID\" type=\"xs:int\" />\r\n              <xs:element name=\"CustomerID\" type=\"xs:int\" minOccurs=\"0\" />\r\n              <xs:element name=\"OrderDate\" type=\"xs:dateTime\" minOccurs=\"0\" />\r\n            </xs:sequence>\r\n          </xs:complexType>\r\n        </xs:element>\r\n      </xs:choice>\r\n    </xs:complexType>\r\n    <xs:unique name=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n      <xs:selector xpath=\".//Table1\" />\r\n      <xs:field xpath=\"ID\" />\r\n    </xs:unique>\r\n    <xs:unique name=\"Table2_Constraint1\" msdata:ConstraintName=\"Constraint1\" msdata:PrimaryKey=\"true\">\r\n      <xs:selector xpath=\".//Table2\" />\r\n      <xs:field xpath=\"OrderID\" />\r\n    </xs:unique>\r\n    <xs:keyref name=\"CustomerOrder\" refer=\"Constraint1\">\r\n      <xs:selector xpath=\".//Table2\" />\r\n      <xs:field xpath=\"CustomerID\" />\r\n    </xs:keyref>\r\n  </xs:element>\r\n</xs:schema>";
3310                         AssertEquals ("#2", expected2, writer2.ToString ());
3311                 }
3312
3313                 [Test]
3314                 [Category ("NotWorking")]
3315                 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3316                 // and is commented-out TWICE below
3317                 public void ReadWriteXmlSchema () {
3318                         DataSet ds = new DataSet ();
3319                         ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3320                         // check dataset properties before testing write
3321                         AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3322                         AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3323                         AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3324                         AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3325                         // FIXME: currently order is not compatible. Use name as index
3326                         AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3327                         AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3328
3329                         ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.InferSchema);
3330                         ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3331
3332                         TextWriter writer1 = new StringWriter ();
3333                         ds.Tables[0].WriteXmlSchema (writer1);
3334                         string TextString1 = GetNormalizedSchema (writer1.ToString ());
3335                         string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3336 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3337   @"<xs:complexType name=""bookstoreType"">" +
3338   @"</xs:complexType>" +
3339   @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3340   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3341     @"<xs:complexType>" +
3342       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3343         @"<xs:element ref=""bookstore"" />" +
3344       @"</xs:choice>" +
3345     @"</xs:complexType>" +
3346   @"</xs:element>" +
3347 @"</xs:schema>";
3348                         AssertEquals ("#1", expected1, TextString1.Replace("\r\n","").Replace("  ",""));
3349
3350                         TextWriter writer2 = new StringWriter ();
3351                         //ds.Tables[1].WriteXmlSchema (writer2,false);
3352                         string TextString2 = GetNormalizedSchema (writer2.ToString ());
3353                         string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3354 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3355   @"<xs:complexType name=""bookType"">" +
3356     @"<xs:sequence>" +
3357       @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3358       @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3359     @"</xs:sequence>" +
3360     @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3361     @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3362   @"</xs:complexType>" +
3363   @"<xs:element name=""book"" type=""bookType"" />" +
3364   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3365     @"<xs:complexType>" +
3366       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3367         @"<xs:element ref=""book"" />" +
3368       @"</xs:choice>" +
3369     @"</xs:complexType>" +
3370   @"</xs:element>" +
3371 @"</xs:schema>";
3372                         AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace ("  ", ""));
3373                         
3374                         TextWriter writer3 = new StringWriter ();
3375                         ds.Tables[2].WriteXmlSchema (writer3);
3376                         string TextString3 = GetNormalizedSchema (writer3.ToString ());
3377                         string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3378 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3379   @"<xs:complexType name=""authorName"">" +
3380     @"<xs:sequence>" +
3381       @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3382       @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3383     @"</xs:sequence>" +
3384     @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3385   @"</xs:complexType>" +
3386   @"<xs:element name=""author"" type=""authorName"" />" +
3387   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3388     @"<xs:complexType>" +
3389       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3390         @"<xs:element ref=""author"" />" +
3391       @"</xs:choice>" +
3392     @"</xs:complexType>" +
3393   @"</xs:element>" +
3394 @"</xs:schema>";
3395                         AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace ("  ", ""));
3396                         
3397                         TextWriter writer4 = new StringWriter ();
3398                         ds.Tables[3].WriteXmlSchema (writer4);
3399                         string TextString4 = GetNormalizedSchema (writer4.ToString ());
3400                         string expected4 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3401 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3402   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""Region"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3403     @"<xs:complexType>" +
3404       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3405         @"<xs:element name=""Region"">" +
3406           @"<xs:complexType>" +
3407             @"<xs:sequence>" +
3408               @"<xs:element minOccurs=""0"" name=""RegionID"" type=""xs:string"" />" +
3409               @"<xs:element minOccurs=""0"" name=""RegionDescription"" type=""xs:string"" />" +
3410             @"</xs:sequence>" +
3411           @"</xs:complexType>" +
3412         @"</xs:element>" +
3413       @"</xs:choice>" +
3414     @"</xs:complexType>" +
3415   @"</xs:element>" +
3416 @"</xs:schema>";
3417                         AssertEquals ("#4", expected4, TextString4.Replace ("\r\n", "").Replace ("  ", ""));
3418                 }
3419
3420                 [Test]
3421                 [Category ("NotWorking")]
3422                 public void ReadWriteXmlSchema_IgnoreSchema () {
3423                         DataSet ds = new DataSet ();
3424                         ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3425                         // check dataset properties before testing write
3426                         AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3427                         AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3428                         AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3429                         AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3430                         // FIXME: currently order is not compatible. Use name as index
3431                         AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3432                         AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3433
3434                         ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.IgnoreSchema);
3435                         ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3436
3437                         TextWriter writer1 = new StringWriter ();
3438                         ds.Tables[0].WriteXmlSchema (writer1);
3439                         string TextString1 = GetNormalizedSchema (writer1.ToString ());
3440                         string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3441 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3442   @"<xs:complexType name=""bookstoreType"">" +
3443   @"</xs:complexType>" +
3444   @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3445   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3446     @"<xs:complexType>" +
3447       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3448         @"<xs:element ref=""bookstore"" />" +
3449       @"</xs:choice>" +
3450     @"</xs:complexType>" +
3451   @"</xs:element>" +
3452 @"</xs:schema>";
3453                         AssertEquals ("#1", expected1, TextString1.Replace ("\r\n", "").Replace ("  ", ""));
3454
3455                         TextWriter writer2 = new StringWriter ();
3456                         //ds.Tables[1].WriteXmlSchema (writer2, false);
3457                         string TextString2 = GetNormalizedSchema (writer2.ToString ());
3458                         string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3459 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3460   @"<xs:complexType name=""bookType"">" +
3461     @"<xs:sequence>" +
3462       @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3463       @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3464     @"</xs:sequence>" +
3465     @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3466     @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3467   @"</xs:complexType>" +
3468   @"<xs:element name=""book"" type=""bookType"" />" +
3469   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3470     @"<xs:complexType>" +
3471       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3472         @"<xs:element ref=""book"" />" +
3473       @"</xs:choice>" +
3474     @"</xs:complexType>" +
3475   @"</xs:element>" +
3476 @"</xs:schema>";
3477                         AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace ("  ", ""));
3478
3479                         TextWriter writer3 = new StringWriter ();
3480                         ds.Tables[2].WriteXmlSchema (writer3);
3481                         string TextString3 = GetNormalizedSchema (writer3.ToString ());
3482                         string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3483 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3484   @"<xs:complexType name=""authorName"">" +
3485     @"<xs:sequence>" +
3486       @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3487       @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3488     @"</xs:sequence>" +
3489     @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3490   @"</xs:complexType>" +
3491   @"<xs:element name=""author"" type=""authorName"" />" +
3492   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3493     @"<xs:complexType>" +
3494       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3495         @"<xs:element ref=""author"" />" +
3496       @"</xs:choice>" +
3497     @"</xs:complexType>" +
3498   @"</xs:element>" +
3499 @"</xs:schema>";
3500                         AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace ("  ", ""));
3501
3502                         TextWriter writer4 = new StringWriter ();
3503                         string expStr = "";
3504                         try {
3505                                 ds.Tables[3].WriteXmlSchema (writer4);
3506                         }
3507                         catch (Exception ex) {
3508                                 expStr = ex.Message;
3509                         }
3510                         AssertEquals ("#4", "Cannot find table 3.", expStr);
3511                 }
3512
3513                 [Test]
3514                 [Category ("NotWorking")]
3515                 public void ReadWriteXmlSchema_2 () {
3516                         DataSet ds = new DataSet ("dataset");
3517                         ds.Tables.Add ("table1");
3518                         ds.Tables.Add ("table2");
3519                         ds.Tables[0].Columns.Add ("col");
3520                         ds.Tables[1].Columns.Add ("col");
3521                         ds.Relations.Add ("rel", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0], true);
3522
3523                         MemoryStream ms1 = new MemoryStream ();
3524                         ds.Tables[0].WriteXmlSchema (ms1);
3525                         MemoryStream ms2 = new MemoryStream ();
3526                         ds.Tables[1].WriteXmlSchema (ms2);
3527
3528                         DataSet ds1 = new DataSet ();
3529                         ds1.Tables.Add ();
3530                         ds1.Tables.Add ();
3531                         ds1.Tables[0].ReadXmlSchema (new MemoryStream (ms1.GetBuffer ()));
3532                         ds1.Tables[1].ReadXmlSchema (new MemoryStream (ms2.GetBuffer ()));
3533
3534                         AssertEquals ("#1", 0, ds1.Relations.Count);
3535                         AssertEquals ("#2", 1, ds1.Tables[0].Columns.Count);
3536                         AssertEquals ("#3", 1, ds1.Tables[1].Columns.Count);
3537                 }
3538
3539                 [Test]
3540                 [ExpectedException (typeof (XmlException))]
3541                 public void ReadWriteXmlSchemaExp_NoRootElmnt () {
3542                         MemoryStream ms = new MemoryStream ();
3543                         DataTable dtr = new DataTable ();
3544                         dtr.ReadXmlSchema (ms);
3545                 }
3546
3547                 [Test]
3548                 [Category ("NotWorking")]
3549                 [ExpectedException (typeof (InvalidOperationException))]
3550                 public void ReadWriteXmlSchemaExp_NoTableName () {
3551                         DataTable dtw = new DataTable ();
3552                         MemoryStream ms = new MemoryStream ();
3553                         dtw.WriteXmlSchema (ms);
3554                 }
3555
3556                 [Test]
3557                 [ExpectedException (typeof (ArgumentException))]
3558                 public void ReadWriteXmlSchemaExp_TableNameConflict () {
3559                         DataTable dtw = new DataTable ("Table1");
3560                         StringWriter writer1 = new StringWriter ();
3561                         dtw.WriteXmlSchema (writer1);
3562                         DataTable dtr = new DataTable ("Table2");
3563                         StringReader reader1 = new StringReader (writer1.ToString());
3564                         dtr.ReadXmlSchema (reader1);
3565                 }
3566
3567                 #endregion // Read/Write XML Tests
3568
3569 #endif // NET_2_0
3570
3571         }
3572                                                                                                     
3573                                                                                                     
3574          public  class MyDataTable:DataTable {
3575                                                                                                     
3576              public static int count = 0;
3577                                                                                                     
3578              public MyDataTable() {
3579                                                                                                     
3580                     count++;
3581              }
3582                                                                                                     
3583          }
3584
3585         [Serializable]
3586         [TestFixture]
3587         public class AppDomainsAndFormatInfo
3588         {
3589                 public void Remote ()
3590                 {
3591                         int n = (int) Convert.ChangeType ("5", typeof (int));
3592                         Assertion.AssertEquals ("n", 5, n);
3593                 }
3594 #if !TARGET_JVM
3595                 [Test]
3596                 public void NFIFromBug55978 ()
3597                 {
3598                         AppDomain domain = AppDomain.CreateDomain ("testdomain");
3599                         AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
3600                         test.Remote ();
3601                         domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
3602                         AppDomain.Unload (domain);
3603                 }
3604 #endif
3605
3606                 [Test]
3607                 public void Bug55978 ()
3608                 {
3609                         DataTable dt = new DataTable ();
3610                         dt.Columns.Add ("StartDate", typeof (DateTime));
3611          
3612                         DataRow dr;
3613                         DateTime date = DateTime.Now;
3614          
3615                         for (int i = 0; i < 10; i++) {
3616                                 dr = dt.NewRow ();
3617                                 dr ["StartDate"] = date.AddDays (i);
3618                                 dt.Rows.Add (dr);
3619                         }
3620          
3621                         DataView dv = dt.DefaultView;
3622                         dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
3623                         Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
3624                         Assertion.AssertEquals ("View", 2, dv.Count);
3625                 }
3626         }
3627 }