XmlTextWriterTests.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. // These should throw ArgumentException because they pass in a
  432. // namespace when Namespaces = false.
  433. try {
  434. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  435. Fail ("Expected an ArgumentException.");
  436. } catch (ArgumentException) {}
  437. try {
  438. xtw.WriteStartElement ("foo", "http://netsack.com/");
  439. Fail ("Expected an ArgumentException.");
  440. } catch (ArgumentException) {}
  441. try {
  442. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  443. Fail ("Expected an ArgumentException.");
  444. } catch (ArgumentException) {}
  445. try {
  446. xtw.WriteStartElement ("foo", "bar", null);
  447. Fail ("Expected an ArgumentException.");
  448. } catch (ArgumentException) {}
  449. try {
  450. xtw.WriteStartElement ("foo", "bar", "");
  451. Fail ("Expected an ArgumentException.");
  452. } catch (ArgumentException) {}
  453. try {
  454. xtw.WriteStartElement ("foo", "", "");
  455. Fail ("Expected an ArgumentException.");
  456. } catch (ArgumentException) {}
  457. }
  458. [Test]
  459. public void NamespacesNoNamespaceClearsDefaultNamespace ()
  460. {
  461. xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
  462. xtw.WriteStartElement(String.Empty, "bar", String.Empty);
  463. xtw.WriteElementString("baz", String.Empty, String.Empty);
  464. xtw.WriteEndElement();
  465. xtw.WriteEndElement();
  466. AssertEquals ("XmlTextWriter is incorrectly outputting namespaces.",
  467. "<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>", StringWriterText);
  468. }
  469. [Test]
  470. public void NamespacesPrefix ()
  471. {
  472. xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
  473. xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
  474. xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
  475. xtw.WriteEndElement ();
  476. xtw.WriteEndElement ();
  477. AssertEquals ("XmlTextWriter is incorrectly outputting prefixes.",
  478. "<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>", StringWriterText);
  479. }
  480. [Test]
  481. [ExpectedException (typeof (ArgumentException))]
  482. public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
  483. {
  484. xtw.WriteStartElement ("foo", "bar", "");
  485. }
  486. [Test]
  487. [ExpectedException (typeof (ArgumentException))]
  488. public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
  489. {
  490. xtw.WriteStartElement ("foo", "bar", null);
  491. }
  492. [Test]
  493. public void NamespacesSettingWhenWriteStateNotStart ()
  494. {
  495. xtw.WriteStartElement ("foo");
  496. try
  497. {
  498. xtw.Namespaces = false;
  499. Fail ("Expected an InvalidOperationException.");
  500. }
  501. catch (InvalidOperationException) {}
  502. AssertEquals (true, xtw.Namespaces);
  503. }
  504. [Test]
  505. public void ProcessingInstructionValid ()
  506. {
  507. xtw.WriteProcessingInstruction("foo", "bar");
  508. AssertEquals ("WriteProcessingInstruction had incorrect output.", "<?foo bar?>", StringWriterText);
  509. }
  510. [Test]
  511. public void ProcessingInstructionInvalid ()
  512. {
  513. try
  514. {
  515. xtw.WriteProcessingInstruction("fo?>o", "bar");
  516. Fail("Should have thrown an ArgumentException.");
  517. }
  518. catch (ArgumentException) { }
  519. try
  520. {
  521. xtw.WriteProcessingInstruction("foo", "ba?>r");
  522. Fail("Should have thrown an ArgumentException.");
  523. }
  524. catch (ArgumentException) { }
  525. try
  526. {
  527. xtw.WriteProcessingInstruction("", "bar");
  528. Fail("Should have thrown an ArgumentException.");
  529. }
  530. catch (ArgumentException) { }
  531. try
  532. {
  533. xtw.WriteProcessingInstruction(null, "bar");
  534. Fail("Should have thrown an ArgumentException.");
  535. }
  536. catch (ArgumentException) { }
  537. }
  538. [Test]
  539. public void QuoteCharDoubleQuote ()
  540. {
  541. xtw.QuoteChar = '"';
  542. // version, encoding, standalone
  543. xtw.WriteStartDocument (true);
  544. // namespace declaration
  545. xtw.WriteElementString ("foo", "http://netsack.com", "bar");
  546. AssertEquals ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
  547. }
  548. [Test]
  549. [ExpectedException (typeof (ArgumentException))]
  550. public void QuoteCharInvalid ()
  551. {
  552. xtw.QuoteChar = 'x';
  553. }
  554. [Test]
  555. public void WriteBase64 ()
  556. {
  557. UTF8Encoding encoding = new UTF8Encoding();
  558. byte[] fooBar = encoding.GetBytes("foobar");
  559. xtw.WriteBase64 (fooBar, 0, 6);
  560. AssertEquals("Zm9vYmFy", StringWriterText);
  561. try {
  562. xtw.WriteBase64 (fooBar, 3, 6);
  563. Fail ("Expected an Argument Exception to be thrown.");
  564. } catch (ArgumentException) {}
  565. try {
  566. xtw.WriteBase64 (fooBar, -1, 6);
  567. Fail ("Expected an Argument Exception to be thrown.");
  568. } catch (ArgumentOutOfRangeException) {}
  569. try {
  570. xtw.WriteBase64 (fooBar, 3, -1);
  571. Fail ("Expected an Argument Exception to be thrown.");
  572. } catch (ArgumentOutOfRangeException) {}
  573. try {
  574. xtw.WriteBase64 (null, 0, 6);
  575. Fail ("Expected an Argument Exception to be thrown.");
  576. } catch (ArgumentNullException) {}
  577. }
  578. [Test]
  579. public void WriteBinHex ()
  580. {
  581. byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
  582. xtw.WriteBinHex (bytes, 0, 11);
  583. AssertEquals ("040E22365E7286C2FF0005", StringWriterText);
  584. }
  585. [Test]
  586. public void WriteCharEntity ()
  587. {
  588. xtw.WriteCharEntity ('a');
  589. AssertEquals ("&#x61;", StringWriterText);
  590. xtw.WriteCharEntity ('A');
  591. AssertEquals ("&#x61;&#x41;", StringWriterText);
  592. xtw.WriteCharEntity ('1');
  593. AssertEquals ("&#x61;&#x41;&#x31;", StringWriterText);
  594. xtw.WriteCharEntity ('K');
  595. AssertEquals ("&#x61;&#x41;&#x31;&#x4B;", StringWriterText);
  596. try {
  597. xtw.WriteCharEntity ((char)0xd800);
  598. } catch (ArgumentException) {}
  599. }
  600. [Test]
  601. [ExpectedException (typeof (InvalidOperationException))]
  602. public void WriteEndAttribute ()
  603. {
  604. xtw.WriteEndAttribute ();
  605. }
  606. [Test]
  607. public void WriteEndDocument ()
  608. {
  609. try {
  610. xtw.WriteEndDocument ();
  611. Fail ("Expected an ArgumentException.");
  612. } catch (ArgumentException) {}
  613. xtw.WriteStartDocument ();
  614. try
  615. {
  616. xtw.WriteEndDocument ();
  617. Fail ("Expected an ArgumentException.");
  618. }
  619. catch (ArgumentException) {}
  620. xtw.WriteStartElement ("foo");
  621. xtw.WriteStartAttribute ("bar", null);
  622. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
  623. xtw.WriteEndDocument ();
  624. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
  625. AssertEquals (WriteState.Start, xtw.WriteState);
  626. }
  627. [Test]
  628. public void WriteEndElement ()
  629. {
  630. try {
  631. xtw.WriteEndElement ();
  632. Fail ("Should have thrown an InvalidOperationException.");
  633. } catch (InvalidOperationException) {
  634. // Don't rely on English message assertion.
  635. // It is enough to check an exception occurs.
  636. // AssertEquals ("Exception message is incorrect.", "There was no XML start tag open.", e.Message);
  637. }
  638. xtw.WriteStartElement ("foo");
  639. xtw.WriteEndElement ();
  640. AssertEquals ("<foo />", StringWriterText);
  641. xtw.WriteStartElement ("bar");
  642. xtw.WriteStartAttribute ("baz", null);
  643. xtw.WriteEndElement ();
  644. AssertEquals ("<foo /><bar baz='' />", StringWriterText);
  645. }
  646. [Test]
  647. public void FullEndElement ()
  648. {
  649. xtw.WriteStartElement ("foo");
  650. xtw.WriteFullEndElement ();
  651. AssertEquals ("<foo></foo>", StringWriterText);
  652. xtw.WriteStartElement ("bar");
  653. xtw.WriteAttributeString ("foo", "bar");
  654. xtw.WriteFullEndElement ();
  655. AssertEquals ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
  656. xtw.WriteStartElement ("baz");
  657. xtw.WriteStartAttribute ("bar", null);
  658. xtw.WriteFullEndElement ();
  659. AssertEquals ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
  660. }
  661. [Test]
  662. public void WriteQualifiedName ()
  663. {
  664. xtw.WriteStartElement (null, "test", null);
  665. xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
  666. xtw.WriteQualifiedName ("bob", "http://localhost/");
  667. xtw.WriteEndElement ();
  668. AssertEquals ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
  669. }
  670. [Test]
  671. public void WriteQualifiedNameNonDeclaredAttribute ()
  672. {
  673. xtw.WriteStartElement ("foo");
  674. xtw.WriteStartAttribute ("a", "");
  675. xtw.WriteQualifiedName ("attr", "urn:a");
  676. xtw.WriteWhitespace (" ");
  677. xtw.WriteQualifiedName ("attr", "urn:b");
  678. xtw.WriteEndAttribute ();
  679. xtw.WriteEndElement ();
  680. string xml = sw.ToString ();
  681. Assert ("foo", xml.IndexOf ("<foo ") >= 0);
  682. Assert ("qnames", xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0);
  683. Assert ("xmlns:a", xml.IndexOf (" xmlns:d1p1='urn:a'") > 0);
  684. Assert ("xmlns:b", xml.IndexOf (" xmlns:d1p2='urn:b'") > 0);
  685. }
  686. [Test]
  687. [ExpectedException (typeof (ArgumentException))]
  688. public void WriteQualifiedNameNonDeclaredContent ()
  689. {
  690. xtw.WriteStartElement ("foo");
  691. xtw.WriteQualifiedName ("abc", "urn:abc");
  692. }
  693. [Test]
  694. [ExpectedException (typeof (ArgumentException))]
  695. public void WriteQualifiedNameNonNCName ()
  696. {
  697. xtw.WriteStartElement ("foo");
  698. xtw.WriteAttributeString ("xmlns", "urn:default");
  699. xtw.WriteStartElement ("child");
  700. xtw.WriteStartAttribute ("a", "");
  701. xtw.WriteQualifiedName ("x:def", "urn:def");
  702. }
  703. [Test]
  704. public void WriteRaw ()
  705. {
  706. xtw.WriteRaw("&<>\"'");
  707. AssertEquals ("&<>\"'", StringWriterText);
  708. xtw.WriteRaw(null);
  709. AssertEquals ("&<>\"'", StringWriterText);
  710. xtw.WriteRaw("");
  711. AssertEquals ("&<>\"'", StringWriterText);
  712. }
  713. [Test]
  714. public void WriteRawInvalidInAttribute ()
  715. {
  716. xtw.WriteStartElement ("foo");
  717. xtw.WriteStartAttribute ("bar", null);
  718. xtw.WriteRaw ("&<>\"'");
  719. xtw.WriteEndAttribute ();
  720. xtw.WriteEndElement ();
  721. AssertEquals ("<foo bar='&<>\"'' />", StringWriterText);
  722. }
  723. [Test]
  724. public void WriteStateTest ()
  725. {
  726. AssertEquals (WriteState.Start, xtw.WriteState);
  727. xtw.WriteStartDocument ();
  728. AssertEquals (WriteState.Prolog, xtw.WriteState);
  729. xtw.WriteStartElement ("root");
  730. AssertEquals (WriteState.Element, xtw.WriteState);
  731. xtw.WriteElementString ("foo", "bar");
  732. AssertEquals (WriteState.Content, xtw.WriteState);
  733. xtw.Close ();
  734. AssertEquals (WriteState.Closed, xtw.WriteState);
  735. }
  736. [Test]
  737. public void WriteString ()
  738. {
  739. xtw.WriteStartDocument ();
  740. try {
  741. xtw.WriteString("foo");
  742. } catch (InvalidOperationException) {}
  743. // Testing attribute values
  744. xtw.WriteStartElement ("foo");
  745. xtw.WriteAttributeString ("bar", "&<>");
  746. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo bar='&amp;&lt;&gt;'", StringWriterText);
  747. }
  748. [Test]
  749. public void WriteAttributeStringSingleQuoteChar()
  750. {
  751. // When QuoteChar is single quote then replaces single quotes within attributes
  752. // but not double quotes.
  753. xtw.WriteStartElement ("foo");
  754. xtw.WriteAttributeString ("bar", "\"baz\"");
  755. xtw.WriteAttributeString ("quux", "'baz'");
  756. AssertEquals ("<foo bar='\"baz\"' quux='&apos;baz&apos;'", StringWriterText);
  757. }
  758. [Test]
  759. public void WriteAttributeStringDoubleQuoteChar()
  760. {
  761. // When QuoteChar is double quote then replaces double quotes within attributes
  762. // but not single quotes.
  763. xtw.QuoteChar = '"';
  764. xtw.WriteStartElement ("foo");
  765. xtw.WriteAttributeString ("bar", "\"baz\"");
  766. xtw.WriteAttributeString ("quux", "'baz'");
  767. AssertEquals ("<foo bar=\"&quot;baz&quot;\" quux=\"'baz'\"", StringWriterText);
  768. }
  769. [Test]
  770. public void WriteStringWithEntities()
  771. {
  772. // Testing element values
  773. xtw.QuoteChar = '\'';
  774. xtw.WriteElementString ("foo", "&<>\"'");
  775. AssertEquals ("<foo>&amp;&lt;&gt;\"'</foo>", StringWriterText);
  776. }
  777. [Test]
  778. public void XmlLang ()
  779. {
  780. AssertNull (xtw.XmlLang);
  781. xtw.WriteStartElement ("foo");
  782. xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
  783. AssertEquals ("langfoo", xtw.XmlLang);
  784. AssertEquals ("<foo xml:lang='langfoo'", StringWriterText);
  785. xtw.WriteAttributeString ("boo", "yah");
  786. AssertEquals ("langfoo", xtw.XmlLang);
  787. AssertEquals ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
  788. xtw.WriteElementString("bar", "baz");
  789. AssertEquals ("langfoo", xtw.XmlLang);
  790. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
  791. xtw.WriteString("baz");
  792. AssertEquals ("langfoo", xtw.XmlLang);
  793. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
  794. xtw.WriteStartElement ("quux");
  795. xtw.WriteStartAttribute ("xml", "lang", null);
  796. AssertEquals ("langfoo", xtw.XmlLang);
  797. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  798. xtw.WriteString("langbar");
  799. // Commented out there: it is implementation-dependent.
  800. // and incompatible between .NET 1.0 and 1.1
  801. // AssertEquals ("langfoo", xtw.XmlLang);
  802. // AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
  803. xtw.WriteEndAttribute ();
  804. // Commented out there: it is implementation-dependent.
  805. // and incompatible between .NET 1.0 and 1.1
  806. // AssertEquals ("langbar", xtw.XmlLang);
  807. // AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
  808. // check if xml:lang repeats output even if same as current scope.
  809. xtw.WriteStartElement ("joe");
  810. xtw.WriteAttributeString ("xml", "lang", null, "langbar");
  811. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
  812. xtw.WriteElementString ("quuux", "squonk");
  813. AssertEquals ("langbar", xtw.XmlLang);
  814. AssertEquals ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
  815. xtw.WriteEndElement ();
  816. xtw.WriteEndElement ();
  817. AssertEquals ("langfoo", xtw.XmlLang);
  818. 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);
  819. xtw.WriteEndElement ();
  820. AssertNull (xtw.XmlLang);
  821. 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);
  822. xtw.Close ();
  823. AssertNull (xtw.XmlLang);
  824. }
  825. // TODO: test operational aspects
  826. [Test]
  827. public void XmlSpaceTest ()
  828. {
  829. xtw.WriteStartElement ("foo");
  830. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  831. xtw.WriteStartElement ("bar");
  832. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  833. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  834. AssertEquals ("<foo><bar xml:space='preserve'", StringWriterText);
  835. xtw.WriteStartElement ("baz");
  836. xtw.WriteAttributeString ("xml", "space", null, "preserve");
  837. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  838. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText);
  839. xtw.WriteStartElement ("quux");
  840. xtw.WriteStartAttribute ("xml", "space", null);
  841. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  842. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  843. // Commented out there: it is implementation-dependent
  844. // and incompatible between .NET 1.0 and 1.1
  845. xtw.WriteString ("default");
  846. // AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  847. // AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
  848. xtw.WriteEndAttribute ();
  849. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  850. AssertEquals ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText);
  851. xtw.WriteEndElement ();
  852. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  853. xtw.WriteEndElement ();
  854. AssertEquals (XmlSpace.Preserve, xtw.XmlSpace);
  855. xtw.WriteEndElement ();
  856. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  857. xtw.WriteStartElement ("quux");
  858. try {
  859. xtw.WriteAttributeString ("xml", "space", null, "bubba");
  860. } catch (ArgumentException) {}
  861. try {
  862. xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
  863. } catch (ArgumentException) {}
  864. try {
  865. xtw.WriteAttributeString ("xml", "space", null, "Preserve");
  866. } catch (ArgumentException) {}
  867. try {
  868. xtw.WriteAttributeString ("xml", "space", null, "Default");
  869. } catch (ArgumentException) {}
  870. try {
  871. xtw.WriteWhitespace ("x");
  872. } catch (ArgumentException) { }
  873. }
  874. [Test]
  875. public void XmlSpaceRaw ()
  876. {
  877. xtw.WriteStartElement ("foo");
  878. xtw.WriteStartAttribute ("xml", "space", null);
  879. AssertEquals (XmlSpace.None, xtw.XmlSpace);
  880. AssertEquals ("<foo xml:space='", StringWriterText);
  881. xtw.WriteString ("default");
  882. // Commented out there: it is implementation-dependent
  883. // and incompatible between .NET 1.0 and 1.1
  884. // AssertEquals (XmlSpace.None, xtw.XmlSpace);
  885. // AssertEquals ("<foo xml:space='", StringWriterText);
  886. xtw.WriteEndAttribute ();
  887. AssertEquals (XmlSpace.Default, xtw.XmlSpace);
  888. AssertEquals ("<foo xml:space='default'", StringWriterText);
  889. }
  890. [Test]
  891. public void WriteAttributes ()
  892. {
  893. XmlDocument doc = new XmlDocument();
  894. StringWriter sw = new StringWriter();
  895. XmlWriter wr = new XmlTextWriter(sw);
  896. StringBuilder sb = sw.GetStringBuilder();
  897. XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  898. 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);
  899. xtr.Read(); // read XMLDecl
  900. wr.WriteAttributes(xtr, false);
  901. // This method don't always have to take this double-quoted style...
  902. AssertEquals("#WriteAttributes.XmlDecl.1", "version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString().Trim());
  903. sb.Remove(0, sb.Length); // init
  904. ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
  905. xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
  906. xtr.Read(); // read XMLDecl
  907. AssertEquals (XmlNodeType.XmlDeclaration, xtr.NodeType);
  908. sw = new StringWriter ();
  909. wr = new XmlTextWriter (sw);
  910. // This block raises an error on MS.NET 1.0.
  911. wr.WriteAttributes(xtr, false);
  912. // This method don't always have to take this double-quoted style...
  913. AssertEquals("#WriteAttributes.XmlDecl.2", "version=\"1.0\" standalone=\"no\"", sw.ToString().Trim());
  914. sw = new StringWriter ();
  915. wr = new XmlTextWriter (sw);
  916. sb.Remove(0, sb.Length); // init
  917. xtr.Read(); // read root
  918. AssertEquals (XmlNodeType.Element, xtr.NodeType);
  919. wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
  920. wr.WriteAttributes(xtr, false);
  921. wr.WriteEndElement();
  922. wr.Close();
  923. // This method don't always have to take this double-quoted style...
  924. AssertEquals("#WriteAttributes.Element", "<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString().Trim());
  925. xtr.Close ();
  926. }
  927. [Test]
  928. public void WriteWhitespace ()
  929. {
  930. xtw.WriteStartElement ("a");
  931. xtw.WriteWhitespace ("\n\t");
  932. xtw.WriteStartElement ("b");
  933. xtw.WriteWhitespace ("\n\t");
  934. xtw.WriteEndElement ();
  935. xtw.WriteWhitespace ("\n");
  936. xtw.WriteEndElement ();
  937. xtw.WriteWhitespace ("\n");
  938. xtw.Flush ();
  939. AssertEquals ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
  940. }
  941. [Test]
  942. public void FlushDoesntCloseTag ()
  943. {
  944. xtw.WriteStartElement ("foo");
  945. xtw.WriteAttributeString ("bar", "baz");
  946. xtw.Flush ();
  947. AssertEquals ("<foo bar='baz'", StringWriterText);
  948. }
  949. [Test]
  950. public void WriteWhitespaceClosesTag ()
  951. {
  952. xtw.WriteStartElement ("foo");
  953. xtw.WriteAttributeString ("bar", "baz");
  954. xtw.WriteWhitespace (" ");
  955. AssertEquals ("<foo bar='baz'> ", StringWriterText);
  956. }
  957. [Test]
  958. public void DontOutputMultipleXmlns ()
  959. {
  960. XmlDocument doc = new XmlDocument();
  961. doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
  962. XmlDocument doc2 = new XmlDocument();
  963. doc2.LoadXml(doc.InnerXml);
  964. AssertEquals ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
  965. doc2.OuterXml);
  966. }
  967. [Test]
  968. public void DontOutputNonDeclaredXmlns ()
  969. {
  970. string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
  971. XmlDocument doc = new XmlDocument();
  972. doc.LoadXml(xml);
  973. XmlDocument doc2 = new XmlDocument();
  974. doc2.LoadXml(doc.InnerXml);
  975. AssertEquals (xml.Replace ('\'', '"'), doc2.OuterXml);
  976. }
  977. [Test]
  978. public void DontOutputRemovalDefaultNSDeclaration ()
  979. {
  980. xtw.WriteStartDocument ();
  981. xtw.WriteStartElement ("foo");
  982. xtw.WriteAttributeString ("xmlns", "probe");
  983. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  984. xtw.WriteStartElement ("b");
  985. AssertEquals (String.Empty, xtw.LookupPrefix ("probe"));
  986. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  987. xtw.WriteEndElement (); // b2
  988. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  989. xtw.WriteEndElement (); // b2
  990. xtw.WriteEndElement (); // b
  991. xtw.WriteEndElement (); // foo
  992. xtw.WriteEndDocument ();
  993. xtw.Close ();
  994. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText);
  995. }
  996. [Test]
  997. public void DontOutputRemovalDefaultNSDeclaration2 ()
  998. {
  999. xtw.WriteStartDocument ();
  1000. // IMPORTANT DIFFERENCE!! ns = "", not null
  1001. xtw.WriteStartElement ("foo", "");
  1002. xtw.WriteAttributeString ("xmlns", "probe");
  1003. AssertNull (xtw.LookupPrefix ("probe"));
  1004. xtw.WriteStartElement ("b");
  1005. AssertNull (xtw.LookupPrefix ("probe"));
  1006. xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
  1007. xtw.WriteEndElement (); // b2
  1008. xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
  1009. xtw.WriteEndElement (); // b2
  1010. xtw.WriteEndElement (); // b
  1011. xtw.WriteEndElement (); // foo
  1012. xtw.WriteEndDocument ();
  1013. xtw.Close ();
  1014. AssertEquals ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText);
  1015. }
  1016. [Test]
  1017. public void DoOutputRemovalDefaultNSDeclaration ()
  1018. {
  1019. xtw.WriteStartElement ("docelem", "a-namespace");
  1020. XmlDocument doc = new XmlDocument ();
  1021. doc.CreateElement ("hola").WriteTo (xtw);
  1022. // This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
  1023. xtw.WriteEndElement ();
  1024. xtw.Close ();
  1025. AssertEquals ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
  1026. }
  1027. [Test]
  1028. public void WriteAttributeTakePrecedenceOnXmlns ()
  1029. {
  1030. xtw.WriteStartElement ("root", "urn:foo");
  1031. xtw.WriteAttributeString ("xmlns", "urn:bar");
  1032. xtw.WriteEndElement ();
  1033. xtw.Close ();
  1034. AssertEquals ("<root xmlns='urn:bar' />", StringWriterText);
  1035. }
  1036. [Test]
  1037. [ExpectedException (typeof (ArgumentException))]
  1038. public void LookupPrefixNull ()
  1039. {
  1040. xtw.LookupPrefix (null);
  1041. }
  1042. [Test]
  1043. [ExpectedException (typeof (ArgumentException))]
  1044. public void LookupPrefixEmpty ()
  1045. {
  1046. xtw.LookupPrefix (String.Empty);
  1047. }
  1048. [Test]
  1049. public void LookupPrefixIgnoresXmlnsAttribute ()
  1050. {
  1051. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1052. xtw.WriteStartElement ("root");
  1053. AssertNull (xtw.LookupPrefix ("urn:foo"));
  1054. xtw.WriteAttributeString ("xmlns", "urn:foo");
  1055. // Surprisingly to say, it is ignored!!
  1056. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1057. xtw.WriteStartElement ("hoge");
  1058. // (still after flushing previous start element.)
  1059. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1060. xtw.WriteStartElement ("fuga", "urn:foo");
  1061. // Is this testing on the correct way? Yes, here it is.
  1062. AssertEquals (String.Empty, xtw.LookupPrefix ("urn:foo"));
  1063. }
  1064. [Test]
  1065. public void WriteInvalidNames ()
  1066. {
  1067. xtw.WriteStartElement ("foo<>");
  1068. xtw.WriteAttributeString ("ho<>ge", "value");
  1069. }
  1070. [Test]
  1071. [ExpectedException (typeof (ArgumentException))]
  1072. public void AttributeWriteStartAttributePrefixWithoutNS ()
  1073. {
  1074. xtw.WriteStartAttribute ("some", "foo", null);
  1075. }
  1076. [Test]
  1077. public void AttributeWriteStartAttributeXmlnsNullNS ()
  1078. {
  1079. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1080. }
  1081. [Test]
  1082. [ExpectedException (typeof (ArgumentException))]
  1083. public void AttributeWriteEndAttributeXmlnsNullNs ()
  1084. {
  1085. // Compare with the test AttributeWriteStartAttributeXmlnsNullNS().
  1086. xtw.WriteStartAttribute ("xmlns", "foo", null);
  1087. xtw.WriteEndAttribute ();
  1088. }
  1089. [Test]
  1090. [ExpectedException (typeof (ArgumentException))]
  1091. public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
  1092. {
  1093. xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
  1094. }
  1095. [Test]
  1096. [ExpectedException (typeof (ArgumentException))]
  1097. public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
  1098. {
  1099. xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
  1100. }
  1101. [Test]
  1102. public void WriteRawProceedToProlog ()
  1103. {
  1104. XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
  1105. xtw.WriteRaw ("");
  1106. AssertEquals (WriteState.Prolog, xtw.WriteState);
  1107. }
  1108. [Test]
  1109. public void Indent ()
  1110. {
  1111. XmlDocument doc = new XmlDocument ();
  1112. doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
  1113. StringWriter sw = new StringWriter ();
  1114. sw.NewLine = "_";
  1115. XmlTextWriter xtw = new XmlTextWriter (sw);
  1116. xtw.Formatting = Formatting.Indented;
  1117. doc.WriteContentTo (xtw);
  1118. AssertEquals (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
  1119. }
  1120. [Test]
  1121. public void Indent2 ()
  1122. {
  1123. StringWriter sw = new StringWriter ();
  1124. XmlTextWriter xtw = new XmlTextWriter (sw);
  1125. xtw.Formatting = Formatting.Indented;
  1126. // sadly, this silly usage of this method is actually
  1127. // used in WriteNode() in MS.NET.
  1128. xtw.WriteProcessingInstruction ("xml",
  1129. "version=\"1.0\"");
  1130. xtw.WriteComment ("sample XML fragment");
  1131. AssertEquals (@"<?xml version=""1.0""?>
  1132. <!--sample XML fragment-->", sw.ToString ().Replace ("\r\n", "\n"));
  1133. }
  1134. [Test]
  1135. public void CloseTwice ()
  1136. {
  1137. StringWriter sw = new StringWriter ();
  1138. XmlTextWriter writer = new XmlTextWriter (sw);
  1139. writer.Close ();
  1140. // should not result in an exception
  1141. writer.Close ();
  1142. }
  1143. [Test]
  1144. public void WriteRawWriteString ()
  1145. {
  1146. // WriteRaw () -> WriteString ().
  1147. xtw.WriteRaw ("");
  1148. xtw.WriteString ("foo");
  1149. AssertEquals (WriteState.Content, xtw.WriteState);
  1150. }
  1151. [Test]
  1152. public void LookupOverridenPrefix ()
  1153. {
  1154. xtw.WriteStartElement ("out");
  1155. xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
  1156. xtw.WriteStartElement ("baz", "foo", "abc");
  1157. AssertNull (xtw.LookupPrefix ("xyz"));
  1158. }
  1159. [Test]
  1160. public void DuplicatingNamespaceMappingInAttributes ()
  1161. {
  1162. xtw.WriteStartElement ("out");
  1163. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1164. xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
  1165. xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
  1166. xtw.WriteStartElement ("out");
  1167. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1168. xtw.WriteStartElement ("out");
  1169. xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
  1170. xtw.WriteEndElement ();
  1171. xtw.WriteEndElement ();
  1172. xtw.WriteEndElement ();
  1173. string xml = sw.ToString ();
  1174. Assert ("p:foo", xml.IndexOf ("p:foo='xyz'") > 0);
  1175. Assert ("d1p1:bar", xml.IndexOf ("d1p1:bar='xyz'") > 0);
  1176. Assert ("d1p1:baz", xml.IndexOf ("d1p2:baz='xyz'") > 0);
  1177. Assert ("xmlns:d1p2", xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0);
  1178. Assert ("xmlns:d1p1", xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0);
  1179. Assert ("xmlns:p", xml.IndexOf ("xmlns:p='urn:foo'") > 0);
  1180. Assert ("remaining", xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0);
  1181. }
  1182. [Test]
  1183. public void WriteXmlSpaceIgnoresNS ()
  1184. {
  1185. xtw.WriteStartElement ("root");
  1186. xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
  1187. xtw.WriteEndElement ();
  1188. AssertEquals ("<root xml:space='preserve' />", sw.ToString ());
  1189. }
  1190. [Test] // bug #75546
  1191. public void WriteEmptyNSQNameInAttribute ()
  1192. {
  1193. XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
  1194. xtw.WriteStartElement ("foo", "urn:goo");
  1195. xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
  1196. xtw.WriteStartAttribute ("foo", "");
  1197. xtw.WriteQualifiedName ("n1", "urn:bar");
  1198. xtw.WriteEndAttribute ();
  1199. xtw.WriteStartAttribute ("foo", "");
  1200. xtw.WriteQualifiedName ("n2", "");
  1201. xtw.WriteEndAttribute ();
  1202. }
  1203. [Test] // bug #76095
  1204. public void SurrogatePairsInWriteString ()
  1205. {
  1206. MemoryStream ms = new MemoryStream ();
  1207. XmlWriter writer = new XmlTextWriter(ms, null);
  1208. writer.WriteElementString("a", "\ud800\udf39");
  1209. writer.Close();
  1210. byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
  1211. 0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
  1212. NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
  1213. }
  1214. }
  1215. }