XmlTextWriterTests.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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) {}
  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) {
  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) {
  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) {
  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) {
  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)
  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) {
  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) {
  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) {
  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. [Test]
  893. public void DontOutputMultipleXmlns ()
  894. {
  895. XmlDocument doc = new XmlDocument();
  896. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  897. XmlDocument doc2 = new XmlDocument();
  898. doc2.LoadXml(doc.InnerXml);
  899. AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  900. doc2.OuterXml);
  901. }
  902. [Test]
  903. public void DontOutputNonDeclaredXmlns ()
  904. {
  905. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  906. XmlDocument doc = new XmlDocument();
  907. doc.LoadXml(xml);
  908. XmlDocument doc2 = new XmlDocument();
  909. doc2.LoadXml(doc.InnerXml);
  910. AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml);
  911. }
  912. }
  913. }