XmlReaderCommonTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. //
  2. // System.Xml.XmlReaderCommonTests
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. // Note: Most of testcases are moved from XmlTextReaderTests.cs and
  9. // XmlNodeReaderTests.cs.
  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 XmlReaderTests
  20. {
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. document = new XmlDocument ();
  25. document.LoadXml (xml1);
  26. }
  27. XmlDocument document;
  28. const string xml1 = "<root attr1='value1'><child /></root>";
  29. const string xml2 = "<root><foo/><bar>test.</bar></root>";
  30. const string xml3 = "<root> test of <b>mixed</b> string.<![CDATA[ cdata string.]]></root>";
  31. const string xml4 = "<root>test of <b>mixed</b> string.</root>";
  32. XmlTextReader xtr;
  33. XmlNodeReader xnr;
  34. // copy from XmlTextReaderTests
  35. private void AssertStartDocument (XmlReader xmlReader)
  36. {
  37. Assertion.Assert (xmlReader.ReadState == ReadState.Initial);
  38. Assertion.Assert (xmlReader.NodeType == XmlNodeType.None);
  39. Assertion.Assert (xmlReader.Depth == 0);
  40. Assertion.Assert (!xmlReader.EOF);
  41. }
  42. private void AssertNode (
  43. XmlReader xmlReader,
  44. XmlNodeType nodeType,
  45. int depth,
  46. bool isEmptyElement,
  47. string name,
  48. string prefix,
  49. string localName,
  50. string namespaceURI,
  51. string value,
  52. int attributeCount)
  53. {
  54. Assertion.Assert ("Read() return value", xmlReader.Read ());
  55. Assertion.Assert ("ReadState", xmlReader.ReadState == ReadState.Interactive);
  56. Assertion.Assert ("!EOF", !xmlReader.EOF);
  57. AssertNodeValues (xmlReader, nodeType, depth, isEmptyElement, name, prefix, localName, namespaceURI, value, attributeCount);
  58. }
  59. private void AssertNodeValues (
  60. XmlReader xmlReader,
  61. XmlNodeType nodeType,
  62. int depth,
  63. bool isEmptyElement,
  64. string name,
  65. string prefix,
  66. string localName,
  67. string namespaceURI,
  68. string value,
  69. int attributeCount)
  70. {
  71. Assertion.AssertEquals ("NodeType", nodeType, xmlReader.NodeType);
  72. Assertion.AssertEquals ("Depth", depth, xmlReader.Depth);
  73. Assertion.AssertEquals ("IsEmptyElement", isEmptyElement, xmlReader.IsEmptyElement);
  74. Assertion.AssertEquals ("name", name, xmlReader.Name);
  75. Assertion.AssertEquals ("prefix", prefix, xmlReader.Prefix);
  76. Assertion.AssertEquals ("localName", localName, xmlReader.LocalName);
  77. Assertion.AssertEquals ("namespaceURI", namespaceURI, xmlReader.NamespaceURI);
  78. Assertion.AssertEquals ("hasValue", (value != String.Empty), xmlReader.HasValue);
  79. Assertion.AssertEquals ("Value", value, xmlReader.Value);
  80. Assertion.AssertEquals ("hasAttributes", attributeCount > 0, xmlReader.HasAttributes);
  81. Assertion.AssertEquals ("attributeCount", attributeCount, xmlReader.AttributeCount);
  82. }
  83. private void AssertAttribute (
  84. XmlReader xmlReader,
  85. string name,
  86. string prefix,
  87. string localName,
  88. string namespaceURI,
  89. string value)
  90. {
  91. Assertion.AssertEquals ("value", value, xmlReader [name]);
  92. Assertion.Assert (xmlReader.GetAttribute (name) == value);
  93. if (namespaceURI != String.Empty) {
  94. Assertion.Assert (xmlReader[localName, namespaceURI] == value);
  95. Assertion.Assert (xmlReader.GetAttribute (localName, namespaceURI) == value);
  96. }
  97. }
  98. private void AssertEndDocument (XmlReader xmlReader)
  99. {
  100. Assertion.Assert ("could read", !xmlReader.Read ());
  101. Assertion.AssertEquals ("NodeType is not XmlNodeType.None", XmlNodeType.None, xmlReader.NodeType);
  102. Assertion.AssertEquals ("Depth is not 0", 0, xmlReader.Depth);
  103. Assertion.AssertEquals ("ReadState is not ReadState.EndOfFile", ReadState.EndOfFile, xmlReader.ReadState);
  104. Assertion.Assert ("not EOF", xmlReader.EOF);
  105. xmlReader.Close ();
  106. Assertion.AssertEquals ("ReadState is not ReadState.Cosed", ReadState.Closed, xmlReader.ReadState);
  107. }
  108. private delegate void TestMethod (XmlReader reader);
  109. private void RunTest (string xml, TestMethod method)
  110. {
  111. xtr = new XmlTextReader (new StringReader (xml));
  112. try {
  113. method (xtr);
  114. } catch (AssertionException ex) {
  115. throw new AssertionException ("XmlTextReader failed: " + ex.Message, ex);
  116. }
  117. document.LoadXml (xml);
  118. xnr = new XmlNodeReader (document);
  119. try {
  120. method (xnr);
  121. } catch (AssertionException ex) {
  122. throw new AssertionException ("XmlNodeReader failed: " + ex.Message, ex);
  123. }
  124. }
  125. [Test]
  126. public void InitialState ()
  127. {
  128. RunTest (xml1, new TestMethod (InitialState));
  129. }
  130. private void InitialState (XmlReader reader)
  131. {
  132. Assertion.AssertEquals ("Depth", 0, reader.Depth);
  133. Assertion.AssertEquals ("EOF", false, reader.EOF);
  134. Assertion.AssertEquals ("HasValue", false, reader.HasValue);
  135. Assertion.AssertEquals ("IsEmptyElement", false, reader.IsEmptyElement);
  136. Assertion.AssertEquals ("LocalName", String.Empty, reader.LocalName);
  137. Assertion.AssertEquals ("NodeType", XmlNodeType.None, reader.NodeType);
  138. Assertion.AssertEquals ("ReadState", ReadState.Initial, reader.ReadState);
  139. }
  140. [Test]
  141. public void Read ()
  142. {
  143. RunTest (xml1, new TestMethod (Read));
  144. }
  145. public void Read (XmlReader reader)
  146. {
  147. reader.Read ();
  148. Assertion.AssertEquals ("<root>.NodeType", XmlNodeType.Element, reader.NodeType);
  149. Assertion.AssertEquals ("<root>.Name", "root", reader.Name);
  150. Assertion.AssertEquals ("<root>.ReadState", ReadState.Interactive, reader.ReadState);
  151. Assertion.AssertEquals ("<root>.Depth", 0, reader.Depth);
  152. // move to 'child'
  153. reader.Read ();
  154. Assertion.AssertEquals ("<child/>.Depth", 1, reader.Depth);
  155. Assertion.AssertEquals ("<child/>.NodeType", XmlNodeType.Element, reader.NodeType);
  156. Assertion.AssertEquals ("<child/>.Name", "child", reader.Name);
  157. reader.Read ();
  158. Assertion.AssertEquals ("</root>.Depth", 0, reader.Depth);
  159. Assertion.AssertEquals ("</root>.NodeType", XmlNodeType.EndElement, reader.NodeType);
  160. Assertion.AssertEquals ("</root>.Name", "root", reader.Name);
  161. reader.Read ();
  162. Assertion.AssertEquals ("end.EOF", true, reader.EOF);
  163. Assertion.AssertEquals ("end.NodeType", XmlNodeType.None, reader.NodeType);
  164. }
  165. [Test]
  166. public void ReadEmptyElement ()
  167. {
  168. RunTest (xml2, new TestMethod (ReadEmptyElement));
  169. }
  170. public void ReadEmptyElement (XmlReader reader)
  171. {
  172. reader.Read (); // root
  173. Assertion.AssertEquals (false, reader.IsEmptyElement);
  174. reader.Read (); // foo
  175. Assertion.AssertEquals ("foo", reader.Name);
  176. Assertion.AssertEquals (true, reader.IsEmptyElement);
  177. reader.Read (); // bar
  178. Assertion.AssertEquals ("bar", reader.Name);
  179. Assertion.AssertEquals (false, reader.IsEmptyElement);
  180. }
  181. [Test]
  182. public void ReadStringFromElement ()
  183. {
  184. RunTest (xml3, new TestMethod (ReadStringFromElement));
  185. }
  186. public void ReadStringFromElement (XmlReader reader)
  187. {
  188. // Note: ReadString() test works only when the reader is
  189. // positioned at the container element.
  190. // In case the reader is positioned at the first
  191. // character node, XmlTextReader and XmlNodeReader works
  192. // different!!
  193. reader.Read ();
  194. string s = reader.ReadString ();
  195. Assertion.AssertEquals ("readString.1.ret_val", " test of ", s);
  196. Assertion.AssertEquals ("readString.1.Name", "b", reader.Name);
  197. s = reader.ReadString ();
  198. Assertion.AssertEquals ("readString.2.ret_val", "mixed", s);
  199. Assertion.AssertEquals ("readString.2.NodeType", XmlNodeType.EndElement, reader.NodeType);
  200. s = reader.ReadString (); // never proceeds.
  201. Assertion.AssertEquals ("readString.3.ret_val", String.Empty, s);
  202. Assertion.AssertEquals ("readString.3.NodeType", XmlNodeType.EndElement, reader.NodeType);
  203. reader.Read ();
  204. Assertion.AssertEquals ("readString.4.NodeType", XmlNodeType.Text, reader.NodeType);
  205. Assertion.AssertEquals ("readString.4.Value", " string.", reader.Value);
  206. s = reader.ReadString (); // reads the same Text node.
  207. Assertion.AssertEquals ("readString.5.ret_val", " string. cdata string.", s);
  208. Assertion.AssertEquals ("readString.5.NodeType", XmlNodeType.EndElement, reader.NodeType);
  209. }
  210. [Test]
  211. public void ReadInnerXml ()
  212. {
  213. const string xml = "<root><foo>test of <b>mixed</b> string.</foo><bar /></root>";
  214. RunTest (xml, new TestMethod (ReadInnerXml));
  215. }
  216. public void ReadInnerXml (XmlReader reader)
  217. {
  218. reader.Read ();
  219. reader.Read ();
  220. Assertion.AssertEquals ("initial.ReadState", ReadState.Interactive, reader.ReadState);
  221. Assertion.AssertEquals ("initial.EOF", false, reader.EOF);
  222. Assertion.AssertEquals ("initial.NodeType", XmlNodeType.Element, reader.NodeType);
  223. string s = reader.ReadInnerXml ();
  224. Assertion.AssertEquals ("read_all", "test of <b>mixed</b> string.", s);
  225. Assertion.AssertEquals ("after.Name", "bar", reader.Name);
  226. Assertion.AssertEquals ("after.NodeType", XmlNodeType.Element, reader.NodeType);
  227. }
  228. [Test]
  229. public void EmptyElement ()
  230. {
  231. RunTest ("<foo/>", new TestMethod (EmptyElement));
  232. }
  233. public void EmptyElement (XmlReader xmlReader)
  234. {
  235. AssertStartDocument (xmlReader);
  236. AssertNode (
  237. xmlReader, // xmlReader
  238. XmlNodeType.Element, // nodeType
  239. 0, // depth
  240. true, // isEmptyElement
  241. "foo", // name
  242. String.Empty, // prefix
  243. "foo", // localName
  244. String.Empty, // namespaceURI
  245. String.Empty, // value
  246. 0 // attributeCount
  247. );
  248. AssertEndDocument (xmlReader);
  249. }
  250. [Test]
  251. public void NestedEmptyTag ()
  252. {
  253. string xml = "<foo><bar/></foo>";
  254. RunTest (xml, new TestMethod (NestedEmptyTag));
  255. }
  256. public void NestedEmptyTag (XmlReader xmlReader)
  257. {
  258. AssertStartDocument (xmlReader);
  259. AssertNode (
  260. xmlReader, // xmlReader
  261. XmlNodeType.Element, // nodeType
  262. 0, //depth
  263. false, // isEmptyElement
  264. "foo", // name
  265. String.Empty, // prefix
  266. "foo", // localName
  267. String.Empty, // namespaceURI
  268. String.Empty, // value
  269. 0 // attributeCount
  270. );
  271. AssertNode (
  272. xmlReader, // xmlReader
  273. XmlNodeType.Element, // nodeType
  274. 1, //depth
  275. true, // isEmptyElement
  276. "bar", // name
  277. String.Empty, // prefix
  278. "bar", // localName
  279. String.Empty, // namespaceURI
  280. String.Empty, // value
  281. 0 // attributeCount
  282. );
  283. AssertNode (
  284. xmlReader, // xmlReader
  285. XmlNodeType.EndElement, // nodeType
  286. 0, //depth
  287. false, // isEmptyElement
  288. "foo", // name
  289. String.Empty, // prefix
  290. "foo", // localName
  291. String.Empty, // namespaceURI
  292. String.Empty, // value
  293. 0 // attributeCount
  294. );
  295. AssertEndDocument (xmlReader);
  296. }
  297. [Test]
  298. public void NestedText ()
  299. {
  300. string xml = "<foo>bar</foo>";
  301. RunTest (xml, new TestMethod (NestedText));
  302. }
  303. public void NestedText (XmlReader xmlReader)
  304. {
  305. AssertStartDocument (xmlReader);
  306. AssertNode (
  307. xmlReader, // xmlReader
  308. XmlNodeType.Element, // nodeType
  309. 0, //depth
  310. false, // isEmptyElement
  311. "foo", // name
  312. String.Empty, // prefix
  313. "foo", // localName
  314. String.Empty, // namespaceURI
  315. String.Empty, // value
  316. 0 // attributeCount
  317. );
  318. AssertNode (
  319. xmlReader, // xmlReader
  320. XmlNodeType.Text, // nodeType
  321. 1, //depth
  322. false, // isEmptyElement
  323. String.Empty, // name
  324. String.Empty, // prefix
  325. String.Empty, // localName
  326. String.Empty, // namespaceURI
  327. "bar", // value
  328. 0 // attributeCount
  329. );
  330. AssertNode (
  331. xmlReader, // xmlReader
  332. XmlNodeType.EndElement, // nodeType
  333. 0, //depth
  334. false, // isEmptyElement
  335. "foo", // name
  336. String.Empty, // prefix
  337. "foo", // localName
  338. String.Empty, // namespaceURI
  339. String.Empty, // value
  340. 0 // attributeCount
  341. );
  342. AssertEndDocument (xmlReader);
  343. }
  344. [Test]
  345. public void EmptyElementWithTwoAttributes ()
  346. {
  347. string xml = @"<foo bar=""baz"" quux='quuux'/>";
  348. RunTest (xml, new TestMethod (EmptyElementWithTwoAttributes ));
  349. }
  350. public void EmptyElementWithTwoAttributes (XmlReader xmlReader)
  351. {
  352. AssertStartDocument (xmlReader);
  353. AssertNode (
  354. xmlReader, // xmlReader
  355. XmlNodeType.Element, // nodeType
  356. 0, //depth
  357. true, // isEmptyElement
  358. "foo", // name
  359. String.Empty, // prefix
  360. "foo", // localName
  361. String.Empty, // namespaceURI
  362. String.Empty, // value
  363. 2 // attributeCount
  364. );
  365. AssertAttribute (
  366. xmlReader, // xmlReader
  367. "bar", // name
  368. String.Empty, // prefix
  369. "bar", // localName
  370. String.Empty, // namespaceURI
  371. "baz" // value
  372. );
  373. AssertAttribute (
  374. xmlReader, // xmlReader
  375. "quux", // name
  376. String.Empty, // prefix
  377. "quux", // localName
  378. String.Empty, // namespaceURI
  379. "quuux" // value
  380. );
  381. AssertEndDocument (xmlReader);
  382. }
  383. [Test]
  384. public void ProcessingInstructionBeforeDocumentElement ()
  385. {
  386. string xml = "<?foo bar?><baz/>";
  387. RunTest (xml, new TestMethod (ProcessingInstructionBeforeDocumentElement));
  388. }
  389. public void ProcessingInstructionBeforeDocumentElement (XmlReader xmlReader)
  390. {
  391. AssertStartDocument (xmlReader);
  392. AssertNode (
  393. xmlReader, // xmlReader
  394. XmlNodeType.ProcessingInstruction, // nodeType
  395. 0, //depth
  396. false, // isEmptyElement
  397. "foo", // name
  398. String.Empty, // prefix
  399. "foo", // localName
  400. String.Empty, // namespaceURI
  401. "bar", // value
  402. 0 // attributeCount
  403. );
  404. AssertNode (
  405. xmlReader, // xmlReader
  406. XmlNodeType.Element, // nodeType
  407. 0, //depth
  408. true, // isEmptyElement
  409. "baz", // name
  410. String.Empty, // prefix
  411. "baz", // localName
  412. String.Empty, // namespaceURI
  413. String.Empty, // value
  414. 0 // attributeCount
  415. );
  416. AssertEndDocument (xmlReader);
  417. }
  418. [Test]
  419. public void CommentBeforeDocumentElement ()
  420. {
  421. string xml = "<!--foo--><bar/>";
  422. RunTest (xml, new TestMethod (CommentBeforeDocumentElement));
  423. }
  424. public void CommentBeforeDocumentElement (XmlReader xmlReader)
  425. {
  426. AssertStartDocument (xmlReader);
  427. AssertNode (
  428. xmlReader, // xmlReader
  429. XmlNodeType.Comment, // nodeType
  430. 0, //depth
  431. false, // isEmptyElement
  432. String.Empty, // name
  433. String.Empty, // prefix
  434. String.Empty, // localName
  435. String.Empty, // namespaceURI
  436. "foo", // value
  437. 0 // attributeCount
  438. );
  439. AssertNode (
  440. xmlReader, // xmlReader
  441. XmlNodeType.Element, // nodeType
  442. 0, //depth
  443. true, // isEmptyElement
  444. "bar", // name
  445. String.Empty, // prefix
  446. "bar", // localName
  447. String.Empty, // namespaceURI
  448. String.Empty, // value
  449. 0 // attributeCount
  450. );
  451. AssertEndDocument (xmlReader);
  452. }
  453. [Test]
  454. public void PredefinedEntities ()
  455. {
  456. string xml = "<foo>&lt;&gt;&amp;&apos;&quot;</foo>";
  457. RunTest (xml, new TestMethod (PredefinedEntities));
  458. }
  459. public void PredefinedEntities (XmlReader xmlReader)
  460. {
  461. AssertStartDocument (xmlReader);
  462. AssertNode (
  463. xmlReader, // xmlReader
  464. XmlNodeType.Element, // nodeType
  465. 0, //depth
  466. false, // isEmptyElement
  467. "foo", // name
  468. String.Empty, // prefix
  469. "foo", // localName
  470. String.Empty, // namespaceURI
  471. String.Empty, // value
  472. 0 // attributeCount
  473. );
  474. AssertNode (
  475. xmlReader, // xmlReader
  476. XmlNodeType.Text, // nodeType
  477. 1, //depth
  478. false, // isEmptyElement
  479. String.Empty, // name
  480. String.Empty, // prefix
  481. String.Empty, // localName
  482. String.Empty, // namespaceURI
  483. "<>&'\"", // value
  484. 0 // attributeCount
  485. );
  486. AssertNode (
  487. xmlReader, // xmlReader
  488. XmlNodeType.EndElement, // nodeType
  489. 0, //depth
  490. false, // isEmptyElement
  491. "foo", // name
  492. String.Empty, // prefix
  493. "foo", // localName
  494. String.Empty, // namespaceURI
  495. String.Empty, // value
  496. 0 // attributeCount
  497. );
  498. AssertEndDocument (xmlReader);
  499. }
  500. [Test]
  501. public void EntityReference ()
  502. {
  503. string xml = "<foo>&bar;</foo>";
  504. RunTest (xml, new TestMethod (EntityReference));
  505. }
  506. public void EntityReference (XmlReader xmlReader)
  507. {
  508. AssertStartDocument (xmlReader);
  509. AssertNode (
  510. xmlReader, // xmlReader
  511. XmlNodeType.Element, // nodeType
  512. 0, //depth
  513. false, // isEmptyElement
  514. "foo", // name
  515. String.Empty, // prefix
  516. "foo", // localName
  517. String.Empty, // namespaceURI
  518. String.Empty, // value
  519. 0 // attributeCount
  520. );
  521. AssertNode (
  522. xmlReader, // xmlReader
  523. XmlNodeType.EntityReference, // nodeType
  524. 1, //depth
  525. false, // isEmptyElement
  526. "bar", // name
  527. String.Empty, // prefix
  528. "bar", // localName
  529. String.Empty, // namespaceURI
  530. String.Empty, // value
  531. 0 // attributeCount
  532. );
  533. AssertNode (
  534. xmlReader, // xmlReader
  535. XmlNodeType.EndElement, // nodeType
  536. 0, //depth
  537. false, // isEmptyElement
  538. "foo", // name
  539. String.Empty, // prefix
  540. "foo", // localName
  541. String.Empty, // namespaceURI
  542. String.Empty, // value
  543. 0 // attributeCount
  544. );
  545. AssertEndDocument (xmlReader);
  546. }
  547. [Test]
  548. public void EntityReferenceInsideText ()
  549. {
  550. string xml = "<foo>bar&baz;quux</foo>";
  551. RunTest (xml, new TestMethod (EntityReferenceInsideText));
  552. }
  553. public void EntityReferenceInsideText (XmlReader xmlReader)
  554. {
  555. AssertStartDocument (xmlReader);
  556. AssertNode (
  557. xmlReader, // xmlReader
  558. XmlNodeType.Element, // nodeType
  559. 0, //depth
  560. false, // isEmptyElement
  561. "foo", // name
  562. String.Empty, // prefix
  563. "foo", // localName
  564. String.Empty, // namespaceURI
  565. String.Empty, // value
  566. 0 // attributeCount
  567. );
  568. AssertNode (
  569. xmlReader, // xmlReader
  570. XmlNodeType.Text, // nodeType
  571. 1, //depth
  572. false, // isEmptyElement
  573. String.Empty, // name
  574. String.Empty, // prefix
  575. String.Empty, // localName
  576. String.Empty, // namespaceURI
  577. "bar", // value
  578. 0 // attributeCount
  579. );
  580. AssertNode (
  581. xmlReader, // xmlReader
  582. XmlNodeType.EntityReference, // nodeType
  583. 1, //depth
  584. false, // isEmptyElement
  585. "baz", // name
  586. String.Empty, // prefix
  587. "baz", // localName
  588. String.Empty, // namespaceURI
  589. String.Empty, // value
  590. 0 // attributeCount
  591. );
  592. AssertNode (
  593. xmlReader, // xmlReader
  594. XmlNodeType.Text, // nodeType
  595. 1, //depth
  596. false, // isEmptyElement
  597. String.Empty, // name
  598. String.Empty, // prefix
  599. String.Empty, // localName
  600. String.Empty, // namespaceURI
  601. "quux", // value
  602. 0 // attributeCount
  603. );
  604. AssertNode (
  605. xmlReader, // xmlReader
  606. XmlNodeType.EndElement, // nodeType
  607. 0, //depth
  608. false, // isEmptyElement
  609. "foo", // name
  610. String.Empty, // prefix
  611. "foo", // localName
  612. String.Empty, // namespaceURI
  613. String.Empty, // value
  614. 0 // attributeCount
  615. );
  616. AssertEndDocument (xmlReader);
  617. }
  618. [Test]
  619. public void CharacterReferences ()
  620. {
  621. string xml = "<foo>&#70;&#x4F;&#x4f;</foo>";
  622. RunTest (xml, new TestMethod (CharacterReferences));
  623. }
  624. public void CharacterReferences (XmlReader xmlReader)
  625. {
  626. AssertStartDocument (xmlReader);
  627. AssertNode (
  628. xmlReader, // xmlReader
  629. XmlNodeType.Element, // nodeType
  630. 0, //depth
  631. false, // isEmptyElement
  632. "foo", // name
  633. String.Empty, // prefix
  634. "foo", // localName
  635. String.Empty, // namespaceURI
  636. String.Empty, // value
  637. 0 // attributeCount
  638. );
  639. AssertNode (
  640. xmlReader, // xmlReader
  641. XmlNodeType.Text, // nodeType
  642. 1, //depth
  643. false, // isEmptyElement
  644. String.Empty, // name
  645. String.Empty, // prefix
  646. String.Empty, // localName
  647. String.Empty, // namespaceURI
  648. "FOO", // value
  649. 0 // attributeCount
  650. );
  651. AssertNode (
  652. xmlReader, // xmlReader
  653. XmlNodeType.EndElement, // nodeType
  654. 0, //depth
  655. false, // isEmptyElement
  656. "foo", // name
  657. String.Empty, // prefix
  658. "foo", // localName
  659. String.Empty, // namespaceURI
  660. String.Empty, // value
  661. 0 // attributeCount
  662. );
  663. AssertEndDocument (xmlReader);
  664. }
  665. [Test]
  666. public void PredefinedEntitiesInAttribute ()
  667. {
  668. string xml = "<foo bar='&lt;&gt;&amp;&apos;&quot;'/>";
  669. RunTest (xml, new TestMethod (PredefinedEntitiesInAttribute ));
  670. }
  671. public void PredefinedEntitiesInAttribute (XmlReader xmlReader)
  672. {
  673. AssertStartDocument (xmlReader);
  674. AssertNode (
  675. xmlReader, // xmlReader
  676. XmlNodeType.Element, // nodeType
  677. 0, //depth
  678. true, // isEmptyElement
  679. "foo", // name
  680. String.Empty, // prefix
  681. "foo", // localName
  682. String.Empty, // namespaceURI
  683. String.Empty, // value
  684. 1 // attributeCount
  685. );
  686. AssertAttribute (
  687. xmlReader, // xmlReader
  688. "bar", // name
  689. String.Empty, // prefix
  690. "bar", // localName
  691. String.Empty, // namespaceURI
  692. "<>&'\"" // value
  693. );
  694. AssertEndDocument (xmlReader);
  695. }
  696. [Test]
  697. public void CharacterReferencesInAttribute ()
  698. {
  699. string xml = "<foo bar='&#70;&#x4F;&#x4f;'/>";
  700. RunTest (xml, new TestMethod (CharacterReferencesInAttribute));
  701. }
  702. public void CharacterReferencesInAttribute (XmlReader xmlReader)
  703. {
  704. AssertStartDocument (xmlReader);
  705. AssertNode (
  706. xmlReader, // xmlReader
  707. XmlNodeType.Element, // nodeType
  708. 0, //depth
  709. true, // isEmptyElement
  710. "foo", // name
  711. String.Empty, // prefix
  712. "foo", // localName
  713. String.Empty, // namespaceURI
  714. String.Empty, // value
  715. 1 // attributeCount
  716. );
  717. AssertAttribute (
  718. xmlReader, // xmlReader
  719. "bar", // name
  720. String.Empty, // prefix
  721. "bar", // localName
  722. String.Empty, // namespaceURI
  723. "FOO" // value
  724. );
  725. AssertEndDocument (xmlReader);
  726. }
  727. [Test]
  728. public void CDATA ()
  729. {
  730. string xml = "<foo><![CDATA[<>&]]></foo>";
  731. RunTest (xml, new TestMethod (CDATA));
  732. }
  733. public void CDATA (XmlReader xmlReader)
  734. {
  735. AssertStartDocument (xmlReader);
  736. AssertNode (
  737. xmlReader, // xmlReader
  738. XmlNodeType.Element, // nodeType
  739. 0, //depth
  740. false, // isEmptyElement
  741. "foo", // name
  742. String.Empty, // prefix
  743. "foo", // localName
  744. String.Empty, // namespaceURI
  745. String.Empty, // value
  746. 0 // attributeCount
  747. );
  748. AssertNode (
  749. xmlReader, // xmlReader
  750. XmlNodeType.CDATA, // nodeType
  751. 1, //depth
  752. false, // isEmptyElement
  753. String.Empty, // name
  754. String.Empty, // prefix
  755. String.Empty, // localName
  756. String.Empty, // namespaceURI
  757. "<>&", // value
  758. 0 // attributeCount
  759. );
  760. AssertNode (
  761. xmlReader, // xmlReader
  762. XmlNodeType.EndElement, // nodeType
  763. 0, //depth
  764. false, // isEmptyElement
  765. "foo", // name
  766. String.Empty, // prefix
  767. "foo", // localName
  768. String.Empty, // namespaceURI
  769. String.Empty, // value
  770. 0 // attributeCount
  771. );
  772. AssertEndDocument (xmlReader);
  773. }
  774. [Test]
  775. public void EmptyElementInDefaultNamespace ()
  776. {
  777. string xml = @"<foo xmlns='http://foo/' />";
  778. RunTest (xml, new TestMethod (EmptyElementInDefaultNamespace));
  779. }
  780. public void EmptyElementInDefaultNamespace (XmlReader xmlReader)
  781. {
  782. AssertStartDocument (xmlReader);
  783. AssertNode (
  784. xmlReader, // xmlReader
  785. XmlNodeType.Element, // nodeType
  786. 0, // depth
  787. true, // isEmptyElement
  788. "foo", // name
  789. String.Empty, // prefix
  790. "foo", // localName
  791. "http://foo/", // namespaceURI
  792. String.Empty, // value
  793. 1 // attributeCount
  794. );
  795. AssertAttribute (
  796. xmlReader, // xmlReader
  797. "xmlns", // name
  798. String.Empty, // prefix
  799. "xmlns", // localName
  800. "http://www.w3.org/2000/xmlns/", // namespaceURI
  801. "http://foo/" // value
  802. );
  803. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace (String.Empty));
  804. AssertEndDocument (xmlReader);
  805. }
  806. [Test]
  807. public void ChildElementInNamespace ()
  808. {
  809. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz:quux xmlns:baz='http://baz/' /></foo:bar>";
  810. RunTest (xml, new TestMethod (ChildElementInNamespace));
  811. }
  812. public void ChildElementInNamespace (XmlReader xmlReader)
  813. {
  814. AssertStartDocument (xmlReader);
  815. AssertNode (
  816. xmlReader, // xmlReader
  817. XmlNodeType.Element, // nodeType
  818. 0, // depth
  819. false, // isEmptyElement
  820. "foo:bar", // name
  821. "foo", // prefix
  822. "bar", // localName
  823. "http://foo/", // namespaceURI
  824. String.Empty, // value
  825. 1 // attributeCount
  826. );
  827. AssertAttribute (
  828. xmlReader, // xmlReader
  829. "xmlns:foo", // name
  830. "xmlns", // prefix
  831. "foo", // localName
  832. "http://www.w3.org/2000/xmlns/", // namespaceURI
  833. "http://foo/" // value
  834. );
  835. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  836. AssertNode (
  837. xmlReader, // xmlReader
  838. XmlNodeType.Element, // nodeType
  839. 1, // depth
  840. true, // isEmptyElement
  841. "baz:quux", // name
  842. "baz", // prefix
  843. "quux", // localName
  844. "http://baz/", // namespaceURI
  845. String.Empty, // value
  846. 1 // attributeCount
  847. );
  848. AssertAttribute (
  849. xmlReader, // xmlReader
  850. "xmlns:baz", // name
  851. "xmlns", // prefix
  852. "baz", // localName
  853. "http://www.w3.org/2000/xmlns/", // namespaceURI
  854. "http://baz/" // value
  855. );
  856. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  857. Assertion.AssertEquals ("http://baz/", xmlReader.LookupNamespace ("baz"));
  858. AssertNode (
  859. xmlReader, // xmlReader
  860. XmlNodeType.EndElement, // nodeType
  861. 0, // depth
  862. false, // isEmptyElement
  863. "foo:bar", // name
  864. "foo", // prefix
  865. "bar", // localName
  866. "http://foo/", // namespaceURI
  867. String.Empty, // value
  868. 0 // attributeCount
  869. );
  870. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  871. Assertion.AssertNull (xmlReader.LookupNamespace ("baz"));
  872. AssertEndDocument (xmlReader);
  873. }
  874. [Test]
  875. public void ChildElementInDefaultNamespace ()
  876. {
  877. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz xmlns='http://baz/' /></foo:bar>";
  878. RunTest (xml, new TestMethod (ChildElementInDefaultNamespace));
  879. }
  880. public void ChildElementInDefaultNamespace (XmlReader xmlReader)
  881. {
  882. AssertStartDocument (xmlReader);
  883. AssertNode (
  884. xmlReader, // xmlReader
  885. XmlNodeType.Element, // nodeType
  886. 0, // depth
  887. false, // isEmptyElement
  888. "foo:bar", // name
  889. "foo", // prefix
  890. "bar", // localName
  891. "http://foo/", // namespaceURI
  892. String.Empty, // value
  893. 1 // attributeCount
  894. );
  895. AssertAttribute (
  896. xmlReader, // xmlReader
  897. "xmlns:foo", // name
  898. "xmlns", // prefix
  899. "foo", // localName
  900. "http://www.w3.org/2000/xmlns/", // namespaceURI
  901. "http://foo/" // value
  902. );
  903. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  904. AssertNode (
  905. xmlReader, // xmlReader
  906. XmlNodeType.Element, // nodeType
  907. 1, // depth
  908. true, // isEmptyElement
  909. "baz", // name
  910. String.Empty, // prefix
  911. "baz", // localName
  912. "http://baz/", // namespaceURI
  913. String.Empty, // value
  914. 1 // attributeCount
  915. );
  916. AssertAttribute (
  917. xmlReader, // xmlReader
  918. "xmlns", // name
  919. String.Empty, // prefix
  920. "xmlns", // localName
  921. "http://www.w3.org/2000/xmlns/", // namespaceURI
  922. "http://baz/" // value
  923. );
  924. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  925. Assertion.AssertEquals ("http://baz/", xmlReader.LookupNamespace (String.Empty));
  926. AssertNode (
  927. xmlReader, // xmlReader
  928. XmlNodeType.EndElement, // nodeType
  929. 0, // depth
  930. false, // isEmptyElement
  931. "foo:bar", // name
  932. "foo", // prefix
  933. "bar", // localName
  934. "http://foo/", // namespaceURI
  935. String.Empty, // value
  936. 0 // attributeCount
  937. );
  938. Assertion.AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  939. AssertEndDocument (xmlReader);
  940. }
  941. [Test]
  942. public void AttributeInNamespace ()
  943. {
  944. string xml = @"<foo bar:baz='quux' xmlns:bar='http://bar/' />";
  945. RunTest (xml, new TestMethod (AttributeInNamespace));
  946. }
  947. public void AttributeInNamespace (XmlReader xmlReader)
  948. {
  949. AssertStartDocument (xmlReader);
  950. AssertNode (
  951. xmlReader, // xmlReader
  952. XmlNodeType.Element, // nodeType
  953. 0, // depth
  954. true, // isEmptyElement
  955. "foo", // name
  956. String.Empty, // prefix
  957. "foo", // localName
  958. String.Empty, // namespaceURI
  959. String.Empty, // value
  960. 2 // attributeCount
  961. );
  962. AssertAttribute (
  963. xmlReader, // xmlReader
  964. "bar:baz", // name
  965. "bar", // prefix
  966. "baz", // localName
  967. "http://bar/", // namespaceURI
  968. "quux" // value
  969. );
  970. AssertAttribute (
  971. xmlReader, // xmlReader
  972. "xmlns:bar", // name
  973. "xmlns", // prefix
  974. "bar", // localName
  975. "http://www.w3.org/2000/xmlns/", // namespaceURI
  976. "http://bar/" // value
  977. );
  978. Assertion.AssertEquals ("http://bar/", xmlReader.LookupNamespace ("bar"));
  979. AssertEndDocument (xmlReader);
  980. }
  981. [Test]
  982. public void MoveToElementFromAttribute ()
  983. {
  984. string xml = @"<foo bar=""baz"" />";
  985. RunTest (xml, new TestMethod (MoveToElementFromAttribute));
  986. }
  987. public void MoveToElementFromAttribute (XmlReader xmlReader)
  988. {
  989. Assertion.Assert (xmlReader.Read ());
  990. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  991. Assertion.Assert (xmlReader.MoveToFirstAttribute ());
  992. Assertion.AssertEquals (XmlNodeType.Attribute, xmlReader.NodeType);
  993. Assertion.Assert (xmlReader.MoveToElement ());
  994. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  995. }
  996. [Test]
  997. public void MoveToElementFromElement ()
  998. {
  999. string xml = @"<foo bar=""baz"" />";
  1000. RunTest (xml, new TestMethod (MoveToElementFromElement));
  1001. }
  1002. public void MoveToElementFromElement (XmlReader xmlReader)
  1003. {
  1004. Assertion.Assert (xmlReader.Read ());
  1005. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1006. Assertion.Assert (!xmlReader.MoveToElement ());
  1007. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1008. }
  1009. [Test]
  1010. public void MoveToFirstAttributeWithNoAttributes ()
  1011. {
  1012. string xml = @"<foo />";
  1013. RunTest (xml, new TestMethod (MoveToFirstAttributeWithNoAttributes));
  1014. }
  1015. public void MoveToFirstAttributeWithNoAttributes (XmlReader xmlReader)
  1016. {
  1017. Assertion.Assert (xmlReader.Read ());
  1018. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1019. Assertion.Assert (!xmlReader.MoveToFirstAttribute ());
  1020. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1021. }
  1022. [Test]
  1023. public void MoveToNextAttributeWithNoAttributes ()
  1024. {
  1025. string xml = @"<foo />";
  1026. RunTest (xml, new TestMethod (MoveToNextAttributeWithNoAttributes));
  1027. }
  1028. public void MoveToNextAttributeWithNoAttributes (XmlReader xmlReader)
  1029. {
  1030. Assertion.Assert (xmlReader.Read ());
  1031. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1032. Assertion.Assert (!xmlReader.MoveToNextAttribute ());
  1033. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1034. }
  1035. [Test]
  1036. public void MoveToNextAttribute()
  1037. {
  1038. string xml = @"<foo bar=""baz"" quux='quuux'/>";
  1039. RunTest (xml, new TestMethod (MoveToNextAttribute));
  1040. }
  1041. public void MoveToNextAttribute (XmlReader xmlReader)
  1042. {
  1043. AssertStartDocument (xmlReader);
  1044. AssertNode (
  1045. xmlReader, // xmlReader
  1046. XmlNodeType.Element, // nodeType
  1047. 0, //depth
  1048. true, // isEmptyElement
  1049. "foo", // name
  1050. String.Empty, // prefix
  1051. "foo", // localName
  1052. String.Empty, // namespaceURI
  1053. String.Empty, // value
  1054. 2 // attributeCount
  1055. );
  1056. AssertAttribute (
  1057. xmlReader, // xmlReader
  1058. "bar", // name
  1059. String.Empty, // prefix
  1060. "bar", // localName
  1061. String.Empty, // namespaceURI
  1062. "baz" // value
  1063. );
  1064. AssertAttribute (
  1065. xmlReader, // xmlReader
  1066. "quux", // name
  1067. String.Empty, // prefix
  1068. "quux", // localName
  1069. String.Empty, // namespaceURI
  1070. "quuux" // value
  1071. );
  1072. Assertion.Assert (xmlReader.MoveToNextAttribute ());
  1073. Assertion.Assert ("bar" == xmlReader.Name || "quux" == xmlReader.Name);
  1074. Assertion.Assert ("baz" == xmlReader.Value || "quuux" == xmlReader.Value);
  1075. Assertion.Assert (xmlReader.MoveToNextAttribute ());
  1076. Assertion.Assert ("bar" == xmlReader.Name || "quux" == xmlReader.Name);
  1077. Assertion.Assert ("baz" == xmlReader.Value || "quuux" == xmlReader.Value);
  1078. Assertion.Assert (!xmlReader.MoveToNextAttribute ());
  1079. Assertion.Assert (xmlReader.MoveToElement ());
  1080. AssertNodeValues (
  1081. xmlReader, // xmlReader
  1082. XmlNodeType.Element, // nodeType
  1083. 0, //depth
  1084. true, // isEmptyElement
  1085. "foo", // name
  1086. String.Empty, // prefix
  1087. "foo", // localName
  1088. String.Empty, // namespaceURI
  1089. String.Empty, // value
  1090. 2 // attributeCount
  1091. );
  1092. AssertEndDocument (xmlReader);
  1093. }
  1094. [Test]
  1095. public void AttributeOrder ()
  1096. {
  1097. string xml = @"<foo _1='1' _2='2' _3='3' />";
  1098. RunTest (xml, new TestMethod (AttributeOrder));
  1099. }
  1100. public void AttributeOrder (XmlReader xmlReader)
  1101. {
  1102. Assertion.Assert (xmlReader.Read ());
  1103. Assertion.AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1104. Assertion.Assert (xmlReader.MoveToFirstAttribute ());
  1105. Assertion.AssertEquals ("_1", xmlReader.Name);
  1106. Assertion.Assert (xmlReader.MoveToNextAttribute ());
  1107. Assertion.AssertEquals ("_2", xmlReader.Name);
  1108. Assertion.Assert (xmlReader.MoveToNextAttribute ());
  1109. Assertion.AssertEquals ("_3", xmlReader.Name);
  1110. Assertion.Assert (!xmlReader.MoveToNextAttribute ());
  1111. }
  1112. }
  1113. }