XmlTextWriterTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // System.Xml.XmlTextWriterTests
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using NUnit.Framework;
  14. namespace Ximian.Mono.Tests
  15. {
  16. public class XmlTextWriterTests : TestCase
  17. {
  18. public XmlTextWriterTests () : base ("Ximian.Mono.Tests.XmlTextWriterTests testsuite") {}
  19. public XmlTextWriterTests (string name) : base (name) {}
  20. StringWriter sw;
  21. XmlTextWriter xtw;
  22. protected override void SetUp ()
  23. {
  24. sw = new StringWriter ();
  25. xtw = new XmlTextWriter (sw);
  26. }
  27. public void TestCDataValid ()
  28. {
  29. xtw.WriteCData ("foo");
  30. AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", sw.GetStringBuilder().ToString());
  31. }
  32. public void TestCDataInvalid ()
  33. {
  34. try {
  35. xtw.WriteCData("foo]]>bar");
  36. Fail("Should have thrown an ArgumentException.");
  37. }
  38. catch (ArgumentException) { }
  39. }
  40. public void TestCloseOpenElements ()
  41. {
  42. xtw.WriteStartElement("foo");
  43. xtw.WriteStartElement("bar");
  44. xtw.WriteStartElement("baz");
  45. xtw.Close();
  46. AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>",
  47. sw.GetStringBuilder().ToString());
  48. }
  49. public void TestCloseWriteAfter ()
  50. {
  51. xtw.WriteElementString ("foo", "bar");
  52. xtw.Close ();
  53. // WriteEndElement and WriteStartDocument aren't tested here because
  54. // they will always throw different exceptions besides 'The Writer is closed.'
  55. // and there are already tests for those exceptions.
  56. try {
  57. xtw.WriteCData ("foo");
  58. Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  59. }
  60. catch (InvalidOperationException e) {
  61. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  62. }
  63. try {
  64. xtw.WriteComment ("foo");
  65. Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  66. }
  67. catch (InvalidOperationException e) {
  68. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  69. }
  70. try {
  71. xtw.WriteProcessingInstruction ("foo", "bar");
  72. Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  73. }
  74. catch (InvalidOperationException e) {
  75. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  76. }
  77. try {
  78. xtw.WriteStartElement ("foo", "bar", "baz");
  79. Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  80. }
  81. catch (InvalidOperationException e) {
  82. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  83. }
  84. try {
  85. xtw.WriteString ("foo");
  86. Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  87. }
  88. catch (InvalidOperationException e) {
  89. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  90. }
  91. }
  92. public void TestCommentValid ()
  93. {
  94. xtw.WriteComment ("foo");
  95. AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", sw.GetStringBuilder().ToString());
  96. }
  97. public void TestCommentInvalid ()
  98. {
  99. try {
  100. xtw.WriteComment("foo-");
  101. Fail("Should have thrown an ArgumentException.");
  102. }
  103. catch (ArgumentException) { }
  104. try {
  105. xtw.WriteComment("foo-->bar");
  106. Fail("Should have thrown an ArgumentException.");
  107. }
  108. catch (ArgumentException) { }
  109. }
  110. public void TestConstructorsAndBaseStream ()
  111. {
  112. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
  113. MemoryStream ms;
  114. StreamReader sr;
  115. XmlTextWriter xtw;
  116. ms = new MemoryStream ();
  117. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  118. xtw.WriteStartDocument ();
  119. xtw.Flush ();
  120. ms.Seek (0, SeekOrigin.Begin);
  121. sr = new StreamReader (ms);
  122. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>", sr.ReadToEnd ());
  123. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  124. ms = new MemoryStream ();
  125. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  126. xtw.WriteStartDocument (true);
  127. xtw.Flush ();
  128. ms.Seek (0, SeekOrigin.Begin);
  129. sr = new StreamReader (ms);
  130. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  131. ms = new MemoryStream ();
  132. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  133. xtw.WriteStartDocument ();
  134. xtw.Flush ();
  135. ms.Seek (0, SeekOrigin.Begin);
  136. sr = new StreamReader (ms);
  137. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  138. ms = new MemoryStream ();
  139. xtw = new XmlTextWriter (ms, null);
  140. xtw.WriteStartDocument ();
  141. xtw.Flush ();
  142. ms.Seek (0, SeekOrigin.Begin);
  143. sr = new StreamReader (ms);
  144. AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  145. ms = new MemoryStream ();
  146. xtw = new XmlTextWriter (ms, null);
  147. xtw.WriteStartDocument (true);
  148. xtw.Flush ();
  149. ms.Seek (0, SeekOrigin.Begin);
  150. sr = new StreamReader (ms);
  151. AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  152. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  153. }
  154. public void TestDocumentStart ()
  155. {
  156. xtw.WriteStartDocument ();
  157. AssertEquals ("XmlDeclaration is incorrect.", "<?xml version=\"1.0\" encoding=\"utf-16\"?>",
  158. sw.GetStringBuilder ().ToString ());
  159. try
  160. {
  161. xtw.WriteStartDocument ();
  162. Fail("Should have thrown an InvalidOperationException.");
  163. }
  164. catch (InvalidOperationException e) {
  165. AssertEquals ("Exception message is incorrect.",
  166. "WriteStartDocument should be the first call.", e.Message);
  167. }
  168. xtw = new XmlTextWriter (sw = new StringWriter ());
  169. xtw.WriteStartDocument (true);
  170. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>",
  171. sw.GetStringBuilder ().ToString ());
  172. xtw = new XmlTextWriter (sw = new StringWriter ());
  173. xtw.WriteStartDocument (false);
  174. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"no\"?>",
  175. sw.GetStringBuilder ().ToString ());
  176. }
  177. public void TestElementEmpty ()
  178. {
  179. xtw.WriteStartElement ("foo");
  180. xtw.WriteEndElement ();
  181. AssertEquals ("Incorrect output.", "<foo />", sw.GetStringBuilder().ToString());
  182. }
  183. public void TestElementWriteElementString ()
  184. {
  185. xtw.WriteElementString ("foo", "bar");
  186. AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", sw.GetStringBuilder().ToString());
  187. }
  188. public void TestFormatting ()
  189. {
  190. xtw.Formatting = Formatting.Indented;
  191. xtw.WriteStartDocument ();
  192. xtw.WriteStartElement ("foo");
  193. xtw.WriteElementString ("bar", "");
  194. xtw.Close ();
  195. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<foo>\r\n <bar />\r\n</foo>",
  196. sw.GetStringBuilder ().ToString ());
  197. }
  198. public void TestFormattingInvalidXmlForFun ()
  199. {
  200. xtw.Formatting = Formatting.Indented;
  201. xtw.IndentChar = 'x';
  202. xtw.WriteStartDocument ();
  203. xtw.WriteStartElement ("foo");
  204. xtw.WriteStartElement ("bar");
  205. xtw.WriteElementString ("baz", "");
  206. xtw.Close ();
  207. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<foo>\r\nxx<bar>\r\nxxxx<baz />\r\nxx</bar>\r\n</foo>",
  208. sw.GetStringBuilder ().ToString ());
  209. }
  210. public void TestFormattingFromRemarks ()
  211. {
  212. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  213. xtw.Formatting = Formatting.Indented;
  214. xtw.WriteStartElement ("ol");
  215. xtw.WriteStartElement ("li");
  216. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  217. xtw.WriteElementString ("b", "E");
  218. xtw.WriteElementString ("i", "lephant");
  219. xtw.WriteString (" walks slowly.");
  220. xtw.WriteEndElement ();
  221. xtw.WriteEndElement ();
  222. AssertEquals ("<ol>\r\n <li>The big <b>E</b><i>lephant</i> walks slowly.</li>\r\n</ol>",
  223. sw.GetStringBuilder ().ToString ());
  224. }
  225. // TODO: Need some tests for attributes with namespaces here...
  226. public void saveTestNamespacesAttributesPassingInNamespaces ()
  227. {
  228. }
  229. public void TestNamespacesElementsPassingInNamespaces ()
  230. {
  231. xtw.Namespaces = false;
  232. // These shouldn't throw any exceptions since they don't pass in
  233. // a namespace.
  234. xtw.WriteElementString ("foo", "bar");
  235. xtw.WriteStartElement ("baz");
  236. xtw.WriteStartElement ("quux", "");
  237. xtw.WriteStartElement ("quuux", null);
  238. xtw.WriteStartElement (null, "a", null);
  239. xtw.WriteStartElement (null, "b", "");
  240. xtw.WriteStartElement ("", "c", null);
  241. xtw.WriteStartElement ("", "d", "");
  242. AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", sw.GetStringBuilder ().ToString ());
  243. // These should throw ArgumentException because they pass in a
  244. // namespace when Namespaces = false.
  245. try {
  246. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  247. Fail ("Expected an ArgumentException.");
  248. } catch (ArgumentException) {}
  249. try {
  250. xtw.WriteStartElement ("foo", "http://netsack.com/");
  251. Fail ("Expected an ArgumentException.");
  252. } catch (ArgumentException) {}
  253. try {
  254. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  255. Fail ("Expected an ArgumentException.");
  256. } catch (ArgumentException) {}
  257. try {
  258. xtw.WriteStartElement ("foo", "bar", null);
  259. Fail ("Expected an ArgumentException.");
  260. } catch (ArgumentException) {}
  261. try {
  262. xtw.WriteStartElement ("foo", "bar", "");
  263. Fail ("Expected an ArgumentException.");
  264. } catch (ArgumentException) {}
  265. try {
  266. xtw.WriteStartElement ("foo", "", "");
  267. Fail ("Expected an ArgumentException.");
  268. } catch (ArgumentException) {}
  269. }
  270. public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
  271. {
  272. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  273. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  274. xtw.WriteElementString("baz", String.Empty, String.Empty);
  275. xtw.WriteEndElement();
  276. xtw.WriteEndElement();
  277. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  278. "<foo xmlns=\"http://netsack.com/\"><bar xmlns=\"\"><baz /></bar></foo>",
  279. sw.GetStringBuilder().ToString());
  280. }
  281. public void TestNamespacesPrefix ()
  282. {
  283. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  284. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  285. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  286. xtw.WriteEndElement ();
  287. xtw.WriteEndElement ();
  288. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  289. "<foo:bar xmlns:foo=\"http://netsack.com/\"><foo:baz><foo:qux /></foo:baz></foo:bar>",
  290. sw.GetStringBuilder ().ToString ());
  291. }
  292. public void TestNamespacesPrefixWithEmptyAndNullNamespace ()
  293. {
  294. try {
  295. xtw.WriteStartElement ("foo", "bar", "");
  296. Fail ("Should have thrown an ArgumentException.");
  297. } catch (ArgumentException) {}
  298. try
  299. {
  300. xtw.WriteStartElement ("foo", "bar", null);
  301. Fail ("Should have thrown an ArgumentException.");
  302. }
  303. catch (ArgumentException) {}
  304. }
  305. public void TestNamespacesSettingWhenWriteStateNotStart ()
  306. {
  307. xtw.WriteStartElement ("foo");
  308. try
  309. {
  310. xtw.Namespaces = false;
  311. Fail ("Expected an InvalidOperationException.");
  312. }
  313. catch (InvalidOperationException) {}
  314. AssertEquals (true, xtw.Namespaces);
  315. }
  316. public void TestProcessingInstructionValid ()
  317. {
  318. xtw.WriteProcessingInstruction("foo", "bar");
  319. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", sw.GetStringBuilder().ToString());
  320. }
  321. public void TestProcessingInstructionInvalid ()
  322. {
  323. try
  324. {
  325. xtw.WriteProcessingInstruction("fo?>o", "bar");
  326. Fail("Should have thrown an ArgumentException.");
  327. }
  328. catch (ArgumentException) { }
  329. try
  330. {
  331. xtw.WriteProcessingInstruction("foo", "ba?>r");
  332. Fail("Should have thrown an ArgumentException.");
  333. }
  334. catch (ArgumentException) { }
  335. try
  336. {
  337. xtw.WriteProcessingInstruction("", "bar");
  338. Fail("Should have thrown an ArgumentException.");
  339. }
  340. catch (ArgumentException) { }
  341. try
  342. {
  343. xtw.WriteProcessingInstruction(null, "bar");
  344. Fail("Should have thrown an ArgumentException.");
  345. }
  346. catch (ArgumentException) { }
  347. }
  348. public void TestQuoteCharInvalid ()
  349. {
  350. try {
  351. xtw.QuoteChar = 'x';
  352. Fail ("Should have thrown an ArgumentException.");
  353. } catch (ArgumentException) {}
  354. }
  355. public void TestWriteEndElement ()
  356. {
  357. try
  358. {
  359. xtw.WriteEndElement ();
  360. Fail ("Should have thrown an InvalidOperationException.");
  361. }
  362. catch (InvalidOperationException e) {
  363. AssertEquals ("Exception message is incorrect.",
  364. "There was no XML start tag open.", e.Message);
  365. }
  366. }
  367. public void TestWriteState ()
  368. {
  369. AssertEquals (WriteState.Start, xtw.WriteState);
  370. xtw.WriteStartDocument ();
  371. AssertEquals (WriteState.Prolog, xtw.WriteState);
  372. xtw.WriteStartElement ("root");
  373. AssertEquals (WriteState.Element, xtw.WriteState);
  374. xtw.WriteElementString ("foo", "bar");
  375. AssertEquals (WriteState.Content, xtw.WriteState);
  376. xtw.Close ();
  377. AssertEquals (WriteState.Closed, xtw.WriteState);
  378. }
  379. }
  380. }