XmlReaderCommonTests.cs 30 KB

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