2 // System.Xml.DeserializationTests
5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 // Hagit Yidov <hagity@mainsoft.com>
8 // (C) 2003 Atsushi Enomoto
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 using System.Globalization;
16 using System.Xml.Serialization;
17 using NUnit.Framework;
18 using MonoTests.System.Xml.TestClasses;
20 namespace MonoTests.System.XmlSerialization
25 public string[] ArrayText;
29 public class DeserializationTests
31 const string XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema";
32 const string XmlSchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
33 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
34 const string WsdlTypesNamespace = "http://microsoft.com/wsdl/types/";
35 const string ANamespace = "some:urn";
36 const string AnotherNamespace = "another:urn";
40 private object Deserialize (Type t, string xml)
42 StringReader sr = new StringReader (xml);
43 XmlReader xr = new XmlTextReader (sr);
44 return Deserialize (t, xr);
47 private object Deserialize (Type t, string xml, string defaultNamespace)
49 StringReader sr = new StringReader (xml);
50 XmlReader xr = new XmlTextReader (sr);
51 return Deserialize (t, xr, defaultNamespace);
54 private object Deserialize (Type t, string xml, XmlAttributeOverrides ao)
56 StringReader sr = new StringReader (xml);
57 XmlReader xr = new XmlTextReader (sr);
58 return Deserialize (t, xr, ao);
61 private object DeserializeEncoded (Type t, string xml)
63 StringReader sr = new StringReader (xml);
64 XmlReader xr = new XmlTextReader (sr);
65 return DeserializeEncoded (t, xr);
68 private object Deserialize (Type t, XmlReader xr)
70 XmlSerializer ser = new XmlSerializer (t);
71 result = ser.Deserialize (xr);
75 private object Deserialize (Type t, XmlReader xr, string defaultNamespace)
77 XmlSerializer ser = new XmlSerializer (t, defaultNamespace);
78 result = ser.Deserialize (xr);
82 private object Deserialize (Type t, XmlReader xr, XmlAttributeOverrides ao)
84 XmlSerializer ser = new XmlSerializer (t, ao);
85 result = ser.Deserialize (xr);
89 private object DeserializeEncoded (Type t, XmlReader xr)
91 SoapReflectionImporter im = new SoapReflectionImporter ();
92 XmlTypeMapping tm = im.ImportTypeMapping (t);
93 XmlSerializer ser = new XmlSerializer (tm);
94 result = ser.Deserialize (xr);
99 public void SimpleDeserialize ()
101 Deserialize (typeof (Sample), "<Sample><Text>Test.</Text></Sample>");
102 Assert.AreEqual (typeof (Sample), result.GetType ());
103 Sample sample = result as Sample;
104 Assert.AreEqual ("Test.", sample.Text);
108 public void DeserializeInt ()
110 Deserialize (typeof (int), "<int>10</int>");
111 Assert.AreEqual (typeof (int), result.GetType ());
112 Assert.AreEqual (10, result);
116 public void DeserializeSimpleArray ()
118 Deserialize (typeof (Sample), "<Sample><ArrayText><string>Test1</string><string>Test2</string></ArrayText></Sample>");
119 Assert.AreEqual (typeof (Sample), result.GetType ());
120 Sample sample = result as Sample;
121 Assert.AreEqual ("Test1", sample.ArrayText[0]);
122 Assert.AreEqual ("Test2", sample.ArrayText[1]);
126 public void DeserializeEmptyEnum ()
128 Field f = Deserialize (typeof (Field), "<field modifiers=\"\" />") as Field;
129 Assert.AreEqual (MapModifiers.Public, f.Modifiers);
133 public void DeserializePrivateCollection ()
135 MemoryStream ms = new MemoryStream ();
136 Container c = new Container ();
139 XmlSerializer serializer = new XmlSerializer (typeof (Container));
140 serializer.Serialize (ms, c);
143 c = (Container) serializer.Deserialize (ms);
144 Assert.AreEqual (1, c.Items[0]);
148 [Category ("NotDotNet")]
149 [ExpectedException (typeof (InvalidOperationException))]
150 public void DeserializeEmptyPrivateCollection ()
152 MemoryStream ms = new MemoryStream ();
153 Container2 c = new Container2 (true);
156 XmlSerializer serializer = new XmlSerializer (typeof (Container2));
157 serializer.Serialize (ms, c);
160 c = (Container2) serializer.Deserialize (ms);
164 [Category ("NotDotNet")]
165 public void DeserializeArrayReferences ()
167 string s = "<Sample xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
168 s += "<ArrayText xmlns:n3=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"n3:Array\" n3:arrayType=\"xsd:string[2]\">";
169 s += "<item href=\"#id-606830706\"></item>";
170 s += "<item xsi:type=\"xsd:string\">Hola</item>";
172 s += "<string id=\"id-606830706\" xsi:type=\"xsd:string\">Adeu</string>";
174 DeserializeEncoded (typeof (Sample), s);
179 public void TestDeserializeXmlNodeArray ()
181 object ob = Deserialize (typeof (object), "<anyType at=\"1\"><elem1/><elem2/></anyType>");
182 Assert.IsTrue (ob is XmlNode[], "Is node array");
184 XmlNode[] nods = (XmlNode[]) ob;
185 Assert.AreEqual (3, nods.Length, "lengh");
186 Assert.IsTrue (nods[0] is XmlAttribute, "#1");
187 Assert.AreEqual ("at", ((XmlAttribute) nods[0]).LocalName, "#2");
188 Assert.AreEqual ("1", ((XmlAttribute) nods[0]).Value, "#3");
189 Assert.IsTrue (nods[1] is XmlElement, "#4");
190 Assert.AreEqual ("elem1", ((XmlElement) nods[1]).LocalName, "#5");
191 Assert.IsTrue (nods[2] is XmlElement, "#6");
192 Assert.AreEqual ("elem2", ((XmlElement) nods[2]).LocalName, "#7");
196 public void TestDeserializeXmlElement ()
198 object ob = Deserialize (typeof (XmlElement), "<elem/>");
199 Assert.IsTrue (ob is XmlElement, "#1");
200 Assert.AreEqual ("elem", ((XmlElement) ob).LocalName, "#2");
204 public void TestDeserializeXmlCDataSection ()
206 CDataContainer c = (CDataContainer) Deserialize (typeof (CDataContainer), "<CDataContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><cdata><![CDATA[data section contents]]></cdata></CDataContainer>");
207 Assert.IsNotNull (c.cdata, "#1");
208 Assert.AreEqual ("data section contents", c.cdata.Value, "#2");
212 public void TestDeserializeXmlNode ()
214 NodeContainer c = (NodeContainer) Deserialize (typeof (NodeContainer), "<NodeContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><node>text</node></NodeContainer>");
215 Assert.IsTrue (c.node is XmlText, "#1");
216 Assert.AreEqual ("text", c.node.Value, "#2");
220 public void TestDeserializeChoices ()
222 Choices ch = (Choices) Deserialize (typeof (Choices), "<Choices><ChoiceZero>choice text</ChoiceZero></Choices>");
223 Assert.AreEqual ("choice text", ch.MyChoice, "#A1");
224 Assert.AreEqual (ItemChoiceType.ChoiceZero, ch.ItemType, "#A2");
226 ch = (Choices) Deserialize (typeof (Choices), "<Choices><ChoiceOne>choice text</ChoiceOne></Choices>");
227 Assert.AreEqual ("choice text", ch.MyChoice, "#B1");
228 Assert.AreEqual (ItemChoiceType.StrangeOne, ch.ItemType, "#B2");
230 ch = (Choices) Deserialize (typeof (Choices), "<Choices><ChoiceTwo>choice text</ChoiceTwo></Choices>");
231 Assert.AreEqual ("choice text", ch.MyChoice, "#C1");
232 Assert.AreEqual (ItemChoiceType.ChoiceTwo, ch.ItemType, "#C2");
236 public void TestDeserializeNamesWithSpaces ()
238 TestSpace ts = (TestSpace) Deserialize (typeof (TestSpace), "<Type_x0020_with_x0020_space xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Attribute_x0020_with_x0020_space='5'><Element_x0020_with_x0020_space>4</Element_x0020_with_x0020_space></Type_x0020_with_x0020_space>");
239 Assert.AreEqual (4, ts.elem, "#1");
240 Assert.AreEqual (5, ts.attr, "#2");
244 public void TestDeserializeDefaults ()
246 ListDefaults d2 = (ListDefaults) Deserialize (typeof (ListDefaults), "<root/>");
248 Assert.IsNotNull (d2.list2, "#A1");
249 Assert.IsNull (d2.list3, "#A2");
250 Assert.IsNull (d2.list4, "#A3");
251 Assert.IsNotNull (d2.list5, "#A4");
252 Assert.IsNotNull (d2.ed, "#A5");
253 Assert.IsNotNull (d2.str, "#A6");
255 d2 = (ListDefaults) Deserialize (typeof (ListDefaults), "<root></root>");
257 Assert.IsNotNull (d2.list2, "#B1");
258 Assert.IsNull (d2.list3, "#B2");
259 Assert.IsNull (d2.list4, "#B3");
260 Assert.IsNotNull (d2.list5, "#B4");
261 Assert.IsNotNull (d2.ed, "#B5");
262 Assert.IsNotNull (d2.str, "#B6");
266 public void TestDeserializeChoiceArray ()
268 CompositeValueType v = (CompositeValueType) Deserialize (typeof (CompositeValueType), "<?xml version=\"1.0\" encoding=\"utf-16\"?><CompositeValueType xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><In>1</In><Es>2</Es></CompositeValueType>");
269 Assert.IsNotNull (v.Items, "#1");
270 Assert.IsNotNull (v.ItemsElementName, "#2");
271 Assert.AreEqual (2, v.Items.Length, "#3");
272 Assert.AreEqual (2, v.ItemsElementName.Length, "#4");
273 Assert.AreEqual (1, v.Items[0], "#5");
274 Assert.AreEqual (2, v.Items[1], "#6");
275 Assert.AreEqual (ItemsChoiceType.In, v.ItemsElementName[0], "#7");
276 Assert.AreEqual (ItemsChoiceType.Es, v.ItemsElementName[1], "#8");
279 #region GenericsDeseralizationTests
283 public void TestDeserializeGenSimpleClassString ()
285 Deserialize (typeof (GenSimpleClass<string>), "<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />");
286 Assert.AreEqual (typeof (GenSimpleClass<string>), result.GetType ());
287 Deserialize (typeof (GenSimpleClass<string>), "<GenSimpleClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></GenSimpleClassOfString>");
288 GenSimpleClass<string> simple = result as GenSimpleClass<string>;
289 Assert.AreEqual ("hello", simple.something);
293 public void TestDeserializeGenSimpleClassBool ()
295 Deserialize (typeof (GenSimpleClass<bool>), "<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>false</something></GenSimpleClassOfBoolean>");
296 Assert.AreEqual (typeof (GenSimpleClass<bool>), result.GetType ());
297 Deserialize (typeof (GenSimpleClass<bool>), "<GenSimpleClassOfBoolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>true</something></GenSimpleClassOfBoolean>");
298 GenSimpleClass<bool> simple = result as GenSimpleClass<bool>;
299 Assert.AreEqual (true, simple.something);
303 public void TestDeserializeGenSimpleStructInt ()
305 Deserialize (typeof (GenSimpleStruct<int>), "<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something></GenSimpleStructOfInt32>");
306 Assert.AreEqual (typeof (GenSimpleStruct<int>), result.GetType ());
307 Deserialize (typeof (GenSimpleStruct<int>), "<GenSimpleStructOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something></GenSimpleStructOfInt32>");
308 GenSimpleStruct<int> simple = new GenSimpleStruct<int> (0);
310 simple = (GenSimpleStruct<int>) result;
311 Assert.AreEqual (123, simple.something);
315 public void TestDeserializeGenListClassString ()
317 Deserialize (typeof (GenListClass<string>), "<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfString>");
318 Assert.AreEqual (typeof (GenListClass<string>), result.GetType ());
319 Deserialize (typeof (GenListClass<string>), "<GenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><string>Value1</string><string>Value2</string></somelist></GenListClassOfString>");
320 GenListClass<string> genlist = result as GenListClass<string>;
321 Assert.AreEqual ("Value1", genlist.somelist[0]);
322 Assert.AreEqual ("Value2", genlist.somelist[1]);
326 public void TestDeserializeGenListClassFloat ()
328 Deserialize (typeof (GenListClass<float>), "<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfSingle>");
329 Assert.AreEqual (typeof (GenListClass<float>), result.GetType ());
330 Deserialize (typeof (GenListClass<float>), "<GenListClassOfSingle xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><float>1</float><float>2.2</float></somelist></GenListClassOfSingle>");
331 GenListClass<float> genlist = result as GenListClass<float>;
332 Assert.AreEqual (1, genlist.somelist[0]);
333 Assert.AreEqual (2.2F, genlist.somelist[1]);
337 public void TestDeserializeGenListClassList ()
339 Deserialize (typeof (GenListClass<GenListClass<int>>), "<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInt32>");
340 Assert.AreEqual (typeof (GenListClass<GenListClass<int>>), result.GetType ());
341 Deserialize (typeof (GenListClass<GenListClass<int>>), "<GenListClassOfGenListClassOfInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInt32><somelist><int>1</int><int>2</int></somelist></GenListClassOfInt32><GenListClassOfInt32><somelist><int>10</int><int>20</int></somelist></GenListClassOfInt32></somelist></GenListClassOfGenListClassOfInt32>");
342 GenListClass<GenListClass<int>> genlist = result as GenListClass<GenListClass<int>>;
343 Assert.AreEqual (1, genlist.somelist[0].somelist[0]);
344 Assert.AreEqual (2, genlist.somelist[0].somelist[1]);
345 Assert.AreEqual (10, genlist.somelist[1].somelist[0]);
346 Assert.AreEqual (20, genlist.somelist[1].somelist[1]);
350 public void TestDeserializeGenListClassArray ()
352 Deserialize (typeof (GenListClass<GenArrayClass<char>>), "<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenArrayClassOfChar>");
353 Assert.AreEqual (typeof (GenListClass<GenArrayClass<char>>), result.GetType ());
354 Deserialize (typeof (GenListClass<GenArrayClass<char>>), "<GenListClassOfGenArrayClassOfChar xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenArrayClassOfChar><arr><char>97</char><char>98</char><char>0</char></arr></GenArrayClassOfChar><GenArrayClassOfChar><arr><char>100</char><char>101</char><char>102</char></arr></GenArrayClassOfChar></somelist></GenListClassOfGenArrayClassOfChar>");
355 GenListClass<GenArrayClass<char>> genlist = result as GenListClass<GenArrayClass<char>>;
356 Assert.AreEqual ('a', genlist.somelist[0].arr[0]);
357 Assert.AreEqual ('b', genlist.somelist[0].arr[1]);
358 Assert.AreEqual ('d', genlist.somelist[1].arr[0]);
359 Assert.AreEqual ('e', genlist.somelist[1].arr[1]);
360 Assert.AreEqual ('f', genlist.somelist[1].arr[2]);
364 public void TestDeserializeGenTwoClassCharDouble ()
366 Deserialize (typeof (GenTwoClass<char, double>), "<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2></GenTwoClassOfCharDouble>");
367 Assert.AreEqual (typeof (GenTwoClass<char, double>), result.GetType ());
368 Deserialize (typeof (GenTwoClass<char, double>), "<GenTwoClassOfCharDouble xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>97</something1><something2>2.2</something2></GenTwoClassOfCharDouble>");
369 GenTwoClass<char, double> gentwo = result as GenTwoClass<char, double>;
370 Assert.AreEqual ('a', gentwo.something1);
371 Assert.AreEqual (2.2, gentwo.something2);
375 public void TestDeserializeGenDerivedClassDecimalShort ()
377 Deserialize (typeof (GenDerivedClass<decimal, short>), "<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something2>0</something2><another1>0</another1><another2>0</another2></GenDerivedClassOfDecimalInt16>");
378 Assert.AreEqual (typeof (GenDerivedClass<decimal, short>), result.GetType ());
379 Deserialize (typeof (GenDerivedClass<decimal, short>), "<GenDerivedClassOfDecimalInt16 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>Value1</something1><something2>1</something2><another1>1.1</another1><another2>-22</another2></GenDerivedClassOfDecimalInt16>");
380 GenDerivedClass<decimal, short> derived = result as GenDerivedClass<decimal, short>;
381 Assert.AreEqual ("Value1", derived.something1);
382 Assert.AreEqual (1, derived.something2);
383 Assert.AreEqual (1.1M, derived.another1);
384 Assert.AreEqual (-22, derived.another2);
388 public void TestDeserializeGenDerivedSecondClassByteUlong ()
390 Deserialize (typeof (GenDerived2Class<byte, ulong>), "<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>0</something1><something2>0</something2><another1>0</another1><another2>0</another2></GenDerived2ClassOfByteUInt64>");
391 Assert.AreEqual (typeof (GenDerived2Class<byte, ulong>), result.GetType ());
392 Deserialize (typeof (GenDerived2Class<byte, ulong>), "<GenDerived2ClassOfByteUInt64 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something1>1</something1><something2>222</something2><another1>111</another1><another2>222222</another2></GenDerived2ClassOfByteUInt64>");
393 GenDerived2Class<byte, ulong> derived2 = result as GenDerived2Class<byte, ulong>;
394 Assert.AreEqual (1, derived2.something1);
395 Assert.AreEqual (222, derived2.something2);
396 Assert.AreEqual (111, derived2.another1);
397 Assert.AreEqual (222222, derived2.another2);
401 public void TestDeserializeGenNestedClass ()
403 Deserialize (typeof (GenNestedClass<string, int>.InnerClass<bool>), "<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>0</inner><something>false</something></InnerClassOfStringInt32Boolean>");
404 Assert.AreEqual (typeof (GenNestedClass<string, int>.InnerClass<bool>), result.GetType ());
405 Deserialize (typeof (GenNestedClass<string, int>.InnerClass<bool>), "<InnerClassOfStringInt32Boolean xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><inner>5</inner><something>true</something></InnerClassOfStringInt32Boolean>");
406 GenNestedClass<string, int>.InnerClass<bool> nested = result as GenNestedClass<string, int>.InnerClass<bool>;
407 Assert.AreEqual (5, nested.inner);
408 Assert.AreEqual (true, nested.something);
412 public void TestDeserializeGenListClassListNested ()
414 Deserialize (typeof (GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>>),
415 "<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>");
416 Assert.AreEqual (typeof (GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>>), result.GetType ());
417 Deserialize (typeof (GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>>),
418 "<GenListClassOfGenListClassOfInnerClassOfInt32Int32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><somelist><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>1</inner><something>ONE</something></InnerClassOfInt32Int32String><InnerClassOfInt32Int32String><inner>2</inner><something>TWO</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String><GenListClassOfInnerClassOfInt32Int32String><somelist><InnerClassOfInt32Int32String><inner>30</inner><something>THIRTY</something></InnerClassOfInt32Int32String></somelist></GenListClassOfInnerClassOfInt32Int32String></somelist></GenListClassOfGenListClassOfInnerClassOfInt32Int32String>");
419 GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>> genlist =
420 result as GenListClass<GenListClass<GenNestedClass<int, int>.InnerClass<string>>>;
421 Assert.AreEqual (1, genlist.somelist[0].somelist[0].inner);
422 Assert.AreEqual ("ONE", genlist.somelist[0].somelist[0].something);
423 Assert.AreEqual (2, genlist.somelist[0].somelist[1].inner);
424 Assert.AreEqual ("TWO", genlist.somelist[0].somelist[1].something);
425 Assert.AreEqual (30, genlist.somelist[1].somelist[0].inner);
426 Assert.AreEqual ("THIRTY", genlist.somelist[1].somelist[0].something);
429 public enum Myenum { one, two, three, four, five, six };
431 public void TestDeserializeGenArrayClassEnum ()
433 Deserialize (typeof (GenArrayClass<Myenum>), "<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>one</Myenum><Myenum>one</Myenum></arr></GenArrayClassOfMyenum>");
434 Assert.AreEqual (typeof (GenArrayClass<Myenum>), result.GetType ());
435 Deserialize (typeof (GenArrayClass<Myenum>), "<GenArrayClassOfMyenum xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><Myenum>one</Myenum><Myenum>three</Myenum><Myenum>five</Myenum></arr></GenArrayClassOfMyenum>");
436 GenArrayClass<Myenum> genarr = result as GenArrayClass<Myenum>;
437 Assert.AreEqual (Myenum.one, genarr.arr[0]);
438 Assert.AreEqual (Myenum.three, genarr.arr[1]);
439 Assert.AreEqual (Myenum.five, genarr.arr[2]);
443 public void TestDeserializeGenArrayClassStruct ()
445 Deserialize (typeof (GenArrayClass<GenSimpleStruct<uint>>), "<GenArrayClassOfGenSimpleStructOfUInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><GenSimpleStructOfUInt32>0</GenSimpleStructOfUInt32><GenSimpleStructOfUInt32>0</GenSimpleStructOfUInt32><GenSimpleStructOfUInt32>0</GenSimpleStructOfUInt32></arr></GenArrayClassOfGenSimpleStructOfUInt32>");
446 Assert.AreEqual (typeof (GenArrayClass<GenSimpleStruct<uint>>), result.GetType ());
447 Deserialize (typeof (GenArrayClass<GenSimpleStruct<uint>>), "<GenArrayClassOfGenSimpleStructOfUInt32 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><GenSimpleStructOfUInt32><something>111</something></GenSimpleStructOfUInt32><GenSimpleStructOfUInt32><something>222</something></GenSimpleStructOfUInt32><GenSimpleStructOfUInt32><something>333</something></GenSimpleStructOfUInt32></arr></GenArrayClassOfGenSimpleStructOfUInt32>");
448 GenArrayClass<GenSimpleStruct<uint>> genarr = result as GenArrayClass<GenSimpleStruct<uint>>;
449 Assert.AreEqual (111, genarr.arr[0].something);
450 Assert.AreEqual (222, genarr.arr[1].something);
451 Assert.AreEqual (333, genarr.arr[2].something);
455 public void TestDeserializeGenArrayClassList ()
457 Deserialize (typeof (GenArrayClass<GenListClass<string>>), "<GenArrayClassOfGenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><GenListClassOfString>0</GenListClassOfString><GenListClassOfString>0</GenListClassOfString><GenListClassOfString>0</GenListClassOfString></arr></GenArrayClassOfGenListClassOfString>");
458 Assert.AreEqual (typeof (GenArrayClass<GenListClass<string>>), result.GetType ());
459 Deserialize (typeof (GenArrayClass<GenListClass<string>>), "<GenArrayClassOfGenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><GenListClassOfString><somelist><string>list1-val1</string><string>list1-val2</string></somelist></GenListClassOfString><GenListClassOfString><somelist><string>list2-val1</string><string>list2-val2</string><string>list2-val3</string><string>list2-val4</string></somelist></GenListClassOfString><GenListClassOfString><somelist><string>list3val</string></somelist></GenListClassOfString></arr></GenArrayClassOfGenListClassOfString>");
460 GenArrayClass<GenListClass<string>> genarr = result as GenArrayClass<GenListClass<string>>;
461 Assert.AreEqual ("list1-val1", genarr.arr[0].somelist[0]);
462 Assert.AreEqual ("list1-val2", genarr.arr[0].somelist[1]);
463 Assert.AreEqual ("list2-val1", genarr.arr[1].somelist[0]);
464 Assert.AreEqual ("list2-val2", genarr.arr[1].somelist[1]);
465 Assert.AreEqual ("list2-val3", genarr.arr[1].somelist[2]);
466 Assert.AreEqual ("list2-val4", genarr.arr[1].somelist[3]);
467 Assert.AreEqual ("list3val", genarr.arr[2].somelist[0]);
468 // The code below checks for DotNet bug (see corresponding test in XmlSerializerTests).
469 Deserialize (typeof (GenArrayClass<GenListClass<string>>), "<GenArrayClassOfGenListClassOfString xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><arr><GenListClassOfString><somelist><string>list1-val1</string><string>list1-val2</string><string>list3val</string></somelist></GenListClassOfString><GenListClassOfString><somelist><string>list2-val1</string><string>list2-val2</string><string>list2-val3</string><string>list2-val4</string></somelist></GenListClassOfString><GenListClassOfString><somelist></somelist></GenListClassOfString></arr></GenArrayClassOfGenListClassOfString>");
470 GenArrayClass<GenListClass<string>> genarr2 = result as GenArrayClass<GenListClass<string>>;
471 Assert.AreEqual ("list1-val1", genarr2.arr[0].somelist[0]);
472 Assert.AreEqual ("list1-val2", genarr2.arr[0].somelist[1]);
474 Assert.AreEqual ("list3val", genarr2.arr[0].somelist[2]);
475 Assert.AreEqual ("list2-val1", genarr2.arr[1].somelist[0]);
476 Assert.AreEqual ("list2-val2", genarr2.arr[1].somelist[1]);
477 Assert.AreEqual ("list2-val3", genarr2.arr[1].somelist[2]);
478 Assert.AreEqual ("list2-val4", genarr2.arr[1].somelist[3]);
479 //Assert.AreEqual ("list3val", genarr2.arr[2].somelist[0]);
483 public void TestDeserializeGenComplexStruct ()
485 Deserialize (typeof (GenComplexStruct<int, string>), "<GenComplexStructOfInt32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>0</something><simpleclass><something>0</something></simpleclass><simplestruct><something>0</something></simplestruct><listclass><somelist></somelist></listclass><arrayclass><arr><int>0</int><int>0</int><int>0</int></arr></arrayclass><twoclass><something1>0</something1></twoclass><derivedclass><something2>0</something2><another1>0</another1></derivedclass><derived2><something1>0</something1><another1>0</another1></derived2><nestedouter><outer>0</outer></nestedouter><nestedinner><something>0</something></nestedinner></GenComplexStructOfInt32String>");
486 Assert.AreEqual (typeof (GenComplexStruct<int, string>), result.GetType ());
487 Deserialize (typeof (GenComplexStruct<int, string>), "<GenComplexStructOfInt32String xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>123</something><simpleclass><something>456</something></simpleclass><simplestruct><something>789</something></simplestruct><listclass><somelist><int>100</int><int>200</int></somelist></listclass><arrayclass><arr><int>11</int><int>22</int><int>33</int></arr></arrayclass><twoclass><something1>10</something1><something2>Ten</something2></twoclass><derivedclass><something1>two</something1><something2>2</something2><another1>1</another1><another2>one</another2></derivedclass><derived2><something1>4</something1><something2>four</something2><another1>3</another1><another2>three</another2></derived2><nestedouter><outer>5</outer></nestedouter><nestedinner><inner>six</inner><something>6</something></nestedinner></GenComplexStructOfInt32String>");
488 GenComplexStruct<int, string> complex = new GenComplexStruct<int, string> (0);
490 complex = (GenComplexStruct<int, string>) result;
491 Assert.AreEqual (123, complex.something);
492 Assert.AreEqual (456, complex.simpleclass.something);
493 Assert.AreEqual (789, complex.simplestruct.something);
494 Assert.AreEqual (100, complex.listclass.somelist[0]);
495 Assert.AreEqual (200, complex.listclass.somelist[1]);
496 Assert.AreEqual (11, complex.arrayclass.arr[0]);
497 Assert.AreEqual (22, complex.arrayclass.arr[1]);
498 Assert.AreEqual (33, complex.arrayclass.arr[2]);
499 Assert.AreEqual (10, complex.twoclass.something1);
500 Assert.AreEqual ("Ten", complex.twoclass.something2);
501 Assert.AreEqual (1, complex.derivedclass.another1);
502 Assert.AreEqual ("one", complex.derivedclass.another2);
503 Assert.AreEqual ("two", complex.derivedclass.something1);
504 Assert.AreEqual (2, complex.derivedclass.something2);
505 Assert.AreEqual (3, complex.derived2.another1);
506 Assert.AreEqual ("three", complex.derived2.another2);
507 Assert.AreEqual (4, complex.derived2.something1);
508 Assert.AreEqual ("four", complex.derived2.something2);
509 Assert.AreEqual (5, complex.nestedouter.outer);
510 Assert.AreEqual ("six", complex.nestedinner.inner);
511 Assert.AreEqual (6, complex.nestedinner.something);
515 #endregion //GenericsDeseralizationTests
518 public void TestDeserializeCollection ()
521 s0 += " <ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
522 s0 += " <Entity Name='node1'/>";
523 s0 += " <Entity Name='node2'/>";
524 s0 += " </ArrayOfEntity>";
526 EntityCollection col = (EntityCollection) Deserialize (typeof (EntityCollection), s0);
527 Assert.IsNotNull (col, "#1");
528 Assert.AreEqual (2, col.Count, "#2");
529 Assert.IsNull (col[0].Parent, "#3");
530 Assert.IsNull (col[1].Parent, "#4");
534 public void TestDeserializeEmptyCollection ()
537 s1 += " <ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
539 EntityCollection col = (EntityCollection) Deserialize (typeof (EntityCollection), s1);
540 Assert.IsNotNull (col, "#A1");
541 Assert.AreEqual (0, col.Count, "#A2");
544 s1_1 += " <ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
545 s1_1 += " </ArrayOfEntity>";
547 col = (EntityCollection) Deserialize (typeof (EntityCollection), s1_1);
548 Assert.IsNotNull (col, "#B1");
549 Assert.AreEqual (0, col.Count, "#B2");
553 public void TestDeserializeNilCollectionIsNotNull ()
556 s2 += " <ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>";
558 EntityCollection col = (EntityCollection) Deserialize (typeof (EntityCollection), s2);
559 Assert.IsNotNull (col, "#1");
560 Assert.AreEqual (0, col.Count, "#2");
564 public void TestDeserializeObjectCollections ()
567 s3 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
568 s3 += " <Collection1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
569 s3 += " <Entity Name='node1'/>";
570 s3 += " <Entity Name='node2'/>";
571 s3 += " </Collection1>";
572 s3 += " <Collection2 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
573 s3 += " <Entity Name='node1'/>";
574 s3 += " <Entity Name='node2'/>";
575 s3 += " </Collection2>";
576 s3 += " <Collection3 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
577 s3 += " <Entity Name='node1'/>";
578 s3 += " <Entity Name='node2'/>";
579 s3 += " </Collection3>";
580 s3 += " <Collection4 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
581 s3 += " <Entity Name='node1'/>";
582 s3 += " <Entity Name='node2'/>";
583 s3 += " </Collection4>";
584 s3 += "</Container>";
586 EntityContainer cont = (EntityContainer) Deserialize (typeof (EntityContainer), s3);
587 Assert.IsNotNull (cont, "#A1");
589 Assert.IsNotNull (cont.Collection1, "#B1");
590 Assert.AreEqual (2, cont.Collection1.Count, "#B2");
591 Assert.AreEqual ("assigned", cont.Collection1.Container, "#B3");
592 Assert.AreEqual ("assigned", cont.Collection1[0].Parent, "#B4");
593 Assert.AreEqual ("assigned", cont.Collection1[1].Parent, "#B5");
595 Assert.IsNotNull (cont.Collection2, "#C1");
596 Assert.AreEqual (2, cont.Collection2.Count, "#C2");
597 Assert.AreEqual ("assigned", cont.Collection2.Container, "#C3");
598 Assert.AreEqual ("assigned", cont.Collection2[0].Parent, "#C4");
599 Assert.AreEqual ("assigned", cont.Collection2[1].Parent, "#C5");
601 Assert.IsNotNull (cont.Collection3, "#D1");
602 Assert.AreEqual (2, cont.Collection3.Count, "#D2");
603 Assert.AreEqual ("root", cont.Collection3.Container, "#D3");
604 Assert.AreEqual ("root", cont.Collection3[0].Parent, "#D4");
605 Assert.AreEqual ("root", cont.Collection3[1].Parent, "#D5");
607 Assert.IsNotNull (cont.Collection4, "#E1");
608 Assert.AreEqual (2, cont.Collection4.Count, "#E2");
609 Assert.AreEqual ("root", cont.Collection4.Container, "#E3");
610 Assert.AreEqual ("root", cont.Collection4[0].Parent, "#E4");
611 Assert.AreEqual ("root", cont.Collection4[1].Parent, "#E5");
615 public void TestDeserializeEmptyObjectCollections ()
618 s4 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
619 s4 += "</Container>";
621 EntityContainer cont = (EntityContainer) Deserialize (typeof (EntityContainer), s4);
622 Assert.IsNotNull (cont, "#A1");
624 Assert.IsNotNull (cont.Collection1, "#B1");
625 Assert.AreEqual (0, cont.Collection1.Count, "#B2");
626 Assert.AreEqual ("assigned", cont.Collection1.Container, "#B3");
628 Assert.IsNotNull (cont.Collection2, "#C1");
629 Assert.AreEqual (0, cont.Collection2.Count, "#C2");
630 Assert.AreEqual ("assigned", cont.Collection2.Container, "#C3");
632 Assert.IsNotNull (cont.Collection3, "#D1");
633 Assert.AreEqual (0, cont.Collection3.Count, "#D2");
634 Assert.AreEqual ("root", cont.Collection3.Container, "#D3");
636 Assert.IsNotNull (cont.Collection4, "#E1");
637 Assert.AreEqual (0, cont.Collection4.Count, "#E2");
638 Assert.AreEqual ("root", cont.Collection4.Container, "#E3");
642 public void TestDeserializeObjectNilCollectionsAreNotNull ()
645 s5 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
646 s5 += " <Collection1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
647 s5 += " <Collection2 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
648 s5 += " <Collection3 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
649 s5 += " <Collection4 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
650 s5 += "</Container>";
652 EntityContainer cont = (EntityContainer) Deserialize (typeof (EntityContainer), s5);
653 Assert.IsNotNull (cont, "#A1");
655 Assert.IsNotNull (cont.Collection1, "#B1");
656 Assert.AreEqual (0, cont.Collection1.Count, "#B2");
657 Assert.AreEqual ("assigned", cont.Collection1.Container, "#B3");
659 Assert.IsNotNull (cont.Collection2, "#C1");
660 Assert.AreEqual (0, cont.Collection2.Count, "#C2");
661 Assert.AreEqual ("assigned", cont.Collection2.Container, "#C3");
663 Assert.IsNotNull (cont.Collection3, "#D1");
664 Assert.AreEqual (0, cont.Collection3.Count, "#D2");
665 Assert.AreEqual ("root", cont.Collection3.Container, "#D3");
667 Assert.IsNotNull (cont.Collection4, "#E1");
668 Assert.AreEqual (0, cont.Collection4.Count, "#E2");
669 Assert.AreEqual ("root", cont.Collection4.Container, "#E3");
673 public void TestDeserializeObjectEmptyCollections ()
676 s6 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
677 s6 += " <Collection1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
678 s6 += " <Collection2 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
679 s6 += " <Collection3 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
680 s6 += " <Collection4 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
681 s6 += "</Container>";
683 EntityContainer cont = (EntityContainer) Deserialize (typeof (EntityContainer), s6);
684 Assert.IsNotNull (cont, "#A1");
686 Assert.IsNotNull (cont.Collection1, "#B1");
687 Assert.AreEqual (0, cont.Collection1.Count, "#B2");
688 Assert.AreEqual ("assigned", cont.Collection1.Container, "#B3");
690 Assert.IsNotNull (cont.Collection2, "#C1");
691 Assert.AreEqual (0, cont.Collection2.Count, "#C2");
692 Assert.AreEqual ("assigned", cont.Collection2.Container, "#C3");
694 Assert.IsNotNull (cont.Collection3, "#D1");
695 Assert.AreEqual (0, cont.Collection3.Count, "#D2");
696 Assert.AreEqual ("root", cont.Collection3.Container, "#D3");
698 Assert.IsNotNull (cont.Collection4, "#E1");
699 Assert.AreEqual (0, cont.Collection4.Count, "#E2");
700 Assert.AreEqual ("root", cont.Collection4.Container, "#E3");
704 public void TestDeserializeObjectEmptyArrays ()
707 s6 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
708 s6 += " <Collection1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
709 s6 += " <Collection2 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
710 s6 += " <Collection3 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
711 s6 += " <Collection4 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
712 s6 += "</Container>";
714 ArrayEntityContainer cont = (ArrayEntityContainer) Deserialize (typeof (ArrayEntityContainer), s6);
715 Assert.IsNotNull (cont, "#A1");
717 Assert.IsNotNull (cont.Collection1, "#B1");
718 Assert.AreEqual (0, cont.Collection1.Length, "#B2");
720 Assert.IsNotNull (cont.Collection2, "#C1");
721 Assert.AreEqual (0, cont.Collection2.Length, "#C2");
723 Assert.IsNotNull (cont.Collection3, "#D1");
724 Assert.AreEqual (0, cont.Collection3.Length, "#D2");
726 Assert.IsNotNull (cont.Collection4, "#E1");
727 Assert.AreEqual (0, cont.Collection4.Length, "#E2");
731 public void TestDeserializeEmptyObjectArrays ()
734 s4 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
735 s4 += "</Container>";
737 ArrayEntityContainer cont = (ArrayEntityContainer) Deserialize (typeof (ArrayEntityContainer), s4);
738 Assert.IsNotNull (cont, "#A1");
740 Assert.IsNull (cont.Collection1, "#B1");
741 Assert.IsNull (cont.Collection2, "#B2");
743 Assert.IsNotNull (cont.Collection3, "#C1");
744 Assert.AreEqual (0, cont.Collection3.Length, "#C2");
746 Assert.IsNotNull (cont.Collection4, "#D1");
747 Assert.AreEqual (0, cont.Collection4.Length, "#D2");
751 public void TestDeserializeObjectNilArrays ()
754 s5 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
755 s5 += " <Collection1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
756 s5 += " <Collection2 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
757 s5 += " <Collection3 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
758 s5 += " <Collection4 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />";
759 s5 += "</Container>";
761 ArrayEntityContainer cont = (ArrayEntityContainer) Deserialize (typeof (ArrayEntityContainer), s5);
762 Assert.IsNotNull (cont, "#A1");
764 Assert.IsNull (cont.Collection1, "#B1");
765 Assert.IsNull (cont.Collection2, "#B2");
766 Assert.IsNull (cont.Collection3, "#B3");
768 Assert.IsNotNull (cont.Collection4, "#C1");
769 Assert.AreEqual (0, cont.Collection4.Length, "#C2");
773 public void TestDeserializeEmptyArray ()
776 s1 += "<ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />";
778 Entity[] col = (Entity[]) Deserialize (typeof (Entity[]), s1);
779 Assert.IsNotNull (col, "#A1");
780 Assert.AreEqual (0, col.Length, "#A2");
783 s1_1 += " <ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
784 s1_1 += " </ArrayOfEntity>";
786 col = (Entity[]) Deserialize (typeof (Entity[]), s1_1);
787 Assert.IsNotNull (col, "#B1");
788 Assert.AreEqual (0, col.Length, "#B2");
792 public void TestDeserializeNilArray ()
795 s2 += "<ArrayOfEntity xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>";
797 Entity[] col = (Entity[]) Deserialize (typeof (Entity[]), s2);
798 Assert.IsNull (col, "#1");
802 public void TestDeserializeObjectWithReadonlyCollection ()
805 s3 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
806 s3 += " <Collection1>";
807 s3 += " <Entity Name='node1'/>";
808 s3 += " <Entity Name='node2'/>";
809 s3 += " </Collection1>";
810 s3 += "</Container>";
812 ObjectWithReadonlyCollection cont = (ObjectWithReadonlyCollection) Deserialize (typeof (ObjectWithReadonlyCollection), s3);
813 Assert.IsNotNull (cont, "#1");
814 Assert.IsNotNull (cont.Collection1, "#2");
815 Assert.AreEqual (2, cont.Collection1.Count, "#3");
816 Assert.AreEqual ("root", cont.Collection1.Container, "#4");
817 Assert.AreEqual ("root", cont.Collection1[0].Parent, "#5");
818 Assert.AreEqual ("root", cont.Collection1[1].Parent, "#6");
822 [ExpectedException (typeof (InvalidOperationException))]
823 public void TestDeserializeObjectWithReadonlyNulCollection ()
826 s3 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
827 s3 += " <Collection1>";
828 s3 += " <Entity Name='node1'/>";
829 s3 += " <Entity Name='node2'/>";
830 s3 += " </Collection1>";
831 s3 += "</Container>";
833 Deserialize (typeof (ObjectWithReadonlyNulCollection), s3);
837 public void TestDeserializeObjectWithReadonlyArray ()
840 s3 += "<Container xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>";
841 s3 += " <Collection1>";
842 s3 += " <Entity Name='node1'/>";
843 s3 += " <Entity Name='node2'/>";
844 s3 += " </Collection1>";
845 s3 += "</Container>";
847 ObjectWithReadonlyArray cont = (ObjectWithReadonlyArray) Deserialize (typeof (ObjectWithReadonlyArray), s3);
848 Assert.IsNotNull (cont, "#1");
849 Assert.IsNotNull (cont.Collection1, "#2");
850 Assert.AreEqual (0, cont.Collection1.Length, "#3");
854 public void TestDeserialize_EnumDefaultValue ()
858 e = (EnumDefaultValue) Deserialize (typeof (EnumDefaultValue), "<EnumDefaultValue />");
859 Assert.AreEqual (0, (int) e, "#1");
861 e = (EnumDefaultValue) Deserialize (typeof (EnumDefaultValue), "<EnumDefaultValue> e3</EnumDefaultValue>");
862 Assert.AreEqual (EnumDefaultValue.e3, e, "#2");
864 e = (EnumDefaultValue) Deserialize (typeof (EnumDefaultValue), "<EnumDefaultValue>e1 e2</EnumDefaultValue>");
865 Assert.AreEqual (EnumDefaultValue.e3, e, "#3");
867 e = (EnumDefaultValue) Deserialize (typeof (EnumDefaultValue), "<EnumDefaultValue> e1 e2 </EnumDefaultValue>");
868 Assert.AreEqual (EnumDefaultValue.e1 | EnumDefaultValue.e2, e, "#4");
872 public void TestDeserialize_EnumDefaultValueNF ()
874 EnumDefaultValueNF e;
876 e = (EnumDefaultValueNF) Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF>e3</EnumDefaultValueNF>");
877 Assert.AreEqual (EnumDefaultValueNF.e3, e, "#A1");
880 Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF />");
883 catch (InvalidOperationException ex) {
884 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
885 Assert.IsNotNull (ex.InnerException, "#B3");
886 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
887 Assert.IsNotNull (ex.InnerException.Message, "#B5");
888 Assert.IsTrue (ex.InnerException.Message.IndexOf ("''") != -1, "#B6");
889 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).Name) != -1, "#B7");
893 Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF>e1 e3</EnumDefaultValueNF>");
896 catch (InvalidOperationException ex) {
897 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
898 Assert.IsNotNull (ex.InnerException, "#C3");
899 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#C4");
900 Assert.IsNotNull (ex.InnerException.Message, "#C5");
901 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'e1 e3'") != -1, "#C6");
902 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).Name) != -1, "#C7");
906 Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF> e3</EnumDefaultValueNF>");
909 catch (InvalidOperationException ex) {
910 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
911 Assert.IsNotNull (ex.InnerException, "#D3");
912 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D4");
913 Assert.IsNotNull (ex.InnerException.Message, "#D5");
914 Assert.IsTrue (ex.InnerException.Message.IndexOf ("' e3'") != -1, "#D6");
915 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).Name) != -1, "#D7");
919 Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF> </EnumDefaultValueNF>");
922 catch (InvalidOperationException ex) {
923 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
924 Assert.IsNotNull (ex.InnerException, "#E3");
925 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E4");
926 Assert.IsNotNull (ex.InnerException.Message, "#E5");
927 Assert.IsTrue (ex.InnerException.Message.IndexOf ("' '") != -1, "#E6");
928 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).Name) != -1, "#E7");
932 Deserialize (typeof (EnumDefaultValueNF), "<EnumDefaultValueNF>1</EnumDefaultValueNF>");
935 catch (InvalidOperationException ex) {
936 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
937 Assert.IsNotNull (ex.InnerException, "#F3");
938 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#F4");
939 Assert.IsNotNull (ex.InnerException.Message, "#F5");
940 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'1'") != -1, "#F6");
941 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (EnumDefaultValueNF).Name) != -1, "#F7");
946 public void TestDeserialize_Field ()
950 f = (Field) Deserialize (typeof (Field),
951 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
952 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
953 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
954 Assert.AreEqual ((FlagEnum) 0, f.Flags1, "#A1");
955 Assert.AreEqual ((FlagEnum) 0, f.Flags2, "#A2");
956 Assert.AreEqual ((FlagEnum) 0, f.Flags3, "#A3");
957 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#A4");
958 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#A5");
959 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#A6");
960 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#A7");
961 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#A8");
962 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#A9");
963 Assert.IsNull (f.Names, "#A10");
964 Assert.IsNull (f.Street, "#A11");
966 f = (Field) Deserialize (typeof (Field),
967 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
968 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4='' modifiers='protected' modifiers2='public' />",
969 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
970 Assert.AreEqual ((FlagEnum) 0, f.Flags1, "#B1");
971 Assert.AreEqual ((FlagEnum) 0, f.Flags2, "#B2");
972 Assert.AreEqual (FlagEnum.e2, f.Flags3, "#B3");
973 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#B4");
974 Assert.AreEqual (MapModifiers.Protected, f.Modifiers, "#B5");
975 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#B6");
976 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#B7");
977 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#B8");
978 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#B9");
979 Assert.IsNull (f.Names, "#B10");
980 Assert.IsNull (f.Street, "#B11");
982 f = (Field) Deserialize (typeof (Field),
983 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
984 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two' flag4='' modifiers='public' modifiers2='protected' modifiers3='protected' modifiers4='public' modifiers5='protected' />",
985 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
986 Assert.AreEqual (FlagEnum.e2, f.Flags1, "#C1");
987 Assert.AreEqual (FlagEnum.e2, f.Flags2, "#C2");
988 Assert.AreEqual ((FlagEnum) 0, f.Flags3, "#C3");
989 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#C4");
990 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#C5");
991 Assert.AreEqual (MapModifiers.Protected, f.Modifiers2, "#C6");
992 Assert.AreEqual (MapModifiers.Protected, f.Modifiers3, "#C7");
993 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#C8");
994 Assert.AreEqual (MapModifiers.Protected, f.Modifiers5, "#C9");
995 Assert.IsNull (f.Names, "#C10");
996 Assert.IsNull (f.Street, "#C11");
999 f = (Field) Deserialize (typeof (Field),
1000 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1001 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='444' />",
1002 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
1003 Assert.Fail ("#D1");
1005 catch (InvalidOperationException ex) {
1006 // There was an error generating the XML document
1007 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1008 Assert.IsNotNull (ex.Message, "#D3");
1009 Assert.IsNotNull (ex.InnerException, "#D4");
1011 // '444' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1012 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1013 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1014 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
1015 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#D8");
1016 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1020 f = (Field) Deserialize (typeof (Field),
1021 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1022 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='Garbage' />",
1023 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
1024 Assert.Fail ("#E1");
1026 catch (InvalidOperationException ex) {
1027 // There was an error generating the XML document
1028 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
1029 Assert.IsNotNull (ex.Message, "#E3");
1030 Assert.IsNotNull (ex.InnerException, "#E4");
1032 // 'Garbage' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1033 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
1034 Assert.IsNotNull (ex.InnerException.Message, "#E6");
1035 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'Garbage'") != -1, "#E7");
1036 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
1037 Assert.IsNull (ex.InnerException.InnerException, "#E9");
1041 f = (Field) Deserialize (typeof (Field),
1042 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1043 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='{2}' />",
1044 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ((int) FlagEnum.e2).ToString (CultureInfo.InvariantCulture)));
1045 Assert.Fail ("#F1");
1047 catch (InvalidOperationException ex) {
1048 // There was an error generating the XML document
1049 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
1050 Assert.IsNotNull (ex.Message, "#F3");
1051 Assert.IsNotNull (ex.InnerException, "#F4");
1053 // '2' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1054 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#F5");
1055 Assert.IsNotNull (ex.InnerException.Message, "#F6");
1056 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'2'") != -1, "#F7");
1057 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#F8");
1058 Assert.IsNull (ex.InnerException.InnerException, "#F9");
1063 [Category ("NotDotNet")] // MS.NET results in compilation error (probably it generates bogus source.)
1064 public void TestDeserialize_Field_Encoded ()
1066 Field_Encoded f = null;
1068 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1069 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1070 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='' flag2='' flag4='' modifiers='PuBlIc' modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='{2}' />",
1071 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1072 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags1, "#A1");
1073 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags2, "#A2");
1074 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags3, "#A3");
1075 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#A4");
1076 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#A5");
1077 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#A6");
1078 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#A7");
1079 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#A8");
1080 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#A9");
1081 Assert.IsNull (f.Names, "#A10");
1082 Assert.IsNull (f.Street, "#A11");
1084 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1085 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1086 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two' flag4='' modifiers='Protected' modifiers2='PuBlIc' xmlns:q1='{2}' />",
1087 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1088 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags1, "#B1");
1089 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags2, "#B2");
1090 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags3, "#B3");
1091 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#B4");
1092 Assert.AreEqual (MapModifiers.Protected, f.Modifiers, "#B5");
1093 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#B6");
1094 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#B7");
1095 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#B8");
1096 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#B9");
1097 Assert.IsNull (f.Names, "#B10");
1098 Assert.IsNull (f.Street, "#B11");
1100 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1101 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1102 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='two' flag2='two' flag4='' modifiers='PuBlIc' modifiers2='Protected' modifiers3='Protected' modifiers4='PuBlIc' modifiers5='Protected' xmlns:q1='{2}' />",
1103 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1104 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags1, "#C1");
1105 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags2, "#C2");
1106 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags3, "#C3");
1107 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#C4");
1108 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#C5");
1109 Assert.AreEqual (MapModifiers.Protected, f.Modifiers2, "#C6");
1110 Assert.AreEqual (MapModifiers.Protected, f.Modifiers3, "#C7");
1111 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#C8");
1112 Assert.AreEqual (MapModifiers.Protected, f.Modifiers5, "#C9");
1113 Assert.IsNull (f.Names, "#C10");
1114 Assert.IsNull (f.Street, "#C11");
1117 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1118 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1119 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag2='444' flag3='555' flag4='' modifiers='666' modifiers2='777' modifiers4='888' modifiers5='999' xmlns:q1='{2}' />",
1120 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1121 Assert.Fail ("#D1");
1123 catch (InvalidOperationException ex) {
1124 // There was an error generating the XML document
1125 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1126 Assert.IsNotNull (ex.Message, "#D3");
1127 Assert.IsNotNull (ex.InnerException, "#D4");
1129 // '444' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
1130 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1131 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1132 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
1133 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
1134 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1139 public void TestDeserialize_FlagEnum ()
1143 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum />");
1144 Assert.AreEqual (0, (int) e, "#A1");
1146 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one</FlagEnum>");
1147 Assert.AreEqual (FlagEnum.e1, e, "#A2");
1149 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one\u200atwo</FlagEnum>");
1150 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2, e, "#A3");
1152 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one two four</FlagEnum>");
1153 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, e, "#A4");
1155 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum> two four </FlagEnum>");
1156 Assert.AreEqual (FlagEnum.e2 | FlagEnum.e4, e, "#A5");
1158 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>two four two</FlagEnum>");
1159 Assert.AreEqual (FlagEnum.e2 | FlagEnum.e4, e, "#A6");
1161 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>two four two\tone\u2002four\rtwo one</FlagEnum>");
1162 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, e, "#A7");
1164 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum></FlagEnum>");
1165 Assert.AreEqual (0, (int) e, "#A8");
1167 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum> </FlagEnum>");
1168 Assert.AreEqual (0, (int) e, "#A9");
1171 Deserialize (typeof (FlagEnum), "<FlagEnum>1</FlagEnum>");
1172 Assert.Fail ("#B1");
1174 catch (InvalidOperationException ex) {
1175 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1176 Assert.IsNotNull (ex.InnerException, "#B3");
1177 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
1178 Assert.IsNotNull (ex.InnerException.Message, "#B5");
1179 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'1'") != -1, "#B6");
1180 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#B7");
1184 Deserialize (typeof (FlagEnum), "<FlagEnum>one,two</FlagEnum>");
1185 Assert.Fail ("#C1");
1187 catch (InvalidOperationException ex) {
1188 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1189 Assert.IsNotNull (ex.InnerException, "#C3");
1190 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#C4");
1191 Assert.IsNotNull (ex.InnerException.Message, "#C5");
1192 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'one,two'") != -1, "#C6");
1193 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#C7");
1197 Deserialize (typeof (FlagEnum), "<FlagEnum>one something</FlagEnum>");
1198 Assert.Fail ("#D1");
1200 catch (InvalidOperationException ex) {
1201 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1202 Assert.IsNotNull (ex.InnerException, "#D3");
1203 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D4");
1204 Assert.IsNotNull (ex.InnerException.Message, "#D5");
1205 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'something'") != -1, "#D6");
1206 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#D7");
1211 public void TestDeserialize_Group ()
1213 string xml = string.Format (CultureInfo.InvariantCulture,
1215 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
1216 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
1217 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
1218 "<MyVehicle href='#id2' />" +
1220 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
1221 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
1222 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
1225 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1227 XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1228 xtr.ReadStartElement ("Wrapper");
1230 Group group = (Group) DeserializeEncoded (typeof (Group), xtr);
1232 Assert.AreEqual (new DateTime (2002, 5, 2), group.Today, "#A1");
1233 Assert.AreEqual (".NET", group.GroupName, "#A2");
1234 Assert.AreEqual (new byte[] { 0x64, 0x32 }, group.GroupNumber, "#A3");
1235 Assert.AreEqual (GroupType.A, group.Grouptype, "#A4");
1236 Assert.AreEqual ("10000", group.PostitiveInt, "#A5");
1237 Assert.IsFalse (group.IgnoreThis, "#A6");
1238 Assert.IsNotNull (group.MyVehicle, "#A7");
1239 Assert.AreEqual (typeof (Car), group.MyVehicle.GetType (), "#A8");
1240 Assert.AreEqual ("1234566", group.MyVehicle.licenseNumber, "#A9");
1241 Assert.AreEqual (new DateTime (1, 1, 1), group.MyVehicle.makeDate, "#A10");
1242 Assert.IsNull (group.MyVehicle.weight, "#A11");
1244 xml = string.Format (CultureInfo.InvariantCulture,
1246 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
1247 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
1248 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
1249 "<MyVehicle href='#id2' />" +
1251 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
1252 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
1255 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1257 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1258 xtr.ReadStartElement ("Wrapper");
1260 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1262 Assert.AreEqual (new DateTime (2002, 5, 2), group.Today, "#B1");
1263 Assert.IsNull (group.GroupName, "#B2");
1264 Assert.AreEqual (new byte[] { 0x64, 0x32 }, group.GroupNumber, "#B3");
1265 Assert.AreEqual (GroupType.B, group.Grouptype, "#B4");
1266 Assert.AreEqual ("10000", group.PostitiveInt, "#B5");
1267 Assert.IsFalse (group.IgnoreThis, "#B6");
1268 Assert.IsNotNull (group.MyVehicle, "#B7");
1269 Assert.AreEqual (typeof (Car), group.MyVehicle.GetType (), "#B8");
1270 Assert.IsNull (group.MyVehicle.licenseNumber, "#B9");
1271 Assert.AreEqual (DateTime.MinValue, group.MyVehicle.makeDate, "#B10");
1272 Assert.AreEqual ("450", group.MyVehicle.weight, "#B11");
1274 xml = string.Format (CultureInfo.InvariantCulture,
1276 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' />" +
1278 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1280 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1281 xtr.ReadStartElement ("Wrapper");
1283 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1285 Assert.AreEqual (DateTime.MinValue, group.Today, "#C1");
1286 Assert.IsNull (group.GroupName, "#C2");
1287 Assert.AreEqual (null, group.GroupNumber, "#C3");
1288 Assert.AreEqual (GroupType.A, group.Grouptype, "#C4");
1289 Assert.IsNull (group.PostitiveInt, "#C5");
1290 Assert.IsFalse (group.IgnoreThis, "#C6");
1291 Assert.IsNull (group.MyVehicle, "#C7");
1293 xml = string.Format (CultureInfo.InvariantCulture,
1295 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1296 "<Grouptype xsi:type='GroupType'>666</Grouptype>" +
1299 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1301 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1302 xtr.ReadStartElement ("Wrapper");
1305 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1306 Assert.Fail ("#D1");
1308 catch (InvalidOperationException ex) {
1309 // There is an error in XML document (1, 174)
1310 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1311 Assert.IsNotNull (ex.Message, "#D3");
1312 Assert.IsNotNull (ex.InnerException, "#D4");
1314 // '666' is not a valid value for GroupType
1315 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1316 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1317 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'666'") != -1, "#D7");
1318 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#D8");
1319 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1322 xml = string.Format (CultureInfo.InvariantCulture,
1324 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1325 "<Grouptype xsi:type='GroupType'>Garbage</Grouptype>" +
1328 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1330 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1331 xtr.ReadStartElement ("Wrapper");
1334 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1335 Assert.Fail ("#E1");
1337 catch (InvalidOperationException ex) {
1338 // There is an error in XML document (1, 178)
1339 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
1340 Assert.IsNotNull (ex.Message, "#E3");
1341 Assert.IsNotNull (ex.InnerException, "#E4");
1343 // 'Garbage' is not a valid value for GroupType
1344 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
1345 Assert.IsNotNull (ex.InnerException.Message, "#E6");
1346 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'Garbage'") != -1, "#E7");
1347 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#E8");
1348 Assert.IsNull (ex.InnerException.InnerException, "#E9");
1351 xml = string.Format (CultureInfo.InvariantCulture,
1353 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1354 "<Grouptype xsi:type='GroupType'>{2}</Grouptype>" +
1357 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance",
1358 ((int) GroupType.B).ToString (CultureInfo.InvariantCulture));
1360 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1361 xtr.ReadStartElement ("Wrapper");
1364 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1365 Assert.Fail ("#F1");
1367 catch (InvalidOperationException ex) {
1368 // There is an error in XML document (1, 172)
1369 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
1370 Assert.IsNotNull (ex.Message, "#F3");
1371 Assert.IsNotNull (ex.InnerException, "#F4");
1373 // '1' is not a valid value for GroupType
1374 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#F5");
1375 Assert.IsNotNull (ex.InnerException.Message, "#F6");
1376 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'1'") != -1, "#F7");
1377 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#F8");
1378 Assert.IsNull (ex.InnerException.InnerException, "#F9");
1383 public void TestDeserialize_ZeroFlagEnum ()
1387 e = (ZeroFlagEnum) Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum />");
1388 Assert.AreEqual (ZeroFlagEnum.e0, e, "#A1");
1389 e = (ZeroFlagEnum) Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum></ZeroFlagEnum>");
1390 Assert.AreEqual (ZeroFlagEnum.e0, e, "#A2");
1393 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum>four</ZeroFlagEnum>");
1394 Assert.Fail ("#B1");
1396 catch (InvalidOperationException ex) {
1397 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1398 Assert.IsNotNull (ex.InnerException, "#B3");
1399 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
1400 Assert.IsNotNull (ex.InnerException.Message, "#B5");
1401 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#B6");
1402 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#B7");
1406 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum> o<n>e four </ZeroFlagEnum>");
1407 Assert.Fail ("#C1");
1409 catch (InvalidOperationException ex) {
1410 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1411 Assert.IsNotNull (ex.InnerException, "#C3");
1412 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#C4");
1413 Assert.IsNotNull (ex.InnerException.Message, "#C5");
1414 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#C6");
1415 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#C7");
1419 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum>four o<n>e</ZeroFlagEnum>");
1420 Assert.Fail ("#D1");
1422 catch (InvalidOperationException ex) {
1423 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1424 Assert.IsNotNull (ex.InnerException, "#D3");
1425 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D4");
1426 Assert.IsNotNull (ex.InnerException.Message, "#D5");
1427 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#D6");
1428 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#D7");
1433 public void TestDeserialize_PrimitiveTypesContainer ()
1435 Deserialize (typeof (PrimitiveTypesContainer), string.Format (CultureInfo.InvariantCulture,
1436 "<?xml version='1.0' encoding='utf-16'?>" +
1437 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1438 "<Number xsi:type='xsd:int'>2004</Number>" +
1439 "<Name xsi:type='xsd:string'>some name</Name>" +
1440 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1441 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1442 "<PathSeparatorCharacter xmlns:q1='{3}' xsi:type='q1:char'>47</PathSeparatorCharacter>" +
1443 "</PrimitiveTypesContainer>", XmlSchemaNamespace,
1444 XmlSchemaInstanceNamespace, ANamespace, WsdlTypesNamespace));
1445 Assert.AreEqual (typeof (PrimitiveTypesContainer), result.GetType (), "#A1");
1447 PrimitiveTypesContainer deserialized = (PrimitiveTypesContainer) result;
1448 Assert.AreEqual (2004, deserialized.Number, "#A2");
1449 Assert.AreEqual ("some name", deserialized.Name, "#A3");
1450 Assert.AreEqual ((byte) 56, deserialized.Index, "#A4");
1451 Assert.AreEqual (new byte[] { 243, 15 }, deserialized.Password, "#A5");
1452 Assert.AreEqual ('/', deserialized.PathSeparatorCharacter, "#A6");
1454 DeserializeEncoded (typeof (PrimitiveTypesContainer), string.Format (CultureInfo.InvariantCulture,
1455 "<?xml version='1.0' encoding='utf-16'?>" +
1456 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
1457 "<Number xsi:type='xsd:int'>2004</Number>" +
1458 "<Name xsi:type='xsd:string'>some name</Name>" +
1459 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1460 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1461 "<PathSeparatorCharacter xmlns:q1='{3}' xsi:type='q1:char'>47</PathSeparatorCharacter>" +
1462 "</q1:PrimitiveTypesContainer>", XmlSchemaNamespace,
1463 XmlSchemaInstanceNamespace, AnotherNamespace, WsdlTypesNamespace));
1464 Assert.AreEqual (typeof (PrimitiveTypesContainer), result.GetType (), "#B1");
1466 deserialized = (PrimitiveTypesContainer) result;
1467 Assert.AreEqual (2004, deserialized.Number, "#B2");
1468 Assert.AreEqual ("some name", deserialized.Name, "#B3");
1469 Assert.AreEqual ((byte) 56, deserialized.Index, "#B4");
1470 Assert.AreEqual (new byte[] { 243, 15 }, deserialized.Password, "#B5");
1471 Assert.AreEqual ('/', deserialized.PathSeparatorCharacter, "#B6");
1474 [Test] // bug #378696
1475 public void DoNotFillDefaultValue ()
1477 XmlSerializer xs = new XmlSerializer (typeof (DefaultDateTimeContainer));
1478 DefaultDateTimeContainer o = (DefaultDateTimeContainer) xs.Deserialize (new StringReader ("<DefaultDateTimeContainer xmlns='urn:foo' />"));
1479 // do not fill DefaultValue / do not bork at generating code.
1480 Assert.AreEqual (DateTime.MinValue, o.FancyDateTime, "#1");
1481 Assert.AreEqual (0, o.Numeric, "#2");
1484 [Test] // bug bxc 4367
1485 public void SpecifiedXmlIgnoreTest ()
1487 XmlReflectionMember [] out_members = new XmlReflectionMember [2];
1488 XmlReflectionMember m;
1490 m = new XmlReflectionMember ();
1491 m.IsReturnValue = false;
1492 m.MemberName = "HasPermissionsForUserResult";
1493 m.MemberType = typeof (bool);
1494 m.SoapAttributes = new SoapAttributes ();
1495 m.XmlAttributes = new XmlAttributes ();
1496 out_members [0] = m;
1498 m = new XmlReflectionMember ();
1499 m.IsReturnValue = false;
1500 m.MemberName = "HasPermissionsForUserResultSpecified";
1501 m.MemberType = typeof (bool);
1502 m.SoapAttributes = new SoapAttributes ();
1503 m.XmlAttributes = new XmlAttributes ();
1504 m.XmlAttributes.XmlIgnore = true;
1505 out_members [1] = m;
1507 XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
1508 XmlMembersMapping OutputMembersMapping = xmlImporter.ImportMembersMapping ("HasPermissionsForUserResponse", "http://tempuri.org", out_members, true);
1509 XmlSerializer xmlSerializer = XmlSerializer.FromMappings (new XmlMapping [] { OutputMembersMapping }) [0];
1511 Assert.AreEqual (2, OutputMembersMapping.Count, "#count");
1514 <HasPermissionsForUserResponse xmlns=""http://tempuri.org/"">
1515 <HasPermissionsForUserResult>true</HasPermissionsForUserResult>
1516 </HasPermissionsForUserResponse>
1519 object res = xmlSerializer.Deserialize (new StringReader (msg));
1520 Assert.AreEqual (typeof (object[]), res.GetType (), "type");
1521 Assert.AreEqual (2, ((object[]) res).Length, "length");
1525 public void InvalidNullableTypeTest ()
1527 XmlReflectionMember [] out_members = new XmlReflectionMember [1];
1528 XmlReflectionMember m;
1530 m = new XmlReflectionMember ();
1531 m.IsReturnValue = false;
1532 m.MemberName = "HasPermissionsForUserResultSpecified";
1533 m.MemberType = typeof (bool);
1534 m.SoapAttributes = new SoapAttributes ();
1535 m.XmlAttributes = new XmlAttributes ();
1536 m.XmlAttributes.XmlIgnore = true;
1537 m.XmlAttributes.XmlElements.Add (new XmlElementAttribute () { IsNullable = true });
1538 out_members [0] = m;
1540 XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
1543 xmlImporter.ImportMembersMapping ("HasPermissionsForUserResponse", "http://tempuri.org", out_members, true);
1544 Assert.Fail ("Expected InvalidOperationException");
1545 } catch (InvalidOperationException) {
1550 public void NotExactDateParse ()
1552 XmlSerializer xs = new XmlSerializer (typeof (NotExactDateParseClass));
1553 NotExactDateParseClass o = (NotExactDateParseClass) xs.Deserialize (new StringReader ("<NotExactDateParseClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SomeDate xsi:type=\"xsd:date\">2012-02-05-09:00</SomeDate></NotExactDateParseClass>"));
1554 Assert.AreEqual (new DateTime (2012,2,5), o.SomeDate);