XmlTextWriterTests.cs 65 KB

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