XmlTextWriterTests.cs 39 KB

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