DeserializeTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.Xml.DeserializationTests
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. //
  10. using System;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14. using NUnit.Framework;
  15. using MonoTests.System.Xml.TestClasses;
  16. namespace MonoTests.System.XmlSerialization
  17. {
  18. public class Sample
  19. {
  20. public string Text;
  21. public string [] ArrayText;
  22. }
  23. [TestFixture]
  24. public class DeserializationTests
  25. {
  26. object result;
  27. private object Deserialize (Type t, string xml)
  28. {
  29. StringReader sr = new StringReader (xml);
  30. XmlReader xr = new XmlTextReader (sr);
  31. return Deserialize (t, xr);
  32. }
  33. private object DeserializeEncoded (Type t, string xml)
  34. {
  35. StringReader sr = new StringReader (xml);
  36. XmlReader xr = new XmlTextReader (sr);
  37. return DeserializeEncoded (t, xr);
  38. }
  39. private object Deserialize (Type t, XmlReader xr)
  40. {
  41. XmlSerializer ser = new XmlSerializer (t);
  42. result = ser.Deserialize (xr);
  43. return result;
  44. }
  45. private object DeserializeEncoded (Type t, XmlReader xr)
  46. {
  47. SoapReflectionImporter im = new SoapReflectionImporter ();
  48. XmlTypeMapping tm = im.ImportTypeMapping (t);
  49. XmlSerializer ser = new XmlSerializer (tm);
  50. result = ser.Deserialize (xr);
  51. return result;
  52. }
  53. [Test]
  54. public void SimpleDeserialize ()
  55. {
  56. Deserialize (typeof (Sample), "<Sample><Text>Test.</Text></Sample>");
  57. Assertion.AssertEquals (typeof (Sample), result.GetType ());
  58. Sample sample = result as Sample;
  59. Assertion.AssertEquals ("Test.", sample.Text);
  60. }
  61. [Test]
  62. public void DeserializeInt ()
  63. {
  64. Deserialize (typeof (int), "<int>10</int>");
  65. Assertion.AssertEquals (typeof (int), result.GetType ());
  66. Assertion.AssertEquals (10, result);
  67. }
  68. [Test]
  69. public void DeserializeSimpleArray ()
  70. {
  71. Deserialize (typeof (Sample), "<Sample><ArrayText><string>Test1</string><string>Test2</string></ArrayText></Sample>");
  72. Assertion.AssertEquals (typeof (Sample), result.GetType ());
  73. Sample sample = result as Sample;
  74. Assertion.AssertEquals ("Test1", sample.ArrayText [0]);
  75. Assertion.AssertEquals ("Test2", sample.ArrayText [1]);
  76. }
  77. [Test]
  78. public void DeserializeEmptyEnum ()
  79. {
  80. Field f = Deserialize (typeof (Field), "<field modifiers=\"\" />") as Field;
  81. Assertion.AssertEquals (MapModifiers.Public, f.Modifiers);
  82. }
  83. [Test]
  84. public void DeserializePrivateCollection ()
  85. {
  86. MemoryStream ms = new MemoryStream ();
  87. Container c = new Container();
  88. c.Items.Add(1);
  89. XmlSerializer serializer = new XmlSerializer(typeof(Container));
  90. serializer.Serialize(ms, c);
  91. ms.Position = 0;
  92. c = (Container) serializer.Deserialize (ms);
  93. Assertion.AssertEquals (1, c.Items[0]);
  94. }
  95. [Test]
  96. [Category("NotDotNet")]
  97. [ExpectedException (typeof (InvalidOperationException))]
  98. public void DeserializeEmptyPrivateCollection ()
  99. {
  100. MemoryStream ms = new MemoryStream ();
  101. Container2 c = new Container2(true);
  102. c.Items.Add(1);
  103. XmlSerializer serializer = new XmlSerializer(typeof(Container2));
  104. serializer.Serialize(ms, c);
  105. ms.Position = 0;
  106. c = (Container2) serializer.Deserialize (ms);
  107. }
  108. [Test]
  109. [Category("NotDotNet")]
  110. public void DeserializeArrayReferences ()
  111. {
  112. string s = "<Sample xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
  113. s += "<ArrayText xmlns:n3=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"n3:Array\" n3:arrayType=\"xsd:string[2]\">";
  114. s += "<item href=\"#id-606830706\"></item>";
  115. s += "<item xsi:type=\"xsd:string\">Hola</item>";
  116. s += "</ArrayText>";
  117. s += "<string id=\"id-606830706\" xsi:type=\"xsd:string\">Adeu</string>";
  118. s += "</Sample>";
  119. DeserializeEncoded (typeof(Sample), s);
  120. }
  121. [Test]
  122. public void TestDeserializeXmlNodeArray ()
  123. {
  124. object ob = Deserialize (typeof(object), "<anyType at=\"1\"><elem1/><elem2/></anyType>");
  125. Assertion.Assert ("Is node array", ob is XmlNode[]);
  126. XmlNode[] nods = (XmlNode[]) ob;
  127. Assertion.AssertEquals ("lengh", 3, nods.Length);
  128. Assertion.Assert ("#1", nods[0] is XmlAttribute);
  129. Assertion.AssertEquals ("#2", "at", ((XmlAttribute)nods[0]).LocalName);
  130. Assertion.AssertEquals ("#3", "1", ((XmlAttribute)nods[0]).Value);
  131. Assertion.Assert ("#4", nods[1] is XmlElement);
  132. Assertion.AssertEquals ("#5", "elem1", ((XmlElement)nods[1]).LocalName);
  133. Assertion.Assert ("#6", nods[2] is XmlElement);
  134. Assertion.AssertEquals ("#7", "elem2", ((XmlElement)nods[2]).LocalName);
  135. }
  136. [Test]
  137. public void TestDeserializeXmlElement ()
  138. {
  139. object ob = Deserialize (typeof(XmlElement), "<elem/>");
  140. Assertion.Assert ("#1", ob is XmlElement);
  141. Assertion.AssertEquals ("#2", "elem", ((XmlElement)ob).LocalName);
  142. }
  143. [Test]
  144. public void TestDeserializeXmlCDataSection ()
  145. {
  146. 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>");
  147. Assertion.AssertNotNull ("#1", c.cdata);
  148. Assertion.AssertEquals ("#2", "data section contents", c.cdata.Value);
  149. }
  150. [Test]
  151. public void TestDeserializeXmlNode ()
  152. {
  153. 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>");
  154. Assertion.Assert ("#1", c.node is XmlText);
  155. Assertion.AssertEquals ("#2", "text", c.node.Value);
  156. }
  157. [Test]
  158. public void TestDeserializeChoices ()
  159. {
  160. Choices ch = (Choices) Deserialize (typeof(Choices), "<Choices><ChoiceZero>choice text</ChoiceZero></Choices>");
  161. Assertion.AssertEquals ("#1", "choice text", ch.MyChoice);
  162. Assertion.AssertEquals ("#2", ItemChoiceType.ChoiceZero, ch.ItemType);
  163. ch = (Choices) Deserialize (typeof(Choices), "<Choices><ChoiceOne>choice text</ChoiceOne></Choices>");
  164. Assertion.AssertEquals ("#1", "choice text", ch.MyChoice);
  165. Assertion.AssertEquals ("#2", ItemChoiceType.StrangeOne, ch.ItemType);
  166. ch = (Choices) Deserialize (typeof(Choices), "<Choices><ChoiceTwo>choice text</ChoiceTwo></Choices>");
  167. Assertion.AssertEquals ("#1", "choice text", ch.MyChoice);
  168. Assertion.AssertEquals ("#2", ItemChoiceType.ChoiceTwo, ch.ItemType);
  169. }
  170. [Test]
  171. public void TestDeserializeNamesWithSpaces ()
  172. {
  173. 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>");
  174. Assertion.AssertEquals ("#1", 4, ts.elem);
  175. Assertion.AssertEquals ("#2", 5, ts.attr);
  176. }
  177. [Test]
  178. public void TestDeserializeDefaults ()
  179. {
  180. ListDefaults d2 = (ListDefaults) Deserialize (typeof(ListDefaults), "<root/>");
  181. Assertion.AssertNotNull ("list2", d2.list2);
  182. Assertion.AssertNull ("list3", d2.list3);
  183. Assertion.AssertNull ("list4", d2.list4);
  184. Assertion.AssertNotNull ("list5", d2.list5);
  185. Assertion.AssertNotNull ("ed", d2.ed);
  186. Assertion.AssertNotNull ("str", d2.str);
  187. d2 = (ListDefaults) Deserialize (typeof(ListDefaults), "<root></root>");
  188. Assertion.AssertNotNull ("2 list2", d2.list2);
  189. Assertion.AssertNull ("2 list3", d2.list3);
  190. Assertion.AssertNull ("2 list4", d2.list4);
  191. Assertion.AssertNotNull ("2 list5", d2.list5);
  192. Assertion.AssertNotNull ("2 ed", d2.ed);
  193. Assertion.AssertNotNull ("2 str", d2.str);
  194. }
  195. }
  196. }