2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 : Assertion
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                         AssertEquals (label + ".DataSetName", name, ds.DataSetName);
100                         AssertEquals (label + ".TableCount", tableCount, ds.Tables.Count);
101                         if (relCount >= 0)
102                                 AssertEquals (label + ".RelationCount", relCount, ds.Relations.Count);
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                         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);
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                         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);
138                 }
139
140                 public void AssertDataRelation (string label, DataRelation rel, string name, bool nested,
141                         string [] parentColNames, string [] childColNames,
142                         bool existsUK, bool existsFK)
143                 {
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);
152                         if (existsUK)
153                                 AssertNotNull (label + ".uniqKeyExists", rel.ParentKeyConstraint);
154                         else
155                                 AssertNull (label + ".uniqKeyNotExists", rel.ParentKeyConstraint);
156                         if (existsFK)
157                                 AssertNotNull (label + ".fkExists", rel.ChildKeyConstraint);
158                         else
159                                 AssertNull (label + ".fkNotExists", rel.ChildKeyConstraint);
160                 }
161
162                 public void AssertUniqueConstraint (string label, UniqueConstraint uc, 
163                         string name, bool isPrimaryKey, string [] colNames)
164                 {
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);
170                 }
171
172                 public void AssertForeignKeyConstraint (string label,
173                         ForeignKeyConstraint fk, string name, 
174                         AcceptRejectRule acceptRejectRule, Rule delRule, Rule updateRule,
175                         string [] colNames, string [] relColNames)
176                 {
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);
187                 }
188 \r
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
196                 {\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
209                         if (ordinal >= 0)\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
214                 }\r
215         }
216 }
217