XmlTextWriterTests.cs 32 KB

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