XmlSimpleDictionaryWriterTest.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. //
  2. // XmlSimpleDictionaryWriterTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Copied from System.XML/Test/System.Xml/XmlTextWriterTests.cs
  10. //
  11. // Authors:
  12. // Kral Ferch <[email protected]>
  13. // Martin Willemoes Hansen <[email protected]>
  14. //
  15. // (C) 2002 Kral Ferch
  16. // (C) 2003 Martin Willemoes Hansen
  17. //
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining
  20. // a copy of this software and associated documentation files (the
  21. // "Software"), to deal in the Software without restriction, including
  22. // without limitation the rights to use, copy, modify, merge, publish,
  23. // distribute, sublicense, and/or sell copies of the Software, and to
  24. // permit persons to whom the Software is furnished to do so, subject to
  25. // the following conditions:
  26. //
  27. // The above copyright notice and this permission notice shall be
  28. // included in all copies or substantial portions of the Software.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. using System;
  39. using System.IO;
  40. using System.Text;
  41. using System.Xml;
  42. using NUnit.Framework;
  43. namespace MonoTests.System.Xml
  44. {
  45. [TestFixture]
  46. public class XmlSimpleDictionaryWriterTest
  47. {
  48. [Test]
  49. public void WriteXmlnsAttribute ()
  50. {
  51. xw.WriteStartElement ("l1");
  52. xw.WriteXmlAttribute ("lang", "ja");
  53. xw.WriteXmlnsAttribute ("f", "urn:foo");
  54. xw.WriteStartElement ("l2");
  55. xw.WriteXmlnsAttribute ("", "");
  56. xw.WriteEndElement ();
  57. xw.WriteStartElement ("l2");
  58. xw.WriteXmlnsAttribute ("", "urn:bar");
  59. xw.WriteEndElement ();
  60. xw.WriteEndElement ();
  61. xw.Flush ();
  62. Assert.AreEqual ("<l1 xml:lang='ja' xmlns:f='urn:foo'><l2 xmlns='' /><l2 xmlns='urn:bar' /></l1>", Output);
  63. }
  64. [Test]
  65. public void WriteXmlnsAttributeNullPrefix ()
  66. {
  67. xw.WriteStartElement ("root", "urn:x");
  68. xw.WriteXmlnsAttribute (null, "urn:foo");
  69. xw.WriteEndElement ();
  70. xw.Close ();
  71. Assert.AreEqual ("<root xmlns:d1p1='urn:foo' xmlns='urn:x' />", Output);
  72. }
  73. #region Copied from XmlTextWriterTests.cs
  74. StringWriter sw;
  75. XmlDictionaryWriter xw;
  76. [SetUp]
  77. public void GetReady ()
  78. {
  79. sw = new StringWriter ();
  80. XmlTextWriter xtw = new XmlTextWriter (sw);
  81. xtw.QuoteChar = '\'';
  82. xw = XmlDictionaryWriter.CreateDictionaryWriter (xtw);
  83. }
  84. private string Output
  85. {
  86. get { return sw.ToString (); }
  87. }
  88. [Test]
  89. public void AttributeNamespacesNonNamespaceAttributeBefore ()
  90. {
  91. xw.WriteStartElement ("foo");
  92. xw.WriteAttributeString("bar", "baz");
  93. xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  94. Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", Output);
  95. }
  96. [Test]
  97. public void AttributeNamespacesNonNamespaceAttributeAfter ()
  98. {
  99. xw.WriteStartElement ("foo");
  100. xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  101. xw.WriteAttributeString("bar", "baz");
  102. Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", Output);
  103. }
  104. [Test]
  105. public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
  106. {
  107. xw.WriteAttributeString ("xmlns", null, "http://abc.def");
  108. Assert.AreEqual ("xmlns='http://abc.def'", Output);
  109. }
  110. [Test]
  111. public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
  112. {
  113. try
  114. {
  115. xw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
  116. }
  117. catch (ArgumentException) {}
  118. }
  119. [Test]
  120. public void AttributeNamespacesWithNullInNamespaceParam ()
  121. {
  122. xw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  123. Assert.AreEqual ("xmlns:abc='http://abc.def'", Output);
  124. }
  125. [Test]
  126. public void AttributeNamespacesWithTextInNamespaceParam ()
  127. {
  128. try {
  129. xw.WriteAttributeString ("xmlns", "abc", "http://somenamespace.com", "http://abc.def");
  130. } catch (ArgumentException) {}
  131. }
  132. [Test]
  133. public void AttributeNamespacesXmlnsXmlns ()
  134. {
  135. xw.WriteStartElement ("foo");
  136. // When namespaceURI argument is null, constraints by
  137. // namespaces in XML are ignored.
  138. xw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  139. xw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
  140. }
  141. [Test]
  142. public void AttributeWriteAttributeString ()
  143. {
  144. xw.WriteStartElement ("foo");
  145. xw.WriteAttributeString ("foo", "bar");
  146. Assert.AreEqual ("<foo foo='bar'", Output);
  147. xw.WriteAttributeString ("bar", "");
  148. Assert.AreEqual ("<foo foo='bar' bar=''", Output);
  149. xw.WriteAttributeString ("baz", null);
  150. Assert.AreEqual ("<foo foo='bar' bar='' baz=''", Output);
  151. xw.WriteAttributeString ("hoge", "a\nb");
  152. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b'", Output);
  153. xw.WriteAttributeString ("fuga", " a\t\r\nb\t");
  154. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b' fuga=' a\t&#xD;&#xA;b\t'", Output);
  155. try {
  156. // Why does this pass Microsoft?
  157. // Anyway, Mono should not allow such code.
  158. xw.WriteAttributeString ("", "quux");
  159. // Assert.AreEqual ("<foo foo='bar' bar='' baz='' ='quux'", Output);
  160. Assert.Fail ("empty name not allowed.");
  161. } catch (Exception) {
  162. }
  163. try {
  164. // Why does this pass Microsoft?
  165. // Anyway, Mono should not allow such code.
  166. xw.WriteAttributeString (null, "quuux");
  167. // Assert.AreEqual ("<foo foo='bar' bar='' baz='' ='quux' ='quuux'", Output);
  168. Assert.Fail ("null name not allowed.");
  169. } catch (Exception) {
  170. }
  171. }
  172. [Test]
  173. [ExpectedException (typeof (InvalidOperationException))]
  174. public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
  175. {
  176. xw.WriteStartElement ("foo");
  177. xw.WriteString ("bar");
  178. xw.WriteAttributeString ("baz", "quux");
  179. }
  180. [Test]
  181. public void AttributeWriteAttributeStringWithoutParentElement ()
  182. {
  183. xw.WriteAttributeString ("foo", "bar");
  184. Assert.AreEqual ("foo='bar'", Output);
  185. xw.WriteAttributeString ("baz", "quux");
  186. Assert.AreEqual ("foo='bar' baz='quux'", Output);
  187. }
  188. [Test]
  189. public void CDataValid ()
  190. {
  191. xw.WriteCData ("foo");
  192. Assert.AreEqual ("<![CDATA[foo]]>", Output,
  193. "WriteCData had incorrect output.");
  194. }
  195. [Test]
  196. [ExpectedException (typeof (ArgumentException))]
  197. public void CDataInvalid ()
  198. {
  199. xw.WriteCData("foo]]>bar");
  200. }
  201. [Test]
  202. public void CloseOpenElements ()
  203. {
  204. xw.WriteStartElement("foo");
  205. xw.WriteStartElement("bar");
  206. xw.WriteStartElement("baz");
  207. xw.Close();
  208. Assert.AreEqual ("<foo><bar><baz /></bar></foo>", Output,
  209. "Close didn't write out end elements properly.");
  210. }
  211. [Test]
  212. public void CloseWriteAfter ()
  213. {
  214. xw.WriteElementString ("foo", "bar");
  215. xw.Close ();
  216. // WriteEndElement and WriteStartDocument aren't tested here because
  217. // they will always throw different exceptions besides 'The Writer is closed.'
  218. // and there are already tests for those exceptions.
  219. try {
  220. xw.WriteCData ("foo");
  221. Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  222. }
  223. catch (InvalidOperationException) {
  224. // Don't rely on English message assertion.
  225. // It is enough to check an exception occurs.
  226. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  227. }
  228. try {
  229. xw.WriteComment ("foo");
  230. Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  231. }
  232. catch (InvalidOperationException) {
  233. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  234. }
  235. try {
  236. xw.WriteProcessingInstruction ("foo", "bar");
  237. Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  238. }
  239. catch (InvalidOperationException) {
  240. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  241. }
  242. try {
  243. xw.WriteStartElement ("foo", "bar", "baz");
  244. Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  245. }
  246. catch (InvalidOperationException) {
  247. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  248. }
  249. try
  250. {
  251. xw.WriteAttributeString ("foo", "bar");
  252. Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  253. }
  254. catch (InvalidOperationException)
  255. {
  256. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  257. }
  258. try {
  259. xw.WriteString ("foo");
  260. Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  261. }
  262. catch (InvalidOperationException) {
  263. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  264. }
  265. }
  266. [Test]
  267. public void CommentValid ()
  268. {
  269. xw.WriteComment ("foo");
  270. Assert.AreEqual ("<!--foo-->", Output,
  271. "WriteComment had incorrect output.");
  272. }
  273. [Test]
  274. public void CommentInvalid ()
  275. {
  276. try {
  277. xw.WriteComment("foo-");
  278. Assert.Fail ("Should have thrown an ArgumentException.");
  279. }
  280. catch (ArgumentException) { }
  281. try {
  282. xw.WriteComment("foo-->bar");
  283. Assert.Fail ("Should have thrown an ArgumentException.");
  284. }
  285. catch (ArgumentException) { }
  286. }
  287. [Test]
  288. public void DocumentStart ()
  289. {
  290. xw.WriteStartDocument ();
  291. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", Output,
  292. "XmlDeclaration is incorrect.");
  293. try
  294. {
  295. xw.WriteStartDocument ();
  296. Assert.Fail ("Should have thrown an InvalidOperationException.");
  297. }
  298. catch (InvalidOperationException) {
  299. // Don't rely on English message assertion.
  300. // It is enough to check an exception occurs.
  301. // Assert.AreEqual ("Exception message is incorrect.",
  302. // "WriteStartDocument should be the first call.", e.Message);
  303. }
  304. GetReady ();
  305. xw.WriteStartDocument (true);
  306. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", Output);
  307. GetReady ();
  308. xw.WriteStartDocument (false);
  309. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", Output);
  310. }
  311. [Test]
  312. public void ElementAndAttributeSameXmlns ()
  313. {
  314. xw.WriteStartElement ("ped", "foo", "urn:foo");
  315. xw.WriteStartAttribute ("ped", "foo", "urn:foo");
  316. xw.WriteEndElement ();
  317. Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", Output);
  318. }
  319. [Test]
  320. [Category ("NotDotNet")]
  321. public void ElementXmlnsNeedEscape ()
  322. {
  323. xw.WriteStartElement ("test", "foo", "'");
  324. xw.WriteEndElement ();
  325. // MS.NET output is : xmlns:test='''
  326. Assert.AreEqual ("<test:foo xmlns:test='&apos;' />", Output);
  327. }
  328. [Test]
  329. public void ElementEmpty ()
  330. {
  331. xw.WriteStartElement ("foo");
  332. xw.WriteEndElement ();
  333. Assert.AreEqual ("<foo />", Output);
  334. }
  335. [Test]
  336. public void ElementWriteElementString ()
  337. {
  338. xw.WriteElementString ("foo", "bar");
  339. Assert.AreEqual ("<foo>bar</foo>", Output,
  340. "WriteElementString has incorrect output.");
  341. xw.WriteElementString ("baz", "");
  342. Assert.AreEqual ("<foo>bar</foo><baz />", Output);
  343. xw.WriteElementString ("quux", null);
  344. Assert.AreEqual ("<foo>bar</foo><baz /><quux />", Output);
  345. xw.WriteElementString ("", "quuux");
  346. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", Output);
  347. xw.WriteElementString (null, "quuuux");
  348. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", Output);
  349. }
  350. [Test]
  351. public void LookupPrefix ()
  352. {
  353. xw.WriteStartElement ("root");
  354. xw.WriteStartElement ("one");
  355. xw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  356. xw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  357. Assert.AreEqual ("foo", xw.LookupPrefix ("http://abc.def"));
  358. Assert.AreEqual ("bar", xw.LookupPrefix ("http://ghi.jkl"));
  359. xw.WriteEndElement ();
  360. xw.WriteStartElement ("two");
  361. xw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  362. xw.WriteString("quux");
  363. Assert.AreEqual ("baz", xw.LookupPrefix ("http://mno.pqr"));
  364. Assert.IsNull (xw.LookupPrefix ("http://abc.def"));
  365. Assert.IsNull (xw.LookupPrefix ("http://ghi.jkl"));
  366. Assert.IsNull (xw.LookupPrefix ("http://bogus"));
  367. }
  368. [Test]
  369. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  370. {
  371. xw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  372. xw.WriteStartElement(String.Empty, "bar", String.Empty);
  373. xw.WriteElementString("baz", String.Empty, String.Empty);
  374. xw.WriteEndElement();
  375. xw.WriteEndElement();
  376. Assert.AreEqual (
  377. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
  378. Output,
  379. "XmlTextWriter is incorrectly outputting namespaces.");
  380. }
  381. [Test]
  382. public void NamespacesPrefix ()
  383. {
  384. xw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  385. xw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  386. xw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  387. xw.WriteEndElement ();
  388. xw.WriteEndElement ();
  389. Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
  390. Output,
  391. "XmlTextWriter is incorrectly outputting prefixes.");
  392. }
  393. [Test]
  394. // [ExpectedException (typeof (ArgumentException))]
  395. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  396. {
  397. xw.WriteStartElement ("foo", "bar", "");
  398. }
  399. [Test]
  400. [ExpectedException (typeof (ArgumentException))]
  401. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  402. {
  403. xw.WriteStartElement ("foo", "bar", null);
  404. }
  405. [Test]
  406. public void ProcessingInstructionValid ()
  407. {
  408. xw.WriteProcessingInstruction("foo", "bar");
  409. Assert.AreEqual ("<?foo bar?>", Output, "WriteProcessingInstruction had incorrect output.");
  410. }
  411. [Test]
  412. [ExpectedException (typeof (ArgumentException))]
  413. public void ProcessingInstructionInvalid1 ()
  414. {
  415. xw.WriteProcessingInstruction("fo?>o", "bar");
  416. }
  417. [Test]
  418. [ExpectedException (typeof (ArgumentException))]
  419. public void ProcessingInstructionInvalid2 ()
  420. {
  421. xw.WriteProcessingInstruction("foo", "ba?>r");
  422. }
  423. [Test]
  424. [ExpectedException (typeof (ArgumentException))]
  425. public void ProcessingInstructionInvalid3 ()
  426. {
  427. xw.WriteProcessingInstruction("", "bar");
  428. }
  429. [Test]
  430. [ExpectedException (typeof (ArgumentException))]
  431. public void ProcessingInstructionInvalid4 ()
  432. {
  433. xw.WriteProcessingInstruction(null, "bar");
  434. }
  435. [Test]
  436. public void WriteBase64 ()
  437. {
  438. UTF8Encoding encoding = new UTF8Encoding();
  439. byte[] fooBar = encoding.GetBytes("foobar");
  440. xw.WriteBase64 (fooBar, 0, 6);
  441. Assert.AreEqual("Zm9vYmFy", Output);
  442. try {
  443. xw.WriteBase64 (fooBar, 3, 6);
  444. Assert.Fail ("Expected an Argument Exception to be thrown.");
  445. } catch (ArgumentException) {}
  446. try {
  447. xw.WriteBase64 (fooBar, -1, 6);
  448. Assert.Fail ("Expected an Argument Exception to be thrown.");
  449. } catch (ArgumentOutOfRangeException) {}
  450. try {
  451. xw.WriteBase64 (fooBar, 3, -1);
  452. Assert.Fail ("Expected an Argument Exception to be thrown.");
  453. } catch (ArgumentOutOfRangeException) {}
  454. try {
  455. xw.WriteBase64 (null, 0, 6);
  456. Assert.Fail ("Expected an Argument Exception to be thrown.");
  457. } catch (ArgumentNullException) {}
  458. }
  459. [Test]
  460. public void WriteBinHex ()
  461. {
  462. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  463. xw.WriteBinHex (bytes, 0, 11);
  464. Assert.AreEqual ("040E22365E7286C2FF0005", Output);
  465. }
  466. [Test]
  467. public void WriteCharEntity ()
  468. {
  469. xw.WriteCharEntity ('a');
  470. Assert.AreEqual ("&#x61;", Output);
  471. xw.WriteCharEntity ('A');
  472. Assert.AreEqual ("&#x61;&#x41;", Output);
  473. xw.WriteCharEntity ('1');
  474. Assert.AreEqual ("&#x61;&#x41;&#x31;", Output);
  475. xw.WriteCharEntity ('K');
  476. Assert.AreEqual ("&#x61;&#x41;&#x31;&#x4B;", Output);
  477. try {
  478. xw.WriteCharEntity ((char)0xd800);
  479. } catch (ArgumentException) {}
  480. }
  481. [Test]
  482. [ExpectedException (typeof (InvalidOperationException))]
  483. public void WriteEndAttribute ()
  484. {
  485. xw.WriteEndAttribute ();
  486. }
  487. [Test]
  488. public void WriteEndDocument ()
  489. {
  490. try {
  491. xw.WriteEndDocument ();
  492. Assert.Fail ("Expected an Exception.");
  493. // in .NET 2.0 it is InvalidOperationException.
  494. // in .NET 1,1 it is ArgumentException.
  495. } catch (Exception) {}
  496. }
  497. [Test]
  498. public void WriteEndDocument2 ()
  499. {
  500. xw.WriteStartDocument ();
  501. try
  502. {
  503. xw.WriteEndDocument ();
  504. Assert.Fail ("Expected an Exception.");
  505. // in .NET 2.0 it is InvalidOperationException.
  506. // in .NET 1,1 it is ArgumentException.
  507. } catch (Exception) {}
  508. }
  509. [Test]
  510. public void WriteEndDocument3 ()
  511. {
  512. xw.WriteStartDocument ();
  513. xw.WriteStartElement ("foo");
  514. xw.WriteStartAttribute ("bar", null);
  515. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", Output);
  516. xw.WriteEndDocument ();
  517. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", Output);
  518. Assert.AreEqual (WriteState.Start, xw.WriteState);
  519. }
  520. [Test]
  521. [ExpectedException (typeof (InvalidOperationException))]
  522. public void WriteEndElement ()
  523. {
  524. // no matching StartElement
  525. xw.WriteEndElement ();
  526. }
  527. [Test]
  528. public void WriteEndElement2 ()
  529. {
  530. xw.WriteStartElement ("foo");
  531. xw.WriteEndElement ();
  532. Assert.AreEqual ("<foo />", Output);
  533. xw.WriteStartElement ("bar");
  534. xw.WriteStartAttribute ("baz", null);
  535. xw.WriteEndElement ();
  536. Assert.AreEqual ("<foo /><bar baz='' />", Output);
  537. }
  538. [Test]
  539. public void FullEndElement ()
  540. {
  541. xw.WriteStartElement ("foo");
  542. xw.WriteFullEndElement ();
  543. Assert.AreEqual ("<foo></foo>", Output);
  544. xw.WriteStartElement ("bar");
  545. xw.WriteAttributeString ("foo", "bar");
  546. xw.WriteFullEndElement ();
  547. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", Output);
  548. xw.WriteStartElement ("baz");
  549. xw.WriteStartAttribute ("bar", null);
  550. xw.WriteFullEndElement ();
  551. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", Output);
  552. }
  553. [Test]
  554. public void WriteQualifiedName ()
  555. {
  556. xw.WriteStartElement (null, "test", null);
  557. xw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  558. xw.WriteQualifiedName ("bob", "http://localhost/");
  559. xw.WriteEndElement ();
  560. Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", Output);
  561. }
  562. [Test]
  563. public void WriteQualifiedNameNonDeclaredAttribute ()
  564. {
  565. xw.WriteStartElement ("foo");
  566. xw.WriteStartAttribute ("a", "");
  567. xw.WriteQualifiedName ("attr", "urn:a");
  568. xw.WriteWhitespace (" ");
  569. xw.WriteQualifiedName ("attr", "urn:b");
  570. xw.WriteEndAttribute ();
  571. xw.WriteEndElement ();
  572. string xml = sw.ToString ();
  573. Assert.IsTrue (
  574. xml.IndexOf ("<foo ") >= 0,
  575. "foo");
  576. Assert.IsTrue (
  577. xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0,
  578. "qnames");
  579. Assert.IsTrue (
  580. xml.IndexOf (" xmlns:d1p1='urn:a'") > 0,
  581. "xmlns:a");
  582. Assert.IsTrue (
  583. xml.IndexOf (" xmlns:d1p2='urn:b'") > 0,
  584. "xmlns:b");
  585. }
  586. [Test]
  587. [ExpectedException (typeof (ArgumentException))]
  588. public void WriteQualifiedNameNonDeclaredContent ()
  589. {
  590. xw.WriteStartElement ("foo");
  591. xw.WriteQualifiedName ("abc", "urn:abc");
  592. }
  593. [Test]
  594. [ExpectedException (typeof (ArgumentException))]
  595. public void WriteQualifiedNameNonNCName ()
  596. {
  597. xw.WriteStartElement ("foo");
  598. xw.WriteAttributeString ("xmlns", "urn:default");
  599. xw.WriteStartElement ("child");
  600. xw.WriteStartAttribute ("a", "");
  601. xw.WriteQualifiedName ("x:def", "urn:def");
  602. }
  603. [Test]
  604. public void WriteRaw ()
  605. {
  606. xw.WriteRaw("&<>\"'");
  607. Assert.AreEqual ("&<>\"'", Output);
  608. xw.WriteRaw(null);
  609. Assert.AreEqual ("&<>\"'", Output);
  610. xw.WriteRaw("");
  611. Assert.AreEqual ("&<>\"'", Output);
  612. }
  613. [Test]
  614. public void WriteRawInvalidInAttribute ()
  615. {
  616. xw.WriteStartElement ("foo");
  617. xw.WriteStartAttribute ("bar", null);
  618. xw.WriteRaw ("&<>\"'");
  619. xw.WriteEndAttribute ();
  620. xw.WriteEndElement ();
  621. Assert.AreEqual ("<foo bar='&<>\"'' />", Output);
  622. }
  623. [Test]
  624. public void WriteStateTest ()
  625. {
  626. Assert.AreEqual (WriteState.Start, xw.WriteState);
  627. xw.WriteStartDocument ();
  628. Assert.AreEqual (WriteState.Prolog, xw.WriteState);
  629. xw.WriteStartElement ("root");
  630. Assert.AreEqual (WriteState.Element, xw.WriteState);
  631. xw.WriteElementString ("foo", "bar");
  632. Assert.AreEqual (WriteState.Content, xw.WriteState);
  633. xw.Close ();
  634. Assert.AreEqual (WriteState.Closed, xw.WriteState);
  635. }
  636. [Test]
  637. public void WriteString ()
  638. {
  639. xw.WriteStartDocument ();
  640. try {
  641. xw.WriteString("foo");
  642. } catch (InvalidOperationException) {}
  643. }
  644. [Test]
  645. public void WriteString2 ()
  646. {
  647. xw.WriteStartDocument ();
  648. // Testing attribute values
  649. xw.WriteStartElement ("foo");
  650. xw.WriteAttributeString ("bar", "&<>");
  651. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", Output);
  652. }
  653. [Test]
  654. public void WriteStringWithEntities()
  655. {
  656. // Testing element values
  657. xw.WriteElementString ("foo", "&<>\"'");
  658. Assert.AreEqual ("<foo>&amp;&lt;&gt;\"'</foo>", Output);
  659. }
  660. [Test]
  661. public void XmlLang ()
  662. {
  663. Assert.IsNull (xw.XmlLang);
  664. xw.WriteStartElement ("foo");
  665. xw.WriteAttributeString ("xml", "lang", null, "langfoo");
  666. Assert.AreEqual ("langfoo", xw.XmlLang);
  667. Assert.AreEqual ("<foo xml:lang='langfoo'", Output);
  668. xw.WriteAttributeString ("boo", "yah");
  669. Assert.AreEqual ("langfoo", xw.XmlLang);
  670. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", Output);
  671. xw.WriteElementString("bar", "baz");
  672. Assert.AreEqual ("langfoo", xw.XmlLang);
  673. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", Output);
  674. xw.WriteString("baz");
  675. Assert.AreEqual ("langfoo", xw.XmlLang);
  676. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", Output);
  677. xw.WriteStartElement ("quux");
  678. xw.WriteStartAttribute ("xml", "lang", null);
  679. Assert.AreEqual ("langfoo", xw.XmlLang);
  680. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", Output);
  681. xw.WriteString("langbar");
  682. // Commented out there: it is implementation-dependent.
  683. // and incompatible between .NET 1.0 and 1.1
  684. // Assert.AreEqual ("langfoo", xw.XmlLang);
  685. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", Output);
  686. xw.WriteEndAttribute ();
  687. // Commented out there: it is implementation-dependent.
  688. // and incompatible between .NET 1.0 and 1.1
  689. // Assert.AreEqual ("langbar", xw.XmlLang);
  690. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", Output);
  691. // check if xml:lang repeats output even if same as current scope.
  692. xw.WriteStartElement ("joe");
  693. xw.WriteAttributeString ("xml", "lang", null, "langbar");
  694. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", Output);
  695. xw.WriteElementString ("quuux", "squonk");
  696. Assert.AreEqual ("langbar", xw.XmlLang);
  697. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", Output);
  698. xw.WriteEndElement ();
  699. xw.WriteEndElement ();
  700. Assert.AreEqual ("langfoo", xw.XmlLang);
  701. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", Output);
  702. xw.WriteEndElement ();
  703. Assert.IsNull (xw.XmlLang);
  704. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", Output);
  705. xw.Close ();
  706. Assert.IsNull (xw.XmlLang);
  707. }
  708. // TODO: test operational aspects
  709. [Test]
  710. public void XmlSpaceTest ()
  711. {
  712. xw.WriteStartElement ("foo");
  713. Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
  714. xw.WriteStartElement ("bar");
  715. xw.WriteAttributeString ("xml", "space", null, "preserve");
  716. Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  717. Assert.AreEqual ("<foo><bar xml:space='preserve'", Output);
  718. xw.WriteStartElement ("baz");
  719. xw.WriteAttributeString ("xml", "space", null, "preserve");
  720. Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  721. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", Output);
  722. xw.WriteStartElement ("quux");
  723. xw.WriteStartAttribute ("xml", "space", null);
  724. Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  725. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", Output);
  726. // Commented out there: it is implementation-dependent
  727. // and incompatible between .NET 1.0 and 1.1
  728. xw.WriteString ("default");
  729. // Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  730. // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", Output);
  731. xw.WriteEndAttribute ();
  732. Assert.AreEqual (XmlSpace.Default, xw.XmlSpace);
  733. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", Output);
  734. xw.WriteEndElement ();
  735. Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  736. xw.WriteEndElement ();
  737. Assert.AreEqual (XmlSpace.Preserve, xw.XmlSpace);
  738. xw.WriteEndElement ();
  739. Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
  740. xw.WriteStartElement ("quux");
  741. }
  742. [Test]
  743. [ExpectedException (typeof (ArgumentException))]
  744. public void XmlSpaceTestInvalidValue1 ()
  745. {
  746. xw.WriteStartElement ("foo");
  747. xw.WriteAttributeString ("xml", "space", null, "bubba");
  748. }
  749. [Test]
  750. [ExpectedException (typeof (ArgumentException))]
  751. public void XmlSpaceTestInvalidValue2 ()
  752. {
  753. xw.WriteStartElement ("foo");
  754. xw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  755. }
  756. [Test]
  757. [ExpectedException (typeof (ArgumentException))]
  758. public void XmlSpaceTestInvalidValue3 ()
  759. {
  760. xw.WriteStartElement ("foo");
  761. xw.WriteAttributeString ("xml", "space", null, "Default");
  762. }
  763. [Test]
  764. [ExpectedException (typeof (ArgumentException))]
  765. public void XmlSpaceTestInvalidValue4 ()
  766. {
  767. xw.WriteStartElement ("foo");
  768. xw.WriteAttributeString ("xml", "space", null, "bubba");
  769. }
  770. [Test]
  771. [ExpectedException (typeof (ArgumentException))]
  772. public void WriteWhitespaceNonWhitespace ()
  773. {
  774. xw.WriteWhitespace ("x");
  775. }
  776. [Test]
  777. public void XmlSpaceRaw ()
  778. {
  779. xw.WriteStartElement ("foo");
  780. xw.WriteStartAttribute ("xml", "space", null);
  781. Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
  782. Assert.AreEqual ("<foo xml:space='", Output);
  783. xw.WriteString ("default");
  784. // Commented out there: it is implementation-dependent
  785. // and incompatible between .NET 1.0 and 1.1
  786. // Assert.AreEqual (XmlSpace.None, xw.XmlSpace);
  787. // Assert.AreEqual ("<foo xml:space='", Output);
  788. xw.WriteEndAttribute ();
  789. Assert.AreEqual (XmlSpace.Default, xw.XmlSpace);
  790. Assert.AreEqual ("<foo xml:space='default'", Output);
  791. }
  792. [Test]
  793. public void WriteAttributes ()
  794. {
  795. XmlDocument doc = new XmlDocument();
  796. StringWriter sw = new StringWriter();
  797. XmlWriter wr = new XmlTextWriter(sw);
  798. StringBuilder sb = sw.GetStringBuilder();
  799. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  800. 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);
  801. xtr.Read(); // read XMLDecl
  802. wr.WriteAttributes(xtr, false);
  803. // This method don't always have to take this double-quoted style...
  804. Assert.AreEqual("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim(), "#WriteAttributes.XmlDecl.1");
  805. sb.Remove(0, sb.Length); // init
  806. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  807. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  808. xtr.Read(); // read XMLDecl
  809. Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
  810. sw = new StringWriter ();
  811. wr = new XmlTextWriter (sw);
  812. // This block raises an error on MS.NET 1.0.
  813. wr.WriteAttributes(xtr, false);
  814. // This method don't always have to take this double-quoted style...
  815. Assert.AreEqual("version=\"1.0\" standalone=\"no\"", sw.ToString().Trim(), "#WriteAttributes.XmlDecl.2");
  816. sw = new StringWriter ();
  817. wr = new XmlTextWriter (sw);
  818. sb.Remove(0, sb.Length); // init
  819. xtr.Read(); // read root
  820. Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
  821. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  822. wr.WriteAttributes(xtr, false);
  823. wr.WriteEndElement();
  824. wr.Close();
  825. // This method don't always have to take this double-quoted style...
  826. Assert.AreEqual("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim(), "#WriteAttributes.Element");
  827. xtr.Close ();
  828. }
  829. [Test]
  830. public void WriteWhitespace ()
  831. {
  832. xw.WriteStartElement ("a");
  833. xw.WriteWhitespace ("\n\t");
  834. xw.WriteStartElement ("b");
  835. xw.WriteWhitespace ("\n\t");
  836. xw.WriteEndElement ();
  837. xw.WriteWhitespace ("\n");
  838. xw.WriteEndElement ();
  839. xw.WriteWhitespace ("\n");
  840. xw.Flush ();
  841. Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", Output);
  842. }
  843. [Test]
  844. public void FlushDoesntCloseTag ()
  845. {
  846. xw.WriteStartElement ("foo");
  847. xw.WriteAttributeString ("bar", "baz");
  848. xw.Flush ();
  849. Assert.AreEqual ("<foo bar='baz'", Output);
  850. }
  851. [Test]
  852. public void WriteWhitespaceClosesTag ()
  853. {
  854. xw.WriteStartElement ("foo");
  855. xw.WriteAttributeString ("bar", "baz");
  856. xw.WriteWhitespace (" ");
  857. Assert.AreEqual ("<foo bar='baz'> ", Output);
  858. }
  859. [Test]
  860. public void DontOutputMultipleXmlns ()
  861. {
  862. XmlDocument doc = new XmlDocument();
  863. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  864. XmlDocument doc2 = new XmlDocument();
  865. doc2.LoadXml(doc.InnerXml);
  866. Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  867. doc2.OuterXml);
  868. }
  869. [Test]
  870. public void DontOutputNonDeclaredXmlns ()
  871. {
  872. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  873. XmlDocument doc = new XmlDocument();
  874. doc.LoadXml(xml);
  875. XmlDocument doc2 = new XmlDocument();
  876. doc2.LoadXml(doc.InnerXml);
  877. Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
  878. }
  879. [Test]
  880. public void DontOutputRemovalDefaultNSDeclaration ()
  881. {
  882. xw.WriteStartDocument ();
  883. xw.WriteStartElement ("foo");
  884. xw.WriteAttributeString ("xmlns", "probe");
  885. Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe"));
  886. xw.WriteStartElement ("b");
  887. Assert.AreEqual (String.Empty, xw.LookupPrefix ("probe"));
  888. xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  889. xw.WriteEndElement (); // b2
  890. xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  891. xw.WriteEndElement (); // b2
  892. xw.WriteEndElement (); // b
  893. xw.WriteEndElement (); // foo
  894. xw.WriteEndDocument ();
  895. xw.Close ();
  896. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", Output);
  897. }
  898. [Test]
  899. public void DontOutputRemovalDefaultNSDeclaration2 ()
  900. {
  901. xw.WriteStartDocument ();
  902. // IMPORTANT DIFFERENCE!! ns = "", not null
  903. xw.WriteStartElement ("foo", "");
  904. xw.WriteAttributeString ("xmlns", "probe");
  905. Assert.IsNull (xw.LookupPrefix ("probe"));
  906. xw.WriteStartElement ("b");
  907. Assert.IsNull (xw.LookupPrefix ("probe"));
  908. xw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  909. xw.WriteEndElement (); // b2
  910. xw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  911. xw.WriteEndElement (); // b2
  912. xw.WriteEndElement (); // b
  913. xw.WriteEndElement (); // foo
  914. xw.WriteEndDocument ();
  915. xw.Close ();
  916. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", Output);
  917. }
  918. [Test]
  919. public void DoOutputRemovalDefaultNSDeclaration ()
  920. {
  921. xw.WriteStartElement ("docelem", "a-namespace");
  922. XmlDocument doc = new XmlDocument ();
  923. doc.CreateElement ("hola").WriteTo (xw);
  924. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  925. xw.WriteEndElement ();
  926. xw.Close ();
  927. Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", Output);
  928. }
  929. [Test]
  930. public void WriteAttributeTakePrecedenceOnXmlns ()
  931. {
  932. xw.WriteStartElement ("root", "urn:foo");
  933. xw.WriteAttributeString ("xmlns", "urn:bar");
  934. xw.WriteEndElement ();
  935. xw.Close ();
  936. Assert.AreEqual ("<root xmlns='urn:bar' />", Output);
  937. }
  938. [Test]
  939. [ExpectedException (typeof (ArgumentException))]
  940. public void LookupPrefixNull ()
  941. {
  942. xw.LookupPrefix (null);
  943. }
  944. [Test]
  945. [ExpectedException (typeof (ArgumentException))]
  946. public void LookupPrefixEmpty ()
  947. {
  948. xw.LookupPrefix (String.Empty);
  949. }
  950. [Test]
  951. public void LookupPrefixIgnoresXmlnsAttribute ()
  952. {
  953. Assert.IsNull (xw.LookupPrefix ("urn:foo"));
  954. xw.WriteStartElement ("root");
  955. Assert.IsNull (xw.LookupPrefix ("urn:foo"));
  956. xw.WriteAttributeString ("xmlns", "urn:foo");
  957. // Surprisingly to say, it is ignored!!
  958. Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
  959. xw.WriteStartElement ("hoge");
  960. // (still after flushing previous start element.)
  961. Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
  962. xw.WriteStartElement ("fuga", "urn:foo");
  963. // Is this testing on the correct way? Yes, here it is.
  964. Assert.AreEqual (String.Empty, xw.LookupPrefix ("urn:foo"));
  965. }
  966. [Test]
  967. public void WriteInvalidNames ()
  968. {
  969. xw.WriteStartElement ("foo<>");
  970. xw.WriteAttributeString ("ho<>ge", "value");
  971. }
  972. [Test]
  973. [ExpectedException (typeof (ArgumentException))]
  974. public void AttributeWriteStartAttributePrefixWithoutNS ()
  975. {
  976. xw.WriteStartAttribute ("some", "foo", null);
  977. }
  978. [Test]
  979. public void AttributeWriteStartAttributeXmlnsNullNS ()
  980. {
  981. xw.WriteStartAttribute ("xmlns", "foo", null);
  982. }
  983. [Test]
  984. [ExpectedException (typeof (ArgumentException))]
  985. public void AttributeWriteEndAttributeXmlnsNullNs ()
  986. {
  987. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  988. xw.WriteStartAttribute ("xmlns", "foo", null);
  989. xw.WriteEndAttribute ();
  990. }
  991. [Test]
  992. [ExpectedException (typeof (ArgumentException))]
  993. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  994. {
  995. xw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  996. }
  997. [Test]
  998. [ExpectedException (typeof (ArgumentException))]
  999. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1000. {
  1001. xw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1002. }
  1003. [Test]
  1004. public void WriteRawProceedToProlog ()
  1005. {
  1006. XmlTextWriter xw = new XmlTextWriter (new StringWriter ());
  1007. xw.WriteRaw ("");
  1008. Assert.AreEqual (WriteState.Prolog, xw.WriteState);
  1009. }
  1010. [Test]
  1011. public void CloseTwice ()
  1012. {
  1013. StringWriter sw = new StringWriter ();
  1014. XmlTextWriter writer = new XmlTextWriter (sw);
  1015. writer.Close ();
  1016. // should not result in an exception
  1017. writer.Close ();
  1018. }
  1019. [Test]
  1020. public void WriteRawWriteString ()
  1021. {
  1022. // WriteRaw () -> WriteString ().
  1023. xw.WriteRaw ("");
  1024. xw.WriteString ("foo");
  1025. Assert.AreEqual (WriteState.Content, xw.WriteState);
  1026. }
  1027. [Test]
  1028. public void LookupOverridenPrefix ()
  1029. {
  1030. xw.WriteStartElement ("out");
  1031. xw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1032. xw.WriteStartElement ("baz", "foo", "abc");
  1033. Assert.IsNull (xw.LookupPrefix ("xyz"));
  1034. }
  1035. [Test]
  1036. public void DuplicatingNamespaceMappingInAttributes ()
  1037. {
  1038. xw.WriteStartElement ("out");
  1039. xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1040. xw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1041. xw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1042. xw.WriteStartElement ("out");
  1043. xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1044. xw.WriteStartElement ("out");
  1045. xw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1046. xw.WriteEndElement ();
  1047. xw.WriteEndElement ();
  1048. xw.WriteEndElement ();
  1049. string xml = sw.ToString ();
  1050. Assert.IsTrue (
  1051. xml.IndexOf ("p:foo='xyz'") > 0,
  1052. "p:foo");
  1053. Assert.IsTrue (
  1054. xml.IndexOf ("d1p1:bar='xyz'") > 0,
  1055. "d1p1:bar");
  1056. Assert.IsTrue (
  1057. xml.IndexOf ("d1p2:baz='xyz'") > 0,
  1058. "d1p1:baz");
  1059. Assert.IsTrue (
  1060. xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0,
  1061. "xmlns:d1p2");
  1062. Assert.IsTrue (
  1063. xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0,
  1064. "xmlns:d1p1");
  1065. Assert.IsTrue (
  1066. xml.IndexOf ("xmlns:p='urn:foo'") > 0,
  1067. "xmlns:p");
  1068. Assert.IsTrue (
  1069. xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0,
  1070. "remaining");
  1071. }
  1072. [Test]
  1073. public void WriteXmlSpaceIgnoresNS ()
  1074. {
  1075. xw.WriteStartElement ("root");
  1076. xw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1077. xw.WriteEndElement ();
  1078. Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
  1079. }
  1080. [Test] // bug #75546
  1081. public void WriteEmptyNSQNameInAttribute ()
  1082. {
  1083. XmlTextWriter xw = new XmlTextWriter (TextWriter.Null);
  1084. xw.WriteStartElement ("foo", "urn:goo");
  1085. xw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1086. xw.WriteStartAttribute ("foo", "");
  1087. xw.WriteQualifiedName ("n1", "urn:bar");
  1088. xw.WriteEndAttribute ();
  1089. xw.WriteStartAttribute ("foo", "");
  1090. xw.WriteQualifiedName ("n2", "");
  1091. xw.WriteEndAttribute ();
  1092. }
  1093. [Test] // bug #76095
  1094. public void SurrogatePairsInWriteString ()
  1095. {
  1096. MemoryStream ms = new MemoryStream ();
  1097. XmlWriter writer = new XmlTextWriter(ms, null);
  1098. writer.WriteElementString("a", "\ud800\udf39");
  1099. writer.Close();
  1100. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1101. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1102. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1103. }
  1104. [Test]
  1105. [ExpectedException (typeof (InvalidOperationException))]
  1106. public void RejectWritingAtErrorState ()
  1107. {
  1108. try {
  1109. xw.WriteEndElement ();
  1110. } catch (Exception) {
  1111. }
  1112. xw.WriteStartElement ("foo");
  1113. }
  1114. #endregion
  1115. [Test]
  1116. public void WriteBooleanArray ()
  1117. {
  1118. bool [] array = new bool [] {true, false, true, true, false};
  1119. xw.WriteArray ("", "root", "", array, 1, 3);
  1120. Assert.AreEqual ("<root>false</root><root>true</root><root>true</root>", Output, "#1");
  1121. }
  1122. }
  1123. }