Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Data / Test / System.Data / DataSetAssertion.cs
1 //
2 // DataSetAssertion.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Data;
36 using System.Text;
37 using System.Xml;
38 using System.Xml.Schema;
39 using NUnit.Framework;
40
41 namespace MonoTests.System.Data
42 {
43         public class DataSetAssertion
44         {
45                 public string GetNormalizedSchema (string source)
46                 {
47 /*
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 ();
55                         xs.Write (writer);
56                         return writer.ToString ();
57 */
58                         XmlDocument doc = new XmlDocument ();
59                         doc.LoadXml (source);
60                         SortAttributes (doc.DocumentElement);
61                         StringWriter writer = new StringWriter ();
62                         doc.Save (writer);
63                         return writer.ToString ();
64                 }
65
66                 private void SortAttributes (XmlElement el)
67                 {
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)
74                                         al.Add (n);
75                         }
76                         foreach (XmlNode n in al)
77                                 el.RemoveChild (n);
78                 }
79
80                 private void SortAttributesAttributes (XmlElement el)
81                 {
82                         ArrayList al = new ArrayList ();
83                         foreach (XmlAttribute a in el.Attributes)
84                                 al.Add (a.Name);
85                         al.Sort ();
86                         string [] names = (string []) al.ToArray (typeof (string));
87                         al.Clear ();
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);
95                 }
96
97                 public void AssertDataSet (string label, DataSet ds, string name, int tableCount, int relCount)
98                 {
99                         Assert.AreEqual (name, ds.DataSetName, label + ".DataSetName");
100                         Assert.AreEqual (tableCount, ds.Tables.Count, label + ".TableCount");
101                         if (relCount >= 0)
102                                 Assert.AreEqual (relCount, ds.Relations.Count, label + ".RelationCount");
103                 }
104
105                 public void AssertDataTable (string label, DataTable dt, string name, int columnCount, int rowCount, int parentRelationCount, int childRelationCount, int constraintCount, int primaryKeyLength)
106                 {
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");
114                 }
115
116                 public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount)
117                 {
118                         AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, ReadState.EndOfFile, null, null);
119                 }
120
121                 public void AssertReadXml (DataSet ds, string label, string xml, XmlReadMode readMode, XmlReadMode resultMode, string datasetName, int tableCount, ReadState state)
122                 {
123                         AssertReadXml (ds, label, xml, readMode, resultMode, datasetName, tableCount, state, null, null);
124                 }
125
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)
128                 {
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");
137                 }
138
139                 public void AssertDataRelation (string label, DataRelation rel, string name, bool nested,
140                         string [] parentColNames, string [] childColNames,
141                         bool existsUK, bool existsFK)
142                 {
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");
151                         if (existsUK)
152                                 Assert.IsNotNull (rel.ParentKeyConstraint, label + ".uniqKeyExists");
153                         else
154                                 Assert.IsNull (rel.ParentKeyConstraint, label + ".uniqKeyNotExists");
155                         if (existsFK)
156                                 Assert.IsNotNull (rel.ChildKeyConstraint, label + ".fkExists");
157                         else
158                                 Assert.IsNull (rel.ChildKeyConstraint, label + ".fkNotExists");
159                 }
160
161                 public void AssertUniqueConstraint (string label, UniqueConstraint uc, 
162                         string name, bool isPrimaryKey, string [] colNames)
163                 {
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");
169                 }
170
171                 public void AssertForeignKeyConstraint (string label,
172                         ForeignKeyConstraint fk, string name, 
173                         AcceptRejectRule acceptRejectRule, Rule delRule, Rule updateRule,
174                         string [] colNames, string [] relColNames)
175                 {
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");
186                 }
187
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)
195                 {
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: " );
208                         if (ordinal >= 0)
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: " );
213                 }
214         }
215 }
216