XmlSimpleDictionaryWriterTest.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. [Test]
  1123. public void WriteNode ()
  1124. {
  1125. string s = @"<Resolve xmlns='http://schemas.microsoft.com/net/2006/05/peer' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><ClientId>79310c9f-18d4-4337-a95a-1865ca54a66e</ClientId><MaxAddresses>3</MaxAddresses><MeshId>amesh</MeshId></Resolve>".Replace ('\'', '"');
  1126. var sw = new StringWriter ();
  1127. var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, new XmlWriterSettings () { OmitXmlDeclaration = true }));
  1128. var xr = XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (s)));
  1129. xr.MoveToContent ();
  1130. while (!xr.EOF && xr.NodeType != XmlNodeType.EndElement)
  1131. xw.WriteNode (xr, false);
  1132. xw.Flush ();
  1133. Assert.AreEqual (s, sw.ToString ());
  1134. }
  1135. }
  1136. }