XmlReaderCommonTests.cs 33 KB

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