// // DataSetReadXmlSchemaTest.cs // // Author: // Atsushi Enomoto // // (C)2004 Novell Inc. // using System; using System.IO; using System.Data; using System.Globalization; using System.Text; using System.Threading; using System.Xml; using NUnit.Framework; namespace MonoTests.System.Data { [TestFixture] public class DataSetReadXmlSchemaTest : DataSetAssertion { private DataSet CreateTestSet () { DataSet ds = new DataSet (); ds.Tables.Add ("Table1"); ds.Tables.Add ("Table2"); ds.Tables [0].Columns.Add ("Column1_1"); ds.Tables [0].Columns.Add ("Column1_2"); ds.Tables [0].Columns.Add ("Column1_3"); ds.Tables [1].Columns.Add ("Column2_1"); ds.Tables [1].Columns.Add ("Column2_2"); ds.Tables [1].Columns.Add ("Column2_3"); ds.Tables [0].Rows.Add (new object [] {"ppp", "www", "xxx"}); ds.Relations.Add ("Rel1", ds.Tables [0].Columns [2], ds.Tables [1].Columns [0]); return ds; } CultureInfo currentCultureBackup; [SetUp] public void Setup () { currentCultureBackup = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo ("fi-FI"); } [TearDown] public void Teardown () { Thread.CurrentThread.CurrentCulture = currentCultureBackup; } [Test] public void SingleElementTreatmentDifference () { // This is one of the most complicated case. When the content // type particle of 'Root' element is a complex element, it // is DataSet element. Otherwise, it is just a data table. // // But also note that there is another test named // LocaleOnRootWithoutIsDataSet(), that tests if locale on // the (mere) data table modifies *DataSet's* locale. string xsbase = @" {0} "; string simple = ""; string complex = @" "; DataSet ds = new DataSet (); string xs = String.Format (xsbase, simple); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("simple", ds, "hoge", 1); AssertDataTable ("simple", ds.Tables [0], "Root", 1, 0); ds = new DataSet (); xs = String.Format (xsbase, complex); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("complex", ds, "Root", 1); AssertDataTable ("complex", ds.Tables [0], "Child", 2, 0); } [Test] public void UnusedComplexTypesIgnored () { string xs = @" "; DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); // Here "unusedType" table is never imported. AssertDataSet ("ds", ds, "hoge", 1); AssertDataTable ("dt", ds.Tables [0], "Root", 1, 0); } [Test] public void SimpleTypeComponentsIgnored () { string xs = @" "; DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); // nothing is imported. AssertDataSet ("ds", ds, "NewDataSet", 0); } [Test] public void IsDataSetAndTypeIgnored () { string xsbase = @" "; // Even if a global element uses a complexType, it will be // ignored if the element has msdata:IsDataSet='true' string xs = String.Format (xsbase, "true"); DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("ds", ds, "Root", 0); // name is "Root" // But when explicit msdata:IsDataSet value is "false", then // treat as usual. xs = String.Format (xsbase, "false"); ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("ds", ds, "NewDataSet", 1); } [Test] [ExpectedException (typeof (ArgumentException))] public void NestedReferenceNotAllowed () { string xs = @" "; // DataSet element cannot be converted into a DataTable. // (i.e. cannot be referenced in any other elements) DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); } [Test] public void IsDataSetOnLocalElementIgnored () { string xsbase = @" "; // msdata:IsDataSet does not affect even if the value is invalid string xs = String.Format (xsbase, "true"); DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); // Child should not be regarded as DataSet element AssertDataSet ("ds", ds, "NewDataSet", 1); } [Test] public void LocaleOnRootWithoutIsDataSet () { string xs = @" "; DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("ds", ds, "NewDataSet", 1); AssertEquals ("fi-FI", ds.Locale.Name); // DataSet's Locale comes from current thread DataTable dt = ds.Tables [0]; AssertDataTable ("dt", dt, "Root", 2, 0); AssertEquals ("ja-JP", dt.Locale.Name); // DataTable's Locale comes from msdata:Locale AssertDataColumn ("col1", dt.Columns [0], "Attr", true, false, 0, 1, "Attr", MappingType.Attribute, typeof (Int64), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false); AssertDataColumn ("col2", dt.Columns [1], "Child", false, false, 0, 1, "Child", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false); } [Test] public void ElementHasIdentityConstraint () { string constraints = @" "; string xsbase = @" {0} {1} {2} "; // Constraints on DataSet element. // Note that in xs:key xpath is crazy except for the last step string xs = String.Format (xsbase, constraints, String.Empty, String.Empty); DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertEquals (1, ds.Relations.Count); // Constraints on another global element - just ignored xs = String.Format (xsbase, String.Empty, constraints, String.Empty); ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertEquals (0, ds.Relations.Count); // Constraints on local element - just ignored xs = String.Format (xsbase, String.Empty, String.Empty, constraints); ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertEquals (0, ds.Relations.Count); } [Test] public void PrefixedTargetNS () { string xs = @" "; // No prefixes on tables and columns DataSet ds = new DataSet (); ds.ReadXmlSchema (new StringReader (xs)); AssertDataSet ("ds", ds, "DS", 3); DataTable dt = ds.Tables [0]; AssertDataTable ("R3", dt, "R3", 3, 0); AssertDataColumn ("col1", dt.Columns [0], "Attr", true, false, 0, 1, "Attr", MappingType.Attribute, typeof (Int64), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false); } [Test] public void ReadTest1 () { DataSet ds = CreateTestSet (); StringWriter sw = new StringWriter (); ds.WriteXmlSchema (sw); string schema = sw.ToString (); // ReadXmlSchema() ds = new DataSet (); ds.ReadXmlSchema (new XmlTextReader (schema, XmlNodeType.Document, null)); ReadTest1Check (ds); // ReadXml() should also be the same ds = new DataSet (); ds.ReadXml (new XmlTextReader (schema, XmlNodeType.Document, null)); ReadTest1Check (ds); } private void ReadTest1Check (DataSet ds) { AssertDataSet ("dataset", ds, "NewDataSet", 2); AssertDataTable ("tbl1", ds.Tables [0], "Table1", 3, 0); AssertDataTable ("tbl2", ds.Tables [1], "Table2", 3, 0); DataRelation rel = ds.Relations [0]; AssertDataRelation ("rel", rel, "Rel1", new string [] {"Column1_3"}, new string [] {"Column2_1"}, true, true); AssertUniqueConstraint ("uc", rel.ParentKeyConstraint, "Constraint1", false, new string [] {"Column1_3"}); AssertForeignKeyConstraint ("fk", rel.ChildKeyConstraint, "Rel1", AcceptRejectRule.None, Rule.Cascade, Rule.Cascade, new string [] {"Column2_1"}, new string [] {"Column1_3"}); } } }