Added time format with only offset. Fixes #22558.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest3.cs
1 // Authors:
2 //   Nagappan A <anagappan@novell.com>
3 //
4 // Copyright (c) 2007 Novell, Inc
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 #if NET_2_0
27 using System;
28 using System.Data;
29 using System.IO;
30 using System.Xml;
31
32 using NUnit.Framework;
33
34 namespace Monotests_System.Data
35 {
36         [TestFixture]
37         public class DataTableTest3
38         {
39                 string tempFile;
40                 DataSet dataSet;
41                 DataTable parentTable;
42                 DataTable childTable;
43                 DataTable secondChildTable;
44
45                 [SetUp]
46                 public void SetUp ()
47                 {
48                         tempFile = Path.GetTempFileName ();
49                 }
50
51                 [TearDown]
52                 public void TearDown ()
53                 {
54                         if (tempFile != null)
55                                 File.Delete (tempFile);
56                 }
57
58                 private void MakeParentTable ()
59                 {
60                         // Create a new Table
61                         parentTable = new DataTable ("ParentTable");
62                         dataSet = new DataSet ("XmlSchemaDataSet");
63                         DataColumn column;
64                         DataRow row;
65  
66                         // Create new DataColumn, set DataType,
67                         // ColumnName and add to Table.
68                         column = new DataColumn ();
69                         column.DataType = typeof (int);
70                         column.ColumnName = "id";
71                         column.Unique = true;
72                         // Add the Column to the DataColumnCollection.
73                         parentTable.Columns.Add (column);
74
75                         // Create second column
76                         column = new DataColumn ();
77                         column.DataType = typeof (string);
78                         column.ColumnName = "ParentItem";
79                         column.AutoIncrement = false;
80                         column.Caption = "ParentItem";
81                         column.Unique = false;
82                         // Add the column to the table
83                         parentTable.Columns.Add (column);
84
85                         // Create third column.
86                         column = new DataColumn ();
87                         column.DataType = typeof (int);
88                         column.ColumnName = "DepartmentID";
89                         column.Caption = "DepartmentID";
90                         // Add the column to the table.
91                         parentTable.Columns.Add (column);
92
93                         // Make the ID column the primary key column.
94                         DataColumn [] PrimaryKeyColumns = new DataColumn [2];
95                         PrimaryKeyColumns [0] = parentTable.Columns ["id"];
96                         PrimaryKeyColumns [1] = parentTable.Columns ["DepartmentID"];
97                         parentTable.PrimaryKey = PrimaryKeyColumns;
98
99                         dataSet.Tables.Add (parentTable);
100  
101                         // Create three new DataRow objects and add 
102                         // them to the DataTable
103                         for (int i = 0; i <= 2; i++) {
104                                 row = parentTable.NewRow ();
105                                 row ["id"] = i + 1 ;
106                                 row ["ParentItem"] = "ParentItem " + (i + 1);
107                                 row ["DepartmentID"] = i + 1;
108                                 parentTable.Rows.Add (row);
109                         }
110                 }
111
112                 private void MakeChildTable ()
113                 {
114                         // Create a new Table
115                         childTable = new DataTable ("ChildTable");
116                         DataColumn column;
117                         DataRow row;
118  
119                         // Create first column and add to the DataTable.
120                         column = new DataColumn ();
121                         column.DataType= typeof (int);
122                         column.ColumnName = "ChildID";
123                         column.AutoIncrement = true;
124                         column.Caption = "ID";
125                         column.Unique = true;
126
127                         // Add the column to the DataColumnCollection
128                         childTable.Columns.Add (column);
129
130                         // Create second column
131                         column = new DataColumn ();
132                         column.DataType= typeof (string);
133                         column.ColumnName = "ChildItem";
134                         column.AutoIncrement = false;
135                         column.Caption = "ChildItem";
136                         column.Unique = false;
137                         childTable.Columns.Add (column);
138                          
139                         //Create third column
140                         column = new DataColumn ();
141                         column.DataType= typeof (int);
142                         column.ColumnName = "ParentID";
143                         column.AutoIncrement = false;
144                         column.Caption = "ParentID";
145                         column.Unique = false;
146                         childTable.Columns.Add (column);
147
148                         dataSet.Tables.Add (childTable);
149  
150                         // Create three sets of DataRow objects,
151                         // five rows each, and add to DataTable.
152                         for (int i = 0; i <= 1; i ++) {
153                                 row = childTable.NewRow ();
154                                 row ["childID"] = i + 1;
155                                 row ["ChildItem"] = "ChildItem " + (i + 1);
156                                 row ["ParentID"] = 1 ;
157                                 childTable.Rows.Add (row);
158                         }
159
160                         for (int i = 0; i <= 1; i ++) {
161                                 row = childTable.NewRow ();
162                                 row ["childID"] = i + 5;
163                                 row ["ChildItem"] = "ChildItem " + (i + 1);
164                                 row ["ParentID"] = 2 ;
165                                 childTable.Rows.Add (row);
166                         }
167
168                         for (int i = 0; i <= 1; i ++) {
169                                 row = childTable.NewRow ();
170                                 row ["childID"] = i + 10;
171                                 row ["ChildItem"] = "ChildItem " + (i + 1);
172                                 row ["ParentID"] = 3 ;
173                                 childTable.Rows.Add (row);
174                         }
175                 }
176
177                 private void MakeSecondChildTable ()
178                 {
179                         // Create a new Table
180                         secondChildTable = new DataTable ("SecondChildTable");
181                         DataColumn column;
182                         DataRow row;
183  
184                         // Create first column and add to the DataTable.
185                         column = new DataColumn ();
186                         column.DataType= typeof (int);
187                         column.ColumnName = "ChildID";
188                         column.AutoIncrement = true;
189                         column.Caption = "ID";
190                         column.ReadOnly = true;
191                         column.Unique = true;
192
193                         // Add the column to the DataColumnCollection.
194                         secondChildTable.Columns.Add (column);
195
196                         // Create second column.
197                         column = new DataColumn ();
198                         column.DataType= typeof (string);
199                         column.ColumnName = "ChildItem";
200                         column.AutoIncrement = false;
201                         column.Caption = "ChildItem";
202                         column.ReadOnly = false;
203                         column.Unique = false;
204                         secondChildTable.Columns.Add (column);
205
206                         //Create third column.
207                         column = new DataColumn ();
208                         column.DataType= typeof (int);
209                         column.ColumnName = "ParentID";
210                         column.AutoIncrement = false;
211                         column.Caption = "ParentID";
212                         column.ReadOnly = false;
213                         column.Unique = false;
214                         secondChildTable.Columns.Add (column);
215
216                         //Create fourth column.
217                         column = new DataColumn ();
218                         column.DataType= typeof (int);
219                         column.ColumnName = "DepartmentID";
220                         column.Caption = "DepartmentID";
221                         column.Unique = false;
222                         secondChildTable.Columns.Add (column);
223
224                         dataSet.Tables.Add (secondChildTable);
225
226                         // Create three sets of DataRow objects,
227                         // five rows each, and add to DataTable.
228                         for (int i = 0; i <= 1; i++) {
229                                 row = secondChildTable.NewRow ();
230                                 row ["childID"] = i + 1;
231                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
232                                 row ["ParentID"] = 1 ;
233                                 row ["DepartmentID"] = 1;
234                                 secondChildTable.Rows.Add (row);
235                         }
236
237                         for (int i = 0;i <= 1;i++) {
238                                 row = secondChildTable.NewRow ();
239                                 row ["childID"] = i + 5;
240                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
241                                 row ["ParentID"] = 2;
242                                 row ["DepartmentID"] = 2;
243                                 secondChildTable.Rows.Add (row);
244                         }
245
246                         for (int i = 0;i <= 1;i++) {
247                                 row = secondChildTable.NewRow ();
248                                 row ["childID"] = i + 10;
249                                 row ["ChildItem"] = "SecondChildItem " + (i + 1);
250                                 row ["ParentID"] = 3 ;
251                                 row ["DepartmentID"] = 3;
252                                 secondChildTable.Rows.Add (row);
253                         }
254                 }
255
256                 private void MakeDataRelation ()
257                 {
258                         DataColumn parentColumn = dataSet.Tables ["ParentTable"].Columns ["id"];
259                         DataColumn childColumn = dataSet.Tables ["ChildTable"].Columns ["ParentID"];
260                         DataRelation relation = new DataRelation ("ParentChild_Relation1", parentColumn, childColumn);
261                         dataSet.Tables ["ChildTable"].ParentRelations.Add (relation);
262         
263                         DataColumn [] parentColumn1 = new DataColumn [2];
264                         DataColumn [] childColumn1 = new DataColumn [2];
265
266                         parentColumn1 [0] = dataSet.Tables ["ParentTable"].Columns ["id"];
267                         parentColumn1 [1] = dataSet.Tables ["ParentTable"].Columns ["DepartmentID"];
268
269                         childColumn1 [0] = dataSet.Tables ["SecondChildTable"].Columns ["ParentID"];
270                         childColumn1 [1] = dataSet.Tables ["SecondChildTable"].Columns ["DepartmentID"];
271  
272                         DataRelation secondRelation = new DataRelation("ParentChild_Relation2", parentColumn1, childColumn1);
273                         dataSet.Tables ["SecondChildTable"].ParentRelations.Add (secondRelation);
274                 }
275
276                 //Test properties of a table which does not belongs to a DataSet
277                 private void VerifyTableSchema (DataTable table, string tableName, DataSet ds)
278                 {
279                         //Check Properties of Table
280                         Assert.AreEqual ("", table.Namespace, "#1");
281                         Assert.AreEqual (ds, table.DataSet, "#2");
282                         Assert.AreEqual (3, table.Columns.Count, "#3");
283                         Assert.AreEqual (0, table.Rows.Count, "#4");
284                         Assert.AreEqual (false, table.CaseSensitive, "#5");
285                         Assert.AreEqual (tableName, table.TableName, "#6");
286                         Assert.AreEqual (2, table.Constraints.Count, "#7");
287                         Assert.AreEqual ("", table.Prefix, "#8");
288                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
289                         Assert.AreEqual ("Constraint2", table.Constraints [1].ToString (), "#10");
290                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType (), "#11");
291                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [1].GetType (), "#12");
292                         Assert.AreEqual (2, table.PrimaryKey.Length, "#13");
293                         Assert.AreEqual ("id", table.PrimaryKey [0].ToString (), "#14");
294                         Assert.AreEqual ("DepartmentID", table.PrimaryKey [1].ToString (), "#15");
295
296                         Assert.AreEqual (0, table.ParentRelations.Count, "#16");
297                         Assert.AreEqual (0, table.ChildRelations.Count, "#17");
298
299                         //Check properties of each column
300                         //First Column
301                         DataColumn col = table.Columns [0];
302                         Assert.AreEqual (false, col.AllowDBNull, "#18");
303                         Assert.AreEqual (false, col.AutoIncrement, "#19");
304                         Assert.AreEqual (0, col.AutoIncrementSeed, "#20");
305                         Assert.AreEqual (1, col.AutoIncrementStep, "#21");
306                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#22");
307                         Assert.AreEqual ("id", col.Caption, "#23");
308                         Assert.AreEqual ("id", col.ColumnName, "#24");
309                         Assert.AreEqual (typeof (int), col.DataType, "#25");
310                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#26");
311                         Assert.AreEqual (false, col.DesignMode, "#27");
312                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#28");
313                         Assert.AreEqual (-1, col.MaxLength, "#29");
314                         Assert.AreEqual (0, col.Ordinal, "#30");
315                         Assert.AreEqual (string.Empty, col.Prefix, "#31");
316                         Assert.AreEqual (tableName, col.Table.ToString (), "#32");
317                         Assert.AreEqual (true, col.Unique, "#33");
318
319                         //Second Column
320                         col = table.Columns [1];
321                         Assert.AreEqual (true, col.AllowDBNull, "#34");
322                         Assert.AreEqual (false, col.AutoIncrement, "#35");
323                         Assert.AreEqual (0, col.AutoIncrementSeed, "#36");
324                         Assert.AreEqual (1, col.AutoIncrementStep, "#37");
325                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#38");
326                         Assert.AreEqual ("ParentItem", col.Caption, "#39");
327                         Assert.AreEqual ("ParentItem", col.ColumnName, "#40");
328                         Assert.AreEqual (typeof (string), col.DataType, "#41");
329                         Assert.AreEqual ("", col.DefaultValue.ToString (), "#42");
330                         Assert.AreEqual (false, col.DesignMode, "#43");
331                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#44");
332                         Assert.AreEqual (-1, col.MaxLength, "#45");
333                         Assert.AreEqual (1, col.Ordinal, "#46");
334                         Assert.AreEqual ("", col.Prefix, "#47");
335                         Assert.AreEqual (tableName, col.Table.ToString (), "#48");
336                         Assert.AreEqual (false, col.Unique, "#49");
337
338                         //Third Column
339                         col = table.Columns [2];
340                         Assert.AreEqual (false, col.AllowDBNull, "#50");
341                         Assert.AreEqual (false, col.AutoIncrement, "#51");
342                         Assert.AreEqual (0, col.AutoIncrementSeed, "#52");
343                         Assert.AreEqual (1, col.AutoIncrementStep, "#53");
344                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#54");
345                         Assert.AreEqual ("DepartmentID", col.Caption, "#55");
346                         Assert.AreEqual ("DepartmentID", col.ColumnName, "#56");
347                         Assert.AreEqual (typeof (int), col.DataType, "#57");
348                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#58");
349                         Assert.AreEqual (false, col.DesignMode, "#59");
350                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#60");
351                         Assert.AreEqual (-1, col.MaxLength, "#61");
352                         Assert.AreEqual (2, col.Ordinal, "#62");
353                         Assert.AreEqual ("", col.Prefix, "#63");
354                         Assert.AreEqual (tableName, col.Table.ToString (), "#64");
355                         Assert.AreEqual (false, col.Unique, "#65");
356                 }
357
358                 private void VerifyParentTableSchema (DataTable table, string tableName, DataSet ds)
359                 {
360                         //Check Properties of Table
361                         Assert.AreEqual (string.Empty, table.Namespace, "#1");
362                         Assert.AreEqual (ds.DataSetName, table.DataSet.DataSetName, "#2");
363                         Assert.AreEqual (3, table.Columns.Count, "#3");
364                         Assert.AreEqual (0, table.Rows.Count, "#4");
365                         Assert.AreEqual (false, table.CaseSensitive, "#5");
366                         Assert.AreEqual ("ParentTable", table.TableName, "#6");
367                         Assert.AreEqual (2, table.Constraints.Count, "#7");
368                         Assert.AreEqual (string.Empty, table.Prefix, "#8");
369
370                         //Check Constraints
371                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
372                         Assert.AreEqual ("Constraint2", table.Constraints [1].ToString (), "#10");
373                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType (), "#11");
374                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [1].GetType (), "#12");
375                         Assert.AreEqual (2, table.PrimaryKey.Length, "#13");
376                         Assert.AreEqual ("id", table.PrimaryKey [0].ToString (), "#14");
377                         Assert.AreEqual ("DepartmentID", table.PrimaryKey [1].ToString (), "#15");
378
379                         //Check Relations of the ParentTable
380                         Assert.AreEqual (0, table.ParentRelations.Count, "#16");
381                         Assert.AreEqual (2, table.ChildRelations.Count, "#17");
382                         Assert.AreEqual ("ParentChild_Relation1", table.ChildRelations [0].ToString (), "#18");
383                         Assert.AreEqual ("ParentChild_Relation2", table.ChildRelations [1].ToString (), "#19");
384                         Assert.AreEqual ("ChildTable", table.ChildRelations [0].ChildTable.TableName, "#20");
385                         Assert.AreEqual ("SecondChildTable", table.ChildRelations [1].ChildTable.TableName, "#21");
386
387                         Assert.AreEqual (1, table.ChildRelations [0].ParentColumns.Length, "#22");
388                         Assert.AreEqual ("id", table.ChildRelations [0].ParentColumns [0].ColumnName, "#23");
389                         Assert.AreEqual (1, table.ChildRelations [0].ChildColumns.Length, "#24");
390                         Assert.AreEqual ("ParentID", table.ChildRelations [0].ChildColumns [0].ColumnName, "#25");
391
392                         Assert.AreEqual (2, table.ChildRelations [1].ParentColumns.Length, "#26");
393                         Assert.AreEqual ("id", table.ChildRelations [1].ParentColumns [0].ColumnName, "#27");
394                         Assert.AreEqual ("DepartmentID", table.ChildRelations [1].ParentColumns [1].ColumnName, "#28");
395                         Assert.AreEqual (2, table.ChildRelations [1].ChildColumns.Length, "#29");
396                         
397                         Assert.AreEqual ("ParentID", table.ChildRelations [1].ChildColumns [0].ColumnName, "#30");
398                         Assert.AreEqual ("DepartmentID", table.ChildRelations [1].ChildColumns [1].ColumnName, "#31");
399
400                         //Check properties of each column
401                         //First Column
402                         DataColumn col = table.Columns [0];
403                         Assert.AreEqual (false, col.AllowDBNull, "#32");
404                         Assert.AreEqual (false, col.AutoIncrement, "#33");
405                         Assert.AreEqual (0, col.AutoIncrementSeed, "#34");
406                         Assert.AreEqual (1, col.AutoIncrementStep, "#35");
407                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#36");
408                         Assert.AreEqual ("id", col.Caption, "#37");
409                         Assert.AreEqual ("id", col.ColumnName, "#38");
410                         Assert.AreEqual (typeof (int), col.DataType, "#39");
411                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#40");
412                         Assert.AreEqual (false, col.DesignMode, "#41");
413                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#42");
414                         Assert.AreEqual (-1, col.MaxLength, "#43");
415                         Assert.AreEqual (0, col.Ordinal, "#44");
416                         Assert.AreEqual (string.Empty, col.Prefix, "#45");
417                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#46");
418                         Assert.AreEqual (true, col.Unique, "#47");
419
420                         //Second Column
421                         col = table.Columns [1];
422                         Assert.AreEqual (true, col.AllowDBNull, "#48");
423                         Assert.AreEqual (false, col.AutoIncrement, "#49");
424                         Assert.AreEqual (0, col.AutoIncrementSeed, "#50");
425                         Assert.AreEqual (1, col.AutoIncrementStep, "#51");
426                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#52");
427                         Assert.AreEqual ("ParentItem", col.Caption, "#53");
428                         Assert.AreEqual ("ParentItem", col.ColumnName, "#54");
429                         Assert.AreEqual (typeof (string), col.DataType, "#55");
430                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#56");
431                         Assert.AreEqual (false, col.DesignMode, "#57");
432                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#58");
433                         Assert.AreEqual (-1, col.MaxLength, "#59");
434                         Assert.AreEqual (1, col.Ordinal, "#60");
435                         Assert.AreEqual (string.Empty, col.Prefix, "#61");
436                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#62");
437                         Assert.AreEqual (false, col.Unique, "#63");
438
439                         //Third Column
440                         col = table.Columns [2];
441                         Assert.AreEqual (false, col.AllowDBNull, "#64");
442                         Assert.AreEqual (false, col.AutoIncrement, "#65");
443                         Assert.AreEqual (0, col.AutoIncrementSeed, "#66");
444                         Assert.AreEqual (1, col.AutoIncrementStep, "#67");
445                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#68");
446                         Assert.AreEqual ("DepartmentID", col.Caption, "#69");
447                         Assert.AreEqual ("DepartmentID", col.ColumnName, "#70");
448                         Assert.AreEqual (typeof (int), col.DataType, "#71");
449                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#72");
450                         Assert.AreEqual (false, col.DesignMode, "#73");
451                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#74");
452                         Assert.AreEqual (-1, col.MaxLength, "#75");
453                         Assert.AreEqual (2, col.Ordinal, "#76");
454                         Assert.AreEqual (string.Empty, col.Prefix, "#77");
455                         Assert.AreEqual ("ParentTable", col.Table.ToString (), "#78");
456                         Assert.AreEqual (false, col.Unique, "#79");
457                         
458                 }
459
460                 [Test]
461                 public void XmlSchemaTest1 ()
462                 {
463                         
464                         MakeParentTable ();
465                         //Detach the table from the DataSet
466                         dataSet.Tables.Remove (parentTable);
467
468                         //Write
469                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
470                                 parentTable.WriteXmlSchema (stream);
471                         }
472
473                         //Read
474                         DataTable table = new DataTable ();
475                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
476                                 table.ReadXmlSchema (stream);
477                         }
478
479                         VerifyTableSchema (table, parentTable.TableName, parentTable.DataSet);
480                 }
481
482                 [Test]
483                 public void XmlSchemaTest2 ()
484                 {
485                         MakeParentTable ();
486                         
487                         dataSet.Tables.Remove (parentTable);
488                         parentTable.TableName = String.Empty;
489
490                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
491                                 try {
492                                         parentTable.WriteXmlSchema (stream);
493                                         Assert.Fail ("#1");
494                                 } catch (InvalidOperationException ex) {
495                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
496                                         Assert.IsNull (ex.InnerException, "#3");
497                                         Assert.IsNotNull (ex.Message, "#4");
498                                 }
499                         }
500                 }
501                 
502                 [Test]
503                 public void XmlSchemaTest3 ()
504                 {
505                         //Write
506                         MakeParentTable ();
507
508                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
509                                 parentTable.WriteXmlSchema (stream);
510                         }
511
512                         //Read
513                         DataTable table = new DataTable ();
514                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
515                                 table.ReadXmlSchema (stream);
516                         }
517
518                         VerifyTableSchema (table, parentTable.TableName, null);
519                 }
520                 
521                 [Test]
522                 [Category ("NotWorking")]
523                 public void XmlSchemaTest4 ()
524                 {
525                         MakeParentTable ();
526                         MakeChildTable ();
527                         MakeSecondChildTable ();
528                         MakeDataRelation ();
529
530                         //Write
531                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
532                                 parentTable.WriteXmlSchema (stream, true);
533                         }
534
535                         //Read
536                         DataTable table = new DataTable ();
537                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
538                                 table.ReadXmlSchema (stream);
539                         }
540
541                         //Test Property of Parent
542                         VerifyParentTableSchema (table, parentTable.TableName, parentTable.DataSet);
543
544                         //Check Properties of First Child Table
545                         DataTable firstChildTable = parentTable.ChildRelations [0].ChildTable;
546                         Assert.AreEqual (string.Empty, firstChildTable.Namespace, "#1");
547                         Assert.AreEqual ("XmlSchemaDataSet", firstChildTable.DataSet.DataSetName, "#2");
548                         Assert.AreEqual (3, firstChildTable.Columns.Count, "#3");
549                         Assert.AreEqual (6, firstChildTable.Rows.Count, "#4");
550                         Assert.AreEqual (false, firstChildTable.CaseSensitive, "#5");
551                         Assert.AreEqual ("ChildTable", firstChildTable.TableName, "#6");
552                         Assert.AreEqual (string.Empty, firstChildTable.Prefix, "#7");
553                         Assert.AreEqual (2, firstChildTable.Constraints.Count, "#8");
554                         Assert.AreEqual ("Constraint1", firstChildTable.Constraints [0].ToString (), "#9");
555                         Assert.AreEqual ("ParentChild_Relation1", firstChildTable.Constraints [1].ToString (), "#10");
556                         Assert.AreEqual (1, firstChildTable.ParentRelations.Count, "#11");
557                         Assert.AreEqual (0, firstChildTable.ChildRelations.Count, "#12");
558                         Assert.AreEqual (0, firstChildTable.PrimaryKey.Length, "#13");
559                         
560                         //Check Properties of Second Child Table
561                         DataTable secondChildTable = parentTable.ChildRelations [1].ChildTable;
562                         Assert.AreEqual (string.Empty, secondChildTable.Namespace, "#14");
563                         Assert.AreEqual ("XmlSchemaDataSet", secondChildTable.DataSet.DataSetName, "#15");
564                         Assert.AreEqual (4, secondChildTable.Columns.Count, "#16");
565                         Assert.AreEqual (6, secondChildTable.Rows.Count, "#17");
566                         Assert.AreEqual (false, secondChildTable.CaseSensitive, "#18");
567                         Assert.AreEqual ("SecondChildTable", secondChildTable.TableName, "#19");
568                         Assert.AreEqual (string.Empty, secondChildTable.Prefix, "#20");
569                         Assert.AreEqual (2, secondChildTable.Constraints.Count, "#21");
570                         Assert.AreEqual ("Constraint1", secondChildTable.Constraints [0].ToString (), "#22");
571                         Assert.AreEqual ("ParentChild_Relation2", secondChildTable.Constraints [1].ToString (), "#23");
572                         Assert.AreEqual (1, secondChildTable.ParentRelations.Count, "#24");;
573                         Assert.AreEqual (0, secondChildTable.ChildRelations.Count, "#25");
574                         Assert.AreEqual (0, secondChildTable.PrimaryKey.Length, "#26");
575                         
576                 }
577                 
578                 [Test]
579                 public void XmlSchemaTest5 ()
580                 {
581                         MakeParentTable ();
582                         MakeChildTable ();
583                         MakeSecondChildTable ();
584                         MakeDataRelation ();
585                         
586                         //Write
587                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
588                                 childTable.WriteXmlSchema (stream);
589                         }
590
591                         //Read
592                         DataTable table = new DataTable (childTable.TableName);
593                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
594                                 table.ReadXmlSchema (stream);
595                         }
596                         
597                         //Check Properties of the table
598                         Assert.AreEqual (string.Empty, table.Namespace, "#1");
599                         Assert.IsNull (table.DataSet, "#2");
600                         Assert.AreEqual (3, table.Columns.Count, "#3");
601                         Assert.AreEqual (0, table.Rows.Count, "#4");
602                         Assert.AreEqual (false, table.CaseSensitive, "#5");
603                         Assert.AreEqual ("ChildTable", table.TableName, "#6");
604                         Assert.AreEqual (string.Empty, table.Prefix, "#7");
605                         Assert.AreEqual (1, table.Constraints.Count, "#8");
606                         Assert.AreEqual ("Constraint1", table.Constraints [0].ToString (), "#9");
607                         Assert.AreEqual (typeof (UniqueConstraint), table.Constraints [0].GetType(), "#10");
608                         Assert.AreEqual (0, table.ParentRelations.Count, "#11");
609                         Assert.AreEqual (0, table.ChildRelations.Count, "#12");
610                         Assert.AreEqual (0, table.PrimaryKey.Length, "#13");
611                         
612                         //First Column
613                         DataColumn col = table.Columns [0];
614                         Assert.AreEqual (true, col.AllowDBNull, "#14");
615                         Assert.AreEqual (true, col.AutoIncrement, "#15");
616                         Assert.AreEqual (0, col.AutoIncrementSeed, "#16");
617                         Assert.AreEqual (1, col.AutoIncrementStep, "#17");
618                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#18");
619                         Assert.AreEqual ("ChildID", col.ColumnName, "#19");
620                         Assert.AreEqual (typeof (int), col.DataType, "#20");
621                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#21");
622                         Assert.AreEqual (false, col.DesignMode, "#22");
623                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#23");
624                         Assert.AreEqual (-1, col.MaxLength, "#24");
625                         Assert.AreEqual (0, col.Ordinal, "#25");
626                         Assert.AreEqual (string.Empty, col.Prefix, "#26");
627                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#27");
628                         Assert.AreEqual (true, col.Unique, "#28");
629
630                         //Second Column
631                         col = table.Columns [1];
632                         Assert.AreEqual (true, col.AllowDBNull, "#29");
633                         Assert.AreEqual (false, col.AutoIncrement, "#30");
634                         Assert.AreEqual (0, col.AutoIncrementSeed, "#31");
635                         Assert.AreEqual (1, col.AutoIncrementStep, "#32");
636                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#33");
637                         Assert.AreEqual ("ChildItem", col.Caption, "#34");
638                         Assert.AreEqual ("ChildItem", col.ColumnName, "#35");
639                         Assert.AreEqual (typeof (string), col.DataType, "#36");
640                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#37");
641                         Assert.AreEqual (false, col.DesignMode, "#38");
642                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#39");
643                         Assert.AreEqual (-1, col.MaxLength, "#40");
644                         Assert.AreEqual (1, col.Ordinal, "#41");
645                         Assert.AreEqual (string.Empty, col.Prefix, "#42");
646                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#42");
647                         Assert.AreEqual (false, col.Unique, "#43");
648
649                         //Third Column
650                         col = table.Columns [2];
651                         Assert.AreEqual (true, col.AllowDBNull, "#44");
652                         Assert.AreEqual (false, col.AutoIncrement, "#45");
653                         Assert.AreEqual (0, col.AutoIncrementSeed, "#46");
654                         Assert.AreEqual (1, col.AutoIncrementStep, "#47");
655                         Assert.AreEqual ("Element", col.ColumnMapping.ToString (), "#48");
656                         Assert.AreEqual ("ParentID", col.Caption, "#49");
657                         Assert.AreEqual ("ParentID", col.ColumnName, "#50");
658                         Assert.AreEqual (typeof (int), col.DataType, "#51");
659                         Assert.AreEqual (string.Empty, col.DefaultValue.ToString (), "#52");
660                         Assert.AreEqual (false, col.DesignMode, "#53");
661                         Assert.AreEqual ("System.Data.PropertyCollection", col.ExtendedProperties.ToString (), "#54");
662                         Assert.AreEqual (-1, col.MaxLength, "#55");
663                         Assert.AreEqual (2, col.Ordinal, "#56");
664                         Assert.AreEqual (string.Empty, col.Prefix, "#57");
665                         Assert.AreEqual ("ChildTable", col.Table.ToString (), "#58");
666                         Assert.AreEqual (false, col.Unique, "#59");
667                 }
668
669                 [Test]
670                 public void XmlSchemaTest6 ()
671                 {
672                         MakeParentTable ();
673
674                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
675                                 parentTable.WriteXmlSchema (stream);
676                         }
677
678                         DataTable table = new DataTable ();
679                         DataSet ds = new DataSet ();
680                         ds.Tables.Add (table);
681                         
682                         using (FileStream stream = new FileStream (tempFile, FileMode.Open)) {
683                                 try {
684                                         table.ReadXmlSchema (stream);
685                                         Assert.Fail ("#1");
686                                 } catch (ArgumentException ex) {
687                                         // DataTable 'Table1' does not match
688                                         // to any DataTable in source
689                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
690                                         Assert.IsNull (ex.InnerException, "#3");
691                                         Assert.IsNotNull (ex.Message, "#4");
692                                         Assert.IsTrue (ex.Message.IndexOf ("'Table1'") != -1, "#5");
693                                         Assert.IsNull (ex.ParamName, "#6");
694                                 }
695                         }
696                 }
697
698                 [Test]
699                 public void XmlSchemaTest7 ()
700                 {
701                         DataTable table = new DataTable ();
702                         
703                         try {
704                                 table.ReadXmlSchema (string.Empty);
705                                 Assert.Fail ("#1");
706                         } catch (ArgumentException ex) {
707                                 // The URL cannot be empty
708                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
709                                 Assert.IsNull (ex.InnerException, "#3");
710                                 Assert.IsNotNull (ex.Message, "#4");
711                                 //Assert.AreEqual ("url", ex.ParamName, "#5");
712                         }
713                 }
714
715                 [Test]
716                 public void XmlSchemaTest8 ()
717                 {
718                         MakeParentTable ();
719
720                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
721                                 parentTable.WriteXmlSchema (stream);
722                         }
723                 
724                         //Create a table and define the schema partially
725                         DataTable table = new DataTable ();
726                         table.Columns.Add (new DataColumn (parentTable.Columns [0].ColumnName, typeof (int)));
727
728                         //ReadXmlSchema will not read any schema in this case
729                         table.ReadXmlSchema (tempFile);
730
731                         Assert.AreEqual (string.Empty, table.TableName, "#1");
732                         Assert.AreEqual (1, table.Columns.Count, "#2");
733                         Assert.AreEqual (0, table.Constraints.Count, "#3");
734                 }
735
736                 [Test]
737                 public void XmlSchemaTest9 ()
738                 {
739                         MakeParentTable ();
740
741                         using (FileStream stream = new FileStream (tempFile, FileMode.Create)) {
742                                 parentTable.WriteXmlSchema (stream);
743                         }
744                 
745                         //Create a table and define the full schema 
746                         DataTable table = new DataTable ();
747                         table.Columns.Add (new DataColumn (parentTable.Columns [0].ColumnName, typeof (int)));
748                         table.Columns.Add (new DataColumn (parentTable.Columns [1].ColumnName, typeof (string)));
749                         table.Columns.Add (new DataColumn (parentTable.Columns [2].ColumnName, typeof (int)));
750
751                         //ReadXmlSchema will not read any schema in this case
752                         table.ReadXmlSchema (tempFile);
753
754                         Assert.AreEqual (string.Empty, table.TableName, "#1");
755                         Assert.AreEqual (3, table.Columns.Count, "#2");
756                         Assert.AreEqual (0, table.Constraints.Count, "#3");
757                 }
758         }
759 }
760 #endif