XmlTextWriterTests.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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 MS.NET doesn't treat as an error.
  85. // See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
  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 ElementAndAttributeSameXmlns ()
  308. {
  309. xtw.WriteStartElement ("ped", "foo", "urn:foo");
  310. xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
  311. xtw.WriteEndElement ();
  312. AssertEquals ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
  313. }
  314. [Test]
  315. public void ElementXmlnsNeedEscape ()
  316. {
  317. xtw.WriteStartElement ("test", "foo", "'");
  318. xtw.WriteEndElement ();
  319. // MS.NET fails this case.
  320. AssertEquals ("<test:foo xmlns:test='&apos;' />", StringWriterText);
  321. }
  322. [Test]
  323. public void ElementEmpty ()
  324. {
  325. xtw.WriteStartElement ("foo");
  326. xtw.WriteEndElement ();
  327. AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
  328. }
  329. [Test]
  330. public void ElementWriteElementString ()
  331. {
  332. xtw.WriteElementString ("foo", "bar");
  333. AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
  334. xtw.WriteElementString ("baz", "");
  335. AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
  336. xtw.WriteElementString ("quux", null);
  337. AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
  338. xtw.WriteElementString ("", "quuux");
  339. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  340. xtw.WriteElementString (null, "quuuux");
  341. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  342. }
  343. [Test]
  344. public void FormattingTest ()
  345. {
  346. xtw.Formatting = Formatting.Indented;
  347. xtw.WriteStartDocument ();
  348. xtw.WriteStartElement ("foo");
  349. xtw.WriteElementString ("bar", "");
  350. xtw.Close ();
  351. AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
  352. }
  353. [Test]
  354. public void FormattingInvalidXmlForFun ()
  355. {
  356. xtw.Formatting = Formatting.Indented;
  357. xtw.IndentChar = 'x';
  358. xtw.WriteStartDocument ();
  359. xtw.WriteStartElement ("foo");
  360. xtw.WriteStartElement ("bar");
  361. xtw.WriteElementString ("baz", "");
  362. xtw.Close ();
  363. 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);
  364. }
  365. [Test]
  366. public void FormattingFromRemarks ()
  367. {
  368. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  369. xtw.Formatting = Formatting.Indented;
  370. xtw.WriteStartElement ("ol");
  371. xtw.WriteStartElement ("li");
  372. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  373. xtw.WriteElementString ("b", "E");
  374. xtw.WriteElementString ("i", "lephant");
  375. xtw.WriteString (" walks slowly.");
  376. xtw.WriteEndElement ();
  377. xtw.WriteEndElement ();
  378. AssertEquals (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
  379. }
  380. [Test]
  381. public void LookupPrefix ()
  382. {
  383. xtw.WriteStartElement ("root");
  384. xtw.WriteStartElement ("one");
  385. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  386. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  387. AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
  388. AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  389. xtw.WriteEndElement ();
  390. xtw.WriteStartElement ("two");
  391. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  392. xtw.WriteString("quux");
  393. AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  394. AssertNull (xtw.LookupPrefix ("http://abc.def"));
  395. AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
  396. AssertNull (xtw.LookupPrefix ("http://bogus"));
  397. }
  398. [Test]
  399. public void NamespacesAttributesPassingInNamespaces ()
  400. {
  401. xtw.Namespaces = false;
  402. xtw.WriteStartElement ("foo");
  403. // These shouldn't throw any exceptions since they don't pass in
  404. // a namespace.
  405. xtw.WriteAttributeString ("bar", "baz");
  406. xtw.WriteAttributeString ("", "a", "", "b");
  407. xtw.WriteAttributeString (null, "c", "", "d");
  408. xtw.WriteAttributeString ("", "e", null, "f");
  409. xtw.WriteAttributeString (null, "g", null, "h");
  410. AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  411. // These should throw ArgumentException because they pass in a
  412. // namespace when Namespaces = false.
  413. }
  414. [Test]
  415. public void NamespacesElementsPassingInNamespaces ()
  416. {
  417. xtw.Namespaces = false;
  418. // These shouldn't throw any exceptions since they don't pass in
  419. // a namespace.
  420. xtw.WriteElementString ("foo", "bar");
  421. xtw.WriteStartElement ("baz");
  422. xtw.WriteStartElement ("quux", "");
  423. xtw.WriteStartElement ("quuux", null);
  424. xtw.WriteStartElement (null, "a", null);
  425. xtw.WriteStartElement (null, "b", "");
  426. xtw.WriteStartElement ("", "c", null);
  427. xtw.WriteStartElement ("", "d", "");
  428. AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  429. // These should throw ArgumentException because they pass in a
  430. // namespace when Namespaces = false.
  431. try {
  432. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  433. Fail ("Expected an ArgumentException.");
  434. } catch (ArgumentException) {}
  435. try {
  436. xtw.WriteStartElement ("foo", "http://netsack.com/");
  437. Fail ("Expected an ArgumentException.");
  438. } catch (ArgumentException) {}
  439. try {
  440. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  441. Fail ("Expected an ArgumentException.");
  442. } catch (ArgumentException) {}
  443. try {
  444. xtw.WriteStartElement ("foo", "bar", null);
  445. Fail ("Expected an ArgumentException.");
  446. } catch (ArgumentException) {}
  447. try {
  448. xtw.WriteStartElement ("foo", "bar", "");
  449. Fail ("Expected an ArgumentException.");
  450. } catch (ArgumentException) {}
  451. try {
  452. xtw.WriteStartElement ("foo", "", "");
  453. Fail ("Expected an ArgumentException.");
  454. } catch (ArgumentException) {}
  455. }
  456. [Test]
  457. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  458. {
  459. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  460. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  461. xtw.WriteElementString("baz", String.Empty, String.Empty);
  462. xtw.WriteEndElement();
  463. xtw.WriteEndElement();
  464. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  465. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
  466. }
  467. [Test]
  468. public void NamespacesPrefix ()
  469. {
  470. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  471. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  472. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  473. xtw.WriteEndElement ();
  474. xtw.WriteEndElement ();
  475. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  476. "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
  477. }
  478. [Test]
  479. [ExpectedException (typeof (ArgumentException))]
  480. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  481. {
  482. xtw.WriteStartElement ("foo", "bar", "");
  483. }
  484. [Test]
  485. [ExpectedException (typeof (ArgumentException))]
  486. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  487. {
  488. xtw.WriteStartElement ("foo", "bar", null);
  489. }
  490. [Test]
  491. public void NamespacesSettingWhenWriteStateNotStart ()
  492. {
  493. xtw.WriteStartElement ("foo");
  494. try
  495. {
  496. xtw.Namespaces = false;
  497. Fail ("Expected an InvalidOperationException.");
  498. }
  499. catch (InvalidOperationException) {}
  500. AssertEquals (true, xtw.Namespaces);
  501. }
  502. [Test]
  503. public void ProcessingInstructionValid ()
  504. {
  505. xtw.WriteProcessingInstruction("foo", "bar");
  506. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
  507. }
  508. [Test]
  509. public void ProcessingInstructionInvalid ()
  510. {
  511. try
  512. {
  513. xtw.WriteProcessingInstruction("fo?>o", "bar");
  514. Fail("Should have thrown an ArgumentException.");
  515. }
  516. catch (ArgumentException) { }
  517. try
  518. {
  519. xtw.WriteProcessingInstruction("foo", "ba?>r");
  520. Fail("Should have thrown an ArgumentException.");
  521. }
  522. catch (ArgumentException) { }
  523. try
  524. {
  525. xtw.WriteProcessingInstruction("", "bar");
  526. Fail("Should have thrown an ArgumentException.");
  527. }
  528. catch (ArgumentException) { }
  529. try
  530. {
  531. xtw.WriteProcessingInstruction(null, "bar");
  532. Fail("Should have thrown an ArgumentException.");
  533. }
  534. catch (ArgumentException) { }
  535. }
  536. [Test]
  537. public void QuoteCharDoubleQuote ()
  538. {
  539. xtw.QuoteChar = '"';
  540. // version, encoding, standalone
  541. xtw.WriteStartDocument (true);
  542. // namespace declaration
  543. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  544. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  545. }
  546. [Test]
  547. [ExpectedException (typeof (ArgumentException))]
  548. public void QuoteCharInvalid ()
  549. {
  550. xtw.QuoteChar = 'x';
  551. }
  552. [Test]
  553. public void WriteBase64 ()
  554. {
  555. UTF8Encoding encoding = new UTF8Encoding();
  556. byte[] fooBar = encoding.GetBytes("foobar");
  557. xtw.WriteBase64 (fooBar, 0, 6);
  558. AssertEquals("Zm9vYmFy", StringWriterText);
  559. try {
  560. xtw.WriteBase64 (fooBar, 3, 6);
  561. Fail ("Expected an Argument Exception to be thrown.");
  562. } catch (ArgumentException) {}
  563. try {
  564. xtw.WriteBase64 (fooBar, -1, 6);
  565. Fail ("Expected an Argument Exception to be thrown.");
  566. } catch (ArgumentOutOfRangeException) {}
  567. try {
  568. xtw.WriteBase64 (fooBar, 3, -1);
  569. Fail ("Expected an Argument Exception to be thrown.");
  570. } catch (ArgumentOutOfRangeException) {}
  571. try {
  572. xtw.WriteBase64 (null, 0, 6);
  573. Fail ("Expected an Argument Exception to be thrown.");
  574. } catch (ArgumentNullException) {}
  575. }
  576. [Test]
  577. public void WriteBinHex ()
  578. {
  579. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  580. xtw.WriteBinHex (bytes, 0, 11);
  581. AssertEquals ("040E22365E7286C2FF0005", StringWriterText);
  582. }
  583. [Test]
  584. public void WriteCharEntity ()
  585. {
  586. xtw.WriteCharEntity ('a');
  587. AssertEquals ("&#x61;", StringWriterText);
  588. xtw.WriteCharEntity ('A');
  589. AssertEquals ("&#x61;&#x41;", StringWriterText);
  590. xtw.WriteCharEntity ('1');
  591. AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
  592. xtw.WriteCharEntity ('K');
  593. AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  594. try {
  595. xtw.WriteCharEntity ((char)0xd800);
  596. } catch (ArgumentException) {}
  597. }
  598. [Test]
  599. [ExpectedException (typeof (InvalidOperationException))]
  600. public void WriteEndAttribute ()
  601. {
  602. xtw.WriteEndAttribute ();
  603. }
  604. [Test]
  605. public void WriteEndDocument ()
  606. {
  607. try {
  608. xtw.WriteEndDocument ();
  609. Fail ("Expected an ArgumentException.");
  610. } catch (ArgumentException) {}
  611. xtw.WriteStartDocument ();
  612. try
  613. {
  614. xtw.WriteEndDocument ();
  615. Fail ("Expected an ArgumentException.");
  616. }
  617. catch (ArgumentException) {}
  618. xtw.WriteStartElement ("foo");
  619. xtw.WriteStartAttribute ("bar", null);
  620. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  621. xtw.WriteEndDocument ();
  622. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  623. AssertEquals (WriteState.Start, xtw.WriteState);
  624. }
  625. [Test]
  626. public void WriteEndElement ()
  627. {
  628. try {
  629. xtw.WriteEndElement ();
  630. Fail ("Should have thrown an InvalidOperationException.");
  631. } catch (InvalidOperationException) {
  632. // Don't rely on English message assertion.
  633. // It is enough to check an exception occurs.
  634. // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
  635. }
  636. xtw.WriteStartElement ("foo");
  637. xtw.WriteEndElement ();
  638. AssertEquals ("<foo />", StringWriterText);
  639. xtw.WriteStartElement ("bar");
  640. xtw.WriteStartAttribute ("baz", null);
  641. xtw.WriteEndElement ();
  642. AssertEquals ("<foo /><bar baz='' />", StringWriterText);
  643. }
  644. [Test]
  645. public void FullEndElement ()
  646. {
  647. xtw.WriteStartElement ("foo");
  648. xtw.WriteFullEndElement ();
  649. AssertEquals ("<foo></foo>", StringWriterText);
  650. xtw.WriteStartElement ("bar");
  651. xtw.WriteAttributeString ("foo", "bar");
  652. xtw.WriteFullEndElement ();
  653. AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  654. xtw.WriteStartElement ("baz");
  655. xtw.WriteStartAttribute ("bar", null);
  656. xtw.WriteFullEndElement ();
  657. AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  658. }
  659. [Test]
  660. public void WriteQualifiedName ()
  661. {
  662. xtw.WriteStartElement (null, "test", null);
  663. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  664. xtw.WriteQualifiedName ("bob", "http://localhost/");
  665. xtw.WriteEndElement ();
  666. AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  667. }
  668. [Test]
  669. public void WriteRaw ()
  670. {
  671. xtw.WriteRaw("&<>\"'");
  672. AssertEquals ("&<>\"'", StringWriterText);
  673. xtw.WriteRaw(null);
  674. AssertEquals ("&<>\"'", StringWriterText);
  675. xtw.WriteRaw("");
  676. AssertEquals ("&<>\"'", StringWriterText);
  677. }
  678. [Test]
  679. public void WriteRawInvalidInAttribute ()
  680. {
  681. xtw.WriteStartElement ("foo");
  682. xtw.WriteStartAttribute ("bar", null);
  683. xtw.WriteRaw ("&<>\"'");
  684. xtw.WriteEndAttribute ();
  685. xtw.WriteEndElement ();
  686. AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
  687. }
  688. [Test]
  689. public void WriteStateTest ()
  690. {
  691. AssertEquals (WriteState.Start, xtw.WriteState);
  692. xtw.WriteStartDocument ();
  693. AssertEquals (WriteState.Prolog, xtw.WriteState);
  694. xtw.WriteStartElement ("root");
  695. AssertEquals (WriteState.Element, xtw.WriteState);
  696. xtw.WriteElementString ("foo", "bar");
  697. AssertEquals (WriteState.Content, xtw.WriteState);
  698. xtw.Close ();
  699. AssertEquals (WriteState.Closed, xtw.WriteState);
  700. }
  701. [Test]
  702. public void WriteString ()
  703. {
  704. xtw.WriteStartDocument ();
  705. try {
  706. xtw.WriteString("foo");
  707. } catch (InvalidOperationException) {}
  708. // Testing attribute values
  709. xtw.WriteStartElement ("foo");
  710. xtw.WriteAttributeString ("bar", "&<>");
  711. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  712. }
  713. [Test]
  714. public void WriteAttributeStringSingleQuoteChar()
  715. {
  716. // When QuoteChar is single quote then replaces single quotes within attributes
  717. // but not double quotes.
  718. xtw.WriteStartElement ("foo");
  719. xtw.WriteAttributeString ("bar", "\"baz\"");
  720. xtw.WriteAttributeString ("quux", "'baz'");
  721. AssertEquals ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  722. }
  723. [Test]
  724. public void WriteAttributeStringDoubleQuoteChar()
  725. {
  726. // When QuoteChar is double quote then replaces double quotes within attributes
  727. // but not single quotes.
  728. xtw.QuoteChar = '"';
  729. xtw.WriteStartElement ("foo");
  730. xtw.WriteAttributeString ("bar", "\"baz\"");
  731. xtw.WriteAttributeString ("quux", "'baz'");
  732. AssertEquals ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  733. }
  734. [Test]
  735. public void WriteStringWithEntities()
  736. {
  737. // Testing element values
  738. xtw.QuoteChar = '\'';
  739. xtw.WriteElementString ("foo", "&<>\"'");
  740. AssertEquals ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  741. }
  742. [Test]
  743. public void XmlLang ()
  744. {
  745. AssertNull (xtw.XmlLang);
  746. xtw.WriteStartElement ("foo");
  747. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  748. AssertEquals ("langfoo", xtw.XmlLang);
  749. AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
  750. xtw.WriteAttributeString ("boo", "yah");
  751. AssertEquals ("langfoo", xtw.XmlLang);
  752. AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  753. xtw.WriteElementString("bar", "baz");
  754. AssertEquals ("langfoo", xtw.XmlLang);
  755. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  756. xtw.WriteString("baz");
  757. AssertEquals ("langfoo", xtw.XmlLang);
  758. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  759. xtw.WriteStartElement ("quux");
  760. xtw.WriteStartAttribute ("xml", "lang", null);
  761. AssertEquals ("langfoo", xtw.XmlLang);
  762. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  763. xtw.WriteString("langbar");
  764. AssertEquals ("langfoo", xtw.XmlLang);
  765. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  766. xtw.WriteEndAttribute ();
  767. AssertEquals ("langbar", xtw.XmlLang);
  768. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  769. // check if xml:lang repeats output even if same as current scope.
  770. xtw.WriteStartElement ("joe");
  771. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  772. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  773. xtw.WriteElementString ("quuux", "squonk");
  774. AssertEquals ("langbar", xtw.XmlLang);
  775. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  776. xtw.WriteEndElement ();
  777. xtw.WriteEndElement ();
  778. AssertEquals ("langfoo", xtw.XmlLang);
  779. 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);
  780. xtw.WriteEndElement ();
  781. AssertNull (xtw.XmlLang);
  782. 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);
  783. xtw.Close ();
  784. AssertNull (xtw.XmlLang);
  785. }
  786. // TODO: test operational aspects
  787. [Test]
  788. public void XmlSpaceTest ()
  789. {
  790. xtw.WriteStartElement ("foo");
  791. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  792. xtw.WriteStartElement ("bar");
  793. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  794. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  795. AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
  796. xtw.WriteStartElement ("baz");
  797. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  798. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  799. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  800. xtw.WriteStartElement ("quux");
  801. xtw.WriteStartAttribute ("xml", "space", null);
  802. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  803. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  804. xtw.WriteString ("default");
  805. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  806. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  807. xtw.WriteEndAttribute ();
  808. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  809. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  810. xtw.WriteEndElement ();
  811. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  812. xtw.WriteEndElement ();
  813. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  814. xtw.WriteEndElement ();
  815. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  816. xtw.WriteStartElement ("quux");
  817. try {
  818. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  819. } catch (ArgumentException) {}
  820. try {
  821. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  822. } catch (ArgumentException) {}
  823. try {
  824. xtw.WriteAttributeString ("xml", "space", null, "Preserve");
  825. } catch (ArgumentException) {}
  826. try {
  827. xtw.WriteAttributeString ("xml", "space", null, "Default");
  828. } catch (ArgumentException) {}
  829. try {
  830. xtw.WriteWhitespace ("x");
  831. } catch (ArgumentException) { }
  832. }
  833. [Test]
  834. public void XmlSpaceRaw ()
  835. {
  836. xtw.WriteStartElement ("foo");
  837. xtw.WriteStartAttribute ("xml", "space", null);
  838. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  839. AssertEquals ("<foo xml:space='", StringWriterText);
  840. xtw.WriteString ("default");
  841. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  842. AssertEquals ("<foo xml:space='", StringWriterText);
  843. xtw.WriteEndAttribute ();
  844. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  845. AssertEquals ("<foo xml:space='default'", StringWriterText);
  846. }
  847. [Test]
  848. public void WriteAttributes ()
  849. {
  850. XmlDocument doc = new XmlDocument();
  851. StringWriter sw = new StringWriter();
  852. XmlWriter wr = new XmlTextWriter(sw);
  853. StringBuilder sb = sw.GetStringBuilder();
  854. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  855. 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);
  856. xtr.Read(); // read XMLDecl
  857. wr.WriteAttributes(xtr, false);
  858. // This method don't always have to take this double-quoted style...
  859. AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
  860. sb.Remove(0, sb.Length); // init
  861. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  862. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  863. xtr.Read(); // read XMLDecl
  864. AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType);
  865. sw = new StringWriter ();
  866. wr = new XmlTextWriter (sw);
  867. // This block raises an error on MS.NET 1.0.
  868. wr.WriteAttributes(xtr, false);
  869. // This method don't always have to take this double-quoted style...
  870. AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
  871. sw = new StringWriter ();
  872. wr = new XmlTextWriter (sw);
  873. sb.Remove(0, sb.Length); // init
  874. xtr.Read(); // read root
  875. AssertEquals (XmlNodeType.Element, xtr.NodeType);
  876. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  877. wr.WriteAttributes(xtr, false);
  878. wr.WriteEndElement();
  879. wr.Close();
  880. // This method don't always have to take this double-quoted style...
  881. AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());
  882. xtr.Close ();
  883. }
  884. [Test]
  885. public void WriteWhitespace ()
  886. {
  887. xtw.WriteStartElement ("a");
  888. xtw.WriteWhitespace ("\n\t");
  889. xtw.WriteStartElement ("b");
  890. xtw.WriteWhitespace ("\n\t");
  891. xtw.WriteEndElement ();
  892. xtw.WriteWhitespace ("\n");
  893. xtw.WriteEndElement ();
  894. xtw.WriteWhitespace ("\n");
  895. xtw.Flush ();
  896. AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  897. }
  898. [Test]
  899. public void FlushDoesntCloseTag ()
  900. {
  901. xtw.WriteStartElement ("foo");
  902. xtw.WriteAttributeString ("bar", "baz");
  903. xtw.Flush ();
  904. AssertEquals ("<foo bar='baz'", StringWriterText);
  905. }
  906. [Test]
  907. public void WriteWhitespaceClosesTag ()
  908. {
  909. xtw.WriteStartElement ("foo");
  910. xtw.WriteAttributeString ("bar", "baz");
  911. xtw.WriteWhitespace (" ");
  912. AssertEquals ("<foo bar='baz'> ", StringWriterText);
  913. }
  914. [Test]
  915. public void DontOutputMultipleXmlns ()
  916. {
  917. XmlDocument doc = new XmlDocument();
  918. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  919. XmlDocument doc2 = new XmlDocument();
  920. doc2.LoadXml(doc.InnerXml);
  921. AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  922. doc2.OuterXml);
  923. }
  924. [Test]
  925. public void DontOutputNonDeclaredXmlns ()
  926. {
  927. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  928. XmlDocument doc = new XmlDocument();
  929. doc.LoadXml(xml);
  930. XmlDocument doc2 = new XmlDocument();
  931. doc2.LoadXml(doc.InnerXml);
  932. AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml);
  933. }
  934. [Test]
  935. public void DontOutputRemovalDefaultNSDeclaration ()
  936. {
  937. xtw.WriteStartDocument ();
  938. xtw.WriteStartElement ("foo");
  939. xtw.WriteAttributeString ("xmlns", "probe");
  940. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  941. xtw.WriteStartElement ("b");
  942. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  943. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  944. xtw.WriteEndElement (); // b2
  945. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  946. xtw.WriteEndElement (); // b2
  947. xtw.WriteEndElement (); // b
  948. xtw.WriteEndElement (); // foo
  949. xtw.WriteEndDocument ();
  950. xtw.Close ();
  951. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText);
  952. }
  953. [Test]
  954. public void DontOutputRemovalDefaultNSDeclaration2 ()
  955. {
  956. xtw.WriteStartDocument ();
  957. // IMPORTANT DIFFERENCE!! ns = "", not null
  958. xtw.WriteStartElement ("foo", "");
  959. xtw.WriteAttributeString ("xmlns", "probe");
  960. AssertNull (xtw.LookupPrefix ("probe"));
  961. xtw.WriteStartElement ("b");
  962. AssertNull (xtw.LookupPrefix ("probe"));
  963. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  964. xtw.WriteEndElement (); // b2
  965. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  966. xtw.WriteEndElement (); // b2
  967. xtw.WriteEndElement (); // b
  968. xtw.WriteEndElement (); // foo
  969. xtw.WriteEndDocument ();
  970. xtw.Close ();
  971. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText);
  972. }
  973. [Test]
  974. public void DoOutputRemovalDefaultNSDeclaration ()
  975. {
  976. xtw.WriteStartElement ("docelem", "a-namespace");
  977. XmlDocument doc = new XmlDocument ();
  978. doc.CreateElement ("hola").WriteTo (xtw);
  979. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  980. xtw.WriteEndElement ();
  981. xtw.Close ();
  982. AssertEquals ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  983. }
  984. [Test]
  985. public void WriteAttributeTakePrecedenceOnXmlns ()
  986. {
  987. xtw.WriteStartElement ("root", "urn:foo");
  988. xtw.WriteAttributeString ("xmlns", "urn:bar");
  989. xtw.WriteEndElement ();
  990. xtw.Close ();
  991. AssertEquals ("<root xmlns='urn:bar' />", StringWriterText);
  992. }
  993. [Test]
  994. [ExpectedException (typeof (ArgumentException))]
  995. public void LookupPrefixNull ()
  996. {
  997. xtw.LookupPrefix (null);
  998. }
  999. [Test]
  1000. [ExpectedException (typeof (ArgumentException))]
  1001. public void LookupPrefixEmpty ()
  1002. {
  1003. xtw.LookupPrefix (String.Empty);
  1004. }
  1005. [Test]
  1006. public void LookupPrefixIgnoresXmlnsAttribute ()
  1007. {
  1008. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1009. xtw.WriteStartElement ("root");
  1010. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1011. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1012. // Surprisingly to say, it is ignored!!
  1013. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1014. xtw.WriteStartElement ("hoge");
  1015. // (still after flushing previous start element.)
  1016. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1017. xtw.WriteStartElement ("fuga", "urn:foo");
  1018. // Is this testing on the correct way? Yes, here it is.
  1019. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1020. }
  1021. [Test]
  1022. public void WriteInvalidNames ()
  1023. {
  1024. xtw.WriteStartElement ("foo<>");
  1025. xtw.WriteAttributeString ("ho<>ge", "value");
  1026. }
  1027. [Test]
  1028. [ExpectedException (typeof (ArgumentException))]
  1029. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1030. {
  1031. xtw.WriteStartAttribute ("some", "foo", null);
  1032. }
  1033. [Test]
  1034. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1035. {
  1036. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1037. }
  1038. [Test]
  1039. [ExpectedException (typeof (ArgumentException))]
  1040. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1041. {
  1042. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1043. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1044. xtw.WriteEndAttribute ();
  1045. }
  1046. [Test]
  1047. [ExpectedException (typeof (ArgumentException))]
  1048. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1049. {
  1050. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1051. }
  1052. [Test]
  1053. [ExpectedException (typeof (ArgumentException))]
  1054. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1055. {
  1056. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1057. }
  1058. [Test]
  1059. public void WriteRawProceedToProlog ()
  1060. {
  1061. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1062. xtw.WriteRaw ("");
  1063. AssertEquals (WriteState.Prolog, xtw.WriteState);
  1064. }
  1065. [Test]
  1066. public void Indent ()
  1067. {
  1068. XmlDocument doc = new XmlDocument ();
  1069. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1070. StringWriter sw = new StringWriter ();
  1071. XmlTextWriter xtw = new XmlTextWriter (sw);
  1072. xtw.Formatting = Formatting.Indented;
  1073. doc.WriteContentTo (xtw);
  1074. AssertEquals (@"<root>\n <test>test<foo></foo>string</test>\n <test>string</test>\n</root>", sw.ToString ().Replace ("\r\n", "\n"));
  1075. }
  1076. }
  1077. }