XmlTextWriterTests.cs 45 KB

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