XmlTextWriterTests.cs 32 KB

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