[runtime] Updates comments.
[mono.git] / mcs / class / System.Data / Test / System.Data / DataSetInferXmlSchemaTest.cs
1 //
2 // DataSetInferXmlSchemaTest.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.Data;
35 using System.IO;
36 using System.Xml;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Data
40 {
41         [TestFixture]
42         public class DataSetInferXmlSchemaTest : DataSetAssertion
43         {
44                 string xml1 = "<root/>";
45                 string xml2 = "<root attr='value' />";
46                 string xml3 = "<root attr='value' attr2='2' />";
47                 string xml4 = "<root>simple.txt</root>";
48                 string xml5 = "<root><child/></root>";
49                 string xml6 = "<root><col1>sample</col1></root>";
50                 string xml7 = @"<root>
51 <col1>column 1 test</col1>
52 <col2>column2test</col2>
53 <col3>3get</col3>
54 </root>";
55                 string xml8 = @"<set>
56 <tab>
57 <col1>1</col1>
58 <col2>1</col2>
59 <col3>1</col3>
60 </tab>
61 </set>";
62                 string xml9 = @"<el1 attr1='val1' attrA='valA'>
63   <el2 attr2='val2' attrB='valB'>
64     <el3 attr3='val3' attrC='valC'>3</el3>
65       <column2>1</column2>
66       <column3>1</column3>
67     <el4 attr4='val4' attrD='valD'>4</el4>
68   </el2>
69 </el1>";
70                 // mixed content
71                 string xml10 = "<root>Here is a <b>mixed</b> content.</root>";
72                 // xml:space support
73                 string xml11 = @"<root xml:space='preserve'>
74    <child_after_significant_space />
75 </root>";
76                 // This is useless ... since xml:space becomes a DataColumn here.
77 //              string xml12 = "<root xml:space='preserve'>     </root>";
78                 // The result is silly under MS.NET. It never ignores comment, so
79                 // They differ:
80                 //   1) <root>simple string.</root>
81                 //   2) <root>simple <!-- comment -->string.</root>
82                 // The same applies to PI.
83 //              string xml13 = "<root><tab><col>test <!-- out --> comment</col></tab></root>";
84
85                 // simple namespace/prefix support
86                 string xml14 = "<p:root xmlns:p='urn:foo'>test string</p:root>";
87                 // two tables that have the same content type.
88                 string xml15 = @"<root>
89 <table1>
90         <col1_1>test1</col1_1>
91         <col1_2>test2</col1_2>
92 </table1>
93 <table2>
94         <col2_1>test1</col2_1>
95         <col2_2>test2</col2_2>
96 </table2>
97 </root>";
98                 // foo cannot be both table chikd and root child
99                 string xml16 = @"<root>
100 <table>
101         <foo>
102                 <tableFooChild1>1</tableFooChild1>
103                 <tableFooChild2>2</tableFooChild2>
104         </foo>
105         <bar />
106 </table>
107 <foo></foo>
108 <bar />
109 </root>";
110                 // simple namespace support
111                 string xml17 = @"<root xmlns='urn:foo' />";
112                 // conflict between simple and complex type element col
113                 string xml18 = @"<set>
114 <table>
115 <col>
116         <another_col />
117 </col>
118 <col>simple text here.</col>
119 </table>
120 </set>";
121                 // variant of xml18: complex column appeared latter
122                 string xml19 =@"<set>
123 <table>
124 <col>simple text</col><!-- ignored -->
125 <col>
126         <another_col />
127 </col>
128 </table>
129 </set>";
130                 // conflict check (actually it is not conflict) on two "col" tables
131                 string xml20 = @"<set>
132 <table>
133 <col>
134         <another_col />
135 </col>
136 <col attr='value' />
137 </table>
138 </set>";
139                 // conflict between the attribute and the child element
140                 string xml21 = @"<set>
141 <table>
142 <col data='value'>
143         <data />
144 </col>
145 </table>
146 </set>";
147                 // simple nest
148                 string xml22 = "<set><table><col><descendant/></col></table><table2><col2>v2</col2></table2></set>";
149           /*
150                 // simple diffgram
151                 string xml23 = @"<set>
152   <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
153     <xs:element name='table'>
154       <xs:complexType>
155       <xs:choice>
156         <xs:any />
157       </xs:choice>
158       </xs:complexType>
159     </xs:element>
160   </xs:schema>
161   <diffgr:diffgram
162         xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'
163         xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'>
164     <table>
165       <col>1</col>
166     </table>
167   </diffgr:diffgram>
168 </set>";
169                 // just deep table
170                 string xml24 = "<p1><p2><p3><p4><p5><p6/></p5></p4></p3></p2></p1>";
171           */
172
173                 private DataSet GetDataSet (string xml, string [] nss)
174                 {
175                         DataSet ds = new DataSet ();
176                         ds.InferXmlSchema (new XmlTextReader (xml, XmlNodeType.Document, null), nss);
177                         return ds;
178                 }
179
180                 [Test]
181                 public void NullFileName ()
182                 {
183                         DataSet ds = new DataSet ();
184                         ds.InferXmlSchema ((XmlReader) null, null);
185                         AssertDataSet ("null", ds, "NewDataSet", 0, 0);
186                 }
187
188                 [Test]
189                 public void SingleElement ()
190                 {
191                         DataSet ds = GetDataSet (xml1, null);
192                         AssertDataSet ("xml1", ds, "root", 0, 0);
193
194                         ds = GetDataSet (xml4, null);
195                         AssertDataSet ("xml4", ds, "root", 0, 0);
196
197                         // namespaces
198                         ds = GetDataSet (xml14, null);
199                         AssertDataSet ("xml14", ds, "root", 0, 0);
200                         Assert.AreEqual ("p", ds.Prefix);
201                         Assert.AreEqual ("urn:foo", ds.Namespace);
202
203                         ds = GetDataSet (xml17, null);
204                         AssertDataSet ("xml17", ds, "root", 0, 0);
205                         Assert.AreEqual ("urn:foo", ds.Namespace);
206                 }
207
208                 [Test]
209                 public void SingleElementWithAttribute ()
210                 {
211                         DataSet ds = GetDataSet (xml2, null);
212                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
213                         DataTable dt = ds.Tables [0];
214                         AssertDataTable ("dt", dt, "root", 1, 0, 0, 0, 0, 0);
215                         AssertDataColumn ("col", dt.Columns [0], "attr", true, false, 0, 1, "attr", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
216                 }
217
218                 [Test]
219                 public void SingleElementWithTwoAttribute ()
220                 {
221                         DataSet ds = GetDataSet (xml3, null);
222                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
223                         DataTable dt = ds.Tables [0];
224                         AssertDataTable ("dt", dt, "root", 2, 0, 0, 0, 0, 0);
225                         AssertDataColumn ("col", dt.Columns [0], "attr", true, false, 0, 1, "attr", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
226                         AssertDataColumn ("col", dt.Columns [1], "attr2", true, false, 0, 1, "attr2", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
227                 }
228
229                 [Test]
230                 public void SingleChild ()
231                 {
232                         DataSet ds = GetDataSet (xml5, null);
233                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
234                         DataTable dt = ds.Tables [0];
235                         AssertDataTable ("dt", dt, "root", 1, 0, 0, 0, 0, 0);
236                         AssertDataColumn ("col", dt.Columns [0], "child", true, false, 0, 1, "child", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
237
238                         ds = GetDataSet (xml6, null);
239                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
240                         dt = ds.Tables [0];
241                         AssertDataTable ("dt", dt, "root", 1, 0, 0, 0, 0, 0);
242                         AssertDataColumn ("col", dt.Columns [0], "col1", true, false, 0, 1, "col1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
243                 }
244
245                 [Test]
246                 public void SimpleElementTable ()
247                 {
248                         DataSet ds = GetDataSet (xml7, null);
249                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
250                         DataTable dt = ds.Tables [0];
251                         AssertDataTable ("dt", dt, "root", 3, 0, 0, 0, 0, 0);
252                         AssertDataColumn ("col", dt.Columns [0], "col1", true, false, 0, 1, "col1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
253                         AssertDataColumn ("col2", dt.Columns [1], "col2", true, false, 0, 1, "col2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
254                         AssertDataColumn ("col3", dt.Columns [2], "col3", true, false, 0, 1, "col3", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
255                 }
256
257                 [Test]
258                 public void SimpleDataSet ()
259                 {
260                         DataSet ds = GetDataSet (xml8, null);
261                         AssertDataSet ("ds", ds, "set", 1, 0);
262                         DataTable dt = ds.Tables [0];
263                         AssertDataTable ("dt", dt, "tab", 3, 0, 0, 0, 0, 0);
264                         AssertDataColumn ("col", dt.Columns [0], "col1", true, false, 0, 1, "col1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
265                         AssertDataColumn ("col2", dt.Columns [1], "col2", true, false, 0, 1, "col2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
266                         AssertDataColumn ("col3", dt.Columns [2], "col3", true, false, 0, 1, "col3", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
267                 }
268
269                 [Test]
270                 public void ComplexElementAttributeTable1 ()
271                 {
272                         // FIXME: Also test ReadXml (, XmlReadMode.InferSchema) and
273                         // make sure that ReadXml() stores DataRow to el1 (and maybe to others)
274                         DataSet ds = GetDataSet (xml9, null);
275                         AssertDataSet ("ds", ds, "NewDataSet", 4, 3);
276                         DataTable dt = ds.Tables [0];
277
278                         AssertDataTable ("dt1", dt, "el1", 3, 0, 0, 1, 1, 1);
279                         AssertDataColumn ("el1_Id", dt.Columns [0], "el1_Id", false, true, 0, 1, "el1_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
280                         AssertDataColumn ("el1_attr1", dt.Columns [1], "attr1", true, false, 0, 1, "attr1", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
281                         AssertDataColumn ("el1_attrA", dt.Columns [2], "attrA", true, false, 0, 1, "attrA", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
282
283                         dt = ds.Tables [1];
284                         AssertDataTable ("dt2", dt, "el2", 6, 0, 1, 2, 2, 1);
285                         AssertDataColumn ("el2_Id", dt.Columns [0], "el2_Id", false, true, 0, 1, "el2_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
286                         AssertDataColumn ("el2_col2", dt.Columns [1], "column2", true, false, 0, 1, "column2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
287                         AssertDataColumn ("el2_col3", dt.Columns [2], "column3", true, false, 0, 1, "column3", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
288                         AssertDataColumn ("el2_attr2", dt.Columns [3], "attr2", true, false, 0, 1, "attr2", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 3, String.Empty, false, false);
289                         AssertDataColumn ("el2_attrB", dt.Columns [4], "attrB", true, false, 0, 1, "attrB", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 4, String.Empty, false, false);
290                         AssertDataColumn ("el2_el1Id", dt.Columns [5], "el1_Id", true, false, 0, 1, "el1_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 5, String.Empty, false, false);
291
292                         dt = ds.Tables [2];
293                         AssertDataTable ("dt3", dt, "el3", 4, 0, 1, 0, 1, 0);
294                         AssertDataColumn ("el3_attr3", dt.Columns [0], "attr3", true, false, 0, 1, "attr3", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
295                         AssertDataColumn ("el3_attrC", dt.Columns [1], "attrC", true, false, 0, 1, "attrC", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
296                         AssertDataColumn ("el3_Text", dt.Columns [2], "el3_Text", true, false, 0, 1, "el3_Text", MappingType.SimpleContent, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
297                         AssertDataColumn ("el3_el2Id", dt.Columns [3], "el2_Id", true, false, 0, 1, "el2_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 3, String.Empty, false, false);
298
299                         dt = ds.Tables [3];
300                         AssertDataTable ("dt4", dt, "el4", 4, 0, 1, 0, 1, 0);
301                         AssertDataColumn ("el3_attr4", dt.Columns [0], "attr4", true, false, 0, 1, "attr4", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
302                         AssertDataColumn ("el4_attrD", dt.Columns [1], "attrD", true, false, 0, 1, "attrD", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
303                         AssertDataColumn ("el4_Text", dt.Columns [2], "el4_Text", true, false, 0, 1, "el4_Text", MappingType.SimpleContent, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
304                         AssertDataColumn ("el4_el2Id", dt.Columns [3], "el2_Id", true, false, 0, 1, "el2_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 3, String.Empty, false, false);
305                 }
306
307                 [Test]
308                 public void MixedContent ()
309                 {
310                         // Note that text part is ignored.
311
312                         DataSet ds = GetDataSet (xml10, null);
313                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
314                         DataTable dt = ds.Tables [0];
315                         AssertDataTable ("dt", dt, "root", 1, 0, 0, 0, 0, 0);
316                         AssertDataColumn ("col", dt.Columns [0], "b", true, false, 0, 1, "b", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
317                 }
318
319                 [Test]
320                 public void SignificantWhitespaceIgnored ()
321                 {
322                         // Note that 1) significant whitespace is ignored, and 
323                         // 2) xml:space is treated as column (and also note namespaces).
324                         DataSet ds = GetDataSet (xml11, null);
325                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
326                         DataTable dt = ds.Tables [0];
327                         AssertDataTable ("dt", dt, "root", 1, 0, 0, 0, 0, 0);
328                         AssertDataColumn ("element", dt.Columns [0], "child_after_significant_space", true, false, 0, 1, "child_after_significant_space", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
329                         Assert.AreEqual (1, dt.Columns.Count, "xml:space is not treated as column");
330                 }
331
332                 [Test]
333                 public void SignificantWhitespaceIgnored2 ()
334                 {
335                         // To make sure, create pure significant whitespace element
336                         // using XmlNodeReader (that does not have xml:space attribute
337                         // column).
338                         DataSet ds = new DataSet ();
339                         XmlDocument doc = new XmlDocument ();
340                         doc.AppendChild (doc.CreateElement ("root"));
341                         doc.DocumentElement.AppendChild (doc.CreateSignificantWhitespace
342                         ("      \n\n"));
343                         XmlReader xr = new XmlNodeReader (doc);
344                         ds.InferXmlSchema (xr, null);
345                         AssertDataSet ("pure_whitespace", ds, "root", 0, 0);
346                 }
347
348                 [Test]
349                 public void TwoElementTable ()
350                 {
351                         // FIXME: Also test ReadXml (, XmlReadMode.InferSchema) and
352                         // make sure that ReadXml() stores DataRow to el1 (and maybe to others)
353                         DataSet ds = GetDataSet (xml15, null);
354                         AssertDataSet ("ds", ds, "root", 2, 0);
355
356                         DataTable dt = ds.Tables [0];
357                         AssertDataTable ("dt", dt, "table1", 2, 0, 0, 0, 0, 0);
358                         AssertDataColumn ("col1_1", dt.Columns [0], "col1_1", true, false, 0, 1, "col1_1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
359                         AssertDataColumn ("col1_2", dt.Columns [1], "col1_2", true, false, 0, 1, "col1_2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
360
361                         dt = ds.Tables [1];
362                         AssertDataTable ("dt", dt, "table2", 2, 0, 0, 0, 0, 0);
363                         AssertDataColumn ("col2_1", dt.Columns [0], "col2_1", true, false, 0, 1, "col2_1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
364                         AssertDataColumn ("col2_2", dt.Columns [1], "col2_2", true, false, 0, 1, "col2_2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
365                 }
366
367                 [Test]
368                 [Category ("NotWorking")]
369                 [ExpectedException (typeof (ArgumentException))]
370                 // The same table cannot be the child table in two nested relations.
371                 public void ComplexElementTable1 ()
372                 {
373                         // TODO: Also test ReadXml (, XmlReadMode.InferSchema) and
374                         // make sure that ReadXml() stores DataRow to el1 (and maybe to others)
375                         DataSet ds = GetDataSet (xml16, null);
376 /*
377                         AssertDataSet ("ds", ds, "NewDataSet", 4, 0);
378
379                         DataTable dt = ds.Tables [0];
380                         AssertDataTable ("dt", dt, "root", 2, 0);
381                         AssertDataColumn ("table#1_id", dt.Columns [0], "root_Id", false, true, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
382                         AssertDataColumn ("table#1_bar", dt.Columns [1], "bar", true, false, 0, 1, "bar", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
383
384                         dt = ds.Tables [1];
385                         AssertDataTable ("dt2", dt, "table", 3, 0);
386                         AssertDataColumn ("table#2_id", dt.Columns [0], "table_Id", false, true, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
387                         AssertDataColumn ("table#2_bar", dt.Columns [1], "bar", true, false, 0, 1, "bar", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
388                         AssertDataColumn ("table#2_refid", dt.Columns [0], "root_Id", true, false, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
389
390                         dt = ds.Tables [2];
391                         AssertDataTable ("dt3", dt, "foo", 3, 0);
392                         AssertDataColumn ("table#3_col1", dt.Columns [0], "tableFooChild1", true, false, 0, 1, "tableFooChild1", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
393                         AssertDataColumn ("table#3_col2", dt.Columns [0], "tableFooChild2", true, false, 0, 1, "tableFooChild2", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
394                         AssertDataColumn ("table#3_refid", dt.Columns [0], "table_Id", true, false, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
395 */
396                 }
397
398                 [Test]
399                 public void ConflictSimpleComplexColumns ()
400                 {
401                         DataSet ds = GetDataSet (xml18, null);
402                         AssertDataSet ("ds", ds, "set", 2, 1);
403
404                         DataTable dt = ds.Tables [0];
405                         AssertDataTable ("dt", dt, "table", 1, 0, 0, 1, 1, 1);
406                         AssertDataColumn ("table_Id", dt.Columns [0], "table_Id", false, true, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
407
408                         dt = ds.Tables [1];
409                         AssertDataTable ("dt", dt, "col", 2, 0, 1, 0, 1, 0);
410                         AssertDataColumn ("another_col", dt.Columns [0], "another_col", true, false, 0, 1, "another_col", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
411                         AssertDataColumn ("table_refId", dt.Columns [1], "table_Id", true, false, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
412
413                         DataRelation dr = ds.Relations [0];
414                         AssertDataRelation ("rel", dr, "table_col", true, new string [] {"table_Id"}, new string [] {"table_Id"}, true, true);
415                         AssertUniqueConstraint ("uniq", dr.ParentKeyConstraint, "Constraint1", true, new string [] {"table_Id"});
416                         AssertForeignKeyConstraint ("fkey", dr.ChildKeyConstraint, "table_col", AcceptRejectRule.None, Rule.Cascade, Rule.Cascade, new string [] {"table_Id"}, new string [] {"table_Id"});
417                 }
418
419                 [Test]
420                 public void ConflictColumnTable ()
421                 {
422                         DataSet ds = GetDataSet (xml19, null);
423                         AssertDataSet ("ds", ds, "set", 2, 1);
424
425                         DataTable dt = ds.Tables [0];
426                         AssertDataTable ("dt", dt, "table", 1, 0, 0, 1, 1, 1);
427                         AssertDataColumn ("table_Id", dt.Columns [0], "table_Id", false, true, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
428
429                         dt = ds.Tables [1];
430                         AssertDataTable ("dt", dt, "col", 2, 0, 1, 0, 1, 0);
431                         AssertDataColumn ("table_refId", dt.Columns ["table_Id"], "table_Id", true, false, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, /*0*/-1, String.Empty, false, false);
432                         AssertDataColumn ("another_col", dt.Columns ["another_col"], "another_col", true, false, 0, 1, "another_col", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, /*1*/-1, String.Empty, false, false);
433
434                         DataRelation dr = ds.Relations [0];
435                         AssertDataRelation ("rel", dr, "table_col", true, new string [] {"table_Id"}, new string [] {"table_Id"}, true, true);
436                         AssertUniqueConstraint ("uniq", dr.ParentKeyConstraint, "Constraint1", true, new string [] {"table_Id"});
437                         AssertForeignKeyConstraint ("fkey", dr.ChildKeyConstraint, "table_col", AcceptRejectRule.None, Rule.Cascade, Rule.Cascade, new string [] {"table_Id"}, new string [] {"table_Id"});
438                 }
439
440                 [Test]
441                 public void ConflictColumnTableAttribute ()
442                 {
443                         // Conflicts between a column and a table, additionally an attribute.
444                         DataSet ds = GetDataSet (xml20, null);
445                         AssertDataSet ("ds", ds, "set", 2, 1);
446
447                         DataTable dt = ds.Tables [0];
448                         AssertDataTable ("dt", dt, "table", 1, 0, 0, 1, 1, 1);
449                         AssertDataColumn ("table_Id", dt.Columns [0], "table_Id", false, true, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
450
451                         dt = ds.Tables [1];
452                         AssertDataTable ("dt", dt, "col", 3, 0, 1, 0, 1, 0);
453                         AssertDataColumn ("another_col", dt.Columns [0], "another_col", true, false, 0, 1, "another_col", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
454                         AssertDataColumn ("table_refId", dt.Columns ["table_Id"], "table_Id", true, false, 0, 1, "table_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, /*1*/-1, String.Empty, false, false);
455                         AssertDataColumn ("attr", dt.Columns ["attr"], "attr", true, false, 0, 1, "attr", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, /*2*/-1, String.Empty, false, false);
456
457                         DataRelation dr = ds.Relations [0];
458                         AssertDataRelation ("rel", dr, "table_col", true, new string [] {"table_Id"}, new string [] {"table_Id"}, true, true);
459                         AssertUniqueConstraint ("uniq", dr.ParentKeyConstraint, "Constraint1", true, new string [] {"table_Id"});
460                         AssertForeignKeyConstraint ("fkey", dr.ChildKeyConstraint, "table_col", AcceptRejectRule.None, Rule.Cascade, Rule.Cascade, new string [] {"table_Id"}, new string [] {"table_Id"});
461                 }
462
463                 [Test]
464                 [ExpectedException (typeof (DataException))]
465                 public void ConflictAttributeDataTable ()
466                 {
467                         // attribute "data" becomes DataTable, and when column "data"
468                         // appears, it cannot be DataColumn, since the name is 
469                         // already allocated for DataTable.
470                         DataSet ds = GetDataSet (xml21, null);
471                 }
472
473                 [Test]
474                 [ExpectedException (typeof (ConstraintException))]
475                 public void ConflictExistingPrimaryKey ()
476                 {
477                         // <wrong>The 'col' DataTable tries to create another primary key (and fails)</wrong> The data violates key constraint.
478                         DataSet ds = new DataSet ();
479                         ds.Tables.Add (new DataTable ("table"));
480                         DataColumn c = new DataColumn ("pk");
481                         ds.Tables [0].Columns.Add (c);
482                         ds.Tables [0].PrimaryKey = new DataColumn [] {c};
483                         XmlTextReader xtr = new XmlTextReader (xml22, XmlNodeType.Document, null);
484                         xtr.Read ();
485                         ds.ReadXml (xtr, XmlReadMode.InferSchema);
486                 }
487
488                 [Test]
489                 public void IgnoredNamespaces ()
490                 {
491                         string xml = "<root attr='val' xmlns:a='urn:foo' a:foo='hogehoge' />";
492                         DataSet ds = new DataSet ();
493                         ds.InferXmlSchema (new StringReader (xml), null);
494                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
495                         AssertDataTable ("dt", ds.Tables [0], "root", 2, 0, 0, 0, 0, 0);
496
497                         ds = new DataSet ();
498                         ds.InferXmlSchema (new StringReader (xml), new string [] {"urn:foo"});
499                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
500                         // a:foo is ignored
501                         AssertDataTable ("dt", ds.Tables [0], "root", 1, 0, 0, 0, 0, 0);
502                 }
503
504                 [Test]
505                 public void ContainsSchema ()
506                 {
507                         DataSet ds = new DataSet();
508                         DataTable dt1 = new DataTable();
509                         ds.Tables.Add(dt1);
510                         DataColumn dc1 = new DataColumn("Col1");
511                         dt1.Columns.Add(dc1);
512                         dt1.Rows.Add(new string[] { "aaa" } );
513                         DataTable dt2 = new DataTable();
514                         ds.Tables.Add(dt2);
515                         DataColumn dc2 = new DataColumn("Col2");
516                         dt2.Columns.Add(dc2);
517                         dt2.Rows.Add(new string[] { "bbb" } );
518
519                         DataRelation rel = new DataRelation("Rel1",dc1,dc2,false);
520                         ds.Relations.Add(rel);
521
522                         StringWriter sw = new StringWriter ();
523                         ds.WriteXml(sw, XmlWriteMode.WriteSchema);
524
525                         ds = new DataSet();
526                         ds.ReadXml(new StringReader (sw.ToString ()));
527                         sw = new StringWriter ();
528                         ds.WriteXml(sw);
529                         XmlDocument doc = new XmlDocument ();
530                         doc.LoadXml (sw.ToString ());
531                         Assert.AreEqual (2, doc.DocumentElement.ChildNodes.Count);
532                 }
533         }
534 }