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 [Category ("NotWorking")] // DefaultValue should not be used when deserializing
947 public void TestDeserialize_Field ()
951 f = (Field) Deserialize (typeof (Field),
952 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
953 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='' flag2='' flag4='' modifiers='public' modifiers2='public' modifiers4='public' />",
954 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
955 Assert.AreEqual ((FlagEnum) 0, f.Flags1, "#A1");
956 Assert.AreEqual ((FlagEnum) 0, f.Flags2, "#A2");
957 Assert.AreEqual ((FlagEnum) 0, f.Flags3, "#A3");
958 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#A4");
959 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#A5");
960 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#A6");
961 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#A7");
962 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#A8");
963 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#A9");
964 Assert.IsNull (f.Names, "#A10");
965 Assert.IsNull (f.Street, "#A11");
967 f = (Field) Deserialize (typeof (Field),
968 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
969 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag3='two' flag4='' modifiers='protected' modifiers2='public' />",
970 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
971 Assert.AreEqual ((FlagEnum) 0, f.Flags1, "#B1");
972 Assert.AreEqual ((FlagEnum) 0, f.Flags2, "#B2");
973 Assert.AreEqual (FlagEnum.e2, f.Flags3, "#B3");
974 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#B4");
975 Assert.AreEqual (MapModifiers.Protected, f.Modifiers, "#B5");
976 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#B6");
977 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#B7");
978 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#B8");
979 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#B9");
980 Assert.IsNull (f.Names, "#B10");
981 Assert.IsNull (f.Street, "#B11");
983 f = (Field) Deserialize (typeof (Field),
984 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
985 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag1='two' flag2='two' flag4='' modifiers='public' modifiers2='protected' modifiers3='protected' modifiers4='public' modifiers5='protected' />",
986 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
987 Assert.AreEqual (FlagEnum.e2, f.Flags1, "#C1");
988 Assert.AreEqual (FlagEnum.e2, f.Flags2, "#C2");
989 Assert.AreEqual ((FlagEnum) 0, f.Flags3, "#C3");
990 Assert.AreEqual ((FlagEnum) 0, f.Flags4, "#C4");
991 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#C5");
992 Assert.AreEqual (MapModifiers.Protected, f.Modifiers2, "#C6");
993 Assert.AreEqual (MapModifiers.Protected, f.Modifiers3, "#C7");
994 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#C8");
995 Assert.AreEqual (MapModifiers.Protected, f.Modifiers5, "#C9");
996 Assert.IsNull (f.Names, "#C10");
997 Assert.IsNull (f.Street, "#C11");
1000 f = (Field) Deserialize (typeof (Field),
1001 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1002 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='444' />",
1003 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
1004 Assert.Fail ("#D1");
1006 catch (InvalidOperationException ex) {
1007 // There was an error generating the XML document
1008 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1009 Assert.IsNotNull (ex.Message, "#D3");
1010 Assert.IsNotNull (ex.InnerException, "#D4");
1012 // '444' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1013 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1014 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1015 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
1016 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#D8");
1017 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1021 f = (Field) Deserialize (typeof (Field),
1022 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1023 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='Garbage' />",
1024 XmlSchemaNamespace, XmlSchemaInstanceNamespace));
1025 Assert.Fail ("#E1");
1027 catch (InvalidOperationException ex) {
1028 // There was an error generating the XML document
1029 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
1030 Assert.IsNotNull (ex.Message, "#E3");
1031 Assert.IsNotNull (ex.InnerException, "#E4");
1033 // 'Garbage' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1034 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
1035 Assert.IsNotNull (ex.InnerException.Message, "#E6");
1036 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'Garbage'") != -1, "#E7");
1037 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#E8");
1038 Assert.IsNull (ex.InnerException.InnerException, "#E9");
1042 f = (Field) Deserialize (typeof (Field),
1043 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1044 "<field xmlns:xsd='{0}' xmlns:xsi='{1}' flag2='{2}' />",
1045 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ((int) FlagEnum.e2).ToString (CultureInfo.InvariantCulture)));
1046 Assert.Fail ("#F1");
1048 catch (InvalidOperationException ex) {
1049 // There was an error generating the XML document
1050 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
1051 Assert.IsNotNull (ex.Message, "#F3");
1052 Assert.IsNotNull (ex.InnerException, "#F4");
1054 // '2' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum
1055 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#F5");
1056 Assert.IsNotNull (ex.InnerException.Message, "#F6");
1057 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'2'") != -1, "#F7");
1058 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#F8");
1059 Assert.IsNull (ex.InnerException.InnerException, "#F9");
1064 [Category ("NotDotNet")] // MS.NET does not allow SoapAttribute backed by enum ??
1065 public void TestDeserialize_Field_Encoded ()
1067 Field_Encoded f = null;
1069 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1070 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1071 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag1='' flag2='' flag4='' modifiers='PuBlIc' modifiers2='PuBlIc' modifiers4='PuBlIc' xmlns:q1='{2}' />",
1072 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1073 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags1, "#A1");
1074 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags2, "#A2");
1075 Assert.AreEqual (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2, f.Flags3, "#A3");
1076 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#A4");
1077 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#A5");
1078 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#A6");
1079 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#A7");
1080 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#A8");
1081 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#A9");
1082 Assert.IsNull (f.Names, "#A10");
1083 Assert.IsNull (f.Street, "#A11");
1085 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1086 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1087 "<q1:field xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' flag3='two' flag4='' modifiers='Protected' modifiers2='PuBlIc' xmlns:q1='{2}' />",
1088 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1089 Assert.AreEqual (FlagEnum_Encoded.e1, f.Flags1, "#B1");
1090 Assert.AreEqual (FlagEnum_Encoded.e1, f.Flags2, "#B2");
1091 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags3, "#B3");
1092 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#B4");
1093 Assert.AreEqual (MapModifiers.Protected, f.Modifiers, "#B5");
1094 Assert.AreEqual (MapModifiers.Public, f.Modifiers2, "#B6");
1095 Assert.AreEqual (MapModifiers.Public, f.Modifiers3, "#B7");
1096 Assert.AreEqual (MapModifiers.Protected, f.Modifiers4, "#B8");
1097 Assert.AreEqual (MapModifiers.Public, f.Modifiers5, "#B9");
1098 Assert.IsNull (f.Names, "#B10");
1099 Assert.IsNull (f.Street, "#B11");
1101 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1102 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1103 "<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}' />",
1104 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1105 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags1, "#C1");
1106 Assert.AreEqual (FlagEnum_Encoded.e2, f.Flags2, "#C2");
1107 Assert.AreEqual (FlagEnum_Encoded.e1 | FlagEnum_Encoded.e2, f.Flags3, "#C3");
1108 Assert.AreEqual ((FlagEnum_Encoded) 0, f.Flags4, "#C4");
1109 Assert.AreEqual (MapModifiers.Public, f.Modifiers, "#C5");
1110 Assert.AreEqual (MapModifiers.Protected, f.Modifiers2, "#C6");
1111 Assert.AreEqual (MapModifiers.Protected, f.Modifiers3, "#C7");
1112 Assert.AreEqual (MapModifiers.Public, f.Modifiers4, "#C8");
1113 Assert.AreEqual (MapModifiers.Protected, f.Modifiers5, "#C9");
1114 Assert.IsNull (f.Names, "#C10");
1115 Assert.IsNull (f.Street, "#C11");
1118 f = (Field_Encoded) DeserializeEncoded (typeof (Field_Encoded),
1119 string.Format (CultureInfo.InvariantCulture, "<?xml version='1.0' encoding='utf-16'?>" +
1120 "<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}' />",
1121 XmlSchemaNamespace, XmlSchemaInstanceNamespace, ANamespace));
1122 Assert.Fail ("#D1");
1124 catch (InvalidOperationException ex) {
1125 // There was an error generating the XML document
1126 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1127 Assert.IsNotNull (ex.Message, "#D3");
1128 Assert.IsNotNull (ex.InnerException, "#D4");
1130 // '444' is not a valid value for MonoTests.System.Xml.TestClasses.FlagEnum_Encoded
1131 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1132 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1133 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'444'") != -1, "#D7");
1134 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum_Encoded).FullName) != -1, "#D8");
1135 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1140 public void TestDeserialize_FlagEnum ()
1144 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum />");
1145 Assert.AreEqual (0, (int) e, "#A1");
1147 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one</FlagEnum>");
1148 Assert.AreEqual (FlagEnum.e1, e, "#A2");
1150 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one\u200atwo</FlagEnum>");
1151 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2, e, "#A3");
1153 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>one two four</FlagEnum>");
1154 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, e, "#A4");
1156 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum> two four </FlagEnum>");
1157 Assert.AreEqual (FlagEnum.e2 | FlagEnum.e4, e, "#A5");
1159 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>two four two</FlagEnum>");
1160 Assert.AreEqual (FlagEnum.e2 | FlagEnum.e4, e, "#A6");
1162 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum>two four two\tone\u2002four\u200btwo one</FlagEnum>");
1163 Assert.AreEqual (FlagEnum.e1 | FlagEnum.e2 | FlagEnum.e4, e, "#A7");
1165 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum></FlagEnum>");
1166 Assert.AreEqual (0, (int) e, "#A8");
1168 e = (FlagEnum) Deserialize (typeof (FlagEnum), "<FlagEnum> </FlagEnum>");
1169 Assert.AreEqual (0, (int) e, "#A9");
1172 Deserialize (typeof (FlagEnum), "<FlagEnum>1</FlagEnum>");
1173 Assert.Fail ("#B1");
1175 catch (InvalidOperationException ex) {
1176 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1177 Assert.IsNotNull (ex.InnerException, "#B3");
1178 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
1179 Assert.IsNotNull (ex.InnerException.Message, "#B5");
1180 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'1'") != -1, "#B6");
1181 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#B7");
1185 Deserialize (typeof (FlagEnum), "<FlagEnum>one,two</FlagEnum>");
1186 Assert.Fail ("#C1");
1188 catch (InvalidOperationException ex) {
1189 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1190 Assert.IsNotNull (ex.InnerException, "#C3");
1191 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#C4");
1192 Assert.IsNotNull (ex.InnerException.Message, "#C5");
1193 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'one,two'") != -1, "#C6");
1194 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#C7");
1198 Deserialize (typeof (FlagEnum), "<FlagEnum>one something</FlagEnum>");
1199 Assert.Fail ("#D1");
1201 catch (InvalidOperationException ex) {
1202 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1203 Assert.IsNotNull (ex.InnerException, "#D3");
1204 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D4");
1205 Assert.IsNotNull (ex.InnerException.Message, "#D5");
1206 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'something'") != -1, "#D6");
1207 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (FlagEnum).FullName) != -1, "#D7");
1212 public void TestDeserialize_Group ()
1214 string xml = string.Format (CultureInfo.InvariantCulture,
1216 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns:d2p1='http://www.cpandl.com' CreationDate='2002-05-02' d2p1:GroupName='.NET' GroupNumber='ZDI=' id='id1'>" +
1217 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
1218 "<Grouptype xsi:type='GroupType'>Small</Grouptype>" +
1219 "<MyVehicle href='#id2' />" +
1221 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
1222 "<licenseNumber xmlns:q1='{0}' d2p1:type='q1:string'>1234566</licenseNumber>" +
1223 "<makeDate xmlns:q2='{0}' d2p1:type='q2:date'>0001-01-01</makeDate>" +
1226 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1228 XmlTextReader xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1229 xtr.ReadStartElement ("Wrapper");
1231 Group group = (Group) DeserializeEncoded (typeof (Group), xtr);
1233 Assert.AreEqual (new DateTime (2002, 5, 2), group.Today, "#A1");
1234 Assert.AreEqual (".NET", group.GroupName, "#A2");
1235 Assert.AreEqual (new byte[] { 0x64, 0x32 }, group.GroupNumber, "#A3");
1236 Assert.AreEqual (GroupType.A, group.Grouptype, "#A4");
1237 Assert.AreEqual ("10000", group.PostitiveInt, "#A5");
1238 Assert.IsFalse (group.IgnoreThis, "#A6");
1239 Assert.IsNotNull (group.MyVehicle, "#A7");
1240 Assert.AreEqual (typeof (Car), group.MyVehicle.GetType (), "#A8");
1241 Assert.AreEqual ("1234566", group.MyVehicle.licenseNumber, "#A9");
1242 Assert.AreEqual (new DateTime (1, 1, 1), group.MyVehicle.makeDate, "#A10");
1243 Assert.IsNull (group.MyVehicle.weight, "#A11");
1245 xml = string.Format (CultureInfo.InvariantCulture,
1247 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' CreationDate='2002-05-02' GroupNumber='ZDI=' id='id1'>" +
1248 "<PosInt xsi:type='xsd:nonNegativeInteger'>10000</PosInt>" +
1249 "<Grouptype xsi:type='GroupType'>Large</Grouptype>" +
1250 "<MyVehicle href='#id2' />" +
1252 "<Car xmlns:d2p1='{1}' id='id2' d2p1:type='Car'>" +
1253 "<weight xmlns:q2='{0}' d2p1:type='q2:string'>450</weight>" +
1256 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1258 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1259 xtr.ReadStartElement ("Wrapper");
1261 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1263 Assert.AreEqual (new DateTime (2002, 5, 2), group.Today, "#B1");
1264 Assert.IsNull (group.GroupName, "#B2");
1265 Assert.AreEqual (new byte[] { 0x64, 0x32 }, group.GroupNumber, "#B3");
1266 Assert.AreEqual (GroupType.B, group.Grouptype, "#B4");
1267 Assert.AreEqual ("10000", group.PostitiveInt, "#B5");
1268 Assert.IsFalse (group.IgnoreThis, "#B6");
1269 Assert.IsNotNull (group.MyVehicle, "#B7");
1270 Assert.AreEqual (typeof (Car), group.MyVehicle.GetType (), "#B8");
1271 Assert.IsNull (group.MyVehicle.licenseNumber, "#B9");
1272 Assert.AreEqual (DateTime.MinValue, group.MyVehicle.makeDate, "#B10");
1273 Assert.AreEqual ("450", group.MyVehicle.weight, "#B11");
1275 xml = string.Format (CultureInfo.InvariantCulture,
1277 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' />" +
1279 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1281 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1282 xtr.ReadStartElement ("Wrapper");
1284 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1286 Assert.AreEqual (DateTime.MinValue, group.Today, "#C1");
1287 Assert.IsNull (group.GroupName, "#C2");
1288 Assert.AreEqual (null, group.GroupNumber, "#C3");
1289 Assert.AreEqual (GroupType.A, group.Grouptype, "#C4");
1290 Assert.IsNull (group.PostitiveInt, "#C5");
1291 Assert.IsFalse (group.IgnoreThis, "#C6");
1292 Assert.IsNull (group.MyVehicle, "#C7");
1294 xml = string.Format (CultureInfo.InvariantCulture,
1296 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1297 "<Grouptype xsi:type='GroupType'>666</Grouptype>" +
1300 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1302 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1303 xtr.ReadStartElement ("Wrapper");
1306 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1307 Assert.Fail ("#D1");
1309 catch (InvalidOperationException ex) {
1310 // There is an error in XML document (1, 174)
1311 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1312 Assert.IsNotNull (ex.Message, "#D3");
1313 Assert.IsNotNull (ex.InnerException, "#D4");
1315 // '666' is not a valid value for GroupType
1316 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D5");
1317 Assert.IsNotNull (ex.InnerException.Message, "#D6");
1318 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'666'") != -1, "#D7");
1319 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#D8");
1320 Assert.IsNull (ex.InnerException.InnerException, "#D9");
1323 xml = string.Format (CultureInfo.InvariantCulture,
1325 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1326 "<Grouptype xsi:type='GroupType'>Garbage</Grouptype>" +
1329 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance");
1331 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1332 xtr.ReadStartElement ("Wrapper");
1335 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1336 Assert.Fail ("#E1");
1338 catch (InvalidOperationException ex) {
1339 // There is an error in XML document (1, 178)
1340 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
1341 Assert.IsNotNull (ex.Message, "#E3");
1342 Assert.IsNotNull (ex.InnerException, "#E4");
1344 // 'Garbage' is not a valid value for GroupType
1345 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#E5");
1346 Assert.IsNotNull (ex.InnerException.Message, "#E6");
1347 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'Garbage'") != -1, "#E7");
1348 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#E8");
1349 Assert.IsNull (ex.InnerException.InnerException, "#E9");
1352 xml = string.Format (CultureInfo.InvariantCulture,
1354 "<Group xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1'>" +
1355 "<Grouptype xsi:type='GroupType'>{2}</Grouptype>" +
1358 "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/2001/XMLSchema-instance",
1359 ((int) GroupType.B).ToString (CultureInfo.InvariantCulture));
1361 xtr = new XmlTextReader (xml, XmlNodeType.Document, null);
1362 xtr.ReadStartElement ("Wrapper");
1365 group = (Group) DeserializeEncoded (typeof (Group), xtr);
1366 Assert.Fail ("#F1");
1368 catch (InvalidOperationException ex) {
1369 // There is an error in XML document (1, 172)
1370 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
1371 Assert.IsNotNull (ex.Message, "#F3");
1372 Assert.IsNotNull (ex.InnerException, "#F4");
1374 // '1' is not a valid value for GroupType
1375 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#F5");
1376 Assert.IsNotNull (ex.InnerException.Message, "#F6");
1377 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'1'") != -1, "#F7");
1378 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (GroupType).Name) != -1, "#F8");
1379 Assert.IsNull (ex.InnerException.InnerException, "#F9");
1384 public void TestDeserialize_ZeroFlagEnum ()
1388 e = (ZeroFlagEnum) Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum />");
1389 Assert.AreEqual (ZeroFlagEnum.e0, e, "#A1");
1390 e = (ZeroFlagEnum) Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum></ZeroFlagEnum>");
1391 Assert.AreEqual (ZeroFlagEnum.e0, e, "#A2");
1394 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum>four</ZeroFlagEnum>");
1395 Assert.Fail ("#B1");
1397 catch (InvalidOperationException ex) {
1398 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
1399 Assert.IsNotNull (ex.InnerException, "#B3");
1400 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#B4");
1401 Assert.IsNotNull (ex.InnerException.Message, "#B5");
1402 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#B6");
1403 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#B7");
1407 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum> o<n>e four </ZeroFlagEnum>");
1408 Assert.Fail ("#C1");
1410 catch (InvalidOperationException ex) {
1411 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
1412 Assert.IsNotNull (ex.InnerException, "#C3");
1413 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#C4");
1414 Assert.IsNotNull (ex.InnerException.Message, "#C5");
1415 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#C6");
1416 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#C7");
1420 Deserialize (typeof (ZeroFlagEnum), "<ZeroFlagEnum>four o<n>e</ZeroFlagEnum>");
1421 Assert.Fail ("#D1");
1423 catch (InvalidOperationException ex) {
1424 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
1425 Assert.IsNotNull (ex.InnerException, "#D3");
1426 Assert.AreEqual (typeof (InvalidOperationException), ex.InnerException.GetType (), "#D4");
1427 Assert.IsNotNull (ex.InnerException.Message, "#D5");
1428 Assert.IsTrue (ex.InnerException.Message.IndexOf ("'four'") != -1, "#D6");
1429 Assert.IsTrue (ex.InnerException.Message.IndexOf (typeof (ZeroFlagEnum).FullName) != -1, "#D7");
1434 public void TestDeserialize_PrimitiveTypesContainer ()
1436 Deserialize (typeof (PrimitiveTypesContainer), string.Format (CultureInfo.InvariantCulture,
1437 "<?xml version='1.0' encoding='utf-16'?>" +
1438 "<PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' xmlns='{2}'>" +
1439 "<Number xsi:type='xsd:int'>2004</Number>" +
1440 "<Name xsi:type='xsd:string'>some name</Name>" +
1441 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1442 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1443 "<PathSeparatorCharacter xmlns:q1='{3}' xsi:type='q1:char'>47</PathSeparatorCharacter>" +
1444 "</PrimitiveTypesContainer>", XmlSchemaNamespace,
1445 XmlSchemaInstanceNamespace, ANamespace, WsdlTypesNamespace));
1446 Assert.AreEqual (typeof (PrimitiveTypesContainer), result.GetType (), "#A1");
1448 PrimitiveTypesContainer deserialized = (PrimitiveTypesContainer) result;
1449 Assert.AreEqual (2004, deserialized.Number, "#A2");
1450 Assert.AreEqual ("some name", deserialized.Name, "#A3");
1451 Assert.AreEqual ((byte) 56, deserialized.Index, "#A4");
1452 Assert.AreEqual (new byte[] { 243, 15 }, deserialized.Password, "#A5");
1453 Assert.AreEqual ('/', deserialized.PathSeparatorCharacter, "#A6");
1455 DeserializeEncoded (typeof (PrimitiveTypesContainer), string.Format (CultureInfo.InvariantCulture,
1456 "<?xml version='1.0' encoding='utf-16'?>" +
1457 "<q1:PrimitiveTypesContainer xmlns:xsd='{0}' xmlns:xsi='{1}' id='id1' xmlns:q1='{2}'>" +
1458 "<Number xsi:type='xsd:int'>2004</Number>" +
1459 "<Name xsi:type='xsd:string'>some name</Name>" +
1460 "<Index xsi:type='xsd:unsignedByte'>56</Index>" +
1461 "<Password xsi:type='xsd:base64Binary'>8w8=</Password>" +
1462 "<PathSeparatorCharacter xmlns:q1='{3}' xsi:type='q1:char'>47</PathSeparatorCharacter>" +
1463 "</q1:PrimitiveTypesContainer>", XmlSchemaNamespace,
1464 XmlSchemaInstanceNamespace, AnotherNamespace, WsdlTypesNamespace));
1465 Assert.AreEqual (typeof (PrimitiveTypesContainer), result.GetType (), "#B1");
1467 deserialized = (PrimitiveTypesContainer) result;
1468 Assert.AreEqual (2004, deserialized.Number, "#B2");
1469 Assert.AreEqual ("some name", deserialized.Name, "#B3");
1470 Assert.AreEqual ((byte) 56, deserialized.Index, "#B4");
1471 Assert.AreEqual (new byte[] { 243, 15 }, deserialized.Password, "#B5");
1472 Assert.AreEqual ('/', deserialized.PathSeparatorCharacter, "#B6");