2 // XsdDataContractImporterTest.cs
5 // Ankit Jain <jankit@novell.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 // This test code contains tests for both DataContractSerializer and
32 // NetDataContractSerializer. The code could be mostly common.
37 using System.CodeDom.Compiler;
38 using System.Collections.Generic;
40 using System.Runtime.Serialization;
41 using System.ServiceModel.Description;
43 using System.Xml.Schema;
44 using System.Xml.Serialization;
45 using Microsoft.CSharp;
46 using NUnit.Framework;
48 namespace MonoTests.System.Runtime.Serialization
51 public class XsdContractImporterTest
59 XmlReader xr = XmlTextReader.Create ("Test/System.Runtime.Serialization/one.xml");
60 metadata = MetadataSet.ReadFrom (xr);
64 private XmlSchemaSet NewXmlSchemaSet ()
66 xss = new XmlSchemaSet ();
67 foreach (MetadataSection section in metadata.MetadataSections)
68 if (section.Metadata is XmlSchema)
69 xss.Add (section.Metadata as XmlSchema);
74 private XsdDataContractImporter GetImporter ()
76 CodeCompileUnit ccu = new CodeCompileUnit ();
77 return new XsdDataContractImporter (ccu);
81 public void CtorTest ()
83 XsdDataContractImporter xi = new XsdDataContractImporter ();
84 Assert.IsNotNull (xi.CodeCompileUnit, "#c01");
86 xi = new XsdDataContractImporter (null);
87 Assert.IsNotNull (xi.CodeCompileUnit, "#c02");
89 xi = new XsdDataContractImporter (new CodeCompileUnit ());
94 [ExpectedException (typeof (InvalidOperationException))]
95 [Category ("NotWorking")]
96 public void GetCodeTypeReferenceTest ()
98 XsdDataContractImporter xsdi = GetImporter ();
99 xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
103 [Category ("NotWorking")]
104 public void GetCodeTypeReferenceTest2 ()
108 Assert.IsFalse (xss.IsCompiled, "#g01");
110 XsdDataContractImporter xsdi = GetImporter ();
111 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
112 Assert.IsTrue (xss.IsCompiled, "#g02");
114 CodeTypeReference type = xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
116 //FIXME: How should this type ref be checked?
117 Assert.IsNotNull (type, "#g03");
118 Assert.AreEqual (type.BaseType, "dc", "#g04");
122 [Category ("NotWorking")]
123 public void CanImportTest ()
126 XsdDataContractImporter xsdi = GetImporter ();
128 Assert.IsFalse (xss.IsCompiled, "#ci01");
130 Assert.IsTrue (xsdi.CanImport (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")), "#ci02");
131 Assert.IsTrue (xss.IsCompiled, "#ci03");
133 Assert.IsFalse (xsdi.CanImport (xss, new XmlQualifiedName ("Echo", "http://myns/echo")), "#ci04");
134 Assert.IsTrue (xsdi.CanImport (xss, new XmlQualifiedName ("int", "http://www.w3.org/2001/XMLSchema")), "#ci05");
136 Assert.IsTrue (xsdi.CanImport (xss), "#ci06");
138 Assert.IsTrue (xsdi.CanImport (xss,
139 xss.GlobalElements [new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")] as XmlSchemaElement),
142 Assert.IsTrue (xsdi.CanImport (xss,
143 xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement),
148 [Category ("NotWorking")]
149 public void CanImportTest2 ()
152 XsdDataContractImporter xsdi = GetImporter ();
154 List<XmlQualifiedName> names = new List<XmlQualifiedName> ();
155 names.Add (new XmlQualifiedName ("Echo", "http://myns/echo"));
156 Assert.IsFalse (xsdi.CanImport (xss, names), "#ci20");
158 names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
159 Assert.IsFalse (xsdi.CanImport (xss, names), "#ci21");
162 names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
163 Assert.IsTrue (xsdi.CanImport (xss, names), "#ci22");
167 [ExpectedException (typeof (ArgumentNullException))]
168 [Category ("NotWorking")]
169 public void CanImportNullTest1 ()
171 XsdDataContractImporter xsdi = GetImporter ();
172 xsdi.CanImport (null);
176 [Category ("NotWorking")]
177 [ExpectedException (typeof (ArgumentNullException))]
178 public void CanImportNullTest2 ()
180 XsdDataContractImporter xsdi = GetImporter ();
181 xsdi.CanImport (xss, (XmlQualifiedName) null);
185 [Category ("NotWorking")]
186 [ExpectedException (typeof (ArgumentNullException))]
187 public void CanImportNullTest3 ()
189 XsdDataContractImporter xsdi = GetImporter ();
190 xsdi.CanImport (xss, (XmlSchemaElement) null);
194 [ExpectedException (typeof (ArgumentNullException))]
195 public void ImportTestNullSchemas ()
197 XsdDataContractImporter xsdi = GetImporter ();
202 [ExpectedException (typeof (ArgumentNullException))]
203 public void ImportTestNullSchemas2 ()
205 XsdDataContractImporter xsdi = GetImporter ();
206 xsdi.Import (null, new XmlQualifiedName ("foo"));
210 [ExpectedException (typeof (ArgumentNullException))]
211 public void ImportTestNullSchemas3 ()
213 XsdDataContractImporter xsdi = GetImporter ();
214 xsdi.Import (null, new List<XmlQualifiedName> ());
218 [ExpectedException (typeof (ArgumentNullException))]
219 public void ImportTestNullTypeName ()
221 XsdDataContractImporter xsdi = GetImporter ();
222 xsdi.Import (new XmlSchemaSet (), (XmlQualifiedName) null);
226 [ExpectedException (typeof (ArgumentNullException))]
227 public void ImportTestNullElement ()
229 XsdDataContractImporter xsdi = GetImporter ();
230 xsdi.Import (new XmlSchemaSet (), (XmlSchemaElement) null);
234 [ExpectedException (typeof (ArgumentNullException))]
235 public void ImportTestNullCollection ()
237 XsdDataContractImporter xsdi = GetImporter ();
238 xsdi.Import (new XmlSchemaSet (), (ICollection<XmlQualifiedName>) null);
242 [Category ("NotWorking")]
243 public void ImportTest ()
245 XsdDataContractImporter xsdi = GetImporter ();
246 XmlSchemaElement element = new XmlSchemaElement();
247 //Assert.IsTrue (xsdi.CanImport (xss, element));
248 Assert.AreEqual (new XmlQualifiedName ("anyType", XmlSchema.Namespace), xsdi.Import (xss, element), "#i02");
250 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
251 Assert.AreEqual (1, ccu.Namespaces.Count, "#i03");
252 Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i04");
254 Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i05");
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");
262 [Category ("NotWorking")]
263 public void ImportDataContract1 ()
266 Assert.IsFalse (xss.IsCompiled, "#i20");
268 XsdDataContractImporter xsdi = GetImporter ();
270 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
271 Assert.IsTrue (xss.IsCompiled, "#i21");
272 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
274 Assert.AreEqual (1, ccu.Namespaces.Count, "#i22");
275 Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i23");
277 Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i24");
279 Dictionary<string, string> mbrs = new Dictionary<string, string> ();
280 mbrs.Add ("foo", "System.String");
281 CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i25");
283 ccu.Namespaces.Clear ();
284 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
285 //Importing same data contract again with same importer
286 Assert.AreEqual (0, ccu.Namespaces.Count, "#i26");
290 [Category ("NotWorking")]
291 public void ImportDataContract2 ()
294 XsdDataContractImporter xsdi = GetImporter ();
297 xsdi.Import (xss, xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement);
298 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
300 Assert.AreEqual (2, ccu.Namespaces.Count, "#i29");
301 Dictionary<string, string> args = new Dictionary<string, string> ();
302 args.Add ("msg", "System.String");
303 args.Add ("num", "System.Int32");
304 args.Add ("d", "dc");
306 CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i30");
309 args.Add ("foo", "System.String");
310 CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i31");
312 ccu.Namespaces.Clear ();
313 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
314 Assert.AreEqual (0, ccu.Namespaces.Count);
318 [ExpectedException (typeof (InvalidDataContractException))]
319 [Category ("NotWorking")]
320 public void ImportMessageEcho ()
322 XsdDataContractImporter xsdi = GetImporter ();
323 xsdi.Import (xss, new XmlQualifiedName ("Echo", "http://myns/echo"));
327 [Category ("NotWorking")]
328 public void ImportAll ()
331 XsdDataContractImporter xsdi = GetImporter ();
333 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
336 Assert.AreEqual (2, ccu.Namespaces.Count, "#i40");
337 Assert.AreEqual ("myns.echo", ccu.Namespaces [0].Name, "#i41");
338 Assert.AreEqual ("", ccu.Namespaces [1].Name, "#i42");
340 Assert.AreEqual (4, ccu.Namespaces [0].Types.Count, "#i43");
344 Dictionary <string, string> args = new Dictionary <string, string> ();
345 args.Add ("msg", "System.String");
346 args.Add ("num", "System.Int32");
347 args.Add ("d", "dc");
349 CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i44");
352 args.Add ("EchoResult", "System.String");
353 CheckDC (ccu.Namespaces [0].Types [1], "EchoResponse", args, "#i45");
356 args.Add ("it", "System.Int32");
357 args.Add ("prefix", "System.String");
358 CheckDC (ccu.Namespaces [0].Types [2], "DoubleIt", args, "#i46");
361 args.Add ("DoubleItResult", "System.String");
362 CheckDC (ccu.Namespaces [0].Types [3], "DoubleItResponse", args, "#i47");
366 args.Add ("foo", "System.String");
367 CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i48");
369 ccu.Namespaces.Clear ();
371 //Importing same data contract again with same importer
372 Assert.AreEqual (0, ccu.Namespaces.Count, "#i49");
376 public void ImportSkipArrayOfPrimitives ()
378 var ccu = new CodeCompileUnit ();
379 var xdi = new XsdDataContractImporter (ccu);
380 var xss = new XmlSchemaSet ();
381 xss.Add (null, "Test/Resources/Schemas/schema1.xsd");
382 xss.Add (null, "Test/Resources/Schemas/schema2.xsd");
384 var sw = new StringWriter ();
385 new CSharpCodeProvider ().GenerateCodeFromCompileUnit (ccu, sw, null);
386 Assert.IsTrue (sw.ToString ().IndexOf ("ArrayOfint") < 0, "#1");
390 public void ImportGivesAppropriateNamespaces ()
392 var ccu = new CodeCompileUnit ();
393 var xdi = new XsdDataContractImporter (ccu);
394 var xss = new XmlSchemaSet ();
395 xss.Add (null, "Test/Resources/Schemas/schema1.xsd");
396 xss.Add (null, "Test/Resources/Schemas/schema2.xsd");
397 xss.Add (null, "Test/Resources/Schemas/schema3.xsd");
399 var sw = new StringWriter ();
400 bool t = false, te = false;
401 foreach (CodeNamespace cns in ccu.Namespaces) {
402 if (cns.Name == "tempuri.org")
404 else if (cns.Name == "tempuri.org.ext")
406 Assert.AreEqual ("GetSearchDataResponse", cns.Types [0].Name, "#1." + cns.Name);
408 Assert.IsTrue (t, "t");
409 Assert.IsTrue (t, "te");
412 CodeCompileUnit DoImport (params string [] schemaFiles)
414 var ccu = new CodeCompileUnit ();
415 var xdi = new XsdDataContractImporter (ccu);
416 var xss = new XmlSchemaSet ();
417 foreach (var schemaFile in schemaFiles)
418 xss.Add (null, schemaFile);
424 // FIXME: this set of tests need further assertion in each test case. Right now it just checks if things import or fail just fine.
427 public void ImportTestX0 ()
429 DoImport ("Test/Resources/Schemas/ns0.xsd");
433 public void ImportTestX1 ()
435 DoImport ("Test/Resources/Schemas/ns1.xsd");
439 public void ImportTestX2 ()
441 DoImport ("Test/Resources/Schemas/ns2.xsd");
445 [ExpectedException (typeof (InvalidDataContractException))]
446 public void ImportTestX3 ()
448 DoImport ("Test/Resources/Schemas/ns3.xsd");
452 [ExpectedException (typeof (InvalidDataContractException))]
453 public void ImportTestX4 ()
455 DoImport ("Test/Resources/Schemas/ns4.xsd");
459 [ExpectedException (typeof (InvalidDataContractException))]
460 public void ImportTestX5 ()
462 DoImport ("Test/Resources/Schemas/ns5.xsd");
466 public void ImportTestX6 ()
468 DoImport ("Test/Resources/Schemas/ns6.xsd",
469 "Test/Resources/Schemas/ns0.xsd");
473 [ExpectedException (typeof (InvalidDataContractException))]
474 public void ImportTestX7 ()
476 DoImport ("Test/Resources/Schemas/ns7.xsd",
477 "Test/Resources/Schemas/ns0.xsd");
481 [ExpectedException (typeof (InvalidDataContractException))]
482 public void ImportTestX8 ()
484 DoImport ("Test/Resources/Schemas/ns8.xsd");
488 [ExpectedException (typeof (InvalidDataContractException))]
489 public void ImportTestX9 ()
491 DoImport ("Test/Resources/Schemas/ns9.xsd");
495 public void ImportTestX10 ()
497 DoImport ("Test/Resources/Schemas/ns10.xsd");
501 [ExpectedException (typeof (InvalidDataContractException))]
502 public void ImportTestX11 ()
504 DoImport ("Test/Resources/Schemas/ns11.xsd");
508 [ExpectedException (typeof (InvalidDataContractException))]
509 public void ImportTestX12 ()
511 DoImport ("Test/Resources/Schemas/ns12.xsd");
515 [ExpectedException (typeof (InvalidDataContractException))]
516 public void ImportTestX13 ()
518 DoImport ("Test/Resources/Schemas/ns13.xsd");
522 public void ImportTestX14 ()
524 DoImport ("Test/Resources/Schemas/ns14.xsd");
528 [ExpectedException (typeof (InvalidDataContractException))]
529 public void ImportTestX15 ()
531 DoImport ("Test/Resources/Schemas/ns15.xsd");
535 public void ImportTestX16 ()
537 DoImport ("Test/Resources/Schemas/ns16.xsd");
541 [ExpectedException (typeof (InvalidDataContractException))]
542 public void ImportTestX17 ()
544 DoImport ("Test/Resources/Schemas/ns17.xsd");
548 public void ImportTestX18 ()
550 DoImport ("Test/Resources/Schemas/ns18.xsd");
554 [ExpectedException (typeof (InvalidDataContractException))]
555 public void ImportTestX19 ()
557 DoImport ("Test/Resources/Schemas/ns19.xsd");
561 [ExpectedException (typeof (InvalidDataContractException))]
562 public void ImportTestX20 ()
564 DoImport ("Test/Resources/Schemas/ns20.xsd");
568 public void ImportTestX21 ()
570 DoImport ("Test/Resources/Schemas/ns21.xsd");
574 public void ImportTestX22 ()
576 DoImport ("Test/Resources/Schemas/ns22.xsd");
580 public void ImportTestX23 ()
582 DoImport ("Test/Resources/Schemas/ns23.xsd");
585 [ExpectedException (typeof (InvalidDataContractException))]
587 public void ImportTestX24 ()
589 DoImport ("Test/Resources/Schemas/ns24.xsd");
593 public void ImportTestX25 ()
595 DoImport ("Test/Resources/Schemas/ns25.xsd");
599 public void ImportTestX26 ()
601 DoImport ("Test/Resources/Schemas/ns26.xsd");
605 public void ImportTestX27 ()
607 DoImport ("Test/Resources/Schemas/ns27.xsd");
611 public void ImportTestX28 ()
613 DoImport ("Test/Resources/Schemas/ns28.xsd");
617 [ExpectedException (typeof (InvalidDataContractException))]
618 public void ImportTestX29 ()
620 DoImport ("Test/Resources/Schemas/ns29.xsd");
624 public void ImportTestX30 ()
626 DoImport ("Test/Resources/Schemas/ns30.xsd");
630 public void ImportTestX31 ()
632 DoImport ("Test/Resources/Schemas/ns31.xsd");
636 private void CheckDC (CodeTypeDeclaration type, string name, Dictionary<string, string> members, string msg)
639 Assert.AreEqual (name, type.Name, msg + "d");
640 //FIXME: Assert.AreEqual (MemberAttributes.Public, type.Attributes);
641 Assert.IsTrue (type.IsClass, msg + "e");
642 Assert.IsTrue (type.IsPartial, msg + "f");
644 CheckDataContractAttribute (type, msg);
645 CheckExtensionData (type, msg);
647 foreach (KeyValuePair<string, string> pair in members)
648 CheckDataMember (type, pair.Key, pair.Value, true, msg);
651 private void CheckExtensionData (CodeTypeDeclaration type, string msg)
653 CheckDataMember (type, "extensionDataField", "ExtensionData", "System.Runtime.Serialization.ExtensionDataObject", false, msg);
656 private void CheckDataMember (CodeTypeDeclaration type, string name, string type_name, bool check_attr, string msg)
658 CheckDataMember (type, name + "Field", name, type_name, check_attr, msg);
661 private void CheckDataMember (CodeTypeDeclaration type, string field_name, string prop_name, string type_name, bool check_attr, string msg)
664 CodeMemberProperty p = FindMember (type, prop_name) as CodeMemberProperty;
665 Assert.IsNotNull (p, msg + "-dm0");
666 Assert.IsTrue (p.HasGet, msg + "-dm1");
667 Assert.IsTrue (p.HasSet, msg + "-dm2");
668 Assert.AreEqual (type_name, p.Type.BaseType, msg + "-dm3");
670 CodeMemberField f = FindMember (type, field_name) as CodeMemberField;
671 Assert.IsNotNull (f, msg + "-dm4");
672 Assert.AreEqual (type_name, f.Type.BaseType, "-dm5");
675 CheckDataContractAttribute (type, msg);
678 private void CheckDataContractAttribute (CodeTypeDeclaration type, string msg)
680 Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
682 // DebuggerStepThroughAttribute - skip it
684 //GeneratedCodeAttribute
685 CodeAttributeDeclaration ca = type.CustomAttributes [1];
686 Assert.AreEqual ("System.CodeDom.Compiler.GeneratedCodeAttribute", ca.Name, msg + "b");
688 ca = type.CustomAttributes [2];
689 Assert.AreEqual ("System.Runtime.Serialization.DataContractAttribute", ca.Name, msg + "c");
690 Assert.AreEqual (2, ca.Arguments.Count, msg + "d");
693 CodeTypeMember FindMember (CodeTypeDeclaration type, string name)
695 foreach (CodeTypeMember m in type.Members)