XmlDictionaryWriterTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // XmlDictionaryWriterTest.cs
  3. //
  4. // Author:
  5. // Jonathan Pryor <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #define TRACE
  29. using System;
  30. using System.Diagnostics;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Xml
  36. {
  37. class WriteTextNodeEventArgs : EventArgs {
  38. public XmlDictionaryReader Reader;
  39. public bool IsAttribute;
  40. }
  41. class DelegatingXmlDictionaryWriter : XmlDictionaryWriter
  42. {
  43. XmlWriter d;
  44. public DelegatingXmlDictionaryWriter (XmlWriter delegateTo)
  45. {
  46. d = delegateTo;
  47. }
  48. //
  49. // XmlWriter Methods
  50. //
  51. public override WriteState WriteState {
  52. get {return d.WriteState;}
  53. }
  54. public override void Close ()
  55. {
  56. d.Close ();
  57. }
  58. public override void Flush ()
  59. {
  60. d.Flush ();
  61. }
  62. public override string LookupPrefix (string ns)
  63. {
  64. return d.LookupPrefix (ns);
  65. }
  66. public override void WriteBase64 (byte[] buffer, int index, int count)
  67. {
  68. var e = WriteBase64Event;
  69. if (e != null)
  70. e (this, new EventArgs ());
  71. d.WriteBase64 (buffer, index, count);
  72. }
  73. public override void WriteCData (string text)
  74. {
  75. d.WriteCData (text);
  76. }
  77. public override void WriteCharEntity (char ch)
  78. {
  79. d.WriteCharEntity (ch);
  80. }
  81. public override void WriteChars (char[] buffer, int index, int count)
  82. {
  83. d.WriteChars (buffer, index, count);
  84. }
  85. public override void WriteComment (string text)
  86. {
  87. d.WriteComment (text);
  88. }
  89. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  90. {
  91. d.WriteDocType (name, pubid, sysid, subset);
  92. }
  93. public override void WriteEndAttribute ()
  94. {
  95. d.WriteEndAttribute ();
  96. }
  97. public override void WriteEndDocument ()
  98. {
  99. d.WriteEndDocument ();
  100. }
  101. public override void WriteEndElement ()
  102. {
  103. d.WriteEndElement ();
  104. }
  105. public override void WriteEntityRef (string name)
  106. {
  107. d.WriteEntityRef (name);
  108. }
  109. public override void WriteFullEndElement ()
  110. {
  111. d.WriteFullEndElement ();
  112. }
  113. public override void WriteProcessingInstruction (string name, string text)
  114. {
  115. d.WriteProcessingInstruction (name, text);
  116. }
  117. public override void WriteRaw (char[] buffer, int index, int count)
  118. {
  119. d.WriteRaw (buffer, index, count);
  120. }
  121. public override void WriteRaw (string data)
  122. {
  123. d.WriteRaw (data);
  124. }
  125. public override void WriteStartAttribute (string prefix, string localName, string ns)
  126. {
  127. d.WriteStartAttribute (prefix, localName, ns);
  128. }
  129. public override void WriteStartDocument ()
  130. {
  131. d.WriteStartDocument ();
  132. }
  133. public override void WriteStartDocument (bool standalone)
  134. {
  135. d.WriteStartDocument (standalone);
  136. }
  137. public override void WriteStartElement (string prefix, string localName, string ns)
  138. {
  139. d.WriteStartElement (prefix, localName, ns);
  140. }
  141. public override void WriteString (string text)
  142. {
  143. d.WriteString (text);
  144. }
  145. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  146. {
  147. d.WriteSurrogateCharEntity (lowChar, highChar);
  148. }
  149. public override void WriteWhitespace (string ws)
  150. {
  151. d.WriteWhitespace (ws);
  152. }
  153. //
  154. // XmlDictionaryWriter methods
  155. //
  156. protected override void WriteTextNode (XmlDictionaryReader reader, bool isAttribute)
  157. {
  158. var e = WriteTextNodeEvent;
  159. if (e != null)
  160. e (this, new WriteTextNodeEventArgs { Reader = reader, IsAttribute = isAttribute });
  161. base.WriteTextNode (reader, isAttribute);
  162. }
  163. public void TestWriteTextNode (XmlDictionaryReader reader, bool isAttribute)
  164. {
  165. base.WriteTextNode (reader, isAttribute);
  166. }
  167. public event EventHandler<EventArgs> WriteBase64Event;
  168. public event EventHandler<WriteTextNodeEventArgs> WriteTextNodeEvent;
  169. }
  170. class ReleaseStreamEventArgs : EventArgs {
  171. public Stream Stream;
  172. }
  173. class DummyStreamProvider : IStreamProvider {
  174. public Stream Stream;
  175. public Stream GetStream ()
  176. {
  177. var e = GetStreamEvent;
  178. if (e != null)
  179. e (this, new EventArgs ());
  180. return Stream;
  181. }
  182. public void ReleaseStream (Stream stream)
  183. {
  184. var e = ReleaseStreamEvent;
  185. if (e != null)
  186. e (this, new ReleaseStreamEventArgs { Stream = stream });
  187. }
  188. public event EventHandler<EventArgs> GetStreamEvent;
  189. public event EventHandler<ReleaseStreamEventArgs> ReleaseStreamEvent;
  190. }
  191. [TestFixture]
  192. public class XmlDictionaryWriterTest
  193. {
  194. DelegatingXmlDictionaryWriter writer;
  195. StringBuilder contents;
  196. [SetUp]
  197. public void Setup()
  198. {
  199. var s = new XmlWriterSettings ();
  200. s.ConformanceLevel = ConformanceLevel.Fragment;
  201. s.OmitXmlDeclaration = true;
  202. writer = new DelegatingXmlDictionaryWriter (
  203. XmlWriter.Create (contents = new StringBuilder (), s));
  204. }
  205. [TearDown]
  206. public void TearDown()
  207. {
  208. contents = null;
  209. writer = null;
  210. }
  211. [Test, ExpectedException (typeof (ArgumentException))]
  212. public void WriteElementString_localNameNull ()
  213. {
  214. XmlDictionaryString localName = null, namespaceUri = null;
  215. string prefix = null, value = null;
  216. writer.WriteElementString (prefix, localName, namespaceUri, value);
  217. }
  218. [Test, ExpectedException (typeof (ArgumentException))]
  219. public void WriteElementString_PrefixWithEmptyNamespace ()
  220. {
  221. XmlDictionary d = new XmlDictionary ();
  222. XmlDictionaryString localName = d.Add ("foo"), namespaceUri = null;
  223. string prefix = "ns", value = null;
  224. writer.WriteElementString (prefix, localName, namespaceUri, value);
  225. }
  226. [Test]
  227. public void WriteElementString ()
  228. {
  229. XmlDictionary d = new XmlDictionary ();
  230. XmlDictionaryString foo = d.Add ("foo");
  231. XmlDictionaryString fooUri = d.Add ("urn:bar");
  232. string ns = "ns";
  233. //
  234. // Skipping empty string values because Mono & .NET generate
  235. // different XML: <foo /> (Mono) vs. <foo></foo> (.NET).
  236. //
  237. writer.WriteElementString (null, foo, null, "data");
  238. // writer.WriteElementString (null, foo, null, "");
  239. writer.WriteElementString (null, foo, null, null);
  240. writer.WriteElementString (null, foo, fooUri, "data");
  241. // writer.WriteElementString (null, foo, fooUri, "");
  242. writer.WriteElementString (null, foo, fooUri, null);
  243. writer.WriteElementString (ns, foo, fooUri, "data");
  244. // writer.WriteElementString (ns, foo, fooUri, "");
  245. writer.WriteElementString (ns, foo, fooUri, null);
  246. writer.Flush ();
  247. Assert.AreEqual (
  248. "<foo>data</foo><foo />" +
  249. "<foo xmlns=\"urn:bar\">data</foo><foo xmlns=\"urn:bar\" />" +
  250. "<ns:foo xmlns:ns=\"urn:bar\">data</ns:foo><ns:foo xmlns:ns=\"urn:bar\" />",
  251. contents.ToString ());
  252. }
  253. [Test, ExpectedException (typeof (ArgumentNullException))]
  254. public void WriteNode_XmlDictionaryReader_ReaderNull ()
  255. {
  256. XmlDictionaryReader reader = null;
  257. writer.WriteNode (reader, true);
  258. }
  259. [Test]
  260. public void WriteNode_XmlDictionaryReader ()
  261. {
  262. string xml = "<outer attr='a'>data<inner attr='b'>more-data</inner>end</outer>";
  263. XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader (
  264. XmlReader.Create (new StringReader (xml)));
  265. int writeTextNodeCount = 0;
  266. writer.WriteTextNodeEvent += (o, e) => {
  267. ++writeTextNodeCount;
  268. writer.WriteString ("[text]");
  269. };
  270. writer.WriteNode (reader, false);
  271. writer.Flush ();
  272. Assert.AreEqual (5, writeTextNodeCount);
  273. Assert.AreEqual (
  274. "<outer attr=\"[text]a\">[text]data<inner attr=\"[text]b\">[text]more-data</inner>[text]end</outer>",
  275. contents.ToString ());
  276. }
  277. [Test, ExpectedException (typeof (ArgumentNullException))]
  278. public void WriteNode_XmlReader_ReaderNull ()
  279. {
  280. XmlReader reader = null;
  281. writer.WriteNode (reader, true);
  282. }
  283. [Test]
  284. public void WriteNode_XmlReader ()
  285. {
  286. string xml = "<outer attr='a'>data<inner attr='b'>more-data</inner>end</outer>";
  287. XmlReader reader = XmlReader.Create (new StringReader (xml));
  288. int writeTextNodeCount = 0;
  289. writer.WriteTextNodeEvent += (o, e) => {
  290. ++writeTextNodeCount;
  291. writer.WriteString ("[text]");
  292. };
  293. writer.WriteNode (reader, false);
  294. writer.Flush ();
  295. Assert.AreEqual (0, writeTextNodeCount);
  296. Assert.AreEqual (
  297. "<outer attr=\"a\">data<inner attr=\"b\">more-data</inner>end</outer>",
  298. contents.ToString ());
  299. }
  300. [Test, ExpectedException (typeof (NullReferenceException))]
  301. public void WriteTextNode_ReaderNull ()
  302. {
  303. writer.TestWriteTextNode (null, false);
  304. }
  305. [Test]
  306. public void WriteTextNode ()
  307. {
  308. string xml = "<foo attr='a'>data</foo>";
  309. XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader (
  310. XmlReader.Create (new StringReader (xml)));
  311. reader.MoveToContent ();
  312. reader.MoveToFirstAttribute ();
  313. Assert.AreEqual (XmlNodeType.Attribute, reader.NodeType);
  314. writer.TestWriteTextNode (reader, true);
  315. writer.Flush ();
  316. Assert.AreEqual (XmlNodeType.Attribute, reader.NodeType);
  317. Assert.AreEqual ("a", contents.ToString ());
  318. contents.Length = 0;
  319. writer.TestWriteTextNode (reader, false);
  320. writer.Flush ();
  321. Assert.AreEqual ("a", contents.ToString ());
  322. Assert.AreEqual (XmlNodeType.Text, reader.NodeType);
  323. contents.Length = 0;
  324. writer.TestWriteTextNode (reader, true);
  325. writer.Flush ();
  326. Assert.AreEqual ("data", contents.ToString ());
  327. Assert.AreEqual (XmlNodeType.Text, reader.NodeType);
  328. contents.Length = 0;
  329. writer.TestWriteTextNode (reader, false);
  330. writer.Flush ();
  331. Assert.AreEqual ("data", contents.ToString ());
  332. Assert.AreNotEqual (XmlNodeType.Text, reader.NodeType);
  333. }
  334. [Test]
  335. public void WriteValue_Guid ()
  336. {
  337. writer.WriteValue (new Guid ());
  338. writer.Flush ();
  339. Assert.AreEqual (new Guid().ToString (), contents.ToString ());
  340. }
  341. [Test, ExpectedException (typeof (ArgumentNullException))]
  342. public void WriteValue_IStreamProvider_ValueNull ()
  343. {
  344. IStreamProvider value = null;
  345. writer.WriteValue (value);
  346. }
  347. [Test]
  348. public void WriteValue_IStreamProvider ()
  349. {
  350. byte[] data = Encoding.UTF8.GetBytes ("Hello, Worlds!!");
  351. int getStreamCount = 0;
  352. int releaseStreamCount = 0;
  353. var provider = new DummyStreamProvider {
  354. Stream = new MemoryStream (data)
  355. };
  356. provider.GetStreamEvent += (o, e) => { ++getStreamCount; };
  357. provider.ReleaseStreamEvent += (o, e) => {
  358. ++releaseStreamCount;
  359. Assert.IsTrue (object.ReferenceEquals (provider.Stream, e.Stream));
  360. };
  361. writer.WriteValue (provider);
  362. writer.Flush ();
  363. Assert.AreEqual (1, getStreamCount);
  364. Assert.AreEqual (1, releaseStreamCount);
  365. Assert.AreEqual (data.Length, provider.Stream.Position);
  366. Assert.AreEqual (Convert.ToBase64String (data), contents.ToString ());
  367. provider.Stream.Position = 0;
  368. writer.WriteBase64Event += (o, e) => {
  369. throw new Exception ("incomplete!");
  370. };
  371. getStreamCount = 0;
  372. releaseStreamCount = 0;
  373. Exception thrown = null;
  374. try {
  375. writer.WriteValue (provider);
  376. }
  377. catch (Exception e) {
  378. thrown = e;
  379. }
  380. Assert.IsNotNull (thrown);
  381. Assert.AreEqual (1, getStreamCount);
  382. Assert.AreEqual (0, releaseStreamCount);
  383. }
  384. [Test]
  385. public void WriteValue_TimeSpan ()
  386. {
  387. writer.WriteValue (new TimeSpan ());
  388. writer.Flush ();
  389. Assert.AreEqual (XmlConvert.ToString (new TimeSpan()), contents.ToString ());
  390. }
  391. [Test, ExpectedException (typeof (ArgumentNullException))]
  392. public void WriteValue_UniqueId_IdNull ()
  393. {
  394. UniqueId value = null;
  395. writer.WriteValue (value);
  396. }
  397. [Test]
  398. public void WriteValue_UniqueId ()
  399. {
  400. writer.WriteValue (new UniqueId (new Guid ()));
  401. writer.WriteValue (new UniqueId ("string"));
  402. writer.Flush ();
  403. Assert.AreEqual ("urn:uuid:" + new Guid ().ToString () + "string",
  404. contents.ToString ());
  405. }
  406. }
  407. }