XmlReaderCommonTests.cs 31 KB

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