XmlTextWriterTests.cs 65 KB

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