XmlTextWriterTests.cs 30 KB

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