XmlDictionaryWriterTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.Text;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Xml
  35. {
  36. class DelegatingXmlDictionaryWriter : XmlDictionaryWriter
  37. {
  38. XmlWriter d;
  39. public DelegatingXmlDictionaryWriter (XmlWriter delegateTo)
  40. {
  41. d = delegateTo;
  42. }
  43. public override WriteState WriteState {
  44. get {return d.WriteState;}
  45. }
  46. public override void Close ()
  47. {
  48. d.Close ();
  49. }
  50. public override void Flush ()
  51. {
  52. d.Flush ();
  53. }
  54. public override string LookupPrefix (string ns)
  55. {
  56. return d.LookupPrefix (ns);
  57. }
  58. public override void WriteBase64 (byte[] buffer, int index, int count)
  59. {
  60. d.WriteBase64 (buffer, index, count);
  61. }
  62. public override void WriteCData (string text)
  63. {
  64. d.WriteCData (text);
  65. }
  66. public override void WriteCharEntity (char ch)
  67. {
  68. d.WriteCharEntity (ch);
  69. }
  70. public override void WriteChars (char[] buffer, int index, int count)
  71. {
  72. d.WriteChars (buffer, index, count);
  73. }
  74. public override void WriteComment (string text)
  75. {
  76. d.WriteComment (text);
  77. }
  78. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  79. {
  80. d.WriteDocType (name, pubid, sysid, subset);
  81. }
  82. public override void WriteEndAttribute ()
  83. {
  84. d.WriteEndAttribute ();
  85. }
  86. public override void WriteEndDocument ()
  87. {
  88. d.WriteEndDocument ();
  89. }
  90. public override void WriteEndElement ()
  91. {
  92. d.WriteEndElement ();
  93. }
  94. public override void WriteEntityRef (string name)
  95. {
  96. d.WriteEntityRef (name);
  97. }
  98. public override void WriteFullEndElement ()
  99. {
  100. d.WriteFullEndElement ();
  101. }
  102. public override void WriteProcessingInstruction (string name, string text)
  103. {
  104. d.WriteProcessingInstruction (name, text);
  105. }
  106. public override void WriteRaw (char[] buffer, int index, int count)
  107. {
  108. d.WriteRaw (buffer, index, count);
  109. }
  110. public override void WriteRaw (string data)
  111. {
  112. d.WriteRaw (data);
  113. }
  114. public override void WriteStartAttribute (string prefix, string localName, string ns)
  115. {
  116. d.WriteStartAttribute (prefix, localName, ns);
  117. }
  118. public override void WriteStartDocument ()
  119. {
  120. d.WriteStartDocument ();
  121. }
  122. public override void WriteStartDocument (bool standalone)
  123. {
  124. d.WriteStartDocument (standalone);
  125. }
  126. public override void WriteStartElement (string prefix, string localName, string ns)
  127. {
  128. d.WriteStartElement (prefix, localName, ns);
  129. }
  130. public override void WriteString (string text)
  131. {
  132. d.WriteString (text);
  133. }
  134. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  135. {
  136. d.WriteSurrogateCharEntity (lowChar, highChar);
  137. }
  138. public override void WriteWhitespace (string ws)
  139. {
  140. d.WriteWhitespace (ws);
  141. }
  142. }
  143. [TestFixture]
  144. public class XmlDictionaryWriterTest
  145. {
  146. XmlDictionaryWriter writer;
  147. StringBuilder contents;
  148. [SetUp]
  149. public void Setup()
  150. {
  151. var s = new XmlWriterSettings ();
  152. s.ConformanceLevel = ConformanceLevel.Fragment;
  153. s.OmitXmlDeclaration = true;
  154. writer = new DelegatingXmlDictionaryWriter (
  155. XmlWriter.Create (contents = new StringBuilder (), s));
  156. }
  157. [TearDown]
  158. public void TearDown()
  159. {
  160. contents = null;
  161. writer = null;
  162. }
  163. [Test, ExpectedException (typeof (ArgumentException))]
  164. public void WriteElementString_localNameNull ()
  165. {
  166. XmlDictionaryString localName = null, namespaceUri = null;
  167. string prefix = null, value = null;
  168. writer.WriteElementString (prefix, localName, namespaceUri, value);
  169. }
  170. [Test, ExpectedException (typeof (ArgumentException))]
  171. public void WriteElementString_PrefixWithEmptyNamespace ()
  172. {
  173. XmlDictionary d = new XmlDictionary ();
  174. XmlDictionaryString localName = d.Add ("foo"), namespaceUri = null;
  175. string prefix = "ns", value = null;
  176. writer.WriteElementString (prefix, localName, namespaceUri, value);
  177. }
  178. [Test]
  179. public void WriteElementString ()
  180. {
  181. XmlDictionary d = new XmlDictionary ();
  182. XmlDictionaryString foo = d.Add ("foo");
  183. XmlDictionaryString fooUri = d.Add ("urn:bar");
  184. string ns = "ns";
  185. //
  186. // Skipping empty string values because Mono & .NET generate
  187. // different XML: <foo /> (Mono) vs. <foo></foo> (.NET).
  188. //
  189. writer.WriteElementString (null, foo, null, "data");
  190. // writer.WriteElementString (null, foo, null, "");
  191. writer.WriteElementString (null, foo, null, null);
  192. writer.WriteElementString (null, foo, fooUri, "data");
  193. // writer.WriteElementString (null, foo, fooUri, "");
  194. writer.WriteElementString (null, foo, fooUri, null);
  195. writer.WriteElementString (ns, foo, fooUri, "data");
  196. // writer.WriteElementString (ns, foo, fooUri, "");
  197. writer.WriteElementString (ns, foo, fooUri, null);
  198. writer.Flush ();
  199. Assert.AreEqual (
  200. "<foo>data</foo><foo />" +
  201. "<foo xmlns=\"urn:bar\">data</foo><foo xmlns=\"urn:bar\" />" +
  202. "<ns:foo xmlns:ns=\"urn:bar\">data</ns:foo><ns:foo xmlns:ns=\"urn:bar\" />",
  203. contents.ToString ());
  204. }
  205. }
  206. }