Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System.Data / Test / System.Data / DataSetReadXmlSchemaTest.cs
1 //
2 // DataSetReadXmlSchemaTest.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.IO;
34 using System.Data;
35 using System.Globalization;
36 using System.Text;
37 using System.Threading;
38 using System.Xml;
39 using NUnit.Framework;
40
41 namespace MonoTests.System.Data
42 {
43         [TestFixture]
44         public class DataSetReadXmlSchemaTest : DataSetAssertion
45         {
46                 private DataSet CreateTestSet ()
47                 {
48                         DataSet ds = new DataSet ();
49                         ds.Tables.Add ("Table1");
50                         ds.Tables.Add ("Table2");
51                         ds.Tables [0].Columns.Add ("Column1_1");
52                         ds.Tables [0].Columns.Add ("Column1_2");
53                         ds.Tables [0].Columns.Add ("Column1_3");
54                         ds.Tables [1].Columns.Add ("Column2_1");
55                         ds.Tables [1].Columns.Add ("Column2_2");
56                         ds.Tables [1].Columns.Add ("Column2_3");
57                         ds.Tables [0].Rows.Add (new object [] {"ppp", "www", "xxx"});
58                         ds.Relations.Add ("Rel1", ds.Tables [0].Columns [2], ds.Tables [1].Columns [0]);
59                         return ds;
60                 }
61
62                 CultureInfo currentCultureBackup;
63
64                 [SetUp]
65                 public void Setup ()
66                 {
67                         currentCultureBackup = Thread.CurrentThread.CurrentCulture;
68                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("fi-FI");
69                 }
70
71                 [TearDown]
72                 public void Teardown ()
73                 {
74                         Thread.CurrentThread.CurrentCulture = currentCultureBackup;
75                 }
76
77                 [Test]
78                 public void SingleElementTreatmentDifference ()
79                 {
80                         // This is one of the most complicated case. When the content
81                         // type particle of 'Root' element is a complex element, it
82                         // is DataSet element. Otherwise, it is just a data table.
83                         //
84                         // But also note that there is another test named
85                         // LocaleOnRootWithoutIsDataSet(), that tests if locale on
86                         // the (mere) data table modifies *DataSet's* locale.
87
88                         // Moreover, when the schema contains another element
89                         // (regardless of its schema type), the elements will
90                         // never be treated as a DataSet.
91                         string xsbase = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' id='hoge'>
92         <xs:element name='Root'> <!-- When simple, it becomes table. When complex, it becomes DataSet -->
93                 <xs:complexType>
94                         <xs:choice>
95                                 {0}
96                         </xs:choice>
97                 </xs:complexType>
98         </xs:element>
99 </xs:schema>";
100
101                         string xsbase2 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' id='hoge'>
102         <xs:element name='Root'> <!-- When simple, it becomes table. When complex, it becomes DataSet -->
103                 <xs:complexType>
104                         <xs:choice>
105                                 {0}
106                         </xs:choice>
107                 </xs:complexType>
108         </xs:element>
109         <xs:element name='more' type='xs:string' />
110 </xs:schema>";
111
112                         string simple = "<xs:element name='Child' type='xs:string' />";
113                         string complex = @"<xs:element name='Child'>
114         <xs:complexType>
115                 <xs:attribute name='a1' />
116                 <xs:attribute name='a2' type='xs:integer' />
117         </xs:complexType>
118 </xs:element>";
119                         string elref = "<xs:element ref='more' />";
120
121                         string xs2 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' id='hoge'>
122         <xs:element name='Root' type='RootType' />
123         <xs:complexType name='RootType'>
124                 <xs:choice>
125                         <xs:element name='Child'>
126                                 <xs:complexType>
127                                         <xs:attribute name='a1' />
128                                         <xs:attribute name='a2' type='xs:integer' />
129                                 </xs:complexType>
130                         </xs:element>
131                 </xs:choice>
132         </xs:complexType>
133 </xs:schema>";
134
135                         DataSet ds = new DataSet ();
136
137                         string xs = String.Format (xsbase, simple);
138                         ds.ReadXmlSchema (new StringReader (xs));
139                         AssertDataSet ("simple", ds, "hoge", 1, 0);
140                         AssertDataTable ("simple", ds.Tables [0], "Root", 1, 0, 0, 0, 0, 0);
141
142                         // reference to global complex type
143                         ds = new DataSet ();
144                         ds.ReadXmlSchema (new StringReader (xs2));
145                         AssertDataSet ("external complexType", ds, "hoge", 2, 1);
146                         AssertDataTable ("external Tab1", ds.Tables [0], "Root", 1, 0, 0, 1, 1, 1);
147                         AssertDataTable ("external Tab2", ds.Tables [1], "Child", 3, 0, 1, 0, 1, 0);
148
149                         // xsbase2 + complex -> datatable
150                         ds = new DataSet ();
151                         xs = String.Format (xsbase2, complex);
152                         ds.ReadXmlSchema (new StringReader (xs));
153                         AssertDataSet ("complex", ds, "hoge", 2, 1);
154                         AssertDataTable ("complex", ds.Tables [0], "Root", 1, 0, 0, 1, 1, 1);
155                         DataTable dt = ds.Tables [1];
156                         AssertDataTable ("complex", dt, "Child", 3, 0, 1, 0, 1, 0);
157                         AssertDataColumn ("a1", dt.Columns ["a1"], "a1", true, false, 0, 1, "a1", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, /*0*/-1, String.Empty, false, false);
158                         AssertDataColumn ("a2", dt.Columns ["a2"], "a2", true, false, 0, 1, "a2", MappingType.Attribute, typeof (long), DBNull.Value, String.Empty, -1, String.Empty, /*1*/-1, String.Empty, false, false);
159                         AssertDataColumn ("Root_Id", dt.Columns [2], "Root_Id", true, false, 0, 1, "Root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 2, String.Empty, false, false);
160
161                         // xsbase + complex -> dataset
162                         ds = new DataSet ();
163                         xs = String.Format (xsbase, complex);
164                         ds.ReadXmlSchema (new StringReader (xs));
165                         AssertDataSet ("complex", ds, "Root", 1, 0);
166
167                         ds = new DataSet ();
168                         xs = String.Format (xsbase2, elref);
169                         ds.ReadXmlSchema (new StringReader (xs));
170                         AssertDataSet ("complex", ds, "hoge", 1, 0);
171                         AssertDataTable ("complex", ds.Tables [0], "Root", 1, 0, 0, 0, 0, 0);
172                 }
173
174                 [Test]
175                 public void SuspiciousDataSetElement ()
176                 {
177                         string schema = @"<?xml version='1.0'?>
178 <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
179         <xsd:attribute name='foo' type='xsd:string'/>
180         <xsd:attribute name='bar' type='xsd:string'/>
181         <xsd:complexType name='attRef'>
182                 <xsd:attribute name='att1' type='xsd:int'/>
183                 <xsd:attribute name='att2' type='xsd:string'/>
184         </xsd:complexType>
185         <xsd:element name='doc'>
186                 <xsd:complexType>
187                         <xsd:choice>
188                                 <xsd:element name='elem' type='attRef'/>
189                         </xsd:choice>
190                 </xsd:complexType>
191         </xsd:element>
192 </xsd:schema>";
193                         DataSet ds = new DataSet ();
194                         ds.ReadXmlSchema (new StringReader (schema));
195                         AssertDataSet ("ds", ds, "doc", 1, 0);
196                         AssertDataTable ("table", ds.Tables [0], "elem", 2, 0, 0, 0, 0, 0);
197                 }
198
199                 [Test]
200                 public void UnusedComplexTypesIgnored ()
201                 {
202                         string xs = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' id='hoge'>
203         <xs:element name='Root'>
204                 <xs:complexType>
205                         <xs:sequence>
206                                 <xs:element name='Child' type='xs:string' />
207                         </xs:sequence>
208                 </xs:complexType>
209         </xs:element>
210         <xs:complexType name='unusedType'>
211                 <xs:sequence>
212                         <xs:element name='Orphan' type='xs:string' />
213                 </xs:sequence>
214         </xs:complexType>
215 </xs:schema>";
216
217                         DataSet ds = new DataSet ();
218                         ds.ReadXmlSchema (new StringReader (xs));
219                         // Here "unusedType" table is never imported.
220                         AssertDataSet ("ds", ds, "hoge", 1, 0);
221                         AssertDataTable ("dt", ds.Tables [0], "Root", 1, 0, 0, 0, 0, 0);
222                 }
223
224                 [Test]
225                 public void SimpleTypeComponentsIgnored ()
226                 {
227                         string xs = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
228         <xs:element name='Root' type='xs:string'/>
229         <xs:attribute name='Attr' type='xs:string'/>
230 </xs:schema>";
231
232                         DataSet ds = new DataSet ();
233                         ds.ReadXmlSchema (new StringReader (xs));
234                         // nothing is imported.
235                         AssertDataSet ("ds", ds, "NewDataSet", 0, 0);
236                 }
237
238                 [Test]
239                 public void IsDataSetAndTypeIgnored ()
240                 {
241                         string xsbase = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
242         <xs:element name='Root' type='unusedType' msdata:IsDataSet='{0}'>
243         </xs:element>
244         <xs:complexType name='unusedType'>
245                 <xs:sequence>
246                         <xs:element name='Child' type='xs:string' />
247                 </xs:sequence>
248         </xs:complexType>
249 </xs:schema>";
250
251                         // Even if a global element uses a complexType, it will be
252                         // ignored if the element has msdata:IsDataSet='true'
253                         string xs = String.Format (xsbase, "true");
254
255                         DataSet ds = new DataSet ();
256                         ds.ReadXmlSchema (new StringReader (xs));
257                         AssertDataSet ("ds", ds, "Root", 0, 0); // name is "Root"
258
259                         // But when explicit msdata:IsDataSet value is "false", then
260                         // treat as usual.
261                         xs = String.Format (xsbase, "false");
262
263                         ds = new DataSet ();
264                         ds.ReadXmlSchema (new StringReader (xs));
265                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
266                 }
267
268                 [Test]
269                 [ExpectedException (typeof (ArgumentException))]
270                 public void NestedReferenceNotAllowed ()
271                 {
272                         string xs = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
273         <xs:element name='Root' type='unusedType' msdata:IsDataSet='true'>
274         </xs:element>
275         <xs:complexType name='unusedType'>
276                 <xs:sequence>
277                         <xs:element name='Child' type='xs:string' />
278                 </xs:sequence>
279         </xs:complexType>
280         <xs:element name='Foo'>
281                 <xs:complexType>
282                         <xs:sequence>
283                                 <xs:element ref='Root' />
284                         </xs:sequence>
285                 </xs:complexType>
286         </xs:element>
287 </xs:schema>";
288
289                         // DataSet element cannot be converted into a DataTable.
290                         // (i.e. cannot be referenced in any other elements)
291                         DataSet ds = new DataSet ();
292                         ds.ReadXmlSchema (new StringReader (xs));
293                 }
294
295                 [Test]
296                 public void IsDataSetOnLocalElementIgnored ()
297                 {
298                         string xsbase = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
299         <xs:element name='Root' type='unusedType'>
300         </xs:element>
301         <xs:complexType name='unusedType'>
302                 <xs:sequence>
303                         <xs:element name='Child' type='xs:string' msdata:IsDataSet='True' />
304                 </xs:sequence>
305         </xs:complexType>
306 </xs:schema>";
307
308                         // msdata:IsDataSet does not affect even if the value is invalid
309                         string xs = String.Format (xsbase, "true");
310
311                         DataSet ds = new DataSet ();
312                         ds.ReadXmlSchema (new StringReader (xs));
313                         // Child should not be regarded as DataSet element
314                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
315                 }
316
317                 [Test]
318                 public void LocaleOnRootWithoutIsDataSet ()
319                 {
320                         string xs = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
321         <xs:element name='Root' msdata:Locale='ja-JP'>
322                 <xs:complexType>
323                         <xs:sequence>
324                                 <xs:element name='Child' type='xs:string' />
325                         </xs:sequence>
326                         <xs:attribute name='Attr' type='xs:integer' />
327                 </xs:complexType>
328         </xs:element>
329 </xs:schema>";
330
331                         DataSet ds = new DataSet ();
332                         ds.ReadXmlSchema (new StringReader (xs));
333                         AssertDataSet ("ds", ds, "NewDataSet", 1, 0);
334                         Assert.AreEqual ("fi-FI", ds.Locale.Name); // DataSet's Locale comes from current thread
335                         DataTable dt = ds.Tables [0];
336                         AssertDataTable ("dt", dt, "Root", 2, 0, 0, 0, 0, 0);
337                         Assert.AreEqual ("ja-JP", dt.Locale.Name); // DataTable's Locale comes from msdata:Locale
338                         AssertDataColumn ("col1", dt.Columns [0], "Attr", true, false, 0, 1, "Attr", MappingType.Attribute, typeof (Int64), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
339                         AssertDataColumn ("col2", dt.Columns [1], "Child", false, false, 0, 1, "Child", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
340                 }
341
342
343                 [Test]
344                 public void ElementHasIdentityConstraint ()
345                 {
346                         string constraints = @"
347                 <xs:key name='key'>
348                         <xs:selector xpath='./any/string_is_OK/R1'/>
349                         <xs:field xpath='Child2'/>
350                 </xs:key>
351                 <xs:keyref name='kref' refer='key'>
352                         <xs:selector xpath='.//R2'/>
353                         <xs:field xpath='Child2'/>
354                 </xs:keyref>";
355                         string xsbase = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
356         <xs:element name='DS' msdata:IsDataSet='true'>
357                 <xs:complexType>
358                         <xs:choice>
359                                 <xs:element ref='R1' />
360                                 <xs:element ref='R2' />
361                         </xs:choice>
362                 </xs:complexType>
363                 {0}
364         </xs:element>
365         <xs:element name='R1' type='RootType'>
366               {1}
367         </xs:element>
368         <xs:element name='R2' type='RootType'>
369         </xs:element>
370         <xs:complexType name='RootType'>
371                 <xs:choice>
372                         <xs:element name='Child1' type='xs:string'>
373                                 {2}
374                         </xs:element>
375                         <xs:element name='Child2' type='xs:string' />
376                 </xs:choice>
377                 <xs:attribute name='Attr' type='xs:integer' />
378         </xs:complexType>
379 </xs:schema>";
380
381                         // Constraints on DataSet element.
382                         // Note that in xs:key xpath is crazy except for the last step
383                         string xs = String.Format (xsbase, constraints, String.Empty, String.Empty);
384                         DataSet ds = new DataSet ();
385                         ds.ReadXmlSchema (new StringReader (xs));
386                         Assert.AreEqual (1, ds.Relations.Count);
387
388                         // Constraints on another global element - just ignored
389                         xs = String.Format (xsbase, String.Empty, constraints, String.Empty);
390                         ds = new DataSet ();
391                         ds.ReadXmlSchema (new StringReader (xs));
392                         Assert.AreEqual (0, ds.Relations.Count);
393
394                         // Constraints on local element - just ignored
395                         xs = String.Format (xsbase, String.Empty, String.Empty, constraints);
396                         ds = new DataSet ();
397                         ds.ReadXmlSchema (new StringReader (xs));
398                         Assert.AreEqual (0, ds.Relations.Count);
399                 }
400
401                 [Test]
402                 public void PrefixedTargetNS ()
403                 {
404                         string xs = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:x='urn:foo' targetNamespace='urn:foo' elementFormDefault='qualified'>
405         <xs:element name='DS' msdata:IsDataSet='true'>
406                 <xs:complexType>
407                         <xs:choice>
408                                 <xs:element ref='x:R1' />
409                                 <xs:element ref='x:R2' />
410                         </xs:choice>
411                 </xs:complexType>
412                 <xs:key name='key'>
413                         <xs:selector xpath='./any/string_is_OK/x:R1'/>
414                         <xs:field xpath='x:Child2'/>
415                 </xs:key>
416                 <xs:keyref name='kref' refer='x:key'>
417                         <xs:selector xpath='.//x:R2'/>
418                         <xs:field xpath='x:Child2'/>
419                 </xs:keyref>
420         </xs:element>
421         <xs:element name='R3' type='x:RootType' />
422         <xs:complexType name='extracted'>
423                 <xs:choice>
424                         <xs:element ref='x:R1' />
425                         <xs:element ref='x:R2' />
426                 </xs:choice>
427         </xs:complexType>
428         <xs:element name='R1' type='x:RootType'>
429                 <xs:unique name='Rkey'>
430                         <xs:selector xpath='.//x:Child1'/>
431                         <xs:field xpath='.'/>
432                 </xs:unique>
433                 <xs:keyref name='Rkref' refer='x:Rkey'>
434                         <xs:selector xpath='.//x:Child2'/>
435                         <xs:field xpath='.'/>
436                 </xs:keyref>
437         </xs:element>
438         <xs:element name='R2' type='x:RootType'>
439         </xs:element>
440         <xs:complexType name='RootType'>
441                 <xs:choice>
442                         <xs:element name='Child1' type='xs:string'>
443                         </xs:element>
444                         <xs:element name='Child2' type='xs:string' />
445                 </xs:choice>
446                 <xs:attribute name='Attr' type='xs:integer' />
447         </xs:complexType>
448 </xs:schema>";
449                         // No prefixes on tables and columns
450                         DataSet ds = new DataSet ();
451                         ds.ReadXmlSchema (new StringReader (xs));
452                         AssertDataSet ("ds", ds, "DS", 3, 1);
453                         DataTable dt = ds.Tables [0];
454                         AssertDataTable ("R3", dt, "R3", 3, 0, 0, 0, 0, 0);
455                         AssertDataColumn ("col1", dt.Columns [0], "Attr", true, false, 0, 1, "Attr", MappingType.Attribute, typeof (Int64), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
456                 }
457
458                 [Test]
459                 public void ReadTest1 ()
460                 {
461                         DataSet ds = CreateTestSet ();
462
463                         StringWriter sw = new StringWriter ();
464                         ds.WriteXmlSchema (sw);
465
466                         string schema = sw.ToString ();
467
468                         // ReadXmlSchema()
469                         ds = new DataSet ();
470                         ds.ReadXmlSchema (new XmlTextReader (schema, XmlNodeType.Document, null));
471                         ReadTest1Check (ds);
472
473                         // ReadXml() should also be the same
474                         ds = new DataSet ();
475                         ds.ReadXml (new XmlTextReader (schema, XmlNodeType.Document, null));
476                         ReadTest1Check (ds);
477                 }
478
479                 private void ReadTest1Check (DataSet ds)
480                 {
481                         AssertDataSet ("dataset", ds, "NewDataSet", 2, 1);
482                         AssertDataTable ("tbl1", ds.Tables [0], "Table1", 3, 0, 0, 1, 1, 0);
483                         AssertDataTable ("tbl2", ds.Tables [1], "Table2", 3, 0, 1, 0, 1, 0);
484
485                         DataRelation rel = ds.Relations [0];
486                         AssertDataRelation ("rel", rel, "Rel1", false,
487                                 new string [] {"Column1_3"},
488                                 new string [] {"Column2_1"}, true, true);
489                         AssertUniqueConstraint ("uc", rel.ParentKeyConstraint, 
490                                 "Constraint1", false, new string [] {"Column1_3"});
491                         AssertForeignKeyConstraint ("fk", rel.ChildKeyConstraint, "Rel1",
492                                 AcceptRejectRule.None, Rule.Cascade, Rule.Cascade,
493                                 new string [] {"Column2_1"}, 
494                                 new string [] {"Column1_3"});
495                 }
496
497                 [Test]
498                 // 001-004
499                 public void TestSampleFileNoTables ()
500                 {
501                         DataSet ds = new DataSet ();
502                         ds.ReadXmlSchema ("Test/System.Data/schemas/test001.xsd");
503                         AssertDataSet ("001", ds, "NewDataSet", 0, 0);
504
505                         ds = new DataSet ();
506                         ds.ReadXmlSchema ("Test/System.Data/schemas/test002.xsd");
507                         AssertDataSet ("002", ds, "NewDataSet", 0, 0);
508
509                         ds = new DataSet ();
510                         ds.ReadXmlSchema ("Test/System.Data/schemas/test003.xsd");
511                         AssertDataSet ("003", ds, "NewDataSet", 0, 0);
512
513                         ds = new DataSet ();
514                         ds.ReadXmlSchema ("Test/System.Data/schemas/test004.xsd");
515                         AssertDataSet ("004", ds, "NewDataSet", 0, 0);
516                 }
517
518                 [Test]
519                 public void TestSampleFileSimpleTables ()
520                 {
521                         DataSet ds = new DataSet ();
522                         ds.ReadXmlSchema ("Test/System.Data/schemas/test005.xsd");
523                         AssertDataSet ("005", ds, "NewDataSet", 1, 0);
524                         DataTable dt = ds.Tables [0];
525                         AssertDataTable ("tab", dt, "foo", 2, 0, 0, 0, 0, 0);
526                         AssertDataColumn ("attr", 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);
527                         AssertDataColumn ("text", dt.Columns [1], "foo_text", false, false, 0, 1, "foo_text", MappingType.SimpleContent, typeof (long), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
528
529                         ds = new DataSet ();
530                         ds.ReadXmlSchema ("Test/System.Data/schemas/test006.xsd");
531                         AssertDataSet ("006", ds, "NewDataSet", 1, 0);
532                         dt = ds.Tables [0];
533                         AssertDataTable ("tab", dt, "foo", 2, 0, 0, 0, 0, 0);
534                         AssertDataColumn ("att1", dt.Columns ["att1"], "att1", true, false, 0, 1, "att1", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, /*0*/-1, String.Empty, false, false);
535                         AssertDataColumn ("att2", dt.Columns ["att2"], "att2", true, false, 0, 1, "att2", MappingType.Attribute, typeof (int), 2, String.Empty, -1, String.Empty, /*1*/-1, String.Empty, false, false);
536                 }
537
538                 [Test]
539                 public void TestSampleFileComplexTables ()
540                 {
541                         // Nested simple type element
542                         DataSet ds = new DataSet ();
543                         ds.ReadXmlSchema ("Test/System.Data/schemas/test007.xsd");
544                         AssertDataSet ("007", ds, "NewDataSet", 2, 1);
545                         DataTable dt = ds.Tables [0];
546                         AssertDataTable ("tab1", dt, "uno", 1, 0, 0, 1, 1, 1);
547                         AssertDataColumn ("id", dt.Columns [0], "uno_Id", false, true, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, "urn:foo", 0, String.Empty, false, true);
548
549                         dt = ds.Tables [1];
550                         AssertDataTable ("tab2", dt, "des", 2, 0, 1, 0, 1, 0);
551                         AssertDataColumn ("child", dt.Columns [0], "tres", false, false, 0, 1, "tres", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
552                         AssertDataColumn ("id", dt.Columns [1], "uno_Id", true, false, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
553
554                         // External simple type element
555                         ds = new DataSet ();
556                         ds.ReadXmlSchema ("Test/System.Data/schemas/test008.xsd");
557                         AssertDataSet ("008", ds, "NewDataSet", 2, 1);
558                         dt = ds.Tables [0];
559                         AssertDataTable ("tab1", dt, "uno", 1, 0, 0, 1, 1, 1);
560                         AssertDataColumn ("id", dt.Columns [0], "uno_Id", false, true, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, "urn:foo", 0, String.Empty, false, true);
561
562                         dt = ds.Tables [1];
563                         AssertDataTable ("tab2", dt, "des", 2, 0, 1, 0, 1, 0);
564                         AssertDataColumn ("child", dt.Columns [0], "tres", false, false, 0, 1, "tres", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, "urn:foo", 0, String.Empty, false, false);
565                         AssertDataColumn ("id", dt.Columns [1], "uno_Id", true, false, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
566
567                 }
568
569                 [Test]
570                 [Category ("NotDotNet")]
571                 // MS.NET has a bug on handling default value of referenced
572                 // attribute.
573                 public void TestSampleFileValueConstraints ()
574                 {
575                         DataSet ds = new DataSet ();
576                         ds.ReadXmlSchema ("Test/System.Data/schemas/test009.xsd");
577                         AssertDataSet ("009", ds, "NewDataSet", 2, 1);
578
579                         DataTable dt = ds.Tables [0];
580                         AssertDataTable ("tab1", dt, "uno", 2, 0, 0, 1, 1, 1);
581                         AssertDataColumn ("global", dt.Columns [0], "global", true, false, 0, 1, "global", MappingType.Attribute, typeof (string), "er", String.Empty, -1, "urn:foo", 0, String.Empty, false, false);
582                         AssertDataColumn ("id", dt.Columns [1], "uno_Id", false, true, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, "urn:foo", 1, String.Empty, false, true);
583
584                         // Here "XmlSchemaComplexType.AttributeUses" does not 
585                         // always return attribute uses in a certain order, thus
586                         // here Columns indexer is accessed by name (actually
587                         // MS.NET returns "global" in prior to "local" for
588                         // Columns[0], even though "local" is defined in prior.
589                         dt = ds.Tables [1];
590                         AssertDataTable ("dos", dt, "des", 4, 0, 1, 0, 1, 0);
591                         AssertDataColumn ("dos.child", dt.Columns ["local"], "local", true, false, 0, 1, "local", MappingType.Attribute, typeof (string), "san", String.Empty, -1, String.Empty, /*0*/-1, String.Empty, false, false);
592                         // LAMESPEC: (MS BUG) default value is overwritten, but MS.NET is ignorant of that.
593 #if BUGGY_MS_COMPATIBLE
594                         AssertDataColumn ("dos.global", dt.Columns ["global"], "global", true, false, 0, 1, "global", MappingType.Attribute, typeof (string), "er", String.Empty, -1, "urn:foo", /*1*/-1, String.Empty, false, false);
595 #else
596                         AssertDataColumn ("dos.global", dt.Columns ["global"], "global", true, false, 0, 1, "global", MappingType.Attribute, typeof (string), "si", String.Empty, -1, "urn:foo", /*1*/-1, String.Empty, false, false);
597 #endif
598                         AssertDataColumn ("dos.tres", dt.Columns [2], "tres", false, false, 0, 1, "tres", MappingType.Element, typeof (string), "yi", String.Empty, -1, "urn:foo", 2, String.Empty, false, false);
599                         AssertDataColumn ("uno.id", dt.Columns [3], "uno_Id", true, false, 0, 1, "uno_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 3, String.Empty, false, false);
600
601                         AssertDataRelation ("rel", ds.Relations [0], "uno_des", true, new string [] {"uno_Id"}, new string [] {"uno_Id"}, true, true);
602                 }
603
604                 [Test]
605                 [Category ("NotWorking")]
606                 public void TestSampleFileImportSimple ()
607                 {
608                         DataSet ds = new DataSet ();
609                         XmlTextReader xtr = null;
610                         try {
611                                 xtr = new XmlTextReader ("Test/System.Data/schemas/test010.xsd");
612                                 xtr.XmlResolver = null;
613                                 ds.ReadXmlSchema (xtr);
614                         } finally {
615                                 if (xtr != null)
616                                         xtr.Close ();
617                         }
618                         AssertDataSet ("010", ds, "NewDataSet", 1, 0);
619
620                         DataTable dt = ds.Tables [0];
621                         AssertDataTable ("root", dt, "foo", 1, 0, 0, 0, 0, 0);
622                         AssertDataColumn ("simple", dt.Columns [0], "bar", false, false, 0, 1, "bar", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
623                 }
624
625                 [Test]
626                 [Category ("NotWorking")]
627                 public void TestSampleFileComplexTables2 ()
628                 {
629                         DataSet ds = new DataSet ();
630                         ds.ReadXmlSchema ("Test/System.Data/schemas/test011.xsd");
631                         AssertDataSet ("011", ds, "NewDataSet", 2, 1);
632
633                         DataTable dt = ds.Tables [0];
634                         AssertDataTable ("root", dt, "e", 3, 0, 1, 0, 1, 0);
635                         AssertDataColumn ("attr", dt.Columns [0], "a", true, false, 0, 1, "a", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, "http://xsdtesting", 0, String.Empty, false, false);
636                         AssertDataColumn ("simple", dt.Columns [1], "e_text", false, false, 0, 1, "e_text", MappingType.SimpleContent, typeof (decimal), DBNull.Value, String.Empty, -1, "http://xsdtesting", 1, String.Empty, false, false);
637                         AssertDataColumn ("hidden", dt.Columns [2], "root_Id", true, false, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, "http://xsdtesting", 2, String.Empty, false, false);
638
639                         dt = ds.Tables [1];
640                         AssertDataTable ("root", dt, "root", 1, 0, 0, 1, 1, 1);
641                         AssertDataColumn ("elem", dt.Columns [0], "root_Id", false, true, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, "http://xsdtesting", 0, String.Empty, false, true);
642
643                         AssertDataRelation ("rel", ds.Relations [0], "root_e", true, new string [] {"root_Id"}, new string [] {"root_Id"}, true, true);
644                 }
645
646                 [Test]
647                 public void TestSampleFileComplexTables3 ()
648                 {
649                         DataSet ds = new DataSet ();
650                         ds.ReadXmlSchema ("Test/System.Data/schemas/test013.xsd");
651                         AssertDataSet ("013", ds, "root", 1, 0);
652
653                         DataTable dt = ds.Tables [0];
654                         AssertDataTable ("root", dt, "e", 2, 0, 0, 0, 0, 0);
655                         AssertDataColumn ("attr", dt.Columns [0], "a", true, false, 0, 1, "a", MappingType.Attribute, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
656                         AssertDataColumn ("simple", dt.Columns [1], "e_text", false, false, 0, 1, "e_text", MappingType.SimpleContent, typeof (decimal), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
657                 }
658
659                 // bug #58744
660                 [Test]
661                 public void TestSampleFileXPath ()
662                 {
663                         DataSet ds = new DataSet ();
664                         ds.ReadXmlSchema ("Test/System.Data/schemas/test103.xsd");
665                 }
666
667                 [Test]
668                 public void TestAnnotatedRelation1 ()
669                 {
670                         DataSet ds = new DataSet ();
671                         ds.ReadXmlSchema ("Test/System.Data/schemas/test101.xsd");
672                         AssertDataSet ("101", ds, "root", 2, 1);
673                         DataTable dt = ds.Tables [0];
674                         AssertDataTable ("parent_table", dt, "p", 2, 0, 0, 1, 0, 0);
675                         AssertDataColumn ("pk", dt.Columns [0], "pk", false, false, 0, 1, "pk", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
676
677                         dt = ds.Tables [1];
678                         AssertDataTable ("child_table", dt, "c", 2, 0, 1, 0, 0, 0);
679                         AssertDataColumn ("fk", dt.Columns [0], "fk", false, false, 0, 1, "fk", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
680
681                         AssertDataRelation ("rel", ds.Relations [0], "rel", false, new string [] {"pk"}, new string [] {"fk"}, false, false);
682                 }
683
684                 [Test]
685                 public void TestAnnotatedRelation2 ()
686                 {
687                         DataSet ds = new DataSet ();
688                         ds.ReadXmlSchema ("Test/System.Data/schemas/test102.xsd");
689                         AssertDataSet ("102", ds, "ds", 2, 1);
690                         DataTable dt = ds.Tables [0];
691                         AssertDataTable ("parent_table", dt, "p", 2, 0, 0, 1, 0, 0);
692                         AssertDataColumn ("pk", dt.Columns [0], "pk", false, false, 0, 1, "pk", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
693
694                         dt = ds.Tables [1];
695                         AssertDataTable ("child_table", dt, "c", 2, 0, 1, 0, 0, 0);
696                         AssertDataColumn ("fk", dt.Columns [0], "fk", false, false, 0, 1, "fk", MappingType.Element, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
697
698                         AssertDataRelation ("rel", ds.Relations [0], "rel", true, new string [] {"pk"}, new string [] {"fk"}, false, false);
699                 }
700
701                 [Test]
702                 public void RepeatableSimpleElement ()
703                 {
704                         DataSet ds = new DataSet ();
705                         ds.ReadXmlSchema ("Test/System.Data/schemas/test012.xsd");
706                         AssertDataSet ("012", ds, "NewDataSet", 2, 1);
707                         DataTable dt = ds.Tables [0];
708                         AssertDataTable ("parent", dt, "Foo", 1, 0, 0, 1, 1, 1);
709                         AssertDataColumn ("key", dt.Columns [0], "Foo_Id", false, true, 0, 1, "Foo_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, true);
710
711                         dt = ds.Tables [1];
712                         AssertDataTable ("repeated", dt, "Bar", 2, 0, 1, 0, 1, 0);
713                         AssertDataColumn ("data", dt.Columns [0], "Bar_Column", false, false, 0, 1, "Bar_Column", MappingType.SimpleContent, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
714                         AssertDataColumn ("refkey", dt.Columns [1], "Foo_Id", true, false, 0, 1, "Foo_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
715
716                         AssertDataRelation ("rel", ds.Relations [0], "Foo_Bar", true, new string [] {"Foo_Id"}, new string [] {"Foo_Id"}, true, true);
717                 }
718
719                 [Test]
720                 public void TestMoreThanOneRepeatableColumns ()
721                 {
722                         DataSet ds = new DataSet ();
723                         ds.ReadXmlSchema ("Test/System.Data/schemas/test014.xsd");
724                         AssertDataSet ("014", ds, "NewDataSet", 3, 2);
725
726                         DataTable dt = ds.Tables [0];
727                         AssertDataTable ("parent", dt, "root", 1, 0, 0, 2, 1, 1);
728                         AssertDataColumn ("key", 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);
729
730                         dt = ds.Tables [1];
731                         AssertDataTable ("repeated", dt, "x", 2, 0, 1, 0, 1, 0);
732                         AssertDataColumn ("data_1", dt.Columns [0], "x_Column", false, false, 0, 1, "x_Column", MappingType.SimpleContent, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
733                         AssertDataColumn ("refkey_1", dt.Columns [1], "root_Id", true, false, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
734
735                         dt = ds.Tables [2];
736                         AssertDataTable ("repeated", dt, "y", 2, 0, 1, 0, 1, 0);
737                         AssertDataColumn ("data", dt.Columns [0], "y_Column", false, false, 0, 1, "y_Column", MappingType.SimpleContent, typeof (string), DBNull.Value, String.Empty, -1, String.Empty, 0, String.Empty, false, false);
738                         AssertDataColumn ("refkey", dt.Columns [1], "root_Id", true, false, 0, 1, "root_Id", MappingType.Hidden, typeof (int), DBNull.Value, String.Empty, -1, String.Empty, 1, String.Empty, false, false);
739
740                         AssertDataRelation ("rel", ds.Relations [0], "root_x", true, new string [] {"root_Id"}, new string [] {"root_Id"}, true, true);
741
742                         AssertDataRelation ("rel", ds.Relations [1], "root_y", true, new string [] {"root_Id"}, new string [] {"root_Id"}, true, true);
743                 }
744
745                 [Test]
746                 public void AutoIncrementStep ()
747                 {
748                         DataSet ds = new DataSet("testds");
749                         DataTable tbl = ds.Tables.Add("testtbl");
750                         DataColumn col = tbl.Columns.Add("id", typeof(int));
751                         col.AutoIncrement = true;
752                         col.AutoIncrementSeed = -1;
753                         col.AutoIncrementStep = -1;
754                         tbl.Columns.Add("data", typeof(string));
755                         Assert.IsTrue (ds.GetXmlSchema ().IndexOf ("AutoIncrementStep") > 0);
756                 }
757
758                 [Test]
759                 public void ReadConstraints ()
760                 {
761                         DataSet ds = new DataSet ();
762                         ds.ReadXmlSchema ("Test/System.Data/schemas/test015.xsd");
763
764                         Assert.AreEqual (0, ds.Relations.Count, "#1");
765                         Assert.AreEqual (1, ds.Tables [0].Constraints.Count, "#2" );
766                         Assert.AreEqual (1, ds.Tables [1].Constraints.Count, "#3" );
767                         Assert.AreEqual ("fk1", ds.Tables [1].Constraints [0].ConstraintName, "#4");
768                 }
769
770                 [Test]
771                 public void ReadAnnotatedRelations_MultipleColumns ()
772                 {
773                         DataSet ds = new DataSet ();
774                         ds.ReadXmlSchema ("Test/System.Data/schemas/test016.xsd");
775
776                         Assert.AreEqual (1, ds.Relations.Count, "#1");
777                         Assert.AreEqual ("rel", ds.Relations [0].RelationName, "#2");
778                         Assert.AreEqual (2, ds.Relations [0].ParentColumns.Length, "#3");
779                         Assert.AreEqual (2, ds.Relations [0].ChildColumns.Length, "#4");
780                         Assert.AreEqual (0, ds.Tables [0].Constraints.Count, "#5" );
781                         Assert.AreEqual (0, ds.Tables [1].Constraints.Count, "#6" );
782
783                         AssertDataRelation ("TestRel", ds.Relations [0], "rel", false, new String[] {"col 1","col2"},
784                                         new String[] {"col1","col  2"}, false, false);
785                 }
786         }
787 }