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