XmlTextWriterTests.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. //
  2. // System.Xml.XmlTextWriterTests
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Martin Willemoes Hansen <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. using System;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Text;
  15. using System.Xml;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Xml
  18. {
  19. [TestFixture]
  20. public class XmlTextWriterTests
  21. {
  22. StringWriter sw;
  23. XmlTextWriter xtw;
  24. [SetUp]
  25. public void GetReady ()
  26. {
  27. sw = new StringWriter ();
  28. CreateXmlTextWriter ();
  29. }
  30. private void CreateXmlTextWriter ()
  31. {
  32. xtw = new XmlTextWriter (sw);
  33. xtw.QuoteChar = '\'';
  34. }
  35. private string StringWriterText
  36. {
  37. get { return sw.GetStringBuilder ().ToString (); }
  38. }
  39. [Test]
  40. public void AttributeNamespacesNonNamespaceAttributeBefore ()
  41. {
  42. xtw.WriteStartElement ("foo");
  43. xtw.WriteAttributeString("bar", "baz");
  44. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  45. Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
  46. }
  47. [Test]
  48. public void AttributeNamespacesNonNamespaceAttributeAfter ()
  49. {
  50. xtw.WriteStartElement ("foo");
  51. xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
  52. xtw.WriteAttributeString("bar", "baz");
  53. Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
  54. }
  55. [Test]
  56. public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
  57. {
  58. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  59. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText);
  60. }
  61. [Test]
  62. public void XmlNs_Valid ()
  63. {
  64. xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
  65. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#1");
  66. sw.GetStringBuilder ().Length = 0;
  67. CreateXmlTextWriter ();
  68. xtw.WriteAttributeString ("xmlns", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  69. Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#2");
  70. }
  71. [Test]
  72. [Category ("NotWorking")] // bug #77083
  73. public void XmlNs_Valid2 ()
  74. {
  75. xtw.WriteAttributeString (null, "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  76. Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#3");
  77. sw.GetStringBuilder ().Length = 0;
  78. CreateXmlTextWriter ();
  79. xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  80. Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#4");
  81. sw.GetStringBuilder ().Length = 0;
  82. CreateXmlTextWriter ();
  83. xtw.WriteStartElement ("person");
  84. xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
  85. xtw.WriteEndElement ();
  86. Assert.AreEqual ("<person xmlns:test='http://abc.def' />", StringWriterText, "#5");
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentException))]
  90. public void XmlNs_Invalid1 ()
  91. {
  92. // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
  93. xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentException))]
  97. public void XmlNs_Invalid2 ()
  98. {
  99. // The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
  100. xtw.WriteAttributeString (null, "xmlns", "http://somenamespace.com", "http://abc.def");
  101. }
  102. [Test]
  103. [Category ("NotWorking")]
  104. public void XmlSpace_Valid () // bug #77084
  105. {
  106. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  107. Assert.AreEqual ("xml:space='preserve'", StringWriterText, "#1");
  108. sw.GetStringBuilder ().Length = 0;
  109. CreateXmlTextWriter ();
  110. xtw.WriteAttributeString ("xml", "space", "whatever", "default");
  111. Assert.AreEqual ("xml:space='default'", StringWriterText, "#2");
  112. sw.GetStringBuilder ().Length = 0;
  113. CreateXmlTextWriter ();
  114. xtw.WriteStartElement ("person");
  115. xtw.WriteAttributeString ("xml", "space", "whatever", "default");
  116. xtw.WriteEndElement ();
  117. Assert.AreEqual ("<person xml:space='default' />", StringWriterText, "#3");
  118. }
  119. [Test]
  120. public void XmlPrefix_ValidMS ()
  121. {
  122. xtw.WriteAttributeString ("xml", "something", "whatever", "default");
  123. Assert.AreEqual ("xml:something='default'", StringWriterText, "#1");
  124. sw.GetStringBuilder ().Length = 0;
  125. CreateXmlTextWriter ();
  126. xtw.WriteAttributeString ("xml", "else", null, "whatever");
  127. Assert.AreEqual ("xml:else='whatever'", StringWriterText, "#2");
  128. sw.GetStringBuilder ().Length = 0;
  129. CreateXmlTextWriter ();
  130. xtw.WriteStartElement ("person");
  131. xtw.WriteAttributeString ("xml", "something", "whatever", "default");
  132. xtw.WriteAttributeString ("xml", "else", null, "whatever");
  133. xtw.WriteEndElement ();
  134. Assert.AreEqual ("<person xml:something='default' xml:else='whatever' />",
  135. StringWriterText, "#3");
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentException))]
  139. public void XmlSpace_Invalid ()
  140. {
  141. // only preserve and default are valid values for xml:space
  142. xtw.WriteAttributeString ("xml", "space", null, "something");
  143. }
  144. [Test]
  145. public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
  146. {
  147. xtw.WriteAttributeString ("a", "http://somenamespace.com", "http://abc.def");
  148. Assert.AreEqual ("d0p1:a='http://abc.def'", StringWriterText, "#1");
  149. }
  150. [Test]
  151. [ExpectedException (typeof (ArgumentException))]
  152. public void AttributeNamespacesWithNullInNamespaceParam ()
  153. {
  154. // you cannot use prefix with an empty namespace
  155. xtw.WriteAttributeString ("a", "abc", null, "http://abc.def");
  156. }
  157. [Test]
  158. public void AttributeNamespacesWithTextInNamespaceParam ()
  159. {
  160. xtw.WriteAttributeString ("a", "abc", "http://somenamespace.com", "http://abc.def");
  161. Assert.AreEqual ("a:abc='http://abc.def'", StringWriterText, "#1");
  162. sw.GetStringBuilder ().Length = 0;
  163. CreateXmlTextWriter ();
  164. xtw.WriteAttributeString ("", "abc", "http://somenamespace.com", "http://abc.def");
  165. Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#2");
  166. sw.GetStringBuilder ().Length = 0;
  167. CreateXmlTextWriter ();
  168. xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
  169. Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#3");
  170. }
  171. [Test]
  172. [Category ("NotWorking")] // bug #77086, #77087 and #77088
  173. public void AutoCreatePrefixes ()
  174. {
  175. xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
  176. xtw.WriteAttributeString (null, "def", "http://somenamespace.com", "http://def.ghi");
  177. xtw.WriteAttributeString (null, "ghi", "http://othernamespace.com", "http://ghi.jkl");
  178. #if NET_2_0
  179. Assert.AreEqual ("d0p1:abc='http://abc.def' d0p1:def='http://def.ghi'" +
  180. " d0p2:ghi='http://ghi.jkl'", StringWriterText, "#1");
  181. #else
  182. // on 1.x a new prefix is always created when level is 0 ?
  183. Assert.AreEqual ("d0p1:abc='http://abc.def' d0p2:def='http://def.ghi'" +
  184. " d0p3:ghi='http://ghi.jkl'", StringWriterText, "#1");
  185. #endif
  186. sw.GetStringBuilder ().Length = 0;
  187. CreateXmlTextWriter ();
  188. xtw.WriteStartElement ("person");
  189. xtw.WriteAttributeString (null, "name", "http://somenamespace.com", "Gates");
  190. xtw.WriteAttributeString (null, "initials", "http://othernamespace.com", "BG");
  191. xtw.WriteAttributeString (null, "firstName", "http://somenamespace.com", "Bill");
  192. xtw.WriteStartElement ("address");
  193. xtw.WriteAttributeString (null, "street", "http://somenamespace.com", "Campus");
  194. xtw.WriteAttributeString (null, "number", "http://othernamespace.com", "1");
  195. xtw.WriteAttributeString (null, "zip", "http://newnamespace.com", "3000");
  196. xtw.WriteAttributeString (null, "box", "http://othernamespace.com", "a");
  197. xtw.WriteEndElement ();
  198. xtw.WriteEndElement ();
  199. Assert.AreEqual (
  200. "<person" +
  201. " d1p1:name='Gates'" +
  202. " d1p2:initials='BG'" +
  203. " d1p1:firstName='Bill'" +
  204. " xmlns:d1p2='http://othernamespace.com'" +
  205. " xmlns:d1p1='http://somenamespace.com'>" +
  206. "<address" +
  207. " d1p1:street='Campus'" +
  208. " d1p2:number='1'" +
  209. " d2p1:zip='3000'" +
  210. " d1p2:box='a'" +
  211. " xmlns:d2p1='http://newnamespace.com' />" +
  212. "</person>", StringWriterText, "#2");
  213. }
  214. [Test]
  215. [Category ("NotDotNet")]
  216. public void AttributeNamespacesXmlnsXmlns ()
  217. {
  218. xtw.WriteStartElement ("foo");
  219. try {
  220. xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
  221. // This should not be allowed, even though MS.NET doesn't treat as an error.
  222. // See http://www.w3.org/TR/REC-xml-names/ Namespace Constraint: Prefix Declared
  223. Assert.Fail ("any prefix which name starts from \"xml\" must not be allowed.");
  224. } catch (ArgumentException) {}
  225. xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
  226. }
  227. [Test]
  228. public void WriteAttributeString_EmptyLocalName ()
  229. {
  230. xtw.WriteAttributeString ("", "something");
  231. Assert.AreEqual ("='something'", StringWriterText, "#1");
  232. sw.GetStringBuilder ().Length = 0;
  233. CreateXmlTextWriter ();
  234. xtw.WriteAttributeString ("", "", "something");
  235. Assert.AreEqual ("='something'", StringWriterText, "#2");
  236. sw.GetStringBuilder ().Length = 0;
  237. CreateXmlTextWriter ();
  238. xtw.WriteAttributeString ("", "http://somenamespace.com", "something");
  239. Assert.AreEqual ("d0p1:='something'", StringWriterText, "#3");
  240. sw.GetStringBuilder ().Length = 0;
  241. CreateXmlTextWriter ();
  242. xtw.WriteAttributeString ("x", "", "http://somenamespace.com", "something");
  243. Assert.AreEqual ("x:='something'", StringWriterText, "#4");
  244. sw.GetStringBuilder ().Length = 0;
  245. CreateXmlTextWriter ();
  246. xtw.WriteAttributeString (null, "something");
  247. Assert.AreEqual ("='something'", StringWriterText, "#5");
  248. sw.GetStringBuilder ().Length = 0;
  249. CreateXmlTextWriter ();
  250. xtw.WriteAttributeString (null, "", "something");
  251. Assert.AreEqual ("='something'", StringWriterText, "#6");
  252. sw.GetStringBuilder ().Length = 0;
  253. CreateXmlTextWriter ();
  254. xtw.WriteAttributeString (null, "http://somenamespace.com", "something");
  255. Assert.AreEqual ("d0p1:='something'", StringWriterText, "#7");
  256. sw.GetStringBuilder ().Length = 0;
  257. CreateXmlTextWriter ();
  258. xtw.WriteAttributeString ("x", null, "http://somenamespace.com", "something");
  259. Assert.AreEqual ("x:='something'", StringWriterText, "#8");
  260. }
  261. [Test]
  262. public void WriteStartAttribute_EmptyLocalName ()
  263. {
  264. xtw.WriteStartAttribute ("", "");
  265. Assert.AreEqual ("='", StringWriterText, "#1");
  266. sw.GetStringBuilder ().Length = 0;
  267. CreateXmlTextWriter ();
  268. xtw.WriteStartAttribute ("", "", "");
  269. Assert.AreEqual ("='", StringWriterText, "#2");
  270. sw.GetStringBuilder ().Length = 0;
  271. CreateXmlTextWriter ();
  272. xtw.WriteStartAttribute ("", "", "http://somenamespace.com");
  273. Assert.AreEqual ("d0p1:='", StringWriterText, "#3");
  274. sw.GetStringBuilder ().Length = 0;
  275. CreateXmlTextWriter ();
  276. xtw.WriteStartAttribute ("x", "", "http://somenamespace.com");
  277. Assert.AreEqual ("x:='", StringWriterText, "#4");
  278. sw.GetStringBuilder ().Length = 0;
  279. CreateXmlTextWriter ();
  280. xtw.WriteStartAttribute ("", null);
  281. Assert.AreEqual ("='", StringWriterText, "#6");
  282. sw.GetStringBuilder ().Length = 0;
  283. CreateXmlTextWriter ();
  284. xtw.WriteStartAttribute ("", null, "");
  285. Assert.AreEqual ("='", StringWriterText, "#7");
  286. sw.GetStringBuilder ().Length = 0;
  287. CreateXmlTextWriter ();
  288. xtw.WriteStartAttribute ("", null, "http://somenamespace.com");
  289. Assert.AreEqual ("d0p1:='", StringWriterText, "#8");
  290. sw.GetStringBuilder ().Length = 0;
  291. CreateXmlTextWriter ();
  292. xtw.WriteStartAttribute ("x", null, "http://somenamespace.com");
  293. Assert.AreEqual ("x:='", StringWriterText, "#9");
  294. }
  295. [Test]
  296. public void AttributeWriteAttributeString ()
  297. {
  298. xtw.WriteStartElement ("foo");
  299. xtw.WriteAttributeString ("foo", "bar");
  300. Assert.AreEqual ("<foo foo='bar'", StringWriterText);
  301. xtw.WriteAttributeString ("bar", "");
  302. Assert.AreEqual ("<foo foo='bar' bar=''", StringWriterText);
  303. xtw.WriteAttributeString ("baz", null);
  304. Assert.AreEqual ("<foo foo='bar' bar='' baz=''", StringWriterText);
  305. xtw.WriteAttributeString ("hoge", "a\nb");
  306. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b'", StringWriterText);
  307. xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
  308. Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a&#xA;b' fuga=' a\t&#xD;&#xA;b\t'", StringWriterText);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (InvalidOperationException))]
  312. public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
  313. {
  314. xtw.WriteStartElement ("foo");
  315. xtw.WriteString ("bar");
  316. xtw.WriteAttributeString ("baz", "quux");
  317. }
  318. [Test]
  319. public void AttributeWriteAttributeStringWithoutParentElement ()
  320. {
  321. xtw.WriteAttributeString ("foo", "bar");
  322. Assert.AreEqual ("foo='bar'", StringWriterText);
  323. xtw.WriteAttributeString ("baz", "quux");
  324. Assert.AreEqual ("foo='bar' baz='quux'", StringWriterText);
  325. }
  326. [Test]
  327. public void CDataValid ()
  328. {
  329. xtw.WriteCData ("foo");
  330. Assert.AreEqual ("<![CDATA[foo]]>", StringWriterText, "WriteCData had incorrect output.");
  331. }
  332. [Test]
  333. public void CDataNull ()
  334. {
  335. xtw.WriteCData (null);
  336. Assert.AreEqual ("<![CDATA[]]>", StringWriterText, "WriteCData had incorrect output.");
  337. }
  338. [Test]
  339. [ExpectedException (typeof (ArgumentException))]
  340. public void CDataInvalid ()
  341. {
  342. xtw.WriteCData("foo]]>bar");
  343. }
  344. [Test]
  345. public void CloseOpenElements ()
  346. {
  347. xtw.WriteStartElement("foo");
  348. xtw.WriteStartElement("bar");
  349. xtw.WriteStartElement("baz");
  350. xtw.Close();
  351. Assert.AreEqual ("<foo><bar><baz /></bar></foo>", StringWriterText,
  352. "Close didn't write out end elements properly.");
  353. }
  354. [Test]
  355. public void CloseWriteAfter ()
  356. {
  357. xtw.WriteElementString ("foo", "bar");
  358. xtw.Close ();
  359. // WriteEndElement and WriteStartDocument aren't tested here because
  360. // they will always throw different exceptions besides 'The Writer is closed.'
  361. // and there are already tests for those exceptions.
  362. try {
  363. xtw.WriteCData ("foo");
  364. Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
  365. } catch (InvalidOperationException) {
  366. // Don't rely on English message assertion.
  367. // It is enough to check an exception occurs.
  368. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  369. }
  370. try {
  371. xtw.WriteComment ("foo");
  372. Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
  373. } catch (InvalidOperationException) {
  374. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  375. }
  376. try {
  377. xtw.WriteProcessingInstruction ("foo", "bar");
  378. Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
  379. } catch (InvalidOperationException) {
  380. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  381. }
  382. try {
  383. xtw.WriteStartElement ("foo", "bar", "baz");
  384. Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
  385. } catch (InvalidOperationException) {
  386. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  387. }
  388. try {
  389. xtw.WriteAttributeString ("foo", "bar");
  390. Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
  391. } catch (InvalidOperationException) {
  392. // Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
  393. }
  394. try {
  395. xtw.WriteString ("foo");
  396. Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
  397. } catch (InvalidOperationException) {
  398. // Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
  399. }
  400. }
  401. [Test]
  402. public void CommentValid ()
  403. {
  404. xtw.WriteComment ("foo");
  405. Assert.AreEqual ("<!--foo-->", StringWriterText, "WriteComment had incorrect output.");
  406. }
  407. [Test]
  408. public void CommentInvalid ()
  409. {
  410. try {
  411. xtw.WriteComment("foo-");
  412. Assert.Fail("Should have thrown an ArgumentException.");
  413. } catch (ArgumentException) { }
  414. try {
  415. xtw.WriteComment("foo-->bar");
  416. Assert.Fail("Should have thrown an ArgumentException.");
  417. } catch (ArgumentException) { }
  418. }
  419. [Test]
  420. public void ConstructorsAndBaseStream ()
  421. {
  422. Assert.IsTrue (Object.ReferenceEquals (null, this.xtw.BaseStream), "BaseStream property returned wrong value.");
  423. MemoryStream ms;
  424. StreamReader sr;
  425. XmlTextWriter xtw;
  426. ms = new MemoryStream ();
  427. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  428. xtw.WriteStartDocument ();
  429. xtw.Flush ();
  430. ms.Seek (0, SeekOrigin.Begin);
  431. sr = new StreamReader (ms, Encoding.Unicode);
  432. string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  433. string actualXmlDeclaration = sr.ReadToEnd();
  434. Assert.AreEqual (expectedXmlDeclaration, actualXmlDeclaration);
  435. Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
  436. ms = new MemoryStream ();
  437. xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
  438. xtw.WriteStartDocument (true);
  439. xtw.Flush ();
  440. ms.Seek (0, SeekOrigin.Begin);
  441. sr = new StreamReader (ms, Encoding.Unicode);
  442. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
  443. ms = new MemoryStream ();
  444. xtw = new XmlTextWriter (ms, new UTF8Encoding ());
  445. xtw.WriteStartDocument ();
  446. xtw.Flush ();
  447. ms.Seek (0, SeekOrigin.Begin);
  448. sr = new StreamReader (ms, Encoding.UTF8);
  449. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
  450. ms = new MemoryStream ();
  451. xtw = new XmlTextWriter (ms, null);
  452. xtw.WriteStartDocument ();
  453. xtw.Flush ();
  454. ms.Seek (0, SeekOrigin.Begin);
  455. sr = new StreamReader (ms, Encoding.UTF8);
  456. Assert.AreEqual ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
  457. ms = new MemoryStream ();
  458. xtw = new XmlTextWriter (ms, null);
  459. xtw.WriteStartDocument (true);
  460. xtw.Flush ();
  461. ms.Seek (0, SeekOrigin.Begin);
  462. sr = new StreamReader (ms, Encoding.UTF8);
  463. Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
  464. Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
  465. }
  466. [Test]
  467. public void DocumentStart ()
  468. {
  469. xtw.WriteStartDocument ();
  470. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", StringWriterText,
  471. "XmlDeclaration is incorrect.");
  472. try {
  473. xtw.WriteStartDocument ();
  474. Assert.Fail("Should have thrown an InvalidOperationException.");
  475. } catch (InvalidOperationException) {
  476. // Don't rely on English message assertion.
  477. // It is enough to check an exception occurs.
  478. // Assert.AreEqual ("WriteStartDocument should be the first call.", e.Message, "Exception message is incorrect.");
  479. }
  480. xtw = new XmlTextWriter (sw = new StringWriter ());
  481. xtw.QuoteChar = '\'';
  482. xtw.WriteStartDocument (true);
  483. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
  484. xtw = new XmlTextWriter (sw = new StringWriter ());
  485. xtw.QuoteChar = '\'';
  486. xtw.WriteStartDocument (false);
  487. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
  488. }
  489. [Test]
  490. public void ElementAndAttributeSameXmlns ()
  491. {
  492. xtw.WriteStartElement ("ped", "foo", "urn:foo");
  493. xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
  494. xtw.WriteEndElement ();
  495. Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
  496. }
  497. [Test]
  498. [Category ("NotDotNet")]
  499. public void ElementXmlnsNeedEscape ()
  500. {
  501. xtw.WriteStartElement ("test", "foo", "'");
  502. xtw.WriteEndElement ();
  503. // MS.NET output is : xmlns:test='''
  504. Assert.AreEqual ("<test:foo xmlns:test='&apos;' />", StringWriterText);
  505. }
  506. [Test]
  507. public void ElementEmpty ()
  508. {
  509. xtw.WriteStartElement ("foo");
  510. xtw.WriteEndElement ();
  511. Assert.AreEqual ("<foo />", StringWriterText, "Incorrect output.");
  512. }
  513. [Test]
  514. public void ElementWriteElementString ()
  515. {
  516. xtw.WriteElementString ("foo", "bar");
  517. Assert.AreEqual ("<foo>bar</foo>", StringWriterText, "WriteElementString has incorrect output.");
  518. xtw.WriteElementString ("baz", "");
  519. Assert.AreEqual ("<foo>bar</foo><baz />", StringWriterText);
  520. xtw.WriteElementString ("quux", null);
  521. Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText);
  522. xtw.WriteElementString ("", "quuux");
  523. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText);
  524. xtw.WriteElementString (null, "quuuux");
  525. Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText);
  526. }
  527. [Test]
  528. public void FormattingTest ()
  529. {
  530. xtw.Formatting = Formatting.Indented;
  531. xtw.WriteStartDocument ();
  532. xtw.WriteStartElement ("foo");
  533. xtw.WriteElementString ("bar", "");
  534. xtw.Close ();
  535. Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
  536. }
  537. [Test]
  538. public void FormattingInvalidXmlForFun ()
  539. {
  540. xtw.Formatting = Formatting.Indented;
  541. xtw.IndentChar = 'x';
  542. xtw.WriteStartDocument ();
  543. xtw.WriteStartElement ("foo");
  544. xtw.WriteStartElement ("bar");
  545. xtw.WriteElementString ("baz", "");
  546. xtw.Close ();
  547. Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
  548. }
  549. [Test]
  550. public void FormattingFromRemarks ()
  551. {
  552. // Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
  553. xtw.Formatting = Formatting.Indented;
  554. xtw.WriteStartElement ("ol");
  555. xtw.WriteStartElement ("li");
  556. xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
  557. xtw.WriteElementString ("b", "E");
  558. xtw.WriteElementString ("i", "lephant");
  559. xtw.WriteString (" walks slowly.");
  560. xtw.WriteEndElement ();
  561. xtw.WriteEndElement ();
  562. Assert.AreEqual (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
  563. }
  564. [Test]
  565. public void LookupPrefix ()
  566. {
  567. xtw.WriteStartElement ("root");
  568. xtw.WriteStartElement ("one");
  569. xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
  570. xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
  571. Assert.AreEqual ("foo", xtw.LookupPrefix ("http://abc.def"));
  572. Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"));
  573. xtw.WriteEndElement ();
  574. xtw.WriteStartElement ("two");
  575. xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
  576. xtw.WriteString("quux");
  577. Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"));
  578. Assert.IsNull (xtw.LookupPrefix ("http://abc.def"));
  579. Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"));
  580. Assert.IsNull (xtw.LookupPrefix ("http://bogus"));
  581. }
  582. [Test]
  583. public void NamespacesAttributesPassingInNamespaces ()
  584. {
  585. xtw.Namespaces = false;
  586. xtw.WriteStartElement ("foo");
  587. // These shouldn't throw any exceptions since they don't pass in
  588. // a namespace.
  589. xtw.WriteAttributeString ("bar", "baz");
  590. xtw.WriteAttributeString ("", "a", "", "b");
  591. xtw.WriteAttributeString (null, "c", "", "d");
  592. xtw.WriteAttributeString ("", "e", null, "f");
  593. xtw.WriteAttributeString (null, "g", null, "h");
  594. Assert.AreEqual ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
  595. }
  596. [Test]
  597. public void NamespacesElementsPassingInNamespaces ()
  598. {
  599. xtw.Namespaces = false;
  600. // These shouldn't throw any exceptions since they don't pass in
  601. // a namespace.
  602. xtw.WriteElementString ("foo", "bar");
  603. xtw.WriteStartElement ("baz");
  604. xtw.WriteStartElement ("quux", "");
  605. xtw.WriteStartElement ("quuux", null);
  606. xtw.WriteStartElement (null, "a", null);
  607. xtw.WriteStartElement (null, "b", "");
  608. xtw.WriteStartElement ("", "c", null);
  609. xtw.WriteStartElement ("", "d", "");
  610. Assert.AreEqual ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
  611. }
  612. [Test]
  613. [ExpectedException (typeof (ArgumentException))]
  614. public void NamespacesElementsPassingInNamespacesInvalid1 ()
  615. {
  616. // These should throw ArgumentException because they pass in a
  617. // namespace when Namespaces = false.
  618. xtw.Namespaces = false;
  619. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  620. }
  621. [Test]
  622. [ExpectedException (typeof (ArgumentException))]
  623. public void NamespacesElementsPassingInNamespacesInvalid2 ()
  624. {
  625. xtw.Namespaces = false;
  626. xtw.WriteStartElement ("foo", "http://netsack.com/");
  627. }
  628. [Test]
  629. [ExpectedException (typeof (ArgumentException))]
  630. public void NamespacesElementsPassingInNamespacesInvalid3 ()
  631. {
  632. xtw.Namespaces = false;
  633. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  634. }
  635. [Test]
  636. [ExpectedException (typeof (ArgumentException))]
  637. public void NamespacesElementsPassingInNamespacesInvalid4 ()
  638. {
  639. xtw.Namespaces = false;
  640. xtw.WriteStartElement ("foo", "bar", null);
  641. }
  642. [Test]
  643. [ExpectedException (typeof (ArgumentException))]
  644. public void NamespacesElementsPassingInNamespacesInvalid5 ()
  645. {
  646. xtw.Namespaces = false;
  647. xtw.WriteStartElement ("foo", "bar", "");
  648. }
  649. [Test]
  650. [ExpectedException (typeof (ArgumentException))]
  651. public void NamespacesElementsPassingInNamespacesInvalid6 ()
  652. {
  653. xtw.Namespaces = false;
  654. xtw.WriteStartElement ("foo", "", "");
  655. }
  656. [Test]
  657. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  658. {
  659. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  660. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  661. xtw.WriteElementString("baz", String.Empty, String.Empty);
  662. xtw.WriteEndElement();
  663. xtw.WriteEndElement();
  664. Assert.AreEqual ("<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
  665. StringWriterText, "XmlTextWriter is incorrectly outputting namespaces.");
  666. }
  667. [Test]
  668. public void NamespacesPrefix ()
  669. {
  670. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  671. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  672. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  673. xtw.WriteEndElement ();
  674. xtw.WriteEndElement ();
  675. Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
  676. StringWriterText, "XmlTextWriter is incorrectly outputting prefixes.");
  677. }
  678. [Test]
  679. [ExpectedException (typeof (ArgumentException))]
  680. #if NET_2_0
  681. [Category ("NotDotNet")] // ... bug or design?
  682. #endif
  683. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  684. {
  685. xtw.WriteStartElement ("foo", "bar", "");
  686. }
  687. [Test]
  688. [ExpectedException (typeof (ArgumentException))]
  689. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  690. {
  691. xtw.WriteStartElement ("foo", "bar", null);
  692. }
  693. [Test]
  694. public void NamespacesSettingWhenWriteStateNotStart ()
  695. {
  696. xtw.WriteStartElement ("foo");
  697. try {
  698. xtw.Namespaces = false;
  699. Assert.Fail ("Expected an InvalidOperationException.");
  700. } catch (InvalidOperationException) {}
  701. Assert.IsTrue (xtw.Namespaces);
  702. }
  703. [Test]
  704. public void ProcessingInstructionValid ()
  705. {
  706. xtw.WriteProcessingInstruction("foo", "bar");
  707. Assert.AreEqual ("<?foo bar?>", StringWriterText, "WriteProcessingInstruction had incorrect output.");
  708. }
  709. [Test]
  710. [ExpectedException (typeof (ArgumentException))]
  711. public void ProcessingInstructionInvalid1 ()
  712. {
  713. xtw.WriteProcessingInstruction("fo?>o", "bar");
  714. }
  715. [Test]
  716. [ExpectedException (typeof (ArgumentException))]
  717. public void ProcessingInstructionInvalid2 ()
  718. {
  719. xtw.WriteProcessingInstruction("foo", "ba?>r");
  720. }
  721. [Test]
  722. [ExpectedException (typeof (ArgumentException))]
  723. public void ProcessingInstructionInvalid3 ()
  724. {
  725. xtw.WriteProcessingInstruction("", "bar");
  726. }
  727. [Test]
  728. [ExpectedException (typeof (ArgumentException))]
  729. public void ProcessingInstructionInvalid4 ()
  730. {
  731. xtw.WriteProcessingInstruction(null, "bar");
  732. }
  733. [Test]
  734. public void QuoteCharDoubleQuote ()
  735. {
  736. xtw.QuoteChar = '"';
  737. // version, encoding, standalone
  738. xtw.WriteStartDocument (true);
  739. // namespace declaration
  740. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  741. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  742. }
  743. [Test]
  744. [ExpectedException (typeof (ArgumentException))]
  745. public void QuoteCharInvalid ()
  746. {
  747. xtw.QuoteChar = 'x';
  748. }
  749. [Test]
  750. public void WriteBase64 ()
  751. {
  752. UTF8Encoding encoding = new UTF8Encoding();
  753. byte[] fooBar = encoding.GetBytes("foobar");
  754. xtw.WriteBase64 (fooBar, 0, 6);
  755. Assert.AreEqual ("Zm9vYmFy", StringWriterText);
  756. try {
  757. xtw.WriteBase64 (fooBar, 3, 6);
  758. Assert.Fail ("Expected an Argument Exception to be thrown.");
  759. } catch (ArgumentException) {}
  760. try {
  761. xtw.WriteBase64 (fooBar, -1, 6);
  762. Assert.Fail ("Expected an Argument Exception to be thrown.");
  763. } catch (ArgumentOutOfRangeException) {}
  764. try {
  765. xtw.WriteBase64 (fooBar, 3, -1);
  766. Assert.Fail ("Expected an Argument Exception to be thrown.");
  767. } catch (ArgumentOutOfRangeException) {}
  768. try {
  769. xtw.WriteBase64 (null, 0, 6);
  770. Assert.Fail ("Expected an Argument Exception to be thrown.");
  771. } catch (ArgumentNullException) {}
  772. }
  773. [Test]
  774. public void WriteBinHex ()
  775. {
  776. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  777. xtw.WriteBinHex (bytes, 0, 11);
  778. Assert.AreEqual ("040E22365E7286C2FF0005", StringWriterText);
  779. }
  780. [Test]
  781. public void WriteCharEntity ()
  782. {
  783. xtw.WriteCharEntity ('a');
  784. Assert.AreEqual ("&#x61;", StringWriterText);
  785. xtw.WriteCharEntity ('A');
  786. Assert.AreEqual ("&#x61;&#x41;", StringWriterText);
  787. xtw.WriteCharEntity ('1');
  788. Assert.AreEqual ("&#x61;&#x41;&#x31;", StringWriterText);
  789. xtw.WriteCharEntity ('K');
  790. Assert.AreEqual ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  791. try {
  792. xtw.WriteCharEntity ((char)0xd800);
  793. } catch (ArgumentException) {}
  794. }
  795. [Test]
  796. [ExpectedException (typeof (InvalidOperationException))]
  797. public void WriteEndAttribute ()
  798. {
  799. xtw.WriteEndAttribute ();
  800. }
  801. [Test]
  802. public void WriteEndDocument ()
  803. {
  804. try {
  805. xtw.WriteEndDocument ();
  806. Assert.Fail ("Expected an Exception.");
  807. // in .NET 2.0 it is InvalidOperationException.
  808. // in .NET 1,1 it is ArgumentException.
  809. } catch (Exception) {}
  810. }
  811. [Test]
  812. public void WriteEndDocument2 ()
  813. {
  814. xtw.WriteStartDocument ();
  815. try
  816. {
  817. xtw.WriteEndDocument ();
  818. Assert.Fail ("Expected an Exception.");
  819. // in .NET 2.0 it is InvalidOperationException.
  820. // in .NET 1,1 it is ArgumentException.
  821. } catch (Exception) {}
  822. }
  823. [Test]
  824. public void WriteEndDocument3 ()
  825. {
  826. xtw.WriteStartDocument ();
  827. xtw.WriteStartElement ("foo");
  828. xtw.WriteStartAttribute ("bar", null);
  829. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  830. xtw.WriteEndDocument ();
  831. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  832. Assert.AreEqual (WriteState.Start, xtw.WriteState);
  833. }
  834. [Test]
  835. [ExpectedException (typeof (InvalidOperationException))]
  836. public void WriteEndElement ()
  837. {
  838. // no matching StartElement
  839. xtw.WriteEndElement ();
  840. }
  841. [Test]
  842. public void WriteEndElement2 ()
  843. {
  844. xtw.WriteStartElement ("foo");
  845. xtw.WriteEndElement ();
  846. Assert.AreEqual ("<foo />", StringWriterText);
  847. xtw.WriteStartElement ("bar");
  848. xtw.WriteStartAttribute ("baz", null);
  849. xtw.WriteEndElement ();
  850. Assert.AreEqual ("<foo /><bar baz='' />", StringWriterText);
  851. }
  852. [Test]
  853. public void FullEndElement ()
  854. {
  855. xtw.WriteStartElement ("foo");
  856. xtw.WriteFullEndElement ();
  857. Assert.AreEqual ("<foo></foo>", StringWriterText);
  858. xtw.WriteStartElement ("bar");
  859. xtw.WriteAttributeString ("foo", "bar");
  860. xtw.WriteFullEndElement ();
  861. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  862. xtw.WriteStartElement ("baz");
  863. xtw.WriteStartAttribute ("bar", null);
  864. xtw.WriteFullEndElement ();
  865. Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  866. }
  867. [Test]
  868. public void WriteQualifiedName ()
  869. {
  870. xtw.WriteStartElement (null, "test", null);
  871. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  872. xtw.WriteQualifiedName ("bob", "http://localhost/");
  873. xtw.WriteEndElement ();
  874. Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  875. }
  876. [Test]
  877. public void WriteQualifiedNameNonDeclaredAttribute ()
  878. {
  879. xtw.WriteStartElement ("foo");
  880. xtw.WriteStartAttribute ("a", "");
  881. xtw.WriteQualifiedName ("attr", "urn:a");
  882. xtw.WriteWhitespace (" ");
  883. xtw.WriteQualifiedName ("attr", "urn:b");
  884. xtw.WriteEndAttribute ();
  885. xtw.WriteEndElement ();
  886. string xml = sw.ToString ();
  887. Assert.IsTrue (xml.IndexOf ("<foo ") >= 0, "foo");
  888. Assert.IsTrue (xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0, "qnames");
  889. Assert.IsTrue (xml.IndexOf (" xmlns:d1p1='urn:a'") > 0, "xmlns:a");
  890. Assert.IsTrue (xml.IndexOf (" xmlns:d1p2='urn:b'") > 0, "xmlns:b");
  891. }
  892. [Test]
  893. [ExpectedException (typeof (ArgumentException))]
  894. public void WriteQualifiedNameNonDeclaredContent ()
  895. {
  896. xtw.WriteStartElement ("foo");
  897. xtw.WriteQualifiedName ("abc", "urn:abc");
  898. }
  899. [Test]
  900. [ExpectedException (typeof (ArgumentException))]
  901. public void WriteQualifiedNameNonNCName ()
  902. {
  903. xtw.WriteStartElement ("foo");
  904. xtw.WriteAttributeString ("xmlns", "urn:default");
  905. xtw.WriteStartElement ("child");
  906. xtw.WriteStartAttribute ("a", "");
  907. xtw.WriteQualifiedName ("x:def", "urn:def");
  908. }
  909. [Test]
  910. public void WriteRaw ()
  911. {
  912. xtw.WriteRaw("&<>\"'");
  913. Assert.AreEqual ("&<>\"'", StringWriterText);
  914. xtw.WriteRaw(null);
  915. Assert.AreEqual ("&<>\"'", StringWriterText);
  916. xtw.WriteRaw("");
  917. Assert.AreEqual ("&<>\"'", StringWriterText);
  918. }
  919. [Test]
  920. public void WriteRawInvalidInAttribute ()
  921. {
  922. xtw.WriteStartElement ("foo");
  923. xtw.WriteStartAttribute ("bar", null);
  924. xtw.WriteRaw ("&<>\"'");
  925. xtw.WriteEndAttribute ();
  926. xtw.WriteEndElement ();
  927. Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
  928. }
  929. [Test]
  930. public void WriteStateTest ()
  931. {
  932. Assert.AreEqual (WriteState.Start, xtw.WriteState);
  933. xtw.WriteStartDocument ();
  934. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  935. xtw.WriteStartElement ("root");
  936. Assert.AreEqual (WriteState.Element, xtw.WriteState);
  937. xtw.WriteElementString ("foo", "bar");
  938. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  939. xtw.Close ();
  940. Assert.AreEqual (WriteState.Closed, xtw.WriteState);
  941. }
  942. [Test]
  943. public void WriteString ()
  944. {
  945. xtw.WriteStartDocument ();
  946. try {
  947. xtw.WriteString("foo");
  948. } catch (InvalidOperationException) {}
  949. }
  950. [Test]
  951. public void WriteString2 ()
  952. {
  953. xtw.WriteStartDocument ();
  954. // Testing attribute values
  955. xtw.WriteStartElement ("foo");
  956. xtw.WriteAttributeString ("bar", "&<>");
  957. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  958. }
  959. [Test]
  960. public void WriteAttributeStringSingleQuoteChar()
  961. {
  962. // When QuoteChar is single quote then replaces single quotes within attributes
  963. // but not double quotes.
  964. xtw.WriteStartElement ("foo");
  965. xtw.WriteAttributeString ("bar", "\"baz\"");
  966. xtw.WriteAttributeString ("quux", "'baz'");
  967. Assert.AreEqual ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  968. }
  969. [Test]
  970. public void WriteAttributeStringDoubleQuoteChar()
  971. {
  972. // When QuoteChar is double quote then replaces double quotes within attributes
  973. // but not single quotes.
  974. xtw.QuoteChar = '"';
  975. xtw.WriteStartElement ("foo");
  976. xtw.WriteAttributeString ("bar", "\"baz\"");
  977. xtw.WriteAttributeString ("quux", "'baz'");
  978. Assert.AreEqual ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  979. }
  980. [Test]
  981. public void WriteStringWithEntities()
  982. {
  983. // Testing element values
  984. xtw.QuoteChar = '\'';
  985. xtw.WriteElementString ("foo", "&<>\"'");
  986. Assert.AreEqual ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  987. }
  988. [Test]
  989. public void XmlLang ()
  990. {
  991. Assert.IsNull (xtw.XmlLang);
  992. xtw.WriteStartElement ("foo");
  993. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  994. Assert.AreEqual ("langfoo", xtw.XmlLang);
  995. Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
  996. xtw.WriteAttributeString ("boo", "yah");
  997. Assert.AreEqual ("langfoo", xtw.XmlLang);
  998. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  999. xtw.WriteElementString("bar", "baz");
  1000. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1001. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  1002. xtw.WriteString("baz");
  1003. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1004. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  1005. xtw.WriteStartElement ("quux");
  1006. xtw.WriteStartAttribute ("xml", "lang", null);
  1007. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1008. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1009. xtw.WriteString("langbar");
  1010. // Commented out there: it is implementation-dependent.
  1011. // and incompatible between .NET 1.0 and 1.1
  1012. // Assert.AreEqual ("langfoo", xtw.XmlLang);
  1013. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  1014. xtw.WriteEndAttribute ();
  1015. // Commented out there: it is implementation-dependent.
  1016. // and incompatible between .NET 1.0 and 1.1
  1017. // Assert.AreEqual ("langbar", xtw.XmlLang);
  1018. // Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  1019. // check if xml:lang repeats output even if same as current scope.
  1020. xtw.WriteStartElement ("joe");
  1021. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  1022. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  1023. xtw.WriteElementString ("quuux", "squonk");
  1024. Assert.AreEqual ("langbar", xtw.XmlLang);
  1025. Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  1026. xtw.WriteEndElement ();
  1027. xtw.WriteEndElement ();
  1028. Assert.AreEqual ("langfoo", xtw.XmlLang);
  1029. 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>", StringWriterText);
  1030. xtw.WriteEndElement ();
  1031. Assert.IsNull (xtw.XmlLang);
  1032. 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>", StringWriterText);
  1033. xtw.Close ();
  1034. Assert.IsNull (xtw.XmlLang);
  1035. }
  1036. // TODO: test operational aspects
  1037. [Test]
  1038. public void XmlSpaceTest ()
  1039. {
  1040. xtw.WriteStartElement ("foo");
  1041. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1042. xtw.WriteStartElement ("bar");
  1043. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1044. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1045. Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText);
  1046. xtw.WriteStartElement ("baz");
  1047. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  1048. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1049. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  1050. xtw.WriteStartElement ("quux");
  1051. xtw.WriteStartAttribute ("xml", "space", null);
  1052. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1053. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  1054. // Commented out there: it is implementation-dependent
  1055. // and incompatible between .NET 1.0 and 1.1
  1056. xtw.WriteString ("default");
  1057. // Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1058. // Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  1059. xtw.WriteEndAttribute ();
  1060. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
  1061. Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  1062. xtw.WriteEndElement ();
  1063. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1064. xtw.WriteEndElement ();
  1065. Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
  1066. xtw.WriteEndElement ();
  1067. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1068. xtw.WriteStartElement ("quux");
  1069. }
  1070. [Test]
  1071. [ExpectedException (typeof (ArgumentException))]
  1072. public void XmlSpaceTestInvalidValue1 ()
  1073. {
  1074. xtw.WriteStartElement ("foo");
  1075. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1076. }
  1077. [Test]
  1078. [ExpectedException (typeof (ArgumentException))]
  1079. public void XmlSpaceTestInvalidValue2 ()
  1080. {
  1081. xtw.WriteStartElement ("foo");
  1082. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  1083. }
  1084. [Test]
  1085. [ExpectedException (typeof (ArgumentException))]
  1086. public void XmlSpaceTestInvalidValue3 ()
  1087. {
  1088. xtw.WriteStartElement ("foo");
  1089. xtw.WriteAttributeString ("xml", "space", null, "Default");
  1090. }
  1091. [Test]
  1092. [ExpectedException (typeof (ArgumentException))]
  1093. public void XmlSpaceTestInvalidValue4 ()
  1094. {
  1095. xtw.WriteStartElement ("foo");
  1096. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  1097. }
  1098. [Test]
  1099. [ExpectedException (typeof (ArgumentException))]
  1100. public void WriteWhitespaceNonWhitespace ()
  1101. {
  1102. xtw.WriteWhitespace ("x");
  1103. }
  1104. [Test]
  1105. public void XmlSpaceRaw ()
  1106. {
  1107. xtw.WriteStartElement ("foo");
  1108. xtw.WriteStartAttribute ("xml", "space", null);
  1109. Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1110. Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1111. xtw.WriteString ("default");
  1112. // Commented out there: it is implementation-dependent
  1113. // and incompatible between .NET 1.0 and 1.1
  1114. // Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
  1115. // Assert.AreEqual ("<foo xml:space='", StringWriterText);
  1116. xtw.WriteEndAttribute ();
  1117. Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
  1118. Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
  1119. }
  1120. [Test]
  1121. public void WriteAttributes ()
  1122. {
  1123. XmlDocument doc = new XmlDocument();
  1124. StringWriter sw = new StringWriter();
  1125. XmlWriter wr = new XmlTextWriter(sw);
  1126. StringBuilder sb = sw.GetStringBuilder();
  1127. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1128. 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);
  1129. xtr.Read(); // read XMLDecl
  1130. wr.WriteAttributes(xtr, false);
  1131. // This method don't always have to take this double-quoted style...
  1132. Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
  1133. "#WriteAttributes.XmlDecl.1");
  1134. sb.Remove(0, sb.Length); // init
  1135. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  1136. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  1137. xtr.Read(); // read XMLDecl
  1138. Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
  1139. sw = new StringWriter ();
  1140. wr = new XmlTextWriter (sw);
  1141. // This block raises an error on MS.NET 1.0.
  1142. wr.WriteAttributes(xtr, false);
  1143. // This method don't always have to take this double-quoted style...
  1144. Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
  1145. "#WriteAttributes.XmlDecl.2");
  1146. sw = new StringWriter ();
  1147. wr = new XmlTextWriter (sw);
  1148. sb.Remove(0, sb.Length); // init
  1149. xtr.Read(); // read root
  1150. Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
  1151. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  1152. wr.WriteAttributes(xtr, false);
  1153. wr.WriteEndElement();
  1154. wr.Close();
  1155. // This method don't always have to take this double-quoted style...
  1156. Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
  1157. "#WriteAttributes.Element");
  1158. xtr.Close ();
  1159. }
  1160. [Test]
  1161. public void WriteWhitespace ()
  1162. {
  1163. xtw.WriteStartElement ("a");
  1164. xtw.WriteWhitespace ("\n\t");
  1165. xtw.WriteStartElement ("b");
  1166. xtw.WriteWhitespace ("\n\t");
  1167. xtw.WriteEndElement ();
  1168. xtw.WriteWhitespace ("\n");
  1169. xtw.WriteEndElement ();
  1170. xtw.WriteWhitespace ("\n");
  1171. xtw.Flush ();
  1172. Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  1173. }
  1174. [Test]
  1175. public void FlushDoesntCloseTag ()
  1176. {
  1177. xtw.WriteStartElement ("foo");
  1178. xtw.WriteAttributeString ("bar", "baz");
  1179. xtw.Flush ();
  1180. Assert.AreEqual ("<foo bar='baz'", StringWriterText);
  1181. }
  1182. [Test]
  1183. public void WriteWhitespaceClosesTag ()
  1184. {
  1185. xtw.WriteStartElement ("foo");
  1186. xtw.WriteAttributeString ("bar", "baz");
  1187. xtw.WriteWhitespace (" ");
  1188. Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
  1189. }
  1190. [Test]
  1191. public void DontOutputMultipleXmlns ()
  1192. {
  1193. XmlDocument doc = new XmlDocument();
  1194. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  1195. XmlDocument doc2 = new XmlDocument();
  1196. doc2.LoadXml(doc.InnerXml);
  1197. Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  1198. doc2.OuterXml);
  1199. }
  1200. [Test]
  1201. public void DontOutputNonDeclaredXmlns ()
  1202. {
  1203. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  1204. XmlDocument doc = new XmlDocument();
  1205. doc.LoadXml(xml);
  1206. XmlDocument doc2 = new XmlDocument();
  1207. doc2.LoadXml(doc.InnerXml);
  1208. Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
  1209. }
  1210. [Test]
  1211. public void DontOutputRemovalDefaultNSDeclaration ()
  1212. {
  1213. xtw.WriteStartDocument ();
  1214. xtw.WriteStartElement ("foo");
  1215. xtw.WriteAttributeString ("xmlns", "probe");
  1216. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
  1217. xtw.WriteStartElement ("b");
  1218. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"));
  1219. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1220. xtw.WriteEndElement (); // b2
  1221. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1222. xtw.WriteEndElement (); // b2
  1223. xtw.WriteEndElement (); // b
  1224. xtw.WriteEndElement (); // foo
  1225. xtw.WriteEndDocument ();
  1226. xtw.Close ();
  1227. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText);
  1228. }
  1229. [Test]
  1230. public void DontOutputRemovalDefaultNSDeclaration2 ()
  1231. {
  1232. xtw.WriteStartDocument ();
  1233. // IMPORTANT DIFFERENCE!! ns = "", not null
  1234. xtw.WriteStartElement ("foo", "");
  1235. xtw.WriteAttributeString ("xmlns", "probe");
  1236. Assert.IsNull (xtw.LookupPrefix ("probe"));
  1237. xtw.WriteStartElement ("b");
  1238. Assert.IsNull (xtw.LookupPrefix ("probe"));
  1239. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1240. xtw.WriteEndElement (); // b2
  1241. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1242. xtw.WriteEndElement (); // b2
  1243. xtw.WriteEndElement (); // b
  1244. xtw.WriteEndElement (); // foo
  1245. xtw.WriteEndDocument ();
  1246. xtw.Close ();
  1247. Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText);
  1248. }
  1249. [Test]
  1250. public void DoOutputRemovalDefaultNSDeclaration ()
  1251. {
  1252. xtw.WriteStartElement ("docelem", "a-namespace");
  1253. XmlDocument doc = new XmlDocument ();
  1254. doc.CreateElement ("hola").WriteTo (xtw);
  1255. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  1256. xtw.WriteEndElement ();
  1257. xtw.Close ();
  1258. Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  1259. }
  1260. [Test]
  1261. public void WriteAttributeTakePrecedenceOnXmlns ()
  1262. {
  1263. xtw.WriteStartElement ("root", "urn:foo");
  1264. xtw.WriteAttributeString ("xmlns", "urn:bar");
  1265. xtw.WriteEndElement ();
  1266. xtw.Close ();
  1267. Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
  1268. }
  1269. [Test]
  1270. [ExpectedException (typeof (ArgumentException))]
  1271. public void LookupPrefixNull ()
  1272. {
  1273. xtw.LookupPrefix (null);
  1274. }
  1275. [Test]
  1276. [ExpectedException (typeof (ArgumentException))]
  1277. public void LookupPrefixEmpty ()
  1278. {
  1279. xtw.LookupPrefix (String.Empty);
  1280. }
  1281. [Test]
  1282. public void LookupPrefixIgnoresXmlnsAttribute ()
  1283. {
  1284. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1285. xtw.WriteStartElement ("root");
  1286. Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
  1287. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1288. // Surprisingly to say, it is ignored!!
  1289. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1290. xtw.WriteStartElement ("hoge");
  1291. // (still after flushing previous start element.)
  1292. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1293. xtw.WriteStartElement ("fuga", "urn:foo");
  1294. // Is this testing on the correct way? Yes, here it is.
  1295. Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1296. }
  1297. [Test]
  1298. public void WriteInvalidNames ()
  1299. {
  1300. xtw.WriteStartElement ("foo<>");
  1301. xtw.WriteAttributeString ("ho<>ge", "value");
  1302. }
  1303. [Test]
  1304. [ExpectedException (typeof (ArgumentException))]
  1305. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1306. {
  1307. xtw.WriteStartAttribute ("some", "foo", null);
  1308. }
  1309. [Test]
  1310. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1311. {
  1312. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1313. }
  1314. [Test]
  1315. [ExpectedException (typeof (ArgumentException))]
  1316. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1317. {
  1318. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1319. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1320. xtw.WriteEndAttribute ();
  1321. }
  1322. [Test]
  1323. [ExpectedException (typeof (ArgumentException))]
  1324. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1325. {
  1326. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1327. }
  1328. [Test]
  1329. [ExpectedException (typeof (ArgumentException))]
  1330. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1331. {
  1332. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1333. }
  1334. [Test]
  1335. public void WriteRawProceedToProlog ()
  1336. {
  1337. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1338. xtw.WriteRaw ("");
  1339. Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
  1340. }
  1341. [Test]
  1342. public void Indent ()
  1343. {
  1344. XmlDocument doc = new XmlDocument ();
  1345. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1346. StringWriter sw = new StringWriter ();
  1347. sw.NewLine = "_";
  1348. XmlTextWriter xtw = new XmlTextWriter (sw);
  1349. xtw.Formatting = Formatting.Indented;
  1350. doc.WriteContentTo (xtw);
  1351. Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1352. }
  1353. [Test]
  1354. public void Indent2 ()
  1355. {
  1356. StringWriter sw = new StringWriter ();
  1357. XmlTextWriter xtw = new XmlTextWriter (sw);
  1358. xtw.Formatting = Formatting.Indented;
  1359. // sadly, this silly usage of this method is actually
  1360. // used in WriteNode() in MS.NET.
  1361. xtw.WriteProcessingInstruction ("xml",
  1362. "version=\"1.0\"");
  1363. xtw.WriteComment ("sample XML fragment");
  1364. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  1365. @"<?xml version=""1.0""?>{0}" +
  1366. "<!--sample XML fragment-->", Environment.NewLine),
  1367. sw.ToString ());
  1368. }
  1369. [Test]
  1370. public void CloseTwice ()
  1371. {
  1372. StringWriter sw = new StringWriter ();
  1373. XmlTextWriter writer = new XmlTextWriter (sw);
  1374. writer.Close ();
  1375. // should not result in an exception
  1376. writer.Close ();
  1377. }
  1378. [Test]
  1379. public void WriteRawWriteString ()
  1380. {
  1381. // WriteRaw () -> WriteString ().
  1382. xtw.WriteRaw ("");
  1383. xtw.WriteString ("foo");
  1384. Assert.AreEqual (WriteState.Content, xtw.WriteState);
  1385. }
  1386. [Test]
  1387. public void LookupOverridenPrefix ()
  1388. {
  1389. xtw.WriteStartElement ("out");
  1390. xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1391. xtw.WriteStartElement ("baz", "foo", "abc");
  1392. Assert.IsNull (xtw.LookupPrefix ("xyz"));
  1393. }
  1394. [Test]
  1395. public void DuplicatingNamespaceMappingInAttributes ()
  1396. {
  1397. xtw.WriteStartElement ("out");
  1398. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1399. xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1400. xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1401. xtw.WriteStartElement ("out");
  1402. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1403. xtw.WriteStartElement ("out");
  1404. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1405. xtw.WriteEndElement ();
  1406. xtw.WriteEndElement ();
  1407. xtw.WriteEndElement ();
  1408. string xml = sw.ToString ();
  1409. Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo");
  1410. Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar");
  1411. Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz");
  1412. Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2");
  1413. Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1");
  1414. Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p");
  1415. Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining");
  1416. }
  1417. [Test]
  1418. public void WriteXmlSpaceIgnoresNS ()
  1419. {
  1420. xtw.WriteStartElement ("root");
  1421. xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1422. xtw.WriteEndElement ();
  1423. Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
  1424. }
  1425. [Test] // bug #75546
  1426. public void WriteEmptyNSQNameInAttribute ()
  1427. {
  1428. XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
  1429. xtw.WriteStartElement ("foo", "urn:goo");
  1430. xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1431. xtw.WriteStartAttribute ("foo", "");
  1432. xtw.WriteQualifiedName ("n1", "urn:bar");
  1433. xtw.WriteEndAttribute ();
  1434. xtw.WriteStartAttribute ("foo", "");
  1435. xtw.WriteQualifiedName ("n2", "");
  1436. xtw.WriteEndAttribute ();
  1437. }
  1438. [Test]
  1439. public void WriteDocType ()
  1440. {
  1441. // we have the following test matrix:
  1442. //
  1443. // | name | publicid | systemid | subset|
  1444. // |------------------------------------|
  1445. // | X | X | X | X | #01
  1446. // | X | E | X | X | #02
  1447. // | X | X | E | X | #03
  1448. // | X | X | X | E | #04
  1449. // | X | E | E | X | #05
  1450. // | X | X | E | E | #06
  1451. // | X | E | X | E | #07
  1452. // | X | E | E | E | #08
  1453. // | X | N | X | X | #09
  1454. // | X | X | N | X | #10
  1455. // | X | X | X | N | #11
  1456. // | X | N | N | X | #12
  1457. // | X | X | N | N | #13
  1458. // | X | N | X | N | #14
  1459. // | X | N | N | N | #15
  1460. //
  1461. // Legend:
  1462. // -------
  1463. // X = Has value
  1464. // E = Zero-length string
  1465. // N = Null
  1466. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1467. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1468. Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
  1469. " 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
  1470. sw.ToString (), "#01");
  1471. sw.GetStringBuilder ().Length = 0;
  1472. xtw = new XmlTextWriter (sw);
  1473. xtw.WriteDocType ("test", string.Empty,
  1474. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1475. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1476. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1477. sw.ToString (), "#02");
  1478. sw.GetStringBuilder ().Length = 0;
  1479. xtw = new XmlTextWriter (sw);
  1480. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1481. string.Empty, "sub");
  1482. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1483. " \"\"[sub]>",
  1484. sw.ToString (), "#03");
  1485. sw.GetStringBuilder ().Length = 0;
  1486. xtw = new XmlTextWriter (sw);
  1487. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1488. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1489. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1490. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1491. sw.ToString (), "#04");
  1492. sw.GetStringBuilder ().Length = 0;
  1493. xtw = new XmlTextWriter (sw);
  1494. xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
  1495. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
  1496. sw.ToString (), "#05");
  1497. sw.GetStringBuilder ().Length = 0;
  1498. xtw = new XmlTextWriter (sw);
  1499. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1500. string.Empty, string.Empty);
  1501. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1502. " \"\"[]>",
  1503. sw.ToString (), "#06");
  1504. sw.GetStringBuilder ().Length = 0;
  1505. xtw = new XmlTextWriter (sw);
  1506. xtw.WriteDocType ("test", string.Empty,
  1507. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
  1508. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
  1509. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
  1510. sw.ToString (), "#07");
  1511. sw.GetStringBuilder ().Length = 0;
  1512. xtw = new XmlTextWriter (sw);
  1513. xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
  1514. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
  1515. sw.ToString (), "#08");
  1516. sw.GetStringBuilder ().Length = 0;
  1517. xtw = new XmlTextWriter (sw);
  1518. xtw.WriteDocType ("test", (string) null,
  1519. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1520. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1521. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
  1522. sw.ToString (), "#09");
  1523. sw.GetStringBuilder ().Length = 0;
  1524. xtw = new XmlTextWriter (sw);
  1525. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1526. (string) null, "sub");
  1527. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1528. " \"\"[sub]>",
  1529. sw.ToString (), "#10");
  1530. sw.GetStringBuilder ().Length = 0;
  1531. xtw = new XmlTextWriter (sw);
  1532. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1533. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1534. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1535. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1536. sw.ToString (), "#11");
  1537. sw.GetStringBuilder ().Length = 0;
  1538. xtw = new XmlTextWriter (sw);
  1539. xtw.WriteDocType ("test", (string) null, (string) null, "sub");
  1540. Assert.AreEqual ("<!DOCTYPE test[sub]>",
  1541. sw.ToString (), "#12");
  1542. sw.GetStringBuilder ().Length = 0;
  1543. xtw = new XmlTextWriter (sw);
  1544. xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
  1545. (string) null, (string) null);
  1546. Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
  1547. " \"\">",
  1548. sw.ToString (), "#13");
  1549. sw.GetStringBuilder ().Length = 0;
  1550. xtw = new XmlTextWriter (sw);
  1551. xtw.WriteDocType ("test", (string) null,
  1552. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
  1553. Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
  1554. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
  1555. sw.ToString (), "#14");
  1556. sw.GetStringBuilder ().Length = 0;
  1557. xtw = new XmlTextWriter (sw);
  1558. xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
  1559. Assert.AreEqual ("<!DOCTYPE test>",
  1560. sw.ToString (), "#15");
  1561. }
  1562. [Test]
  1563. [ExpectedException (typeof (ArgumentException))]
  1564. public void WriteDocType_EmptyName ()
  1565. {
  1566. xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1567. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1568. }
  1569. [Test]
  1570. [ExpectedException (typeof (ArgumentException))]
  1571. public void WriteDocType_NullName ()
  1572. {
  1573. xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
  1574. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
  1575. }
  1576. [Test] // bug #76095
  1577. public void SurrogatePairsInWriteString ()
  1578. {
  1579. MemoryStream ms = new MemoryStream ();
  1580. XmlWriter writer = new XmlTextWriter(ms, null);
  1581. writer.WriteElementString("a", "\ud800\udf39");
  1582. writer.Close();
  1583. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1584. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1585. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1586. }
  1587. #if NET_2_0
  1588. [Test]
  1589. [ExpectedException (typeof (InvalidOperationException))]
  1590. public void RejectWritingAtErrorState ()
  1591. {
  1592. try {
  1593. xtw.WriteEndElement ();
  1594. } catch (Exception) {
  1595. }
  1596. xtw.WriteStartElement ("foo");
  1597. }
  1598. #endif
  1599. }
  1600. }