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