XmlTextWriterTests.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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. [Category ("NotDotNet")]
  80. public void AttributeNamespacesXmlnsXmlns ()
  81. {
  82. xtw.WriteStartElement ("foo");
  83. try {
  84. xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  85. // This should not be allowed, even though MS.NET doesn't treat as an error.
  86. // See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
  87. Fail ("any prefix which name starts from \"xml\" must not be allowed.");
  88. }
  89. catch (ArgumentException) {}
  90. xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
  91. }
  92. [Test]
  93. public void AttributeWriteAttributeString ()
  94. {
  95. xtw.WriteStartElement ("foo");
  96. xtw.WriteAttributeString ("foo", "bar");
  97. AssertEquals ("<foo foo='bar'", StringWriterText);
  98. xtw.WriteAttributeString ("bar", "");
  99. AssertEquals ("<foo foo='bar' bar=''", StringWriterText);
  100. xtw.WriteAttributeString ("baz", null);
  101. AssertEquals ("<foo foo='bar' bar='' baz=''", StringWriterText);
  102. xtw.WriteAttributeString ("hoge", "a\nb");
  103. AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b'", StringWriterText);
  104. xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
  105. AssertEquals ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b' fuga=' a\t&#xD;&#xA;b\t'", StringWriterText);
  106. try {
  107. // Why does this pass Microsoft?
  108. // Anyway, Mono should not allow such code.
  109. xtw.WriteAttributeString ("", "quux");
  110. // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux'", StringWriterText);
  111. Fail ("empty name not allowed.");
  112. } catch (Exception) {
  113. }
  114. try {
  115. // Why does this pass Microsoft?
  116. // Anyway, Mono should not allow such code.
  117. xtw.WriteAttributeString (null, "quuux");
  118. // AssertEquals ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", StringWriterText);
  119. Fail ("null name not allowed.");
  120. } catch (Exception) {
  121. }
  122. }
  123. [Test]
  124. [ExpectedException (typeof (InvalidOperationException))]
  125. public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
  126. {
  127. xtw.WriteStartElement ("foo");
  128. xtw.WriteString ("bar");
  129. xtw.WriteAttributeString ("baz", "quux");
  130. }
  131. [Test]
  132. public void AttributeWriteAttributeStringWithoutParentElement ()
  133. {
  134. xtw.WriteAttributeString ("foo", "bar");
  135. AssertEquals ("foo='bar'", StringWriterText);
  136. xtw.WriteAttributeString ("baz", "quux");
  137. AssertEquals ("foo='bar' baz='quux'", StringWriterText);
  138. }
  139. [Test]
  140. public void CDataValid ()
  141. {
  142. xtw.WriteCData ("foo");
  143. AssertEquals ("WriteCData had incorrect output.", "<![CDATA[foo]]>", StringWriterText);
  144. }
  145. [Test]
  146. [ExpectedException (typeof (ArgumentException))]
  147. public void CDataInvalid ()
  148. {
  149. xtw.WriteCData("foo]]>bar");
  150. }
  151. [Test]
  152. public void CloseOpenElements ()
  153. {
  154. xtw.WriteStartElement("foo");
  155. xtw.WriteStartElement("bar");
  156. xtw.WriteStartElement("baz");
  157. xtw.Close();
  158. 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. Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  171. }
  172. catch (InvalidOperationException) {
  173. // Don't rely on English message assertion.
  174. // It is enough to check an exception occurs.
  175. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  176. }
  177. try {
  178. xtw.WriteComment ("foo");
  179. Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  180. }
  181. catch (InvalidOperationException) {
  182. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  183. }
  184. try {
  185. xtw.WriteProcessingInstruction ("foo", "bar");
  186. Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  187. }
  188. catch (InvalidOperationException) {
  189. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  190. }
  191. try {
  192. xtw.WriteStartElement ("foo", "bar", "baz");
  193. Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  194. }
  195. catch (InvalidOperationException) {
  196. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  197. }
  198. try
  199. {
  200. xtw.WriteAttributeString ("foo", "bar");
  201. Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  202. }
  203. catch (InvalidOperationException)
  204. {
  205. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  206. }
  207. try {
  208. xtw.WriteString ("foo");
  209. Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  210. }
  211. catch (InvalidOperationException) {
  212. // AssertEquals ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  213. }
  214. }
  215. [Test]
  216. public void CommentValid ()
  217. {
  218. xtw.WriteComment ("foo");
  219. AssertEquals ("WriteComment had incorrect output.", "<!--foo-->", StringWriterText);
  220. }
  221. [Test]
  222. public void CommentInvalid ()
  223. {
  224. try {
  225. xtw.WriteComment("foo-");
  226. Fail("Should have thrown an ArgumentException.");
  227. }
  228. catch (ArgumentException) { }
  229. try {
  230. xtw.WriteComment("foo-->bar");
  231. Fail("Should have thrown an ArgumentException.");
  232. }
  233. catch (ArgumentException) { }
  234. }
  235. [Test]
  236. public void ConstructorsAndBaseStream ()
  237. {
  238. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (null, this.xtw.BaseStream));
  239. MemoryStream ms;
  240. StreamReader sr;
  241. XmlTextWriter xtw;
  242. ms = new MemoryStream ();
  243. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  244. xtw.WriteStartDocument ();
  245. xtw.Flush ();
  246. ms.Seek (0, SeekOrigin.Begin);
  247. sr = new StreamReader (ms, Encoding.Unicode);
  248. string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  249. string actualXmlDeclaration = sr.ReadToEnd();
  250. AssertEquals (expectedXmlDeclaration, actualXmlDeclaration);
  251. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  252. ms = new MemoryStream ();
  253. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  254. xtw.WriteStartDocument (true);
  255. xtw.Flush ();
  256. ms.Seek (0, SeekOrigin.Begin);
  257. sr = new StreamReader (ms, Encoding.Unicode);
  258. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  259. ms = new MemoryStream ();
  260. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  261. xtw.WriteStartDocument ();
  262. xtw.Flush ();
  263. ms.Seek (0, SeekOrigin.Begin);
  264. sr = new StreamReader (ms, Encoding.UTF8);
  265. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  266. ms = new MemoryStream ();
  267. xtw = new XmlTextWriter (ms, null);
  268. xtw.WriteStartDocument ();
  269. xtw.Flush ();
  270. ms.Seek (0, SeekOrigin.Begin);
  271. sr = new StreamReader (ms, Encoding.UTF8);
  272. AssertEquals ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  273. ms = new MemoryStream ();
  274. xtw = new XmlTextWriter (ms, null);
  275. xtw.WriteStartDocument (true);
  276. xtw.Flush ();
  277. ms.Seek (0, SeekOrigin.Begin);
  278. sr = new StreamReader (ms, Encoding.UTF8);
  279. AssertEquals ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  280. Assert ("BaseStream property returned wrong value.", Object.ReferenceEquals (ms, xtw.BaseStream));
  281. }
  282. [Test]
  283. public void DocumentStart ()
  284. {
  285. xtw.WriteStartDocument ();
  286. AssertEquals ("XmlDeclaration is incorrect.", "<?xml version='1.0' encoding='utf-16'?>", StringWriterText);
  287. try
  288. {
  289. xtw.WriteStartDocument ();
  290. Fail("Should have thrown an InvalidOperationException.");
  291. }
  292. catch (InvalidOperationException) {
  293. // Don't rely on English message assertion.
  294. // It is enough to check an exception occurs.
  295. // AssertEquals ("Exception message is incorrect.",
  296. // "WriteStartDocument should be the first call.", e.Message);
  297. }
  298. xtw = new XmlTextWriter (sw = new StringWriter ());
  299. xtw.QuoteChar = '\'';
  300. xtw.WriteStartDocument (true);
  301. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
  302. xtw = new XmlTextWriter (sw = new StringWriter ());
  303. xtw.QuoteChar = '\'';
  304. xtw.WriteStartDocument (false);
  305. AssertEquals ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
  306. }
  307. [Test]
  308. public void ElementAndAttributeSameXmlns ()
  309. {
  310. xtw.WriteStartElement ("ped", "foo", "urn:foo");
  311. xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
  312. xtw.WriteEndElement ();
  313. AssertEquals ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
  314. }
  315. [Test]
  316. [Category ("NotDotNet")]
  317. public void ElementXmlnsNeedEscape ()
  318. {
  319. xtw.WriteStartElement ("test", "foo", "'");
  320. xtw.WriteEndElement ();
  321. // MS.NET output is : xmlns:test='''
  322. AssertEquals ("<test:foo xmlns:test='&apos;' />", StringWriterText);
  323. }
  324. [Test]
  325. public void ElementEmpty ()
  326. {
  327. xtw.WriteStartElement ("foo");
  328. xtw.WriteEndElement ();
  329. AssertEquals ("Incorrect output.", "<foo />", StringWriterText);
  330. }
  331. [Test]
  332. public void ElementWriteElementString ()
  333. {
  334. xtw.WriteElementString ("foo", "bar");
  335. AssertEquals ("WriteElementString has incorrect output.", "<foo>bar</foo>", StringWriterText);
  336. xtw.WriteElementString ("baz", "");
  337. AssertEquals ("<foo>bar</foo><baz />", StringWriterText);
  338. xtw.WriteElementString ("quux", null);
  339. AssertEquals ("<foo>bar</foo><baz /><quux />", StringWriterText);
  340. xtw.WriteElementString ("", "quuux");
  341. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  342. xtw.WriteElementString (null, "quuuux");
  343. AssertEquals ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  344. }
  345. [Test]
  346. public void FormattingTest ()
  347. {
  348. xtw.Formatting = Formatting.Indented;
  349. xtw.WriteStartDocument ();
  350. xtw.WriteStartElement ("foo");
  351. xtw.WriteElementString ("bar", "");
  352. xtw.Close ();
  353. AssertEquals (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
  354. }
  355. [Test]
  356. public void FormattingInvalidXmlForFun ()
  357. {
  358. xtw.Formatting = Formatting.Indented;
  359. xtw.IndentChar = 'x';
  360. xtw.WriteStartDocument ();
  361. xtw.WriteStartElement ("foo");
  362. xtw.WriteStartElement ("bar");
  363. xtw.WriteElementString ("baz", "");
  364. xtw.Close ();
  365. 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);
  366. }
  367. [Test]
  368. public void FormattingFromRemarks ()
  369. {
  370. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  371. xtw.Formatting = Formatting.Indented;
  372. xtw.WriteStartElement ("ol");
  373. xtw.WriteStartElement ("li");
  374. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  375. xtw.WriteElementString ("b", "E");
  376. xtw.WriteElementString ("i", "lephant");
  377. xtw.WriteString (" walks slowly.");
  378. xtw.WriteEndElement ();
  379. xtw.WriteEndElement ();
  380. AssertEquals (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
  381. }
  382. [Test]
  383. public void LookupPrefix ()
  384. {
  385. xtw.WriteStartElement ("root");
  386. xtw.WriteStartElement ("one");
  387. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  388. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  389. AssertEquals ("foo", xtw.LookupPrefix ("http://abc.def"));
  390. AssertEquals ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  391. xtw.WriteEndElement ();
  392. xtw.WriteStartElement ("two");
  393. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  394. xtw.WriteString("quux");
  395. AssertEquals ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  396. AssertNull (xtw.LookupPrefix ("http://abc.def"));
  397. AssertNull (xtw.LookupPrefix ("http://ghi.jkl"));
  398. AssertNull (xtw.LookupPrefix ("http://bogus"));
  399. }
  400. [Test]
  401. public void NamespacesAttributesPassingInNamespaces ()
  402. {
  403. xtw.Namespaces = false;
  404. xtw.WriteStartElement ("foo");
  405. // These shouldn't throw any exceptions since they don't pass in
  406. // a namespace.
  407. xtw.WriteAttributeString ("bar", "baz");
  408. xtw.WriteAttributeString ("", "a", "", "b");
  409. xtw.WriteAttributeString (null, "c", "", "d");
  410. xtw.WriteAttributeString ("", "e", null, "f");
  411. xtw.WriteAttributeString (null, "g", null, "h");
  412. AssertEquals ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  413. // These should throw ArgumentException because they pass in a
  414. // namespace when Namespaces = false.
  415. }
  416. [Test]
  417. public void NamespacesElementsPassingInNamespaces ()
  418. {
  419. xtw.Namespaces = false;
  420. // These shouldn't throw any exceptions since they don't pass in
  421. // a namespace.
  422. xtw.WriteElementString ("foo", "bar");
  423. xtw.WriteStartElement ("baz");
  424. xtw.WriteStartElement ("quux", "");
  425. xtw.WriteStartElement ("quuux", null);
  426. xtw.WriteStartElement (null, "a", null);
  427. xtw.WriteStartElement (null, "b", "");
  428. xtw.WriteStartElement ("", "c", null);
  429. xtw.WriteStartElement ("", "d", "");
  430. AssertEquals ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  431. // These should throw ArgumentException because they pass in a
  432. // namespace when Namespaces = false.
  433. try {
  434. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  435. Fail ("Expected an ArgumentException.");
  436. } catch (ArgumentException) {}
  437. try {
  438. xtw.WriteStartElement ("foo", "http://netsack.com/");
  439. Fail ("Expected an ArgumentException.");
  440. } catch (ArgumentException) {}
  441. try {
  442. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  443. Fail ("Expected an ArgumentException.");
  444. } catch (ArgumentException) {}
  445. try {
  446. xtw.WriteStartElement ("foo", "bar", null);
  447. Fail ("Expected an ArgumentException.");
  448. } catch (ArgumentException) {}
  449. try {
  450. xtw.WriteStartElement ("foo", "bar", "");
  451. Fail ("Expected an ArgumentException.");
  452. } catch (ArgumentException) {}
  453. try {
  454. xtw.WriteStartElement ("foo", "", "");
  455. Fail ("Expected an ArgumentException.");
  456. } catch (ArgumentException) {}
  457. }
  458. [Test]
  459. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  460. {
  461. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  462. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  463. xtw.WriteElementString("baz", String.Empty, String.Empty);
  464. xtw.WriteEndElement();
  465. xtw.WriteEndElement();
  466. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  467. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
  468. }
  469. [Test]
  470. public void NamespacesPrefix ()
  471. {
  472. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  473. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  474. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  475. xtw.WriteEndElement ();
  476. xtw.WriteEndElement ();
  477. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  478. "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
  479. }
  480. [Test]
  481. [ExpectedException (typeof (ArgumentException))]
  482. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  483. {
  484. xtw.WriteStartElement ("foo", "bar", "");
  485. }
  486. [Test]
  487. [ExpectedException (typeof (ArgumentException))]
  488. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  489. {
  490. xtw.WriteStartElement ("foo", "bar", null);
  491. }
  492. [Test]
  493. public void NamespacesSettingWhenWriteStateNotStart ()
  494. {
  495. xtw.WriteStartElement ("foo");
  496. try
  497. {
  498. xtw.Namespaces = false;
  499. Fail ("Expected an InvalidOperationException.");
  500. }
  501. catch (InvalidOperationException) {}
  502. AssertEquals (true, xtw.Namespaces);
  503. }
  504. [Test]
  505. public void ProcessingInstructionValid ()
  506. {
  507. xtw.WriteProcessingInstruction("foo", "bar");
  508. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
  509. }
  510. [Test]
  511. public void ProcessingInstructionInvalid ()
  512. {
  513. try
  514. {
  515. xtw.WriteProcessingInstruction("fo?>o", "bar");
  516. Fail("Should have thrown an ArgumentException.");
  517. }
  518. catch (ArgumentException) { }
  519. try
  520. {
  521. xtw.WriteProcessingInstruction("foo", "ba?>r");
  522. Fail("Should have thrown an ArgumentException.");
  523. }
  524. catch (ArgumentException) { }
  525. try
  526. {
  527. xtw.WriteProcessingInstruction("", "bar");
  528. Fail("Should have thrown an ArgumentException.");
  529. }
  530. catch (ArgumentException) { }
  531. try
  532. {
  533. xtw.WriteProcessingInstruction(null, "bar");
  534. Fail("Should have thrown an ArgumentException.");
  535. }
  536. catch (ArgumentException) { }
  537. }
  538. [Test]
  539. public void QuoteCharDoubleQuote ()
  540. {
  541. xtw.QuoteChar = '"';
  542. // version, encoding, standalone
  543. xtw.WriteStartDocument (true);
  544. // namespace declaration
  545. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  546. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  547. }
  548. [Test]
  549. [ExpectedException (typeof (ArgumentException))]
  550. public void QuoteCharInvalid ()
  551. {
  552. xtw.QuoteChar = 'x';
  553. }
  554. [Test]
  555. public void WriteBase64 ()
  556. {
  557. UTF8Encoding encoding = new UTF8Encoding();
  558. byte[] fooBar = encoding.GetBytes("foobar");
  559. xtw.WriteBase64 (fooBar, 0, 6);
  560. AssertEquals("Zm9vYmFy", StringWriterText);
  561. try {
  562. xtw.WriteBase64 (fooBar, 3, 6);
  563. Fail ("Expected an Argument Exception to be thrown.");
  564. } catch (ArgumentException) {}
  565. try {
  566. xtw.WriteBase64 (fooBar, -1, 6);
  567. Fail ("Expected an Argument Exception to be thrown.");
  568. } catch (ArgumentOutOfRangeException) {}
  569. try {
  570. xtw.WriteBase64 (fooBar, 3, -1);
  571. Fail ("Expected an Argument Exception to be thrown.");
  572. } catch (ArgumentOutOfRangeException) {}
  573. try {
  574. xtw.WriteBase64 (null, 0, 6);
  575. Fail ("Expected an Argument Exception to be thrown.");
  576. } catch (ArgumentNullException) {}
  577. }
  578. [Test]
  579. public void WriteBinHex ()
  580. {
  581. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  582. xtw.WriteBinHex (bytes, 0, 11);
  583. AssertEquals ("040E22365E7286C2FF0005", StringWriterText);
  584. }
  585. [Test]
  586. public void WriteCharEntity ()
  587. {
  588. xtw.WriteCharEntity ('a');
  589. AssertEquals ("&#x61;", StringWriterText);
  590. xtw.WriteCharEntity ('A');
  591. AssertEquals ("&#x61;&#x41;", StringWriterText);
  592. xtw.WriteCharEntity ('1');
  593. AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
  594. xtw.WriteCharEntity ('K');
  595. AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  596. try {
  597. xtw.WriteCharEntity ((char)0xd800);
  598. } catch (ArgumentException) {}
  599. }
  600. [Test]
  601. [ExpectedException (typeof (InvalidOperationException))]
  602. public void WriteEndAttribute ()
  603. {
  604. xtw.WriteEndAttribute ();
  605. }
  606. [Test]
  607. public void WriteEndDocument ()
  608. {
  609. try {
  610. xtw.WriteEndDocument ();
  611. Fail ("Expected an ArgumentException.");
  612. } catch (ArgumentException) {}
  613. xtw.WriteStartDocument ();
  614. try
  615. {
  616. xtw.WriteEndDocument ();
  617. Fail ("Expected an ArgumentException.");
  618. }
  619. catch (ArgumentException) {}
  620. xtw.WriteStartElement ("foo");
  621. xtw.WriteStartAttribute ("bar", null);
  622. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  623. xtw.WriteEndDocument ();
  624. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  625. AssertEquals (WriteState.Start, xtw.WriteState);
  626. }
  627. [Test]
  628. public void WriteEndElement ()
  629. {
  630. try {
  631. xtw.WriteEndElement ();
  632. Fail ("Should have thrown an InvalidOperationException.");
  633. } catch (InvalidOperationException) {
  634. // Don't rely on English message assertion.
  635. // It is enough to check an exception occurs.
  636. // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
  637. }
  638. xtw.WriteStartElement ("foo");
  639. xtw.WriteEndElement ();
  640. AssertEquals ("<foo />", StringWriterText);
  641. xtw.WriteStartElement ("bar");
  642. xtw.WriteStartAttribute ("baz", null);
  643. xtw.WriteEndElement ();
  644. AssertEquals ("<foo /><bar baz='' />", StringWriterText);
  645. }
  646. [Test]
  647. public void FullEndElement ()
  648. {
  649. xtw.WriteStartElement ("foo");
  650. xtw.WriteFullEndElement ();
  651. AssertEquals ("<foo></foo>", StringWriterText);
  652. xtw.WriteStartElement ("bar");
  653. xtw.WriteAttributeString ("foo", "bar");
  654. xtw.WriteFullEndElement ();
  655. AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  656. xtw.WriteStartElement ("baz");
  657. xtw.WriteStartAttribute ("bar", null);
  658. xtw.WriteFullEndElement ();
  659. AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  660. }
  661. [Test]
  662. public void WriteQualifiedName ()
  663. {
  664. xtw.WriteStartElement (null, "test", null);
  665. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  666. xtw.WriteQualifiedName ("bob", "http://localhost/");
  667. xtw.WriteEndElement ();
  668. AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  669. }
  670. [Test]
  671. public void WriteRaw ()
  672. {
  673. xtw.WriteRaw("&<>\"'");
  674. AssertEquals ("&<>\"'", StringWriterText);
  675. xtw.WriteRaw(null);
  676. AssertEquals ("&<>\"'", StringWriterText);
  677. xtw.WriteRaw("");
  678. AssertEquals ("&<>\"'", StringWriterText);
  679. }
  680. [Test]
  681. public void WriteRawInvalidInAttribute ()
  682. {
  683. xtw.WriteStartElement ("foo");
  684. xtw.WriteStartAttribute ("bar", null);
  685. xtw.WriteRaw ("&<>\"'");
  686. xtw.WriteEndAttribute ();
  687. xtw.WriteEndElement ();
  688. AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
  689. }
  690. [Test]
  691. public void WriteStateTest ()
  692. {
  693. AssertEquals (WriteState.Start, xtw.WriteState);
  694. xtw.WriteStartDocument ();
  695. AssertEquals (WriteState.Prolog, xtw.WriteState);
  696. xtw.WriteStartElement ("root");
  697. AssertEquals (WriteState.Element, xtw.WriteState);
  698. xtw.WriteElementString ("foo", "bar");
  699. AssertEquals (WriteState.Content, xtw.WriteState);
  700. xtw.Close ();
  701. AssertEquals (WriteState.Closed, xtw.WriteState);
  702. }
  703. [Test]
  704. public void WriteString ()
  705. {
  706. xtw.WriteStartDocument ();
  707. try {
  708. xtw.WriteString("foo");
  709. } catch (InvalidOperationException) {}
  710. // Testing attribute values
  711. xtw.WriteStartElement ("foo");
  712. xtw.WriteAttributeString ("bar", "&<>");
  713. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  714. }
  715. [Test]
  716. public void WriteAttributeStringSingleQuoteChar()
  717. {
  718. // When QuoteChar is single quote then replaces single quotes within attributes
  719. // but not double quotes.
  720. xtw.WriteStartElement ("foo");
  721. xtw.WriteAttributeString ("bar", "\"baz\"");
  722. xtw.WriteAttributeString ("quux", "'baz'");
  723. AssertEquals ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  724. }
  725. [Test]
  726. public void WriteAttributeStringDoubleQuoteChar()
  727. {
  728. // When QuoteChar is double quote then replaces double quotes within attributes
  729. // but not single quotes.
  730. xtw.QuoteChar = '"';
  731. xtw.WriteStartElement ("foo");
  732. xtw.WriteAttributeString ("bar", "\"baz\"");
  733. xtw.WriteAttributeString ("quux", "'baz'");
  734. AssertEquals ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  735. }
  736. [Test]
  737. public void WriteStringWithEntities()
  738. {
  739. // Testing element values
  740. xtw.QuoteChar = '\'';
  741. xtw.WriteElementString ("foo", "&<>\"'");
  742. AssertEquals ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  743. }
  744. [Test]
  745. public void XmlLang ()
  746. {
  747. AssertNull (xtw.XmlLang);
  748. xtw.WriteStartElement ("foo");
  749. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  750. AssertEquals ("langfoo", xtw.XmlLang);
  751. AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
  752. xtw.WriteAttributeString ("boo", "yah");
  753. AssertEquals ("langfoo", xtw.XmlLang);
  754. AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  755. xtw.WriteElementString("bar", "baz");
  756. AssertEquals ("langfoo", xtw.XmlLang);
  757. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  758. xtw.WriteString("baz");
  759. AssertEquals ("langfoo", xtw.XmlLang);
  760. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  761. xtw.WriteStartElement ("quux");
  762. xtw.WriteStartAttribute ("xml", "lang", null);
  763. AssertEquals ("langfoo", xtw.XmlLang);
  764. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  765. xtw.WriteString("langbar");
  766. // Commented out there: it is implementation-dependent.
  767. // and incompatible between .NET 1.0 and 1.1
  768. // AssertEquals ("langfoo", xtw.XmlLang);
  769. // AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  770. xtw.WriteEndAttribute ();
  771. // Commented out there: it is implementation-dependent.
  772. // and incompatible between .NET 1.0 and 1.1
  773. // AssertEquals ("langbar", xtw.XmlLang);
  774. // AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  775. // check if xml:lang repeats output even if same as current scope.
  776. xtw.WriteStartElement ("joe");
  777. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  778. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  779. xtw.WriteElementString ("quuux", "squonk");
  780. AssertEquals ("langbar", xtw.XmlLang);
  781. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  782. xtw.WriteEndElement ();
  783. xtw.WriteEndElement ();
  784. AssertEquals ("langfoo", xtw.XmlLang);
  785. 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);
  786. xtw.WriteEndElement ();
  787. AssertNull (xtw.XmlLang);
  788. 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);
  789. xtw.Close ();
  790. AssertNull (xtw.XmlLang);
  791. }
  792. // TODO: test operational aspects
  793. [Test]
  794. public void XmlSpaceTest ()
  795. {
  796. xtw.WriteStartElement ("foo");
  797. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  798. xtw.WriteStartElement ("bar");
  799. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  800. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  801. AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
  802. xtw.WriteStartElement ("baz");
  803. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  804. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  805. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  806. xtw.WriteStartElement ("quux");
  807. xtw.WriteStartAttribute ("xml", "space", null);
  808. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  809. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  810. // Commented out there: it is implementation-dependent
  811. // and incompatible between .NET 1.0 and 1.1
  812. xtw.WriteString ("default");
  813. // AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  814. // AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  815. xtw.WriteEndAttribute ();
  816. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  817. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  818. xtw.WriteEndElement ();
  819. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  820. xtw.WriteEndElement ();
  821. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  822. xtw.WriteEndElement ();
  823. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  824. xtw.WriteStartElement ("quux");
  825. try {
  826. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  827. } catch (ArgumentException) {}
  828. try {
  829. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  830. } catch (ArgumentException) {}
  831. try {
  832. xtw.WriteAttributeString ("xml", "space", null, "Preserve");
  833. } catch (ArgumentException) {}
  834. try {
  835. xtw.WriteAttributeString ("xml", "space", null, "Default");
  836. } catch (ArgumentException) {}
  837. try {
  838. xtw.WriteWhitespace ("x");
  839. } catch (ArgumentException) { }
  840. }
  841. [Test]
  842. public void XmlSpaceRaw ()
  843. {
  844. xtw.WriteStartElement ("foo");
  845. xtw.WriteStartAttribute ("xml", "space", null);
  846. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  847. AssertEquals ("<foo xml:space='", StringWriterText);
  848. xtw.WriteString ("default");
  849. // Commented out there: it is implementation-dependent
  850. // and incompatible between .NET 1.0 and 1.1
  851. // AssertEquals (XmlSpace.None, xtw.XmlSpace);
  852. // AssertEquals ("<foo xml:space='", StringWriterText);
  853. xtw.WriteEndAttribute ();
  854. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  855. AssertEquals ("<foo xml:space='default'", StringWriterText);
  856. }
  857. [Test]
  858. public void WriteAttributes ()
  859. {
  860. XmlDocument doc = new XmlDocument();
  861. StringWriter sw = new StringWriter();
  862. XmlWriter wr = new XmlTextWriter(sw);
  863. StringBuilder sb = sw.GetStringBuilder();
  864. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  865. 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);
  866. xtr.Read(); // read XMLDecl
  867. wr.WriteAttributes(xtr, false);
  868. // This method don't always have to take this double-quoted style...
  869. AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
  870. sb.Remove(0, sb.Length); // init
  871. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  872. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  873. xtr.Read(); // read XMLDecl
  874. AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType);
  875. sw = new StringWriter ();
  876. wr = new XmlTextWriter (sw);
  877. // This block raises an error on MS.NET 1.0.
  878. wr.WriteAttributes(xtr, false);
  879. // This method don't always have to take this double-quoted style...
  880. AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
  881. sw = new StringWriter ();
  882. wr = new XmlTextWriter (sw);
  883. sb.Remove(0, sb.Length); // init
  884. xtr.Read(); // read root
  885. AssertEquals (XmlNodeType.Element, xtr.NodeType);
  886. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  887. wr.WriteAttributes(xtr, false);
  888. wr.WriteEndElement();
  889. wr.Close();
  890. // This method don't always have to take this double-quoted style...
  891. AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());
  892. xtr.Close ();
  893. }
  894. [Test]
  895. public void WriteWhitespace ()
  896. {
  897. xtw.WriteStartElement ("a");
  898. xtw.WriteWhitespace ("\n\t");
  899. xtw.WriteStartElement ("b");
  900. xtw.WriteWhitespace ("\n\t");
  901. xtw.WriteEndElement ();
  902. xtw.WriteWhitespace ("\n");
  903. xtw.WriteEndElement ();
  904. xtw.WriteWhitespace ("\n");
  905. xtw.Flush ();
  906. AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  907. }
  908. [Test]
  909. public void FlushDoesntCloseTag ()
  910. {
  911. xtw.WriteStartElement ("foo");
  912. xtw.WriteAttributeString ("bar", "baz");
  913. xtw.Flush ();
  914. AssertEquals ("<foo bar='baz'", StringWriterText);
  915. }
  916. [Test]
  917. public void WriteWhitespaceClosesTag ()
  918. {
  919. xtw.WriteStartElement ("foo");
  920. xtw.WriteAttributeString ("bar", "baz");
  921. xtw.WriteWhitespace (" ");
  922. AssertEquals ("<foo bar='baz'> ", StringWriterText);
  923. }
  924. [Test]
  925. public void DontOutputMultipleXmlns ()
  926. {
  927. XmlDocument doc = new XmlDocument();
  928. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  929. XmlDocument doc2 = new XmlDocument();
  930. doc2.LoadXml(doc.InnerXml);
  931. AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  932. doc2.OuterXml);
  933. }
  934. [Test]
  935. public void DontOutputNonDeclaredXmlns ()
  936. {
  937. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  938. XmlDocument doc = new XmlDocument();
  939. doc.LoadXml(xml);
  940. XmlDocument doc2 = new XmlDocument();
  941. doc2.LoadXml(doc.InnerXml);
  942. AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml);
  943. }
  944. [Test]
  945. public void DontOutputRemovalDefaultNSDeclaration ()
  946. {
  947. xtw.WriteStartDocument ();
  948. xtw.WriteStartElement ("foo");
  949. xtw.WriteAttributeString ("xmlns", "probe");
  950. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  951. xtw.WriteStartElement ("b");
  952. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  953. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  954. xtw.WriteEndElement (); // b2
  955. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  956. xtw.WriteEndElement (); // b2
  957. xtw.WriteEndElement (); // b
  958. xtw.WriteEndElement (); // foo
  959. xtw.WriteEndDocument ();
  960. xtw.Close ();
  961. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText);
  962. }
  963. [Test]
  964. public void DontOutputRemovalDefaultNSDeclaration2 ()
  965. {
  966. xtw.WriteStartDocument ();
  967. // IMPORTANT DIFFERENCE!! ns = "", not null
  968. xtw.WriteStartElement ("foo", "");
  969. xtw.WriteAttributeString ("xmlns", "probe");
  970. AssertNull (xtw.LookupPrefix ("probe"));
  971. xtw.WriteStartElement ("b");
  972. AssertNull (xtw.LookupPrefix ("probe"));
  973. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  974. xtw.WriteEndElement (); // b2
  975. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  976. xtw.WriteEndElement (); // b2
  977. xtw.WriteEndElement (); // b
  978. xtw.WriteEndElement (); // foo
  979. xtw.WriteEndDocument ();
  980. xtw.Close ();
  981. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText);
  982. }
  983. [Test]
  984. public void DoOutputRemovalDefaultNSDeclaration ()
  985. {
  986. xtw.WriteStartElement ("docelem", "a-namespace");
  987. XmlDocument doc = new XmlDocument ();
  988. doc.CreateElement ("hola").WriteTo (xtw);
  989. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  990. xtw.WriteEndElement ();
  991. xtw.Close ();
  992. AssertEquals ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  993. }
  994. [Test]
  995. public void WriteAttributeTakePrecedenceOnXmlns ()
  996. {
  997. xtw.WriteStartElement ("root", "urn:foo");
  998. xtw.WriteAttributeString ("xmlns", "urn:bar");
  999. xtw.WriteEndElement ();
  1000. xtw.Close ();
  1001. AssertEquals ("<root xmlns='urn:bar' />", StringWriterText);
  1002. }
  1003. [Test]
  1004. [ExpectedException (typeof (ArgumentException))]
  1005. public void LookupPrefixNull ()
  1006. {
  1007. xtw.LookupPrefix (null);
  1008. }
  1009. [Test]
  1010. [ExpectedException (typeof (ArgumentException))]
  1011. public void LookupPrefixEmpty ()
  1012. {
  1013. xtw.LookupPrefix (String.Empty);
  1014. }
  1015. [Test]
  1016. public void LookupPrefixIgnoresXmlnsAttribute ()
  1017. {
  1018. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1019. xtw.WriteStartElement ("root");
  1020. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1021. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1022. // Surprisingly to say, it is ignored!!
  1023. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1024. xtw.WriteStartElement ("hoge");
  1025. // (still after flushing previous start element.)
  1026. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1027. xtw.WriteStartElement ("fuga", "urn:foo");
  1028. // Is this testing on the correct way? Yes, here it is.
  1029. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1030. }
  1031. [Test]
  1032. public void WriteInvalidNames ()
  1033. {
  1034. xtw.WriteStartElement ("foo<>");
  1035. xtw.WriteAttributeString ("ho<>ge", "value");
  1036. }
  1037. [Test]
  1038. [ExpectedException (typeof (ArgumentException))]
  1039. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1040. {
  1041. xtw.WriteStartAttribute ("some", "foo", null);
  1042. }
  1043. [Test]
  1044. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1045. {
  1046. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1047. }
  1048. [Test]
  1049. [ExpectedException (typeof (ArgumentException))]
  1050. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1051. {
  1052. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1053. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1054. xtw.WriteEndAttribute ();
  1055. }
  1056. [Test]
  1057. [ExpectedException (typeof (ArgumentException))]
  1058. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1059. {
  1060. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1061. }
  1062. [Test]
  1063. [ExpectedException (typeof (ArgumentException))]
  1064. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1065. {
  1066. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1067. }
  1068. [Test]
  1069. public void WriteRawProceedToProlog ()
  1070. {
  1071. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1072. xtw.WriteRaw ("");
  1073. AssertEquals (WriteState.Prolog, xtw.WriteState);
  1074. }
  1075. [Test]
  1076. public void Indent ()
  1077. {
  1078. XmlDocument doc = new XmlDocument ();
  1079. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1080. StringWriter sw = new StringWriter ();
  1081. sw.NewLine = "_";
  1082. XmlTextWriter xtw = new XmlTextWriter (sw);
  1083. xtw.Formatting = Formatting.Indented;
  1084. doc.WriteContentTo (xtw);
  1085. AssertEquals (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1086. }
  1087. [Test]
  1088. public void Indent2 ()
  1089. {
  1090. StringWriter sw = new StringWriter ();
  1091. XmlTextWriter xtw = new XmlTextWriter (sw);
  1092. xtw.Formatting = Formatting.Indented;
  1093. // sadly, this silly usage of this method is actually
  1094. // used in WriteNode() in MS.NET.
  1095. xtw.WriteProcessingInstruction ("xml",
  1096. "version=\"1.0\"");
  1097. xtw.WriteComment ("sample XML fragment");
  1098. AssertEquals (@"<?xml version=""1.0""?>
  1099. <!--sample XML fragment-->", sw.ToString ().Replace ("\r\n", "\n"));
  1100. }
  1101. [Test]
  1102. public void CloseTwice ()
  1103. {
  1104. StringWriter sw = new StringWriter ();
  1105. XmlTextWriter writer = new XmlTextWriter (sw);
  1106. writer.Close ();
  1107. // should not result in an exception
  1108. writer.Close ();
  1109. }
  1110. }
  1111. }