Merge pull request #1266 from esdrubal/datetimenewformat
[mono.git] / mcs / class / System.Data / Test / System.Data / DataSetTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 //   Veerapuram Varadhan  <vvaradhan@novell.com>
7 //
8 // Copyright (c) 2004 Mainsoft Co.
9 // Copyright (c) 2009 Novell Inc.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Text;
34 using System.IO;
35 using System.Data;
36 using MonoTests.System.Data.Utils;
37 using System.Xml;
38 using System.Runtime.Serialization;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Globalization;
41
42 namespace MonoTests.System.Data
43 {
44         [TestFixture]
45         public class DataSetTest2
46         {
47                 private DataSet m_ds = null;
48                 private bool EventRaised = false;
49
50                 [Test] public void AcceptChanges()
51                 {
52                         DataSet ds = new DataSet();
53                         DataTable dtP = DataProvider.CreateParentDataTable();
54                         DataTable dtC = DataProvider.CreateChildDataTable();
55                         ds.Tables.Add(dtP);
56                         ds.Tables.Add(dtC);
57                         ds.Relations.Add(new DataRelation("myRelation",dtP.Columns[0],dtC.Columns[0]));
58
59                         //create changes
60                         dtP.Rows[0][0] = "70";
61                         dtP.Rows[1].Delete();
62                         dtP.Rows.Add(new object[] {9,"string1","string2"});
63
64                         // AcceptChanges
65                         ds.AcceptChanges();
66                         Assert.AreEqual(null, dtP.GetChanges(), "DS1");
67
68                         //read only exception
69                         dtP.Columns[0].ReadOnly = true;
70                         // check ReadOnlyException
71                         try
72                         {
73                                 dtP.Rows[0][0] = 99;
74                                 Assert.Fail("DS2: Indexer Failed to throw ReadOnlyException");
75                         }
76                         catch (ReadOnlyException) {}
77                         catch (AssertionException exc) {throw  exc;}
78                         catch (Exception exc)
79                         {
80                                 Assert.Fail("DS3: Indexer. Wrong exception type. Got:" + exc);
81                         }
82
83                         // check invoke AcceptChanges
84                         ds.AcceptChanges();
85                 }
86
87                 [Test] public void CaseSensitive()
88                 {
89                         DataSet ds = new DataSet();
90                         DataTable dt = new DataTable();
91
92                         // CaseSensitive - default value (false)
93                         Assert.AreEqual(false , ds.CaseSensitive  , "DS4");
94
95                         ds.CaseSensitive = true;
96
97                         // CaseSensitive - get
98                         Assert.AreEqual(true , ds.CaseSensitive  , "DS5");
99
100                         //add a datatable to a dataset
101                         ds.Tables.Add(dt);
102                         // DataTable CaseSensitive from DataSet - true
103                         Assert.AreEqual(true , dt.CaseSensitive  , "DS6");
104
105                         ds.Tables.Clear();
106                         ds.CaseSensitive = false;
107                         dt = new DataTable();
108                         ds.Tables.Add(dt);
109
110                         // DataTable CaseSensitive from DataSet - false
111                         Assert.AreEqual(false, dt.CaseSensitive , "DS7");
112
113                         //change DataSet CaseSensitive and check DataTables in it
114                         ds.Tables.Clear();
115                         ds.CaseSensitive = false;
116                         dt = new DataTable();
117                         ds.Tables.Add(dt);
118
119                         // Change DataSet CaseSensitive - check Table - true
120                         ds.CaseSensitive = true;
121                         Assert.AreEqual(true, dt.CaseSensitive , "DS8");
122
123                         // Change DataSet CaseSensitive - check Table - false
124                         ds.CaseSensitive = false;
125                         Assert.AreEqual(false, dt.CaseSensitive , "DS9");
126
127                         //Add new table to DataSet with CaseSensitive,check the table case after adding it to DataSet
128                         ds.Tables.Clear();
129                         ds.CaseSensitive = true;
130                         dt = new DataTable();
131                         dt.CaseSensitive = false;
132                         ds.Tables.Add(dt);
133
134                         // DataTable get case sensitive from DataSet - false
135                         Assert.AreEqual(false, dt.CaseSensitive , "DS10");
136
137                         ds.Tables.Clear();
138                         ds.CaseSensitive = false;
139                         dt = new DataTable();
140                         dt.CaseSensitive = true;
141                         ds.Tables.Add(dt);
142
143                         // DataTable get case sensitive from DataSet - true
144                         Assert.AreEqual(true, dt.CaseSensitive , "DS11");
145
146                         //Add new table to DataSet and change the DataTable CaseSensitive
147                         ds.Tables.Clear();
148                         ds.CaseSensitive = true;
149                         dt = new DataTable();
150                         ds.Tables.Add(dt);
151
152                         // Add new table to DataSet and change the DataTable CaseSensitive - false
153                         dt.CaseSensitive = false;
154                         Assert.AreEqual(false, dt.CaseSensitive , "DS12");
155
156                         ds.Tables.Clear();
157                         ds.CaseSensitive = false;
158                         dt = new DataTable();
159                         ds.Tables.Add(dt);
160
161                         // Add new table to DataSet and change the DataTable CaseSensitive - true
162                         dt.CaseSensitive = true;
163                         Assert.AreEqual(true, dt.CaseSensitive , "DS13");
164
165                         //Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable
166                         ds.Tables.Clear();
167                         ds.CaseSensitive = true;
168                         dt = new DataTable();
169                         dt.CaseSensitive = true;
170                         ds.Tables.Add(dt);
171
172                         // Add DataTable to Dataset, Change DataSet CaseSensitive, check DataTable - true
173                         ds.CaseSensitive = false;
174                         Assert.AreEqual(true, dt.CaseSensitive , "DS14");
175                 }
176
177                 [Test] public void Clear()
178                 {
179                         DataSet ds = new DataSet();
180                         ds.Tables.Add(DataProvider.CreateParentDataTable());
181                         ds.Tables[0].Rows.Add(new object[] {9,"",""});
182
183                         // Clear
184                         ds.Clear();
185                         Assert.AreEqual(0, ds.Tables[0].Rows.Count , "DS15");
186                 }
187
188                 [Test] public void Clear_WithNoDataWithConstraint()
189                 {
190                         // Test dataset with no data and with constraint
191                         DataSet ds = new DataSet();
192                         ds.Tables.Add(DataProvider.CreateParentDataTable());
193                         ds.Tables.Add(DataProvider.CreateChildDataTable());
194                         ds.Tables[0].Rows.Clear();
195                         ds.Tables[1].Rows.Clear();
196
197                         ds.Tables[0].Constraints.Add("test",ds.Tables[1].Columns[0],ds.Tables[0].Columns[0]);
198                         ds.Clear();
199                 }
200
201                 [Test] public void Clone()
202                 {
203                         DataSet ds = new DataSet(), dsTarget = null;
204                         ds.Tables.Add(DataProvider.CreateParentDataTable());
205                         ds.Tables.Add(DataProvider.CreateChildDataTable());
206                         ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
207                         ds.Tables[0].Rows.Add(new object[] {9,"",""});
208                         ds.Tables[1].Columns[2].ReadOnly = true;
209                         ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
210
211                         //copy schema only, no data
212
213                         // Clone 1
214                         dsTarget = ds.Clone();
215                         //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS16");
216                         //use my function because GetXmlSchema not implemented in java
217                         Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema(dsTarget), "DS17");
218
219                         // Clone 2
220                         Assert.AreEqual(false, dsTarget.GetXml() == ds.GetXml(), "DS18");
221                 }
222
223                 [Test] public void Copy()
224                 {
225                         DataSet ds = new DataSet(), dsTarget = null;
226                         ds.Tables.Add(DataProvider.CreateParentDataTable());
227                         ds.Tables.Add(DataProvider.CreateChildDataTable());
228                         ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
229                         ds.Tables[0].Rows.Add(new object[] {9,"",""});
230                         ds.Tables[1].Columns[2].ReadOnly = true;
231                         ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
232
233                         //copy data and schema
234
235                         // Copy 1
236                         dsTarget = ds.Copy();
237                         //Assert.AreEqual(ds.GetXmlSchema(), dsTarget.GetXmlSchema() , "DS19");
238                         //using my function because GetXmlSchema in not implemented in java
239                         Assert.AreEqual(DataProvider.GetDSSchema(ds), DataProvider.GetDSSchema (dsTarget) , "DS20");
240
241                         // Copy 2
242                         Assert.AreEqual(true, dsTarget.GetXml() == ds.GetXml(), "DS21");
243                 }
244
245                 [Test] public void DataSetName()
246                 {
247                         DataSet ds = new DataSet();
248
249                         // DataSetName - default value
250                         Assert.AreEqual("NewDataSet" , ds.DataSetName   , "DS22");
251
252                         ds.DataSetName = "NewName";
253
254                         // DataSetName - get
255                         Assert.AreEqual("NewName" , ds.DataSetName   , "DS23");
256                 }
257
258                 [Test] public void EnforceConstraints()
259                 {
260                         DataSet ds = new DataSet();
261
262                         // EnforceConstraints - default value (true)
263                         Assert.AreEqual(true, ds.EnforceConstraints , "DS24");
264
265                         ds.EnforceConstraints = false;
266
267                         // EnforceConstraints - get
268                         Assert.AreEqual(false, ds.EnforceConstraints  , "DS25");
269                 }
270
271                 [Test]
272                 public void EnforceConstraints_CheckPrimaryConstraint ()
273                 {
274                         DataSet ds = new DataSet();
275                         ds.Tables.Add ("table");
276                         ds.Tables [0].Columns.Add ("col");
277                         ds.Tables [0].PrimaryKey = new DataColumn[] {ds.Tables [0].Columns [0]};
278                         ds.EnforceConstraints = false;
279                         ds.Tables [0].Rows.Add (new object[] {null});
280                         try {
281                                 ds.EnforceConstraints = true;
282                                 Assert.Fail ("#1");
283                         } catch (ConstraintException e) {
284                                 // Never premise English.
285                                 //Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " + 
286                                 //              "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
287                         }
288                 }
289
290                 [Test]
291                 public void EnforceConstraints_NonNullCols ()
292                 {
293                         DataSet ds = new DataSet();
294                         ds.Tables.Add ("table");
295                         ds.Tables [0].Columns.Add ("col");
296                         ds.Tables [0].Columns [0].AllowDBNull = false;
297
298                         ds.EnforceConstraints = false;
299                         ds.Tables [0].Rows.Add (new object[] {null});
300                         try {
301                                 ds.EnforceConstraints = true;
302                                 Assert.Fail ("#1");
303                         } catch (ConstraintException e) {
304                                 // Never premise English.
305                                 //Assert.AreEqual ("Failed to enable constraints. One or more rows contain values " + 
306                                 //              "violating non-null, unique, or foreign-key constraints.", e.Message, "#2");
307                         }
308                 }
309
310                 [Test] public void GetChanges()
311                 {
312                         DataSet ds = new DataSet();
313                         ds.Tables.Add(DataProvider.CreateParentDataTable());
314
315                         // GetChanges 1
316                         Assert.AreEqual(null , ds.GetChanges(), "DS26");
317
318                         DataRow dr = ds.Tables[0].NewRow();
319                         dr[0] = 9;
320                         ds.Tables[0].Rows.Add(dr);
321
322                         // GetChanges 2
323                         Assert.AreEqual(true , ds.GetChanges()!=null, "DS27");
324
325                         // GetChanges 3
326                         Assert.AreEqual(dr.ItemArray, ds.GetChanges().Tables[0].Rows[0].ItemArray  , "DS28");
327                 }
328
329                 [Test] public void GetChanges_ByDataRowState()
330                 {
331                         DataSet ds = new DataSet();
332                         object[] arrAdded,arrDeleted,arrModified,arrUnchanged;
333                         //object[] arrDetached;
334
335                         DataRow dr;
336                         ds.Tables.Add(DataProvider.CreateParentDataTable());
337
338                         // GetChanges 1
339                         Assert.AreEqual(null , ds.GetChanges(), "DS29");
340
341                         //make some changes
342
343                         // can't check detached
344                         //              dr = ds.Tables[0].Rows[0];
345                         //              arrDetached = dr.ItemArray;
346                         //              dr.Delete();
347                         //              ds.Tables[0].AcceptChanges();
348
349                         dr= ds.Tables[0].Rows[1];
350                         arrDeleted  = dr.ItemArray;
351                         dr.Delete();
352
353                         dr = ds.Tables[0].Rows[2];
354                         dr[1] = "NewValue";
355                         arrModified = dr.ItemArray;
356
357                         dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
358                         arrUnchanged = dr.ItemArray;
359
360                         dr = ds.Tables[0].NewRow();
361                         dr[0] = 1;
362                         ds.Tables[0].Rows.Add(dr);
363                         arrAdded = dr.ItemArray;
364
365                         // GetChanges Added
366                         Assert.AreEqual(arrAdded, ds.GetChanges(DataRowState.Added).Tables[0].Rows[0].ItemArray , "DS30");
367
368                         // GetChanges Deleted
369                         dr = ds.GetChanges(DataRowState.Deleted).Tables[0].Rows[0];
370                         object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original],dr[3,DataRowVersion.Original],dr[4,DataRowVersion.Original],dr[5,DataRowVersion.Original]};
371                         Assert.AreEqual(arrDeleted, tmp, "DS31");
372
373                         //      can't check it  
374                         //              // GetChanges Detached
375                         //              dr = ds.GetChanges(DataRowState.Detached).Tables[0].Rows[0];
376                         //              object[] tmp = new object[] {dr[0,DataRowVersion.Original],dr[1,DataRowVersion.Original],dr[2,DataRowVersion.Original]};
377                         //              Assert.AreEqual(arrDetached, tmp, "DS32");
378
379                         // GetChanges Modified
380                         Assert.AreEqual(arrModified, ds.GetChanges(DataRowState.Modified).Tables[0].Rows[0].ItemArray , "DS33");
381
382                         // GetChanges Unchanged
383                         Assert.AreEqual(arrUnchanged, ds.GetChanges(DataRowState.Unchanged).Tables[0].Rows[0].ItemArray , "DS34");
384                 }
385
386                 [Test] public void BeginInitTest ()
387                 {
388                         DataSet ds = new DataSet ();
389
390                         DataTable table1 = new DataTable ("table1");
391                         DataTable table2 = new DataTable ("table2");
392
393                         DataColumn col1 = new DataColumn ("col1", typeof (int));
394                         DataColumn col2 = new DataColumn ("col2", typeof (int));
395                         table1.Columns.Add (col1);
396                         table2.Columns.Add (col2);
397                         
398                         UniqueConstraint pkey = new UniqueConstraint ("pk", new string[] {"col1"}, true);
399                         ForeignKeyConstraint fkey = new ForeignKeyConstraint ("fk", "table1", new String[] {"col1"}, 
400                                                                 new String[] {"col2"}, AcceptRejectRule.Cascade,
401                                                                 Rule.Cascade, Rule.Cascade);
402                         DataRelation relation = new DataRelation ("rel", "table1", "table2", new String[] {"col1"},
403                                                                  new String[] {"col2"}, false);
404                         ds.BeginInit ();
405                         table1.BeginInit ();
406                         table2.BeginInit ();
407
408                         ds.Tables.AddRange (new DataTable[] {table1, table2});
409                         ds.Relations.AddRange (new DataRelation[] {relation});
410                         
411                         table1.Constraints.AddRange (new Constraint[] {pkey});
412                         table2.Constraints.AddRange (new Constraint[] {fkey});
413
414                         // The tables/relations shud not get added to the DataSet yet
415                         Assert.AreEqual (0, ds.Tables.Count, "#1");
416                         Assert.AreEqual (0, ds.Relations.Count, "#2");
417                         Assert.AreEqual (0, table1.Constraints.Count, "#3");
418                         Assert.AreEqual (0, table2.Constraints.Count, "#4");
419                         ds.EndInit ();
420
421                         Assert.AreEqual (2, ds.Tables.Count, "#5");
422                         Assert.AreEqual (1, ds.Relations.Count, "#6");
423                         Assert.AreEqual (1, ds.Tables [0].Constraints.Count, "#7");
424                         Assert.AreEqual (1, ds.Tables [1].Constraints.Count, "#8");
425
426                         // Table shud still be in BeginInit .. 
427                         DataColumn col3 = new DataColumn ("col2");
428                         UniqueConstraint uc = new UniqueConstraint ("uc", new string[] {"col2"}, false);
429
430                         table1.Columns.AddRange (new DataColumn[] {col3});
431                         table1.Constraints.AddRange (new Constraint[] {uc});
432
433                         Assert.AreEqual (1, table1.Columns.Count, "#9");
434                         Assert.AreEqual (1, table1.Constraints.Count, "#10");
435
436                         table1.EndInit ();
437                         Assert.AreEqual (2, table1.Columns.Count, "#11");
438                         Assert.AreEqual (2, table1.Columns.Count, "#12");
439                 }
440
441                 [Test] public void GetXml()
442                 {
443                         DataSet ds = new DataSet();
444                         ds.Namespace = "namespace"; //if we don't add namespace the test will fail because GH (by design) always add namespace
445                         DataTable dt = DataProvider.CreateParentDataTable();
446                         dt.Clear();
447                         dt.Rows.Add(new object[] {1,"Value1","Value2"});
448                         dt.Rows.Add(new object[] {2,"Value3","Value4"});
449                         dt.Rows.Add(new object[] {3,"Value5","Value5"});
450
451                         StringBuilder resultXML = new StringBuilder();
452
453                         resultXML.Append("<" + ds.DataSetName  + "xmlns=\"namespace\">");
454
455                         resultXML.Append("<Parent>");
456                         resultXML.Append("<ParentId>1</ParentId>");
457                         resultXML.Append("<String1>Value1</String1>");
458                         resultXML.Append("<String2>Value2</String2>");
459                         resultXML.Append("</Parent>");
460
461                         resultXML.Append("<Parent>");
462                         resultXML.Append("<ParentId>2</ParentId>");
463                         resultXML.Append("<String1>Value3</String1>");
464                         resultXML.Append("<String2>Value4</String2>");
465                         resultXML.Append("</Parent>");
466
467                         resultXML.Append("<Parent>");
468                         resultXML.Append("<ParentId>3</ParentId>");
469                         resultXML.Append("<String1>Value5</String1>");
470                         resultXML.Append("<String2>Value5</String2>");
471                         resultXML.Append("</Parent>");
472
473                         resultXML.Append("</" + ds.DataSetName  + ">");
474
475                         ds.Tables.Add(dt);
476                         string strXML = ds.GetXml();
477                         strXML = strXML.Replace(" ","");
478                         strXML = strXML.Replace("\t","");
479                         strXML = strXML.Replace("\n","");
480                         strXML = strXML.Replace("\r","");
481
482                         // GetXml
483                         Assert.AreEqual(resultXML.ToString() , strXML , "DS35");
484                 }
485
486                 [Test] public void HasChanges()
487                 {
488                         DataSet ds = new DataSet();
489                         ds.Tables.Add(DataProvider.CreateParentDataTable());
490
491                         // HasChanges 1
492                         Assert.AreEqual(false , ds.HasChanges(), "DS36");
493
494                         DataRow dr = ds.Tables[0].NewRow();
495                         dr[0] = 9;
496                         ds.Tables[0].Rows.Add(dr);
497
498                         // HasChanges 2
499                         Assert.AreEqual(true , ds.HasChanges(), "DS37");
500                 }
501
502                 [Test] public void HasChanges_ByDataRowState()
503                 {
504                         DataSet ds = new DataSet();
505
506                         DataRow dr;
507                         ds.Tables.Add(DataProvider.CreateParentDataTable());
508
509                         // HasChanges 1
510                         Assert.AreEqual(false , ds.HasChanges(), "DS38");
511
512                         //make some changes
513
514                         dr= ds.Tables[0].Rows[1];
515                         dr.Delete();
516
517                         dr = ds.Tables[0].Rows[2];
518                         dr[1] = "NewValue";
519
520                         dr = ds.Tables[0].Select("","",DataViewRowState.Unchanged)[0];
521
522                         dr = ds.Tables[0].NewRow();
523                         dr[0] = 1;
524                         ds.Tables[0].Rows.Add(dr);
525
526                         // HasChanges Added
527                         Assert.AreEqual(true , ds.HasChanges(DataRowState.Added), "DS39");
528
529                         // HasChanges Deleted
530                         Assert.AreEqual(true  , ds.HasChanges(DataRowState.Deleted) , "DS40");
531
532                         // HasChanges Modified
533                         Assert.AreEqual(true, ds.HasChanges(DataRowState.Modified), "DS41");
534
535                         // HasChanges Unchanged
536                         Assert.AreEqual(true, ds.HasChanges(DataRowState.Unchanged), "DS42");
537                 }
538
539                 [Test] public void HasErrors()
540                 {
541                         DataSet ds = new DataSet();
542                         ds.Tables.Add(DataProvider.CreateParentDataTable());
543
544                         // HasErrors - default
545                         Assert.AreEqual(false , ds.HasErrors  , "DS43");
546
547                         ds.Tables[0].Rows[0].RowError = "ErrDesc";
548
549                         // HasErrors
550                         Assert.AreEqual(true , ds.HasErrors , "DS44");
551                 }
552
553                 #region test namespaces
554
555                 [Test] public void InferXmlSchema_BasicXml()
556                 {
557                         StringBuilder sb  = new StringBuilder();
558                         sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
559                         sb.Append("<Categories>");
560                         sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
561                         sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
562                         sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
563                         sb.Append("</Categories>");
564                         sb.Append("<Products>");
565                         sb.Append("<ProductID od:adotype='20'>1</ProductID>");
566                         sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
567                         sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
568                         sb.Append("</Products>");
569                         sb.Append("</NewDataSet>");
570
571                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
572
573                         DataSet ds = new DataSet();
574                         //      ds.ReadXml(myStream);
575                         ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
576                         Assert.AreEqual(2, ds.Tables.Count, "DS45");
577                         Assert.AreEqual("CategoryID", ds.Tables[0].Columns[0].ColumnName, "DS46");
578                         Assert.AreEqual("CategoryName", ds.Tables[0].Columns[1].ColumnName, "DS47");
579                         Assert.AreEqual("Description", ds.Tables[0].Columns[2].ColumnName, "DS48");
580
581                         Assert.AreEqual("ProductID", ds.Tables[1].Columns[0].ColumnName, "DS49");
582                         Assert.AreEqual("ReorderLevel", ds.Tables[1].Columns[1].ColumnName, "DS50");
583                         Assert.AreEqual("Discontinued", ds.Tables[1].Columns[2].ColumnName, "DS51");
584                 }
585
586                 [Test] public void InferXmlSchema_WithoutIgnoreNameSpaces()
587                 {
588                         StringBuilder sb  = new StringBuilder();
589                         sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
590                         sb.Append("<Categories>");
591                         sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
592                         sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>");
593                         sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
594                         sb.Append("</Categories>");
595                         sb.Append("<Products>");
596                         sb.Append("<ProductID od:adotype='20'>1</ProductID>");
597                         sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
598                         sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
599                         sb.Append("</Products>");
600                         sb.Append("</NewDataSet>");
601
602                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
603
604                         DataSet ds = new DataSet();
605                         //ds.ReadXml(myStream);
606                         ds.InferXmlSchema(myStream,new string[] {"urn:schemas-microsoft-com:officedata1"});
607                         Assert.AreEqual(8, ds.Tables.Count, "DS52");
608                 }
609
610                 [Test] public void InferXmlSchema_IgnoreNameSpace()
611                 {
612                         StringBuilder sb  = new StringBuilder();
613                         sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>");
614                         sb.Append("<Categories>");
615                         sb.Append("<CategoryID od:adotype='3'>1</CategoryID>");
616                         sb.Append("<CategoryName od:maxLength='15' adotype='130'>Beverages</CategoryName>");
617                         sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>");
618                         sb.Append("</Categories>");
619                         sb.Append("<Products>");
620                         sb.Append("<ProductID od:adotype='20'>1</ProductID>");
621                         sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>");
622                         sb.Append("<Discontinued od:adotype='11'>0</Discontinued>");
623                         sb.Append("</Products>");
624                         sb.Append("</NewDataSet>");
625
626                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
627
628                         DataSet ds = new DataSet();
629                         //      ds.ReadXml(myStream);
630                         ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"});
631                         Assert.AreEqual(3, ds.Tables.Count, "DS53");
632
633                         Assert.AreEqual(3, ds.Tables[0].Columns.Count, "DS54");
634                         Assert.AreEqual("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName, "DS55");
635                         Assert.AreEqual("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName, "DS56");//Hidden
636                         Assert.AreEqual("Description", ds.Tables[0].Columns["Description"].ColumnName, "DS57");
637
638                         Assert.AreEqual(3, ds.Tables[1].Columns.Count, "DS58");
639                         Assert.AreEqual("adotype", ds.Tables[1].Columns["adotype"].ColumnName, "DS59");
640                         Assert.AreEqual("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName, "DS60");
641                         Assert.AreEqual("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName, "DS61");//Hidden
642
643                         Assert.AreEqual(3, ds.Tables[2].Columns.Count, "DS62");
644                         Assert.AreEqual("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName, "DS63");
645                         Assert.AreEqual("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName, "DS64");
646                         Assert.AreEqual("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName, "DS65");
647                 }
648
649                 [Test] public void InferXmlSchema_IgnoreNameSpaces() //Ignoring 2 namespaces
650                 {
651                         StringBuilder sb  = new StringBuilder();
652                         sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>");
653                         sb.Append("<h:head><h:title>Book Review</h:title></h:head>");
654                         sb.Append("<h:body>");
655                         sb.Append("<xdc:bookreview>");
656                         sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>");
657                         sb.Append("</xdc:bookreview>");
658                         sb.Append("</h:body>");
659                         sb.Append("</h:html>");
660
661                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
662                         DataSet tempDs = new DataSet();
663                         tempDs.ReadXml(myStream);
664                         myStream.Seek(0,SeekOrigin.Begin);
665                         DataSet ds = new DataSet();
666                         ds.InferXmlSchema(myStream, new string[] {"http://www.xml.com/books","http://www.w3.org/HTML/1998/html4"});
667                         //Assert.AreEqual(8, ds.Tables.Count, "DS66");
668
669                         //                      string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs);
670                         //                      string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds);
671                         Assert.AreEqual(3, ds.Tables.Count, "DS67");
672                         Assert.AreEqual("bookreview", ds.Tables[2].TableName, "DS68");
673                         Assert.AreEqual(2, ds.Tables[2].Columns.Count, "DS69");
674                 }
675                 #endregion
676
677                 #region inferingTables
678                 [Test] public void InferXmlSchema_inferingTables1()
679                 {
680                         //Acroding to the msdn documantaion :
681                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
682                         //Elements that have attributes specified in them will result in inferred tables
683
684                         // inferingTables1
685                         StringBuilder sb  = new StringBuilder();
686
687                         sb.Append("<DocumentElement>");
688                         sb.Append("<Element1 attr1='value1'/>");
689                         sb.Append("<Element1 attr1='value2'>Text1</Element1>");
690                         sb.Append("</DocumentElement>");
691                         DataSet ds = new DataSet();
692                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
693                         ds.InferXmlSchema(myStream,null);
694                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS70");
695                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS71");
696                         Assert.AreEqual(1, ds.Tables.Count, "DS72");
697                         Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS73");
698                         Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS74");
699                 }
700
701                 [Test] public void InferXmlSchema_inferingTables2()
702                 {
703                         //Acroding to the msdn documantaion :
704                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
705                         //Elements that have child elements will result in inferred tables
706
707                         // inferingTables2
708                         StringBuilder sb  = new StringBuilder();
709
710                         sb.Append("<DocumentElement>");
711                         sb.Append("<Element1>");
712                         sb.Append("<ChildElement1>Text1</ChildElement1>");
713                         sb.Append("</Element1>");
714                         sb.Append("</DocumentElement>");
715                         DataSet ds = new DataSet();
716                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
717                         ds.InferXmlSchema(myStream,null);
718                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS75");
719                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS76");
720                         Assert.AreEqual(1, ds.Tables.Count, "DS77");
721                         Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS78");
722                 }
723
724                 [Test] public void InferXmlSchema_inferingTables3()
725                 {
726                         //Acroding to the msdn documantaion :
727                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
728                         //The document, or root, element will result in an inferred table if it has attributes
729                         //or child elements that will be inferred as columns.
730                         //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
731
732                         // inferingTables3
733                         StringBuilder sb  = new StringBuilder();
734
735                         sb.Append("<DocumentElement>");
736                         sb.Append("<Element1>Text1</Element1>");
737                         sb.Append("<Element2>Text2</Element2>");
738                         sb.Append("</DocumentElement>");
739                         DataSet ds = new DataSet();
740                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
741                         ds.InferXmlSchema(myStream,null);
742                         Assert.AreEqual("NewDataSet", ds.DataSetName, "DS79");
743                         Assert.AreEqual("DocumentElement", ds.Tables[0].TableName, "DS80");
744                         Assert.AreEqual(1, ds.Tables.Count, "DS81");
745                         Assert.AreEqual("Element1", ds.Tables[0].Columns["Element1"].ColumnName, "DS82");
746                         Assert.AreEqual("Element2", ds.Tables[0].Columns["Element2"].ColumnName, "DS83");
747                 }
748
749                 [Test] public void InferXmlSchema_inferingTables4()
750                 {
751                         //Acroding to the msdn documantaion :
752                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
753                         //The document, or root, element will result in an inferred table if it has attributes
754                         //or child elements that will be inferred as columns.
755                         //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet
756
757                         // inferingTables4
758                         StringBuilder sb  = new StringBuilder();
759
760                         sb.Append("<DocumentElement>");
761                         sb.Append("<Element1 attr1='value1' attr2='value2'/>");
762                         sb.Append("</DocumentElement>");
763                         DataSet ds = new DataSet();
764                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
765                         ds.InferXmlSchema(myStream,null);
766                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS84");
767                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS85");
768                         Assert.AreEqual(1, ds.Tables.Count, "DS86");
769                         Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS87");
770                         Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS88");
771                 }
772
773                 [Test]
774                 public void InferXmlSchema_inferingTables5()
775                 {
776                         //Acroding to the msdn documantaion :
777                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm
778                         //Elements that repeat will result in a single inferred table
779
780                         // inferingTables5
781                         StringBuilder sb  = new StringBuilder();
782
783                         sb.Append("<DocumentElement>");
784                         sb.Append("<Element1>Text1</Element1>");
785                         sb.Append("<Element1>Text2</Element1>");
786                         sb.Append("</DocumentElement>");
787                         DataSet ds = new DataSet();
788                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
789                         ds.InferXmlSchema(myStream,null);
790                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS89");
791                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS90");
792                         Assert.AreEqual(1, ds.Tables.Count, "DS91");
793                         Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS92");
794                 }
795                 #endregion
796
797                 #region inferringColumns
798                 [Test] public void InferXmlSchema_inferringColumns1()
799                 {
800                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
801                         // inferringColumns1
802                         StringBuilder sb  = new StringBuilder();
803
804                         sb.Append("<DocumentElement>");
805                         sb.Append("<Element1 attr1='value1' attr2='value2'/>");
806                         sb.Append("</DocumentElement>");
807                         DataSet ds = new DataSet();
808                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
809                         ds.InferXmlSchema(myStream,null);
810                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS93");
811                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS94");
812                         Assert.AreEqual(1, ds.Tables.Count, "DS95");
813                         Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS96");
814                         Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS97");
815                         Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping , "DS98");
816                         Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping , "DS99");
817                         Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr1"].DataType  , "DS100");
818                         Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr2"].DataType  , "DS101");
819                 }
820
821                 [Test] public void InferXmlSchema_inferringColumns2()
822                 {
823                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm
824                         //If an element has no child elements or attributes, it will be inferred as a column.
825                         //The ColumnMapping property of the column will be set to MappingType.Element.
826                         //The text for child elements is stored in a row in the table
827
828                         // inferringColumns2
829                         StringBuilder sb  = new StringBuilder();
830
831                         sb.Append("<DocumentElement>");
832                         sb.Append("<Element1>");
833                         sb.Append("<ChildElement1>Text1</ChildElement1>");
834                         sb.Append("<ChildElement2>Text2</ChildElement2>");
835                         sb.Append("</Element1>");
836                         sb.Append("</DocumentElement>");
837                         DataSet ds = new DataSet();
838                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
839                         ds.InferXmlSchema(myStream,null);
840                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS102");
841                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS103");
842                         Assert.AreEqual(1, ds.Tables.Count, "DS104");
843                         Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS105");
844                         Assert.AreEqual("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName, "DS106");
845                         Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping , "DS107");
846                         Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping , "DS108");
847                         Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType  , "DS109");
848                         Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType  , "DS110");
849                 }
850
851                 #endregion
852
853                 #region Inferring Relationships
854
855                 [Test] public void InferXmlSchema_inferringRelationships1()
856                 {
857                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm
858
859                         // inferringRelationships1
860                         StringBuilder sb  = new StringBuilder();
861
862                         sb.Append("<DocumentElement>");
863                         sb.Append("<Element1>");
864                         sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>");
865                         sb.Append("<ChildElement2>Text2</ChildElement2>");
866                         sb.Append("</Element1>");
867                         sb.Append("</DocumentElement>");
868                         DataSet ds = new DataSet();
869                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
870                         ds.InferXmlSchema(myStream,null);
871                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS111");
872                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS112");
873                         Assert.AreEqual("ChildElement1", ds.Tables[1].TableName, "DS113");
874                         Assert.AreEqual(2, ds.Tables.Count, "DS114");
875
876                         Assert.AreEqual("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "DS115");
877                         Assert.AreEqual(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping , "DS116");
878                         Assert.AreEqual(typeof(Int32), ds.Tables["Element1"].Columns["Element1_Id"].DataType  , "DS117");
879
880                         Assert.AreEqual("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "DS118");
881                         Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping , "DS119");
882                         Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType  , "DS120");
883
884                         Assert.AreEqual("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "DS121");
885                         Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping , "DS122");
886                         Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType  , "DS123");
887
888                         Assert.AreEqual("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "DS124");
889                         Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping , "DS125");
890                         Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType  , "DS126");
891
892                         Assert.AreEqual("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "DS127");
893                         Assert.AreEqual(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping , "DS128");
894                         Assert.AreEqual(typeof(Int32), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType  , "DS129");
895
896                         //Checking dataRelation :
897                         Assert.AreEqual("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "DS130");
898                         Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "DS131");
899                         Assert.AreEqual("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "DS132");
900                         Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "DS133");
901                         Assert.AreEqual(true, ds.Relations["Element1_ChildElement1"].Nested, "DS134");
902
903                         //Checking ForeignKeyConstraint
904
905                         ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"];
906
907                         Assert.AreEqual("Element1_Id", con.Columns[0].ColumnName, "DS135");
908                         Assert.AreEqual(Rule.Cascade, con.DeleteRule, "DS136");
909                         Assert.AreEqual(AcceptRejectRule.None, con.AcceptRejectRule, "DS137");
910                         Assert.AreEqual("Element1", con.RelatedTable.TableName, "DS138");
911                         Assert.AreEqual("ChildElement1", con.Table.TableName, "DS139");
912                 }
913
914                 #endregion
915
916                 #region Inferring Element Text
917
918                 [Test] public void InferXmlSchema_elementText1()
919                 {
920                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
921
922                         // elementText1
923                         StringBuilder sb  = new StringBuilder();
924
925                         sb.Append("<DocumentElement>");
926                         sb.Append("<Element1 attr1='value1'>Text1</Element1>");
927                         sb.Append("</DocumentElement>");
928                         DataSet ds = new DataSet();
929                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
930                         ds.InferXmlSchema(myStream,null);
931
932                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS140");
933                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS141");
934                         Assert.AreEqual(1, ds.Tables.Count, "DS142");
935
936                         Assert.AreEqual("attr1", ds.Tables["Element1"].Columns["attr1"].ColumnName, "DS143");
937                         Assert.AreEqual(MappingType.Attribute, ds.Tables["Element1"].Columns["attr1"].ColumnMapping , "DS144");
938                         Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["attr1"].DataType  , "DS145");
939
940                         Assert.AreEqual("Element1_Text", ds.Tables["Element1"].Columns["Element1_Text"].ColumnName, "DS146");
941                         Assert.AreEqual(MappingType.SimpleContent, ds.Tables["Element1"].Columns["Element1_Text"].ColumnMapping , "DS147");
942                         Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["Element1_Text"].DataType  , "DS148");
943                 }
944
945                 [Test] public void InferXmlSchema_elementText2()
946                 {
947                         //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm
948
949                         // elementText1
950                         StringBuilder sb  = new StringBuilder();
951
952                         sb.Append("<DocumentElement>");
953                         sb.Append("<Element1>");
954                         sb.Append("Text1");
955                         sb.Append("<ChildElement1>Text2</ChildElement1>");
956                         sb.Append("Text3");
957                         sb.Append("</Element1>");
958                         sb.Append("</DocumentElement>");
959                         DataSet ds = new DataSet();
960                         MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString()));
961                         ds.InferXmlSchema(myStream,null);
962
963                         Assert.AreEqual("DocumentElement", ds.DataSetName, "DS149");
964                         Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS150");
965                         Assert.AreEqual(1, ds.Tables.Count, "DS151");
966
967                         Assert.AreEqual("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "DS152");
968                         Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping , "DS153");
969                         Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType  , "DS154");
970                         Assert.AreEqual(1, ds.Tables["Element1"].Columns.Count, "DS155");
971                 }
972
973                 #endregion
974
975                 [Test] public void Locale()
976                 {
977                         DataSet ds = new DataSet("MyDataSet");
978                         CultureInfo culInfo = CultureInfo.CurrentCulture ;
979
980                         // Checking Locale default from system
981                         Assert.AreEqual(culInfo, ds.Locale  , "DS156");
982
983                         // Checking Locale get/set
984                         culInfo = new CultureInfo("fr"); // = french
985                         ds.Locale = culInfo ;
986                         Assert.AreEqual(culInfo , ds.Locale , "DS157");
987                 }
988
989                 [Test]
990                 [SetCulture ("cs-CZ")]
991                 public void DataSetSpecificCulture ()
992                 {                       
993                         var ds = new DataSet() ;
994                         ds.Locale = CultureInfo.GetCultureInfo (1033);
995                         var dt = ds.Tables.Add ("machine");
996                         dt.Locale = ds.Locale;
997                         Assert.AreSame (dt, ds.Tables["MACHINE"]);
998                 }               
999
1000                 [Test] public void MergeFailed()
1001                 {
1002                         EventRaised = false;
1003                         DataSet ds1,ds2;
1004                         ds1 = new DataSet();
1005                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
1006                         //add primary key to the FIRST column
1007                         ds1.Tables[0].PrimaryKey = new DataColumn[] {ds1.Tables[0].Columns[0]};
1008
1009                         //create target dataset which is a copy of the source
1010                         ds2 = ds1.Copy();
1011                         //clear the data
1012                         ds2.Clear();
1013                         //add primary key to the SECOND columnn
1014                         ds2.Tables[0].PrimaryKey = new DataColumn[] {ds2.Tables[0].Columns[1]};
1015                         //add a new row that already exists in the source dataset
1016                         //ds2.Tables[0].Rows.Add(ds1.Tables[0].Rows[0].ItemArray);
1017
1018                         //enforce constraints
1019                         ds2.EnforceConstraints = true;
1020                         ds1.EnforceConstraints = true;
1021
1022                         // Add MergeFailed event handler for the table.
1023                         ds2.MergeFailed += new MergeFailedEventHandler( Merge_Failed );
1024
1025                         ds2.Merge(ds1); //will raise MergeFailed event
1026
1027                         // MergeFailed event
1028                         Assert.AreEqual(true , EventRaised , "DS158");
1029                 }
1030                 private void Merge_Failed( object sender, MergeFailedEventArgs e )
1031                 {
1032                         EventRaised = true;
1033                 }
1034
1035                 [Test] public void Merge_ByDataRowsNoPreserveIgnoreMissingSchema()
1036                 {
1037                         DataTable dt = DataProvider.CreateParentDataTable();
1038                         dt.TableName = "Table1";
1039                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1040
1041                         //create target dataset (copy of source dataset)
1042                         DataSet dsTarget = new DataSet();
1043                         dsTarget.Tables.Add(dt.Copy());
1044
1045                         DataRow[] drArr = new DataRow[3];
1046                         //Update row
1047                         string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1048                         drArr[0] = dt.Select("ParentId=1")[0];
1049                         drArr[0][1]     = "NewValue";
1050                         //delete rows
1051                         drArr[1] = dt.Select("ParentId=2")[0];
1052                         drArr[1].Delete();
1053                         //add row
1054                         drArr[2] = dt.NewRow();
1055                         drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2"};
1056                         dt.Rows.Add(drArr[2]);
1057
1058                         dsTarget.Merge(drArr,false,MissingSchemaAction.Ignore );
1059
1060                         // Merge update row
1061                         Assert.AreEqual("NewValue", dsTarget.Tables["Table1"].Select("ParentId=1")[0][1] , "DS159");
1062
1063                         // Merge added row
1064                         Assert.AreEqual(1, dsTarget.Tables["Table1"].Select("ParentId=99").Length , "DS160");
1065
1066                         // Merge deleted row
1067                         Assert.AreEqual(0, dsTarget.Tables["Table1"].Select("ParentId=2").Length , "DS161");
1068                 }
1069
1070                 [Test] public void Merge_ByDataRowsPreserveMissingSchema()
1071                 {
1072                         DataTable dt = DataProvider.CreateParentDataTable();
1073                         dt.TableName = "Table1";
1074                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1075
1076                         //create target dataset (copy of source dataset)
1077                         DataSet dsTarget = new DataSet();
1078                         dsTarget.Tables.Add(dt.Copy());
1079
1080                         //add new column (for checking MissingSchemaAction)
1081                         DataColumn dc = new DataColumn("NewColumn",typeof(float));
1082                         dt.Columns.Add(dc);
1083
1084                         DataRow[] drArr = new DataRow[3];
1085
1086                         //Update row
1087                         string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1088                         drArr[0] = dt.Select("ParentId=1")[0];
1089                         drArr[0][1]     = "NewValue";
1090                         //delete rows
1091                         drArr[1] = dt.Select("ParentId=2")[0];
1092                         drArr[1].Delete();
1093                         //add row
1094                         drArr[2] = dt.NewRow();
1095                         drArr[2].ItemArray = new object[] {99 ,"NewRowValue1", "NewRowValue2",null};
1096                         dt.Rows.Add(drArr[2]);
1097
1098                         DataSet dsTarget1 = null;
1099
1100                         #region "Merge(drArr,true,MissingSchemaAction.Ignore )"
1101                         dsTarget1 = dsTarget.Copy();
1102                         dsTarget1.Merge(drArr,true,MissingSchemaAction.Ignore );
1103                         // Merge true,Ignore - Column
1104                         Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS162");
1105
1106                         // Merge true,Ignore - changed values
1107                         Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS163");
1108
1109                         // Merge true,Ignore - added values
1110                         Assert.AreEqual(1 , dsTarget1.Tables["Table1"].Select("ParentId=99").Length  , "DS164");
1111
1112                         // Merge true,Ignore - deleted row
1113                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS165");
1114                         #endregion
1115
1116                         #region "Merge(drArr,false,MissingSchemaAction.Ignore )"
1117                         dsTarget1 = dsTarget.Copy();
1118                         dsTarget1.Merge(drArr,false,MissingSchemaAction.Ignore );
1119                         // Merge true,Ignore - Column
1120                         Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS166");
1121
1122                         // Merge true,Ignore - changed values
1123                         Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS167");
1124
1125                         // Merge true,Ignore - added values
1126                         Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS168");
1127
1128                         // Merge true,Ignore - deleted row
1129                         Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS169");
1130                         #endregion
1131
1132                         #region "Merge(drArr,true,MissingSchemaAction.Add )"
1133                         dsTarget1 = dsTarget.Copy();
1134                         dsTarget1.Merge(drArr,true,MissingSchemaAction.Add  );
1135                         // Merge true,Ignore - Column
1136                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS170");
1137
1138                         // Merge true,Ignore - changed values
1139                         Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS171");
1140
1141                         // Merge true,Ignore - added values
1142                         Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS172");
1143
1144                         // Merge true,Ignore - deleted row
1145                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length >0, "DS173");
1146                         #endregion
1147
1148                         #region "Merge(drArr,false,MissingSchemaAction.Add )"
1149                         dsTarget1 = dsTarget.Copy();
1150                         dsTarget1.Merge(drArr,false,MissingSchemaAction.Add  );
1151                         // Merge true,Ignore - Column
1152                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS174");
1153
1154                         // Merge true,Ignore - changed values
1155                         Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS175");
1156
1157                         // Merge true,Ignore - added values
1158                         Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS176");
1159
1160                         // Merge true,Ignore - deleted row
1161                         Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS177");
1162                         #endregion
1163
1164                         #region "Merge(drArr,false/true,MissingSchemaAction.Error  )"
1165                         //              dsTarget1 = dsTarget.Copy();
1166                         //              // Merge true,Error - Column
1167                         //              try {
1168                         //                      dsTarget1.Merge(drArr,true,MissingSchemaAction.Error );
1169                         //                      Assert.Fail("DS178: Merge Failed to throw InvalidOperationException");
1170                         //              }
1171                         //              catch (InvalidOperationException) {}
1172                         //              catch (AssertionException exc) {throw  exc;}
1173                         //              catch (Exception exc)
1174                         //              {
1175                         //                      Assert.Fail("DS179: Merge. Wrong exception type. Got:" + exc);
1176                         //              }
1177                         //              
1178                         //              // Merge false,Error - Column
1179                         //              try {
1180                         //                      dsTarget1.Merge(drArr,false,MissingSchemaAction.Error );
1181                         //                      Assert.Fail("DS180: Merge Failed to throw InvalidOperationException");
1182                         //              }
1183                         //              catch (InvalidOperationException) {}
1184                         //              catch (AssertionException exc) {throw  exc;}
1185                         //              catch (Exception exc)
1186                         //              {
1187                         //                      Assert.Fail("DS181: Merge. Wrong exception type. Got:" + exc);
1188                         //              }
1189                         #endregion
1190                 }
1191
1192                 [Test] public void Merge_ByDataSet()
1193                 {
1194                         //create source dataset
1195                         DataSet ds = new DataSet();
1196                         DataTable dt = DataProvider.CreateParentDataTable();
1197                         dt.TableName = "Table1";
1198                         ds.Tables.Add(dt.Copy());
1199                         dt.TableName = "Table2";
1200                         //add primary key
1201                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1202                         ds.Tables.Add(dt.Copy());
1203
1204                         //create target dataset (copy of source dataset)
1205                         DataSet dsTarget = ds.Copy();
1206                         int iTable1RowsCount = dsTarget.Tables["Table1"].Rows.Count;
1207
1208                         //Source - add another table, don't exists on the target dataset
1209                         ds.Tables.Add(new DataTable("SomeTable"));
1210                         ds.Tables["SomeTable"].Columns.Add("Id");
1211                         ds.Tables["SomeTable"].Rows.Add(new object[] {777});
1212
1213                         //Target - add another table, don't exists on the source dataset
1214                         dsTarget.Tables.Add(new DataTable("SmallTable"));
1215                         dsTarget.Tables["SmallTable"].Columns.Add("Id");
1216                         dsTarget.Tables["SmallTable"].Rows.Add(new object[] {777});
1217
1218                         //update existing row
1219                         ds.Tables["Table2"].Select("ParentId=1")[0][1] = "OldValue1";
1220                         //add new row
1221                         object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1222                         ds.Tables["Table2"].Rows.Add(arrAddedRow);
1223                         //delete existing rows
1224                         foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1225                         {
1226                                 dr.Delete();
1227                         }
1228
1229                         // Merge - changed values
1230                         dsTarget.Merge(ds);
1231                         Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS182");
1232
1233                         // Merge - added values
1234                         Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray  , "DS183");
1235
1236                         // Merge - deleted row
1237                         Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS184");
1238
1239                         //Table1 rows count should be double (no primary key)
1240                         // Merge - Unchanged table 1
1241                         Assert.AreEqual(iTable1RowsCount * 2, dsTarget.Tables["Table1"].Rows.Count , "DS185");
1242
1243                         //SmallTable rows count should be the same
1244                         // Merge - Unchanged table 2
1245                         Assert.AreEqual(1, dsTarget.Tables["SmallTable"].Rows.Count , "DS186");
1246
1247                         //SomeTable - new table
1248                         // Merge - new table
1249                         Assert.AreEqual(true, dsTarget.Tables["SomeTable"] != null, "DS187");
1250                 }
1251
1252                 [Test] public void Merge_ByDataSetPreserve()
1253                 {
1254                         //create source dataset
1255                         DataSet ds = new DataSet();
1256                         DataTable dt = DataProvider.CreateParentDataTable();
1257                         dt.TableName = "Table1";
1258                         ds.Tables.Add(dt.Copy());
1259                         dt.TableName = "Table2";
1260                         //add primary key
1261                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1262                         ds.Tables.Add(dt.Copy());
1263
1264                         //create target dataset (copy of source dataset)
1265                         DataSet dsTarget1 = ds.Copy();
1266                         DataSet dsTarget2 = ds.Copy();
1267                         int iTable1RowsCount = dsTarget1.Tables["Table1"].Rows.Count;
1268
1269                         //update existing row
1270                         string oldValue = ds.Tables["Table2"].Select("ParentId=1")[0][1].ToString();
1271                         ds.Tables["Table2"].Select("ParentId=1")[0][1] = "NewValue";
1272                         //add new row
1273                         object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1274                         ds.Tables["Table2"].Rows.Add(arrAddedRow);
1275                         //delete existing rows
1276                         int iDeleteLength = dsTarget1.Tables["Table2"].Select("ParentId=2").Length;
1277                         foreach (DataRow dr in ds.Tables["Table2"].Select("ParentId=2"))
1278                         {
1279                                 dr.Delete();
1280                         }
1281
1282                         #region "Merge(ds,true)"
1283                         //only new added rows are merged (preserveChanges = true)
1284                         dsTarget1.Merge(ds,true);
1285                         // Merge - changed values
1286                         Assert.AreEqual(oldValue, dsTarget1.Tables["Table2"].Select("ParentId=1")[0][1] , "DS188");
1287
1288                         // Merge - added values
1289                         Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table2"].Select("ParentId=99")[0].ItemArray  , "DS189");
1290
1291                         // Merge - deleted row
1292                         Assert.AreEqual(iDeleteLength, dsTarget1.Tables["Table2"].Select("ParentId=2").Length , "DS190");
1293                         #endregion
1294
1295                         #region "Merge(ds,false)"
1296                         //all changes are merged (preserveChanges = false)
1297                         dsTarget2.Merge(ds,false);
1298                         // Merge - changed values
1299                         Assert.AreEqual("NewValue", dsTarget2.Tables["Table2"].Select("ParentId=1")[0][1] , "DS191");
1300
1301                         // Merge - added values
1302                         Assert.AreEqual(arrAddedRow, dsTarget2.Tables["Table2"].Select("ParentId=99")[0].ItemArray  , "DS192");
1303
1304                         // Merge - deleted row
1305                         Assert.AreEqual(0, dsTarget2.Tables["Table2"].Select("ParentId=2").Length , "DS193");
1306                         #endregion
1307                 }
1308
1309                 [Test] public void Merge_ByDataSetPreserveMissingSchemaAction()
1310                 {
1311                         //create source dataset
1312                         DataSet ds = new DataSet();
1313
1314                         DataTable dt = DataProvider.CreateParentDataTable();
1315                         dt.TableName = "Table1";
1316
1317                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1318
1319                         //add table to dataset
1320                         ds.Tables.Add(dt.Copy());
1321
1322                         dt = ds.Tables[0];
1323
1324                         //create target dataset (copy of source dataset)
1325                         DataSet dsTarget = ds.Copy();
1326
1327                         //add new column (for checking MissingSchemaAction)
1328                         DataColumn dc = new DataColumn("NewColumn",typeof(float));
1329                         //make the column to be primary key
1330                         dt.Columns.Add(dc);
1331
1332                         //add new table (for checking MissingSchemaAction)
1333                         ds.Tables.Add(new DataTable("NewTable"));
1334                         ds.Tables["NewTable"].Columns.Add("NewColumn1",typeof(int));
1335                         ds.Tables["NewTable"].Columns.Add("NewColumn2",typeof(long));
1336                         ds.Tables["NewTable"].Rows.Add(new object[] {1,2});
1337                         ds.Tables["NewTable"].Rows.Add(new object[] {3,4});
1338                         ds.Tables["NewTable"].PrimaryKey = new DataColumn[] {ds.Tables["NewTable"].Columns["NewColumn1"]};
1339
1340                         #region "ds,false,MissingSchemaAction.Add)"
1341                         DataSet dsTarget1 = dsTarget.Copy();
1342                         dsTarget1.Merge(ds,false,MissingSchemaAction.Add);
1343                         // Merge MissingSchemaAction.Add - Column
1344                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS194");
1345
1346                         // Merge MissingSchemaAction.Add - Table
1347                         Assert.AreEqual(true, dsTarget1.Tables.Contains("NewTable"), "DS195");
1348
1349                         //failed, should be success by MSDN Library documentation
1350                         //              // Merge MissingSchemaAction.Add - PrimaryKey
1351                         //              Assert.AreEqual(0, dsTarget1.Tables["NewTable"].PrimaryKey.Length, "DS196");
1352                         #endregion
1353
1354                         #region "ds,false,MissingSchemaAction.AddWithKey)"
1355                         //MissingSchemaAction.Add,MissingSchemaAction.AddWithKey - behave the same, checked only Add
1356
1357                         //              DataSet dsTarget2 = dsTarget.Copy();
1358                         //              dsTarget2.Merge(ds,false,MissingSchemaAction.AddWithKey);
1359                         //              // Merge MissingSchemaAction.AddWithKey - Column
1360                         //              Assert.AreEqual(true, dsTarget2.Tables["Table1"].Columns.Contains("NewColumn"), "DS197");
1361                         //
1362                         //              // Merge MissingSchemaAction.AddWithKey - Table
1363                         //              Assert.AreEqual(true, dsTarget2.Tables.Contains("NewTable"), "DS198");
1364                         //
1365                         //              // Merge MissingSchemaAction.AddWithKey - PrimaryKey
1366                         //              Assert.AreEqual(dsTarget2.Tables["NewTable"].Columns["NewColumn1"], dsTarget2.Tables["NewTable"].PrimaryKey[0], "DS199");
1367                         #endregion
1368
1369                         #region "ds,false,MissingSchemaAction.Error)"
1370                         //Error - throw System.Data.DataException, should throw InvalidOperationException
1371                         //              DataSet dsTarget3 ;
1372                         //              // Merge MissingSchemaAction.Error
1373                         //              dsTarget3 = dsTarget.Copy();
1374                         //              try {
1375                         //                      dsTarget3.Merge(ds,false,MissingSchemaAction.Error);
1376                         //                      Assert.Fail("DS200: Merge Failed to throw InvalidOperationException");
1377                         //              }
1378                         //              catch (InvalidOperationException) {}
1379                         //              catch (AssertionException exc) {throw  exc;}
1380                         //              catch (Exception exc)
1381                         //              {
1382                         //                      Assert.Fail("DS201: Merge. Wrong exception type. Got:" + exc);
1383                         //              }
1384                         #endregion
1385
1386                         #region "ds,false,MissingSchemaAction.Ignore )"
1387                         DataSet dsTarget4 = dsTarget.Copy();
1388                         dsTarget4.Merge(ds,false,MissingSchemaAction.Ignore );
1389                         // Merge MissingSchemaAction.Ignore - Column
1390                         Assert.AreEqual(false, dsTarget4.Tables["Table1"].Columns.Contains("NewColumn"), "DS202");
1391
1392                         // Merge MissingSchemaAction.Ignore - Table
1393                         Assert.AreEqual(false, dsTarget4.Tables.Contains("NewTable"), "DS203");
1394                         #endregion
1395                 }
1396
1397                 [Test] public void Merge_ByComplexDataSet()
1398                 {
1399                         //create source dataset
1400                         DataSet ds = new DataSet();
1401
1402                         ds.Tables.Add(DataProvider.CreateParentDataTable());
1403                         ds.Tables.Add(DataProvider.CreateChildDataTable());
1404                         ds.Tables["Child"].TableName = "Child2";
1405                         ds.Tables.Add(DataProvider.CreateChildDataTable());
1406
1407                         // Console.WriteLine(ds.Tables[0].TableName + ds.Tables[1].TableName + ds.Tables[2].TableName);
1408                         // Console.WriteLine(ds.Tables[2].Rows.Count.ToString());
1409
1410                         //craete a target dataset to the merge operation
1411                         DataSet dsTarget = ds.Copy();
1412
1413                         //craete a second target dataset to the merge operation
1414                         DataSet dsTarget1 = ds.Copy();
1415
1416                         //------------------ make some changes in the second target dataset schema --------------------
1417                         //add primary key
1418                         dsTarget1.Tables["Parent"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Parent"].Columns["ParentId"]};
1419                         dsTarget1.Tables["Child"].PrimaryKey = new DataColumn[] {dsTarget1.Tables["Child"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ChildId"]};
1420
1421                         //add Foreign Key (different name)
1422                         dsTarget1.Tables["Child2"].Constraints.Add("Child2_FK_2",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child2"].Columns["ParentId"]);
1423
1424                         //add relation (different name)
1425                         //dsTarget1.Relations.Add("Parent_Child_1",dsTarget1.Tables["Parent"].Columns["ParentId"],dsTarget1.Tables["Child"].Columns["ParentId"]);
1426
1427                         //------------------ make some changes in the source dataset schema --------------------
1428                         //add primary key
1429                         ds.Tables["Parent"].PrimaryKey = new DataColumn[] {ds.Tables["Parent"].Columns["ParentId"]};
1430                         ds.Tables["Child"].PrimaryKey = new DataColumn[] {ds.Tables["Child"].Columns["ParentId"],ds.Tables["Child"].Columns["ChildId"]};
1431
1432                         //unique column
1433                         ds.Tables["Parent"].Columns["String2"].Unique = true; //will not be merged
1434
1435                         //add Foreign Key
1436                         ds.Tables["Child2"].Constraints.Add("Child2_FK",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child2"].Columns["ParentId"]);
1437
1438                         //add relation
1439                         ds.Relations.Add("Parent_Child",ds.Tables["Parent"].Columns["ParentId"],ds.Tables["Child"].Columns["ParentId"]);
1440
1441                         //add allow null constraint
1442                         ds.Tables["Parent"].Columns["ParentBool"].AllowDBNull = false; //will not be merged
1443
1444                         //add Indentity column
1445                         ds.Tables["Parent"].Columns.Add("Indentity",typeof(int));
1446                         ds.Tables["Parent"].Columns["Indentity"].AutoIncrement = true;
1447                         ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep = 2;
1448
1449                         //modify default value
1450                         ds.Tables["Child"].Columns["String1"].DefaultValue = "Default"; //will not be merged
1451
1452                         //remove column
1453                         ds.Tables["Child"].Columns.Remove("String2"); //will not be merged
1454
1455                         //-------------------- begin to check ----------------------------------------------
1456                         // merge 1 - make sure the merge method invoked without exceptions
1457                         dsTarget.Merge(ds);
1458                         Assert.AreEqual("Success", "Success", "DS204");
1459
1460                         CompareResults_1("merge 1",ds,dsTarget);
1461
1462                         //merge again,
1463                         // merge 2 - make sure the merge method invoked without exceptions
1464                         dsTarget.Merge(ds);
1465                         Assert.AreEqual("Success", "Success", "DS205");
1466
1467                         CompareResults_1("merge 2",ds,dsTarget);
1468
1469                         // merge second dataset - make sure the merge method invoked without exceptions
1470                         dsTarget1.Merge(ds);
1471                         Assert.AreEqual("Success", "Success", "DS206");
1472
1473                         CompareResults_2("merge 3",ds,dsTarget1);
1474                 }
1475
1476                 [Test]
1477                 public void Merge_RelationWithoutConstraints ()
1478                 {
1479                         DataSet ds = new DataSet ();
1480
1481                         DataTable table1 = ds.Tables.Add ("table1");
1482                         DataTable table2 = ds.Tables.Add ("table2");
1483
1484                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1485                         DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1486
1487                         DataSet ds1 = ds.Copy ();
1488                         DataRelation rel = ds1.Relations.Add ("rel1", ds1.Tables[0].Columns[0], 
1489                                                                 ds1.Tables [1].Columns [0], false);
1490
1491                         ds.Merge (ds1);
1492                         Assert.AreEqual (1, ds.Relations.Count , "#1");
1493                         Assert.AreEqual (0, ds.Tables [0].Constraints.Count , "#2");
1494                         Assert.AreEqual (0, ds.Tables [1].Constraints.Count , "#3");
1495                 }
1496
1497                 [Test]
1498                 public void Merge_DuplicateConstraints ()
1499                 {
1500                         DataSet ds = new DataSet ();
1501
1502                         DataTable table1 = ds.Tables.Add ("table1");
1503                         DataTable table2 = ds.Tables.Add ("table2");
1504
1505                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1506                         DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1507
1508                         DataSet ds1 = ds.Copy ();
1509
1510                         DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol);
1511
1512                         ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns [0], ds1.Tables [1].Columns [0]);
1513
1514                         // No Exceptions shud be thrown
1515                         ds.Merge (ds1);
1516                         Assert.AreEqual (1, table2.Constraints.Count, "#1 Constraints shudnt be duplicated");
1517                 }
1518
1519                 [Test]
1520                 public void Merge_DuplicateConstraints_1 ()
1521                 {
1522                         DataSet ds = new DataSet ();
1523
1524                         DataTable table1 = ds.Tables.Add ("table1");
1525                         DataTable table2 = ds.Tables.Add ("table2");
1526
1527                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1528                         DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1529                         DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1530                         DataColumn ccol1 = table2.Columns.Add ("col2", typeof (int));
1531
1532                         DataSet ds1 = ds.Copy ();
1533
1534                         table2.Constraints.Add ("fk", pcol, ccol);
1535                         ds1.Tables [1].Constraints.Add ("fk", ds1.Tables [0].Columns ["col2"], ds1.Tables [1].Columns ["col2"]);
1536
1537                         // No Exceptions shud be thrown
1538                         ds.Merge (ds1);
1539                         Assert.AreEqual (2, table2.Constraints.Count, "#1 fk constraint shud be merged");
1540                         Assert.AreEqual ("Constraint1", table2.Constraints [1].ConstraintName, "#2 constraint name shud be changed");
1541                 }
1542
1543                 [Test]
1544                 public void CopyClone_RelationWithoutConstraints ()
1545                 {
1546                         DataSet ds = new DataSet ();
1547
1548                         DataTable table1 = ds.Tables.Add ("table1");
1549                         DataTable table2 = ds.Tables.Add ("table2");
1550
1551                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1552                         DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1553
1554                         DataRelation rel = ds.Relations.Add ("rel1", pcol, ccol, false);
1555
1556                         DataSet ds1 = ds.Copy ();
1557                         DataSet ds2 = ds.Clone ();
1558                         
1559                         Assert.AreEqual (1, ds1.Relations.Count, "#1");
1560                         Assert.AreEqual (1, ds2.Relations.Count, "#2");
1561
1562                         Assert.AreEqual (0, ds1.Tables [0].Constraints.Count, "#3");
1563                         Assert.AreEqual (0, ds1.Tables [1].Constraints.Count, "#4");
1564
1565                         Assert.AreEqual (0, ds2.Tables [0].Constraints.Count, "#5");
1566                         Assert.AreEqual (0, ds2.Tables [1].Constraints.Count, "#6");
1567                 }
1568
1569                 [Test]
1570                 public void Merge_ConstraintsFromReadXmlSchema ()
1571                 {
1572                         DataSet ds = new DataSet ();
1573                         ds.ReadXml ("Test/System.Data/TestMerge1.xml");
1574                         DataSet ds2 = new DataSet ();
1575                         ds2.Merge (ds, true, MissingSchemaAction.AddWithKey);
1576                         DataRelation c = ds2.Tables [0].ChildRelations [0];
1577                         Assert.IsNotNull (c.ParentKeyConstraint, "#1");
1578                         Assert.IsNotNull (c.ChildKeyConstraint, "#2");
1579                 }
1580
1581                 [Test]
1582                 [ExpectedException (typeof (DataException))]
1583                 public void Merge_MissingEventHandler ()
1584                 {
1585                         DataSet ds = new DataSet ();
1586                         DataTable table1 = ds.Tables.Add ("table1");
1587
1588                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1589                         DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1590                         
1591                         DataSet ds1 = ds.Copy ();
1592                         table1.PrimaryKey = new DataColumn[] {pcol};
1593                         ds1.Tables [0].PrimaryKey = new DataColumn[] {ds1.Tables [0].Columns [1]};
1594
1595                         // Exception shud be raised when handler is not set for MergeFailed Event
1596                         ds1.Merge (ds);
1597                 }
1598
1599                 [Test]
1600                 [ExpectedException (typeof(DataException))]
1601                 public void Merge_MissingColumn  ()
1602                 {
1603                         DataSet ds = new DataSet ();
1604                         DataTable table1 = ds.Tables.Add ("table1");
1605                         DataTable table2 = ds.Tables.Add ("table2");
1606
1607                         table1.Columns.Add ("col1", typeof (int));
1608                         table2.Columns.Add ("col1", typeof (int));
1609
1610                         DataSet ds1 = ds.Copy ();
1611
1612                         ds1.Tables [0].Columns.Add ("col2");
1613
1614                         ds.Merge (ds1, true, MissingSchemaAction.Error);
1615                 }
1616
1617                 [Test]
1618                 public void Merge_MissingConstraint ()
1619                 {
1620                         DataSet ds = new DataSet ();
1621                         DataTable table1 = ds.Tables.Add ("table1");
1622                         DataTable table2 = ds.Tables.Add ("table2");
1623                         table1.Columns.Add ("col1", typeof (int));
1624                         table2.Columns.Add ("col1", typeof (int));
1625
1626                         try {
1627                                 DataSet ds1 = ds.Copy ();
1628                                 DataSet ds2 = ds.Copy ();
1629                                 ds2.Tables [0].Constraints.Add ("uc", ds2.Tables [0].Columns [0], false);
1630                                 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1631                                 Assert.Fail ("#1 If uniqueconstraint is missing, exception shud be thrown");
1632                         }catch (DataException e) {
1633                         }
1634
1635                         try {
1636                                 DataSet ds1 = ds.Copy ();
1637                                 DataSet ds2 = ds.Copy ();
1638                                 ds2.Tables [0].Constraints.Add ("fk", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0]);
1639                                 ds1.Tables [0].Constraints.Add ("uc", ds1.Tables [0].Columns [0],false);
1640                                 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1641                                 Assert.Fail ("#2 If foreignkeyconstraint is missing, exception shud be thrown");
1642                         }catch (DataException e) {
1643                         }
1644
1645                         try {
1646                                 DataSet ds1 = ds.Copy ();
1647                                 DataSet ds2 = ds.Copy ();
1648                                 ds2.Relations.Add ("rel", ds2.Tables [0].Columns [0], ds2.Tables[1].Columns [0], false);
1649                                 ds1.Merge (ds2, true, MissingSchemaAction.Error);
1650                                 Assert.Fail ("#2 If datarelation is missing, exception shud be thrown");
1651                         }catch (ArgumentException e) {
1652                         }
1653                 }
1654
1655                 [Test]
1656                 [ExpectedException (typeof (DataException))]
1657                 public void Merge_PrimaryKeys_IncorrectOrder ()
1658                 {
1659                         DataSet ds = new DataSet ();
1660                         DataTable table1 = ds.Tables.Add ("table1");
1661                         DataTable table2 = ds.Tables.Add ("table2");
1662                         DataColumn pcol = table1.Columns.Add ("col1", typeof (int));
1663                         DataColumn pcol1 = table1.Columns.Add ("col2", typeof (int));
1664                         DataColumn ccol = table2.Columns.Add ("col1", typeof (int));
1665
1666                         DataSet ds1 = ds.Copy ();
1667                         table1.PrimaryKey = new DataColumn[] {pcol,pcol1};
1668                         ds1.Tables [0].PrimaryKey = new DataColumn [] {ds1.Tables[0].Columns [1], ds1.Tables [0].Columns [0]};
1669
1670                         // Though the key columns are the same, if the order is incorrect
1671                         // Exception must be raised
1672                         ds1.Merge (ds);
1673                 }
1674
1675                 void CompareResults_1(string Msg,DataSet ds, DataSet dsTarget)
1676                 {
1677                         // check Parent Primary key length
1678                         Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS207");
1679
1680                         // check Child Primary key length
1681                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS208");
1682
1683                         // check Parent Primary key columns
1684                         Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS209");
1685
1686                         // check Child Primary key columns[0]
1687                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS210");
1688
1689                         // check Child Primary key columns[1]
1690                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS211");
1691
1692                         // check Parent Unique columns
1693                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS212");
1694
1695                         // check Child2 Foreign Key name
1696                         Assert.AreEqual(dsTarget.Tables["Child2"].Constraints[0].ConstraintName , ds.Tables["Child2"].Constraints[0].ConstraintName , "DS213");
1697
1698                         // check dataset relation count
1699                         Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS214");
1700
1701                         // check dataset relation - Parent column
1702                         Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS215");
1703
1704                         // check dataset relation - Child column 
1705                         Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS216");
1706
1707                         // check allow null constraint
1708                         Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS217");
1709
1710                         // check Indentity column
1711                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS218");
1712
1713                         // check Indentity column - AutoIncrementStep
1714                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS219");
1715
1716                         // check Indentity column - AutoIncrement
1717                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS220");
1718
1719                         // check Indentity column - DefaultValue
1720                         Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS221");
1721
1722                         // check remove colum
1723                         Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS222");
1724                 }
1725
1726                 void CompareResults_2(string Msg,DataSet ds, DataSet dsTarget)
1727                 {
1728                         // check Parent Primary key length
1729                         Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey.Length , ds.Tables["Parent"].PrimaryKey.Length , "DS223");
1730
1731                         // check Child Primary key length
1732                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey.Length , ds.Tables["Child"].PrimaryKey.Length , "DS224");
1733
1734                         // check Parent Primary key columns
1735                         Assert.AreEqual(dsTarget.Tables["Parent"].PrimaryKey[0].ColumnName, ds.Tables["Parent"].PrimaryKey[0].ColumnName , "DS225");
1736
1737                         // check Child Primary key columns[0]
1738                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[0].ColumnName, ds.Tables["Child"].PrimaryKey[0].ColumnName , "DS226");
1739
1740                         // check Child Primary key columns[1]
1741                         Assert.AreEqual(dsTarget.Tables["Child"].PrimaryKey[1].ColumnName, ds.Tables["Child"].PrimaryKey[1].ColumnName , "DS227");
1742
1743                         // check Parent Unique columns
1744                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["String2"].Unique, ds.Tables["Parent"].Columns["String2"].Unique , "DS228");
1745
1746                         // check Child2 Foreign Key name
1747                         Assert.AreEqual("Child2_FK_2" , dsTarget.Tables["Child2"].Constraints[0].ConstraintName, "DS229");
1748
1749                         // check dataset relation count
1750                         Assert.AreEqual(dsTarget.Relations.Count , ds.Relations.Count , "DS230");
1751
1752                         // check dataset relation - Parent column
1753                         Assert.AreEqual(dsTarget.Relations[0].ParentColumns[0].ColumnName , ds.Relations[0].ParentColumns[0].ColumnName , "DS231");
1754
1755                         // check dataset relation - Child column 
1756                         Assert.AreEqual(dsTarget.Relations[0].ChildColumns[0].ColumnName , ds.Relations[0].ChildColumns[0].ColumnName , "DS232");
1757
1758                         // check allow null constraint
1759                         Assert.AreEqual(true, dsTarget.Tables["Parent"].Columns["ParentBool"].AllowDBNull, "DS233");
1760
1761                         // check Indentity column
1762                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns.Contains("Indentity"), ds.Tables["Parent"].Columns.Contains("Indentity"), "DS234");
1763
1764                         // check Indentity column - AutoIncrementStep
1765                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, ds.Tables["Parent"].Columns["Indentity"].AutoIncrementStep, "DS235");
1766
1767                         // check Indentity column - AutoIncrement
1768                         Assert.AreEqual(dsTarget.Tables["Parent"].Columns["Indentity"].AutoIncrement, ds.Tables["Parent"].Columns["Indentity"].AutoIncrement, "DS236");
1769
1770                         // check Indentity column - DefaultValue
1771                         Assert.AreEqual(true, dsTarget.Tables["Child"].Columns["String1"].DefaultValue == DBNull.Value , "DS237");
1772
1773                         // check remove colum
1774                         Assert.AreEqual(true, dsTarget.Tables["Child"].Columns.Contains("String2"), "DS238");
1775                         //TestCase for bug #3168
1776                         // Check Relation.Nested value, TestCase for bug #3168
1777                         DataSet orig = new DataSet();
1778
1779                         DataTable parent = orig.Tables.Add("Parent");
1780                         parent.Columns.Add("Id", typeof(int));
1781                         parent.Columns.Add("col1", typeof(string));
1782                         parent.Rows.Add(new object[] {0, "aaa"});
1783
1784                         DataTable child = orig.Tables.Add("Child");
1785                         child.Columns.Add("ParentId", typeof(int));
1786                         child.Columns.Add("col1", typeof(string));
1787                         child.Rows.Add(new object[] {0, "bbb"});
1788
1789                         orig.Relations.Add("Parent_Child", parent.Columns["Id"], child.Columns["ParentId"]);
1790                         orig.Relations["Parent_Child"].Nested = true;
1791
1792                         DataSet merged = new DataSet();
1793                         merged.Merge(orig);
1794                         Assert.AreEqual(orig.Relations["Parent_Child"].Nested, merged.Relations["Parent_Child"].Nested, "DS239");
1795                 }
1796
1797                 [Test] public void Merge_ByDataTable()
1798                 {
1799                         //create source dataset
1800                         DataSet ds = new DataSet();
1801                         //create datatable
1802                         DataTable dt = DataProvider.CreateParentDataTable();
1803                         dt.TableName = "Table1";
1804                         //add a copy of the datatable to the dataset
1805                         ds.Tables.Add(dt.Copy());
1806
1807                         dt.TableName = "Table2";
1808                         //add primary key
1809                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1810                         ds.Tables.Add(dt.Copy());
1811                         //now the dataset hase two tables
1812
1813                         //create target dataset (copy of source dataset)
1814                         DataSet dsTarget = ds.Copy();
1815
1816                         dt = ds.Tables["Table2"];
1817                         //update existing row
1818                         dt.Select("ParentId=1")[0][1] = "OldValue1";
1819                         //add new row
1820                         object[] arrAddedRow = new object[] {99,"NewValue1","NewValue2",new DateTime(0),0.5,true};
1821                         dt.Rows.Add(arrAddedRow);
1822                         //delete existing rows
1823                         foreach (DataRow dr in dt.Select("ParentId=2"))
1824                         {
1825                                 dr.Delete();
1826                         }
1827
1828                         // Merge - changed values
1829                         dsTarget.Merge(dt);
1830                         Assert.AreEqual("OldValue1", dsTarget.Tables["Table2"].Select("ParentId=1")[0][1] , "DS240");
1831
1832                         // Merge - added values
1833                         Assert.AreEqual(arrAddedRow, dsTarget.Tables["Table2"].Select("ParentId=99")[0].ItemArray  , "DS241");
1834
1835                         // Merge - deleted row
1836                         Assert.AreEqual(0, dsTarget.Tables["Table2"].Select("ParentId=2").Length , "DS242");
1837
1838                         //test case added due to a reported bug from infogate
1839                         //when merging a DataTable with TableName=null, GH throw null reference exception.
1840                         ds = new DataSet();
1841                         dt = new DataTable();
1842                         dt.Columns.Add("Col1");
1843                         dt.Rows.Add(new object[] {1});
1844
1845                         // Merge - add a table with no name
1846                         ds.Merge(dt);
1847                         Assert.AreEqual(1, ds.Tables.Count, "DS243");
1848
1849                         // Merge - add a table with no name - check Rows.Count
1850                         Assert.AreEqual(dt.Rows.Count , ds.Tables[0].Rows.Count , "DS244");
1851                 }
1852
1853                 [Test] public void Merge_ByDataTablePreserveMissingSchemaAction()
1854                 {
1855                         DataTable dt = DataProvider.CreateParentDataTable();
1856                         dt.TableName = "Table1";
1857                         dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
1858
1859                         //create target dataset (copy of source dataset)
1860                         DataSet dsTarget = new DataSet();
1861                         dsTarget.Tables.Add(dt.Copy());
1862
1863                         //add new column (for checking MissingSchemaAction)
1864                         DataColumn dc = new DataColumn("NewColumn",typeof(float));
1865                         dt.Columns.Add(dc);
1866
1867                         //Update row
1868                         string OldValue = dt.Select("ParentId=1")[0][1].ToString();
1869                         dt.Select("ParentId=1")[0][1] = "NewValue";
1870                         //delete rows
1871                         dt.Select("ParentId=2")[0].Delete();
1872                         //add row
1873                         object[] arrAddedRow = new object[] {99,"NewRowValue1","NewRowValue2",new DateTime(0),0.5,true};
1874                         dt.Rows.Add(arrAddedRow);
1875
1876                         #region "Merge(dt,true,MissingSchemaAction.Ignore )"
1877                         DataSet dsTarget1 = dsTarget.Copy();
1878                         dsTarget1.Merge(dt,true,MissingSchemaAction.Ignore );
1879                         // Merge true,Ignore - Column
1880                         Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS245");
1881
1882                         // Merge true,Ignore - changed values
1883                         Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS246");
1884
1885                         // Merge true,Ignore - added values
1886                         Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray  , "DS247");
1887
1888                         // Merge true,Ignore - deleted row
1889                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS248");
1890                         #endregion
1891
1892                         #region "Merge(dt,false,MissingSchemaAction.Ignore )"
1893
1894                         dsTarget1 = dsTarget.Copy();
1895                         dsTarget1.Merge(dt,false,MissingSchemaAction.Ignore );
1896                         // Merge true,Ignore - Column
1897                         Assert.AreEqual(false, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS249");
1898
1899                         // Merge true,Ignore - changed values
1900                         Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS250");
1901
1902                         // Merge true,Ignore - added values
1903                         Assert.AreEqual(arrAddedRow, dsTarget1.Tables["Table1"].Select("ParentId=99")[0].ItemArray  , "DS251");
1904
1905                         // Merge true,Ignore - deleted row
1906                         Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS252");
1907                         #endregion
1908
1909                         #region "Merge(dt,true,MissingSchemaAction.Add  )"
1910                         dsTarget1 = dsTarget.Copy();
1911                         dsTarget1.Merge(dt,true,MissingSchemaAction.Add  );
1912                         // Merge true,Add - Column
1913                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS253");
1914
1915                         // Merge true,Add - changed values
1916                         Assert.AreEqual(OldValue, dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS254");
1917
1918                         // Merge true,Add - added values
1919                         Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS255");
1920
1921                         // Merge true,Add - deleted row
1922                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Select("ParentId=2").Length > 0, "DS256");
1923                         #endregion
1924
1925                         #region "Merge(dt,false,MissingSchemaAction.Add  )"
1926                         dsTarget1 = dsTarget.Copy();
1927                         dsTarget1.Merge(dt,false,MissingSchemaAction.Add  );
1928                         // Merge true,Add - Column
1929                         Assert.AreEqual(true, dsTarget1.Tables["Table1"].Columns.Contains("NewColumn"), "DS257");
1930
1931                         // Merge true,Add - changed values
1932                         Assert.AreEqual("NewValue", dsTarget1.Tables["Table1"].Select("ParentId=1")[0][1] , "DS258");
1933
1934                         // Merge true,Add - added values
1935                         Assert.AreEqual(1, dsTarget1.Tables["Table1"].Select("ParentId=99").Length , "DS259");
1936
1937                         // Merge true,Add - deleted row
1938                         Assert.AreEqual(0, dsTarget1.Tables["Table1"].Select("ParentId=2").Length , "DS260");
1939                         #endregion
1940
1941                         #region "Merge(dt,false/true,MissingSchemaAction.Error  )"
1942                         //              dsTarget1 = dsTarget.Copy();
1943                         //              // Merge true,Error - Column
1944                         //              try {
1945                         //                      dsTarget1.Merge(dt,true,MissingSchemaAction.Error );
1946                         //                      Assert.Fail("DS261: Merge Failed to throw InvalidOperationException");
1947                         //              }
1948                         //              catch (InvalidOperationException) {}
1949                         //              catch (AssertionException exc) {throw  exc;}
1950                         //              catch (Exception exc)
1951                         //              {
1952                         //                      Assert.Fail("DS262: Merge. Wrong exception type. Got:" + exc);
1953                         //              }
1954                         //
1955                         //              // Merge false,Error - Column
1956                         //              try {
1957                         //                      dsTarget1.Merge(dt,false,MissingSchemaAction.Error );
1958                         //                      Assert.Fail("DS263: Merge Failed to throw InvalidOperationException");
1959                         //              }
1960                         //              catch (InvalidOperationException) {}
1961                         //              catch (AssertionException exc) {throw  exc;}
1962                         //              catch (Exception exc)
1963                         //              {
1964                         //                      Assert.Fail("DS264: Merge. Wrong exception type. Got:" + exc);
1965                         //              }
1966                         #endregion
1967                 }
1968
1969                 [Test] public void Namespace()
1970                 {
1971                         DataSet ds = new DataSet();
1972
1973                         // Checking Namespace default
1974                         Assert.AreEqual(String.Empty, ds.Namespace, "DS265");
1975
1976                         // Checking Namespace set/get
1977                         String s = "MyNamespace";
1978                         ds.Namespace=s;
1979                         Assert.AreEqual(s, ds.Namespace, "DS266");
1980                 }
1981
1982                 [Test] public void Prefix()
1983                 {
1984                         DataSet ds = new DataSet();
1985
1986                         // Checking Prefix default
1987                         Assert.AreEqual(String.Empty, ds.Prefix , "DS267");
1988
1989                         // Checking Prefix set/get
1990                         String s = "MyPrefix";
1991                         ds.Prefix=s;
1992                         Assert.AreEqual(s, ds.Prefix, "DS268");
1993                 }
1994
1995                 [Test] public void ReadXmlSchema_ByStream()
1996                 {
1997                         DataSet ds1 = new DataSet();
1998                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
1999                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2000
2001                         MemoryStream ms = new MemoryStream();
2002                         //write xml  schema only
2003                         ds1.WriteXmlSchema(ms);
2004
2005                         MemoryStream ms1 = new MemoryStream(ms.GetBuffer());
2006                         //copy schema
2007                         DataSet ds2 = new DataSet();
2008                         ds2.ReadXmlSchema(ms1);
2009
2010                         //check xml schema
2011                         // ReadXmlSchema - Tables count
2012                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS269");
2013
2014                         // ReadXmlSchema - Tables 0 Col count
2015                         Assert.AreEqual(ds1.Tables[0].Columns.Count  , ds2.Tables[0].Columns.Count , "DS270");
2016
2017                         // ReadXmlSchema - Tables 1 Col count
2018                         Assert.AreEqual(ds1.Tables[1].Columns.Count  , ds2.Tables[1].Columns.Count  , "DS271");
2019
2020                         //check some colummns types
2021                         // ReadXmlSchema - Tables 0 Col type
2022                         Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS272");
2023
2024                         // ReadXmlSchema - Tables 1 Col type
2025                         Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS273");
2026
2027                         //check that no data exists
2028                         // ReadXmlSchema - Table 1 row count
2029                         Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS274");
2030
2031                         // ReadXmlSchema - Table 2 row count
2032                         Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS275");
2033                 }
2034
2035                 [Test] public void ReadXmlSchema_ByFileName()
2036                 {
2037                         string sTempFileName = Path.Combine (Path.GetTempPath (), "tmpDataSet_ReadWriteXml_43899.xml");
2038
2039                         DataSet ds1 = new DataSet();
2040                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2041                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2042
2043                         //write xml file, schema only
2044                         ds1.WriteXmlSchema(sTempFileName);
2045
2046                         //copy both data and schema
2047                         DataSet ds2 = new DataSet();
2048
2049                         ds2.ReadXmlSchema(sTempFileName);
2050
2051                         //check xml schema
2052                         // ReadXmlSchema - Tables count
2053                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS276");
2054
2055                         // ReadXmlSchema - Tables 0 Col count
2056                         Assert.AreEqual(ds1.Tables[0].Columns.Count  , ds2.Tables[0].Columns.Count , "DS277");
2057
2058                         // ReadXmlSchema - Tables 1 Col count
2059                         Assert.AreEqual(ds1.Tables[1].Columns.Count  , ds2.Tables[1].Columns.Count  , "DS278");
2060
2061                         //check some colummns types
2062                         // ReadXmlSchema - Tables 0 Col type
2063                         Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS279");
2064
2065                         // ReadXmlSchema - Tables 1 Col type
2066                         Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS280");
2067
2068                         //check that no data exists
2069                         // ReadXmlSchema - Table 1 row count
2070                         Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS281");
2071
2072                         // ReadXmlSchema - Table 2 row count
2073                         Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS282");
2074
2075                         //try to delete the file
2076                         File.Delete(sTempFileName);
2077                 }
2078
2079                 [Test] public void ReadXmlSchema_ByTextReader()
2080                 {
2081                         DataSet ds1 = new DataSet();
2082                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2083                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2084
2085                         StringWriter sw = new StringWriter();
2086                         //write xml file, schema only
2087                         ds1.WriteXmlSchema(sw);
2088
2089                         StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
2090                         //copy both data and schema
2091                         DataSet ds2 = new DataSet();
2092                         ds2.ReadXmlSchema(sr);
2093
2094                         //check xml schema
2095                         // ReadXmlSchema - Tables count
2096                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS283");
2097
2098                         // ReadXmlSchema - Tables 0 Col count
2099                         Assert.AreEqual(ds1.Tables[0].Columns.Count  , ds2.Tables[0].Columns.Count , "DS284");
2100
2101                         // ReadXmlSchema - Tables 1 Col count
2102                         Assert.AreEqual(ds1.Tables[1].Columns.Count  , ds2.Tables[1].Columns.Count  , "DS285");
2103
2104                         //check some colummns types
2105                         // ReadXmlSchema - Tables 0 Col type
2106                         Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS286");
2107
2108                         // ReadXmlSchema - Tables 1 Col type
2109                         Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS287");
2110
2111                         //check that no data exists
2112                         // ReadXmlSchema - Table 1 row count
2113                         Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS288");
2114
2115                         // ReadXmlSchema - Table 2 row count
2116                         Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS289");
2117                 }
2118
2119                 [Test] public void ReadXmlSchema_ByXmlReader()
2120                 {
2121                         DataSet ds1 = new DataSet();
2122                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2123                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2124
2125                         StringWriter sw = new StringWriter();
2126                         XmlTextWriter xmlTW = new XmlTextWriter(sw);
2127                         //write xml file, schema only
2128                         ds1.WriteXmlSchema(xmlTW);
2129                         xmlTW.Flush();
2130
2131                         StringReader sr = new StringReader(sw.ToString());
2132                         XmlTextReader xmlTR = new XmlTextReader(sr);
2133
2134                         //copy both data and schema
2135                         DataSet ds2 = new DataSet();
2136                         ds2.ReadXmlSchema(xmlTR);
2137
2138                         //check xml schema
2139                         // ReadXmlSchema - Tables count
2140                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS290");
2141
2142                         // ReadXmlSchema - Tables 0 Col count
2143                         Assert.AreEqual(ds1.Tables[0].Columns.Count  , ds2.Tables[0].Columns.Count , "DS291");
2144
2145                         // ReadXmlSchema - Tables 1 Col count
2146                         Assert.AreEqual(ds1.Tables[1].Columns.Count  , ds2.Tables[1].Columns.Count  , "DS292");
2147
2148                         //check some colummns types
2149                         // ReadXmlSchema - Tables 0 Col type
2150                         Assert.AreEqual(ds1.Tables[0].Columns[0].GetType() , ds2.Tables[0].Columns[0].GetType() , "DS293");
2151
2152                         // ReadXmlSchema - Tables 1 Col type
2153                         Assert.AreEqual(ds1.Tables[1].Columns[3].GetType() , ds2.Tables[1].Columns[3].GetType() , "DS294");
2154
2155                         //check that no data exists
2156                         // ReadXmlSchema - Table 1 row count
2157                         Assert.AreEqual(0, ds2.Tables[0].Rows.Count , "DS295");
2158
2159                         // ReadXmlSchema - Table 2 row count
2160                         Assert.AreEqual(0, ds2.Tables[1].Rows.Count , "DS296");
2161                 }
2162
2163                 [Test]
2164                 [Category ("NotWorking")]
2165                 public void ReadXml_Strg()
2166                 {
2167                         string sTempFileName = "tmpDataSet_ReadWriteXml_43894.xml"  ;
2168
2169                         DataSet ds1 = new DataSet();
2170                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2171                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2172
2173                         //add data to check GH bug of DataSet.ReadXml of empty strings
2174                         ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2175                         ds1.Tables[1].Rows.Add(new object[] {7,2," ","          ",new DateTime(2000,1,1,0,0,0,0),35});
2176                         ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2177
2178                         //write xml file, data only
2179                         ds1.WriteXml(sTempFileName);
2180
2181                         //copy both data and schema
2182                         DataSet ds2 = ds1.Copy();
2183                         //clear the data
2184                         ds2.Clear();
2185
2186                         ds2.ReadXml(sTempFileName);
2187
2188                         //check xml data
2189                         // ReadXml - Tables count
2190                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS297");
2191
2192                         // ReadXml - Table 1 row count
2193                         Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS298");
2194
2195                         // ReadXml - Table 2 row count
2196                         Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS299");
2197
2198                         //try to delete the file
2199                         File.Delete(sTempFileName);
2200                 }
2201
2202                 [Test]
2203                 [Category ("NotWorking")]
2204                 public void ReadXml_Strm()
2205                 {
2206                         DataSet ds1 = new DataSet();
2207                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2208                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2209
2210                         //add data to check GH bug of DataSet.ReadXml of empty strings
2211                         ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2212                         ds1.Tables[1].Rows.Add(new object[] {7,2," ","          ",new DateTime(2000,1,1,0,0,0,0),35});
2213                         ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2214
2215                         MemoryStream ms = new MemoryStream();
2216                         //write xml file, data only
2217                         ds1.WriteXml(ms);
2218
2219                         //copy both data and schema
2220                         DataSet ds2 = ds1.Copy();
2221                         //clear the data
2222                         ds2.Clear();
2223
2224                         ms.Position=0;
2225                         ds2.ReadXml(ms);
2226
2227                         //check xml data
2228                         // ReadXml - Tables count
2229                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS300");
2230
2231                         // ReadXml - Table 1 row count
2232                         Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS301");
2233
2234                         // ReadXml - Table 2 row count
2235                         Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS302");
2236
2237                         ms.Close();
2238                 }
2239
2240                 [Test] public void ReadXml_Strm2()
2241                 {
2242                         string input = string.Empty;
2243
2244                         StringReader sr;
2245                         DataSet ds = new DataSet();
2246
2247                         input += "<?xml version=\"1.0\"?>";
2248                         input += "<Stock name=\"MSFT\">";
2249                         input += "              <Company name=\"Microsoft Corp.\"/>";
2250                         input += "              <Price type=\"high\">";
2251                         input += "                      <Value>10.0</Value>";           
2252                         input += "                      <Date>01/20/2000</Date>";
2253                         input += "              </Price>";
2254                         input += "              <Price type=\"low\">";
2255                         input += "                      <Value>1.0</Value>";
2256                         input += "                      <Date>03/21/2002</Date>";
2257                         input += "              </Price>";
2258                         input += "              <Price type=\"current\">";
2259                         input += "                      <Value>3.0</Value>";
2260                         input += "                      <Date>TODAY</Date>";
2261                         input += "              </Price>";
2262                         input += "</Stock>";
2263
2264                         sr = new StringReader(input);
2265
2266                         ds.ReadXml(sr);
2267
2268                         // Relation Count
2269                         Assert.AreEqual(2 , ds.Relations.Count , "DS303");
2270
2271                         // RelationName 1
2272                         Assert.AreEqual("Stock_Company" , ds.Relations[0].RelationName , "DS304");
2273
2274                         // RelationName 2
2275                         Assert.AreEqual("Stock_Price" , ds.Relations[1].RelationName , "DS305");
2276
2277                         // Tables count
2278                         Assert.AreEqual(3, ds.Tables.Count , "DS306");
2279
2280                         // Tables[0] ChildRelations count
2281                         Assert.AreEqual(2, ds.Tables[0].ChildRelations.Count , "DS307");
2282
2283                         // Tables[0] ChildRelations[0] name
2284                         Assert.AreEqual("Stock_Company", ds.Tables[0].ChildRelations[0].RelationName , "DS308");
2285
2286                         // Tables[0] ChildRelations[1] name
2287                         Assert.AreEqual("Stock_Price", ds.Tables[0].ChildRelations[1].RelationName , "DS309");
2288
2289                         // Tables[1] ChildRelations count
2290                         Assert.AreEqual(0, ds.Tables[1].ChildRelations.Count , "DS310");
2291
2292                         // Tables[2] ChildRelations count
2293                         Assert.AreEqual(0, ds.Tables[2].ChildRelations.Count , "DS311");
2294
2295                         // Tables[0] ParentRelations count
2296                         Assert.AreEqual(0, ds.Tables[0].ParentRelations.Count , "DS312");
2297
2298                         // Tables[1] ParentRelations count
2299                         Assert.AreEqual(1, ds.Tables[1].ParentRelations.Count , "DS313");
2300
2301                         // Tables[1] ParentRelations[0] name
2302                         Assert.AreEqual("Stock_Company", ds.Tables[1].ParentRelations[0].RelationName , "DS314");
2303
2304                         // Tables[2] ParentRelations count
2305                         Assert.AreEqual(1, ds.Tables[2].ParentRelations.Count , "DS315");
2306
2307                         // Tables[2] ParentRelations[0] name
2308                         Assert.AreEqual("Stock_Price", ds.Tables[2].ParentRelations[0].RelationName , "DS316");
2309                 }
2310                 [Test] public void ReadXml_Strm3()
2311                 {
2312                         DataSet ds = new DataSet("TestDataSet");
2313                         string input = string.Empty;
2314                         StringReader sr;
2315
2316                         input += "<?xml version=\"1.0\" standalone=\"yes\"?>";
2317                         input += "<Stocks><Stock name=\"MSFT\"><Company name=\"Microsoft Corp.\" /><Price type=\"high\"><Value>10.0</Value>";
2318                         input += "<Date>01/20/2000</Date></Price><Price type=\"low\"><Value>10</Value><Date>03/21/2002</Date></Price>";
2319                         input += "<Price type=\"current\"><Value>3.0</Value><Date>TODAY</Date></Price></Stock><Stock name=\"GE\">";
2320                         input += "<Company name=\"General Electric\" /><Price type=\"high\"><Value>22.23</Value><Date>02/12/2001</Date></Price>";
2321                         input += "<Price type=\"low\"><Value>1.97</Value><Date>04/20/2003</Date></Price><Price type=\"current\"><Value>3.0</Value>";
2322                         input += "<Date>TODAY</Date></Price></Stock></Stocks>";
2323                         sr = new StringReader(input);
2324                         ds.EnforceConstraints = false;
2325                         ds.ReadXml(sr);
2326
2327                         //Test that all added columns have "Hidden" mapping type.
2328                         // StockTable.Stock_IdCol.ColumnMapping
2329                         Assert.AreEqual(MappingType.Hidden, ds.Tables["Stock"].Columns["Stock_Id"].ColumnMapping, "DS317");
2330
2331                         // CompanyTable.Stock_IdCol.ColumnMapping
2332                         Assert.AreEqual(MappingType.Hidden, ds.Tables["Company"].Columns["Stock_Id"].ColumnMapping, "DS318");
2333
2334                         // PriceTable.Stock_IdCol.ColumnMapping
2335                         Assert.AreEqual(MappingType.Hidden, ds.Tables["Price"].Columns["Stock_Id"].ColumnMapping, "DS319");
2336                 }
2337
2338                 [Test] public void ReadXml_Strm4()
2339                 {
2340                         m_ds = new DataSet("Stocks");
2341                         string input = string.Empty;
2342                         StringReader sr;
2343
2344                         input += "<?xml version=\"1.0\"?>";
2345                         input += "<Stocks>";
2346                         input += "              <Stock name=\"MSFT\">";
2347                         input += "                      <Company name=\"Microsoft Corp.\" />";
2348                         input += "                      <Company name=\"General Electric\"/>";
2349                         input += "                      <Price type=\"high\">";
2350                         input += "                              <Value>10.0</Value>";
2351                         input += "                              <Date>01/20/2000</Date>";
2352                         input += "                      </Price>";
2353                         input += "                      <Price type=\"low\">";
2354                         input += "                              <Value>1.0</Value>";
2355                         input += "                              <Date>03/21/2002</Date>";
2356                         input += "                      </Price>";
2357                         input += "                      <Price type=\"current\">";
2358                         input += "                              <Value>3.0</Value>";
2359                         input += "                              <Date>TODAY</Date>";
2360                         input += "                      </Price>";
2361                         input += "              </Stock>";
2362                         input += "              <Stock name=\"GE\">";
2363                         input += "                      <Company name=\"GE company\"/>";
2364                         input += "                      <Price type=\"high\">";
2365                         input += "                              <Value>22.23</Value>";
2366                         input += "                              <Date>02/12/2001</Date>";
2367                         input += "                      </Price>";
2368                         input += "                      <Price type=\"low\">";
2369                         input += "                              <Value>1.97</Value>";
2370                         input += "                              <Date>04/20/2003</Date>";
2371                         input += "                      </Price>";
2372                         input += "                      <Price type=\"current\">";
2373                         input += "                              <Value>3.0</Value>";
2374                         input += "                              <Date>TODAY</Date>";
2375                         input += "                      </Price>";
2376                         input += "              </Stock>";
2377                         input += "              <Stock name=\"Intel\">";
2378                         input += "                      <Company name=\"Intel Corp.\"/>";
2379                         input += "                      <Company name=\"Test1\" />";
2380                         input += "                      <Company name=\"Test2\"/>";
2381                         input += "                      <Price type=\"high\">";
2382                         input += "                              <Value>15.0</Value>";
2383                         input += "                              <Date>01/25/2000</Date>";
2384                         input += "                      </Price>";
2385                         input += "                      <Price type=\"low\">";
2386                         input += "                              <Value>1.0</Value>";
2387                         input += "                              <Date>03/23/2002</Date>";
2388                         input += "                      </Price>";
2389                         input += "                      <Price type=\"current\">";
2390                         input += "                              <Value>3.0</Value>";
2391                         input += "                              <Date>TODAY</Date>";
2392                         input += "                      </Price>";
2393                         input += "              </Stock>";
2394                         input += "              <Stock name=\"Mainsoft\">";
2395                         input += "                      <Company name=\"Mainsoft Corp.\"/>";
2396                         input += "                      <Price type=\"high\">";
2397                         input += "                              <Value>30.0</Value>";
2398                         input += "                              <Date>01/26/2000</Date>";
2399                         input += "                      </Price>";
2400                         input += "                      <Price type=\"low\">";
2401                         input += "                              <Value>1.0</Value>";
2402                         input += "                              <Date>03/26/2002</Date>";
2403                         input += "                      </Price>";
2404                         input += "                      <Price type=\"current\">";
2405                         input += "                              <Value>27.0</Value>";
2406                         input += "                              <Date>TODAY</Date>";
2407                         input += "                      </Price>";
2408                         input += "              </Stock>";
2409                         input += "</Stocks>";
2410
2411                         sr = new StringReader(input);
2412                         m_ds.EnforceConstraints = true;
2413                         m_ds.ReadXml(sr);
2414                         this.privateTestCase("TestCase 1", "Company", "name='Microsoft Corp.'", "Stock", "name='MSFT'", "DS320");
2415                         this.privateTestCase("TestCase 2", "Company", "name='General Electric'", "Stock", "name='MSFT'", "DS321");
2416                         this.privateTestCase("TestCase 3", "Price", "Date='01/20/2000'", "Stock", "name='MSFT'", "DS322");
2417                         this.privateTestCase("TestCase 4", "Price", "Date='03/21/2002'", "Stock", "name='MSFT'", "DS323");
2418                         this.privateTestCase("TestCase 5", "Company", "name='GE company'", "Stock", "name='GE'", "DS324");
2419                         this.privateTestCase("TestCase 6", "Price", "Date='02/12/2001'", "Stock", "name='GE'", "DS325");
2420                         this.privateTestCase("TestCase 7", "Price", "Date='04/20/2003'", "Stock", "name='GE'", "DS326");
2421                         this.privateTestCase("TestCase 8", "Company", "name='Intel Corp.'", "Stock", "name='Intel'", "DS327");
2422                         this.privateTestCase("TestCase 9", "Company", "name='Test1'", "Stock", "name='Intel'", "DS328");
2423                         this.privateTestCase("TestCase 10", "Company", "name='Test2'", "Stock", "name='Intel'", "DS329");
2424                         this.privateTestCase("TestCase 11", "Price", "Date='01/25/2000'", "Stock", "name='Intel'", "DS330");
2425                         this.privateTestCase("TestCase 12", "Price", "Date='03/23/2002'", "Stock", "name='Intel'", "DS331");
2426                         this.privateTestCase("TestCase 13", "Company", "name='Mainsoft Corp.'", "Stock", "name='Mainsoft'", "DS332");
2427                         this.privateTestCase("TestCase 12", "Price", "Date='01/26/2000'", "Stock", "name='Mainsoft'", "DS333");
2428                         this.privateTestCase("TestCase 12", "Price", "Date='03/26/2002'", "Stock", "name='Mainsoft'", "DS334");
2429                 }
2430
2431                 private void privateTestCase(string name, string toTable, string toTestSelect, string toCompareTable, string toCompareSelect, string AssertTag)
2432                 {
2433                         DataRow drToTest = m_ds.Tables[toTable].Select(toTestSelect)[0];
2434                         DataRow drToCompare = m_ds.Tables[toCompareTable].Select(toCompareSelect)[0];
2435                         Assert.AreEqual(m_ds.Tables[toTable].Select(toTestSelect)[0]["Stock_Id"], m_ds.Tables[toCompareTable].Select(toCompareSelect)[0]["Stock_Id"], AssertTag);
2436                 }
2437
2438                 [Test]
2439                 public void ReadXml_Strm5()
2440                 {
2441                         string xmlData;
2442                         string name;
2443                         string expected;
2444                         #region "TestCase 1 - Empty string"
2445                         // Empty string
2446                         DataSet ds = new DataSet();
2447                         StringReader sr = new StringReader (string.Empty);
2448                         XmlTextReader xReader = new XmlTextReader(sr);
2449                         try
2450                         {
2451                                 ds.ReadXml (xReader);
2452                                 Assert.Fail("DS335: ReadXml Failed to throw XmlException");
2453                         }
2454                         catch (XmlException) {}
2455                         catch (AssertionException exc) {throw  exc;}
2456                         catch (Exception exc)
2457                         {
2458                                 Assert.Fail("DS336: ReadXml. Wrong exception type. Got:" + exc);
2459                         }
2460                         #endregion
2461                         #region "TestCase 2 - Single element"
2462                         name = "Single element";
2463                         expected = "DataSet Name=a Tables count=0";
2464                         xmlData = "<a>1</a>";
2465                         PrivateTestCase(name, expected, xmlData);
2466                         #endregion
2467                         #region "TestCase 3 - Nesting one level single element."
2468                         name = "Nesting one level single element.";
2469                         expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=1 1";
2470                         xmlData = "<a><b>1</b></a>";
2471                         PrivateTestCase(name, expected, xmlData);
2472                         #endregion
2473                         #region "TestCase 4 - Nesting one level multiple elements."
2474                         name = "Nesting one level multiple elements.";
2475                         expected = "DataSet Name=NewDataSet Tables count=1 Table Name=a Rows count=1 Items count=3 bb cc dd";
2476                         xmlData = "<a><b>bb</b><c>cc</c><d>dd</d></a>";
2477                         PrivateTestCase(name, expected, xmlData);
2478                         #endregion
2479                         #region "TestCase 5 - Nesting two levels single elements."
2480                         name = "Nesting two levels single elements.";
2481                         expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=1 cc";
2482                         xmlData = "<a><b><c>cc</c></b></a>";
2483                         PrivateTestCase(name, expected, xmlData);
2484                         #endregion
2485                         #region "TestCase 6 - Nesting two levels multiple elements."
2486                         name = "Nesting two levels multiple elements.";
2487                         expected = "DataSet Name=a Tables count=1 Table Name=b Rows count=1 Items count=2 cc dd";
2488                         xmlData = string.Empty;
2489                         xmlData += "<a>";
2490                         xmlData +=              "<b>";
2491                         xmlData +=                      "<c>cc</c>";
2492                         xmlData +=                      "<d>dd</d>";
2493                         xmlData +=              "</b>";
2494                         xmlData +=      "</a>";
2495                         PrivateTestCase(name, expected, xmlData);
2496                         #endregion
2497                         #region "TestCase 7 - Nesting two levels multiple elements."
2498                         name = "Nesting two levels multiple elements.";
2499                         expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 cc dd Table Name=e Rows count=1 Items count=2 cc dd";
2500                         xmlData = string.Empty;
2501                         xmlData += "<a>";
2502                         xmlData +=              "<b>";
2503                         xmlData +=                      "<c>cc</c>";
2504                         xmlData +=                      "<d>dd</d>";
2505                         xmlData +=              "</b>";
2506                         xmlData +=              "<e>";
2507                         xmlData +=                      "<c>cc</c>";
2508                         xmlData +=                      "<d>dd</d>";
2509                         xmlData +=              "</e>";
2510                         xmlData +=      "</a>";
2511                         PrivateTestCase(name, expected, xmlData);
2512                         #endregion
2513                         #region "TestCase 8 - Nesting three levels single element."
2514                         name = "Nesting three levels single element.";
2515                         xmlData = string.Empty;
2516                         xmlData += "<a>";
2517                         xmlData +=              "<b>";
2518                         xmlData +=                      "<c>";
2519                         xmlData +=                              "<d>dd</d>";
2520                         xmlData +=                      "</c>";
2521                         xmlData +=              "</b>";
2522                         xmlData +=      "</a>";
2523                         expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=2 0 dd";
2524                         PrivateTestCase(name, expected, xmlData);
2525                         #endregion
2526                         #region "TestCase 9 - Nesting three levels multiple elements."
2527                         name = "Nesting three levels multiple elements.";
2528                         xmlData = string.Empty;
2529                         xmlData += "<a>";
2530                         xmlData +=              "<b>";
2531                         xmlData +=                      "<c>";
2532                         xmlData +=                              "<d>dd</d>";
2533                         xmlData +=                              "<e>ee</e>";
2534                         xmlData +=                      "</c>";
2535                         xmlData +=              "</b>";
2536                         xmlData +=      "</a>";
2537                         expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=1 0 Table Name=c Rows count=1 Items count=3 0 dd ee";
2538                         PrivateTestCase(name, expected, xmlData);
2539                         #endregion
2540                         #region "TestCase 10 - Nesting three levels multiple elements."
2541                         name = "Nesting three levels multiple elements.";
2542                         xmlData = string.Empty;
2543                         xmlData += "<a>";
2544                         xmlData +=              "<b>";
2545                         xmlData +=                      "<c>";
2546                         xmlData +=                              "<d>dd</d>";
2547                         xmlData +=                              "<e>ee</e>";
2548                         xmlData +=                      "</c>";
2549                         xmlData +=                      "<f>ff</f>";
2550                         xmlData +=              "</b>";
2551                         xmlData +=      "</a>";
2552                         expected = "DataSet Name=a Tables count=2 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee";
2553                         PrivateTestCase(name, expected, xmlData);
2554                         #endregion
2555                         #region "TestCase 11 - Nesting three levels multiple elements."
2556                         name = "Nesting three levels multiple elements.";
2557                         xmlData = string.Empty;
2558                         xmlData += "<a>";
2559                         xmlData +=              "<b>";
2560                         xmlData +=                      "<c>";
2561                         xmlData +=                              "<d>dd</d>";
2562                         xmlData +=                              "<e>ee</e>";
2563                         xmlData +=                      "</c>";
2564                         xmlData +=                      "<f>ff</f>";
2565                         xmlData +=                      "<g>";
2566                         xmlData +=                              "<h>hh</h>";
2567                         xmlData +=                              "<i>ii</i>";
2568                         xmlData +=                      "</g>";
2569                         xmlData +=                      "<j>jj</j>";
2570                         xmlData +=              "</b>";
2571                         xmlData +=      "</a>";
2572                         expected = "DataSet Name=a Tables count=3 Table Name=b Rows count=1 Items count=3 0 ff jj Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=3 0 hh ii";
2573                         PrivateTestCase(name, expected, xmlData);
2574                         #endregion
2575                         #region "TestCase 12 - Nesting three levels multiple elements."
2576                         name = "Nesting three levels multiple elements.";
2577                         xmlData = string.Empty;
2578                         xmlData += "<a>";
2579                         xmlData +=              "<b>";
2580                         xmlData +=                      "<c>";
2581                         xmlData +=                              "<d>dd</d>";
2582                         xmlData +=                              "<e>ee</e>";
2583                         xmlData +=                      "</c>";
2584                         xmlData +=                      "<f>ff</f>";
2585                         xmlData +=              "</b>";
2586                         xmlData +=              "<g>";
2587                         xmlData +=                      "<h>";
2588                         xmlData +=                              "<i>ii</i>";
2589                         xmlData +=                              "<j>jj</j>";
2590                         xmlData +=                      "</h>";
2591                         xmlData +=                      "<f>ff</f>";
2592                         xmlData +=              "</g>";
2593                         xmlData +=      "</a>";
2594                         expected = "DataSet Name=a Tables count=4 Table Name=b Rows count=1 Items count=2 0 ff Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=g Rows count=1 Items count=2 ff 0 Table Name=h Rows count=1 Items count=3 0 ii jj";
2595                         PrivateTestCase(name, expected, xmlData);
2596                         #endregion
2597                         #region "TestCase 13 - Nesting three levels multiple elements."
2598                         name = "Nesting three levels multiple elements.";
2599                         xmlData = string.Empty;
2600                         xmlData += "<a>";
2601                         xmlData +=              "<b>";
2602                         xmlData +=                      "<c>";
2603                         xmlData +=                              "<d>dd</d>";
2604                         xmlData +=                              "<e>ee</e>";
2605                         xmlData +=                      "</c>";
2606                         xmlData +=                      "<f>ff</f>";
2607                         xmlData +=                      "<k>";
2608                         xmlData +=                              "<l>ll</l>";
2609                         xmlData +=                              "<m>mm</m>";
2610                         xmlData +=                      "</k>";
2611                         xmlData +=                      "<n>nn</n>";
2612                         xmlData +=              "</b>";
2613                         xmlData +=              "<g>";
2614                         xmlData +=                      "<h>";
2615                         xmlData +=                              "<i>ii</i>";
2616                         xmlData +=                              "<j>jj</j>";
2617                         xmlData +=                      "</h>";
2618                         xmlData +=                      "<o>oo</o>";
2619                         xmlData +=              "</g>";
2620                         xmlData +=      "</a>";
2621                         expected = "DataSet Name=a Tables count=5 Table Name=b Rows count=1 Items count=3 0 ff nn Table Name=c Rows count=1 Items count=3 0 dd ee Table Name=k Rows count=1 Items count=3 0 ll mm Table Name=g Rows count=1 Items count=2 0 oo Table Name=h Rows count=1 Items count=3 0 ii jj";
2622                         PrivateTestCase(name, expected, xmlData);
2623                         #endregion
2624
2625                         #region "TestCase 14 - for Bug 2387 (System.Data.DataSet.ReadXml(..) - ArgumentException while reading specific XML)"
2626
2627                         name = "Specific XML - for Bug 2387";
2628                         expected = "DataSet Name=PKRoot Tables count=2 Table Name=Content Rows count=4 Items count=2 0  Items count=2 1 103 Items count=2 2 123 Items count=2 3 252 Table Name=Cont Rows count=3 Items count=3 1 103 0 Items count=3 2 123 0 Items count=3 3 252 -4";
2629                         xmlData = "<PKRoot><Content /><Content><ContentId>103</ContentId><Cont><ContentId>103</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>123</ContentId><Cont><ContentId>123</ContentId><ContentStatusId>0</ContentStatusId></Cont></Content><Content><ContentId>252</ContentId><Cont><ContentId>252</ContentId><ContentStatusId>-4</ContentStatusId></Cont></Content></PKRoot>";
2630                         PrivateTestCase(name, expected, xmlData);
2631
2632                         #endregion
2633                 }
2634
2635                 private void PrivateTestCase(string a_name, string a_expected, string a_xmlData)
2636                 {
2637                         DataSet ds = new DataSet();
2638                         StringReader sr = new StringReader(a_xmlData) ;
2639                         XmlTextReader xReader = new XmlTextReader(sr) ;
2640                         ds.ReadXml (xReader);
2641                         Assert.AreEqual(a_expected, this.dataSetDescription(ds), "DS337");
2642                 }
2643
2644                 private string dataSetDescription(DataSet ds)
2645                 {
2646                         string desc = string.Empty;
2647                         desc += "DataSet Name=" + ds.DataSetName;
2648                         desc += " Tables count=" + ds.Tables.Count;
2649                         foreach (DataTable dt in ds.Tables)
2650                         {
2651                                 desc += " Table Name=" + dt.TableName;
2652                                 desc += " Rows count=" + dt.Rows.Count;
2653
2654                                 string[] colNames = new string[dt.Columns.Count];
2655                                 for(int i = 0; i < dt.Columns.Count; i++)
2656                                         colNames[i] = dt.Columns[i].ColumnName;
2657
2658                                 Array.Sort(colNames);
2659
2660                                 foreach (DataRow dr in dt.Rows)
2661                                 {
2662                                         desc += " Items count=" + dr.ItemArray.Length;
2663                                         foreach (string name in colNames)
2664                                         {
2665                                                 desc += " " + dr[name].ToString();
2666                                         }
2667                                 }
2668                         }
2669                         return desc;
2670                 }
2671
2672                 [Test] public void ReadXml_Strm6()
2673                 {
2674                         // TC1
2675                         DataSet ds = new DataSet();
2676                         string xmlData = string.Empty;
2677                         xmlData += "<a>";
2678                         xmlData +=    "<b>";
2679                         xmlData +=              "<c>1</c>";
2680                         xmlData +=              "<c>2</c>";
2681                         xmlData +=              "<c>3</c>";
2682                         xmlData +=    "</b>";
2683                         xmlData +=      "</a>";
2684                         StringReader sr = new StringReader(xmlData) ;
2685                         XmlTextReader xReader = new XmlTextReader(sr) ;
2686                         ds.ReadXml (xReader);
2687                         Assert.AreEqual(3, ds.Tables["c"].Rows.Count, "DS338");
2688                 }
2689
2690                 [Test]
2691                 public void ReadXmlSchema_2()
2692                 {
2693                         DataSet ds = new DataSet();
2694                         string xmlData = string.Empty;
2695                         xmlData += "<?xml version=\"1.0\"?>";
2696                         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\">";
2697                         xmlData +=      "<xs:element name=\"SiteConfiguration\" msdata:IsDataSet=\"true\" msdata:EnforceConstraints=\"False\">";
2698                         xmlData +=              "<xs:complexType>";
2699                         xmlData +=                      "<xs:choice maxOccurs=\"unbounded\">";
2700                         xmlData +=                              "<xs:element name=\"Tab\">";
2701                         xmlData +=                                      "<xs:complexType>";
2702                         xmlData +=                                              "<xs:sequence>";
2703                         xmlData +=                                                      "<xs:element name=\"Module\" minOccurs=\"0\" maxOccurs=\"unbounded\">";
2704                         xmlData +=                                                              "<xs:complexType>";
2705                         xmlData +=                                                                      "<xs:attribute name=\"ModuleId\" form=\"unqualified\" type=\"xs:int\" />";
2706                         xmlData +=                                                              "</xs:complexType>";
2707                         xmlData +=                                                      "</xs:element>";
2708                         xmlData +=                                              "</xs:sequence>";
2709                         xmlData +=                                              "<xs:attribute name=\"TabId\" form=\"unqualified\" type=\"xs:int\" />";
2710                         xmlData +=                                      "</xs:complexType>";
2711                         xmlData +=                              "</xs:element>";
2712                         xmlData +=                      "</xs:choice>";
2713                         xmlData +=       "</xs:complexType>";
2714                         xmlData +=              "<xs:key name=\"TabKey\" msdata:PrimaryKey=\"true\">";
2715                         xmlData +=                      "<xs:selector xpath=\".//mstns:Tab\" />";
2716                         xmlData +=                      "<xs:field xpath=\"@TabId\" />";
2717                         xmlData +=              "</xs:key>";
2718                         xmlData +=              "<xs:key name=\"ModuleKey\" msdata:PrimaryKey=\"true\">";
2719                         xmlData +=                      "<xs:selector xpath=\".//mstns:Module\" />";
2720                         xmlData +=                      "<xs:field xpath=\"@ModuleID\" />";
2721                         xmlData +=              "</xs:key>";
2722                         xmlData +=      "</xs:element>";
2723                         xmlData += "</xs:schema>";
2724
2725                         ds.ReadXmlSchema(new StringReader(xmlData));
2726                 }
2727
2728                 [Test]
2729                 [Category ("NotWorking")]
2730                 public void ReadXml_ByTextReader()
2731                 {
2732                         DataSet ds1 = new DataSet();
2733                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2734                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2735
2736                         //add data to check GH bug of DataSet.ReadXml of empty strings
2737                         ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2738                         ds1.Tables[1].Rows.Add(new object[] {7,2," ","          ",new DateTime(2000,1,1,0,0,0,0),35});
2739                         ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2740
2741                         StringWriter sw = new StringWriter();
2742                         //write xml file, data only
2743                         ds1.WriteXml(sw);
2744
2745                         //copy both data and schema
2746                         DataSet ds2 = ds1.Copy();
2747                         //clear the data
2748                         ds2.Clear();
2749
2750                         StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
2751                         ds2.ReadXml(sr);
2752
2753                         //check xml data
2754                         // ReadXml - Tables count
2755                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS339");
2756
2757                         // ReadXml - Table 1 row count
2758                         Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS340");
2759
2760                         // ReadXml - Table 2 row count
2761                         Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS341");
2762
2763                         sr.Close();
2764                         sw.Close();
2765                 }
2766
2767                 [Test]
2768                 [Category ("NotWorking")]
2769                 public void ReadXml_ByXmlReader()
2770                 {
2771                         DataSet ds1 = new DataSet();
2772                         ds1.Tables.Add(DataProvider.CreateParentDataTable());
2773                         ds1.Tables.Add(DataProvider.CreateChildDataTable());
2774
2775                         //add data to check GH bug of DataSet.ReadXml of empty strings
2776                         ds1.Tables[1].Rows.Add(new object[] {7,1,string.Empty,string.Empty,new DateTime(2000,1,1,0,0,0,0),35});
2777                         ds1.Tables[1].Rows.Add(new object[] {7,2," ","          ",new DateTime(2000,1,1,0,0,0,0),35});
2778                         ds1.Tables[1].Rows.Add(new object[] {7,3,"","",new DateTime(2000,1,1,0,0,0,0),35});
2779
2780                         StringWriter sw = new StringWriter();
2781                         XmlTextWriter xmlTW = new XmlTextWriter(sw);
2782
2783                         //write xml file, data only
2784                         ds1.WriteXml(xmlTW);
2785                         //ds1.WriteXml("C:\\Temp\\q.xml");
2786
2787                         //copy both data and schema
2788                         DataSet ds2 = ds1.Copy();
2789                         //clear the data
2790                         ds2.Clear();
2791                         StringReader sr = new StringReader(sw.ToString());
2792                         XmlTextReader xmlTR = new XmlTextReader(sr);
2793                         ds2.ReadXml(xmlTR);
2794
2795                         //check xml data
2796                         // ReadXml - Tables count
2797                         Assert.AreEqual(ds2.Tables.Count , ds1.Tables.Count , "DS342");
2798
2799                         // ReadXml - Table 1 row count
2800                         Assert.AreEqual(ds2.Tables[0].Rows.Count, ds1.Tables[0].Rows.Count , "DS343");
2801
2802                         // ReadXml - Table 2 row count
2803                         Assert.AreEqual(ds2.Tables[1].Rows.Count, ds1.Tables[1].Rows.Count , "DS344");
2804                 }
2805                 
2806                 [Test] public void WriteXmlSchema_ForignKeyConstraint ()
2807                 {
2808                         DataSet ds1 = new DataSet();
2809
2810                         DataTable table1 = ds1.Tables.Add();
2811                         DataTable table2 = ds1.Tables.Add();
2812
2813                         DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
2814                         DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
2815
2816                         table2.Constraints.Add ("fk", col1_1, col2_1);
2817
2818                         StringWriter sw = new StringWriter ();
2819                         ds1.WriteXmlSchema (sw);
2820                         String xml = sw.ToString ();
2821
2822                         Assert.IsTrue (xml.IndexOf (@"<xs:keyref name=""fk"" refer=""Constraint1"" " +
2823                                                 @"msdata:ConstraintOnly=""true"">") != -1, "#1");
2824                 }
2825
2826                 [Test] public void WriteXmlSchema_RelationAnnotation ()
2827                 {
2828                         DataSet ds1 = new DataSet();
2829
2830                         DataTable table1 = ds1.Tables.Add();
2831                         DataTable table2 = ds1.Tables.Add();
2832
2833                         DataColumn col1_1 = table1.Columns.Add ("col1", typeof (int));
2834                         DataColumn col2_1 = table2.Columns.Add ("col1", typeof (int));
2835
2836                         ds1.Relations.Add ("rel", col1_1, col2_1, false);
2837
2838                         StringWriter sw = new StringWriter ();
2839                         ds1.WriteXmlSchema (sw);
2840                         String xml = sw.ToString ();
2841
2842       
2843                         Assert.IsTrue (xml.IndexOf (@"<msdata:Relationship name=""rel"" msdata:parent=""Table1""" +
2844                                                 @" msdata:child=""Table2"" msdata:parentkey=""col1"" " + 
2845                                                 @"msdata:childkey=""col1"" />") != -1, "#1");
2846                 }
2847
2848                 [Test] public void WriteXmlSchema_Relations_ForeignKeys ()
2849                 {
2850                         MemoryStream ms = null;
2851                         MemoryStream ms1 = null;
2852
2853                         DataSet ds1 = new DataSet();
2854
2855                         DataTable table1 = ds1.Tables.Add("Table 1");
2856                         DataTable table2 = ds1.Tables.Add("Table 2");
2857
2858                         DataColumn col1_1 = table1.Columns.Add ("col 1", typeof (int));
2859                         DataColumn col1_2 = table1.Columns.Add ("col 2", typeof (int));
2860                         DataColumn col1_3 = table1.Columns.Add ("col 3", typeof (int));
2861                         DataColumn col1_4 = table1.Columns.Add ("col 4", typeof (int));
2862                         DataColumn col1_5 = table1.Columns.Add ("col 5", typeof (int));
2863                         DataColumn col1_6 = table1.Columns.Add ("col 6", typeof (int));
2864                         DataColumn col1_7 = table1.Columns.Add ("col 7", typeof (int));
2865
2866                         DataColumn col2_1 = table2.Columns.Add ("col 1", typeof (int));
2867                         DataColumn col2_2 = table2.Columns.Add ("col 2", typeof (int));
2868                         DataColumn col2_3 = table2.Columns.Add ("col 3", typeof (int));
2869                         DataColumn col2_4 = table2.Columns.Add ("col 4", typeof (int));
2870                         DataColumn col2_5 = table2.Columns.Add ("col 5", typeof (int));
2871                         DataColumn col2_6 = table2.Columns.Add ("col 6", typeof (int));
2872
2873                         ds1.Relations.Add ("rel 1", 
2874                                 new DataColumn[] {col1_1, col1_2},
2875                                 new DataColumn[] {col2_1, col2_2});
2876                         ds1.Relations.Add ("rel 2", 
2877                                 new DataColumn[] {col1_3, col1_4}, 
2878                                 new DataColumn[] {col2_3, col2_4},
2879                                 false);
2880
2881                         table1.Constraints.Add ("pk 1", col1_7, true);
2882
2883                         table2.Constraints.Add ("fk 1",
2884                                 new DataColumn[] {col1_5, col1_6},
2885                                 new DataColumn[] {col2_5, col2_6});
2886
2887                         ms = new MemoryStream();
2888                         ds1.WriteXmlSchema (ms);
2889
2890                         ms1 = new MemoryStream (ms.GetBuffer());
2891                         DataSet ds2 = new DataSet();
2892                         ds2.ReadXmlSchema(ms1);
2893                 
2894                         Assert.AreEqual (2, ds2.Relations.Count, "#1");
2895                         Assert.AreEqual (3, ds2.Tables [0].Constraints.Count, "#2");
2896                         Assert.AreEqual (2, ds2.Tables [1].Constraints.Count, "#2");
2897
2898                         Assert.IsTrue (ds2.Relations.Contains ("rel 1"), "#3");
2899                         Assert.IsTrue (ds2.Relations.Contains ("rel 2"), "#4");
2900
2901                         Assert.IsTrue (ds2.Tables [0].Constraints.Contains ("pk 1"), "#5");
2902                         Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("fk 1"), "#6");
2903                         Assert.IsTrue (ds2.Tables [1].Constraints.Contains ("rel 1"), "#7");
2904
2905                         Assert.AreEqual (2, ds2.Relations ["rel 1"].ParentColumns.Length, "#8");
2906                         Assert.AreEqual (2, ds2.Relations ["rel 1"].ChildColumns.Length, "#9");
2907
2908                         Assert.AreEqual (2, ds2.Relations ["rel 2"].ParentColumns.Length, "#10");
2909                         Assert.AreEqual (2, ds2.Relations ["rel 2"].ChildColumns.Length, "#11");
2910
2911                         ForeignKeyConstraint fk = (ForeignKeyConstraint)ds2.Tables [1].Constraints ["fk 1"];
2912                         Assert.AreEqual (2, fk.RelatedColumns.Length, "#12");
2913                         Assert.AreEqual (2, fk.Columns.Length, "#13");
2914                 }
2915                 
2916                 [Test] public void RejectChanges()
2917                 {
2918                         DataSet ds1,ds2 = new DataSet();
2919                         ds2.Tables.Add(DataProvider.CreateParentDataTable());
2920                         ds1 = ds2.Copy();
2921
2922                         //create changes
2923                         ds2.Tables[0].Rows[0][0] = "70";
2924                         ds2.Tables[0].Rows[1].Delete();
2925                         ds2.Tables[0].Rows.Add(new object[] {9,"string1","string2"});
2926
2927                         // RejectChanges
2928                         ds2.RejectChanges();
2929                         Assert.AreEqual(ds2.GetXml(), ds1.GetXml(), "DS345");
2930                 }
2931
2932                 [Test] public void Relations()
2933                 {
2934                         DataTable dtChild1,dtChild2,dtParent;
2935                         DataSet ds = new DataSet();
2936                         //Create tables
2937                         dtChild1 = DataProvider.CreateChildDataTable();
2938                         dtChild1.TableName = "Child";
2939                         dtChild2 = DataProvider.CreateChildDataTable();
2940                         dtChild2.TableName = "CHILD";
2941                         dtParent= DataProvider.CreateParentDataTable();
2942                         //Add tables to dataset
2943                         ds.Tables.Add(dtChild1);
2944                         ds.Tables.Add(dtChild2);
2945
2946                         ds.Tables.Add(dtParent);
2947
2948                         DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild1.Columns["ParentId"]);
2949                         DataRelation drl1 = new DataRelation("Parent-CHILD",dtParent.Columns["ParentId"],dtChild2.Columns["ParentId"]);
2950
2951                         // Checking Relations - default value
2952                         //Check default
2953                         Assert.AreEqual(0, ds.Relations.Count  , "DS346");
2954
2955                         ds.Relations.Add(drl);
2956
2957                         // Checking Relations Count
2958                         Assert.AreEqual(1, ds.Relations.Count  , "DS347");
2959
2960                         // Checking Relations Value
2961                         Assert.AreEqual(drl, ds.Relations[0] , "DS348");
2962
2963                         // Checking Relations - get by name
2964                         Assert.AreEqual(drl, ds.Relations["Parent-Child"] , "DS349");
2965
2966                         // Checking Relations - get by name case sensetive
2967                         Assert.AreEqual(drl, ds.Relations["PARENT-CHILD"] , "DS350");
2968
2969                         // Checking Relations Count 2
2970                         ds.Relations.Add(drl1);
2971                         Assert.AreEqual(2, ds.Relations.Count  , "DS351");
2972
2973                         // Checking Relations - get by name case sensetive,ArgumentException
2974                         try
2975                         {
2976                                 DataRelation tmp = ds.Relations["PARENT-CHILD"];
2977                                 Assert.Fail("DS352: Relations Failed to throw ArgumentException");
2978                         }
2979                         catch (ArgumentException) {}
2980                         catch (AssertionException exc) {throw  exc;}
2981                         catch (Exception exc)
2982                         {
2983                                 Assert.Fail("DS353: Relations. Wrong exception type. Got:" + exc);
2984                         }
2985                 }
2986
2987                 [Test] public void Reset()
2988                 {
2989                         DataTable dt1 = DataProvider.CreateParentDataTable();
2990                         DataTable dt2 = DataProvider.CreateChildDataTable();
2991                         dt1.PrimaryKey  = new DataColumn[] {dt1.Columns[0]};
2992                         dt2.PrimaryKey  = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
2993                         DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
2994                         DataSet ds = new DataSet();
2995                         ds.Tables.AddRange(new DataTable[] {dt1,dt2});
2996                         ds.Relations.Add(rel);
2997
2998                         ds.Reset();
2999
3000                         // Reset - Relations
3001                         Assert.AreEqual(0 , ds.Relations.Count  , "DS354");
3002                         // Reset - Tables
3003                         Assert.AreEqual(0 , ds.Tables.Count  , "DS355");
3004                 }
3005
3006                 [Test] public void ShouldSerializeRelations()
3007                 {
3008                         // DataSet ShouldSerializeRelations
3009                         newDataSet ds = new newDataSet();
3010
3011                         Assert.AreEqual(true, ds.testMethod(), "DS356");
3012                 }
3013
3014                 class newDataSet:DataSet
3015                 {
3016                         public bool testMethod()
3017                         {
3018                                 return ShouldSerializeRelations();
3019                         }
3020                 }
3021                 [Test] public void ShouldSerializeTables()
3022                 {
3023                         // DataSet ShouldSerializeTables
3024                         newDataSet1 ds = new newDataSet1();
3025
3026                         Assert.AreEqual(true, ds.testMethod(), "DS357");
3027                 }
3028
3029                 class newDataSet1:DataSet
3030                 {
3031                         public bool testMethod()
3032                         {
3033                                 return ShouldSerializeTables();
3034                         }
3035                 }
3036                 [Test] public void Tables()
3037                 {
3038                         //References by name to tables and relations in a DataSet are case-sensitive. Two or more tables or relations can exist in a DataSet that have the same name, but that differ in case. For example you can have Table1 and table1. In this situation, a reference to one of the tables by name must match the case of the table name exactly, otherwise an exception is thrown. For example, if the DataSet myDS contains tables Table1 and table1, you would reference Table1 by name as myDS.Tables["Table1"], and table1 as myDS.Tables ["table1"]. Attempting to reference either of the tables as myDS.Tables ["TABLE1"] would generate an exception.
3039                         //The case-sensitivity rule does not apply if only one table or relation exists with a particular name. That is, if no other table or relation object in the DataSet matches the name of that particular table or relation object, even by a difference in case, you can reference the object by name using any case and no exception is thrown. For example, if the DataSet has only Table1, you can reference it using myDS.Tables["TABLE1"].
3040                         //The CaseSensitive property of the DataSet does not affect this behavior. The CaseSensitive property
3041
3042                         DataSet ds = new DataSet();
3043
3044                         DataTable dt1 = new DataTable();
3045                         DataTable dt2 = new DataTable();
3046                         DataTable dt3 = new DataTable();
3047                         dt3.TableName = "Table3";
3048                         DataTable dt4 = new DataTable(dt3.TableName.ToUpper());
3049
3050                         // Checking Tables - default value
3051                         //Check default
3052                         Assert.AreEqual(0, ds.Tables.Count  , "DS358");
3053
3054                         ds.Tables.Add(dt1);
3055                         ds.Tables.Add(dt2);
3056                         ds.Tables.Add(dt3);
3057
3058                         // Checking Tables Count
3059                         Assert.AreEqual(3, ds.Tables.Count  , "DS359");
3060
3061                         // Checking Tables Value 1
3062                         Assert.AreEqual(dt1, ds.Tables[0] , "DS360");
3063
3064                         // Checking Tables Value 2
3065                         Assert.AreEqual(dt2, ds.Tables[1] , "DS361");
3066
3067                         // Checking Tables Value 3
3068                         Assert.AreEqual(dt3, ds.Tables[2] , "DS362");
3069
3070                         // Checking get table by name.ToUpper
3071                         Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToUpper()] , "DS363");
3072
3073                         // Checking get table by name.ToLower
3074                         Assert.AreEqual(dt3, ds.Tables[dt3.TableName.ToLower()] , "DS364");
3075
3076                         // Checking Tables add with name case insensetive
3077                         ds.Tables.Add(dt4); //same name as Table3, but different case
3078                         Assert.AreEqual(4, ds.Tables.Count  , "DS365");
3079
3080                         // Checking get table by name
3081                         Assert.AreEqual(dt4, ds.Tables[dt4.TableName] , "DS366");
3082
3083                         // Checking get table by name with diferent case, ArgumentException
3084                         try
3085                         {
3086                                 DataTable tmp = ds.Tables[dt4.TableName.ToLower()];
3087                                 Assert.Fail("DS367: Tables Failed to throw ArgumentException");
3088                         }
3089                         catch (ArgumentException) {}
3090                         catch (AssertionException exc) {throw  exc;}
3091                         catch (Exception exc)
3092                         {
3093                                 Assert.Fail("DS368: Tables. Wrong exception type. Got:" + exc);
3094                         }
3095                 }
3096
3097                 [Test] public void WriteXml_ByTextWriterXmlWriteMode()
3098                 {
3099                         StringReader sr = null;
3100                         StringWriter sw = null;
3101
3102                         try  // For real
3103                         {
3104                                 // ReadXml - DataSetOut
3105
3106                                 DataSet oDataset = new DataSet("DataSetOut");
3107                                 sw = new StringWriter();
3108                                 oDataset.WriteXml(sw, XmlWriteMode.WriteSchema);
3109
3110                                 sr = new StringReader(sw.GetStringBuilder().ToString());
3111                                 oDataset = new DataSet("DataSetOut");
3112
3113                                 oDataset.ReadXml(sr);
3114                                 Assert.AreEqual(0, oDataset.Tables.Count , "DS369");
3115                         }
3116                         finally 
3117                         {
3118                                 sw.Close();
3119                         }
3120                 }
3121
3122                 [Test] public void ctor()
3123                 {
3124                         DataSet ds;
3125
3126                         // ctor
3127                         ds = new DataSet();
3128                         Assert.AreEqual(true, ds != null , "DS370");
3129                 }
3130
3131                 [Test] public void ctor_ByDataSetName()
3132                 {
3133                         DataSet ds = null;
3134
3135                         // ctor
3136                         ds = new DataSet("NewDataSet");
3137                         Assert.AreEqual(true, ds != null , "DS371");
3138
3139                         // ctor - name
3140                         Assert.AreEqual("NewDataSet" , ds.DataSetName  , "DS372");
3141                 }
3142
3143                 [Test] public void extendedProperties()
3144                 {
3145                         DataSet ds = new DataSet();
3146                         PropertyCollection pc;
3147
3148                         pc = ds.ExtendedProperties ;
3149
3150                         // Checking ExtendedProperties default
3151                         Assert.AreEqual(true, pc != null, "DS373");
3152
3153                         // Checking ExtendedProperties count
3154                         Assert.AreEqual(0, pc.Count , "DS374");
3155                 }
3156
3157 #if NET_2_0
3158                 // Test for bug #76517
3159                 [Test] public void SchemaSerializationModeTest ()
3160                 {
3161                         DataSet ds = new DataSet ();
3162                         Assert.AreEqual (SchemaSerializationMode.IncludeSchema,
3163                                         ds.SchemaSerializationMode, "#1");
3164                         try {
3165                                 ds.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;
3166                                 Assert.Fail ("#2 InvalidOperationException must be thrown");
3167                         }catch (InvalidOperationException e) {
3168                                 //ok    
3169                         }       
3170                 }       
3171 #endif
3172
3173                 ///<?xml version="1.0" encoding="utf-16"?>
3174                 ///<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3175                 ///     <xs:element name="NewDataSet" msdata:IsDataSet="true">
3176                 ///             <xs:complexType>
3177                 ///                     <xs:choice maxOccurs="unbounded">
3178                 ///                             <xs:element name="Parent">
3179                 ///                                     <xs:complexType>
3180                 ///                                             <xs:sequence>
3181                 ///                                                     <xs:element name="ParentId" type="xs:int" minOccurs="0"/>
3182                 ///                                                     <xs:element name="String1" type="xs:string" minOccurs="0"/>
3183                 ///                                                     <xs:element name="String2" type="xs:string" minOccurs="0"/>
3184                 ///                                                     <xs:element name="ParentDateTime" type="xs:dateTime" minOccurs="0"/>
3185                 ///                                                     <xs:element name="ParentDouble" type="xs:double" minOccurs="0"/>
3186                 ///                                                     <xs:element name="ParentBool" type="xs:boolean" minOccurs="0"/>
3187                 ///                                             </xs:sequence>
3188                 ///                                     </xs:complexType>
3189                 ///                             </xs:element>
3190                 ///                     </xs:choice>
3191                 ///             </xs:complexType>
3192                 ///     </xs:element>
3193                 ///</xs:schema>
3194                 
3195                 [Test]
3196                 public void ParentDataTableSchema()
3197                 {
3198                         XmlDocument testedSchema;
3199                         XmlNamespaceManager testedSchemaNamepaces;
3200                         InitParentDataTableSchema(out testedSchema, out testedSchemaNamepaces);
3201
3202                         CheckNode("DataSet name", "/xs:schema/xs:element[@name='NewDataSet']", 1, testedSchema, testedSchemaNamepaces);
3203
3204                         CheckNode("Parent datatable name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element[@name='Parent']", 1, testedSchema, testedSchemaNamepaces);
3205
3206                         CheckNode("ParentId column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentId']", 1, testedSchema, testedSchemaNamepaces);
3207
3208                         CheckNode("String1 column - name",       "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
3209
3210                         CheckNode("String2 column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='String1']", 1, testedSchema, testedSchemaNamepaces);
3211
3212                         CheckNode("ParentDateTime column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDateTime']", 1, testedSchema, testedSchemaNamepaces);
3213
3214                         CheckNode("ParentDouble column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentDouble']", 1, testedSchema, testedSchemaNamepaces);
3215
3216                         CheckNode("ParentBool column - name", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@name='ParentBool']", 1, testedSchema, testedSchemaNamepaces);
3217
3218                         CheckNode("Int columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:int']", 1, testedSchema, testedSchemaNamepaces);
3219
3220                         CheckNode("string columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:string']",       2, testedSchema, testedSchemaNamepaces);
3221
3222                         CheckNode("dateTime columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:dateTime']",   1, testedSchema, testedSchemaNamepaces);
3223
3224                         CheckNode("double columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:double']",       1, testedSchema, testedSchemaNamepaces);
3225
3226                         CheckNode("boolean columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@type='xs:boolean']",     1, testedSchema, testedSchemaNamepaces);
3227
3228                         CheckNode("minOccurs columns", "/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element[@minOccurs='0']", 6, testedSchema, testedSchemaNamepaces);
3229                 }
3230
3231                 private void InitParentDataTableSchema(out XmlDocument schemaDocInit, out XmlNamespaceManager namespaceManagerToInit)
3232                 {
3233                         DataSet ds = new DataSet();
3234                         ds.Tables.Add(DataProvider.CreateParentDataTable());
3235                         string strXML = ds.GetXmlSchema();
3236                         schemaDocInit = new XmlDocument();
3237                         schemaDocInit.LoadXml(strXML);
3238                         namespaceManagerToInit = new XmlNamespaceManager(schemaDocInit.NameTable);
3239                         namespaceManagerToInit.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
3240                         namespaceManagerToInit.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
3241                 }
3242
3243                 private void CheckNode(string description, string xPath, int expectedNodesCout, XmlDocument schemaDoc, XmlNamespaceManager nm)
3244                 {
3245                         int actualNodeCount = schemaDoc.SelectNodes(xPath, nm).Count;
3246                         Assert.AreEqual(expectedNodesCout,actualNodeCount, "DS75" + description);
3247                 }
3248
3249                 [Test]
3250                 public void WriteXml_Stream()
3251                 {
3252                         {
3253                         DataSet ds = new DataSet();
3254                         string input = "<a><b><c>2</c></b></a>";
3255                         StringReader sr = new StringReader(input) ;
3256                         XmlTextReader xReader = new XmlTextReader(sr) ;
3257                         ds.ReadXml (xReader);
3258
3259                         StringBuilder sb = new StringBuilder();
3260                         StringWriter sw = new StringWriter(sb);
3261                         XmlTextWriter xWriter = new XmlTextWriter(sw);
3262                         ds.WriteXml(xWriter);
3263                         string output = sb.ToString();
3264                         Assert.AreEqual(input,output, "DS76");
3265                         }
3266                         {
3267                         DataSet ds = new DataSet();
3268                         string input = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a><b><c>2</c></b></a>";
3269                         string expectedOutput = "<a><b><c>2</c></b></a>";
3270                         StringReader sr = new StringReader(input) ;
3271                         XmlTextReader xReader = new XmlTextReader(sr) ;
3272                         ds.ReadXml (xReader);
3273                         
3274                         StringBuilder sb = new StringBuilder();
3275                         StringWriter sw = new StringWriter(sb);
3276                         XmlTextWriter xWriter = new XmlTextWriter(sw);
3277                         ds.WriteXml(xWriter);
3278                         string output = sb.ToString();
3279                         Assert.AreEqual(expectedOutput,output, "DS77");
3280                         }
3281                         {
3282                         DataSet ds = new DataSet("DSName"); 
3283                         StringWriter sr = new StringWriter();
3284                         ds.WriteXml(sr); 
3285                         Assert.AreEqual("<DSName />",sr.ToString(), "DS78");
3286                         }
3287                         {
3288                         DataSet ds = new DataSet();
3289                         DataTable dt;
3290
3291                         //Create parent table.
3292                         dt = ds.Tables.Add("ParentTable");
3293                         dt.Columns.Add("ParentTable_Id", typeof(int));
3294                         dt.Columns.Add("ParentTableCol", typeof(int));
3295                         dt.Rows.Add(new object[] {0,1});
3296
3297                         //Create child table.
3298                         dt = ds.Tables.Add("ChildTable");
3299                         dt.Columns.Add("ParentTable_Id", typeof(int));
3300                         dt.Columns.Add("ChildTableCol", typeof(string));
3301                         dt.Rows.Add(new object[] {0,"aa"});
3302
3303                         //Add a relation between parent and child table.
3304                         ds.Relations.Add("ParentTable_ChildTable", ds.Tables["ParentTable"].Columns["ParentTable_Id"], ds.Tables["ChildTable"].Columns["ParentTable_Id"], true);
3305                         ds.Relations["ParentTable_ChildTable"].Nested=true;
3306
3307                         //Reomve the Parent_Child relation.
3308                         dt = ds.Tables["ChildTable"];
3309                         dt.ParentRelations.Remove("ParentTable_ChildTable");
3310
3311                         //Remove the constraint created automatically to enforce the "ParentTable_ChildTable" relation.
3312                         dt.Constraints.Remove("ParentTable_ChildTable");
3313
3314                         //Remove the child table from the dataset.
3315                         ds.Tables.Remove("ChildTable");
3316
3317                         //Get the xml representation of the dataset.
3318                         StringWriter sr = new StringWriter();
3319                         ds.WriteXml(sr); 
3320                         string xml = sr.ToString();
3321
3322                         Assert.AreEqual(-1,xml.IndexOf("<ChildTable>"), "DS79");
3323                         }
3324                 }
3325                 
3326                 [Test]
3327                 public void WriteXmlSchema_ConstraintNameWithSpaces ()
3328                 {
3329                         DataSet ds = new DataSet ();
3330                         DataTable table1 = ds.Tables.Add ("table1");
3331                         DataTable table2 = ds.Tables.Add ("table2");
3332
3333                         table1.Columns.Add ("col1", typeof (int));
3334                         table2.Columns.Add ("col1", typeof (int));
3335
3336                         table1.Constraints.Add ("uc 1", table1.Columns [0], false);
3337                         table2.Constraints.Add ("fc 1", table1.Columns [0], table2.Columns [0]);
3338                         
3339                         StringWriter sw = new StringWriter ();
3340
3341                         //should not throw an exception
3342                         ds.WriteXmlSchema (sw);
3343                 }
3344
3345                 [Test]
3346                 public void ReadWriteXmlSchema_Nested ()
3347                 {
3348                         DataSet ds = new DataSet ("dataset");
3349                         ds.Tables.Add ("table1");
3350                         ds.Tables.Add ("table2");
3351                         ds.Tables[0].Columns.Add ("col");
3352                         ds.Tables[1].Columns.Add ("col");
3353                         ds.Relations.Add ("rel", ds.Tables [0].Columns [0],ds.Tables [1].Columns [0], true);
3354                         ds.Relations [0].Nested = true;
3355
3356                         MemoryStream ms = new MemoryStream ();
3357                         ds.WriteXmlSchema (ms);
3358
3359                         DataSet ds1 = new DataSet ();
3360                         ds1.ReadXmlSchema (new MemoryStream (ms.GetBuffer ()));
3361
3362                         // no new relation, and <table>_Id columns, should get created when 
3363                         // Relation.Nested = true
3364                         Assert.AreEqual (1, ds1.Relations.Count, "#1");
3365                         Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
3366                         Assert.AreEqual (1, ds1.Tables [1].Columns.Count, "#3");
3367                 }
3368
3369                 [Test]
3370                 public void ReadXmlSchema_Nested ()
3371                 {
3372                         //when Relation.Nested = false, and the schema is nested, create new relations on <table>_Id
3373                         //columns.
3374                         DataSet ds = new DataSet ();
3375                         ds.ReadXmlSchema ("Test/System.Data/schemas/test017.xsd");
3376                         Assert.AreEqual (2, ds.Relations.Count, "#1");
3377                         Assert.AreEqual (3, ds.Tables [0].Columns.Count, "#2");
3378                         Assert.AreEqual (3, ds.Tables [1].Columns.Count, "#3");
3379                         Assert.AreEqual ("table1_Id_0", ds.Tables [0].Columns [2].ColumnName, "#4");
3380                         Assert.AreEqual ("table1_Id_0", ds.Tables [0].PrimaryKey [0].ColumnName, "#5");
3381                 }
3382
3383                 [Test]
3384                 public void ReadXmlSchema_TableOrder ()
3385                 {
3386                         DataSet ds = new DataSet ();
3387                         ds.ReadXmlSchema ("Test/System.Data/schemas/Items.xsd");
3388                         Assert.AreEqual ("category", ds.Tables [0].TableName, "#1");
3389                         Assert.AreEqual ("childItemId", ds.Tables [1].TableName, "#2");
3390                         Assert.AreEqual ("item", ds.Tables [2].TableName, "#3");
3391                 }
3392
3393                 [Test]
3394                 public void ReadXml_Diffgram_MissingSchema ()
3395                 {
3396                         DataSet ds = new DataSet ();
3397                         ds.Tables.Add ("table");
3398                         ds.Tables [0].Columns.Add ("col1");
3399                         ds.Tables [0].Columns.Add ("col2");
3400
3401                         ds.Tables [0].Rows.Add (new object[] {"a", "b"});
3402                         ds.Tables [0].Rows.Add (new object[] {"a", "b"});
3403
3404                         MemoryStream ms = new MemoryStream ();
3405                         ds.WriteXml (ms, XmlWriteMode.DiffGram);
3406
3407                         DataSet ds1 = new DataSet ();
3408                         ds1.Tables.Add ("table");
3409                         ds1.Tables [0].Columns.Add ("col1");
3410
3411                         // When table schema is missing, it shud load up the data
3412                         // for the existing schema
3413                         ds1.ReadXml (new MemoryStream (ms.GetBuffer ()), XmlReadMode.DiffGram);
3414
3415                         Assert.AreEqual (2, ds1.Tables [0].Rows.Count, "#1");
3416                         Assert.AreEqual (1, ds1.Tables [0].Columns.Count, "#2");
3417                         Assert.AreEqual ("a", ds1.Tables [0].Rows [0][0], "#3");
3418                         Assert.AreEqual ("a", ds1.Tables [0].Rows [1][0], "#4");
3419                 }
3420
3421                 [Test]
3422                 public void WriteXml_Morethan2Relations ()
3423                 {
3424                         DataSet ds = new DataSet ();
3425                         DataTable p1 = ds.Tables.Add ("parent1");
3426                         DataTable p2 = ds.Tables.Add ("parent2");
3427                         DataTable p3 = ds.Tables.Add ("parent3");
3428                         DataTable c1 = ds.Tables.Add ("child");
3429
3430                         c1.Columns.Add ("col1");
3431                         c1.Columns.Add ("col2");
3432                         c1.Columns.Add ("col3");
3433                         c1.Columns.Add ("col4");
3434
3435                         p1.Columns.Add ("col1");
3436                         p2.Columns.Add ("col1");
3437                         p3.Columns.Add ("col1");
3438
3439                         ds.Relations.Add ("rel1", p1.Columns [0], c1.Columns [0], false);
3440                         ds.Relations.Add ("rel2", p2.Columns [0], c1.Columns [1], false);
3441                         ds.Relations.Add ("rel3", p3.Columns [0], c1.Columns [2], false);
3442                         ds.Relations [2].Nested = true;
3443
3444                         p1.Rows.Add (new object[] {"p1"});
3445                         p2.Rows.Add (new object[] {"p2"});
3446                         p3.Rows.Add (new object[] {"p3"});
3447
3448                         c1.Rows.Add (new object[] {"p1","p2","p3","c1"});
3449
3450                         StringWriter sw = new StringWriter ();
3451                         XmlTextWriter xw = new XmlTextWriter (sw);
3452                         ds.WriteXml (xw);
3453                         string dataset_xml = sw.ToString ();
3454                         string child_xml = "<child><col1>p1</col1><col2>p2</col2><col3>p3</col3><col4>c1</col4></child>";
3455                         //the child table data must not be repeated.
3456                         Assert.AreEqual (dataset_xml.IndexOf (child_xml), dataset_xml.LastIndexOf (child_xml), "#1");
3457                 }
3458
3459                 [Test]  
3460                 public void MergeTest_ColumnTypeMismatch ()
3461                 {
3462                         DataSet dataSet = new DataSet ();
3463                         dataSet.Tables.Add (new DataTable ());
3464                         dataSet.Tables [0].Columns.Add (new DataColumn ("id", typeof (int)));
3465                         dataSet.Tables [0].Columns.Add (new DataColumn ("name", typeof (string)));
3466
3467                         DataSet ds = new DataSet ();
3468                         ds.Tables.Add (new DataTable ());
3469                         ds.Tables [0].Columns.Add (new DataColumn ("id", typeof (string)));
3470
3471                         try {
3472                                 ds.Merge (dataSet, true, MissingSchemaAction.Add);
3473                                 Assert.Fail ("#1");
3474                         } catch (DataException e) {}
3475
3476                         ds = new DataSet ();
3477                         ds.Tables.Add (new DataTable ());
3478                         ds.Tables [0].Columns.Add (new DataColumn("id", typeof (string)));
3479
3480                         ds.Merge (dataSet, true, MissingSchemaAction.Ignore);
3481
3482                         Assert.AreEqual ("Table1", ds.Tables [0].TableName, "#2");
3483                         Assert.AreEqual (1, ds.Tables.Count, "#3");
3484                         Assert.AreEqual (1, ds.Tables [0].Columns.Count, "#4");         
3485                         Assert.AreEqual (typeof (string), ds.Tables [0].Columns [0].DataType, "#5");
3486                 }
3487
3488 #if NET_2_0               
3489                [Test]
3490                public void MergeTest_SameDataSet_536194 ()
3491                {
3492                        DataSet dataSet = new DataSet ("Test");
3493                        
3494                        DataTable dataTable = new DataTable("Test");
3495                        dataTable.Columns.Add("Test");
3496                        dataTable.Rows.Add("Test");
3497                        dataSet.Tables.Add(dataTable);
3498                        dataSet.Merge(dataTable);
3499                        Assert.AreEqual (1, dataSet.Tables.Count, "1");
3500                }
3501 #endif
3502
3503 #if NET_2_0
3504                 [Test]  
3505                 public void LoadTest1 ()
3506                 {
3507                         DataSet ds1 = new DataSet ();
3508                         DataSet ds2 = new DataSet ();
3509                         DataTable dt1 = new DataTable ("T1");
3510                         DataTable dt2 = new DataTable ("T2");
3511                         DataTable dt3 = new DataTable ("T1");
3512                         DataTable dt4 = new DataTable ("T2");
3513                         dt1.Columns.Add ("ID", typeof (int));
3514                         dt1.Columns.Add ("Name", typeof (string));
3515                         dt2.Columns.Add ("EmpNO", typeof (int));
3516                         dt2.Columns.Add ("EmpName", typeof (string));
3517
3518                         dt1.Rows.Add (new object[] {1, "Andrews"});
3519                         dt1.Rows.Add (new object[] {2, "Mathew"});
3520                         dt1.Rows.Add (new object[] {3, "Jaccob"});
3521
3522                         dt2.Rows.Add (new object[] {1, "Arul"});
3523                         dt2.Rows.Add (new object[] {2, "Jothi"});
3524                         dt2.Rows.Add (new object[] {3, "Murugan"});
3525
3526                         ds2.Tables.Add (dt1);
3527                         ds2.Tables.Add (dt2);
3528                         ds1.Tables.Add (dt3);
3529                         ds1.Tables.Add (dt4);
3530
3531                         DataTableReader reader = ds2.CreateDataReader ();
3532                         //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
3533                         ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
3534
3535                         Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DataSet Tables count mistmatch");
3536                         int i = 0;
3537                         foreach (DataTable dt in ds1.Tables) {
3538                                 DataTable dt5 = ds2.Tables[i];
3539                                 Assert.AreEqual (dt5.Rows.Count, dt.Rows.Count, "Table " + dt.TableName + " row count mistmatch");
3540                                 int j = 0;
3541                                 DataRow row1;
3542                                 foreach (DataRow row in dt.Rows) {
3543                                         row1 = dt5.Rows[j];
3544                                         for (int k = 0; k < dt.Columns.Count; k++) {
3545                                                 Assert.AreEqual (row1[k], row[k], "DataRow " + k + " mismatch");
3546                                         }
3547                                         j++;
3548                                 }
3549                                 i++;
3550                         }
3551                 }
3552                 [Test]  
3553                 public void LoadTest2 ()
3554                 {
3555                         DataSet ds1 = new DataSet ();
3556                         DataSet ds2 = new DataSet ();
3557                         DataTable dt1 = new DataTable ("T1");
3558                         DataTable dt2 = new DataTable ("T2");
3559                         DataTable dt3 = new DataTable ("T1");
3560                         DataTable dt4 = new DataTable ("T2");
3561                         dt1.Columns.Add ("ID", typeof (int));
3562                         dt1.Columns.Add ("Name", typeof (string));
3563                         dt2.Columns.Add ("EmpNO", typeof (int));
3564                         dt2.Columns.Add ("EmpName", typeof (string));
3565
3566                         dt1.Rows.Add (new object[] {1, "Andrews"});
3567                         dt1.Rows.Add (new object[] {2, "Mathew"});
3568                         dt1.Rows.Add (new object[] {3, "Jaccob"});
3569
3570                         dt2.Rows.Add (new object[] {1, "Arul"});
3571                         dt2.Rows.Add (new object[] {2, "Jothi"});
3572                         dt2.Rows.Add (new object[] {3, "Murugan"});
3573
3574                         ds2.Tables.Add (dt1);
3575                         ds2.Tables.Add (dt2);
3576                         ds1.Tables.Add (dt3);
3577                         ds1.Tables.Add (dt4);
3578
3579                         DataTableReader reader = ds2.CreateDataReader ();
3580                         //ds1.Load (reader, LoadOption.PreserveChanges, dt3, dt4);
3581                         ds1.Load (reader, LoadOption.OverwriteChanges, dt3, dt4);
3582
3583                         Assert.AreEqual (ds2.Tables.Count, ds1.Tables.Count, "DataSet Tables count mistmatch");
3584                         int i = 0;
3585                         foreach (DataTable dt in ds1.Tables) {
3586                                 DataTable dt5 = ds2.Tables[i];
3587                                 Assert.AreEqual (dt5.Rows.Count, dt.Rows.Count, "Table " + dt.TableName + " row count mistmatch");
3588                                 int j = 0;
3589                                 DataRow row1;
3590                                 foreach (DataRow row in dt.Rows) {
3591                                         row1 = dt5.Rows[j];
3592                                         for (int k = 0; k < dt.Columns.Count; k++) {
3593                                                 Assert.AreEqual (row1[k], row[k], "DataRow " + k + " mismatch");
3594                                         }
3595                                         j++;
3596                                 }
3597                                 i++;
3598                         }
3599                 }
3600 #endif
3601                 private void AssertDataTableValues (DataTable dt)
3602                 {
3603                         Assert.AreEqual ("data1", dt.Rows[0]["_ID"], "1");
3604                         Assert.AreEqual ("data2", dt.Rows[0]["#ID"], "2");
3605                         Assert.AreEqual ("data3", dt.Rows[0]["%ID"], "2");
3606                         Assert.AreEqual ("data4", dt.Rows[0]["$ID"], "2");
3607                         Assert.AreEqual ("data5", dt.Rows[0][":ID"], "2");
3608                         Assert.AreEqual ("data6", dt.Rows[0][".ID"], "2");
3609                         Assert.AreEqual ("data7", dt.Rows[0]["ID"], "2");
3610                         Assert.AreEqual ("data8", dt.Rows[0]["*ID"], "2");
3611                         Assert.AreEqual ("data8", dt.Rows[0]["+ID"], "2");
3612                         Assert.AreEqual ("data8", dt.Rows[0]["-ID"], "2");
3613                         Assert.AreEqual ("data8", dt.Rows[0]["~ID"], "2");
3614                         Assert.AreEqual ("data8", dt.Rows[0]["@ID"], "2");
3615                         Assert.AreEqual ("data8", dt.Rows[0]["&ID"], "2");
3616
3617                 }
3618
3619                 [Test]  
3620                 public void Bug537229_BinFormatSerializer_Test ()
3621                 {
3622                         DataSet ds = new DataSet ();
3623                         DataTable dt = new DataTable ();
3624                         ds.Tables.Add (dt);
3625                         dt.Columns.Add ("_ID", typeof(String));
3626                         dt.Columns.Add ("#ID", typeof(String));
3627                         dt.Columns.Add ("%ID", typeof(String));
3628                         dt.Columns.Add ("$ID", typeof(String));
3629                         dt.Columns.Add (":ID", typeof(String));
3630                         dt.Columns.Add (".ID", typeof(String));
3631                         dt.Columns.Add ("ID", typeof(String));
3632                         dt.Columns.Add ("*ID", typeof(String));
3633                         dt.Columns.Add ("+ID", typeof(String));
3634                         dt.Columns.Add ("-ID", typeof(String));
3635                         dt.Columns.Add ("~ID", typeof(String));
3636                         dt.Columns.Add ("@ID", typeof(String));
3637                         dt.Columns.Add ("&ID", typeof(String));
3638                         DataRow row = dt.NewRow ();
3639                         row["#ID"] = "data2";
3640                         row["%ID"] = "data3";
3641                         row["$ID"] = "data4";
3642                         row["ID"] = "data7";
3643                         row[":ID"] = "data5";
3644                         row[".ID"] = "data6";
3645                         row["_ID"] = "data1";
3646                         row["*ID"] = "data8";
3647                         row["+ID"] = "data8";
3648                         row["-ID"] = "data8";
3649                         row["~ID"] = "data8";
3650                         row["@ID"] = "data8";
3651                         row["&ID"] = "data8";
3652                         dt.Rows.Add (row);
3653
3654                         AssertDataTableValues (dt);
3655
3656                         MemoryStream mstm=new MemoryStream();
3657                         BinaryFormatter bfmt=new BinaryFormatter();
3658                         bfmt.Serialize(mstm,dt);
3659                         MemoryStream mstm2=new MemoryStream(mstm.ToArray());
3660                         DataTable vdt=(DataTable)bfmt.Deserialize(mstm2);
3661                         AssertDataTableValues (vdt);
3662                 }
3663         }
3664 }