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
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 Assert.AreEqual (name, ds.DataSetName, label + ".DataSetName");
100 Assert.AreEqual (tableCount, ds.Tables.Count, label + ".TableCount");
102 Assert.AreEqual (relCount, ds.Relations.Count, label + ".RelationCount");
105 public void AssertDataTable (string label, DataTable dt, string name, int columnCount, int rowCount, int parentRelationCount, int childRelationCount, int constraintCount, int primaryKeyLength)
107 Assert.AreEqual (name, dt.TableName, label + ".TableName");
108 Assert.AreEqual (columnCount, dt.Columns.Count, label + ".ColumnCount");
109 Assert.AreEqual (rowCount, dt.Rows.Count, label + ".RowCount");
110 Assert.AreEqual (parentRelationCount, dt.ParentRelations.Count, label + ".ParentRelCount");
111 Assert.AreEqual (childRelationCount, dt.ChildRelations.Count, label + ".ChildRelCount");
112 Assert.AreEqual (constraintCount, dt.Constraints.Count, label + ".ConstraintCount");
113 Assert.AreEqual (primaryKeyLength, dt.PrimaryKey.Length, label + ".PrimaryKeyLength");
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 Assert.AreEqual (resultMode, ds.ReadXml (xtr, readMode), label + ".return");
131 AssertDataSet (label + ".dataset", ds, datasetName, tableCount, -1);
132 Assert.AreEqual (state, xtr.ReadState, label + ".readstate");
133 if (readerLocalName != null)
134 Assert.AreEqual (readerLocalName, xtr.LocalName, label + ".reader-localName");
135 if (readerNS != null)
136 Assert.AreEqual (readerNS, xtr.NamespaceURI, label + ".reader-ns");
139 public void AssertDataRelation (string label, DataRelation rel, string name, bool nested,
140 string [] parentColNames, string [] childColNames,
141 bool existsUK, bool existsFK)
143 Assert.AreEqual (name, rel.RelationName, label + ".Name");
144 Assert.AreEqual (nested, rel.Nested, label + ".Nested");
145 for (int i = 0; i < parentColNames.Length; i++)
146 Assert.AreEqual (parentColNames [i], rel.ParentColumns [i].ColumnName, label + ".parentColumn_" + i);
147 Assert.AreEqual (parentColNames.Length, rel.ParentColumns.Length, label + ".ParentColCount");
148 for (int i = 0; i < childColNames.Length; i++)
149 Assert.AreEqual (childColNames [i], rel.ChildColumns [i].ColumnName, label + ".childColumn_" + i);
150 Assert.AreEqual (childColNames.Length, rel.ChildColumns.Length, label + ".ChildColCount");
152 Assert.IsNotNull (rel.ParentKeyConstraint, label + ".uniqKeyExists");
154 Assert.IsNull (rel.ParentKeyConstraint, label + ".uniqKeyNotExists");
156 Assert.IsNotNull (rel.ChildKeyConstraint, label + ".fkExists");
158 Assert.IsNull (rel.ChildKeyConstraint, label + ".fkNotExists");
161 public void AssertUniqueConstraint (string label, UniqueConstraint uc,
162 string name, bool isPrimaryKey, string [] colNames)
164 Assert.AreEqual (name, uc.ConstraintName, label + ".name");
165 Assert.AreEqual (isPrimaryKey, uc.IsPrimaryKey, label + ".pkey");
166 for (int i = 0; i < colNames.Length; i++)
167 Assert.AreEqual (colNames [i], uc.Columns [i].ColumnName, label + ".column_" + i);
168 Assert.AreEqual (colNames.Length, uc.Columns.Length, label + ".colCount");
171 public void AssertForeignKeyConstraint (string label,
172 ForeignKeyConstraint fk, string name,
173 AcceptRejectRule acceptRejectRule, Rule delRule, Rule updateRule,
174 string [] colNames, string [] relColNames)
176 Assert.AreEqual (name, fk.ConstraintName, label + ".name");
177 Assert.AreEqual (acceptRejectRule, fk.AcceptRejectRule, label + ".acceptRejectRule");
178 Assert.AreEqual (delRule, fk.DeleteRule, label + ".delRule");
179 Assert.AreEqual (updateRule, fk.UpdateRule, label + ".updateRule");
180 for (int i = 0; i < colNames.Length; i++)
181 Assert.AreEqual (colNames [i], fk.Columns [i].ColumnName, label + ".column_" + i);
182 Assert.AreEqual (colNames.Length, fk.Columns.Length, label + ".colCount");
183 for (int i = 0; i < relColNames.Length; i++)
184 Assert.AreEqual (relColNames [i], fk.RelatedColumns [i].ColumnName, label + ".relatedColumn_" + i);
185 Assert.AreEqual (relColNames.Length, fk.RelatedColumns.Length, label + ".relatedColCount");
188 public void AssertDataColumn (string label, DataColumn col,
189 string colName, bool allowDBNull,
190 bool autoIncr, int autoIncrSeed, int autoIncrStep,
191 string caption, MappingType colMap,
192 Type type, object defaultValue, string expression,
193 int maxLength, string ns, int ordinal, string prefix,
194 bool readOnly, bool unique)
196 Assert.AreEqual (colName, col.ColumnName, label + "ColumnName: " );
197 Assert.AreEqual (allowDBNull, col.AllowDBNull, label + "AllowDBNull? " );
198 Assert.AreEqual (autoIncr, col.AutoIncrement, label + "AutoIncrement? " );
199 Assert.AreEqual (autoIncrSeed, col.AutoIncrementSeed, label + " Seed: " );
200 Assert.AreEqual (autoIncrStep, col.AutoIncrementStep, label + " Step: " );
201 Assert.AreEqual (caption, col.Caption, label + "Caption " );
202 Assert.AreEqual (colMap, col.ColumnMapping, label + "Mapping: " );
203 Assert.AreEqual (type, col.DataType, label + "Type: " );
204 Assert.AreEqual (defaultValue, col.DefaultValue, label + "DefaultValue: " );
205 Assert.AreEqual (expression, col.Expression, label + "Expression: " );
206 Assert.AreEqual (maxLength, col.MaxLength, label + "MaxLength: " );
207 Assert.AreEqual (ns, col.Namespace, label + "Namespace: " );
209 Assert.AreEqual (ordinal, col.Ordinal, label + "Ordinal: " );
210 Assert.AreEqual (prefix, col.Prefix, label + "Prefix: " );
211 Assert.AreEqual (readOnly, col.ReadOnly, label + "ReadOnly: " );
212 Assert.AreEqual (unique, col.Unique, label + "Unique: " );