DataTableReadXmlSchemaTest.cs fixed #ifNet2.0 and notworking
[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                 [Category ("NotWorking")]
1679                 // Load doesn't have a third overload in System.Data
1680                 // and is commented-out below
1681                 public void Load_IncompatibleEHandlerT () {
1682                         fillErrCounter = 0;
1683                         fillErr[0].init ("LoadIncompatible", 1, true,
1684                                 "Input string was not in a correct format.Couldn't store <mono 1> in name Column.  Expected type is Double.");
1685                         fillErr[1].init ("LoadIncompatible", 2, true,
1686                                 "Input string was not in a correct format.Couldn't store <mono 2> in name Column.  Expected type is Double.");
1687                         fillErr[2].init ("LoadIncompatible", 3, true,
1688                                 "Input string was not in a correct format.Couldn't store <mono 3> in name Column.  Expected type is Double.");
1689                         localSetup ();
1690                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1691                         dtLoad.Columns.Add ("name", typeof (double));
1692                         DataTableReader dtr = dt.CreateDataReader ();
1693                         //dtLoad.Load (dtr,LoadOption.PreserveChanges,fillErrorHandler);
1694                 }
1695                 [Test]
1696                 [Category ("NotWorking")]
1697                 // Load doesn't have a third overload in System.Data
1698                 // and is commented-out below
1699                 [ExpectedException (typeof (ArgumentException))]
1700                 public void Load_IncompatibleEHandlerF () {
1701                         fillErrCounter = 0;
1702                         fillErr[0].init ("LoadIncompatible", 1, false,
1703                                 "Input string was not in a correct format.Couldn't store <mono 1> in name Column.  Expected type is Double.");
1704                         localSetup ();
1705                         DataTable dtLoad = new DataTable ("LoadIncompatible");
1706                         dtLoad.Columns.Add ("name", typeof (double));
1707                         DataTableReader dtr = dt.CreateDataReader ();
1708                         //dtLoad.Load (dtr, LoadOption.PreserveChanges, fillErrorHandler);
1709                 }
1710
1711                 [Test]
1712                 public void Load_ExtraColsEqualVal () {
1713                         localSetup ();
1714                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1715                         dtLoad.Columns.Add ("id", typeof (int));
1716                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1717                         dtLoad.Rows.Add (new object[] { 1 });
1718                         dtLoad.Rows.Add (new object[] { 2 });
1719                         dtLoad.Rows.Add (new object[] { 3 });
1720                         dtLoad.AcceptChanges ();
1721                         DataTableReader dtr = dt.CreateDataReader ();
1722                         dtLoad.Load (dtr);
1723                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1724                         AssertEquals ("NRows", 3, dtLoad.Rows.Count);
1725                         AssertEquals ("RowData0-0", 1, dtLoad.Rows[0][0]);
1726                         AssertEquals ("RowData0-1", "mono 1", dtLoad.Rows[0][1]);
1727                         AssertEquals ("RowData1-0", 2, dtLoad.Rows[1][0]);
1728                         AssertEquals ("RowData1-1", "mono 2", dtLoad.Rows[1][1]);
1729                         AssertEquals ("RowData2-0", 3, dtLoad.Rows[2][0]);
1730                         AssertEquals ("RowData2-1", "mono 3", dtLoad.Rows[2][1]);
1731                 }
1732
1733                 [Test]
1734                 public void Load_ExtraColsNonEqualVal () {
1735                         localSetup ();
1736                         DataTable dtLoad = new DataTable ("LoadExtraCols");
1737                         dtLoad.Columns.Add ("id", typeof (int));
1738                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1739                         dtLoad.Rows.Add (new object[] { 4 });
1740                         dtLoad.Rows.Add (new object[] { 5 });
1741                         dtLoad.Rows.Add (new object[] { 6 });
1742                         dtLoad.AcceptChanges ();
1743                         DataTableReader dtr = dt.CreateDataReader ();
1744                         dtLoad.Load (dtr);
1745                         AssertEquals ("NColumns", 2, dtLoad.Columns.Count);
1746                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1747                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1748                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1749                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1750                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1751                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1752                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1753                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1754                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1755                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1756                 }
1757
1758                 [Test]
1759                 [ExpectedException (typeof (ConstraintException))]
1760                 public void Load_MissingColsNonNullable () {
1761                         localSetup ();
1762                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1763                         dtLoad.Columns.Add ("id", typeof (int));
1764                         dtLoad.Columns.Add ("name", typeof (string));
1765                         dtLoad.Columns.Add ("missing", typeof (string));
1766                         dtLoad.Columns["missing"].AllowDBNull = false;
1767                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1768                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1769                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1770                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1771                         dtLoad.AcceptChanges ();
1772                         DataTableReader dtr = dt.CreateDataReader ();
1773                         dtLoad.Load (dtr);
1774                 }
1775
1776                 [Test]
1777                 public void Load_MissingColsDefault () {
1778                         localSetup ();
1779                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1780                         dtLoad.Columns.Add ("id", typeof (int));
1781                         dtLoad.Columns.Add ("name", typeof (string));
1782                         dtLoad.Columns.Add ("missing", typeof (string));
1783                         dtLoad.Columns["missing"].AllowDBNull = false;
1784                         dtLoad.Columns["missing"].DefaultValue = "DefaultValue";
1785                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1786                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1787                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1788                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1789                         dtLoad.AcceptChanges ();
1790                         DataTableReader dtr = dt.CreateDataReader ();
1791                         dtLoad.Load (dtr);
1792                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1793                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1794                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1795                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1796                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1797                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1798                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1799                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1800                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1801                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1802                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1803                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1804                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1805                         AssertEquals ("RowData3-2", "DefaultValue", dtLoad.Rows[3][2]);
1806                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1807                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1808                         AssertEquals ("RowData4-2", "DefaultValue", dtLoad.Rows[4][2]);
1809                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1810                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1811                         AssertEquals ("RowData5-2", "DefaultValue", dtLoad.Rows[5][2]);
1812                 }
1813
1814                 [Test]
1815                 public void Load_MissingColsNullable () {
1816                         localSetup ();
1817                         DataTable dtLoad = new DataTable ("LoadMissingCols");
1818                         dtLoad.Columns.Add ("id", typeof (int));
1819                         dtLoad.Columns.Add ("name", typeof (string));
1820                         dtLoad.Columns.Add ("missing", typeof (string));
1821                         dtLoad.Columns["missing"].AllowDBNull = true;
1822                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1823                         dtLoad.Rows.Add (new object[] { 4, "mono 4", "miss4" });
1824                         dtLoad.Rows.Add (new object[] { 5, "mono 5", "miss5" });
1825                         dtLoad.Rows.Add (new object[] { 6, "mono 6", "miss6" });
1826                         dtLoad.AcceptChanges ();
1827                         DataTableReader dtr = dt.CreateDataReader ();
1828                         dtLoad.Load (dtr);
1829                         AssertEquals ("NColumns", 3, dtLoad.Columns.Count);
1830                         AssertEquals ("NRows", 6, dtLoad.Rows.Count);
1831                         AssertEquals ("RowData0-0", 4, dtLoad.Rows[0][0]);
1832                         AssertEquals ("RowData0-1", "mono 4", dtLoad.Rows[0][1]);
1833                         AssertEquals ("RowData0-2", "miss4", dtLoad.Rows[0][2]);
1834                         AssertEquals ("RowData1-0", 5, dtLoad.Rows[1][0]);
1835                         AssertEquals ("RowData1-1", "mono 5", dtLoad.Rows[1][1]);
1836                         AssertEquals ("RowData1-2", "miss5", dtLoad.Rows[1][2]);
1837                         AssertEquals ("RowData2-0", 6, dtLoad.Rows[2][0]);
1838                         AssertEquals ("RowData2-1", "mono 6", dtLoad.Rows[2][1]);
1839                         AssertEquals ("RowData2-2", "miss6", dtLoad.Rows[2][2]);
1840                         AssertEquals ("RowData3-0", 1, dtLoad.Rows[3][0]);
1841                         AssertEquals ("RowData3-1", "mono 1", dtLoad.Rows[3][1]);
1842                         //AssertEquals ("RowData3-2", null, dtLoad.Rows[3][2]);
1843                         AssertEquals ("RowData4-0", 2, dtLoad.Rows[4][0]);
1844                         AssertEquals ("RowData4-1", "mono 2", dtLoad.Rows[4][1]);
1845                         //AssertEquals ("RowData4-2", null, dtLoad.Rows[4][2]);
1846                         AssertEquals ("RowData5-0", 3, dtLoad.Rows[5][0]);
1847                         AssertEquals ("RowData5-1", "mono 3", dtLoad.Rows[5][1]);
1848                         //AssertEquals ("RowData5-2", null, dtLoad.Rows[5][2]);
1849                 }
1850
1851                 private DataTable setupRowState () {
1852                         DataTable tbl = new DataTable ("LoadRowStateChanges");
1853                         tbl.RowChanged += new DataRowChangeEventHandler (dtLoad_RowChanged);
1854                         tbl.RowChanging += new DataRowChangeEventHandler (dtLoad_RowChanging);
1855                         tbl.Columns.Add ("id", typeof (int));
1856                         tbl.Columns.Add ("name", typeof (string));
1857                         tbl.PrimaryKey = new DataColumn[] { tbl.Columns["id"] };
1858                         tbl.Rows.Add (new object[] { 1, "RowState 1" });
1859                         tbl.Rows.Add (new object[] { 2, "RowState 2" });
1860                         tbl.Rows.Add (new object[] { 3, "RowState 3" });
1861                         tbl.AcceptChanges ();
1862                         // Update Table with following changes: Row0 unmodified, 
1863                         // Row1 modified, Row2 deleted, Row3 added, Row4 not-present.
1864                         tbl.Rows[1]["name"] = "Modify 2";
1865                         tbl.Rows[2].Delete ();
1866                         DataRow row = tbl.NewRow ();
1867                         row["id"] = 4;
1868                         row["name"] = "Add 4";
1869                         tbl.Rows.Add (row);
1870                         return (tbl);
1871                 }
1872
1873                 private DataRowAction[] rowChangeAction = new DataRowAction[5];
1874                 private bool checkAction = false;
1875                 private int rowChagedCounter, rowChangingCounter;
1876                 private void rowActionInit (DataRowAction[] act) {
1877                         checkAction = true;
1878                         rowChagedCounter = 0;
1879                         rowChangingCounter = 0;
1880                         for (int i = 0; i < 5; i++)
1881                                 rowChangeAction[i] = act[i];
1882                 }
1883                 private void rowActionEnd () {
1884                         checkAction = false;
1885                 }
1886                 private void dtLoad_RowChanged (object sender, DataRowChangeEventArgs e) {
1887                         if (checkAction) {
1888                                 AssertEquals ("RowChanged" + rowChagedCounter,
1889                                         rowChangeAction[rowChagedCounter], e.Action);
1890                                 rowChagedCounter++;
1891                         }
1892                 }
1893                 private void dtLoad_RowChanging (object sender, DataRowChangeEventArgs e) {
1894                         if (checkAction) {
1895                                 AssertEquals ("RowChanging" + rowChangingCounter,
1896                                         rowChangeAction[rowChangingCounter], e.Action);
1897                                 rowChangingCounter++;
1898                         }
1899                 }
1900
1901                 [Test]
1902                 [Category ("NotWorking")]
1903                 public void Load_RowStateChangesDefault () {
1904                         localSetup ();
1905                         dt.Rows.Add (new object[] { 4, "mono 4" });
1906                         dt.Rows.Add (new object[] { 5, "mono 5" });
1907                         dt.AcceptChanges ();
1908                         DataTableReader dtr = dt.CreateDataReader ();
1909                         DataTable dtLoad = setupRowState ();
1910                         DataRowAction[] dra = new DataRowAction[] {
1911                                 DataRowAction.ChangeCurrentAndOriginal,
1912                                 DataRowAction.ChangeOriginal,
1913                                 DataRowAction.ChangeOriginal,
1914                                 DataRowAction.ChangeOriginal,
1915                                 DataRowAction.ChangeCurrentAndOriginal};
1916                         rowActionInit (dra);
1917                         dtLoad.Load (dtr);
1918                         rowActionEnd ();
1919                         // asserting Unchanged Row0
1920                         AssertEquals ("RowData0-C", "mono 1",
1921                                 dtLoad.Rows[0][1,DataRowVersion.Current]);
1922                         AssertEquals ("RowData0-O", "mono 1",
1923                                 dtLoad.Rows[0][1,DataRowVersion.Original]);
1924                         AssertEquals ("RowState0", DataRowState.Unchanged,
1925                                 dtLoad.Rows[0].RowState);
1926                         // asserting Modified Row1
1927                         AssertEquals ("RowData1-C", "Modify 2",
1928                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
1929                         AssertEquals ("RowData1-O", "mono 2",
1930                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
1931                         AssertEquals ("RowState1", DataRowState.Modified,
1932                                 dtLoad.Rows[1].RowState);
1933                         // asserting Deleted Row2
1934                         AssertEquals ("RowData1-O", "mono 3",
1935                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
1936                         AssertEquals ("RowState2", DataRowState.Deleted,
1937                                 dtLoad.Rows[2].RowState);
1938                         // asserting Added Row3
1939                         AssertEquals ("RowData3-C", "Add 4",
1940                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
1941                         AssertEquals ("RowData3-O", "mono 4",
1942                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
1943                         AssertEquals ("RowState3", DataRowState.Modified,
1944                                 dtLoad.Rows[3].RowState);
1945                         // asserting Unpresent Row4
1946                         AssertEquals ("RowData4-C", "mono 5",
1947                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
1948                         AssertEquals ("RowData4-O", "mono 5",
1949                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
1950                         AssertEquals ("RowState4", DataRowState.Unchanged,
1951                                 dtLoad.Rows[4].RowState);
1952                 }
1953
1954                 [Test]
1955                 [ExpectedException (typeof (VersionNotFoundException))]
1956                 [Category ("NotWorking")]
1957                 public void Load_RowStateChangesDefaultDelete () {
1958                         localSetup ();
1959                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
1960                         dtLoad.Columns.Add ("id", typeof (int));
1961                         dtLoad.Columns.Add ("name", typeof (string));
1962                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
1963                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
1964                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
1965                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
1966                         dtLoad.AcceptChanges ();
1967                         dtLoad.Rows[2].Delete ();
1968                         DataTableReader dtr = dt.CreateDataReader ();
1969                         dtLoad.Load (dtr);
1970                         AssertEquals ("RowData2-C", " ",
1971                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
1972                 }
1973
1974                 [Test]
1975                 [Category ("NotWorking")]
1976                 public void Load_RowStatePreserveChanges () {
1977                         localSetup ();
1978                         dt.Rows.Add (new object[] { 4, "mono 4" });
1979                         dt.Rows.Add (new object[] { 5, "mono 5" });
1980                         dt.AcceptChanges ();
1981                         DataTableReader dtr = dt.CreateDataReader ();
1982                         DataTable dtLoad = setupRowState ();
1983                         DataRowAction[] dra = new DataRowAction[] {
1984                                 DataRowAction.ChangeCurrentAndOriginal,
1985                                 DataRowAction.ChangeOriginal,
1986                                 DataRowAction.ChangeOriginal,
1987                                 DataRowAction.ChangeOriginal,
1988                                 DataRowAction.ChangeCurrentAndOriginal};
1989                         rowActionInit (dra);
1990                         dtLoad.Load (dtr, LoadOption.PreserveChanges);
1991                         rowActionEnd ();
1992                         // asserting Unchanged Row0
1993                         AssertEquals ("RowData0-C", "mono 1",
1994                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
1995                         AssertEquals ("RowData0-O", "mono 1",
1996                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
1997                         AssertEquals ("RowState0", DataRowState.Unchanged,
1998                                 dtLoad.Rows[0].RowState);
1999                         // asserting Modified Row1
2000                         AssertEquals ("RowData1-C", "Modify 2",
2001                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
2002                         AssertEquals ("RowData1-O", "mono 2",
2003                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2004                         AssertEquals ("RowState1", DataRowState.Modified,
2005                                 dtLoad.Rows[1].RowState);
2006                         // asserting Deleted Row2
2007                         AssertEquals ("RowData1-O", "mono 3",
2008                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2009                         AssertEquals ("RowState2", DataRowState.Deleted,
2010                                 dtLoad.Rows[2].RowState);
2011                         // asserting Added Row3
2012                         AssertEquals ("RowData3-C", "Add 4",
2013                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2014                         AssertEquals ("RowData3-O", "mono 4",
2015                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2016                         AssertEquals ("RowState3", DataRowState.Modified,
2017                                 dtLoad.Rows[3].RowState);
2018                         // asserting Unpresent Row4
2019                         AssertEquals ("RowData4-C", "mono 5",
2020                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2021                         AssertEquals ("RowData4-O", "mono 5",
2022                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
2023                         AssertEquals ("RowState4", DataRowState.Unchanged,
2024                                 dtLoad.Rows[4].RowState);
2025                 }
2026
2027                 [Test]
2028                 [ExpectedException (typeof (VersionNotFoundException))]
2029                 [Category ("NotWorking")]
2030                 public void Load_RowStatePreserveChangesDelete () {
2031                         localSetup ();
2032                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2033                         dtLoad.Columns.Add ("id", typeof (int));
2034                         dtLoad.Columns.Add ("name", typeof (string));
2035                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2036                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2037                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2038                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2039                         dtLoad.AcceptChanges ();
2040                         dtLoad.Rows[2].Delete ();
2041                         DataTableReader dtr = dt.CreateDataReader ();
2042                         dtLoad.Load (dtr,LoadOption.PreserveChanges);
2043                         AssertEquals ("RowData2-C", " ",
2044                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2045                 }
2046
2047                 [Test]
2048                 [Category ("NotWorking")]
2049                 public void Load_RowStateOverwriteChanges () {
2050                         localSetup ();
2051                         dt.Rows.Add (new object[] { 4, "mono 4" });
2052                         dt.Rows.Add (new object[] { 5, "mono 5" });
2053                         dt.AcceptChanges ();
2054                         DataTableReader dtr = dt.CreateDataReader ();
2055                         DataTable dtLoad = setupRowState ();
2056                         DataRowAction[] dra = new DataRowAction[] {
2057                                 DataRowAction.ChangeCurrentAndOriginal,
2058                                 DataRowAction.ChangeCurrentAndOriginal,
2059                                 DataRowAction.ChangeCurrentAndOriginal,
2060                                 DataRowAction.ChangeCurrentAndOriginal,
2061                                 DataRowAction.ChangeCurrentAndOriginal};
2062                         rowActionInit (dra);
2063                         dtLoad.Load (dtr, LoadOption.OverwriteChanges);
2064                         rowActionEnd ();
2065                         // asserting Unchanged Row0
2066                         AssertEquals ("RowData0-C", "mono 1",
2067                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2068                         AssertEquals ("RowData0-O", "mono 1",
2069                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2070                         AssertEquals ("RowState0", DataRowState.Unchanged,
2071                                 dtLoad.Rows[0].RowState);
2072                         // asserting Modified Row1
2073                         AssertEquals ("RowData1-C", "mono 2",
2074                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
2075                         AssertEquals ("RowData1-O", "mono 2",
2076                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2077                         AssertEquals ("RowState1", DataRowState.Unchanged,
2078                                 dtLoad.Rows[1].RowState);
2079                         // asserting Deleted Row2
2080                         AssertEquals ("RowData1-C", "mono 3",
2081                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2082                         AssertEquals ("RowData1-O", "mono 3",
2083                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2084                         AssertEquals ("RowState2", DataRowState.Unchanged,
2085                                 dtLoad.Rows[2].RowState);
2086                         // asserting Added Row3
2087                         AssertEquals ("RowData3-C", "mono 4",
2088                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2089                         AssertEquals ("RowData3-O", "mono 4",
2090                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2091                         AssertEquals ("RowState3", DataRowState.Unchanged,
2092                                 dtLoad.Rows[3].RowState);
2093                         // asserting Unpresent Row4
2094                         AssertEquals ("RowData4-C", "mono 5",
2095                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2096                         AssertEquals ("RowData4-O", "mono 5",
2097                                 dtLoad.Rows[4][1, DataRowVersion.Original]);
2098                         AssertEquals ("RowState4", DataRowState.Unchanged,
2099                                 dtLoad.Rows[4].RowState);
2100                 }
2101
2102                 [Test]
2103                 [Category ("NotWorking")]
2104                 public void Load_RowStateUpsert () {
2105                         localSetup ();
2106                         dt.Rows.Add (new object[] { 4, "mono 4" });
2107                         dt.Rows.Add (new object[] { 5, "mono 5" });
2108                         dt.AcceptChanges ();
2109                         DataTableReader dtr = dt.CreateDataReader ();
2110                         DataTable dtLoad = setupRowState ();
2111                         // Notice rowChange-Actions only occur 5 times, as number 
2112                         // of actual rows, ignoring row duplication of the deleted row.
2113                         DataRowAction[] dra = new DataRowAction[] {
2114                                 DataRowAction.Change,
2115                                 DataRowAction.Change,
2116                                 DataRowAction.Add,
2117                                 DataRowAction.Change,
2118                                 DataRowAction.Add};
2119                         rowActionInit (dra);
2120                         dtLoad.Load (dtr, LoadOption.Upsert);
2121                         rowActionEnd ();
2122                         // asserting Unchanged Row0
2123                         AssertEquals ("RowData0-C", "mono 1",
2124                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2125                         AssertEquals ("RowData0-O", "RowState 1",
2126                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2127                         AssertEquals ("RowState0", DataRowState.Modified,
2128                                 dtLoad.Rows[0].RowState);
2129                         // asserting Modified Row1
2130                         AssertEquals ("RowData1-C", "mono 2",
2131                                 dtLoad.Rows[1][1, DataRowVersion.Current]);
2132                         AssertEquals ("RowData1-O", "RowState 2",
2133                                 dtLoad.Rows[1][1, DataRowVersion.Original]);
2134                         AssertEquals ("RowState1", DataRowState.Modified,
2135                                 dtLoad.Rows[1].RowState);
2136                         // asserting Deleted Row2 and "Deleted-Added" Row4
2137                         AssertEquals ("RowData2-O", "RowState 3",
2138                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2139                         AssertEquals ("RowState2", DataRowState.Deleted,
2140                                 dtLoad.Rows[2].RowState);
2141                         AssertEquals ("RowData4-C", "mono 3",
2142                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2143                         AssertEquals ("RowState4", DataRowState.Added,
2144                                 dtLoad.Rows[4].RowState);
2145                         // asserting Added Row3
2146                         AssertEquals ("RowData3-C", "mono 4",
2147                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2148                         AssertEquals ("RowState3", DataRowState.Added,
2149                                 dtLoad.Rows[3].RowState);
2150                         // asserting Unpresent Row5
2151                         // Notice row4 is used for added row of deleted row2 and so
2152                         // unpresent row4 moves to row5
2153                         AssertEquals ("RowData5-C", "mono 5",
2154                                 dtLoad.Rows[5][1, DataRowVersion.Current]);
2155                         AssertEquals ("RowState5", DataRowState.Added,
2156                                 dtLoad.Rows[5].RowState);
2157                 }
2158
2159                 [Test]
2160                 [Category ("NotWorking")]
2161                 public void Load_RowStateUpsertDuplicateKey1 () {
2162                         localSetup ();
2163                         dt.Rows.Add (new object[] { 4, "mono 4" });
2164                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2165                         dtLoad.Columns.Add ("id", typeof (int));
2166                         dtLoad.Columns.Add ("name", typeof (string));
2167                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2168                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2169                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2170                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2171                         dtLoad.AcceptChanges ();
2172                         dtLoad.Rows[2].Delete ();
2173                         DataTableReader dtr = dt.CreateDataReader ();
2174                         dtLoad.Load (dtr, LoadOption.Upsert);
2175                         dtLoad.Rows[3][1] = "NEWVAL";
2176                         AssertEquals ("A-RowState2", DataRowState.Deleted,
2177                                 dtLoad.Rows[2].RowState);
2178                         AssertEquals ("A-RowData2-id", 3,
2179                                 dtLoad.Rows[2][0, DataRowVersion.Original]);
2180                         AssertEquals ("A-RowData2-name", "RowState 3",
2181                                 dtLoad.Rows[2][1, DataRowVersion.Original]);
2182                         AssertEquals ("A-RowState3", DataRowState.Added,
2183                                 dtLoad.Rows[3].RowState);
2184                         AssertEquals ("A-RowData3-id", 3,
2185                                 dtLoad.Rows[3][0, DataRowVersion.Current]);
2186                         AssertEquals ("A-RowData3-name", "NEWVAL",
2187                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2188                         AssertEquals ("A-RowState4", DataRowState.Added,
2189                                 dtLoad.Rows[4].RowState);
2190                         AssertEquals ("A-RowData4-id", 4,
2191                                 dtLoad.Rows[4][0, DataRowVersion.Current]);
2192                         AssertEquals ("A-RowData4-name", "mono 4",
2193                                 dtLoad.Rows[4][1, DataRowVersion.Current]);
2194
2195                         dtLoad.AcceptChanges ();
2196
2197                         AssertEquals ("B-RowState2", DataRowState.Unchanged,
2198                                 dtLoad.Rows[2].RowState);
2199                         AssertEquals ("B-RowData2-id", 3,
2200                                 dtLoad.Rows[2][0, DataRowVersion.Current]);
2201                         AssertEquals ("B-RowData2-name", "NEWVAL",
2202                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2203                         AssertEquals ("B-RowState3", DataRowState.Unchanged,
2204                                 dtLoad.Rows[3].RowState);
2205                         AssertEquals ("B-RowData3-id", 4,
2206                                 dtLoad.Rows[3][0, DataRowVersion.Current]);
2207                         AssertEquals ("B-RowData3-name", "mono 4",
2208                                 dtLoad.Rows[3][1, DataRowVersion.Current]);
2209                 }
2210
2211                 [Test]
2212                 [ExpectedException (typeof (IndexOutOfRangeException))]
2213                 [Category ("NotWorking")]
2214                 public void Load_RowStateUpsertDuplicateKey2 () {
2215                         localSetup ();
2216                         dt.Rows.Add (new object[] { 4, "mono 4" });
2217                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2218                         dtLoad.Columns.Add ("id", typeof (int));
2219                         dtLoad.Columns.Add ("name", typeof (string));
2220                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2221                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2222                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2223                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2224                         dtLoad.AcceptChanges ();
2225                         dtLoad.Rows[2].Delete ();
2226                         DataTableReader dtr = dt.CreateDataReader ();
2227                         dtLoad.Load (dtr, LoadOption.Upsert);
2228                         dtLoad.AcceptChanges ();
2229                         AssertEquals ("RowData4", " ", dtLoad.Rows[4][1]);
2230                 }
2231
2232                 [Test]
2233                 [ExpectedException (typeof (VersionNotFoundException))]
2234                 [Category ("NotWorking")]
2235                 public void Load_RowStateUpsertDelete1 () {
2236                         localSetup ();
2237                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2238                         dtLoad.Columns.Add ("id", typeof (int));
2239                         dtLoad.Columns.Add ("name", typeof (string));
2240                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2241                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2242                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2243                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2244                         dtLoad.AcceptChanges ();
2245                         dtLoad.Rows[2].Delete ();
2246                         DataTableReader dtr = dt.CreateDataReader ();
2247                         dtLoad.Load (dtr, LoadOption.Upsert);
2248                         AssertEquals ("RowData2-C", " ",
2249                                 dtLoad.Rows[2][1, DataRowVersion.Current]);
2250                 }
2251
2252                 [Test]
2253                 [ExpectedException (typeof (VersionNotFoundException))]
2254                 [Category ("NotWorking")]
2255                 public void Load_RowStateUpsertDelete2 () {
2256                         localSetup ();
2257                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2258                         dtLoad.Columns.Add ("id", typeof (int));
2259                         dtLoad.Columns.Add ("name", typeof (string));
2260                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2261                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2262                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2263                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2264                         dtLoad.AcceptChanges ();
2265                         dtLoad.Rows[2].Delete ();
2266                         DataTableReader dtr = dt.CreateDataReader ();
2267                         dtLoad.Load (dtr, LoadOption.Upsert);
2268                         AssertEquals ("RowData3-O", " ",
2269                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2270                 }
2271
2272                 [Test]
2273                 [ExpectedException (typeof (VersionNotFoundException))]
2274                 public void Load_RowStateUpsertAdd () {
2275                         localSetup ();
2276                         dt.Rows.Add (new object[] { 4, "mono 4" });
2277                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2278                         dtLoad.Columns.Add ("id", typeof (int));
2279                         dtLoad.Columns.Add ("name", typeof (string));
2280                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2281                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2282                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2283                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2284                         dtLoad.AcceptChanges ();
2285                         DataRow row = dtLoad.NewRow ();
2286                         row["id"] = 4;
2287                         row["name"] = "Add 4";
2288                         dtLoad.Rows.Add (row);
2289                         DataTableReader dtr = dt.CreateDataReader ();
2290                         dtLoad.Load (dtr, LoadOption.Upsert);
2291                         AssertEquals ("RowData3-O", " ",
2292                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2293                 }
2294
2295                 [Test]
2296                 [ExpectedException (typeof (VersionNotFoundException))]
2297                 public void Load_RowStateUpsertUnpresent () {
2298                         localSetup ();
2299                         dt.Rows.Add (new object[] { 4, "mono 4" });
2300                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2301                         dtLoad.Columns.Add ("id", typeof (int));
2302                         dtLoad.Columns.Add ("name", typeof (string));
2303                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2304                         dtLoad.Rows.Add (new object[] { 1, "RowState 1" });
2305                         dtLoad.Rows.Add (new object[] { 2, "RowState 2" });
2306                         dtLoad.Rows.Add (new object[] { 3, "RowState 3" });
2307                         dtLoad.AcceptChanges ();
2308                         DataTableReader dtr = dt.CreateDataReader ();
2309                         dtLoad.Load (dtr, LoadOption.Upsert);
2310                         AssertEquals ("RowData3-O", " ",
2311                                 dtLoad.Rows[3][1, DataRowVersion.Original]);
2312                 }
2313
2314                 [Test]
2315                 public void Load_RowStateUpsertUnchangedEqualVal () {
2316                         localSetup ();
2317                         DataTable dtLoad = new DataTable ("LoadRowStateChanges");
2318                         dtLoad.Columns.Add ("id", typeof (int));
2319                         dtLoad.Columns.Add ("name", typeof (string));
2320                         dtLoad.PrimaryKey = new DataColumn[] { dtLoad.Columns["id"] };
2321                         dtLoad.Rows.Add (new object[] { 1, "mono 1" });
2322                         dtLoad.AcceptChanges ();
2323                         DataTableReader dtr = dt.CreateDataReader ();
2324                         DataRowAction[] dra = new DataRowAction[] {
2325                                 DataRowAction.Nothing,// REAL action
2326                                 DataRowAction.Nothing,// dummy  
2327                                 DataRowAction.Nothing,// dummy  
2328                                 DataRowAction.Nothing,// dummy  
2329                                 DataRowAction.Nothing};// dummy  
2330                         rowActionInit (dra);
2331                         dtLoad.Load (dtr, LoadOption.Upsert);
2332                         rowActionEnd ();
2333                         AssertEquals ("RowData0-C", "mono 1",
2334                                 dtLoad.Rows[0][1, DataRowVersion.Current]);
2335                         AssertEquals ("RowData0-O", "mono 1",
2336                                 dtLoad.Rows[0][1, DataRowVersion.Original]);
2337                         AssertEquals ("RowState0", DataRowState.Unchanged,
2338                                 dtLoad.Rows[0].RowState);
2339                 }
2340
2341                 [Test]
2342                 public void LoadDataRow_LoadOptions () {
2343                         // LoadDataRow is covered in detail (without LoadOptions) in DataTableTest2
2344                         // LoadOption tests are covered in detail in DataTable.Load().
2345                         // Therefore only minimal tests of LoadDataRow with LoadOptions are covered here.
2346                         DataTable dt;
2347                         DataRow dr;
2348                         dt = CreateDataTableExample ();
2349                         dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };     //add ParentId as Primary Key
2350                         dt.Columns["String1"].DefaultValue = "Default";
2351
2352                         dr = dt.Select ("ParentId=1")[0];
2353
2354                         //Update existing row with LoadOptions = OverwriteChanges
2355                         dt.BeginLoadData ();
2356                         dt.LoadDataRow (new object[] { 1, null, "Changed" },
2357                                 LoadOption.OverwriteChanges);
2358                         dt.EndLoadData ();
2359
2360                         // LoadDataRow(update1) - check column String2
2361                         AssertEquals ("DT72-C", "Changed",
2362                                 dr["String2", DataRowVersion.Current]);
2363                         AssertEquals ("DT72-O", "Changed",
2364                                 dr["String2", DataRowVersion.Original]);
2365
2366                         // LoadDataRow(update1) - check row state
2367                         AssertEquals ("DT73-LO", DataRowState.Unchanged, dr.RowState);
2368
2369                         //Add New row with LoadOptions = Upsert
2370                         dt.BeginLoadData ();
2371                         dt.LoadDataRow (new object[] { 99, null, "Changed" },
2372                                 LoadOption.Upsert);
2373                         dt.EndLoadData ();
2374
2375                         // LoadDataRow(insert1) - check column String2
2376                         dr = dt.Select ("ParentId=99")[0];
2377                         AssertEquals ("DT75-C", "Changed",
2378                                 dr["String2", DataRowVersion.Current]);
2379
2380                         // LoadDataRow(insert1) - check row state
2381                         AssertEquals ("DT76-LO", DataRowState.Added, dr.RowState);
2382                 }
2383
2384                 public static DataTable CreateDataTableExample () {
2385                         DataTable dtParent = new DataTable ("Parent");
2386
2387                         dtParent.Columns.Add ("ParentId", typeof (int));
2388                         dtParent.Columns.Add ("String1", typeof (string));
2389                         dtParent.Columns.Add ("String2", typeof (string));
2390
2391                         dtParent.Columns.Add ("ParentDateTime", typeof (DateTime));
2392                         dtParent.Columns.Add ("ParentDouble", typeof (double));
2393                         dtParent.Columns.Add ("ParentBool", typeof (bool));
2394
2395                         dtParent.Rows.Add (new object[] { 1, "1-String1", "1-String2", new DateTime (2005, 1, 1, 0, 0, 0, 0), 1.534, true });
2396                         dtParent.Rows.Add (new object[] { 2, "2-String1", "2-String2", new DateTime (2004, 1, 1, 0, 0, 0, 1), -1.534, true });
2397                         dtParent.Rows.Add (new object[] { 3, "3-String1", "3-String2", new DateTime (2003, 1, 1, 0, 0, 1, 0), double.MinValue * 10000, false });
2398                         dtParent.Rows.Add (new object[] { 4, "4-String1", "4-String2", new DateTime (2002, 1, 1, 0, 1, 0, 0), double.MaxValue / 10000, true });
2399                         dtParent.Rows.Add (new object[] { 5, "5-String1", "5-String2", new DateTime (2001, 1, 1, 1, 0, 0, 0), 0.755, true });
2400                         dtParent.Rows.Add (new object[] { 6, "6-String1", "6-String2", new DateTime (2000, 1, 1, 0, 0, 0, 0), 0.001, false });
2401                         dtParent.AcceptChanges ();
2402                         return dtParent;
2403                 }
2404
2405                 #endregion // DataTable.Load Tests
2406
2407                 #region Read/Write XML Tests
2408
2409                 [Test]
2410                 [Category ("NotWorking")]
2411                 public void ReadXmlSchema () {
2412                         DataTable Table = new DataTable ();
2413                         Table.ReadXmlSchema ("Test/System.Data/own_schema1.xsd");
2414
2415                         AssertEquals ("test#02", "test_table", Table.TableName);
2416                         AssertEquals ("test#03", "", Table.Namespace);
2417                         AssertEquals ("test#04", 2, Table.Columns.Count);
2418                         AssertEquals ("test#05", 0, Table.Rows.Count);
2419                         AssertEquals ("test#06", false, Table.CaseSensitive);
2420                         AssertEquals ("test#07", 1, Table.Constraints.Count);
2421                         AssertEquals ("test#08", "", Table.Prefix);
2422
2423                         Constraint cons = Table.Constraints[0];
2424                         AssertEquals ("test#09", "Constraint1", cons.ConstraintName.ToString ());
2425                         AssertEquals ("test#10", "Constraint1", cons.ToString ());
2426
2427                         DataColumn column = Table.Columns[0];
2428                         AssertEquals ("test#11", true, column.AllowDBNull);
2429                         AssertEquals ("test#12", false, column.AutoIncrement);
2430                         AssertEquals ("test#13", 0L, column.AutoIncrementSeed);
2431                         AssertEquals ("test#14", 1L, column.AutoIncrementStep);
2432                         AssertEquals ("test#15", "test", column.Caption);
2433                         AssertEquals ("test#16", "Element", column.ColumnMapping.ToString ());
2434                         AssertEquals ("test#17", "first", column.ColumnName);
2435                         AssertEquals ("test#18", "System.String", column.DataType.ToString ());
2436                         AssertEquals ("test#19", "test_default_value", column.DefaultValue.ToString ());
2437                         AssertEquals ("test#20", false, column.DesignMode);
2438                         AssertEquals ("test#21", "", column.Expression);
2439                         AssertEquals ("test#22", 100, column.MaxLength);
2440                         AssertEquals ("test#23", "", column.Namespace);
2441                         AssertEquals ("test#24", 0, column.Ordinal);
2442                         AssertEquals ("test#25", "", column.Prefix);
2443                         AssertEquals ("test#26", false, column.ReadOnly);
2444                         AssertEquals ("test#27", true, column.Unique);
2445
2446                         DataColumn column2 = Table.Columns[1];
2447                         AssertEquals ("test#28", true, column2.AllowDBNull);
2448                         AssertEquals ("test#29", false, column2.AutoIncrement);
2449                         AssertEquals ("test#30", 0L, column2.AutoIncrementSeed);
2450                         AssertEquals ("test#31", 1L, column2.AutoIncrementStep);
2451                         AssertEquals ("test#32", "second", column2.Caption);
2452                         AssertEquals ("test#33", "Element", column2.ColumnMapping.ToString ());
2453                         AssertEquals ("test#34", "second", column2.ColumnName);
2454                         AssertEquals ("test#35", "System.Data.SqlTypes.SqlGuid", column2.DataType.ToString ());
2455                         AssertEquals ("test#36", "Null", column2.DefaultValue.ToString ());
2456                         AssertEquals ("test#37", false, column2.DesignMode);
2457                         AssertEquals ("test#38", "", column2.Expression);
2458                         AssertEquals ("test#39", -1, column2.MaxLength);
2459                         AssertEquals ("test#40", "", column2.Namespace);
2460                         AssertEquals ("test#41", 1, column2.Ordinal);
2461                         AssertEquals ("test#42", "", column2.Prefix);
2462                         AssertEquals ("test#43", false, column2.ReadOnly);
2463                         AssertEquals ("test#44", false, column2.Unique);
2464
2465                         DataTable Table2 = new DataTable ();
2466                         Table2.ReadXmlSchema ("Test/System.Data/own_schema2.xsd");
2467
2468                         AssertEquals ("test#45", "second_test_table", Table2.TableName);
2469                         AssertEquals ("test#46", "", Table2.Namespace);
2470                         AssertEquals ("test#47", 1, Table2.Columns.Count);
2471                         AssertEquals ("test#48", 0, Table2.Rows.Count);
2472                         AssertEquals ("test#49", false, Table2.CaseSensitive);
2473                         AssertEquals ("test#50", 1, Table2.Constraints.Count);
2474                         AssertEquals ("test#51", "", Table2.Prefix);
2475
2476                         DataColumn column3 = Table2.Columns[0];
2477                         AssertEquals ("test#52", true, column3.AllowDBNull);
2478                         AssertEquals ("test#53", false, column3.AutoIncrement);
2479                         AssertEquals ("test#54", 0L, column3.AutoIncrementSeed);
2480                         AssertEquals ("test#55", 1L, column3.AutoIncrementStep);
2481                         AssertEquals ("test#56", "second_first", column3.Caption);
2482                         AssertEquals ("test#57", "Element", column3.ColumnMapping.ToString ());
2483                         AssertEquals ("test#58", "second_first", column3.ColumnName);
2484                         AssertEquals ("test#59", "System.String", column3.DataType.ToString ());
2485                         AssertEquals ("test#60", "default_value", column3.DefaultValue.ToString ());
2486                         AssertEquals ("test#61", false, column3.DesignMode);
2487                         AssertEquals ("test#62", "", column3.Expression);
2488                         AssertEquals ("test#63", 100, column3.MaxLength);
2489                         AssertEquals ("test#64", "", column3.Namespace);
2490                         AssertEquals ("test#65", 0, column3.Ordinal);
2491                         AssertEquals ("test#66", "", column3.Prefix);
2492                         AssertEquals ("test#67", false, column3.ReadOnly);
2493                         AssertEquals ("test#68", true, column3.Unique);
2494                 }
2495
2496                 [Test]
2497                 public void ReadXmlSchema_2 () {
2498                         DataTable dt = new DataTable ();
2499                         string xmlData = string.Empty;
2500                         xmlData += "<?xml version=\"1.0\"?>";
2501                         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\">";
2502                         xmlData += "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2503                         xmlData += "<xs:complexType>";
2504                         xmlData += "<xs:choice maxOccurs=\"unbounded\">";
2505                         xmlData += "<xs:element name=\"Tab\">";
2506                         xmlData += "<xs:complexType>";
2507                         xmlData += "<xs:sequence>";
2508                         xmlData += "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2509                         xmlData += "<xs:complexType>";
2510                         xmlData += "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2511                         xmlData += "</xs:complexType>";
2512                         xmlData += "</xs:element>";
2513                         xmlData += "</xs:sequence>";
2514                         xmlData += "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2515                         xmlData += "</xs:complexType>";
2516                         xmlData += "</xs:element>";
2517                         xmlData += "</xs:choice>";
2518                         xmlData += "</xs:complexType>";
2519                         xmlData += "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2520                         xmlData += "<xs:selector xpath=\".//mstns:Tab\" />";
2521                         xmlData += "<xs:field xpath=\"@TabId\" />";
2522                         xmlData += "</xs:key>";
2523                         xmlData += "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2524                         xmlData += "<xs:selector xpath=\".//mstns:Module\" />";
2525                         xmlData += "<xs:field xpath=\"@ModuleID\" />";
2526                         xmlData += "</xs:key>";
2527                         xmlData += "</xs:element>";
2528                         xmlData += "</xs:schema>";
2529                         dt.ReadXmlSchema (new StringReader (xmlData));
2530                 }
2531
2532                 [Test]
2533                 public void ReadXmlSchema_ByStream () {
2534                         DataSet ds1 = new DataSet ();
2535                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2536                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2537
2538                         MemoryStream ms1 = new MemoryStream ();
2539                         MemoryStream ms2 = new MemoryStream ();
2540                         //write xml  schema only
2541                         //ds1.WriteXmlSchema (ms);
2542                         ds1.Tables[0].WriteXmlSchema (ms1);
2543                         ds1.Tables[1].WriteXmlSchema (ms2);
2544
2545                         MemoryStream ms11 = new MemoryStream (ms1.GetBuffer ());
2546                         MemoryStream ms22 = new MemoryStream (ms2.GetBuffer ());
2547                         //copy schema
2548                         //DataSet ds2 = new DataSet ();
2549                         DataTable dt1 = new DataTable ();
2550                         DataTable dt2 = new DataTable ();
2551
2552                         //ds2.ReadXmlSchema (ms1);
2553                         dt1.ReadXmlSchema (ms11);
2554                         dt2.ReadXmlSchema (ms22);
2555
2556                         //check xml schema
2557                         // ReadXmlSchema - Tables count
2558                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS269");
2559
2560                         // ReadXmlSchema - Tables 0 Col count
2561                         AssertEquals ("DS270", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2562
2563                         // ReadXmlSchema - Tables 1 Col count
2564                         AssertEquals ("DS271", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2565
2566                         //check some colummns types
2567                         // ReadXmlSchema - Tables 0 Col type
2568                         AssertEquals ("DS272", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2569
2570                         // ReadXmlSchema - Tables 1 Col type
2571                         AssertEquals ("DS273", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2572
2573                         //check that no data exists
2574                         // ReadXmlSchema - Table 1 row count
2575                         AssertEquals ("DS274",0, dt1.Rows.Count);
2576
2577                         // ReadXmlSchema - Table 2 row count
2578                         AssertEquals ("DS275",0, dt2.Rows.Count);
2579                 }
2580
2581                 [Test]
2582                 [Category ("NotWorking")]
2583                 public void ReadWriteXmlSchema_ByFileName () {
2584                         string sTempFileName1 = "tmpDataSet_ReadWriteXml_43899-1.xml";
2585                         string sTempFileName2 = "tmpDataSet_ReadWriteXml_43899-2.xml";
2586
2587                         DataSet ds1 = new DataSet ();
2588                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2589                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2590
2591                         ds1.Tables[0].WriteXmlSchema (sTempFileName1);
2592                         ds1.Tables[1].WriteXmlSchema (sTempFileName2);
2593
2594                         DataTable dt1 = new DataTable ();
2595                         DataTable dt2 = new DataTable ();
2596
2597                         dt1.ReadXmlSchema (sTempFileName1);
2598                         dt2.ReadXmlSchema (sTempFileName2);
2599
2600                         AssertEquals ("DS277", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2601                         AssertEquals ("DS278", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2602                         AssertEquals ("DS279", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2603                         AssertEquals ("DS280", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2604                         AssertEquals ("DS281", 0, dt1.Rows.Count);
2605                         AssertEquals ("DS282", 0, dt2.Rows.Count);
2606
2607                         File.Delete (sTempFileName1);
2608                         File.Delete (sTempFileName2);
2609                 }
2610
2611                 [Test]
2612                 public void ReadXmlSchema_ByTextReader () {
2613                         DataSet ds1 = new DataSet ();
2614                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2615                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2616
2617                         StringWriter sw1 = new StringWriter ();
2618                         StringWriter sw2 = new StringWriter ();
2619                         //write xml file, schema only
2620                         //ds1.WriteXmlSchema (sw);
2621                         ds1.Tables[0].WriteXmlSchema (sw1);
2622                         ds1.Tables[1].WriteXmlSchema (sw2);
2623
2624                         StringReader sr1 = new StringReader (sw1.GetStringBuilder ().ToString ());
2625                         StringReader sr2 = new StringReader (sw2.GetStringBuilder ().ToString ());
2626                         //copy both data and schema
2627                         //DataSet ds2 = new DataSet ();
2628                         DataTable dt1 = new DataTable ();
2629                         DataTable dt2 = new DataTable ();
2630
2631                         //ds2.ReadXmlSchema (sr);
2632                         dt1.ReadXmlSchema (sr1);
2633                         dt2.ReadXmlSchema (sr2);
2634
2635                         //check xml schema
2636                         // ReadXmlSchema - Tables count
2637                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS283");
2638
2639                         // ReadXmlSchema - Tables 0 Col count
2640                         AssertEquals ("DS284", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2641
2642                         // ReadXmlSchema - Tables 1 Col count
2643                         AssertEquals ("DS285", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2644
2645                         //check some colummns types
2646                         // ReadXmlSchema - Tables 0 Col type
2647                         AssertEquals ("DS286", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2648
2649                         // ReadXmlSchema - Tables 1 Col type
2650                         AssertEquals ("DS287", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2651
2652                         //check that no data exists
2653                         // ReadXmlSchema - Table 1 row count
2654                         AssertEquals ("DS288", 0, dt1.Rows.Count);
2655
2656                         // ReadXmlSchema - Table 2 row count
2657                         AssertEquals ("DS289", 0, dt2.Rows.Count);
2658                 }
2659
2660                 [Test]
2661                 public void ReadXmlSchema_ByXmlReader () {
2662                         DataSet ds1 = new DataSet ();
2663                         ds1.Tables.Add (DataProvider.CreateParentDataTable ());
2664                         ds1.Tables.Add (DataProvider.CreateChildDataTable ());
2665
2666                         StringWriter sw1 = new StringWriter ();
2667                         XmlTextWriter xmlTW1 = new XmlTextWriter (sw1);
2668                         StringWriter sw2 = new StringWriter ();
2669                         XmlTextWriter xmlTW2 = new XmlTextWriter (sw2);
2670
2671                         //write xml file, schema only
2672                         ds1.Tables[0].WriteXmlSchema (xmlTW1);
2673                         xmlTW1.Flush ();
2674                         ds1.Tables[1].WriteXmlSchema (xmlTW2);
2675                         xmlTW2.Flush ();
2676
2677                         StringReader sr1 = new StringReader (sw1.ToString ());
2678                         XmlTextReader xmlTR1 = new XmlTextReader (sr1);
2679                         StringReader sr2 = new StringReader (sw2.ToString ());
2680                         XmlTextReader xmlTR2 = new XmlTextReader (sr2);
2681
2682                         //copy both data and schema
2683                         //DataSet ds2 = new DataSet ();
2684                         DataTable dt1 = new DataTable ();
2685                         DataTable dt2 = new DataTable ();
2686
2687                         //ds2.ReadXmlSchema (xmlTR);
2688                         dt1.ReadXmlSchema (xmlTR1);
2689                         dt2.ReadXmlSchema (xmlTR2);
2690
2691                         //check xml schema
2692                         // ReadXmlSchema - Tables count
2693                         //Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DS290");
2694
2695                         // ReadXmlSchema - Tables 0 Col count
2696                         AssertEquals ("DS291", ds1.Tables[0].Columns.Count, dt1.Columns.Count);
2697
2698                         // ReadXmlSchema - Tables 1 Col count
2699                         AssertEquals ("DS292", ds1.Tables[1].Columns.Count, dt2.Columns.Count);
2700
2701                         //check some colummns types
2702                         // ReadXmlSchema - Tables 0 Col type
2703                         AssertEquals ("DS293", ds1.Tables[0].Columns[0].GetType (), dt1.Columns[0].GetType ());
2704
2705                         // ReadXmlSchema - Tables 1 Col type
2706                         AssertEquals ("DS294", ds1.Tables[1].Columns[3].GetType (), dt2.Columns[3].GetType ());
2707
2708                         //check that no data exists
2709                         // ReadXmlSchema - Table 1 row count
2710                         AssertEquals ("DS295", 0, dt1.Rows.Count);
2711
2712                         // ReadXmlSchema - Table 2 row count
2713                         AssertEquals ("DS296", 0, dt2.Rows.Count);
2714                 }
2715
2716                 [Test]
2717                 [Category ("NotWorking")]
2718                 public void WriteXmlSchema () {
2719                         DataSet ds = new DataSet ();
2720                         ds.ReadXml ("Test/System.Data/region.xml");
2721                         TextWriter writer = new StringWriter ();
2722                         ds.Tables[0].WriteXmlSchema (writer);
2723
2724
2725                         string TextString = GetNormalizedSchema (writer.ToString ());
2726                         //string TextString = writer.ToString ();
2727
2728                         string substring = TextString.Substring (0, TextString.IndexOf (EOL));
2729                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2730                         AssertEquals ("test#01", "<?xml version=\"1.0\" encoding=\"utf-16\"?>", substring);
2731
2732                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2733                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2734                         AssertEquals ("test#02", "<xs:schema id=\"Root\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">", substring);
2735
2736                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2737                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2738                         AssertEquals ("test#03", "  <xs:element msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Region\" msdata:UseCurrentLocale=\"true\" name=\"Root\">", substring);
2739
2740                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2741                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2742                         AssertEquals ("test#04", "    <xs:complexType>", substring);
2743
2744                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2745                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2746                         AssertEquals ("test#05", "      <xs:choice maxOccurs=\"unbounded\" minOccurs=\"0\">", substring);
2747
2748                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2749                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2750                         AssertEquals ("test#06", "        <xs:element name=\"Region\">", substring);
2751
2752                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2753                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2754                         AssertEquals ("test#07", "          <xs:complexType>", substring);
2755
2756                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2757                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2758                         AssertEquals ("test#08", "            <xs:sequence>", substring);
2759
2760                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2761                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2762                         AssertEquals ("test#09", "              <xs:element minOccurs=\"0\" name=\"RegionID\" type=\"xs:string\" />", substring);
2763
2764                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2765                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2766                         AssertEquals ("test#10", "              <xs:element minOccurs=\"0\" name=\"RegionDescription\" type=\"xs:string\" />", substring);
2767
2768                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2769                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2770                         AssertEquals ("test#11", "            </xs:sequence>", substring);
2771
2772                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2773                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2774                         AssertEquals ("test#12", "          </xs:complexType>", substring);
2775
2776                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2777                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2778                         AssertEquals ("test#13", "        </xs:element>", substring);
2779
2780                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2781                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2782                         AssertEquals ("test#14", "      </xs:choice>", substring);
2783
2784                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2785                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2786                         AssertEquals ("test#15", "    </xs:complexType>", substring);
2787
2788                         substring = TextString.Substring (0, TextString.IndexOf (EOL));
2789                         TextString = TextString.Substring (TextString.IndexOf (EOL) + EOL.Length);
2790                         AssertEquals ("test#16", "  </xs:element>", substring);
2791
2792                         AssertEquals ("test#17", "</xs:schema>", TextString);
2793                 }
2794
2795                 [Test]
2796                 [Category ("NotWorking")]
2797                 public void WriteXmlSchema2 () {
2798                         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>";
2799                         string schema = @"<?xml version='1.0' encoding='utf-16'?>
2800 <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'>
2801   <xs:element name='myDataSet' msdata:IsDataSet='true' msdata:MainDataTable='NetFrameWork_x003A_myTable' msdata:UseCurrentLocale='true'>
2802     <xs:complexType>
2803       <xs:choice minOccurs='0' maxOccurs='unbounded'>
2804         <xs:element name='myTable'>
2805           <xs:complexType>
2806             <xs:sequence>
2807               <xs:element name='id' msdata:AutoIncrement='true' type='xs:int' minOccurs='0' />
2808               <xs:element name='item' type='xs:string' minOccurs='0' />
2809             </xs:sequence>
2810           </xs:complexType>
2811         </xs:element>
2812       </xs:choice>
2813     </xs:complexType>
2814   </xs:element>
2815 </xs:schema>";
2816                         DataSet OriginalDataSet = new DataSet ("myDataSet");
2817                         OriginalDataSet.Namespace = "NetFrameWork";
2818                         DataTable myTable = new DataTable ("myTable");
2819                         DataColumn c1 = new DataColumn ("id", typeof (int));
2820                         c1.AutoIncrement = true;
2821                         DataColumn c2 = new DataColumn ("item");
2822                         myTable.Columns.Add (c1);
2823                         myTable.Columns.Add (c2);
2824                         OriginalDataSet.Tables.Add (myTable);
2825                         // Add ten rows.
2826                         DataRow newRow;
2827                         for (int i = 0; i < 10; i++) {
2828                                 newRow = myTable.NewRow ();
2829                                 newRow["item"] = "item " + i;
2830                                 myTable.Rows.Add (newRow);
2831                         }
2832                         OriginalDataSet.AcceptChanges ();
2833
2834                         StringWriter sw = new StringWriter ();
2835                         XmlTextWriter xtw = new XmlTextWriter (sw);
2836                         xtw.QuoteChar = '\'';
2837                         OriginalDataSet.WriteXml (xtw);
2838                         string result = sw.ToString ();
2839
2840                         AssertEquals (xml, result);
2841
2842                         sw = new StringWriter ();
2843                         xtw = new XmlTextWriter (sw);
2844                         xtw.Formatting = Formatting.Indented;
2845                         OriginalDataSet.Tables[0].WriteXmlSchema (xtw);
2846                         result = sw.ToString ();
2847
2848                         result = result.Replace ("\r\n", "\n").Replace ('"', '\'');
2849                         AssertEquals (schema.Replace ("\r\n", "\n"), result);
2850                 }
2851
2852                 [Test]
2853                 [Category ("NotWorking")]
2854                 public void WriteXmlSchema3 () {
2855                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2856 <xs:schema id=""ExampleDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2857   <xs:element name=""ExampleDataSet"" msdata:IsDataSet=""true"" msdata:MainDataTable=""ExampleDataTable"" msdata:UseCurrentLocale=""true"">
2858     <xs:complexType>
2859       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2860         <xs:element name=""ExampleDataTable"">
2861           <xs:complexType>
2862             <xs:attribute name=""PrimaryKeyColumn"" type=""xs:int"" use=""required"" />
2863           </xs:complexType>
2864         </xs:element>
2865       </xs:choice>
2866     </xs:complexType>
2867     <xs:unique name=""PK_ExampleDataTable"" msdata:PrimaryKey=""true"">
2868       <xs:selector xpath="".//ExampleDataTable"" />
2869       <xs:field xpath=""@PrimaryKeyColumn"" />
2870     </xs:unique>
2871   </xs:element>
2872 </xs:schema>";
2873                         DataSet ds = new DataSet ("ExampleDataSet");
2874
2875                         ds.Tables.Add (new DataTable ("ExampleDataTable"));
2876                         ds.Tables["ExampleDataTable"].Columns.Add (
2877                                 new DataColumn ("PrimaryKeyColumn", typeof (int), "", MappingType.Attribute));
2878                         ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"].AllowDBNull = false;
2879
2880                         ds.Tables["ExampleDataTable"].Constraints.Add (
2881                                 "PK_ExampleDataTable",
2882                                 ds.Tables["ExampleDataTable"].Columns["PrimaryKeyColumn"],
2883                                 true);
2884
2885                         ds.AcceptChanges ();
2886                         StringWriter sw = new StringWriter ();
2887                         ds.Tables[0].WriteXmlSchema (sw);
2888
2889                         string result = sw.ToString ();
2890
2891                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2892                         //AssertEquals (xmlschema, result.Replace ("\r\n", "\n"));
2893                 }
2894
2895                 [Test]
2896                 [Category ("NotWorking")]
2897                 public void WriteXmlSchema4 () {
2898                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
2899 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2900   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
2901     <xs:complexType>
2902       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2903         <xs:element name=""MyType"">
2904           <xs:complexType>
2905             <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2906             <xs:attribute name=""Desc"" type=""xs:string"" />
2907           </xs:complexType>
2908         </xs:element>
2909       </xs:choice>
2910     </xs:complexType>
2911   </xs:element>
2912 </xs:schema>";
2913                         DataSet ds = new DataSet ("Example");
2914
2915                         // Add MyType DataTable
2916                         DataTable dt = new DataTable ("MyType");
2917                         ds.Tables.Add (dt);
2918
2919                         dt.Columns.Add (new DataColumn ("ID", typeof (int), "",
2920                                 MappingType.Attribute));
2921                         dt.Columns["ID"].AllowDBNull = false;
2922
2923                         dt.Columns.Add (new DataColumn ("Desc", typeof
2924                                 (string), "", MappingType.Attribute));
2925
2926                         ds.AcceptChanges ();
2927
2928                         StringWriter sw = new StringWriter ();
2929                         ds.Tables[0].WriteXmlSchema (sw);
2930
2931                         string result = sw.ToString ();
2932
2933                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
2934                 }
2935
2936                 [Test]
2937                 [Category ("NotWorking")]
2938                 public void WriteXmlSchema5 () {
2939                         string xmlschema1 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2940 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2941   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""StandAlone"" msdata:UseCurrentLocale=""true"">
2942     <xs:complexType>
2943       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2944         <xs:element name=""StandAlone"">
2945           <xs:complexType>
2946             <xs:attribute name=""ID"" type=""xs:int"" use=""required"" />
2947             <xs:attribute name=""Desc"" type=""xs:string"" use=""required"" />
2948           </xs:complexType>
2949         </xs:element>
2950       </xs:choice>
2951     </xs:complexType>
2952   </xs:element>
2953 </xs:schema>";
2954                         string xmlschema2 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2955 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2956   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Dimension"" msdata:UseCurrentLocale=""true"">
2957     <xs:complexType>
2958       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2959         <xs:element name=""Dimension"">
2960           <xs:complexType>
2961             <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2962             <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2963           </xs:complexType>
2964         </xs:element>
2965       </xs:choice>
2966     </xs:complexType>
2967     <xs:unique name=""PK_Dimension"" msdata:PrimaryKey=""true"">
2968       <xs:selector xpath="".//Dimension"" />
2969       <xs:field xpath=""@Number"" />
2970     </xs:unique>
2971   </xs:element>
2972 </xs:schema>";
2973                         string xmlschema3 = @"<?xml version=""1.0"" encoding=""utf-16""?>
2974 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
2975   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""Element"" msdata:UseCurrentLocale=""true"">
2976     <xs:complexType>
2977       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
2978         <xs:element name=""Element"">
2979           <xs:complexType>
2980             <xs:attribute name=""Dimension"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2981             <xs:attribute name=""Number"" msdata:ReadOnly=""true"" type=""xs:int"" use=""required"" />
2982             <xs:attribute name=""Title"" type=""xs:string"" use=""required"" />
2983           </xs:complexType>
2984         </xs:element>
2985       </xs:choice>
2986     </xs:complexType>
2987     <xs:unique name=""PK_Element"" msdata:PrimaryKey=""true"">
2988       <xs:selector xpath="".//Element"" />
2989       <xs:field xpath=""@Dimension"" />
2990       <xs:field xpath=""@Number"" />
2991     </xs:unique>
2992   </xs:element>
2993 </xs:schema>";
2994                         DataSet ds = new DataSet ("Example");
2995
2996                         // Add a DataTable with no ReadOnly columns
2997                         DataTable dt1 = new DataTable ("StandAlone");
2998                         ds.Tables.Add (dt1);
2999
3000                         // Add a ReadOnly column
3001                         dt1.Columns.Add (new DataColumn ("ID", typeof (int), "",
3002                                 MappingType.Attribute));
3003                         dt1.Columns["ID"].AllowDBNull = false;
3004
3005                         dt1.Columns.Add (new DataColumn ("Desc", typeof
3006                                 (string), "", MappingType.Attribute));
3007                         dt1.Columns["Desc"].AllowDBNull = false;
3008
3009                         // Add related DataTables with ReadOnly columns
3010                         DataTable dt2 = new DataTable ("Dimension");
3011                         ds.Tables.Add (dt2);
3012                         dt2.Columns.Add (new DataColumn ("Number", typeof
3013                                 (int), "", MappingType.Attribute));
3014                         dt2.Columns["Number"].AllowDBNull = false;
3015                         dt2.Columns["Number"].ReadOnly = true;
3016
3017                         dt2.Columns.Add (new DataColumn ("Title", typeof
3018                                 (string), "", MappingType.Attribute));
3019                         dt2.Columns["Title"].AllowDBNull = false;
3020
3021                         dt2.Constraints.Add ("PK_Dimension", dt2.Columns["Number"], true);
3022
3023                         DataTable dt3 = new DataTable ("Element");
3024                         ds.Tables.Add (dt3);
3025
3026                         dt3.Columns.Add (new DataColumn ("Dimension", typeof
3027                                 (int), "", MappingType.Attribute));
3028                         dt3.Columns["Dimension"].AllowDBNull = false;
3029                         dt3.Columns["Dimension"].ReadOnly = true;
3030
3031                         dt3.Columns.Add (new DataColumn ("Number", typeof
3032                                 (int), "", MappingType.Attribute));
3033                         dt3.Columns["Number"].AllowDBNull = false;
3034                         dt3.Columns["Number"].ReadOnly = true;
3035
3036                         dt3.Columns.Add (new DataColumn ("Title", typeof
3037                                 (string), "", MappingType.Attribute));
3038                         dt3.Columns["Title"].AllowDBNull = false;
3039
3040                         dt3.Constraints.Add ("PK_Element", new DataColumn[] { 
3041                                 dt3.Columns ["Dimension"],
3042                                 dt3.Columns ["Number"] }, true);
3043
3044                         ds.AcceptChanges ();
3045
3046                         StringWriter sw1 = new StringWriter ();
3047                         ds.Tables[0].WriteXmlSchema (sw1);
3048                         string result1 = sw1.ToString ();
3049                         AssertEquals (xmlschema1.Replace ("\r\n", "\n"), result1.Replace ("\r\n", "\n"));
3050
3051                         StringWriter sw2 = new StringWriter ();
3052                         ds.Tables[1].WriteXmlSchema (sw2);
3053                         string result2 = sw2.ToString ();
3054                         AssertEquals (xmlschema2.Replace ("\r\n", "\n"), result2.Replace ("\r\n", "\n"));
3055
3056                         StringWriter sw3 = new StringWriter ();
3057                         ds.Tables[2].WriteXmlSchema (sw3);
3058                         string result3 = sw3.ToString ();
3059                         AssertEquals (xmlschema3.Replace ("\r\n", "\n"), result3.Replace ("\r\n", "\n"));
3060                 }
3061
3062                 [Test]
3063                 [Category ("NotWorking")]
3064                 public void WriteXmlSchema6 () {
3065                         string xmlschema = @"<?xml version=""1.0"" encoding=""utf-16""?>
3066 <xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
3067   <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:MainDataTable=""MyType"" msdata:UseCurrentLocale=""true"">
3068     <xs:complexType>
3069       <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
3070         <xs:element name=""MyType"">
3071           <xs:complexType>
3072             <xs:attribute name=""Desc"">
3073               <xs:simpleType>
3074                 <xs:restriction base=""xs:string"">
3075                   <xs:maxLength value=""32"" />
3076                 </xs:restriction>
3077               </xs:simpleType>
3078             </xs:attribute>
3079           </xs:complexType>
3080         </xs:element>
3081       </xs:choice>
3082     </xs:complexType>
3083   </xs:element>
3084 </xs:schema>";
3085                         DataSet ds = new DataSet ("Example");
3086
3087                         // Add MyType DataTable
3088                         ds.Tables.Add ("MyType");
3089
3090                         ds.Tables["MyType"].Columns.Add (new DataColumn (
3091                                 "Desc", typeof (string), "", MappingType.Attribute));
3092                         ds.Tables["MyType"].Columns["Desc"].MaxLength = 32;
3093
3094                         ds.AcceptChanges ();
3095
3096                         StringWriter sw = new StringWriter ();
3097                         ds.Tables[0].WriteXmlSchema (sw);
3098
3099                         string result = sw.ToString ();
3100
3101                         AssertEquals (xmlschema.Replace ("\r\n", "\n"), result.Replace ("\r\n", "\n"));
3102                 }
3103
3104                 [Test]
3105                 public void WriteXmlSchema7 () {
3106                         DataSet ds = new DataSet ();
3107                         DataTable dt = new DataTable ("table");
3108                         dt.Columns.Add ("col1");
3109                         dt.Columns.Add ("col2");
3110                         ds.Tables.Add (dt);
3111                         dt.Rows.Add (new object[] { "foo", "bar" });
3112                         StringWriter sw = new StringWriter ();
3113                         ds.Tables[0].WriteXmlSchema (sw);
3114                         Assert (sw.ToString ().IndexOf ("xmlns=\"\"") > 0);
3115                 }
3116
3117                 [Test]
3118                 public void WriteXmlSchema_ConstraintNameWithSpaces () {
3119                         DataSet ds = new DataSet ();
3120                         DataTable table1 = ds.Tables.Add ("table1");
3121                         DataTable table2 = ds.Tables.Add ("table2");
3122
3123                         table1.Columns.Add ("col1", typeof (int));
3124                         table2.Columns.Add ("col1", typeof (int));
3125
3126                         table1.Constraints.Add ("uc 1", table1.Columns[0], false);
3127                         table2.Constraints.Add ("fc 1", table1.Columns[0], table2.Columns[0]);
3128
3129                         StringWriter sw1 = new StringWriter ();
3130                         StringWriter sw2 = new StringWriter ();
3131
3132                         //should not throw an exception
3133                         ds.Tables[0].WriteXmlSchema (sw1);
3134                         ds.Tables[1].WriteXmlSchema (sw2);
3135                 }
3136
3137                 [Test]
3138                 public void WriteXmlSchema_ForignKeyConstraint () {
3139                         DataSet ds1 = new DataSet ();
3140
3141                         DataTable table1 = ds1.Tables.Add ();
3142                         DataTable table2 = ds1.Tables.Add ();
3143
3144                         DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
3145                         DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
3146
3147                         table2.Constraints.Add ("fk", col1_1, col2_1);
3148
3149                         StringWriter sw1 = new StringWriter ();
3150                         ds1.Tables[0].WriteXmlSchema (sw1);
3151                         String xml1 = sw1.ToString ();
3152                         Assert ("#1", xml1.IndexOf (@"<xs:unique name=""Constraint1"">") != -1);
3153
3154                         StringWriter sw2 = new StringWriter ();
3155                         ds1.Tables[1].WriteXmlSchema (sw2);
3156                         String xml2 = sw2.ToString ();
3157                         Assert ("#2", xml2.IndexOf (@"<xs:unique name=""Constraint1"">") == -1);
3158                 }
3159
3160                 [Test]
3161                 [Category ("NotWorking")]
3162                 public void WriteXmlSchema_Relations_ForeignKeys () {
3163                         MemoryStream ms1 = null;
3164                         MemoryStream ms2 = null;
3165                         MemoryStream msA = null;
3166                         MemoryStream msB = null;
3167
3168                         DataSet ds1 = new DataSet ();
3169
3170                         DataTable table1 = ds1.Tables.Add ("Table 1");
3171                         DataTable table2 = ds1.Tables.Add ("Table 2");
3172
3173                         DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
3174                         DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
3175                         DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
3176                         DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
3177                         DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
3178                         DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
3179                         DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
3180
3181                         DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
3182                         DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
3183                         DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
3184                         DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
3185                         DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
3186                         DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
3187                         DataColumn col2_7 = table2.Columns.Add ("col 7", typeof (int));
3188
3189                         ds1.Relations.Add ("rel 1",
3190                                 new DataColumn[] { col1_1, col1_2 },
3191                                 new DataColumn[] { col2_1, col2_2 },
3192                                 false);
3193                         ds1.Relations.Add ("rel 2",
3194                                 new DataColumn[] { col1_3, col1_4 },
3195                                 new DataColumn[] { col2_3, col2_4 },
3196                                 true);
3197                         table2.Constraints.Add ("fk 1",
3198                                 new DataColumn[] { col1_5, col1_6 },
3199                                 new DataColumn[] { col2_5, col2_6 });
3200                         table1.Constraints.Add ("fk 2",
3201                                 new DataColumn[] { col2_5, col2_6 },
3202                                 new DataColumn[] { col1_5, col1_6 });
3203
3204                         table1.Constraints.Add ("pk 1", col1_7, true);
3205                         table2.Constraints.Add ("pk 2", col2_7, true);
3206
3207                         ms1 = new MemoryStream ();
3208                         ds1.Tables[0].WriteXmlSchema (ms1);
3209                         ms2 = new MemoryStream ();
3210                         ds1.Tables[1].WriteXmlSchema (ms2);
3211
3212                         msA = new MemoryStream (ms1.GetBuffer ());
3213                         DataTable dtA = new DataTable ();
3214                         dtA.ReadXmlSchema (msA);
3215
3216                         msB = new MemoryStream (ms2.GetBuffer ());
3217                         DataTable dtB = new DataTable ();
3218                         dtB.ReadXmlSchema (msB);
3219
3220                         AssertEquals ("#2", 3, dtA.Constraints.Count);
3221                         AssertEquals ("#3", 2, dtB.Constraints.Count);
3222
3223                         Assert ("#5", dtA.Constraints.Contains ("pk 1"));
3224                         Assert ("#6", dtA.Constraints.Contains ("Constraint1"));
3225                         Assert ("#7", dtA.Constraints.Contains ("Constraint2"));
3226                         Assert ("#9", dtB.Constraints.Contains ("pk 2"));
3227                         Assert ("#10", dtB.Constraints.Contains ("Constraint1"));
3228                 }
3229
3230                 [Test]
3231                 [Category ("NotWorking")]
3232                 public void WriteXmlSchema_DifferentNamespace () {
3233                         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'>
3234   <xs:import namespace='urn:foo' />
3235   <xs:import namespace='urn:baz' />
3236   <xs:element name='NewDataSet' msdata:IsDataSet='true' msdata:MainDataTable='urn_x003A_foo_x003A_NS1Table' msdata:UseCurrentLocale='true'>
3237     <xs:complexType>
3238       <xs:choice minOccurs='0' maxOccurs='unbounded'>
3239         <xs:element ref='app2:NS1Table' />
3240       </xs:choice>
3241     </xs:complexType>
3242   </xs:element>
3243 </xs:schema>
3244 <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'>
3245   <xs:import namespace='urn:foo' />
3246   <xs:import namespace='urn:bar' />
3247   <xs:element name='column2' type='xs:string' />
3248 </xs:schema>
3249 <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'>
3250   <xs:import namespace='urn:bar' />
3251   <xs:import namespace='urn:baz' />
3252   <xs:element name='NS1Table'>
3253     <xs:complexType>
3254       <xs:sequence>
3255         <xs:element name='column1' type='xs:string' minOccurs='0' />
3256         <xs:element ref='app1:column2' minOccurs='0' />
3257       </xs:sequence>
3258     </xs:complexType>
3259   </xs:element>
3260 </xs:schema>";
3261                         DataSet ds = new DataSet ();
3262                         DataTable dt = new DataTable ();
3263                         dt.TableName = "NS1Table";
3264                         dt.Namespace = "urn:foo";
3265                         dt.Columns.Add ("column1");
3266                         dt.Columns.Add ("column2");
3267                         dt.Columns[1].Namespace = "urn:baz";
3268                         ds.Tables.Add (dt);
3269                         DataTable dt2 = new DataTable ();
3270                         dt2.TableName = "NS2Table";
3271                         dt2.Namespace = "urn:bar";
3272                         ds.Tables.Add (dt2);
3273                         ds.Namespace = "urn:bar";
3274
3275                         StringWriter sw1 = new StringWriter ();
3276                         XmlTextWriter xw1 = new XmlTextWriter (sw1);
3277                         xw1.Formatting = Formatting.Indented;
3278                         xw1.QuoteChar = '\'';
3279                         ds.Tables[0].WriteXmlSchema (xw1);
3280                         string result1 = sw1.ToString ();
3281                         AssertEquals ("#1", schema, result1);
3282
3283                         StringWriter sw2 = new StringWriter ();
3284                         XmlTextWriter xw2 = new XmlTextWriter (sw2);
3285                         xw2.Formatting = Formatting.Indented;
3286                         xw2.QuoteChar = '\'';
3287                         ds.Tables[0].WriteXmlSchema (xw2);
3288                         string result2 = sw2.ToString ();
3289                         AssertEquals ("#2", schema, result2);
3290                 }
3291
3292                 [Test]
3293                 [Category ("NotWorking")]
3294                 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3295                 // and is commented-out TWICE below
3296                 public void WriteXmlSchema_Hierarchy () {
3297                         DataSet ds = new DataSet ();
3298                         DataTable table1 = new DataTable ();
3299                         DataColumn idColumn = table1.Columns.Add ("ID", typeof (Int32));
3300                         table1.Columns.Add ("Name", typeof (String));
3301                         table1.PrimaryKey = new DataColumn[] { idColumn };
3302                         DataTable table2 = new DataTable ();
3303                         table2.Columns.Add (new DataColumn ("OrderID", typeof (Int32)));
3304                         table2.Columns.Add (new DataColumn ("CustomerID", typeof (Int32)));
3305                         table2.Columns.Add (new DataColumn ("OrderDate", typeof (DateTime)));
3306                         table2.PrimaryKey = new DataColumn[] { table2.Columns[0] };
3307                         ds.Tables.Add (table1);
3308                         ds.Tables.Add (table2);
3309                         ds.Relations.Add ("CustomerOrder",
3310                             new DataColumn[] { table1.Columns[0] },
3311                             new DataColumn[] { table2.Columns[1] }, true);
3312
3313                         StringWriter writer1 = new StringWriter ();
3314                         //table1.WriteXmlSchema (writer1, false);
3315                         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>";
3316                         AssertEquals ("#1", expected1, writer1.ToString());
3317
3318                         StringWriter writer2 = new StringWriter ();
3319                         //table1.WriteXmlSchema (writer2, true);
3320                         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>";
3321                         AssertEquals ("#2", expected2, writer2.ToString ());
3322                 }
3323
3324                 [Test]
3325                 [Category ("NotWorking")]
3326                 // WriteXmlSchema doesn't have overload wityh 2 parameters in System.Data
3327                 // and is commented-out TWICE below
3328                 public void ReadWriteXmlSchema () {
3329                         DataSet ds = new DataSet ();
3330                         ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3331                         // check dataset properties before testing write
3332                         AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3333                         AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3334                         AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3335                         AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3336                         // FIXME: currently order is not compatible. Use name as index
3337                         AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3338                         AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3339
3340                         ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.InferSchema);
3341                         ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3342
3343                         TextWriter writer1 = new StringWriter ();
3344                         ds.Tables[0].WriteXmlSchema (writer1);
3345                         string TextString1 = GetNormalizedSchema (writer1.ToString ());
3346                         string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3347 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3348   @"<xs:complexType name=""bookstoreType"">" +
3349   @"</xs:complexType>" +
3350   @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3351   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3352     @"<xs:complexType>" +
3353       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3354         @"<xs:element ref=""bookstore"" />" +
3355       @"</xs:choice>" +
3356     @"</xs:complexType>" +
3357   @"</xs:element>" +
3358 @"</xs:schema>";
3359                         AssertEquals ("#1", expected1, TextString1.Replace("\r\n","").Replace("  ",""));
3360
3361                         TextWriter writer2 = new StringWriter ();
3362                         //ds.Tables[1].WriteXmlSchema (writer2,false);
3363                         string TextString2 = GetNormalizedSchema (writer2.ToString ());
3364                         string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3365 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3366   @"<xs:complexType name=""bookType"">" +
3367     @"<xs:sequence>" +
3368       @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3369       @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3370     @"</xs:sequence>" +
3371     @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3372     @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3373   @"</xs:complexType>" +
3374   @"<xs:element name=""book"" type=""bookType"" />" +
3375   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3376     @"<xs:complexType>" +
3377       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3378         @"<xs:element ref=""book"" />" +
3379       @"</xs:choice>" +
3380     @"</xs:complexType>" +
3381   @"</xs:element>" +
3382 @"</xs:schema>";
3383                         AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace ("  ", ""));
3384                         
3385                         TextWriter writer3 = new StringWriter ();
3386                         ds.Tables[2].WriteXmlSchema (writer3);
3387                         string TextString3 = GetNormalizedSchema (writer3.ToString ());
3388                         string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3389 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3390   @"<xs:complexType name=""authorName"">" +
3391     @"<xs:sequence>" +
3392       @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3393       @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3394     @"</xs:sequence>" +
3395     @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3396   @"</xs:complexType>" +
3397   @"<xs:element name=""author"" type=""authorName"" />" +
3398   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3399     @"<xs:complexType>" +
3400       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3401         @"<xs:element ref=""author"" />" +
3402       @"</xs:choice>" +
3403     @"</xs:complexType>" +
3404   @"</xs:element>" +
3405 @"</xs:schema>";
3406                         AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace ("  ", ""));
3407                         
3408                         TextWriter writer4 = new StringWriter ();
3409                         ds.Tables[3].WriteXmlSchema (writer4);
3410                         string TextString4 = GetNormalizedSchema (writer4.ToString ());
3411                         string expected4 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3412 @"<xs:schema id=""Root"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3413   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""Region"" msdata:UseCurrentLocale=""true"" name=""Root"">" +
3414     @"<xs:complexType>" +
3415       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3416         @"<xs:element name=""Region"">" +
3417           @"<xs:complexType>" +
3418             @"<xs:sequence>" +
3419               @"<xs:element minOccurs=""0"" name=""RegionID"" type=""xs:string"" />" +
3420               @"<xs:element minOccurs=""0"" name=""RegionDescription"" type=""xs:string"" />" +
3421             @"</xs:sequence>" +
3422           @"</xs:complexType>" +
3423         @"</xs:element>" +
3424       @"</xs:choice>" +
3425     @"</xs:complexType>" +
3426   @"</xs:element>" +
3427 @"</xs:schema>";
3428                         AssertEquals ("#4", expected4, TextString4.Replace ("\r\n", "").Replace ("  ", ""));
3429                 }
3430
3431                 [Test]
3432                 [Category ("NotWorking")]
3433                 public void ReadWriteXmlSchema_IgnoreSchema () {
3434                         DataSet ds = new DataSet ();
3435                         ds.ReadXmlSchema ("Test/System.Data/store.xsd");
3436                         // check dataset properties before testing write
3437                         AssertDataSet ("ds", ds, "NewDataSet", 3, 2);
3438                         AssertDataTable ("tab1", ds.Tables[0], "bookstore", 1, 0, 0, 1, 1, 1);
3439                         AssertDataTable ("tab2", ds.Tables[1], "book", 5, 0, 1, 1, 2, 1);
3440                         AssertDataTable ("tab3", ds.Tables[2], "author", 3, 0, 1, 0, 1, 0);
3441                         // FIXME: currently order is not compatible. Use name as index
3442                         AssertDataRelation ("rel1", ds.Relations["book_author"], "book_author", true, new string[] { "book_Id" }, new string[] { "book_Id" }, true, true);
3443                         AssertDataRelation ("rel2", ds.Relations["bookstore_book"], "bookstore_book", true, new string[] { "bookstore_Id" }, new string[] { "bookstore_Id" }, true, true);
3444
3445                         ds.ReadXml ("Test/System.Data/region.xml", XmlReadMode.IgnoreSchema);
3446                         ds.Relations.Clear (); // because can not call WriteXmlSchema with nested relations.
3447
3448                         TextWriter writer1 = new StringWriter ();
3449                         ds.Tables[0].WriteXmlSchema (writer1);
3450                         string TextString1 = GetNormalizedSchema (writer1.ToString ());
3451                         string expected1 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3452 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3453   @"<xs:complexType name=""bookstoreType"">" +
3454   @"</xs:complexType>" +
3455   @"<xs:element name=""bookstore"" type=""bookstoreType"" />" +
3456   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""bookstore"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3457     @"<xs:complexType>" +
3458       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3459         @"<xs:element ref=""bookstore"" />" +
3460       @"</xs:choice>" +
3461     @"</xs:complexType>" +
3462   @"</xs:element>" +
3463 @"</xs:schema>";
3464                         AssertEquals ("#1", expected1, TextString1.Replace ("\r\n", "").Replace ("  ", ""));
3465
3466                         TextWriter writer2 = new StringWriter ();
3467                         //ds.Tables[1].WriteXmlSchema (writer2, false);
3468                         string TextString2 = GetNormalizedSchema (writer2.ToString ());
3469                         string expected2 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3470 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3471   @"<xs:complexType name=""bookType"">" +
3472     @"<xs:sequence>" +
3473       @"<xs:element msdata:Ordinal=""1"" name=""title"" type=""xs:string"" />" +
3474       @"<xs:element msdata:Ordinal=""2"" name=""price"" type=""xs:decimal"" />" +
3475     @"</xs:sequence>" +
3476     @"<xs:attribute name=""genre"" type=""xs:string"" />" +
3477     @"<xs:attribute name=""bookstore_Id"" type=""xs:int"" use=""prohibited"" />" +
3478   @"</xs:complexType>" +
3479   @"<xs:element name=""book"" type=""bookType"" />" +
3480   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""book"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3481     @"<xs:complexType>" +
3482       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3483         @"<xs:element ref=""book"" />" +
3484       @"</xs:choice>" +
3485     @"</xs:complexType>" +
3486   @"</xs:element>" +
3487 @"</xs:schema>";
3488                         AssertEquals ("#2", expected2, TextString2.Replace ("\r\n", "").Replace ("  ", ""));
3489
3490                         TextWriter writer3 = new StringWriter ();
3491                         ds.Tables[2].WriteXmlSchema (writer3);
3492                         string TextString3 = GetNormalizedSchema (writer3.ToString ());
3493                         string expected3 = @"<?xml version=""1.0"" encoding=""utf-16""?>" +
3494 @"<xs:schema id=""NewDataSet"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">" +
3495   @"<xs:complexType name=""authorName"">" +
3496     @"<xs:sequence>" +
3497       @"<xs:element msdata:Ordinal=""0"" name=""first-name"" type=""xs:string"" />" +
3498       @"<xs:element msdata:Ordinal=""1"" name=""last-name"" type=""xs:string"" />" +
3499     @"</xs:sequence>" +
3500     @"<xs:attribute name=""book_Id"" type=""xs:int"" use=""prohibited"" />" +
3501   @"</xs:complexType>" +
3502   @"<xs:element name=""author"" type=""authorName"" />" +
3503   @"<xs:element msdata:IsDataSet=""true"" msdata:MainDataTable=""author"" msdata:UseCurrentLocale=""true"" name=""NewDataSet"">" +
3504     @"<xs:complexType>" +
3505       @"<xs:choice maxOccurs=""unbounded"" minOccurs=""0"">" +
3506         @"<xs:element ref=""author"" />" +
3507       @"</xs:choice>" +
3508     @"</xs:complexType>" +
3509   @"</xs:element>" +
3510 @"</xs:schema>";
3511                         AssertEquals ("#3", expected3, TextString3.Replace ("\r\n", "").Replace ("  ", ""));
3512
3513                         TextWriter writer4 = new StringWriter ();
3514                         string expStr = "";
3515                         try {
3516                                 ds.Tables[3].WriteXmlSchema (writer4);
3517                         }
3518                         catch (Exception ex) {
3519                                 expStr = ex.Message;
3520                         }
3521                         AssertEquals ("#4", "Cannot find table 3.", expStr);
3522                 }
3523
3524                 [Test]
3525                 [Category ("NotWorking")]
3526                 public void ReadWriteXmlSchema_2 () {
3527                         DataSet ds = new DataSet ("dataset");
3528                         ds.Tables.Add ("table1");
3529                         ds.Tables.Add ("table2");
3530                         ds.Tables[0].Columns.Add ("col");
3531                         ds.Tables[1].Columns.Add ("col");
3532                         ds.Relations.Add ("rel", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0], true);
3533
3534                         MemoryStream ms1 = new MemoryStream ();
3535                         ds.Tables[0].WriteXmlSchema (ms1);
3536                         MemoryStream ms2 = new MemoryStream ();
3537                         ds.Tables[1].WriteXmlSchema (ms2);
3538
3539                         DataSet ds1 = new DataSet ();
3540                         ds1.Tables.Add ();
3541                         ds1.Tables.Add ();
3542                         ds1.Tables[0].ReadXmlSchema (new MemoryStream (ms1.GetBuffer ()));
3543                         ds1.Tables[1].ReadXmlSchema (new MemoryStream (ms2.GetBuffer ()));
3544
3545                         AssertEquals ("#1", 0, ds1.Relations.Count);
3546                         AssertEquals ("#2", 1, ds1.Tables[0].Columns.Count);
3547                         AssertEquals ("#3", 1, ds1.Tables[1].Columns.Count);
3548                 }
3549
3550                 [Test]
3551                 [ExpectedException (typeof (XmlException))]
3552                 public void ReadWriteXmlSchemaExp_NoRootElmnt () {
3553                         MemoryStream ms = new MemoryStream ();
3554                         DataTable dtr = new DataTable ();
3555                         dtr.ReadXmlSchema (ms);
3556                 }
3557
3558                 [Test]
3559                 [Category ("NotWorking")]
3560                 [ExpectedException (typeof (InvalidOperationException))]
3561                 public void ReadWriteXmlSchemaExp_NoTableName () {
3562                         DataTable dtw = new DataTable ();
3563                         MemoryStream ms = new MemoryStream ();
3564                         dtw.WriteXmlSchema (ms);
3565                 }
3566
3567                 [Test]
3568                 [ExpectedException (typeof (ArgumentException))]
3569                 public void ReadWriteXmlSchemaExp_TableNameConflict () {
3570                         DataTable dtw = new DataTable ("Table1");
3571                         StringWriter writer1 = new StringWriter ();
3572                         dtw.WriteXmlSchema (writer1);
3573                         DataTable dtr = new DataTable ("Table2");
3574                         StringReader reader1 = new StringReader (writer1.ToString());
3575                         dtr.ReadXmlSchema (reader1);
3576                 }
3577
3578                 #endregion // Read/Write XML Tests
3579
3580 #endif // NET_2_0
3581
3582         }
3583                                                                                                     
3584                                                                                                     
3585          public  class MyDataTable:DataTable {
3586                                                                                                     
3587              public static int count = 0;
3588                                                                                                     
3589              public MyDataTable() {
3590                                                                                                     
3591                     count++;
3592              }
3593                                                                                                     
3594          }
3595
3596         [Serializable]
3597         [TestFixture]
3598         public class AppDomainsAndFormatInfo
3599         {
3600                 public void Remote ()
3601                 {
3602                         int n = (int) Convert.ChangeType ("5", typeof (int));
3603                         Assertion.AssertEquals ("n", 5, n);
3604                 }
3605 #if !TARGET_JVM
3606                 [Test]
3607                 public void NFIFromBug55978 ()
3608                 {
3609                         AppDomain domain = AppDomain.CreateDomain ("testdomain");
3610                         AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
3611                         test.Remote ();
3612                         domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
3613                         AppDomain.Unload (domain);
3614                 }
3615 #endif
3616
3617                 [Test]
3618                 public void Bug55978 ()
3619                 {
3620                         DataTable dt = new DataTable ();
3621                         dt.Columns.Add ("StartDate", typeof (DateTime));
3622          
3623                         DataRow dr;
3624                         DateTime date = DateTime.Now;
3625          
3626                         for (int i = 0; i < 10; i++) {
3627                                 dr = dt.NewRow ();
3628                                 dr ["StartDate"] = date.AddDays (i);
3629                                 dt.Rows.Add (dr);
3630                         }
3631          
3632                         DataView dv = dt.DefaultView;
3633                         dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
3634                         Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
3635                         Assertion.AssertEquals ("View", 2, dv.Count);
3636                 }
3637         }
3638 }