XmlTextWriterTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. //
  2. // System.Xml.XmlTextWriterTests
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlTextWriterTests : Assertion
  20. {
  21. StringWriter sw;
  22. XmlTextWriter xtw;
  23. [SetUp]
  24. public void GetReady ()
  25. {
  26. sw = new StringWriter ();
  27. xtw = new XmlTextWriter (sw);
  28. xtw.QuoteChar = '\'';
  29. }
  30. private string StringWriterText
  31. {
  32. get { return sw.GetStringBuilder ().ToString (); }
  33. }
  34. [Test]
  35. public void AttributeNamespacesNonNamespaceAttributeBefore ()
  36. {
  37. xtw.WriteStartElement ("foo");
  38. xtw.WriteAttributeString("bar", "baz");
  39. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  40. AssertEquals ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
  41. }
  42. [Test]
  43. public void AttributeNamespacesNonNamespaceAttributeAfter ()
  44. {
  45. xtw.WriteStartElement ("foo");
  46. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  47. xtw.WriteAttributeString("bar", "baz");
  48. AssertEquals ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
  49. }
  50. [Test]
  51. public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
  52. {
  53. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  54. AssertEquals ("xmlns='http://abc.def'", StringWriterText);
  55. }
  56. [Test]
  57. public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
  58. {
  59. try
  60. {
  61. xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
  62. }
  63. catch (ArgumentException) {}
  64. }
  65. [Test]
  66. public void AttributeNamespacesWithNullInNamespaceParam ()
  67. {
  68. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  69. AssertEquals ("xmlns:abc='http://abc.def'", StringWriterText);
  70. }
  71. [Test]
  72. public void AttributeNamespacesWithTextInNamespaceParam ()
  73. {
  74. try {
  75. xtw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
  76. } catch (ArgumentException) {}
  77. }
  78. [Test]
  79. public void AttributeNamespacesXmlnsXmlns ()
  80. {
  81. xtw.WriteStartElement ("foo");
  82. try {
  83. xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  84. // This should not be allowed, even though
  85. // MS.NET doesn't treat as an error.
  86. Fail ("any prefix which name starts from \"xml\" must not be allowed.");
  87. }
  88. catch (ArgumentException e) {}
  89. xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
  90. }
  91. [Test]
  92. public void AttributeWriteAttributeString ()
  93. {
  94. xtw.WriteStartElement ("foo");
  95. xtw.WriteAttributeString ("foo", "bar");
  96. AssertEquals ("<foo foo='bar'", StringWriterText);
  97. xtw.WriteAttributeString ("bar", "");
  98. AssertEquals ("<foo foo='bar' bar=''", StringWriterText);
  99. xtw.WriteAttributeString ("baz", null);
  100. AssertEquals ("<foo foo='bar' bar='' baz=''", StringWriterText);
  101. xtw.WriteAttributeString ("hoge", "a\nb");
  102. AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b'", StringWriterText);
  103. xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
  104. AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b' fuga=' a\t&#xD;&#xA;b\t'", StringWriterText);
  105. try {
  106. // Why does this pass Microsoft?
  107. // Anyway, Mono should not allow such code.
  108. xtw.WriteAttributeString ("", "quux");
  109. // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux'", StringWriterText);
  110. Fail ("empty name not allowed.");
  111. } catch (Exception) {
  112. }
  113. try {
  114. // Why does this pass Microsoft?
  115. // Anyway, Mono should not allow such code.
  116. xtw.WriteAttributeString (null, "quuux");
  117. // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
  118. Fail ("null name not allowed.");
  119. } catch (Exception) {
  120. }
  121. }
  122. [Test]
  123. [ExpectedException (typeof (InvalidOperationException))]
  124. public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
  125. {
  126. xtw.WriteStartElement ("foo");
  127. xtw.WriteString ("bar");
  128. xtw.WriteAttributeString ("baz", "quux");
  129. }
  130. [Test]
  131. public void AttributeWriteAttributeStringWithoutParentElement ()
  132. {
  133. xtw.WriteAttributeString ("foo", "bar");
  134. AssertEquals ("foo='bar'", StringWriterText);
  135. xtw.WriteAttributeString ("baz", "quux");
  136. AssertEquals ("foo='bar' baz='quux'", StringWriterText);
  137. }
  138. [Test]
  139. public void CDataValid ()
  140. {
  141. xtw.WriteCData ("foo");
  142. AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", StringWriterText);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentException))]
  146. public void CDataInvalid ()
  147. {
  148. xtw.WriteCData("foo]]>bar");
  149. }
  150. [Test]
  151. public void CloseOpenElements ()
  152. {
  153. xtw.WriteStartElement("foo");
  154. xtw.WriteStartElement("bar");
  155. xtw.WriteStartElement("baz");
  156. xtw.Close();
  157. AssertEquals ("Close didn't write out end elements properly.", "<foo><bar><baz /></bar></foo>", StringWriterText);
  158. }
  159. [Test]
  160. public void CloseWriteAfter ()
  161. {
  162. xtw.WriteElementString ("foo", "bar");
  163. xtw.Close ();
  164. // WriteEndElement and WriteStartDocument aren't tested here because
  165. // they will always throw different exceptions besides 'The Writer is closed.'
  166. // and there are already tests for those exceptions.
  167. try {
  168. xtw.WriteCData ("foo");
  169. Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  170. }
  171. catch (InvalidOperationException e) {
  172. // Don't rely on English message assertion.
  173. // It is enough to check an exception occurs.
  174. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  175. }
  176. try {
  177. xtw.WriteComment ("foo");
  178. Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  179. }
  180. catch (InvalidOperationException e) {
  181. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  182. }
  183. try {
  184. xtw.WriteProcessingInstruction ("foo", "bar");
  185. Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  186. }
  187. catch (InvalidOperationException e) {
  188. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  189. }
  190. try {
  191. xtw.WriteStartElement ("foo", "bar", "baz");
  192. Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  193. }
  194. catch (InvalidOperationException e) {
  195. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  196. }
  197. try
  198. {
  199. xtw.WriteAttributeString ("foo", "bar");
  200. Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  201. }
  202. catch (InvalidOperationException e)
  203. {
  204. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  205. }
  206. try {
  207. xtw.WriteString ("foo");
  208. Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  209. }
  210. catch (InvalidOperationException e) {
  211. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  212. }
  213. }
  214. [Test]
  215. public void CommentValid ()
  216. {
  217. xtw.WriteComment ("foo");
  218. AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", StringWriterText);
  219. }
  220. [Test]
  221. public void CommentInvalid ()
  222. {
  223. try {
  224. xtw.WriteComment("foo-");
  225. Fail("Should have thrown an ArgumentException.");
  226. }
  227. catch (ArgumentException) { }
  228. try {
  229. xtw.WriteComment("foo-->bar");
  230. Fail("Should have thrown an ArgumentException.");
  231. }
  232. catch (ArgumentException) { }
  233. }
  234. [Test]
  235. public void ConstructorsAndBaseStream ()
  236. {
  237. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
  238. MemoryStream ms;
  239. StreamReader sr;
  240. XmlTextWriter xtw;
  241. ms = new MemoryStream ();
  242. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  243. xtw.WriteStartDocument ();
  244. xtw.Flush ();
  245. ms.Seek (0, SeekOrigin.Begin);
  246. sr = new StreamReader (ms, Encoding.Unicode);
  247. string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  248. string actualXmlDeclaration = sr.ReadToEnd();
  249. AssertEquals (expectedXmlDeclaration, actualXmlDeclaration);
  250. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  251. ms = new MemoryStream ();
  252. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  253. xtw.WriteStartDocument (true);
  254. xtw.Flush ();
  255. ms.Seek (0, SeekOrigin.Begin);
  256. sr = new StreamReader (ms, Encoding.Unicode);
  257. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  258. ms = new MemoryStream ();
  259. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  260. xtw.WriteStartDocument ();
  261. xtw.Flush ();
  262. ms.Seek (0, SeekOrigin.Begin);
  263. sr = new StreamReader (ms, Encoding.UTF8);
  264. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  265. ms = new MemoryStream ();
  266. xtw = new XmlTextWriter (ms, null);
  267. xtw.WriteStartDocument ();
  268. xtw.Flush ();
  269. ms.Seek (0, SeekOrigin.Begin);
  270. sr = new StreamReader (ms, Encoding.UTF8);
  271. AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  272. ms = new MemoryStream ();
  273. xtw = new XmlTextWriter (ms, null);
  274. xtw.WriteStartDocument (true);
  275. xtw.Flush ();
  276. ms.Seek (0, SeekOrigin.Begin);
  277. sr = new StreamReader (ms, Encoding.UTF8);
  278. AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  279. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  280. }
  281. [Test]
  282. public void DocumentStart ()
  283. {
  284. xtw.WriteStartDocument ();
  285. AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
  286. try
  287. {
  288. xtw.WriteStartDocument ();
  289. Fail("Should have thrown an InvalidOperationException.");
  290. }
  291. catch (InvalidOperationException e) {
  292. // Don't rely on English message assertion.
  293. // It is enough to check an exception occurs.
  294. // AssertEquals ("Exception message is incorrect.",
  295. // "WriteStartDocument should be the first call.", e.Message);
  296. }
  297. xtw = new XmlTextWriter (sw = new StringWriter ());
  298. xtw.QuoteChar = '\'';
  299. xtw.WriteStartDocument (true);
  300. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
  301. xtw = new XmlTextWriter (sw = new StringWriter ());
  302. xtw.QuoteChar = '\'';
  303. xtw.WriteStartDocument (false);
  304. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
  305. }
  306. [Test]
  307. public void ElementEmpty ()
  308. {
  309. xtw.WriteStartElement ("foo");
  310. xtw.WriteEndElement ();
  311. AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
  312. }
  313. [Test]
  314. public void ElementWriteElementString ()
  315. {
  316. xtw.WriteElementString ("foo", "bar");
  317. AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
  318. xtw.WriteElementString ("baz", "");
  319. AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
  320. xtw.WriteElementString ("quux", null);
  321. AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
  322. xtw.WriteElementString ("", "quuux");
  323. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  324. xtw.WriteElementString (null, "quuuux");
  325. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  326. }
  327. [Test]
  328. public void FormattingTest ()
  329. {
  330. xtw.Formatting = Formatting.Indented;
  331. xtw.WriteStartDocument ();
  332. xtw.WriteStartElement ("foo");
  333. xtw.WriteElementString ("bar", "");
  334. xtw.Close ();
  335. AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
  336. }
  337. [Test]
  338. public void FormattingInvalidXmlForFun ()
  339. {
  340. xtw.Formatting = Formatting.Indented;
  341. xtw.IndentChar = 'x';
  342. xtw.WriteStartDocument ();
  343. xtw.WriteStartElement ("foo");
  344. xtw.WriteStartElement ("bar");
  345. xtw.WriteElementString ("baz", "");
  346. xtw.Close ();
  347. AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
  348. }
  349. [Test]
  350. public void FormattingFromRemarks ()
  351. {
  352. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  353. xtw.Formatting = Formatting.Indented;
  354. xtw.WriteStartElement ("ol");
  355. xtw.WriteStartElement ("li");
  356. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  357. xtw.WriteElementString ("b", "E");
  358. xtw.WriteElementString ("i", "lephant");
  359. xtw.WriteString (" walks slowly.");
  360. xtw.WriteEndElement ();
  361. xtw.WriteEndElement ();
  362. AssertEquals (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
  363. }
  364. [Test]
  365. public void LookupPrefix ()
  366. {
  367. xtw.WriteStartElement ("root");
  368. xtw.WriteStartElement ("one");
  369. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  370. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  371. AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
  372. AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  373. xtw.WriteEndElement ();
  374. xtw.WriteStartElement ("two");
  375. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  376. xtw.WriteString("quux");
  377. AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  378. AssertNull (xtw.LookupPrefix ("http://abc.def"));
  379. AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
  380. AssertNull (xtw.LookupPrefix ("http://bogus"));
  381. }
  382. [Test]
  383. public void NamespacesAttributesPassingInNamespaces ()
  384. {
  385. xtw.Namespaces = false;
  386. xtw.WriteStartElement ("foo");
  387. // These shouldn't throw any exceptions since they don't pass in
  388. // a namespace.
  389. xtw.WriteAttributeString ("bar", "baz");
  390. xtw.WriteAttributeString ("", "a", "", "b");
  391. xtw.WriteAttributeString (null, "c", "", "d");
  392. xtw.WriteAttributeString ("", "e", null, "f");
  393. xtw.WriteAttributeString (null, "g", null, "h");
  394. AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  395. // These should throw ArgumentException because they pass in a
  396. // namespace when Namespaces = false.
  397. }
  398. [Test]
  399. public void NamespacesElementsPassingInNamespaces ()
  400. {
  401. xtw.Namespaces = false;
  402. // These shouldn't throw any exceptions since they don't pass in
  403. // a namespace.
  404. xtw.WriteElementString ("foo", "bar");
  405. xtw.WriteStartElement ("baz");
  406. xtw.WriteStartElement ("quux", "");
  407. xtw.WriteStartElement ("quuux", null);
  408. xtw.WriteStartElement (null, "a", null);
  409. xtw.WriteStartElement (null, "b", "");
  410. xtw.WriteStartElement ("", "c", null);
  411. xtw.WriteStartElement ("", "d", "");
  412. AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  413. // These should throw ArgumentException because they pass in a
  414. // namespace when Namespaces = false.
  415. try {
  416. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  417. Fail ("Expected an ArgumentException.");
  418. } catch (ArgumentException) {}
  419. try {
  420. xtw.WriteStartElement ("foo", "http://netsack.com/");
  421. Fail ("Expected an ArgumentException.");
  422. } catch (ArgumentException) {}
  423. try {
  424. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  425. Fail ("Expected an ArgumentException.");
  426. } catch (ArgumentException) {}
  427. try {
  428. xtw.WriteStartElement ("foo", "bar", null);
  429. Fail ("Expected an ArgumentException.");
  430. } catch (ArgumentException) {}
  431. try {
  432. xtw.WriteStartElement ("foo", "bar", "");
  433. Fail ("Expected an ArgumentException.");
  434. } catch (ArgumentException) {}
  435. try {
  436. xtw.WriteStartElement ("foo", "", "");
  437. Fail ("Expected an ArgumentException.");
  438. } catch (ArgumentException) {}
  439. }
  440. [Test]
  441. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  442. {
  443. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  444. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  445. xtw.WriteElementString("baz", String.Empty, String.Empty);
  446. xtw.WriteEndElement();
  447. xtw.WriteEndElement();
  448. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  449. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
  450. }
  451. [Test]
  452. public void NamespacesPrefix ()
  453. {
  454. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  455. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  456. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  457. xtw.WriteEndElement ();
  458. xtw.WriteEndElement ();
  459. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  460. "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
  461. }
  462. [Test]
  463. public void NamespacesPrefixWithEmptyAndNullNamespace ()
  464. {
  465. try {
  466. xtw.WriteStartElement ("foo", "bar", "");
  467. Fail ("Should have thrown an ArgumentException.");
  468. } catch (ArgumentException) {}
  469. try
  470. {
  471. xtw.WriteStartElement ("foo", "bar", null);
  472. Fail ("Should have thrown an ArgumentException.");
  473. }
  474. catch (ArgumentException) {}
  475. }
  476. [Test]
  477. public void NamespacesSettingWhenWriteStateNotStart ()
  478. {
  479. xtw.WriteStartElement ("foo");
  480. try
  481. {
  482. xtw.Namespaces = false;
  483. Fail ("Expected an InvalidOperationException.");
  484. }
  485. catch (InvalidOperationException) {}
  486. AssertEquals (true, xtw.Namespaces);
  487. }
  488. [Test]
  489. public void ProcessingInstructionValid ()
  490. {
  491. xtw.WriteProcessingInstruction("foo", "bar");
  492. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
  493. }
  494. [Test]
  495. public void ProcessingInstructionInvalid ()
  496. {
  497. try
  498. {
  499. xtw.WriteProcessingInstruction("fo?>o", "bar");
  500. Fail("Should have thrown an ArgumentException.");
  501. }
  502. catch (ArgumentException) { }
  503. try
  504. {
  505. xtw.WriteProcessingInstruction("foo", "ba?>r");
  506. Fail("Should have thrown an ArgumentException.");
  507. }
  508. catch (ArgumentException) { }
  509. try
  510. {
  511. xtw.WriteProcessingInstruction("", "bar");
  512. Fail("Should have thrown an ArgumentException.");
  513. }
  514. catch (ArgumentException) { }
  515. try
  516. {
  517. xtw.WriteProcessingInstruction(null, "bar");
  518. Fail("Should have thrown an ArgumentException.");
  519. }
  520. catch (ArgumentException) { }
  521. }
  522. [Test]
  523. public void QuoteCharDoubleQuote ()
  524. {
  525. xtw.QuoteChar = '"';
  526. // version, encoding, standalone
  527. xtw.WriteStartDocument (true);
  528. // namespace declaration
  529. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  530. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  531. }
  532. [Test]
  533. [ExpectedException (typeof (ArgumentException))]
  534. public void QuoteCharInvalid ()
  535. {
  536. xtw.QuoteChar = 'x';
  537. }
  538. [Test]
  539. public void WriteBase64 ()
  540. {
  541. UTF8Encoding encoding = new UTF8Encoding();
  542. byte[] fooBar = encoding.GetBytes("foobar");
  543. xtw.WriteBase64 (fooBar, 0, 6);
  544. AssertEquals("Zm9vYmFy", StringWriterText);
  545. try {
  546. xtw.WriteBase64 (fooBar, 3, 6);
  547. Fail ("Expected an Argument Exception to be thrown.");
  548. } catch (ArgumentException) {}
  549. try {
  550. xtw.WriteBase64 (fooBar, -1, 6);
  551. Fail ("Expected an Argument Exception to be thrown.");
  552. } catch (ArgumentOutOfRangeException) {}
  553. try {
  554. xtw.WriteBase64 (fooBar, 3, -1);
  555. Fail ("Expected an Argument Exception to be thrown.");
  556. } catch (ArgumentOutOfRangeException) {}
  557. try {
  558. xtw.WriteBase64 (null, 0, 6);
  559. Fail ("Expected an Argument Exception to be thrown.");
  560. } catch (ArgumentNullException) {}
  561. }
  562. [Test]
  563. public void WriteCharEntity ()
  564. {
  565. xtw.WriteCharEntity ('a');
  566. AssertEquals ("&#x61;", StringWriterText);
  567. xtw.WriteCharEntity ('A');
  568. AssertEquals ("&#x61;&#x41;", StringWriterText);
  569. xtw.WriteCharEntity ('1');
  570. AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
  571. xtw.WriteCharEntity ('K');
  572. AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  573. try {
  574. xtw.WriteCharEntity ((char)0xd800);
  575. } catch (ArgumentException) {}
  576. }
  577. [Test]
  578. [ExpectedException (typeof (InvalidOperationException))]
  579. public void WriteEndAttribute ()
  580. {
  581. xtw.WriteEndAttribute ();
  582. }
  583. [Test]
  584. public void WriteEndDocument ()
  585. {
  586. try {
  587. xtw.WriteEndDocument ();
  588. Fail ("Expected an ArgumentException.");
  589. } catch (ArgumentException) {}
  590. xtw.WriteStartDocument ();
  591. try
  592. {
  593. xtw.WriteEndDocument ();
  594. Fail ("Expected an ArgumentException.");
  595. }
  596. catch (ArgumentException) {}
  597. xtw.WriteStartElement ("foo");
  598. xtw.WriteStartAttribute ("bar", null);
  599. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  600. xtw.WriteEndDocument ();
  601. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  602. AssertEquals (WriteState.Start, xtw.WriteState);
  603. }
  604. [Test]
  605. public void WriteEndElement ()
  606. {
  607. try {
  608. xtw.WriteEndElement ();
  609. Fail ("Should have thrown an InvalidOperationException.");
  610. } catch (InvalidOperationException e) {
  611. // Don't rely on English message assertion.
  612. // It is enough to check an exception occurs.
  613. // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
  614. }
  615. xtw.WriteStartElement ("foo");
  616. xtw.WriteEndElement ();
  617. AssertEquals ("<foo />", StringWriterText);
  618. xtw.WriteStartElement ("bar");
  619. xtw.WriteStartAttribute ("baz", null);
  620. xtw.WriteEndElement ();
  621. AssertEquals ("<foo /><bar baz='' />", StringWriterText);
  622. }
  623. [Test]
  624. public void FullEndElement ()
  625. {
  626. xtw.WriteStartElement ("foo");
  627. xtw.WriteFullEndElement ();
  628. AssertEquals ("<foo></foo>", StringWriterText);
  629. xtw.WriteStartElement ("bar");
  630. xtw.WriteAttributeString ("foo", "bar");
  631. xtw.WriteFullEndElement ();
  632. AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  633. xtw.WriteStartElement ("baz");
  634. xtw.WriteStartAttribute ("bar", null);
  635. xtw.WriteFullEndElement ();
  636. AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  637. }
  638. [Test]
  639. public void WriteQualifiedName ()
  640. {
  641. xtw.WriteStartElement (null, "test", null);
  642. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  643. xtw.WriteQualifiedName ("bob", "http://localhost/");
  644. xtw.WriteEndElement ();
  645. AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  646. }
  647. [Test]
  648. public void WriteRaw ()
  649. {
  650. xtw.WriteRaw("&<>\"'");
  651. AssertEquals ("&<>\"'", StringWriterText);
  652. xtw.WriteRaw(null);
  653. AssertEquals ("&<>\"'", StringWriterText);
  654. xtw.WriteRaw("");
  655. AssertEquals ("&<>\"'", StringWriterText);
  656. }
  657. [Test]
  658. public void WriteRawInvalidInAttribute ()
  659. {
  660. xtw.WriteStartElement ("foo");
  661. xtw.WriteStartAttribute ("bar", null);
  662. xtw.WriteRaw ("&<>\"'");
  663. xtw.WriteEndAttribute ();
  664. xtw.WriteEndElement ();
  665. AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
  666. }
  667. [Test]
  668. public void WriteStateTest ()
  669. {
  670. AssertEquals (WriteState.Start, xtw.WriteState);
  671. xtw.WriteStartDocument ();
  672. AssertEquals (WriteState.Prolog, xtw.WriteState);
  673. xtw.WriteStartElement ("root");
  674. AssertEquals (WriteState.Element, xtw.WriteState);
  675. xtw.WriteElementString ("foo", "bar");
  676. AssertEquals (WriteState.Content, xtw.WriteState);
  677. xtw.Close ();
  678. AssertEquals (WriteState.Closed, xtw.WriteState);
  679. }
  680. [Test]
  681. public void WriteString ()
  682. {
  683. xtw.WriteStartDocument ();
  684. try {
  685. xtw.WriteString("foo");
  686. } catch (InvalidOperationException) {}
  687. // Testing attribute values
  688. xtw.WriteStartElement ("foo");
  689. xtw.WriteAttributeString ("bar", "&<>");
  690. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  691. }
  692. [Test]
  693. public void WriteAttributeStringSingleQuoteChar()
  694. {
  695. // When QuoteChar is single quote then replaces single quotes within attributes
  696. // but not double quotes.
  697. xtw.WriteStartElement ("foo");
  698. xtw.WriteAttributeString ("bar", "\"baz\"");
  699. xtw.WriteAttributeString ("quux", "'baz'");
  700. AssertEquals ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  701. }
  702. [Test]
  703. public void WriteAttributeStringDoubleQuoteChar()
  704. {
  705. // When QuoteChar is double quote then replaces double quotes within attributes
  706. // but not single quotes.
  707. xtw.QuoteChar = '"';
  708. xtw.WriteStartElement ("foo");
  709. xtw.WriteAttributeString ("bar", "\"baz\"");
  710. xtw.WriteAttributeString ("quux", "'baz'");
  711. AssertEquals ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  712. }
  713. [Test]
  714. public void WriteStringWithEntities()
  715. {
  716. // Testing element values
  717. xtw.QuoteChar = '\'';
  718. xtw.WriteElementString ("foo", "&<>\"'");
  719. AssertEquals ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  720. }
  721. [Test]
  722. public void XmlLang ()
  723. {
  724. AssertNull (xtw.XmlLang);
  725. xtw.WriteStartElement ("foo");
  726. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  727. AssertEquals ("langfoo", xtw.XmlLang);
  728. AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
  729. xtw.WriteAttributeString ("boo", "yah");
  730. AssertEquals ("langfoo", xtw.XmlLang);
  731. AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  732. xtw.WriteElementString("bar", "baz");
  733. AssertEquals ("langfoo", xtw.XmlLang);
  734. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  735. xtw.WriteString("baz");
  736. AssertEquals ("langfoo", xtw.XmlLang);
  737. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  738. xtw.WriteStartElement ("quux");
  739. xtw.WriteStartAttribute ("xml", "lang", null);
  740. AssertEquals ("langfoo", xtw.XmlLang);
  741. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  742. xtw.WriteString("langbar");
  743. AssertEquals ("langfoo", xtw.XmlLang);
  744. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  745. xtw.WriteEndAttribute ();
  746. AssertEquals ("langbar", xtw.XmlLang);
  747. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  748. // check if xml:lang repeats output even if same as current scope.
  749. xtw.WriteStartElement ("joe");
  750. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  751. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  752. xtw.WriteElementString ("quuux", "squonk");
  753. AssertEquals ("langbar", xtw.XmlLang);
  754. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  755. xtw.WriteEndElement ();
  756. xtw.WriteEndElement ();
  757. AssertEquals ("langfoo", xtw.XmlLang);
  758. 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);
  759. xtw.WriteEndElement ();
  760. AssertNull (xtw.XmlLang);
  761. 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);
  762. xtw.Close ();
  763. AssertNull (xtw.XmlLang);
  764. }
  765. // TODO: test operational aspects
  766. [Test]
  767. public void XmlSpaceTest ()
  768. {
  769. xtw.WriteStartElement ("foo");
  770. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  771. xtw.WriteStartElement ("bar");
  772. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  773. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  774. AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
  775. xtw.WriteStartElement ("baz");
  776. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  777. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  778. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  779. xtw.WriteStartElement ("quux");
  780. xtw.WriteStartAttribute ("xml", "space", null);
  781. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  782. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  783. xtw.WriteString ("default");
  784. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  785. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  786. xtw.WriteEndAttribute ();
  787. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  788. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  789. xtw.WriteEndElement ();
  790. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  791. xtw.WriteEndElement ();
  792. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  793. xtw.WriteEndElement ();
  794. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  795. xtw.WriteStartElement ("quux");
  796. try {
  797. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  798. } catch (ArgumentException) {}
  799. try {
  800. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  801. } catch (ArgumentException) {}
  802. try {
  803. xtw.WriteAttributeString ("xml", "space", null, "Preserve");
  804. } catch (ArgumentException) {}
  805. try {
  806. xtw.WriteAttributeString ("xml", "space", null, "Default");
  807. } catch (ArgumentException) {}
  808. try {
  809. xtw.WriteWhitespace ("x");
  810. } catch (ArgumentException) { }
  811. }
  812. [Test]
  813. public void XmlSpaceRaw ()
  814. {
  815. xtw.WriteStartElement ("foo");
  816. xtw.WriteStartAttribute ("xml", "space", null);
  817. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  818. AssertEquals ("<foo xml:space='", StringWriterText);
  819. xtw.WriteString ("default");
  820. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  821. AssertEquals ("<foo xml:space='", StringWriterText);
  822. xtw.WriteEndAttribute ();
  823. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  824. AssertEquals ("<foo xml:space='default'", StringWriterText);
  825. }
  826. [Test]
  827. public void WriteAttributes ()
  828. {
  829. XmlDocument doc = new XmlDocument();
  830. StringWriter sw = new StringWriter();
  831. XmlWriter wr = new XmlTextWriter(sw);
  832. StringBuilder sb = sw.GetStringBuilder();
  833. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  834. 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);
  835. xtr.Read(); // read XMLDecl
  836. wr.WriteAttributes(xtr, false);
  837. // This method don't always have to take this double-quoted style...
  838. AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
  839. sb.Remove(0, sb.Length); // init
  840. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  841. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  842. xtr.Read(); // read XMLDecl
  843. AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType);
  844. sw = new StringWriter ();
  845. wr = new XmlTextWriter (sw);
  846. // This block raises an error on MS.NET 1.0.
  847. wr.WriteAttributes(xtr, false);
  848. // This method don't always have to take this double-quoted style...
  849. AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
  850. sw = new StringWriter ();
  851. wr = new XmlTextWriter (sw);
  852. sb.Remove(0, sb.Length); // init
  853. xtr.Read(); // read root
  854. AssertEquals (XmlNodeType.Element, xtr.NodeType);
  855. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  856. wr.WriteAttributes(xtr, false);
  857. wr.WriteEndElement();
  858. wr.Close();
  859. // This method don't always have to take this double-quoted style...
  860. AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());
  861. }
  862. [Test]
  863. public void WriteWhitespace ()
  864. {
  865. xtw.WriteStartElement ("a");
  866. xtw.WriteWhitespace ("\n\t");
  867. xtw.WriteStartElement ("b");
  868. xtw.WriteWhitespace ("\n\t");
  869. xtw.WriteEndElement ();
  870. xtw.WriteWhitespace ("\n");
  871. xtw.WriteEndElement ();
  872. xtw.WriteWhitespace ("\n");
  873. xtw.Flush ();
  874. AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  875. }
  876. [Test]
  877. public void FlushDoesntCloseTag ()
  878. {
  879. xtw.WriteStartElement ("foo");
  880. xtw.WriteAttributeString ("bar", "baz");
  881. xtw.Flush ();
  882. AssertEquals ("<foo bar='baz'", StringWriterText);
  883. }
  884. [Test]
  885. public void WriteWhitespaceClosesTag ()
  886. {
  887. xtw.WriteStartElement ("foo");
  888. xtw.WriteAttributeString ("bar", "baz");
  889. xtw.WriteWhitespace (" ");
  890. AssertEquals ("<foo bar='baz'> ", StringWriterText);
  891. }
  892. }
  893. }