Fix wsdl import wrt element qname (!= type name).
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / XsdDataContractImporterTest.cs
1 //
2 // XsdDataContractImporterTest.cs
3 //
4 // Author:
5 //      Ankit Jain <jankit@novell.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 //
31 // This test code contains tests for both DataContractSerializer and
32 // NetDataContractSerializer. The code could be mostly common.
33 //
34
35 using System;
36 using System.CodeDom;
37 using System.CodeDom.Compiler;
38 using System.Collections.Generic;
39 using System.IO;
40 using System.Linq;
41 using System.Runtime.Serialization;
42 using System.ServiceModel.Description;
43 using System.Xml;
44 using System.Xml.Schema;
45 using System.Xml.Serialization;
46 using Microsoft.CSharp;
47 using NUnit.Framework;
48
49 using QName = System.Xml.XmlQualifiedName;
50
51 namespace MonoTests.System.Runtime.Serialization
52 {
53         [TestFixture]
54         public class XsdContractImporterTest
55         {
56                 MetadataSet metadata;
57                 XmlSchemaSet xss;
58
59                 [SetUp]
60                 public void Setup ()
61                 {
62                         XmlReader xr = XmlTextReader.Create ("Test/System.Runtime.Serialization/one.xml");
63                         metadata = MetadataSet.ReadFrom (xr);
64                         NewXmlSchemaSet ();
65                 }
66
67                 private XmlSchemaSet NewXmlSchemaSet ()
68                 {
69                         xss = new XmlSchemaSet ();
70                         foreach (MetadataSection section in metadata.MetadataSections)
71                                 if (section.Metadata is XmlSchema)
72                                         xss.Add (section.Metadata as XmlSchema);
73
74                         Assert.AreEqual (3, xss.Schemas ().Count, "#1");
75                         return xss;
76                 }
77
78                 private XsdDataContractImporter GetImporter ()
79                 {
80                         CodeCompileUnit ccu = new CodeCompileUnit ();
81                         return new XsdDataContractImporter (ccu);
82                 }
83
84                 [Test]
85                 public void CtorTest ()
86                 {
87                         XsdDataContractImporter xi = new XsdDataContractImporter ();
88                         Assert.IsNotNull (xi.CodeCompileUnit, "#c01");
89
90                         xi = new XsdDataContractImporter (null);
91                         Assert.IsNotNull (xi.CodeCompileUnit, "#c02");
92
93                         xi = new XsdDataContractImporter (new CodeCompileUnit ());
94                 }
95
96
97                 [Test]
98                 [ExpectedException (typeof (InvalidOperationException))]
99                 public void GetCodeTypeReferenceTest ()
100                 {
101                         XsdDataContractImporter xsdi = GetImporter ();
102                         xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
103                 }
104
105                 [Test]
106                 public void GetCodeTypeReferenceTest2 ()
107                 {
108                         NewXmlSchemaSet ();
109
110                         Assert.IsFalse (xss.IsCompiled, "#g01");
111
112                         XsdDataContractImporter xsdi = GetImporter ();
113                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
114                         Assert.IsTrue (xss.IsCompiled, "#g02");
115
116                         CodeTypeReference type = xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
117
118                         //FIXME: How should this type ref be checked?
119                         Assert.IsNotNull (type, "#g03");
120                         Assert.AreEqual (type.BaseType, "dc", "#g04");
121                 }
122
123                 [Test]
124                 public void CanImportTest ()
125                 {
126                         NewXmlSchemaSet ();
127                         XsdDataContractImporter xsdi = GetImporter ();
128
129                         Assert.IsFalse (xss.IsCompiled, "#ci01");
130
131                         Assert.IsTrue (xsdi.CanImport (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")), "#ci02");
132                         Assert.IsTrue (xss.IsCompiled, "#ci03");
133
134                         Assert.IsFalse (xsdi.CanImport (xss, new XmlQualifiedName ("Echo", "http://myns/echo")), "#ci04");
135                         Assert.IsTrue (xsdi.CanImport (xss, new XmlQualifiedName ("int", "http://www.w3.org/2001/XMLSchema")), "#ci05");
136
137                         Assert.IsTrue (xsdi.CanImport (xss), "#ci06");
138
139                         Assert.IsTrue (xsdi.CanImport (xss, 
140                                 xss.GlobalElements [new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")] as XmlSchemaElement), 
141                                 "#ci07");
142
143                         Assert.IsTrue (xsdi.CanImport (xss,
144                                 xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement),
145                                 "#ci08");
146                 }
147
148                 [Test]
149                 public void CanImportTest2 ()
150                 {
151                         NewXmlSchemaSet ();
152                         XsdDataContractImporter xsdi = GetImporter ();
153
154                         List<XmlQualifiedName> names = new List<XmlQualifiedName> ();
155                         names.Add (new XmlQualifiedName ("Echo", "http://myns/echo"));
156                         Assert.IsFalse (xsdi.CanImport (xss, names), "#ci20");
157
158                         names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
159                         Assert.IsFalse (xsdi.CanImport (xss, names), "#ci21");
160
161                         names.Clear ();
162                         names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
163                         Assert.IsTrue (xsdi.CanImport (xss, names), "#ci22");
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (ArgumentNullException))]
168                 public void CanImportNullTest1 ()
169                 {
170                         XsdDataContractImporter xsdi = GetImporter ();
171                         xsdi.CanImport (null);
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (ArgumentNullException))]
176                 public void CanImportNullTest2 ()
177                 {
178                         XsdDataContractImporter xsdi = GetImporter ();
179                         xsdi.CanImport (xss, (XmlQualifiedName) null);
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentNullException))]
184                 public void CanImportNullTest3 ()
185                 {
186                         XsdDataContractImporter xsdi = GetImporter ();
187                         xsdi.CanImport (xss, (XmlSchemaElement) null);
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (ArgumentNullException))]
192                 public void ImportTestNullSchemas ()
193                 {
194                         XsdDataContractImporter xsdi = GetImporter ();
195                         xsdi.Import (null);
196                 }
197
198                 [Test]
199                 [ExpectedException (typeof (ArgumentNullException))]
200                 public void ImportTestNullSchemas2 ()
201                 {
202                         XsdDataContractImporter xsdi = GetImporter ();
203                         xsdi.Import (null, new XmlQualifiedName ("foo"));
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (ArgumentNullException))]
208                 public void ImportTestNullSchemas3 ()
209                 {
210                         XsdDataContractImporter xsdi = GetImporter ();
211                         xsdi.Import (null, new List<XmlQualifiedName> ());
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (ArgumentNullException))]
216                 public void ImportTestNullTypeName ()
217                 {
218                         XsdDataContractImporter xsdi = GetImporter ();
219                         xsdi.Import (new XmlSchemaSet (), (XmlQualifiedName) null);
220                 }
221
222                 [Test]
223                 [ExpectedException (typeof (ArgumentNullException))]
224                 public void ImportTestNullElement ()
225                 {
226                         XsdDataContractImporter xsdi = GetImporter ();
227                         xsdi.Import (new XmlSchemaSet (), (XmlSchemaElement) null);
228                 }
229
230                 [Test]
231                 [ExpectedException (typeof (ArgumentNullException))]
232                 public void ImportTestNullCollection ()
233                 {
234                         XsdDataContractImporter xsdi = GetImporter ();
235                         xsdi.Import (new XmlSchemaSet (), (ICollection<XmlQualifiedName>) null);
236                 }
237
238                 [Test]
239                 [Category ("NotWorking")] // importing almost-invalid element. This test is almost missing the point.
240                 public void ImportTest ()
241                 {
242                         XsdDataContractImporter xsdi = GetImporter ();
243                         XmlSchemaElement element = new XmlSchemaElement();
244                         Assert.IsTrue (xsdi.CanImport (xss, new QName ("dc", "http://schemas.datacontract.org/2004/07/")), "#i01");
245                         Assert.IsTrue (xsdi.CanImport (xss, element), "#i01-2");
246                         Assert.AreEqual (new XmlQualifiedName ("anyType", XmlSchema.Namespace), xsdi.Import (xss, element), "#i02");
247
248                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
249                         Assert.AreEqual (1, ccu.Namespaces.Count, "#i03");
250                         Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i04");
251
252                         Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i05");
253
254                         Dictionary<string, string> mbrs = new Dictionary<string, string> ();
255                         mbrs.Add ("foo", "System.String");
256                         CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i06");
257                 }
258
259                 [Test]
260                 public void ImportDataContract1 ()
261                 {
262                         NewXmlSchemaSet ();
263                         Assert.IsFalse (xss.IsCompiled, "#i20");
264
265                         XsdDataContractImporter xsdi = GetImporter ();
266
267                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
268                         Assert.IsTrue (xss.IsCompiled, "#i21");
269                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
270
271                         Assert.AreEqual (1, ccu.Namespaces.Count, "#i22");
272                         Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i23");
273
274                         Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i24");
275
276                         Dictionary<string, string> mbrs = new Dictionary<string, string> ();
277                         mbrs.Add ("foo", "System.String");
278                         CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i25");
279
280                         ccu.Namespaces.Clear ();
281                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
282                         //Importing same data contract again with same importer
283                         Assert.AreEqual (0, ccu.Namespaces.Count, "#i26");
284                 }
285
286                 [Test]
287                 public void ImportDataContract2 ()
288                 {
289                         NewXmlSchemaSet ();
290                         XsdDataContractImporter xsdi = GetImporter ();
291
292                         xss.Compile ();
293                         xsdi.Import (xss, xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement);
294                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
295
296                         Assert.AreEqual (2, ccu.Namespaces.Count, "#i29");
297                         Dictionary<string, string> args = new Dictionary<string, string> ();
298                         args.Add ("msg", "System.String");
299                         args.Add ("num", "System.Int32");
300                         args.Add ("d", "dc");
301
302                         CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i30");
303
304                         args.Clear ();
305                         args.Add ("foo", "System.String");
306                         CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i31");
307
308                         ccu.Namespaces.Clear ();
309                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
310                         Assert.AreEqual (0, ccu.Namespaces.Count);
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (InvalidDataContractException))]
315                 public void ImportMessageEcho ()
316                 {
317                         XsdDataContractImporter xsdi = GetImporter ();
318                         xsdi.Import (xss, new XmlQualifiedName ("Echo", "http://myns/echo"));
319                 }
320
321                 [Test]
322                 public void ImportAll ()
323                 {
324                         NewXmlSchemaSet ();
325                         XsdDataContractImporter xsdi = GetImporter ();
326
327                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
328                         xsdi.Import (xss);
329
330                         Assert.AreEqual (2, ccu.Namespaces.Count, "#i40");
331                         Assert.AreEqual ("myns.echo", ccu.Namespaces [0].Name, "#i41");
332                         Assert.AreEqual ("", ccu.Namespaces [1].Name, "#i42");
333
334                         Assert.AreEqual (4, ccu.Namespaces [0].Types.Count, "#i43");
335
336                         /* ns : myns.echo
337                          * Messages */
338                         Dictionary <string, string> args = new Dictionary <string, string> ();
339                         args.Add ("msg", "System.String");
340                         args.Add ("num", "System.Int32");
341                         args.Add ("d", "dc");
342                         
343                         CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i44");
344
345                         args.Clear ();
346                         args.Add ("EchoResult", "System.String");
347                         CheckDC (ccu.Namespaces [0].Types [1], "EchoResponse", args, "#i45");
348
349                         args.Clear ();
350                         args.Add ("it", "System.Int32");
351                         args.Add ("prefix", "System.String");
352                         CheckDC (ccu.Namespaces [0].Types [2], "DoubleIt", args, "#i46");
353
354                         args.Clear ();
355                         args.Add ("DoubleItResult", "System.String");
356                         CheckDC (ccu.Namespaces [0].Types [3], "DoubleItResponse", args, "#i47");
357
358                         /* ns: "" */
359                         args.Clear ();
360                         args.Add ("foo", "System.String");
361                         CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i48");
362
363                         ccu.Namespaces.Clear ();
364                         xsdi.Import (xss);
365                         //Importing same data contract again with same importer
366                         Assert.AreEqual (0, ccu.Namespaces.Count, "#i49");
367                 }
368
369                 [Test]
370                 public void ImportSkipArrayOfPrimitives ()
371                 {
372                         var ccu = new CodeCompileUnit ();
373                         var xdi = new XsdDataContractImporter (ccu);
374                         var xss = new XmlSchemaSet ();
375                         xss.Add (null, "Test/Resources/Schemas/schema1.xsd");
376                         xss.Add (null, "Test/Resources/Schemas/schema2.xsd");
377                         xdi.Import (xss);
378                         var sw = new StringWriter ();
379                         new CSharpCodeProvider ().GenerateCodeFromCompileUnit (ccu, sw, null);
380                         Assert.IsTrue (sw.ToString ().IndexOf ("ArrayOfint") < 0, "#1");
381                 }
382
383                 [Test]
384                 public void ImportGivesAppropriateNamespaces ()
385                 {
386                         var ccu = new CodeCompileUnit ();
387                         var xdi = new XsdDataContractImporter (ccu);
388                         var xss = new XmlSchemaSet ();
389                         xss.Add (null, "Test/Resources/Schemas/schema1.xsd");
390                         xss.Add (null, "Test/Resources/Schemas/schema2.xsd");
391                         xss.Add (null, "Test/Resources/Schemas/schema3.xsd");
392                         xdi.Import (xss);
393                         var sw = new StringWriter ();
394                         bool t = false, te = false;
395                         foreach (CodeNamespace cns in ccu.Namespaces) {
396                                 if (cns.Name == "tempuri.org")
397                                         t = true;
398                                 else if (cns.Name == "tempuri.org.ext")
399                                         te = true;
400                                 Assert.AreEqual ("GetSearchDataResponse", cns.Types [0].Name, "#1." + cns.Name);
401                         }
402                         Assert.IsTrue (t, "t");
403                         Assert.IsTrue (t, "te");
404                 }
405
406                 CodeCompileUnit DoImport (params string [] schemaFiles)
407                 {
408                         return DoImport (false, schemaFiles);
409                 }
410
411                 CodeCompileUnit DoImport (bool xmlType, params string [] schemaFiles)
412                 {
413                         var ccu = new CodeCompileUnit ();
414                         var xdi = new XsdDataContractImporter (ccu);
415                         if (xmlType)
416                                 xdi.Options = new ImportOptions () { ImportXmlType = true };
417                         var xss = new XmlSchemaSet ();
418                         foreach (var schemaFile in schemaFiles)
419                                 xss.Add (null, schemaFile);
420                         xdi.Import (xss);
421
422                         return ccu;
423                 }
424
425                 void DoCanImport (bool result, params string [] schemaFiles)
426                 {
427                         var ccu = new CodeCompileUnit ();
428                         var xdi = new XsdDataContractImporter (ccu);
429                         var xss = new XmlSchemaSet ();
430                         foreach (var schemaFile in schemaFiles)
431                                 xss.Add (null, schemaFile);
432                         Assert.AreEqual (result, xdi.CanImport (xss));
433                 }
434
435                 string GenerateCode (CodeCompileUnit ccu)
436                 {
437                         var sw = new StringWriter ();
438                         new CSharpCodeProvider ().GenerateCodeFromCompileUnit (ccu, sw, null);
439                         return sw.ToString ();
440                 }
441
442                 // FIXME: this set of tests need further assertion in each test case. Right now it just checks if things import or fail just fine.
443
444                 [Test]
445                 public void ImportTestX0 ()
446                 {
447                         DoImport ("Test/Resources/Schemas/ns0.xsd");
448                 }
449
450                 [Test]
451                 public void ImportTestX0_2 ()
452                 {
453                         var ccu = DoImport (true, "Test/Resources/Schemas/ns0.xsd");
454                         Assert.IsTrue (GenerateCode (ccu).IndexOf ("class") < 0, "#1");
455                 }
456
457                 [Test]
458                 public void ImportTestX1 ()
459                 {
460                         DoImport ("Test/Resources/Schemas/ns1.xsd");
461                 }
462
463                 [Test]
464                 public void ImportTestX1_2 ()
465                 {
466                         Assert.AreEqual (GenerateCode (DoImport ("Test/Resources/Schemas/ns1.xsd")), GenerateCode (DoImport (true, "Test/Resources/Schemas/ns1.xsd")), "#1");
467                 }
468
469                 [Test]
470                 public void ImportTestX2 ()
471                 {
472                         DoImport ("Test/Resources/Schemas/ns2.xsd");
473                 }
474
475                 [Test]
476                 public void ImportTestX2_2 ()
477                 {
478                         Assert.AreEqual (GenerateCode (DoImport ("Test/Resources/Schemas/ns2.xsd")), GenerateCode (DoImport (true, "Test/Resources/Schemas/ns2.xsd")), "#1");
479                 }
480
481                 [Test]
482                 [ExpectedException (typeof (InvalidDataContractException))]
483                 public void ImportTestX3 ()
484                 {
485                         DoImport ("Test/Resources/Schemas/ns3.xsd");
486                 }
487
488                 [Test]
489                 [Category ("NotWorking")]
490                 public void ImportTestX3_2 ()
491                 {
492                         var ccu = DoImport (true, "Test/Resources/Schemas/ns3.xsd");
493                         var code = GenerateCode (ccu);
494                         Assert.IsTrue (code.IndexOf ("class T2") > 0, "#1");
495                         Assert.IsTrue (code.IndexOf ("IXmlSerializable") > 0, "#2");
496                         Assert.IsTrue (code.IndexOf ("WriteXml") > 0, "#3");
497                         Assert.IsTrue (code.IndexOf ("XmlRootAttribute(ElementName=\"E2\", Namespace=\"urn:bar\"") > 0, "#4");
498                         Assert.IsTrue (code.IndexOf ("XmlSchemaProviderAttribute(\"ExportSchema\")") > 0, "#5");
499                 }
500
501                 [Test]
502                 [ExpectedException (typeof (InvalidDataContractException))]
503                 public void ImportTestX4 ()
504                 {
505                         DoImport ("Test/Resources/Schemas/ns4.xsd");
506                 }
507
508                 [Test]
509                 [ExpectedException (typeof (InvalidDataContractException))]
510                 public void ImportTestX5 ()
511                 {
512                         DoImport ("Test/Resources/Schemas/ns5.xsd");
513                 }
514
515                 [Test]
516                 public void ImportTestX6 ()
517                 {
518                         DoImport ("Test/Resources/Schemas/ns6.xsd",
519                                   "Test/Resources/Schemas/ns0.xsd");
520                 }
521
522                 [Test]
523                 [ExpectedException (typeof (InvalidDataContractException))]
524                 public void ImportTestX7 ()
525                 {
526                         DoImport ("Test/Resources/Schemas/ns7.xsd",
527                                   "Test/Resources/Schemas/ns0.xsd");
528                 }
529
530                 [Test]
531                 [ExpectedException (typeof (InvalidDataContractException))]
532                 public void ImportTestX8 ()
533                 {
534                         DoImport ("Test/Resources/Schemas/ns8.xsd");
535                 }
536
537                 [Test]
538                 [ExpectedException (typeof (InvalidDataContractException))]
539                 public void ImportTestX9 ()
540                 {
541                         DoImport ("Test/Resources/Schemas/ns9.xsd");
542                 }
543
544                 [Test]
545                 public void ImportTestX10 ()
546                 {
547                         DoImport ("Test/Resources/Schemas/ns10.xsd");
548                 }
549
550                 [Test]
551                 [ExpectedException (typeof (InvalidDataContractException))]
552                 public void ImportTestX11 ()
553                 {
554                         DoImport ("Test/Resources/Schemas/ns11.xsd");
555                 }
556
557                 [Test]
558                 [ExpectedException (typeof (InvalidDataContractException))]
559                 public void ImportTestX12 ()
560                 {
561                         DoImport ("Test/Resources/Schemas/ns12.xsd");
562                 }
563
564                 [Test]
565                 [ExpectedException (typeof (InvalidDataContractException))]
566                 public void ImportTestX13 ()
567                 {
568                         DoImport ("Test/Resources/Schemas/ns13.xsd");
569                 }
570
571                 [Test]
572                 public void ImportTestX14 ()
573                 {
574                         DoImport ("Test/Resources/Schemas/ns14.xsd");
575                 }
576
577                 [Test]
578                 [ExpectedException (typeof (InvalidDataContractException))]
579                 public void ImportTestX15 ()
580                 {
581                         DoImport ("Test/Resources/Schemas/ns15.xsd");
582                 }
583
584                 [Test]
585                 public void ImportTestX16 ()
586                 {
587                         DoImport ("Test/Resources/Schemas/ns16.xsd");
588                 }
589
590                 [Test]
591                 [ExpectedException (typeof (InvalidDataContractException))]
592                 public void ImportTestX17 ()
593                 {
594                         DoImport ("Test/Resources/Schemas/ns17.xsd");
595                 }
596
597                 [Test]
598                 public void ImportTestX18 ()
599                 {
600                         DoImport ("Test/Resources/Schemas/ns18.xsd");
601                 }
602
603                 [Test]
604                 [ExpectedException (typeof (InvalidDataContractException))]
605                 public void ImportTestX19 ()
606                 {
607                         DoImport ("Test/Resources/Schemas/ns19.xsd");
608                 }
609
610                 [Test]
611                 [ExpectedException (typeof (InvalidDataContractException))]
612                 public void ImportTestX20 ()
613                 {
614                         DoImport ("Test/Resources/Schemas/ns20.xsd");
615                 }
616
617                 [Test]
618                 public void ImportTestX21 ()
619                 {
620                         DoImport ("Test/Resources/Schemas/ns21.xsd");
621                 }
622
623                 [Test]
624                 public void ImportTestX22 ()
625                 {
626                         DoImport ("Test/Resources/Schemas/ns22.xsd");
627                 }
628
629                 [Test]
630                 public void ImportTestX23 ()
631                 {
632                         DoImport ("Test/Resources/Schemas/ns23.xsd");
633                 }
634
635                 [ExpectedException (typeof (InvalidDataContractException))]
636                 [Test]
637                 public void ImportTestX24 ()
638                 {
639                         DoImport ("Test/Resources/Schemas/ns24.xsd");
640                 }
641
642                 [Test]
643                 public void ImportTestX25 ()
644                 {
645                         DoImport ("Test/Resources/Schemas/ns25.xsd");
646                 }
647
648                 [Test]
649                 public void ImportTestX26 ()
650                 {
651                         DoImport ("Test/Resources/Schemas/ns26.xsd");
652                 }
653
654                 [Test]
655                 public void ImportTestX27 ()
656                 {
657                         DoImport ("Test/Resources/Schemas/ns27.xsd");
658                 }
659
660                 [Test]
661                 public void ImportTestX28 ()
662                 {
663                         DoImport ("Test/Resources/Schemas/ns28.xsd");
664                 }
665
666                 [Test]
667                 [ExpectedException (typeof (InvalidDataContractException))]
668                 public void ImportTestX29 ()
669                 {
670                         DoImport ("Test/Resources/Schemas/ns29.xsd");
671                 }
672
673                 [Test]
674                 public void ImportTestX30 ()
675                 {
676                         DoImport ("Test/Resources/Schemas/ns30.xsd");
677                 }
678
679                 [Test]
680                 public void ImportTestX31 ()
681                 {
682                         DoImport ("Test/Resources/Schemas/ns31.xsd");
683                 }
684
685                 [Test]
686                 public void ImportTestX32 ()
687                 {
688                         DoImport ("Test/Resources/Schemas/ns32.xsd");
689                 }
690
691                 [Test]
692                 public void ImportTestX33 ()
693                 {
694                         DoImport ("Test/Resources/Schemas/ns33.xsd");
695                 }
696
697                 [Test]
698                 public void ImportTestX34 ()
699                 {
700                         DoImport (true, "Test/Resources/Schemas/ns34.xsd", "Test/Resources/Schemas/ns34_2.xsd");
701                 }
702
703                 [Test]
704                 public void CanImportTestX34 ()
705                 {
706                         DoCanImport (false, "Test/Resources/Schemas/ns34.xsd", "Test/Resources/Schemas/ns34_2.xsd");
707                 }
708
709                 /* Helper methods */
710                 private void CheckDC (CodeTypeDeclaration type, string name, Dictionary<string, string> members, string msg)
711                 {
712                         // "dc" DataContract
713                         Assert.AreEqual (name, type.Name, msg + "d");
714                         //FIXME: Assert.AreEqual (MemberAttributes.Public, type.Attributes);
715                         Assert.IsTrue (type.IsClass, msg + "e");
716                         Assert.IsTrue (type.IsPartial, msg + "f");
717
718                         CheckDataContractAttribute (type, msg);
719                         CheckExtensionData (type, msg);
720
721                         foreach (KeyValuePair<string, string> pair in members)
722                                 CheckDataMember (type, pair.Key, pair.Value, true, msg);
723                 }
724
725                 private void CheckExtensionData (CodeTypeDeclaration type, string msg)
726                 {
727                         CheckDataMember (type, "extensionDataField", "ExtensionData", "System.Runtime.Serialization.ExtensionDataObject", false, msg);
728                 }
729
730                 private void CheckDataMember (CodeTypeDeclaration type, string name, string type_name, bool check_attr, string msg)
731                 {
732                         CheckDataMember (type, name + "Field", name, type_name, check_attr, msg);
733                 }
734                  
735                 private void CheckDataMember (CodeTypeDeclaration type, string field_name, string prop_name, string type_name, bool check_attr, string msg)
736                 {
737                         // "field"
738                         CodeMemberProperty p = FindMember (type, prop_name) as CodeMemberProperty;
739                         Assert.IsNotNull (p, msg + "-dm0");
740                         Assert.IsTrue (p.HasGet, msg + "-dm1");
741                         Assert.IsTrue (p.HasSet, msg + "-dm2");
742                         Assert.AreEqual (type_name, p.Type.BaseType, msg + "-dm3");
743
744                         CodeMemberField f = FindMember (type, field_name) as CodeMemberField;
745                         Assert.IsNotNull (f, msg + "-dm4");
746                         Assert.AreEqual (type_name, f.Type.BaseType, "-dm5");
747
748                         if (check_attr)
749                                 CheckDataContractAttribute (type, msg);
750                 }
751
752                 private void CheckDataContractAttribute (CodeTypeDeclaration type, string msg)
753                 {
754                         // DebuggerStepThrouAttribute is insignificant. So, no reason to check the attribute count.
755                         // Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
756
757                         // DebuggerStepThroughAttribute - skip it
758
759                         //GeneratedCodeAttribute
760                         var l = new List<CodeAttributeDeclaration> ();
761                         foreach (CodeAttributeDeclaration a in type.CustomAttributes)
762                                 l.Add (a);
763                         Assert.IsTrue (l.Any (a => a.Name == "System.CodeDom.Compiler.GeneratedCodeAttribute"), msg + "b");
764
765                         var ca = l.FirstOrDefault (a => a.Name == "System.Runtime.Serialization.DataContractAttribute");
766                         Assert.IsNotNull (ca, msg + "b");
767                         Assert.AreEqual (2, ca.Arguments.Count, msg + "d");
768                 }
769
770                 CodeTypeMember FindMember (CodeTypeDeclaration type, string name)
771                 {
772                         foreach (CodeTypeMember m in type.Members)
773                                 if (m.Name == name)
774                                         return m;
775                         return null;
776                 }
777
778                 [Test]
779                 public void ImportXsdBuiltInTypes ()
780                 {
781                         DoImport ("Test/Resources/Schemas/xml.xsd");
782                 }
783         }
784
785 }