2005-01-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Data / Test / System.Data / DataSetAssertion.cs
index 2c32971b532f4f8a60728a100f05bb67da3f8c1e..08ab1bbe7267b7413e31effd553841d577c6a947 100755 (executable)
@@ -4,20 +4,96 @@
 // Author:
 //     Atsushi Enomoto <atsushi@ximian.com>
 //
-// (C)2004 Novell Inc.
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+
 using System;
+using System.Collections;
 using System.IO;
 using System.Data;
 using System.Text;
 using System.Xml;
+using System.Xml.Schema;
 using NUnit.Framework;
 
 namespace MonoTests.System.Data
 {
        public class DataSetAssertion : Assertion
        {
+               public string GetNormalizedSchema (string source)
+               {
+/*
+                       // Due to the implementation difference, we must have
+                       // one more step to reorder attributes. Here, read
+                       // schema document into XmlSchema once, and compare
+                       // output string with those emission from Write().
+                       XmlSchema xs = XmlSchema.Read (new XmlTextReader (
+                               new StringReader (source)), null);
+                       StringWriter writer = new StringWriter ();
+                       xs.Write (writer);
+                       return writer.ToString ();
+*/
+                       XmlDocument doc = new XmlDocument ();
+                       doc.LoadXml (source);
+                       SortAttributes (doc.DocumentElement);
+                       StringWriter writer = new StringWriter ();
+                       doc.Save (writer);
+                       return writer.ToString ();
+               }
+
+               private void SortAttributes (XmlElement el)
+               {
+                       SortAttributesAttributes (el);
+                       ArrayList al = new ArrayList ();
+                       foreach (XmlNode n in el.ChildNodes) {
+                               if (n.NodeType == XmlNodeType.Element)
+                                       SortAttributes (n as XmlElement);
+                               if (n.NodeType == XmlNodeType.Comment)
+                                       al.Add (n);
+                       }
+                       foreach (XmlNode n in al)
+                               el.RemoveChild (n);
+               }
+
+               private void SortAttributesAttributes (XmlElement el)
+               {
+                       ArrayList al = new ArrayList ();
+                       foreach (XmlAttribute a in el.Attributes)
+                               al.Add (a.Name);
+                       al.Sort ();
+                       string [] names = (string []) al.ToArray (typeof (string));
+                       al.Clear ();
+                       foreach (string name in names)
+                               al.Add (el.RemoveAttributeNode (
+                                       el.GetAttributeNode (name)));
+                       foreach (XmlAttribute a in al)
+                               // Exclude xmlns="" here.
+                               if (a.Name != "xmlns" || a.Value != String.Empty)
+                                       el.SetAttributeNode (a);
+               }
+
                public void AssertDataSet (string label, DataSet ds, string name, int tableCount, int relCount)
                {
                        AssertEquals (label + ".DataSetName", name, ds.DataSetName);
@@ -26,27 +102,39 @@ namespace MonoTests.System.Data
                                AssertEquals (label + ".RelationCount", relCount, ds.Relations.Count);
                }
 
-               public void AssertDataTable (string label, DataTable dt, string name, int columnCount, int rowCount, int parentRelationCount, int childRelationCount)
+               public void AssertDataTable (string label, DataTable dt, string name, int columnCount, int rowCount, int parentRelationCount, int childRelationCount, int constraintCount, int primaryKeyLength)
                {
                        AssertEquals (label + ".TableName", name, dt.TableName);
                        AssertEquals (label + ".ColumnCount", columnCount, dt.Columns.Count);
                        AssertEquals (label + ".RowCount", rowCount, dt.Rows.Count);
                        AssertEquals (label + ".ParentRelCount", parentRelationCount, dt.ParentRelations.Count);
                        AssertEquals (label + ".ChildRelCount", childRelationCount, dt.ChildRelations.Count);
+                       AssertEquals (label + ".ConstraintCount", constraintCount, dt.Constraints.Count);
+                       AssertEquals (label + ".PrimaryKeyLength", primaryKeyLength, dt.PrimaryKey.Length);
                }
 
                public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount)
                {
-                       AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, ReadState.EndOfFile);
+                       AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, ReadState.EndOfFile, null, null);
                }
 
-               // a bit detailed version
                public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount, ReadState state)
+               {
+                       AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, state, null, null);
+               }
+
+               // a bit detailed version
+               public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount, ReadState state, string readerLocalName, string readerNS)
                {
                        XmlReader xtr = new XmlTextReader (xml, XmlNodeType.Element, null);
                        AssertEquals (label + ".return", resultMode, ds.ReadXml (xtr, readMode));
                        AssertDataSet (label + ".dataset", ds, datasetName, tableCount, -1);
                        AssertEquals (label + ".readstate", state, xtr.ReadState);
+                       if (readerLocalName != null)
+                               AssertEquals (label + ".reader-localName",
+                                       readerLocalName, xtr.LocalName);
+                       if (readerNS != null)
+                               AssertEquals (label + ".reader-ns", readerNS, xtr.NamespaceURI);
                }
 
                public void AssertDataRelation (string label, DataRelation rel, string name, bool nested,
@@ -118,7 +206,8 @@ namespace MonoTests.System.Data
                        AssertEquals (label + "Expression: " , expression, col.Expression);\r
                        AssertEquals (label + "MaxLength: " , maxLength, col.MaxLength);\r
                        AssertEquals (label + "Namespace: " , ns, col.Namespace);\r
-                       AssertEquals (label + "Ordinal: " , ordinal, col.Ordinal);\r
+                       if (ordinal >= 0)\r
+                               AssertEquals (label + "Ordinal: " , ordinal, col.Ordinal);\r
                        AssertEquals (label + "Prefix: " , prefix, col.Prefix);\r
                        AssertEquals (label + "ReadOnly: " , readOnly, col.ReadOnly);\r
                        AssertEquals (label + "Unique: " , unique, col.Unique);\r