DeserializeTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  122. }