2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / Test / System.Xml.Serialization / XmlSerializerTests.cs
1 //\r
2 // System.Xml.XmlSerializerTests\r
3 //\r
4 // Author:\r
5 //   Erik LeBel <eriklebel@yahoo.ca>\r
6 //\r
7 // (C) 2003 Erik LeBel\r
8 //\r
9 //\r
10 // NOTES:\r
11 //  Where possible, these tests avoid testing the order of\r
12 //  an object's members serialization. Mono and .NET do not\r
13 //  reflect members in the same order.\r
14 //\r
15 //  Only serializations tests so far, no deserialization.\r
16 //\r
17 // FIXME\r
18 //  test XmlArrayAttribute\r
19 //  test XmlArrayItemAttribute\r
20 //  test serialization of decimal type\r
21 //  test serialization of Guid type\r
22 //  test XmlNode serialization with and without modifying attributes.\r
23 //  test deserialization\r
24 //  FIXMEs found in this file\r
25 \r
26 using System;\r
27 using System.Collections;\r
28 using System.IO;\r
29 using System.Text;\r
30 using System.Xml;\r
31 using System.Xml.Schema;\r
32 using System.Xml.Serialization;\r
33 \r
34 using NUnit.Framework;\r
35 \r
36 using MonoTests.System.Xml.TestClasses;\r
37 \r
38 namespace MonoTests.System.XmlSerialization\r
39 {\r
40         [TestFixture]\r
41         public class XmlSerializerTests : Assertion\r
42         {\r
43                 StringWriter sw;\r
44                 XmlTextWriter xtw;\r
45                 XmlSerializer xs;\r
46 \r
47                 private void SetUpWriter()\r
48                 {\r
49                         sw = new StringWriter ();\r
50                         xtw = new XmlTextWriter (sw);\r
51                         xtw.QuoteChar = '\'';\r
52                         xtw.Formatting = Formatting.None;\r
53                 }\r
54                 \r
55                 private string WriterText \r
56                 {\r
57                         get\r
58                         {\r
59                                 string val = sw.GetStringBuilder().ToString();\r
60                                 int offset = val.IndexOf('>') + 1;\r
61                                 val = val.Substring(offset);\r
62                                 return Infoset(val);\r
63                         }\r
64                 }\r
65 \r
66                 private void Serialize(object o)\r
67                 {\r
68                         SetUpWriter();\r
69                         xs = new XmlSerializer(o.GetType());\r
70                         xs.Serialize(xtw, o);\r
71                 }\r
72                 \r
73                 private void Serialize(object o, Type type)\r
74                 {\r
75                         SetUpWriter();\r
76                         xs = new XmlSerializer(type);\r
77                         xs.Serialize(xtw, o);\r
78                 }\r
79 \r
80                 private void Serialize(object o, XmlSerializerNamespaces ns)\r
81                 {\r
82                         SetUpWriter();\r
83                         xs = new XmlSerializer(o.GetType());\r
84                         xs.Serialize(xtw, o, ns);\r
85                 }\r
86 \r
87                 private void Serialize(object o, XmlAttributeOverrides ao)\r
88                 {\r
89                         SetUpWriter();\r
90                         xs = new XmlSerializer(o.GetType(), ao);\r
91                         xs.Serialize(xtw, o);\r
92                 }\r
93                 \r
94                 private void Serialize(object o, XmlRootAttribute root)\r
95                 {\r
96                         SetUpWriter();\r
97                         xs = new XmlSerializer(o.GetType(), root);\r
98                         xs.Serialize(xtw, o);\r
99                 }\r
100                 \r
101                 // test constructors\r
102 #if USE_VERSION_1_1     // It doesn't pass on MS.NET 1.1.\r
103                 [Test]\r
104                 public void TestConstructor()\r
105                 {\r
106                         XmlSerializer ser = new XmlSerializer (null, "");\r
107                 }\r
108 #else\r
109 #endif\r
110 \r
111                 // test basic types ////////////////////////////////////////////////////////\r
112                 [Test]\r
113                 public void TestSerializeInt()\r
114                 {\r
115                         Serialize(10);\r
116                         AssertEquals(Infoset("<int>10</int>"), WriterText);\r
117                 }\r
118 \r
119                 [Test]\r
120                 public void TestSerializeBool()\r
121                 {\r
122                         Serialize(true);\r
123                         AssertEquals(Infoset("<boolean>true</boolean>"), WriterText);\r
124                         \r
125                         Serialize(false);\r
126                         AssertEquals(Infoset("<boolean>false</boolean>"), WriterText);\r
127                 }\r
128                 \r
129                 [Test]\r
130                 public void TestSerializeString()\r
131                 {\r
132                         Serialize("hello");\r
133                         AssertEquals(Infoset("<string>hello</string>"), WriterText);\r
134                 }\r
135 \r
136                 [Test]\r
137                 public void TestSerializeEmptyString()\r
138                 {\r
139                         Serialize(String.Empty);\r
140                         AssertEquals(Infoset("<string />"), WriterText);\r
141                 }\r
142                 \r
143                 [Test]\r
144                 public void TestSerializeNullObject()\r
145                 {\r
146                         Serialize(null, typeof(object));\r
147                         AssertEquals(Infoset("<anyType xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);\r
148                 }\r
149 \r
150                 [Test]\r
151                 [Ignore ("The generated XML is not exact but it is equivalent")]\r
152                 public void TestSerializeNullString()\r
153                 {\r
154                         Serialize(null, typeof(string));\r
155                         Console.WriteLine (WriterText);\r
156                         AssertEquals (Infoset("<string xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />"), WriterText);\r
157                 }\r
158                         \r
159                 [Test]\r
160                 public void TestSerializeIntArray()\r
161                 {\r
162                         Serialize(new int[] {1, 2, 3, 4});\r
163                         AssertEquals (Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><int>1</int><int>2</int><int>3</int><int>4</int></ArrayOfInt>"), WriterText);\r
164                 }\r
165                 \r
166                 [Test]\r
167                 public void TestSerializeEmptyArray()\r
168                 {\r
169                         Serialize(new int[] {});\r
170                         AssertEquals(Infoset("<ArrayOfInt xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
171                 }\r
172                 \r
173                 [Test]\r
174                 public void TestSerializeChar()\r
175                 {\r
176                         Serialize('A');\r
177                         AssertEquals(Infoset("<char>65</char>"), WriterText);\r
178                         \r
179                         Serialize('\0');\r
180                         AssertEquals(Infoset("<char>0</char>"), WriterText);\r
181                         \r
182                         Serialize('\n');\r
183                         AssertEquals(Infoset("<char>10</char>"), WriterText);\r
184                         \r
185                         Serialize('\uFF01');\r
186                         AssertEquals(Infoset("<char>65281</char>"), WriterText);\r
187                 }\r
188                 \r
189                 [Test]\r
190                 public void TestSerializeFloat()\r
191                 {\r
192                         Serialize(10.78);\r
193                         AssertEquals(Infoset("<double>10.78</double>"), WriterText);\r
194                         \r
195                         Serialize(-1e8);\r
196                         AssertEquals(Infoset("<double>-100000000</double>"), WriterText);\r
197                         \r
198                         // FIXME test INF and other boundary conditions that may exist with floats\r
199                 }\r
200                 \r
201                 \r
202                 [Test]\r
203                 public void TestSerializeEnumeration()\r
204                 {\r
205                         Serialize(SimpleEnumeration.FIRST);\r
206                         AssertEquals(Infoset("<SimpleEnumeration>FIRST</SimpleEnumeration>"), WriterText);\r
207                         \r
208                         Serialize(SimpleEnumeration.SECOND);\r
209                         AssertEquals(Infoset("<SimpleEnumeration>SECOND</SimpleEnumeration>"), WriterText);\r
210                 }\r
211                 \r
212                 [Test]\r
213                 public void TestSerializeQualifiedName()\r
214                 {\r
215                         Serialize(new XmlQualifiedName("me", "home.urn"));\r
216                         AssertEquals(Infoset("<QName xmlns:q1='home.urn'>q1:me</QName>"), WriterText);\r
217                 }\r
218                 \r
219                 [Test]\r
220                 public void TestSerializeBytes()\r
221                 {\r
222                         Serialize((byte)0xAB);\r
223                         AssertEquals(Infoset("<unsignedByte>171</unsignedByte>"), WriterText);\r
224                         \r
225                         Serialize((byte)15);\r
226                         AssertEquals(Infoset("<unsignedByte>15</unsignedByte>"), WriterText);\r
227                 }\r
228                 \r
229                 [Test]\r
230                 public void TestSerializeByteArrays()\r
231                 {\r
232                         Serialize(new byte[] {});\r
233                         AssertEquals(Infoset("<base64Binary />"), WriterText);\r
234                         \r
235                         Serialize(new byte[] {0xAB, 0xCD});\r
236                         AssertEquals(Infoset("<base64Binary>q80=</base64Binary>"), WriterText);\r
237                 }\r
238                 \r
239                 [Test]\r
240                 public void TestSerializeDateTime()\r
241                 {\r
242                         DateTime d = new DateTime();\r
243                         Serialize(d);\r
244                         \r
245                         TimeZone tz = TimeZone.CurrentTimeZone;\r
246                         TimeSpan off = tz.GetUtcOffset (d);\r
247                         string sp = string.Format ("{0:00}:{1:00}", off.TotalHours, off.TotalMinutes%60);\r
248                         if (off.Ticks > 0) sp = "+" + sp;\r
249                         else sp = "-" + sp;\r
250 \r
251                         AssertEquals (Infoset("<dateTime>0001-01-01T00:00:00.0000000" + sp + "</dateTime>"), WriterText);\r
252                 }\r
253 \r
254                 /*\r
255                 FIXME\r
256                  - decimal\r
257                  - Guid\r
258                  - XmlNode objects\r
259                 \r
260                 [Test]\r
261                 public void TestSerialize()\r
262                 {\r
263                         Serialize();\r
264                         AssertEquals(WriterText, "");\r
265                 }\r
266                 */\r
267                 \r
268                 // test basic class serialization /////////////////////////////////////         \r
269                 [Test]\r
270                 public void TestSerializeSimpleClass()\r
271                 {\r
272                         SimpleClass simple = new SimpleClass();\r
273                         Serialize(simple);\r
274                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
275                         \r
276                         simple.something = "hello";\r
277                         \r
278                         Serialize(simple);\r
279                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);\r
280                 }\r
281                 \r
282                 [Test]\r
283                 public void TestSerializeStringCollection()\r
284                 {\r
285                         StringCollection strings = new StringCollection();\r
286                         Serialize(strings);\r
287                         AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
288                         \r
289                         strings.Add("hello");\r
290                         strings.Add("goodbye");\r
291                         Serialize(strings);\r
292                         AssertEquals(Infoset("<ArrayOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><string>hello</string><string>goodbye</string></ArrayOfString>"), WriterText);\r
293                         \r
294                 }\r
295                 \r
296                 [Test]\r
297                 public void TestSerializePlainContainer()\r
298                 {\r
299                         StringCollectionContainer container = new StringCollectionContainer();\r
300                         Serialize(container);\r
301                         AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages /></StringCollectionContainer>"), WriterText);\r
302                         \r
303                         container.Messages.Add("hello");\r
304                         container.Messages.Add("goodbye");\r
305                         Serialize(container);\r
306                         AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string><string>goodbye</string></Messages></StringCollectionContainer>"), WriterText);\r
307                 }\r
308 \r
309                 [Test]\r
310                 public void TestSerializeArrayContainer()\r
311                 {\r
312                         ArrayContainer container = new ArrayContainer();\r
313                         Serialize(container);\r
314                         AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);\r
315                         \r
316                         container.items = new object[] {10, 20};\r
317                         Serialize(container);\r
318                         AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:int'>20</anyType></items></ArrayContainer>"),WriterText);\r
319                         \r
320                         container.items = new object[] {10, "hello"};\r
321                         Serialize(container);\r
322                         AssertEquals(Infoset("<ArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><anyType xsi:type='xsd:int'>10</anyType><anyType xsi:type='xsd:string'>hello</anyType></items></ArrayContainer>"),WriterText);\r
323                 }\r
324                 \r
325                 [Test]\r
326                 public void TestSerializeClassArrayContainer()\r
327                 {\r
328                         ClassArrayContainer container = new ClassArrayContainer();\r
329                         Serialize(container);\r
330                         AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);\r
331                         \r
332                         SimpleClass simple1 = new SimpleClass();\r
333                         simple1.something = "hello";\r
334                         SimpleClass simple2 = new SimpleClass();\r
335                         simple2.something = "hello";\r
336                         container.items = new SimpleClass[2];\r
337                         container.items[0] = simple1;\r
338                         container.items[1] = simple2;\r
339                         Serialize(container);\r
340                         AssertEquals(Infoset("<ClassArrayContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass><something>hello</something></SimpleClass><SimpleClass><something>hello</something></SimpleClass></items></ClassArrayContainer>"),WriterText);\r
341                 }\r
342                 \r
343                 // test basic attributes ///////////////////////////////////////////////\r
344                 [Test]\r
345                 public void TestSerializeSimpleClassWithXmlAttributes()\r
346                 {\r
347                         SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();\r
348                         Serialize(simple);\r
349                         AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
350 \r
351                         simple.something = "hello";\r
352                         Serialize(simple);\r
353                         AssertEquals (Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' member='hello' />"), WriterText);\r
354                 }\r
355                 \r
356                 // test overrides ///////////////////////////////////////////////////////\r
357                 [Test]\r
358                 public void TestSerializeSimpleClassWithOverrides()\r
359                 {\r
360                         // Also tests XmlIgnore\r
361                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
362                         \r
363                         XmlAttributes attr = new XmlAttributes();\r
364                         attr.XmlIgnore = true;\r
365                         overrides.Add(typeof(SimpleClassWithXmlAttributes), "something", attr);\r
366                         \r
367                         SimpleClassWithXmlAttributes simple = new SimpleClassWithXmlAttributes();\r
368                         simple.something = "hello";\r
369                         Serialize(simple, overrides);\r
370                         AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
371                 }\r
372                 \r
373                 // test xmlText //////////////////////////////////////////////////////////\r
374                 [Test]\r
375                 public void TestSerializeXmlTextAttribute()\r
376                 {\r
377                         SimpleClass simple = new SimpleClass();\r
378                         simple.something = "hello";\r
379                         \r
380                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
381                         XmlAttributes attr = new XmlAttributes();\r
382                         overrides.Add(typeof(SimpleClass), "something", attr);\r
383                         \r
384                         attr.XmlText = new XmlTextAttribute();\r
385                         Serialize(simple, overrides);\r
386                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);\r
387                         \r
388                         attr.XmlText = new XmlTextAttribute(typeof(string));\r
389                         Serialize(simple, overrides);\r
390                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>hello</SimpleClass>"), WriterText);\r
391                         \r
392                         try\r
393                         {\r
394                                 attr.XmlText = new XmlTextAttribute(typeof(byte[]));\r
395                                 Serialize(simple, overrides);\r
396                                 Fail("XmlText.Type does not match the type it serializes: this should have failed");\r
397                         }\r
398                         catch (Exception)\r
399                         {\r
400                         }\r
401                         \r
402                         try\r
403                         {\r
404                                 attr.XmlText = new XmlTextAttribute();\r
405                                 attr.XmlText.DataType = "sometype";\r
406                                 Serialize(simple, overrides);\r
407                                 Fail("XmlText.DataType does not match the type it serializes: this should have failed");\r
408                         }\r
409                         catch (Exception)\r
410                         {\r
411                         }\r
412                 }\r
413                 \r
414                 // test xmlRoot //////////////////////////////////////////////////////////\r
415                 [Test]\r
416                 public void TestSerializeXmlRootAttribute()\r
417                 {\r
418                         // constructor override & element name\r
419                         XmlRootAttribute root = new XmlRootAttribute();\r
420                         root.ElementName = "renamed";\r
421                         \r
422                         SimpleClassWithXmlAttributes simpleWithAttributes = new SimpleClassWithXmlAttributes();\r
423                         Serialize(simpleWithAttributes, root);\r
424                         AssertEquals(Infoset("<renamed xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
425                         \r
426                         SimpleClass simple = null;\r
427                         root.IsNullable = false;\r
428                         try\r
429                         {\r
430                                 Serialize(simple, root);\r
431                                 Fail("Cannot serialize null object if XmlRoot's IsNullable == false");\r
432                         }\r
433                         catch (Exception)\r
434                         {\r
435                         }\r
436                         \r
437                         root.IsNullable = true;\r
438                         try\r
439                         {\r
440                                 Serialize(simple, root);\r
441                                 Fail("Cannot serialize null object if XmlRoot's IsNullable == true");\r
442                         }\r
443                         catch (Exception)\r
444                         {\r
445                         }\r
446                         \r
447                         simple = new SimpleClass();\r
448                         root.ElementName = null;\r
449                         root.Namespace = "some.urn";\r
450                         Serialize(simple, root);\r
451                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='some.urn' />"), WriterText);\r
452                 }\r
453                 \r
454                 [Test]\r
455                 public void TestSerializeXmlRootAttributeOnMember()\r
456                 {                       \r
457                         // nested root\r
458                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
459                         XmlAttributes childAttr = new XmlAttributes();\r
460                         childAttr.XmlRoot = new XmlRootAttribute("simple");\r
461                         overrides.Add(typeof(SimpleClass), childAttr);\r
462                         \r
463                         XmlAttributes attr = new XmlAttributes();\r
464                         attr.XmlRoot = new XmlRootAttribute("simple");\r
465                         overrides.Add(typeof(ClassArrayContainer), attr);\r
466                         \r
467                         ClassArrayContainer container = new ClassArrayContainer();\r
468                         container.items = new SimpleClass[1];\r
469                         container.items[0] = new SimpleClass();\r
470                         Serialize(container, overrides);\r
471                         AssertEquals(Infoset("<simple xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ><items><SimpleClass /></items></simple>"),WriterText);\r
472                         \r
473                         // FIXME test data type\r
474                 }\r
475                 \r
476                 // test XmlAttribute /////////////////////////////////////////////////////\r
477                 [Test]\r
478                 public void TestSerializeXmlAttributeAttribute()\r
479                 {       \r
480                         // null\r
481                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
482                         XmlAttributes attr = new XmlAttributes();\r
483                         attr.XmlAttribute = new XmlAttributeAttribute();\r
484                         overrides.Add(typeof(SimpleClass), "something", attr);\r
485                         \r
486                         SimpleClass simple = new SimpleClass();;\r
487                         Serialize(simple, overrides);\r
488                         AssertEquals("#1", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
489                         \r
490                         // regular\r
491                         simple.something = "hello";\r
492                         Serialize(simple, overrides);\r
493                         AssertEquals ("#2", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' something='hello' />"), WriterText);\r
494                         \r
495                         // AttributeName\r
496                         attr.XmlAttribute.AttributeName = "somethingelse";\r
497                         Serialize(simple, overrides);\r
498                         AssertEquals ("#3", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' somethingelse='hello' />"), WriterText);\r
499                         \r
500                         // Type\r
501                         // FIXME this should work, shouldnt it?\r
502                         // attr.XmlAttribute.Type = typeof(string);\r
503                         // Serialize(simple, overrides);\r
504                         // Assert(WriterText.EndsWith(" something='hello' />"));\r
505                         \r
506                         // Namespace\r
507                         attr.XmlAttribute.Namespace = "some:urn";\r
508                         Serialize(simple, overrides);\r
509                         AssertEquals ("#4", Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' d1p1:somethingelse='hello' xmlns:d1p1='some:urn' />"), WriterText);\r
510                         \r
511                         // FIXME DataType\r
512                         // FIXME XmlSchemaForm Form\r
513                         \r
514                         // FIXME write XmlQualifiedName as attribute\r
515                 }\r
516                 \r
517                 // test XmlElement ///////////////////////////////////////////////////////\r
518                 [Test]\r
519                 public void TestSerializeXmlElementAttribute()\r
520                 {\r
521                         \r
522                         \r
523                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
524                         XmlAttributes attr = new XmlAttributes();\r
525                         XmlElementAttribute element = new XmlElementAttribute();\r
526                         attr.XmlElements.Add(element);\r
527                         overrides.Add(typeof(SimpleClass), "something", attr);\r
528                         \r
529                         // null\r
530                         SimpleClass simple = new SimpleClass();;\r
531                         Serialize(simple, overrides);\r
532                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
533                         \r
534                         // not null\r
535                         simple.something = "hello";\r
536                         Serialize(simple, overrides);\r
537                         AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);\r
538                         \r
539                         //ElementName\r
540                         element.ElementName = "saying";\r
541                         Serialize(simple, overrides);\r
542                         AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);\r
543                         \r
544                         //IsNullable\r
545                         element.IsNullable = false;\r
546                         simple.something = null;\r
547                         Serialize(simple, overrides);\r
548                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);\r
549                         \r
550                         element.IsNullable = true;\r
551                         simple.something = null;\r
552                         Serialize(simple, overrides);\r
553                         AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText);\r
554                         \r
555                         //Namespace\r
556                         element.ElementName = null;\r
557                         element.IsNullable = false;\r
558                         element.Namespace = "some:urn";\r
559                         simple.something = "hello";\r
560                         Serialize(simple, overrides);\r
561                         AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText);\r
562                         \r
563                         //FIXME DataType\r
564                         //FIXME Form\r
565                         //FIXME Type\r
566                 }\r
567                 \r
568                 // test XmlElementAttribute with arrays and collections //////////////////\r
569                 [Test]\r
570                 public void TestSerializeCollectionWithXmlElementAttribute()\r
571                 {\r
572                         // the rule is:\r
573                         // if no type is specified or the specified type \r
574                         //    matches the contents of the collection, \r
575                         //    serialize each element in an element named after the member.\r
576                         // if the type does not match, or matches the collection itself,\r
577                         //    create a base wrapping element for the member, and then\r
578                         //    wrap each collection item in its own wrapping element based on type.\r
579                         \r
580                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
581                         XmlAttributes attr = new XmlAttributes();\r
582                         XmlElementAttribute element = new XmlElementAttribute();\r
583                         attr.XmlElements.Add(element);\r
584                         overrides.Add(typeof(StringCollectionContainer), "Messages", attr);\r
585                         \r
586                         // empty collection & no type info in XmlElementAttribute\r
587                         StringCollectionContainer container = new StringCollectionContainer();\r
588                         Serialize(container, overrides);\r
589                         AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
590                         \r
591                         // non-empty collection & no type info in XmlElementAttribute\r
592                         container.Messages.Add("hello");\r
593                         Serialize(container, overrides);\r
594                         AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);\r
595                         \r
596                         // non-empty collection & only type info in XmlElementAttribute\r
597                         element.Type = typeof(StringCollection);\r
598                         Serialize(container, overrides);\r
599                         AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText);\r
600                         \r
601                         // non-empty collection & only type info in XmlElementAttribute\r
602                         element.Type = typeof(string);\r
603                         Serialize(container, overrides);\r
604                         AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);\r
605                         \r
606                         // two elements\r
607                         container.Messages.Add("goodbye");\r
608                         element.Type = null;\r
609                         Serialize(container, overrides);\r
610                         AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText);\r
611                 }\r
612                 \r
613                 // test DefaultValue /////////////////////////////////////////////////////\r
614                 [Test]\r
615                 public void TestSerializeDefaultValueAttribute()\r
616                 {\r
617                         XmlAttributeOverrides overrides = new XmlAttributeOverrides();\r
618                         \r
619                         XmlAttributes attr = new XmlAttributes();\r
620                         string defaultValueInstance = "nothing";\r
621                         attr.XmlDefaultValue = defaultValueInstance;\r
622                         overrides.Add(typeof(SimpleClass), "something", attr);\r
623                         \r
624                         // use the default\r
625                         SimpleClass simple = new SimpleClass();\r
626                         Serialize(simple, overrides);\r
627                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
628                         \r
629                         // same value as default\r
630                         simple.something = defaultValueInstance;\r
631                         Serialize(simple, overrides);\r
632                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);\r
633                         \r
634                         // some other value\r
635                         simple.something = "hello";\r
636                         Serialize(simple, overrides);\r
637                         AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);\r
638                 }\r
639                 \r
640                 // test XmlEnum //////////////////////////////////////////////////////////\r
641                 [Test]\r
642                 public void TestSerializeXmlEnumAttribute()\r
643                 {\r
644                         // technically this has an XmlIgnore attribute, \r
645                         // but it is not being serialized as a member.\r
646                         Serialize(XmlSchemaForm.None);\r
647                         AssertEquals(Infoset("<XmlSchemaForm>0</XmlSchemaForm>"), WriterText);\r
648                         \r
649                         Serialize(XmlSchemaForm.Qualified);\r
650                         AssertEquals(Infoset("<XmlSchemaForm>qualified</XmlSchemaForm>"), WriterText);\r
651                         \r
652                         Serialize(XmlSchemaForm.Unqualified);\r
653                         AssertEquals(Infoset("<XmlSchemaForm>unqualified</XmlSchemaForm>"), WriterText);\r
654                 }\r
655                 \r
656                 public static string Infoset (string sx)\r
657                 {\r
658                         XmlDocument doc = new XmlDocument ();\r
659                         doc.LoadXml (sx);\r
660                         StringBuilder sb = new StringBuilder ();\r
661                         GetInfoset (doc.DocumentElement, sb);\r
662                         return sb.ToString ();\r
663                 }\r
664                 \r
665                 public static string Infoset (XmlNode nod)\r
666                 {\r
667                         StringBuilder sb = new StringBuilder ();\r
668                         GetInfoset (nod, sb);\r
669                         return sb.ToString ();\r
670                 }\r
671                 \r
672                 static void GetInfoset (XmlNode nod, StringBuilder sb)\r
673                 {\r
674                         switch (nod.NodeType)\r
675                         {\r
676                                 case XmlNodeType.Attribute:\r
677                                         if (nod.LocalName == "xmlns" && nod.NamespaceURI == "http://www.w3.org/2000/xmlns/") return;\r
678                                         sb.Append (" " + nod.NamespaceURI + ":" + nod.LocalName + "='" + nod.Value + "'");\r
679                                         break;\r
680                                         \r
681                                 case XmlNodeType.Element:\r
682                                         XmlElement elem = (XmlElement) nod;\r
683                                         sb.Append ("<" + elem.NamespaceURI + ":" + elem.LocalName);\r
684                                         \r
685                                         ArrayList ats = new ArrayList ();\r
686                                         foreach (XmlAttribute at in elem.Attributes)\r
687                                                 ats.Add (at.LocalName + " " + at.NamespaceURI);\r
688                                                 \r
689                                         ats.Sort ();\r
690                                                 \r
691                                         foreach (string name in ats)\r
692                                         {\r
693                                                 string[] nn = name.Split (' ');\r
694                                                 GetInfoset (elem.Attributes[nn[0],nn[1]], sb);\r
695                                         }\r
696                                                 \r
697                                         sb.Append (">");\r
698                                         foreach (XmlNode cn in elem.ChildNodes)\r
699                                                 GetInfoset (cn, sb);\r
700                                         sb.Append ("</>");\r
701                                         break;\r
702                                         \r
703                                 default:\r
704                                         sb.Append (nod.OuterXml);\r
705                                         break;\r
706                         }\r
707                 }\r
708         }\r
709 }\r