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.
36 using System.Collections.Generic;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
43 using System.CodeDom.Compiler;
44 using System.Runtime.Serialization;
46 using NUnit.Framework;
47 using System.ServiceModel.Description;
49 namespace MonoTests.System.Runtime.Serialization
52 public class XsdContractImporterTest
60 XmlReader xr = XmlTextReader.Create ("Test/System.Runtime.Serialization/one.xml");
61 metadata = MetadataSet.ReadFrom (xr);
65 private XmlSchemaSet NewXmlSchemaSet ()
67 xss = new XmlSchemaSet ();
68 foreach (MetadataSection section in metadata.MetadataSections)
69 if (section.Metadata is XmlSchema)
70 xss.Add (section.Metadata as XmlSchema);
75 private XsdDataContractImporter GetImporter ()
77 CodeCompileUnit ccu = new CodeCompileUnit ();
78 return new XsdDataContractImporter (ccu);
82 public void CtorTest ()
84 XsdDataContractImporter xi = new XsdDataContractImporter ();
85 Assert.IsNotNull (xi.CodeCompileUnit, "#c01");
87 xi = new XsdDataContractImporter (null);
88 Assert.IsNotNull (xi.CodeCompileUnit, "#c02");
90 xi = new XsdDataContractImporter (new CodeCompileUnit ());
95 [ExpectedException (typeof (InvalidOperationException))]
96 [Category ("NotWorking")]
97 public void GetCodeTypeReferenceTest ()
99 XsdDataContractImporter xsdi = GetImporter ();
100 xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
104 [Category ("NotWorking")]
105 public void GetCodeTypeReferenceTest2 ()
109 Assert.IsFalse (xss.IsCompiled, "#g01");
111 XsdDataContractImporter xsdi = GetImporter ();
112 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
113 Assert.IsTrue (xss.IsCompiled, "#g02");
115 CodeTypeReference type = xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
117 //FIXME: How should this type ref be checked?
118 Assert.IsNotNull (type, "#g03");
119 Assert.AreEqual (type.BaseType, "dc", "#g04");
123 [Category ("NotWorking")]
124 public void CanImportTest ()
127 XsdDataContractImporter xsdi = GetImporter ();
129 Assert.IsFalse (xss.IsCompiled, "#ci01");
131 Assert.IsTrue (xsdi.CanImport (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")), "#ci02");
132 Assert.IsTrue (xss.IsCompiled, "#ci03");
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");
137 Assert.IsTrue (xsdi.CanImport (xss), "#ci06");
139 Assert.IsTrue (xsdi.CanImport (xss,
140 xss.GlobalElements [new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/")] as XmlSchemaElement),
143 Assert.IsTrue (xsdi.CanImport (xss,
144 xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement),
149 [Category ("NotWorking")]
150 public void CanImportTest2 ()
153 XsdDataContractImporter xsdi = GetImporter ();
155 List<XmlQualifiedName> names = new List<XmlQualifiedName> ();
156 names.Add (new XmlQualifiedName ("Echo", "http://myns/echo"));
157 Assert.IsFalse (xsdi.CanImport (xss, names), "#ci20");
159 names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
160 Assert.IsFalse (xsdi.CanImport (xss, names), "#ci21");
163 names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
164 Assert.IsTrue (xsdi.CanImport (xss, names), "#ci22");
168 [ExpectedException (typeof (ArgumentNullException))]
169 [Category ("NotWorking")]
170 public void CanImportNullTest1 ()
172 XsdDataContractImporter xsdi = GetImporter ();
173 xsdi.CanImport (null);
177 [Category ("NotWorking")]
178 [ExpectedException (typeof (ArgumentNullException))]
179 public void CanImportNullTest2 ()
181 XsdDataContractImporter xsdi = GetImporter ();
182 xsdi.CanImport (xss, (XmlQualifiedName) null);
186 [Category ("NotWorking")]
187 [ExpectedException (typeof (ArgumentNullException))]
188 public void CanImportNullTest3 ()
190 XsdDataContractImporter xsdi = GetImporter ();
191 xsdi.CanImport (xss, (XmlSchemaElement) null);
195 [ExpectedException (typeof (ArgumentNullException))]
196 public void ImportTestNullSchemas ()
198 XsdDataContractImporter xsdi = GetImporter ();
203 [ExpectedException (typeof (ArgumentNullException))]
204 public void ImportTestNullSchemas2 ()
206 XsdDataContractImporter xsdi = GetImporter ();
207 xsdi.Import (null, new XmlQualifiedName ("foo"));
211 [ExpectedException (typeof (ArgumentNullException))]
212 public void ImportTestNullSchemas3 ()
214 XsdDataContractImporter xsdi = GetImporter ();
215 xsdi.Import (null, new List<XmlQualifiedName> ());
219 [ExpectedException (typeof (ArgumentNullException))]
220 public void ImportTestNullTypeName ()
222 XsdDataContractImporter xsdi = GetImporter ();
223 xsdi.Import (new XmlSchemaSet (), (XmlQualifiedName) null);
227 [ExpectedException (typeof (ArgumentNullException))]
228 public void ImportTestNullElement ()
230 XsdDataContractImporter xsdi = GetImporter ();
231 xsdi.Import (new XmlSchemaSet (), (XmlSchemaElement) null);
235 [ExpectedException (typeof (ArgumentNullException))]
236 public void ImportTestNullCollection ()
238 XsdDataContractImporter xsdi = GetImporter ();
239 xsdi.Import (new XmlSchemaSet (), (ICollection<XmlQualifiedName>) null);
243 [Category ("NotWorking")]
244 public void ImportTest ()
246 XsdDataContractImporter xsdi = GetImporter ();
247 XmlSchemaElement element = new XmlSchemaElement();
248 //Assert.IsTrue (xsdi.CanImport (xss, element));
249 Assert.AreEqual (new XmlQualifiedName ("anyType", XmlSchema.Namespace), xsdi.Import (xss, element), "#i02");
251 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
252 Assert.AreEqual (1, ccu.Namespaces.Count, "#i03");
253 Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i04");
255 Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i05");
257 Dictionary<string, string> mbrs = new Dictionary<string, string> ();
258 mbrs.Add ("foo", "System.String");
259 CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i06");
263 [Category ("NotWorking")]
264 public void ImportDataContract1 ()
267 Assert.IsFalse (xss.IsCompiled, "#i20");
269 XsdDataContractImporter xsdi = GetImporter ();
271 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
272 Assert.IsTrue (xss.IsCompiled, "#i21");
273 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
275 Assert.AreEqual (1, ccu.Namespaces.Count, "#i22");
276 Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i23");
278 Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i24");
280 Dictionary<string, string> mbrs = new Dictionary<string, string> ();
281 mbrs.Add ("foo", "System.String");
282 CheckDC (ccu.Namespaces [0].Types [0], "dc", mbrs, "#i25");
284 ccu.Namespaces.Clear ();
285 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
286 //Importing same data contract again with same importer
287 Assert.AreEqual (0, ccu.Namespaces.Count, "#i26");
291 [Category ("NotWorking")]
292 public void ImportDataContract2 ()
295 XsdDataContractImporter xsdi = GetImporter ();
298 xsdi.Import (xss, xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement);
299 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
301 Assert.AreEqual (2, ccu.Namespaces.Count, "#i29");
302 Dictionary<string, string> args = new Dictionary<string, string> ();
303 args.Add ("msg", "System.String");
304 args.Add ("num", "System.Int32");
305 args.Add ("d", "dc");
307 CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i30");
310 args.Add ("foo", "System.String");
311 CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i31");
313 ccu.Namespaces.Clear ();
314 xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
315 Assert.AreEqual (0, ccu.Namespaces.Count);
319 [ExpectedException (typeof (InvalidDataContractException))]
320 [Category ("NotWorking")]
321 public void ImportMessageEcho ()
323 XsdDataContractImporter xsdi = GetImporter ();
324 xsdi.Import (xss, new XmlQualifiedName ("Echo", "http://myns/echo"));
328 [Category ("NotWorking")]
329 public void ImportAll ()
332 XsdDataContractImporter xsdi = GetImporter ();
334 CodeCompileUnit ccu = xsdi.CodeCompileUnit;
337 Assert.AreEqual (2, ccu.Namespaces.Count, "#i40");
338 Assert.AreEqual ("myns.echo", ccu.Namespaces [0].Name, "#i41");
339 Assert.AreEqual ("", ccu.Namespaces [1].Name, "#i42");
341 Assert.AreEqual (4, ccu.Namespaces [0].Types.Count, "#i43");
345 Dictionary <string, string> args = new Dictionary <string, string> ();
346 args.Add ("msg", "System.String");
347 args.Add ("num", "System.Int32");
348 args.Add ("d", "dc");
350 CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i44");
353 args.Add ("EchoResult", "System.String");
354 CheckDC (ccu.Namespaces [0].Types [1], "EchoResponse", args, "#i45");
357 args.Add ("it", "System.Int32");
358 args.Add ("prefix", "System.String");
359 CheckDC (ccu.Namespaces [0].Types [2], "DoubleIt", args, "#i46");
362 args.Add ("DoubleItResult", "System.String");
363 CheckDC (ccu.Namespaces [0].Types [3], "DoubleItResponse", args, "#i47");
367 args.Add ("foo", "System.String");
368 CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i48");
370 ccu.Namespaces.Clear ();
372 //Importing same data contract again with same importer
373 Assert.AreEqual (0, ccu.Namespaces.Count, "#i49");
377 private void CheckDC (CodeTypeDeclaration type, string name, Dictionary<string, string> members, string msg)
380 Assert.AreEqual (name, type.Name, msg + "d");
381 //FIXME: Assert.AreEqual (MemberAttributes.Public, type.Attributes);
382 Assert.IsTrue (type.IsClass, msg + "e");
383 Assert.IsTrue (type.IsPartial, msg + "f");
385 CheckDataContractAttribute (type, msg);
386 CheckExtensionData (type, msg);
388 foreach (KeyValuePair<string, string> pair in members)
389 CheckDataMember (type, pair.Key, pair.Value, true, msg);
392 private void CheckExtensionData (CodeTypeDeclaration type, string msg)
394 CheckDataMember (type, "extensionDataField", "ExtensionData", "System.Runtime.Serialization.ExtensionDataObject", false, msg);
397 private void CheckDataMember (CodeTypeDeclaration type, string name, string type_name, bool check_attr, string msg)
399 CheckDataMember (type, name + "Field", name, type_name, check_attr, msg);
402 private void CheckDataMember (CodeTypeDeclaration type, string field_name, string prop_name, string type_name, bool check_attr, string msg)
405 CodeMemberProperty p = FindMember (type, prop_name) as CodeMemberProperty;
406 Assert.IsNotNull (p, msg + "-dm0");
407 Assert.IsTrue (p.HasGet, msg + "-dm1");
408 Assert.IsTrue (p.HasSet, msg + "-dm2");
409 Assert.AreEqual (type_name, p.Type.BaseType, msg + "-dm3");
411 CodeMemberField f = FindMember (type, field_name) as CodeMemberField;
412 Assert.IsNotNull (f, msg + "-dm4");
413 Assert.AreEqual (type_name, f.Type.BaseType, "-dm5");
416 CheckDataContractAttribute (type, msg);
419 private void CheckDataContractAttribute (CodeTypeDeclaration type, string msg)
421 Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
423 // DebuggerStepThroughAttribute - skip it
425 //GeneratedCodeAttribute
426 CodeAttributeDeclaration ca = type.CustomAttributes [1];
427 Assert.AreEqual ("System.CodeDom.Compiler.GeneratedCodeAttribute", ca.Name, msg + "b");
429 ca = type.CustomAttributes [2];
430 Assert.AreEqual ("System.Runtime.Serialization.DataContractAttribute", ca.Name, msg + "c");
431 Assert.AreEqual (2, ca.Arguments.Count, msg + "d");
434 CodeTypeMember FindMember (CodeTypeDeclaration type, string name)
436 foreach (CodeTypeMember m in type.Members)