XmlTextWriterTests.cs 30 KB

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