XmlReaderCommonTests.cs 31 KB

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