XmlTextWriterTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 MonoTests.System.Xml
  15. {
  16. public class XmlTextWriterTests : TestCase
  17. {
  18. public XmlTextWriterTests () : base ("MonoTests.System.Xml.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. xtw.QuoteChar = '\'';
  27. }
  28. private string StringWriterText
  29. {
  30. get { return sw.GetStringBuilder ().ToString (); }
  31. }
  32. public void TestAttributeNamespacesNonNamespaceAttributeBefore ()
  33. {
  34. xtw.WriteStartElement ("foo");
  35. xtw.WriteAttributeString("bar", "baz");
  36. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  37. AssertEquals ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
  38. }
  39. public void TestAttributeNamespacesNonNamespaceAttributeAfter ()
  40. {
  41. xtw.WriteStartElement ("foo");
  42. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  43. xtw.WriteAttributeString("bar", "baz");
  44. AssertEquals ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
  45. }
  46. public void TestAttributeNamespacesThreeParamWithNullInNamespaceParam ()
  47. {
  48. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  49. AssertEquals ("xmlns='http://abc.def'", StringWriterText);
  50. }
  51. public void TestAttributeNamespacesThreeParamWithTextInNamespaceParam ()
  52. {
  53. try
  54. {
  55. xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
  56. }
  57. catch (ArgumentException) {}
  58. }
  59. public void TestAttributeNamespacesWithNullInNamespaceParam ()
  60. {
  61. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  62. AssertEquals ("xmlns:abc='http://abc.def'", StringWriterText);
  63. }
  64. public void TestAttributeNamespacesWithTextInNamespaceParam ()
  65. {
  66. try {
  67. xtw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
  68. } catch (ArgumentException) {}
  69. }
  70. public void TestAttributeNamespacesXmlnsXmlns ()
  71. {
  72. xtw.WriteStartElement ("foo");
  73. try
  74. {
  75. xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  76. Fail ("Expected an ArgumentException to be thrown.");
  77. }
  78. catch (ArgumentException) {}
  79. }
  80. public void TestAttributeWriteAttributeString ()
  81. {
  82. xtw.WriteStartElement ("foo");
  83. xtw.WriteAttributeString ("foo", "bar");
  84. AssertEquals ("<foo foo='bar'", StringWriterText);
  85. xtw.WriteAttributeString ("bar", "");
  86. AssertEquals ("<foo foo='bar' bar=''", StringWriterText);
  87. xtw.WriteAttributeString ("baz", null);
  88. AssertEquals ("<foo foo='bar' bar='' baz=''", StringWriterText);
  89. // TODO: Why does this pass Microsoft?
  90. xtw.WriteAttributeString ("", "quux");
  91. AssertEquals ("<foo foo='bar' bar='' baz='' ='quux'", StringWriterText);
  92. // TODO: Why does this pass Microsoft?
  93. xtw.WriteAttributeString (null, "quuux");
  94. AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
  95. }
  96. public void TestAttributeWriteAttributeStringNotInsideOpenStartElement ()
  97. {
  98. xtw.WriteStartElement ("foo");
  99. xtw.WriteString ("bar");
  100. try
  101. {
  102. xtw.WriteAttributeString ("baz", "quux");
  103. Fail ("Expected an InvalidOperationException to be thrown.");
  104. }
  105. catch (InvalidOperationException) {}
  106. }
  107. public void TestAttributeWriteAttributeStringWithoutParentElement ()
  108. {
  109. xtw.WriteAttributeString ("foo", "bar");
  110. AssertEquals ("foo='bar'", StringWriterText);
  111. xtw.WriteAttributeString ("baz", "quux");
  112. AssertEquals ("foo='bar' baz='quux'", StringWriterText);
  113. }
  114. public void TestCDataValid ()
  115. {
  116. xtw.WriteCData ("foo");
  117. AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", StringWriterText);
  118. }
  119. public void TestCDataInvalid ()
  120. {
  121. try {
  122. xtw.WriteCData("foo]]>bar");
  123. Fail("Should have thrown an ArgumentException.");
  124. }
  125. catch (ArgumentException) { }
  126. }
  127. public void TestCloseOpenElements ()
  128. {
  129. xtw.WriteStartElement("foo");
  130. xtw.WriteStartElement("bar");
  131. xtw.WriteStartElement("baz");
  132. xtw.Close();
  133. AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>", StringWriterText);
  134. }
  135. public void TestCloseWriteAfter ()
  136. {
  137. xtw.WriteElementString ("foo", "bar");
  138. xtw.Close ();
  139. // WriteEndElement and WriteStartDocument aren't tested here because
  140. // they will always throw different exceptions besides 'The Writer is closed.'
  141. // and there are already tests for those exceptions.
  142. try {
  143. xtw.WriteCData ("foo");
  144. Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  145. }
  146. catch (InvalidOperationException e) {
  147. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  148. }
  149. try {
  150. xtw.WriteComment ("foo");
  151. Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  152. }
  153. catch (InvalidOperationException e) {
  154. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  155. }
  156. try {
  157. xtw.WriteProcessingInstruction ("foo", "bar");
  158. Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  159. }
  160. catch (InvalidOperationException e) {
  161. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  162. }
  163. try {
  164. xtw.WriteStartElement ("foo", "bar", "baz");
  165. Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  166. }
  167. catch (InvalidOperationException e) {
  168. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  169. }
  170. try
  171. {
  172. xtw.WriteAttributeString ("foo", "bar");
  173. Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  174. }
  175. catch (InvalidOperationException e)
  176. {
  177. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  178. }
  179. try {
  180. xtw.WriteString ("foo");
  181. Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  182. }
  183. catch (InvalidOperationException e) {
  184. AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  185. }
  186. }
  187. public void TestCommentValid ()
  188. {
  189. xtw.WriteComment ("foo");
  190. AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", StringWriterText);
  191. }
  192. public void TestCommentInvalid ()
  193. {
  194. try {
  195. xtw.WriteComment("foo-");
  196. Fail("Should have thrown an ArgumentException.");
  197. }
  198. catch (ArgumentException) { }
  199. try {
  200. xtw.WriteComment("foo-->bar");
  201. Fail("Should have thrown an ArgumentException.");
  202. }
  203. catch (ArgumentException) { }
  204. }
  205. public void TestConstructorsAndBaseStream ()
  206. {
  207. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
  208. MemoryStream ms;
  209. StreamReader sr;
  210. XmlTextWriter xtw;
  211. ms = new MemoryStream ();
  212. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  213. xtw.WriteStartDocument ();
  214. xtw.Flush ();
  215. ms.Seek (0, SeekOrigin.Begin);
  216. sr = new StreamReader (ms);
  217. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\"?>", sr.ReadToEnd ());
  218. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  219. ms = new MemoryStream ();
  220. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  221. xtw.WriteStartDocument (true);
  222. xtw.Flush ();
  223. ms.Seek (0, SeekOrigin.Begin);
  224. sr = new StreamReader (ms);
  225. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  226. ms = new MemoryStream ();
  227. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  228. xtw.WriteStartDocument ();
  229. xtw.Flush ();
  230. ms.Seek (0, SeekOrigin.Begin);
  231. sr = new StreamReader (ms);
  232. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  233. ms = new MemoryStream ();
  234. xtw = new XmlTextWriter (ms, null);
  235. xtw.WriteStartDocument ();
  236. xtw.Flush ();
  237. ms.Seek (0, SeekOrigin.Begin);
  238. sr = new StreamReader (ms);
  239. AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  240. ms = new MemoryStream ();
  241. xtw = new XmlTextWriter (ms, null);
  242. xtw.WriteStartDocument (true);
  243. xtw.Flush ();
  244. ms.Seek (0, SeekOrigin.Begin);
  245. sr = new StreamReader (ms);
  246. AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  247. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  248. }
  249. public void TestDocumentStart ()
  250. {
  251. xtw.WriteStartDocument ();
  252. AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
  253. try
  254. {
  255. xtw.WriteStartDocument ();
  256. Fail("Should have thrown an InvalidOperationException.");
  257. }
  258. catch (InvalidOperationException e) {
  259. AssertEquals ("Exception message is incorrect.",
  260. "WriteStartDocument should be the first call.", e.Message);
  261. }
  262. xtw = new XmlTextWriter (sw = new StringWriter ());
  263. xtw.QuoteChar = '\'';
  264. xtw.WriteStartDocument (true);
  265. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
  266. xtw = new XmlTextWriter (sw = new StringWriter ());
  267. xtw.QuoteChar = '\'';
  268. xtw.WriteStartDocument (false);
  269. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
  270. }
  271. public void TestElementEmpty ()
  272. {
  273. xtw.WriteStartElement ("foo");
  274. xtw.WriteEndElement ();
  275. AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
  276. }
  277. public void TestElementWriteElementString ()
  278. {
  279. xtw.WriteElementString ("foo", "bar");
  280. AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
  281. xtw.WriteElementString ("baz", "");
  282. AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
  283. xtw.WriteElementString ("quux", null);
  284. AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
  285. xtw.WriteElementString ("", "quuux");
  286. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  287. xtw.WriteElementString (null, "quuuux");
  288. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  289. }
  290. public void TestFormatting ()
  291. {
  292. xtw.Formatting = Formatting.Indented;
  293. xtw.WriteStartDocument ();
  294. xtw.WriteStartElement ("foo");
  295. xtw.WriteElementString ("bar", "");
  296. xtw.Close ();
  297. AssertEquals ("<?xml version='1.0' encoding='utf-16'?>\r\n<foo>\r\n <bar />\r\n</foo>", StringWriterText);
  298. }
  299. public void TestFormattingInvalidXmlForFun ()
  300. {
  301. xtw.Formatting = Formatting.Indented;
  302. xtw.IndentChar = 'x';
  303. xtw.WriteStartDocument ();
  304. xtw.WriteStartElement ("foo");
  305. xtw.WriteStartElement ("bar");
  306. xtw.WriteElementString ("baz", "");
  307. xtw.Close ();
  308. AssertEquals ("<?xml version='1.0' encoding='utf-16'?>\r\n<foo>\r\nxx<bar>\r\nxxxx<baz />\r\nxx</bar>\r\n</foo>", StringWriterText);
  309. }
  310. public void TestFormattingFromRemarks ()
  311. {
  312. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  313. xtw.Formatting = Formatting.Indented;
  314. xtw.WriteStartElement ("ol");
  315. xtw.WriteStartElement ("li");
  316. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  317. xtw.WriteElementString ("b", "E");
  318. xtw.WriteElementString ("i", "lephant");
  319. xtw.WriteString (" walks slowly.");
  320. xtw.WriteEndElement ();
  321. xtw.WriteEndElement ();
  322. AssertEquals ("<ol>\r\n <li>The big <b>E</b><i>lephant</i> walks slowly.</li>\r\n</ol>", StringWriterText);
  323. }
  324. public void TestLookupPrefix ()
  325. {
  326. xtw.WriteStartElement ("root");
  327. xtw.WriteStartElement ("one");
  328. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  329. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  330. AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
  331. AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  332. xtw.WriteEndElement ();
  333. xtw.WriteStartElement ("two");
  334. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  335. xtw.WriteString("quux");
  336. AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  337. AssertNull (xtw.LookupPrefix ("http://abc.def"));
  338. AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
  339. AssertNull (xtw.LookupPrefix ("http://bogus"));
  340. }
  341. public void TestNamespacesAttributesPassingInNamespaces ()
  342. {
  343. xtw.Namespaces = false;
  344. xtw.WriteStartElement ("foo");
  345. // These shouldn't throw any exceptions since they don't pass in
  346. // a namespace.
  347. xtw.WriteAttributeString ("bar", "baz");
  348. xtw.WriteAttributeString ("", "a", "", "b");
  349. xtw.WriteAttributeString (null, "c", "", "d");
  350. xtw.WriteAttributeString ("", "e", null, "f");
  351. xtw.WriteAttributeString (null, "g", null, "h");
  352. AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  353. // These should throw ArgumentException because they pass in a
  354. // namespace when Namespaces = false.
  355. }
  356. public void TestNamespacesElementsPassingInNamespaces ()
  357. {
  358. xtw.Namespaces = false;
  359. // These shouldn't throw any exceptions since they don't pass in
  360. // a namespace.
  361. xtw.WriteElementString ("foo", "bar");
  362. xtw.WriteStartElement ("baz");
  363. xtw.WriteStartElement ("quux", "");
  364. xtw.WriteStartElement ("quuux", null);
  365. xtw.WriteStartElement (null, "a", null);
  366. xtw.WriteStartElement (null, "b", "");
  367. xtw.WriteStartElement ("", "c", null);
  368. xtw.WriteStartElement ("", "d", "");
  369. AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  370. // These should throw ArgumentException because they pass in a
  371. // namespace when Namespaces = false.
  372. try {
  373. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  374. Fail ("Expected an ArgumentException.");
  375. } catch (ArgumentException) {}
  376. try {
  377. xtw.WriteStartElement ("foo", "http://netsack.com/");
  378. Fail ("Expected an ArgumentException.");
  379. } catch (ArgumentException) {}
  380. try {
  381. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  382. Fail ("Expected an ArgumentException.");
  383. } catch (ArgumentException) {}
  384. try {
  385. xtw.WriteStartElement ("foo", "bar", null);
  386. Fail ("Expected an ArgumentException.");
  387. } catch (ArgumentException) {}
  388. try {
  389. xtw.WriteStartElement ("foo", "bar", "");
  390. Fail ("Expected an ArgumentException.");
  391. } catch (ArgumentException) {}
  392. try {
  393. xtw.WriteStartElement ("foo", "", "");
  394. Fail ("Expected an ArgumentException.");
  395. } catch (ArgumentException) {}
  396. }
  397. public void TestNamespacesNoNamespaceClearsDefaultNamespace ()
  398. {
  399. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  400. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  401. xtw.WriteElementString("baz", String.Empty, String.Empty);
  402. xtw.WriteEndElement();
  403. xtw.WriteEndElement();
  404. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  405. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
  406. }
  407. public void TestNamespacesPrefix ()
  408. {
  409. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  410. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  411. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  412. xtw.WriteEndElement ();
  413. xtw.WriteEndElement ();
  414. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  415. "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
  416. }
  417. public void TestNamespacesPrefixWithEmptyAndNullNamespace ()
  418. {
  419. try {
  420. xtw.WriteStartElement ("foo", "bar", "");
  421. Fail ("Should have thrown an ArgumentException.");
  422. } catch (ArgumentException) {}
  423. try
  424. {
  425. xtw.WriteStartElement ("foo", "bar", null);
  426. Fail ("Should have thrown an ArgumentException.");
  427. }
  428. catch (ArgumentException) {}
  429. }
  430. public void TestNamespacesSettingWhenWriteStateNotStart ()
  431. {
  432. xtw.WriteStartElement ("foo");
  433. try
  434. {
  435. xtw.Namespaces = false;
  436. Fail ("Expected an InvalidOperationException.");
  437. }
  438. catch (InvalidOperationException) {}
  439. AssertEquals (true, xtw.Namespaces);
  440. }
  441. public void TestProcessingInstructionValid ()
  442. {
  443. xtw.WriteProcessingInstruction("foo", "bar");
  444. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
  445. }
  446. public void TestProcessingInstructionInvalid ()
  447. {
  448. try
  449. {
  450. xtw.WriteProcessingInstruction("fo?>o", "bar");
  451. Fail("Should have thrown an ArgumentException.");
  452. }
  453. catch (ArgumentException) { }
  454. try
  455. {
  456. xtw.WriteProcessingInstruction("foo", "ba?>r");
  457. Fail("Should have thrown an ArgumentException.");
  458. }
  459. catch (ArgumentException) { }
  460. try
  461. {
  462. xtw.WriteProcessingInstruction("", "bar");
  463. Fail("Should have thrown an ArgumentException.");
  464. }
  465. catch (ArgumentException) { }
  466. try
  467. {
  468. xtw.WriteProcessingInstruction(null, "bar");
  469. Fail("Should have thrown an ArgumentException.");
  470. }
  471. catch (ArgumentException) { }
  472. }
  473. public void TestQuoteCharDoubleQuote ()
  474. {
  475. xtw.QuoteChar = '"';
  476. // version, encoding, standalone
  477. xtw.WriteStartDocument (true);
  478. // namespace declaration
  479. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  480. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  481. }
  482. public void TestQuoteCharInvalid ()
  483. {
  484. try {
  485. xtw.QuoteChar = 'x';
  486. Fail ("Should have thrown an ArgumentException.");
  487. } catch (ArgumentException) {}
  488. }
  489. public void TestWriteBase64 ()
  490. {
  491. UTF8Encoding encoding = new UTF8Encoding();
  492. byte[] fooBar = encoding.GetBytes("foobar");
  493. xtw.WriteBase64 (fooBar, 0, 6);
  494. AssertEquals("Zm9vYmFy", StringWriterText);
  495. try {
  496. xtw.WriteBase64 (fooBar, 3, 6);
  497. Fail ("Expected an Argument Exception to be thrown.");
  498. } catch (ArgumentException) {}
  499. try {
  500. xtw.WriteBase64 (fooBar, -1, 6);
  501. Fail ("Expected an Argument Exception to be thrown.");
  502. } catch (ArgumentOutOfRangeException) {}
  503. try {
  504. xtw.WriteBase64 (fooBar, 3, -1);
  505. Fail ("Expected an Argument Exception to be thrown.");
  506. } catch (ArgumentOutOfRangeException) {}
  507. try {
  508. xtw.WriteBase64 (null, 0, 6);
  509. Fail ("Expected an Argument Exception to be thrown.");
  510. } catch (ArgumentNullException) {}
  511. }
  512. public void TestWriteCharEntity ()
  513. {
  514. xtw.WriteCharEntity ('a');
  515. AssertEquals ("&#x61;", StringWriterText);
  516. xtw.WriteCharEntity ('A');
  517. AssertEquals ("&#x61;&#x41;", StringWriterText);
  518. xtw.WriteCharEntity ('1');
  519. AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
  520. xtw.WriteCharEntity ('K');
  521. AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  522. try {
  523. xtw.WriteCharEntity ((char)0xd800);
  524. } catch (ArgumentException) {}
  525. }
  526. public void TestWriteEndAttribute ()
  527. {
  528. try
  529. {
  530. xtw.WriteEndAttribute ();
  531. Fail ("Should have thrown an InvalidOperationException.");
  532. }
  533. catch (InvalidOperationException) {}
  534. }
  535. public void TestWriteEndDocument ()
  536. {
  537. try {
  538. xtw.WriteEndDocument ();
  539. Fail ("Expected an ArgumentException.");
  540. } catch (ArgumentException) {}
  541. xtw.WriteStartDocument ();
  542. try
  543. {
  544. xtw.WriteEndDocument ();
  545. Fail ("Expected an ArgumentException.");
  546. }
  547. catch (ArgumentException) {}
  548. xtw.WriteStartElement ("foo");
  549. xtw.WriteStartAttribute ("bar", null);
  550. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  551. xtw.WriteEndDocument ();
  552. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  553. AssertEquals (WriteState.Start, xtw.WriteState);
  554. }
  555. public void TestWriteEndElement ()
  556. {
  557. try {
  558. xtw.WriteEndElement ();
  559. Fail ("Should have thrown an InvalidOperationException.");
  560. } catch (InvalidOperationException e) {
  561. AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
  562. }
  563. xtw.WriteStartElement ("foo");
  564. xtw.WriteEndElement ();
  565. AssertEquals ("<foo />", StringWriterText);
  566. xtw.WriteStartElement ("bar");
  567. xtw.WriteStartAttribute ("baz", null);
  568. xtw.WriteEndElement ();
  569. AssertEquals ("<foo /><bar baz='' />", StringWriterText);
  570. }
  571. public void TestFullEndElement ()
  572. {
  573. xtw.WriteStartElement ("foo");
  574. xtw.WriteFullEndElement ();
  575. AssertEquals ("<foo></foo>", StringWriterText);
  576. xtw.WriteStartElement ("bar");
  577. xtw.WriteAttributeString ("foo", "bar");
  578. xtw.WriteFullEndElement ();
  579. AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  580. xtw.WriteStartElement ("baz");
  581. xtw.WriteStartAttribute ("bar", null);
  582. xtw.WriteFullEndElement ();
  583. AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  584. }
  585. public void TestWriteRaw ()
  586. {
  587. xtw.WriteRaw("&<>\"'");
  588. AssertEquals ("&<>\"'", StringWriterText);
  589. xtw.WriteRaw(null);
  590. AssertEquals ("&<>\"'", StringWriterText);
  591. xtw.WriteRaw("");
  592. AssertEquals ("&<>\"'", StringWriterText);
  593. }
  594. public void TestWriteRawInvalidInAttribute ()
  595. {
  596. xtw.WriteStartElement ("foo");
  597. xtw.WriteStartAttribute ("bar", null);
  598. xtw.WriteRaw ("&<>\"'");
  599. xtw.WriteEndAttribute ();
  600. xtw.WriteEndElement ();
  601. AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
  602. }
  603. public void TestWriteState ()
  604. {
  605. AssertEquals (WriteState.Start, xtw.WriteState);
  606. xtw.WriteStartDocument ();
  607. AssertEquals (WriteState.Prolog, xtw.WriteState);
  608. xtw.WriteStartElement ("root");
  609. AssertEquals (WriteState.Element, xtw.WriteState);
  610. xtw.WriteElementString ("foo", "bar");
  611. AssertEquals (WriteState.Content, xtw.WriteState);
  612. xtw.Close ();
  613. AssertEquals (WriteState.Closed, xtw.WriteState);
  614. }
  615. public void TestWriteString ()
  616. {
  617. xtw.WriteStartDocument ();
  618. try {
  619. xtw.WriteString("foo");
  620. } catch (InvalidOperationException) {}
  621. // Testing attribute values
  622. xtw.WriteStartElement ("foo");
  623. xtw.WriteAttributeString ("bar", "&<>");
  624. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  625. // When QuoteChar is single quote then replaces single quotes within attributes
  626. // but not double quotes.
  627. sw.GetStringBuilder ().Remove (0, sw.GetStringBuilder ().Length);
  628. xtw.WriteStartElement ("foo");
  629. xtw.WriteAttributeString ("bar", "\"baz\"");
  630. xtw.WriteAttributeString ("quux", "'baz'");
  631. AssertEquals ("><foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  632. // When QuoteChar is double quote then replaces double quotes within attributes
  633. // but not single quotes.
  634. xtw.QuoteChar = '"';
  635. sw.GetStringBuilder ().Remove (0, sw.GetStringBuilder ().Length);
  636. xtw.WriteStartElement ("foo");
  637. xtw.WriteAttributeString ("bar", "\"baz\"");
  638. xtw.WriteAttributeString ("quux", "'baz'");
  639. AssertEquals ("><foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  640. // Testing element values
  641. xtw.QuoteChar = '\'';
  642. sw.GetStringBuilder ().Remove (0, sw.GetStringBuilder ().Length);
  643. xtw.WriteElementString ("foo", "&<>\"'");
  644. AssertEquals ("><foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  645. }
  646. public void TestXmlLang ()
  647. {
  648. AssertNull (xtw.XmlLang);
  649. xtw.WriteStartElement ("foo");
  650. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  651. AssertEquals ("langfoo", xtw.XmlLang);
  652. AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
  653. xtw.WriteAttributeString ("boo", "yah");
  654. AssertEquals ("langfoo", xtw.XmlLang);
  655. AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  656. xtw.WriteElementString("bar", "baz");
  657. AssertEquals ("langfoo", xtw.XmlLang);
  658. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  659. xtw.WriteString("baz");
  660. AssertEquals ("langfoo", xtw.XmlLang);
  661. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  662. xtw.WriteStartElement ("quux");
  663. xtw.WriteStartAttribute ("xml", "lang", null);
  664. AssertEquals ("langfoo", xtw.XmlLang);
  665. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  666. xtw.WriteString("langbar");
  667. AssertEquals ("langfoo", xtw.XmlLang);
  668. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  669. xtw.WriteEndAttribute ();
  670. AssertEquals ("langbar", xtw.XmlLang);
  671. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  672. // check if xml:lang repeats output even if same as current scope.
  673. xtw.WriteStartElement ("joe");
  674. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  675. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  676. xtw.WriteElementString ("quuux", "squonk");
  677. AssertEquals ("langbar", xtw.XmlLang);
  678. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  679. xtw.WriteEndElement ();
  680. xtw.WriteEndElement ();
  681. AssertEquals ("langfoo", xtw.XmlLang);
  682. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", StringWriterText);
  683. xtw.WriteEndElement ();
  684. AssertNull (xtw.XmlLang);
  685. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", StringWriterText);
  686. xtw.Close ();
  687. AssertNull (xtw.XmlLang);
  688. }
  689. // TODO: test operational aspects
  690. public void TestXmlSpace ()
  691. {
  692. xtw.WriteStartElement ("foo");
  693. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  694. xtw.WriteStartElement ("bar");
  695. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  696. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  697. AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
  698. xtw.WriteStartElement ("baz");
  699. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  700. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  701. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  702. xtw.WriteStartElement ("quux");
  703. xtw.WriteStartAttribute ("xml", "space", null);
  704. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  705. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  706. xtw.WriteString ("default");
  707. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  708. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  709. xtw.WriteEndAttribute ();
  710. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  711. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  712. xtw.WriteEndElement ();
  713. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  714. xtw.WriteEndElement ();
  715. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  716. xtw.WriteEndElement ();
  717. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  718. xtw.WriteStartElement ("quux");
  719. try {
  720. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  721. } catch (ArgumentException) {}
  722. try {
  723. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  724. } catch (ArgumentException) {}
  725. try {
  726. xtw.WriteAttributeString ("xml", "space", null, "Preserve");
  727. } catch (ArgumentException) {}
  728. try {
  729. xtw.WriteAttributeString ("xml", "space", null, "Default");
  730. } catch (ArgumentException) {}
  731. try {
  732. xtw.WriteWhitespace ("x");
  733. } catch (ArgumentException) { }
  734. }
  735. public void TestXmlSpaceRaw ()
  736. {
  737. xtw.WriteStartElement ("foo");
  738. xtw.WriteStartAttribute ("xml", "space", null);
  739. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  740. AssertEquals ("<foo xml:space='", StringWriterText);
  741. xtw.WriteString ("default");
  742. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  743. AssertEquals ("<foo xml:space='", StringWriterText);
  744. xtw.WriteEndAttribute ();
  745. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  746. AssertEquals ("<foo xml:space='default'", StringWriterText);
  747. }
  748. }
  749. }