5 // Atsushi Enomoto <atsushi@ximian.com>
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
38 using System.Xml.Schema;
39 using NUnit.Framework;
41 namespace MonoTests.System.Data
43 public class DataSetAssertion : Assertion
45 public string GetNormalizedSchema (string source)
48 // Due to the implementation difference, we must have
49 // one more step to reorder attributes. Here, read
50 // schema document into XmlSchema once, and compare
51 // output string with those emission from Write().
52 XmlSchema xs = XmlSchema.Read (new XmlTextReader (
53 new StringReader (source)), null);
54 StringWriter writer = new StringWriter ();
56 return writer.ToString ();
58 XmlDocument doc = new XmlDocument ();
60 SortAttributes (doc.DocumentElement);
61 StringWriter writer = new StringWriter ();
63 return writer.ToString ();
66 private void SortAttributes (XmlElement el)
68 SortAttributesAttributes (el);
69 ArrayList al = new ArrayList ();
70 foreach (XmlNode n in el.ChildNodes) {
71 if (n.NodeType == XmlNodeType.Element)
72 SortAttributes (n as XmlElement);
73 if (n.NodeType == XmlNodeType.Comment)
76 foreach (XmlNode n in al)
80 private void SortAttributesAttributes (XmlElement el)
82 ArrayList al = new ArrayList ();
83 foreach (XmlAttribute a in el.Attributes)
86 string [] names = (string []) al.ToArray (typeof (string));
88 foreach (string name in names)
89 al.Add (el.RemoveAttributeNode (
90 el.GetAttributeNode (name)));
91 foreach (XmlAttribute a in al)
92 // Exclude xmlns="" here.
93 if (a.Name != "xmlns" || a.Value != String.Empty)
94 el.SetAttributeNode (a);
97 public void AssertDataSet (string label, DataSet ds, string name, int tableCount, int relCount)
99 AssertEquals (label + ".DataSetName", name, ds.DataSetName);
100 AssertEquals (label + ".TableCount", tableCount, ds.Tables.Count);
102 AssertEquals (label + ".RelationCount", relCount, ds.Relations.Count);
105 public void AssertDataTable (string label, DataTable dt, string name, int columnCount, int rowCount, int parentRelationCount, int childRelationCount, int constraintCount, int primaryKeyLength)
107 AssertEquals (label + ".TableName", name, dt.TableName);
108 AssertEquals (label + ".ColumnCount", columnCount, dt.Columns.Count);
109 AssertEquals (label + ".RowCount", rowCount, dt.Rows.Count);
110 AssertEquals (label + ".ParentRelCount", parentRelationCount, dt.ParentRelations.Count);
111 AssertEquals (label + ".ChildRelCount", childRelationCount, dt.ChildRelations.Count);
112 AssertEquals (label + ".ConstraintCount", constraintCount, dt.Constraints.Count);
113 AssertEquals (label + ".PrimaryKeyLength", primaryKeyLength, dt.PrimaryKey.Length);
116 public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount)
118 AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, ReadState.EndOfFile, null, null);
121 public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount, ReadState state)
123 AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, state, null, null);
126 // a bit detailed version
127 public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount, ReadState state, string readerLocalName, string readerNS)
129 XmlReader xtr = new XmlTextReader (xml, XmlNodeType.Element, null);
130 AssertEquals (label + ".return", resultMode, ds.ReadXml (xtr, readMode));
131 AssertDataSet (label + ".dataset", ds, datasetName, tableCount, -1);
132 AssertEquals (label + ".readstate", state, xtr.ReadState);
133 if (readerLocalName != null)
134 AssertEquals (label + ".reader-localName",
135 readerLocalName, xtr.LocalName);
136 if (readerNS != null)
137 AssertEquals (label + ".reader-ns", readerNS, xtr.NamespaceURI);
140 public void AssertDataRelation (string label, DataRelation rel, string name, bool nested,
141 string [] parentColNames, string [] childColNames,
142 bool existsUK, bool existsFK)
144 AssertEquals (label + ".Name", name, rel.RelationName);
145 AssertEquals (label + ".Nested", nested, rel.Nested);
146 for (int i = 0; i < parentColNames.Length; i++)
147 AssertEquals (label + ".parentColumn_" + i, parentColNames [i], rel.ParentColumns [i].ColumnName);
148 AssertEquals (label + ".ParentColCount", parentColNames.Length, rel.ParentColumns.Length);
149 for (int i = 0; i < childColNames.Length; i++)
150 AssertEquals (label + ".childColumn_" + i, childColNames [i], rel.ChildColumns [i].ColumnName);
151 AssertEquals (label + ".ChildColCount", childColNames.Length, rel.ChildColumns.Length);
153 AssertNotNull (label + ".uniqKeyExists", rel.ParentKeyConstraint);
155 AssertNull (label + ".uniqKeyNotExists", rel.ParentKeyConstraint);
157 AssertNotNull (label + ".fkExists", rel.ChildKeyConstraint);
159 AssertNull (label + ".fkNotExists", rel.ChildKeyConstraint);
162 public void AssertUniqueConstraint (string label, UniqueConstraint uc,
163 string name, bool isPrimaryKey, string [] colNames)
165 AssertEquals (label + ".name", name, uc.ConstraintName);
166 AssertEquals (label + ".pkey", isPrimaryKey, uc.IsPrimaryKey);
167 for (int i = 0; i < colNames.Length; i++)
168 AssertEquals (label + ".column_" + i, colNames [i], uc.Columns [i].ColumnName);
169 AssertEquals (label + ".colCount", colNames.Length, uc.Columns.Length);
172 public void AssertForeignKeyConstraint (string label,
173 ForeignKeyConstraint fk, string name,
174 AcceptRejectRule acceptRejectRule, Rule delRule, Rule updateRule,
175 string [] colNames, string [] relColNames)
177 AssertEquals (label + ".name", name, fk.ConstraintName);
178 AssertEquals (label + ".acceptRejectRule", acceptRejectRule, fk.AcceptRejectRule);
179 AssertEquals (label + ".delRule", delRule, fk.DeleteRule);
180 AssertEquals (label + ".updateRule", updateRule, fk.UpdateRule);
181 for (int i = 0; i < colNames.Length; i++)
182 AssertEquals (label + ".column_" + i, colNames [i], fk.Columns [i].ColumnName);
183 AssertEquals (label + ".colCount", colNames.Length, fk.Columns.Length);
184 for (int i = 0; i < relColNames.Length; i++)
185 AssertEquals (label + ".relatedColumn_" + i, relColNames [i], fk.RelatedColumns [i].ColumnName);
186 AssertEquals (label + ".relatedColCount", relColNames.Length, fk.RelatedColumns.Length);
189 public void AssertDataColumn (string label, DataColumn col,
\r
190 string colName, bool allowDBNull,
\r
191 bool autoIncr, int autoIncrSeed, int autoIncrStep,
\r
192 string caption, MappingType colMap,
\r
193 Type type, object defaultValue, string expression,
\r
194 int maxLength, string ns, int ordinal, string prefix,
\r
195 bool readOnly, bool unique)
\r
197 AssertEquals (label + "ColumnName: " , colName, col.ColumnName);
\r
198 AssertEquals (label + "AllowDBNull? " , allowDBNull, col.AllowDBNull);
\r
199 AssertEquals (label + "AutoIncrement? " , autoIncr, col.AutoIncrement);
\r
200 AssertEquals (label + " Seed: " , autoIncrSeed, col.AutoIncrementSeed);
\r
201 AssertEquals (label + " Step: " , autoIncrStep, col.AutoIncrementStep);
\r
202 AssertEquals (label + "Caption " , caption, col.Caption);
\r
203 AssertEquals (label + "Mapping: " , colMap, col.ColumnMapping);
\r
204 AssertEquals (label + "Type: " , type, col.DataType);
\r
205 AssertEquals (label + "DefaultValue: " , defaultValue, col.DefaultValue);
\r
206 AssertEquals (label + "Expression: " , expression, col.Expression);
\r
207 AssertEquals (label + "MaxLength: " , maxLength, col.MaxLength);
\r
208 AssertEquals (label + "Namespace: " , ns, col.Namespace);
\r
210 AssertEquals (label + "Ordinal: " , ordinal, col.Ordinal);
\r
211 AssertEquals (label + "Prefix: " , prefix, col.Prefix);
\r
212 AssertEquals (label + "ReadOnly: " , readOnly, col.ReadOnly);
\r
213 AssertEquals (label + "Unique: " , unique, col.Unique);
\r