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