XmlSerializationWriterTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // MonoTests.System.Xml.Serialization.XmlSerializationWriterTests
  3. //
  4. // Author: Erik LeBel <[email protected]>
  5. //
  6. // (C) Erik LeBel 2003
  7. //
  8. // FIXME add tests for callbacks
  9. // FIXME add tests for writes that generate namespaces
  10. // FIXME add test that write XmlNode objects
  11. //
  12. using System;
  13. using System.IO;
  14. using System.Xml;
  15. using System.Xml.Serialization;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.XmlSerialization
  18. {
  19. // base, common implementation of XmlSerializationWriter test harness.
  20. // the reason for this is that all auto generated namespace prefixes
  21. // of the form q# are modified by any Write* that defines a new namespace.
  22. // The result of this is that even though we redefine the string results
  23. // to exclude previous tests, the q#s will change depending on number of
  24. // namespace declarations were made prior to the perticual test. This
  25. // means that if the [Test] methods are called out of sequence, they
  26. // all start to fail. For this reason, tests that define and verify
  27. // temporary namespaces should be stored in a seperate class which protects
  28. // itself from accidental pre-definitions.
  29. public class XmlSerializarionWriterTester : XmlSerializationWriter
  30. {
  31. // appease the compiler
  32. protected override void InitCallbacks ()
  33. {
  34. }
  35. StringWriter sw;
  36. XmlTextWriter writer;
  37. [SetUp]
  38. protected void Reset()
  39. {
  40. sw = new StringWriter ();
  41. writer = new XmlTextWriter (sw);
  42. writer.QuoteChar = '\'';
  43. writer.Formatting = Formatting.None;
  44. Writer = writer;
  45. }
  46. protected string Content
  47. {
  48. get
  49. {
  50. string val = sw.GetStringBuilder().ToString();
  51. // Console.WriteLine(val);
  52. return val;
  53. }
  54. }
  55. }
  56. // this class tests the methods of the XmlSerializationWriter that
  57. // can be executed out of order.
  58. [TestFixture]
  59. public class XmlSerializationWriterSimpleTests : XmlSerializarionWriterTester
  60. {
  61. const string ANamespace = "some:urn";
  62. // These TestFrom* methods indirectly test the functionality of XmlCustomFormatter
  63. [Test]
  64. public void TestFromByteArrayBase64()
  65. {
  66. // FIXME
  67. // This should work according to Mono's API, but .NET's FromByteArrayBase64
  68. // returns a byte array.
  69. //
  70. //string val = this.FromByteArrayBase64(new byte [] {143, 144, 1, 0});
  71. //Assertion.AssertEquals(FromByteArrayBase64(null), "");
  72. //val = FromByteArrayBase64(null);
  73. //try/catch or AssertEruals?
  74. }
  75. [Test]
  76. public void TestFromByteArrayHex()
  77. {
  78. byte [] vals = {143, 144, 1, 0};
  79. Assertion.AssertEquals("8F900100", FromByteArrayHex(vals));
  80. Assertion.AssertEquals(null, FromByteArrayHex(null));
  81. }
  82. [Test]
  83. public void TestFromChar()
  84. {
  85. Assertion.AssertEquals("97", FromChar('a'));
  86. Assertion.AssertEquals("0", FromChar('\0'));
  87. Assertion.AssertEquals("10", FromChar('\n'));
  88. Assertion.AssertEquals("65281", FromChar('\uFF01'));
  89. }
  90. [Test]
  91. public void TestFromDate()
  92. {
  93. DateTime d = new DateTime();
  94. Assertion.AssertEquals("0001-01-01", FromDate(d));
  95. }
  96. [Test]
  97. public void TestFromDateTime()
  98. {
  99. DateTime d = new DateTime();
  100. Assertion.AssertEquals("0001-01-01T00:00:00.0000000", FromDateTime(d).Substring (0, 27));
  101. }
  102. [Test]
  103. public void TestFromEnum()
  104. {
  105. long[] ids = {1, 2, 3, 4};
  106. string[] values = {"one", "two", "three"};
  107. Assertion.AssertEquals("one", FromEnum(1, values, ids));
  108. Assertion.AssertEquals("", FromEnum(0, values, ids));
  109. try
  110. {
  111. string dummy = FromEnum(4, values, ids);
  112. Assertion.Fail("This should fail with an array-out-of-bunds error");
  113. }
  114. catch (Exception)
  115. {
  116. }
  117. }
  118. [Test]
  119. public void TestFromTime()
  120. {
  121. DateTime d = new DateTime();
  122. // Don't include time zone.
  123. Assertion.AssertEquals("00:00:00.0000000", FromTime(d).Substring (0, 16));
  124. }
  125. [Test]
  126. public void TestFromXmlName()
  127. {
  128. Assertion.AssertEquals("Hello", FromXmlName("Hello"));
  129. Assertion.AssertEquals("go_x0020_dogs_x0020_go", FromXmlName("go dogs go"));
  130. Assertion.AssertEquals("what_x0027_s_x0020_up", FromXmlName("what's up"));
  131. Assertion.AssertEquals("_x0031_23go", FromXmlName("123go"));
  132. Assertion.AssertEquals("Hello_x0020_what_x0027_s.up", FromXmlName("Hello what's.up"));
  133. }
  134. [Test]
  135. public void TestFromXmlNCName()
  136. {
  137. Assertion.AssertEquals("Hello", FromXmlNCName("Hello"));
  138. Assertion.AssertEquals("go_x0020_dogs_x0020_go", FromXmlNCName("go dogs go"));
  139. Assertion.AssertEquals("what_x0027_s_x0020_up", FromXmlNCName("what's up"));
  140. Assertion.AssertEquals("_x0031_23go", FromXmlNCName("123go"));
  141. Assertion.AssertEquals("Hello_x0020_what_x0027_s.up", FromXmlNCName("Hello what's.up"));
  142. }
  143. [Test]
  144. public void TestFromXmlNmToken()
  145. {
  146. Assertion.AssertEquals("Hello", FromXmlNmToken("Hello"));
  147. Assertion.AssertEquals("go_x0020_dogs_x0020_go", FromXmlNmToken("go dogs go"));
  148. Assertion.AssertEquals("what_x0027_s_x0020_up", FromXmlNmToken("what's up"));
  149. Assertion.AssertEquals("123go", FromXmlNmToken("123go"));
  150. Assertion.AssertEquals("Hello_x0020_what_x0027_s.up", FromXmlNmToken("Hello what's.up"));
  151. }
  152. [Test]
  153. public void TestFromXmlNmTokens()
  154. {
  155. Assertion.AssertEquals("Hello go dogs_go 123go what_x0027_s.up", FromXmlNmTokens("Hello go dogs_go 123go what's.up"));
  156. }
  157. [Test]
  158. public void TestWriteAttribute()
  159. {
  160. WriteStartElement("x");
  161. WriteAttribute("a", "b");
  162. WriteEndElement();
  163. Assertion.AssertEquals("<x a='b' />", Content);
  164. Reset();
  165. WriteStartElement("x");
  166. WriteAttribute("a", new byte[] {1, 2, 3});
  167. WriteEndElement();
  168. Assertion.AssertEquals("<x a='AQID' />", Content);
  169. Reset();
  170. WriteStartElement("x");
  171. WriteAttribute("a", "<b");
  172. WriteEndElement();
  173. Assertion.AssertEquals("<x a='&lt;b' />", Content);
  174. Reset();
  175. WriteStartElement("x");
  176. string typedPlaceholder = null;
  177. WriteAttribute("a", typedPlaceholder);
  178. WriteEndElement();
  179. Assertion.AssertEquals("<x />", Content);
  180. Reset();
  181. WriteStartElement("x");
  182. WriteAttribute("a", "\"");
  183. WriteEndElement();
  184. Assertion.AssertEquals("<x a='\"' />", Content);
  185. Reset();
  186. WriteStartElement("x");
  187. WriteAttribute("a", "b\nc");
  188. WriteEndElement();
  189. Assertion.AssertEquals("<x a='b&#xA;c' />", Content);
  190. Reset();
  191. WriteStartElement("x");
  192. WriteAttribute("a", ANamespace, "b");
  193. WriteEndElement();
  194. Assertion.AssertEquals("<x d1p1:a='b' xmlns:d1p1='some:urn' />", Content);
  195. }
  196. [Test]
  197. public void TestWriteElementEncoded()
  198. {
  199. // FIXME
  200. // XmlNode related
  201. }
  202. [Test]
  203. public void TestWriteElementLiteral()
  204. {
  205. // FIXME
  206. // XmlNode related
  207. }
  208. [Test]
  209. public void TestWriteElementString()
  210. {
  211. WriteElementString("x", "a");
  212. Assertion.AssertEquals("<x>a</x>", Content);
  213. Reset();
  214. WriteElementString("x", "<a");
  215. Assertion.AssertEquals("<x>&lt;a</x>", Content);
  216. }
  217. [Test]
  218. public void TestWriteElementStringRaw()
  219. {
  220. byte [] placeHolderArray = null;
  221. WriteElementStringRaw("x", placeHolderArray);
  222. Assertion.AssertEquals("", Content);
  223. Reset();
  224. WriteElementStringRaw("x", new byte[] {0, 2, 4});
  225. Assertion.AssertEquals("<x>AAIE</x>", Content);
  226. Reset();
  227. WriteElementStringRaw("x", new byte[] {});
  228. Assertion.AssertEquals("<x />", Content);
  229. // Note to reader, the output is not valid xml
  230. Reset();
  231. WriteElementStringRaw("x", "a > 13 && a < 19");
  232. Assertion.AssertEquals("<x>a > 13 && a < 19</x>", Content);
  233. }
  234. [Test]
  235. public void TestWriteEmptyTag()
  236. {
  237. WriteEmptyTag("x");
  238. Assertion.AssertEquals("<x />", Content);
  239. }
  240. [Test]
  241. public void TestWriteNamespaceDeclarations()
  242. {
  243. XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  244. WriteStartElement("x");
  245. WriteNamespaceDeclarations(ns);
  246. WriteEndElement();
  247. Assertion.AssertEquals("<x />", Content);
  248. Reset();
  249. ns.Add("mypref", ANamespace);
  250. WriteStartElement("x");
  251. WriteNamespaceDeclarations(ns);
  252. WriteEndElement();
  253. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
  254. Reset();
  255. ns.Add("ns2", "another:urn");
  256. WriteStartElement("x");
  257. WriteNamespaceDeclarations(ns);
  258. WriteEndElement();
  259. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
  260. Reset();
  261. ns.Add("ns3", "ya:urn");
  262. WriteStartElement("x");
  263. WriteNamespaceDeclarations(ns);
  264. WriteEndElement();
  265. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x xmlns:ns3='ya:urn' xmlns:ns2='another:urn' xmlns:mypref='some:urn' />"), XmlSerializerTests.Infoset(Content));
  266. }
  267. [Test]
  268. public void TestWriteNullableStringLiteral()
  269. {
  270. WriteNullableStringLiteral("x", null, null);
  271. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
  272. Reset();
  273. WriteNullableStringLiteral("x", null, "");
  274. Assertion.AssertEquals("<x />", Content);
  275. Reset();
  276. WriteNullableStringLiteral("x", null, "a<b\'c");
  277. Assertion.AssertEquals("<x>a&lt;b\'c</x>", Content);
  278. Reset();
  279. WriteNullableStringLiteral("x", ANamespace, "b");
  280. Assertion.AssertEquals("<x xmlns='some:urn'>b</x>", Content);
  281. }
  282. [Test]
  283. public void TestWriteNullableStringLiteralRaw()
  284. {
  285. WriteNullableStringLiteralRaw("x", null, new byte[] {1, 2, 244});
  286. Assertion.AssertEquals("<x>AQL0</x>", Content);
  287. }
  288. [Test]
  289. public void TestWriteNullTagEncoded()
  290. {
  291. WriteNullTagEncoded("x");
  292. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
  293. }
  294. [Test]
  295. public void TestWriteNullTagLiteral()
  296. {
  297. WriteNullTagLiteral("x");
  298. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x d1p1:nil='true' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
  299. }
  300. [Test]
  301. public void TestWriteSerializable()
  302. {
  303. // FIXME
  304. //Assertion.AssertEquals(, "");
  305. }
  306. public void TestWriteStartDocument()
  307. {
  308. Assertion.AssertEquals("", Content);
  309. WriteStartDocument();
  310. Assertion.AssertEquals("<?xml version='1.0' encoding='utf-16'?>", Content);
  311. }
  312. [Test]
  313. public void TestWriteStartElement()
  314. {
  315. WriteStartElement("x");
  316. WriteEndElement();
  317. Assertion.AssertEquals("<x />", Content);
  318. Reset();
  319. WriteStartElement("x");
  320. WriteValue("a");
  321. WriteEndElement();
  322. Assertion.AssertEquals("<x>a</x>", Content);
  323. Reset();
  324. WriteStartElement("x");
  325. WriteStartElement("y", "z");
  326. WriteEndElement();
  327. WriteEndElement();
  328. Assertion.AssertEquals("<x><y xmlns='z' /></x>", Content);
  329. Reset();
  330. WriteStartElement("x");
  331. WriteStartElement("y", "z", true);
  332. WriteEndElement();
  333. WriteEndElement();
  334. Assertion.AssertEquals("<x><q1:y xmlns:q1='z' /></x>", Content);
  335. }
  336. public void TestWriteTypedPrimitive()
  337. {
  338. // as long as WriteTypePrimitive's last argument is false, this is OK here.
  339. WriteTypedPrimitive("x", ANamespace, "hello", false);
  340. Assertion.AssertEquals("<x xmlns='some:urn'>hello</x>", Content);
  341. Reset();
  342. WriteTypedPrimitive("x", ANamespace, 10, false);
  343. Assertion.AssertEquals("<x xmlns='some:urn'>10</x>", Content);
  344. try
  345. {
  346. WriteTypedPrimitive("x", ANamespace, null, false);
  347. Assertion.Fail("Should not be able to write a null primitive");
  348. }
  349. catch (Exception)
  350. {
  351. }
  352. }
  353. public void TestWriteValue()
  354. {
  355. WriteValue("");
  356. Assertion.AssertEquals("", Content);
  357. Reset();
  358. WriteValue("hello");
  359. Assertion.AssertEquals("hello", Content);
  360. Reset();
  361. string v = null;
  362. WriteValue(v);
  363. Assertion.AssertEquals("", Content);
  364. Reset();
  365. WriteValue(new byte[] {13, 8, 99});
  366. Assertion.AssertEquals("DQhj", Content);
  367. }
  368. public void TestWriteXmlAttribute()
  369. {
  370. // FIXME
  371. // XmlNode related
  372. }
  373. public void TestWriteXsiType()
  374. {
  375. WriteStartElement("x");
  376. WriteXsiType("pref", null);
  377. WriteEndElement();
  378. Assertion.AssertEquals(XmlSerializerTests.Infoset("<x d1p1:type='pref' xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' />"), XmlSerializerTests.Infoset(Content));
  379. }
  380. }
  381. }