2009-09-28 Atsushi Enomoto <atsushi@ximian.com>
[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.Collections.Generic;
37
38 using System.Xml;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
41
42 using System.CodeDom;
43 using System.CodeDom.Compiler;
44 using System.Runtime.Serialization;
45
46 using NUnit.Framework;
47 using System.ServiceModel.Description;
48
49 namespace MonoTests.System.Runtime.Serialization
50 {
51         [TestFixture]
52         public class XsdContractImporterTest
53         {
54                 MetadataSet metadata;
55                 XmlSchemaSet xss;
56
57                 [SetUp]
58                 public void Setup ()
59                 {
60                         XmlReader xr = XmlTextReader.Create ("Test/System.Runtime.Serialization/one.xml");
61                         metadata = MetadataSet.ReadFrom (xr);
62                         NewXmlSchemaSet ();
63                 }
64
65                 private XmlSchemaSet NewXmlSchemaSet ()
66                 {
67                         xss = new XmlSchemaSet ();
68                         foreach (MetadataSection section in metadata.MetadataSections)
69                                 if (section.Metadata is XmlSchema)
70                                         xss.Add (section.Metadata as XmlSchema);
71
72                         return xss;
73                 }
74
75                 private XsdDataContractImporter GetImporter ()
76                 {
77                         CodeCompileUnit ccu = new CodeCompileUnit ();
78                         return new XsdDataContractImporter (ccu);
79                 }
80
81                 [Test]
82                 public void CtorTest ()
83                 {
84                         XsdDataContractImporter xi = new XsdDataContractImporter ();
85                         Assert.IsNotNull (xi.CodeCompileUnit, "#c01");
86
87                         xi = new XsdDataContractImporter (null);
88                         Assert.IsNotNull (xi.CodeCompileUnit, "#c02");
89
90                         xi = new XsdDataContractImporter (new CodeCompileUnit ());
91                 }
92
93
94                 [Test]
95                 [ExpectedException (typeof (InvalidOperationException))]
96                 [Category ("NotWorking")]
97                 public void GetCodeTypeReferenceTest ()
98                 {
99                         XsdDataContractImporter xsdi = GetImporter ();
100                         xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
101                 }
102
103                 [Test]
104                 [Category ("NotWorking")]
105                 public void GetCodeTypeReferenceTest2 ()
106                 {
107                         NewXmlSchemaSet ();
108
109                         Assert.IsFalse (xss.IsCompiled, "#g01");
110
111                         XsdDataContractImporter xsdi = GetImporter ();
112                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
113                         Assert.IsTrue (xss.IsCompiled, "#g02");
114
115                         CodeTypeReference type = xsdi.GetCodeTypeReference (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
116
117                         //FIXME: How should this type ref be checked?
118                         Assert.IsNotNull (type, "#g03");
119                         Assert.AreEqual (type.BaseType, "dc", "#g04");
120                 }
121
122                 [Test]
123                 [Category ("NotWorking")]
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                 [Category ("NotWorking")]
150                 public void CanImportTest2 ()
151                 {
152                         NewXmlSchemaSet ();
153                         XsdDataContractImporter xsdi = GetImporter ();
154
155                         List<XmlQualifiedName> names = new List<XmlQualifiedName> ();
156                         names.Add (new XmlQualifiedName ("Echo", "http://myns/echo"));
157                         Assert.IsFalse (xsdi.CanImport (xss, names), "#ci20");
158
159                         names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
160                         Assert.IsFalse (xsdi.CanImport (xss, names), "#ci21");
161
162                         names.Clear ();
163                         names.Add (new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
164                         Assert.IsTrue (xsdi.CanImport (xss, names), "#ci22");
165                 }
166
167                 [Test]
168                 [ExpectedException (typeof (ArgumentNullException))]
169                 [Category ("NotWorking")]
170                 public void CanImportNullTest1 ()
171                 {
172                         XsdDataContractImporter xsdi = GetImporter ();
173                         xsdi.CanImport (null);
174                 }
175
176                 [Test]
177                 [Category ("NotWorking")]
178                 [ExpectedException (typeof (ArgumentNullException))]
179                 public void CanImportNullTest2 ()
180                 {
181                         XsdDataContractImporter xsdi = GetImporter ();
182                         xsdi.CanImport (xss, (XmlQualifiedName) null);
183                 }
184
185                 [Test]
186                 [Category ("NotWorking")]
187                 [ExpectedException (typeof (ArgumentNullException))]
188                 public void CanImportNullTest3 ()
189                 {
190                         XsdDataContractImporter xsdi = GetImporter ();
191                         xsdi.CanImport (xss, (XmlSchemaElement) null);
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (ArgumentNullException))]
196                 public void ImportTestNullSchemas ()
197                 {
198                         XsdDataContractImporter xsdi = GetImporter ();
199                         xsdi.Import (null);
200                 }
201
202                 [Test]
203                 [ExpectedException (typeof (ArgumentNullException))]
204                 public void ImportTestNullSchemas2 ()
205                 {
206                         XsdDataContractImporter xsdi = GetImporter ();
207                         xsdi.Import (null, new XmlQualifiedName ("foo"));
208                 }
209
210                 [Test]
211                 [ExpectedException (typeof (ArgumentNullException))]
212                 public void ImportTestNullSchemas3 ()
213                 {
214                         XsdDataContractImporter xsdi = GetImporter ();
215                         xsdi.Import (null, new List<XmlQualifiedName> ());
216                 }
217
218                 [Test]
219                 [ExpectedException (typeof (ArgumentNullException))]
220                 public void ImportTestNullTypeName ()
221                 {
222                         XsdDataContractImporter xsdi = GetImporter ();
223                         xsdi.Import (new XmlSchemaSet (), (XmlQualifiedName) null);
224                 }
225
226                 [Test]
227                 [ExpectedException (typeof (ArgumentNullException))]
228                 public void ImportTestNullElement ()
229                 {
230                         XsdDataContractImporter xsdi = GetImporter ();
231                         xsdi.Import (new XmlSchemaSet (), (XmlSchemaElement) null);
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentNullException))]
236                 public void ImportTestNullCollection ()
237                 {
238                         XsdDataContractImporter xsdi = GetImporter ();
239                         xsdi.Import (new XmlSchemaSet (), (ICollection<XmlQualifiedName>) null);
240                 }
241
242                 [Test]
243                 [Category ("NotWorking")]
244                 public void ImportTest ()
245                 {
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");
250
251                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
252                         Assert.AreEqual (1, ccu.Namespaces.Count, "#i03");
253                         Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i04");
254
255                         Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i05");
256
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");
260                 }
261
262                 [Test]
263                 [Category ("NotWorking")]
264                 public void ImportDataContract1 ()
265                 {
266                         NewXmlSchemaSet ();
267                         Assert.IsFalse (xss.IsCompiled, "#i20");
268
269                         XsdDataContractImporter xsdi = GetImporter ();
270
271                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
272                         Assert.IsTrue (xss.IsCompiled, "#i21");
273                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
274
275                         Assert.AreEqual (1, ccu.Namespaces.Count, "#i22");
276                         Assert.AreEqual ("", ccu.Namespaces [0].Name, "#i23");
277
278                         Assert.AreEqual (1, ccu.Namespaces [0].Types.Count, "#i24");
279
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");
283
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");
288                 }
289
290                 [Test]
291                 [Category ("NotWorking")]
292                 public void ImportDataContract2 ()
293                 {
294                         NewXmlSchemaSet ();
295                         XsdDataContractImporter xsdi = GetImporter ();
296
297                         xss.Compile ();
298                         xsdi.Import (xss, xss.GlobalElements [new XmlQualifiedName ("Echo", "http://myns/echo")] as XmlSchemaElement);
299                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
300
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");
306
307                         CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i30");
308
309                         args.Clear ();
310                         args.Add ("foo", "System.String");
311                         CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i31");
312
313                         ccu.Namespaces.Clear ();
314                         xsdi.Import (xss, new XmlQualifiedName ("dc", "http://schemas.datacontract.org/2004/07/"));
315                         Assert.AreEqual (0, ccu.Namespaces.Count);
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (InvalidDataContractException))]
320                 [Category ("NotWorking")]
321                 public void ImportMessageEcho ()
322                 {
323                         XsdDataContractImporter xsdi = GetImporter ();
324                         xsdi.Import (xss, new XmlQualifiedName ("Echo", "http://myns/echo"));
325                 }
326
327                 [Test]
328                 [Category ("NotWorking")]
329                 public void ImportAll ()
330                 {
331                         NewXmlSchemaSet ();
332                         XsdDataContractImporter xsdi = GetImporter ();
333
334                         CodeCompileUnit ccu = xsdi.CodeCompileUnit;
335                         xsdi.Import (xss);
336
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");
340
341                         Assert.AreEqual (4, ccu.Namespaces [0].Types.Count, "#i43");
342
343                         /* ns : myns.echo
344                          * Messages */
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");
349                         
350                         CheckDC (ccu.Namespaces [0].Types [0], "Echo", args, "#i44");
351
352                         args.Clear ();
353                         args.Add ("EchoResult", "System.String");
354                         CheckDC (ccu.Namespaces [0].Types [1], "EchoResponse", args, "#i45");
355
356                         args.Clear ();
357                         args.Add ("it", "System.Int32");
358                         args.Add ("prefix", "System.String");
359                         CheckDC (ccu.Namespaces [0].Types [2], "DoubleIt", args, "#i46");
360
361                         args.Clear ();
362                         args.Add ("DoubleItResult", "System.String");
363                         CheckDC (ccu.Namespaces [0].Types [3], "DoubleItResponse", args, "#i47");
364
365                         /* ns: "" */
366                         args.Clear ();
367                         args.Add ("foo", "System.String");
368                         CheckDC (ccu.Namespaces [1].Types [0], "dc", args, "#i48");
369
370                         ccu.Namespaces.Clear ();
371                         xsdi.Import (xss);
372                         //Importing same data contract again with same importer
373                         Assert.AreEqual (0, ccu.Namespaces.Count, "#i49");
374                 }
375
376                 /* Helper methods */
377                 private void CheckDC (CodeTypeDeclaration type, string name, Dictionary<string, string> members, string msg)
378                 {
379                         // "dc" DataContract
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");
384
385                         CheckDataContractAttribute (type, msg);
386                         CheckExtensionData (type, msg);
387
388                         foreach (KeyValuePair<string, string> pair in members)
389                                 CheckDataMember (type, pair.Key, pair.Value, true, msg);
390                 }
391
392                 private void CheckExtensionData (CodeTypeDeclaration type, string msg)
393                 {
394                         CheckDataMember (type, "extensionDataField", "ExtensionData", "System.Runtime.Serialization.ExtensionDataObject", false, msg);
395                 }
396
397                 private void CheckDataMember (CodeTypeDeclaration type, string name, string type_name, bool check_attr, string msg)
398                 {
399                         CheckDataMember (type, name + "Field", name, type_name, check_attr, msg);
400                 }
401                  
402                 private void CheckDataMember (CodeTypeDeclaration type, string field_name, string prop_name, string type_name, bool check_attr, string msg)
403                 {
404                         // "field"
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");
410
411                         CodeMemberField f = FindMember (type, field_name) as CodeMemberField;
412                         Assert.IsNotNull (f, msg + "-dm4");
413                         Assert.AreEqual (type_name, f.Type.BaseType, "-dm5");
414
415                         if (check_attr)
416                                 CheckDataContractAttribute (type, msg);
417                 }
418
419                 private void CheckDataContractAttribute (CodeTypeDeclaration type, string msg)
420                 {
421                         Assert.AreEqual (3, type.CustomAttributes.Count, msg + "a");
422
423                         // DebuggerStepThroughAttribute - skip it
424
425                         //GeneratedCodeAttribute
426                         CodeAttributeDeclaration ca = type.CustomAttributes [1];
427                         Assert.AreEqual ("System.CodeDom.Compiler.GeneratedCodeAttribute", ca.Name, msg + "b");
428
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");
432                 }
433
434                 CodeTypeMember FindMember (CodeTypeDeclaration type, string name)
435                 {
436                         foreach (CodeTypeMember m in type.Members)
437                                 if (m.Name == name)
438                                         return m;
439                         return null;
440                 }
441
442         }
443
444 }