XmlReaderCommonTests.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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.Schema;
  16. using System.Xml.XPath;
  17. using NUnit.Framework;
  18. namespace MonoTests.System.Xml
  19. {
  20. [TestFixture]
  21. public class XmlReaderTests : Assertion
  22. {
  23. [SetUp]
  24. public void GetReady ()
  25. {
  26. document = new XmlDocument ();
  27. document.LoadXml (xml1);
  28. }
  29. XmlDocument document;
  30. const string xml1 = "<root attr1='value1'><child /></root>";
  31. const string xml2 = "<root><foo/><bar>test.</bar></root>";
  32. const string xml3 = "<root> test of <b>mixed</b> string.<![CDATA[ cdata string.]]></root>";
  33. const string xml4 = "<root>test of <b>mixed</b> string.</root>";
  34. XmlTextReader xtr;
  35. XmlNodeReader xnr;
  36. // copy from XmlTextReaderTests
  37. private void AssertStartDocument (XmlReader xmlReader)
  38. {
  39. Assert (xmlReader.ReadState == ReadState.Initial);
  40. Assert (xmlReader.NodeType == XmlNodeType.None);
  41. Assert (xmlReader.Depth == 0);
  42. Assert (!xmlReader.EOF);
  43. }
  44. private void AssertNode (
  45. XmlReader xmlReader,
  46. XmlNodeType nodeType,
  47. int depth,
  48. bool isEmptyElement,
  49. string name,
  50. string prefix,
  51. string localName,
  52. string namespaceURI,
  53. string value,
  54. int attributeCount)
  55. {
  56. AssertNode ("", xmlReader, nodeType, depth,
  57. isEmptyElement, name, prefix, localName,
  58. namespaceURI, value, attributeCount);
  59. }
  60. private void AssertNode (
  61. string label,
  62. XmlReader xmlReader,
  63. XmlNodeType nodeType,
  64. int depth,
  65. bool isEmptyElement,
  66. string name,
  67. string prefix,
  68. string localName,
  69. string namespaceURI,
  70. string value,
  71. int attributeCount)
  72. {
  73. Assert (label + " Read() return value", xmlReader.Read ());
  74. Assert (label + " ReadState", xmlReader.ReadState == ReadState.Interactive);
  75. Assert (label + " !EOF", !xmlReader.EOF);
  76. AssertNodeValues (label, xmlReader, nodeType, depth,
  77. isEmptyElement, name, prefix, localName,
  78. namespaceURI, value, value != String.Empty,
  79. attributeCount, attributeCount > 0);
  80. }
  81. private void AssertNodeValues (
  82. string label,
  83. XmlReader xmlReader,
  84. XmlNodeType nodeType,
  85. int depth,
  86. bool isEmptyElement,
  87. string name,
  88. string prefix,
  89. string localName,
  90. string namespaceURI,
  91. string value,
  92. int attributeCount)
  93. {
  94. AssertNodeValues (label, xmlReader, nodeType, depth,
  95. isEmptyElement, name, prefix, localName,
  96. namespaceURI, value, value != String.Empty,
  97. attributeCount, attributeCount > 0);
  98. }
  99. private void AssertNodeValues (
  100. string label,
  101. XmlReader xmlReader,
  102. XmlNodeType nodeType,
  103. int depth,
  104. bool isEmptyElement,
  105. string name,
  106. string prefix,
  107. string localName,
  108. string namespaceURI,
  109. string value,
  110. bool hasValue,
  111. int attributeCount,
  112. bool hasAttributes)
  113. {
  114. label = String.Concat (label, "(", xmlReader.GetType ().Name, ")");
  115. AssertEquals (label + ": NodeType", nodeType, xmlReader.NodeType);
  116. AssertEquals (label + ": IsEmptyElement", isEmptyElement, xmlReader.IsEmptyElement);
  117. AssertEquals (label + ": name", name, xmlReader.Name);
  118. AssertEquals (label + ": prefix", prefix, xmlReader.Prefix);
  119. AssertEquals (label + ": localName", localName, xmlReader.LocalName);
  120. AssertEquals (label + ": namespaceURI", namespaceURI, xmlReader.NamespaceURI);
  121. AssertEquals (label + ": Depth", depth, xmlReader.Depth);
  122. AssertEquals (label + ": hasValue", hasValue, xmlReader.HasValue);
  123. AssertEquals (label + ": Value", value, xmlReader.Value);
  124. AssertEquals (label + ": hasAttributes", hasAttributes, xmlReader.HasAttributes);
  125. AssertEquals (label + ": attributeCount", attributeCount, xmlReader.AttributeCount);
  126. }
  127. private void AssertAttribute (
  128. XmlReader xmlReader,
  129. string name,
  130. string prefix,
  131. string localName,
  132. string namespaceURI,
  133. string value)
  134. {
  135. AssertEquals ("value", value, xmlReader [name]);
  136. Assert (xmlReader.GetAttribute (name) == value);
  137. if (namespaceURI != String.Empty) {
  138. Assert (xmlReader[localName, namespaceURI] == value);
  139. Assert (xmlReader.GetAttribute (localName, namespaceURI) == value);
  140. }
  141. }
  142. private void AssertEndDocument (XmlReader xmlReader)
  143. {
  144. Assert ("could read", !xmlReader.Read ());
  145. AssertEquals ("NodeType is not XmlNodeType.None", XmlNodeType.None, xmlReader.NodeType);
  146. AssertEquals ("Depth is not 0", 0, xmlReader.Depth);
  147. AssertEquals ("ReadState is not ReadState.EndOfFile", ReadState.EndOfFile, xmlReader.ReadState);
  148. Assert ("not EOF", xmlReader.EOF);
  149. xmlReader.Close ();
  150. AssertEquals ("ReadState is not ReadState.Cosed", ReadState.Closed, xmlReader.ReadState);
  151. }
  152. private delegate void TestMethod (XmlReader reader);
  153. private void RunTest (string xml, TestMethod method)
  154. {
  155. xtr = new XmlTextReader (new StringReader (xml));
  156. method (xtr);
  157. // DTD validation
  158. xtr = new XmlTextReader (new StringReader (xml));
  159. XmlValidatingReader xvr = new XmlValidatingReader (xtr);
  160. xvr.ValidationType = ValidationType.DTD;
  161. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  162. method (xvr);
  163. // XSD validation
  164. xtr = new XmlTextReader (new StringReader (xml));
  165. xvr = new XmlValidatingReader (xtr);
  166. xvr.EntityHandling = EntityHandling.ExpandCharEntities;
  167. method (xvr);
  168. document.XmlResolver = null;
  169. document.LoadXml (xml);
  170. xnr = new XmlNodeReader (document);
  171. method (xnr);
  172. #if NET_2_0
  173. /*
  174. // XPathNavigatorReader tests
  175. System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument (new StringReader (xml));
  176. XmlReader xpr = doc.CreateNavigator ().ReadSubtree ();
  177. method (xpr);
  178. */
  179. #endif
  180. }
  181. [Test]
  182. public void InitialState ()
  183. {
  184. RunTest (xml1, new TestMethod (InitialState));
  185. }
  186. private void InitialState (XmlReader reader)
  187. {
  188. AssertEquals ("Depth", 0, reader.Depth);
  189. AssertEquals ("EOF", false, reader.EOF);
  190. AssertEquals ("HasValue", false, reader.HasValue);
  191. AssertEquals ("IsEmptyElement", false, reader.IsEmptyElement);
  192. AssertEquals ("LocalName", String.Empty, reader.LocalName);
  193. AssertEquals ("NodeType", XmlNodeType.None, reader.NodeType);
  194. AssertEquals ("ReadState", ReadState.Initial, reader.ReadState);
  195. }
  196. [Test]
  197. public void Read ()
  198. {
  199. RunTest (xml1, new TestMethod (Read));
  200. }
  201. public void Read (XmlReader reader)
  202. {
  203. reader.Read ();
  204. AssertEquals ("<root>.NodeType", XmlNodeType.Element, reader.NodeType);
  205. AssertEquals ("<root>.Name", "root", reader.Name);
  206. AssertEquals ("<root>.ReadState", ReadState.Interactive, reader.ReadState);
  207. AssertEquals ("<root>.Depth", 0, reader.Depth);
  208. // move to 'child'
  209. reader.Read ();
  210. AssertEquals ("<child/>.Depth", 1, reader.Depth);
  211. AssertEquals ("<child/>.NodeType", XmlNodeType.Element, reader.NodeType);
  212. AssertEquals ("<child/>.Name", "child", reader.Name);
  213. reader.Read ();
  214. AssertEquals ("</root>.Depth", 0, reader.Depth);
  215. AssertEquals ("</root>.NodeType", XmlNodeType.EndElement, reader.NodeType);
  216. AssertEquals ("</root>.Name", "root", reader.Name);
  217. reader.Read ();
  218. AssertEquals ("end.EOF", true, reader.EOF);
  219. AssertEquals ("end.NodeType", XmlNodeType.None, reader.NodeType);
  220. }
  221. [Test]
  222. [Category ("NotDotNet")]
  223. public void ReadAttributeValue ()
  224. {
  225. RunTest ("<root attr=''/>", new TestMethod (ReadAttributeValue));
  226. }
  227. public void ReadAttributeValue (XmlReader reader)
  228. {
  229. reader.Read (); // root
  230. Assert (reader.MoveToFirstAttribute ());
  231. // It looks like that MS.NET shows AttributeCount and
  232. // HasAttributes as the same as element node!
  233. this.AssertNodeValues ("#1",
  234. reader, XmlNodeType.Attribute,
  235. 1, false, "attr", "", "attr", "", "", true, 1, true);
  236. Assert (reader.ReadAttributeValue ());
  237. // MS.NET XmlTextReader fails. Its Prefix returns
  238. // null instead of "". It is fixed in MS.NET 2.0.
  239. this.AssertNodeValues ("#2",
  240. reader, XmlNodeType.Text,
  241. 2, false, "", "", "", "", "", true, 1, true);
  242. Assert (reader.MoveToElement ());
  243. this.AssertNodeValues ("#3",
  244. reader, XmlNodeType.Element,
  245. 0, true, "root", "", "root", "", "", false, 1, true);
  246. }
  247. [Test]
  248. public void ReadEmptyElement ()
  249. {
  250. RunTest (xml2, new TestMethod (ReadEmptyElement));
  251. }
  252. public void ReadEmptyElement (XmlReader reader)
  253. {
  254. reader.Read (); // root
  255. AssertEquals (false, reader.IsEmptyElement);
  256. reader.Read (); // foo
  257. AssertEquals ("foo", reader.Name);
  258. AssertEquals (true, reader.IsEmptyElement);
  259. reader.Read (); // bar
  260. AssertEquals ("bar", reader.Name);
  261. AssertEquals (false, reader.IsEmptyElement);
  262. }
  263. [Test]
  264. public void ReadStringFromElement ()
  265. {
  266. RunTest (xml3, new TestMethod (ReadStringFromElement));
  267. }
  268. public void ReadStringFromElement (XmlReader reader)
  269. {
  270. // Note: ReadString() test works only when the reader is
  271. // positioned at the container element.
  272. // In case the reader is positioned at the first
  273. // character node, XmlTextReader and XmlNodeReader works
  274. // different!!
  275. reader.Read ();
  276. string s = reader.ReadString ();
  277. AssertEquals ("readString.1.ret_val", " test of ", s);
  278. AssertEquals ("readString.1.Name", "b", reader.Name);
  279. s = reader.ReadString ();
  280. AssertEquals ("readString.2.ret_val", "mixed", s);
  281. AssertEquals ("readString.2.NodeType", XmlNodeType.EndElement, reader.NodeType);
  282. s = reader.ReadString (); // never proceeds.
  283. AssertEquals ("readString.3.ret_val", String.Empty, s);
  284. AssertEquals ("readString.3.NodeType", XmlNodeType.EndElement, reader.NodeType);
  285. reader.Read ();
  286. AssertEquals ("readString.4.NodeType", XmlNodeType.Text, reader.NodeType);
  287. AssertEquals ("readString.4.Value", " string.", reader.Value);
  288. s = reader.ReadString (); // reads the same Text node.
  289. AssertEquals ("readString.5.ret_val", " string. cdata string.", s);
  290. AssertEquals ("readString.5.NodeType", XmlNodeType.EndElement, reader.NodeType);
  291. }
  292. [Test]
  293. public void ReadInnerXml ()
  294. {
  295. const string xml = "<root><foo>test of <b>mixed</b> string.</foo><bar /></root>";
  296. RunTest (xml, new TestMethod (ReadInnerXml));
  297. }
  298. public void ReadInnerXml (XmlReader reader)
  299. {
  300. reader.Read ();
  301. reader.Read ();
  302. AssertEquals ("initial.ReadState", ReadState.Interactive, reader.ReadState);
  303. AssertEquals ("initial.EOF", false, reader.EOF);
  304. AssertEquals ("initial.NodeType", XmlNodeType.Element, reader.NodeType);
  305. string s = reader.ReadInnerXml ();
  306. AssertEquals ("read_all", "test of <b>mixed</b> string.", s);
  307. AssertEquals ("after.Name", "bar", reader.Name);
  308. AssertEquals ("after.NodeType", XmlNodeType.Element, reader.NodeType);
  309. }
  310. [Test]
  311. public void EmptyElement ()
  312. {
  313. RunTest ("<foo/>", new TestMethod (EmptyElement));
  314. }
  315. public void EmptyElement (XmlReader xmlReader)
  316. {
  317. AssertStartDocument (xmlReader);
  318. AssertNode (
  319. xmlReader, // xmlReader
  320. XmlNodeType.Element, // nodeType
  321. 0, // depth
  322. true, // isEmptyElement
  323. "foo", // name
  324. String.Empty, // prefix
  325. "foo", // localName
  326. String.Empty, // namespaceURI
  327. String.Empty, // value
  328. 0 // attributeCount
  329. );
  330. AssertEndDocument (xmlReader);
  331. }
  332. [Test]
  333. public void NestedEmptyTag ()
  334. {
  335. string xml = "<foo><bar/></foo>";
  336. RunTest (xml, new TestMethod (NestedEmptyTag));
  337. }
  338. public void NestedEmptyTag (XmlReader xmlReader)
  339. {
  340. AssertStartDocument (xmlReader);
  341. AssertNode (
  342. "#1",
  343. xmlReader, // xmlReader
  344. XmlNodeType.Element, // nodeType
  345. 0, //depth
  346. false, // isEmptyElement
  347. "foo", // name
  348. String.Empty, // prefix
  349. "foo", // localName
  350. String.Empty, // namespaceURI
  351. String.Empty, // value
  352. 0 // attributeCount
  353. );
  354. AssertNode (
  355. "#2",
  356. xmlReader, // xmlReader
  357. XmlNodeType.Element, // nodeType
  358. 1, //depth
  359. true, // isEmptyElement
  360. "bar", // name
  361. String.Empty, // prefix
  362. "bar", // localName
  363. String.Empty, // namespaceURI
  364. String.Empty, // value
  365. 0 // attributeCount
  366. );
  367. AssertNode (
  368. "#3",
  369. xmlReader, // xmlReader
  370. XmlNodeType.EndElement, // nodeType
  371. 0, //depth
  372. false, // isEmptyElement
  373. "foo", // name
  374. String.Empty, // prefix
  375. "foo", // localName
  376. String.Empty, // namespaceURI
  377. String.Empty, // value
  378. 0 // attributeCount
  379. );
  380. AssertEndDocument (xmlReader);
  381. }
  382. [Test]
  383. public void NestedText ()
  384. {
  385. string xml = "<foo>bar</foo>";
  386. RunTest (xml, new TestMethod (NestedText));
  387. }
  388. public void NestedText (XmlReader xmlReader)
  389. {
  390. AssertStartDocument (xmlReader);
  391. AssertNode (
  392. xmlReader, // xmlReader
  393. XmlNodeType.Element, // nodeType
  394. 0, //depth
  395. false, // isEmptyElement
  396. "foo", // name
  397. String.Empty, // prefix
  398. "foo", // localName
  399. String.Empty, // namespaceURI
  400. String.Empty, // value
  401. 0 // attributeCount
  402. );
  403. AssertNode (
  404. xmlReader, // xmlReader
  405. XmlNodeType.Text, // nodeType
  406. 1, //depth
  407. false, // isEmptyElement
  408. String.Empty, // name
  409. String.Empty, // prefix
  410. String.Empty, // localName
  411. String.Empty, // namespaceURI
  412. "bar", // value
  413. 0 // attributeCount
  414. );
  415. AssertNode (
  416. xmlReader, // xmlReader
  417. XmlNodeType.EndElement, // nodeType
  418. 0, //depth
  419. false, // isEmptyElement
  420. "foo", // name
  421. String.Empty, // prefix
  422. "foo", // localName
  423. String.Empty, // namespaceURI
  424. String.Empty, // value
  425. 0 // attributeCount
  426. );
  427. AssertEndDocument (xmlReader);
  428. }
  429. [Test]
  430. public void EmptyElementWithAttributes ()
  431. {
  432. string xml = @"<foo bar=""baz"" quux='quuux' x:foo='x-foo' xmlns:x = 'urn:xfoo' />";
  433. RunTest (xml, new TestMethod (EmptyElementWithAttributes ));
  434. }
  435. public void EmptyElementWithAttributes (XmlReader xmlReader)
  436. {
  437. AssertStartDocument (xmlReader);
  438. AssertNode (
  439. xmlReader, // xmlReader
  440. XmlNodeType.Element, // nodeType
  441. 0, //depth
  442. true, // isEmptyElement
  443. "foo", // name
  444. String.Empty, // prefix
  445. "foo", // localName
  446. String.Empty, // namespaceURI
  447. String.Empty, // value
  448. 4 // attributeCount
  449. );
  450. AssertAttribute (
  451. xmlReader, // xmlReader
  452. "bar", // name
  453. String.Empty, // prefix
  454. "bar", // localName
  455. String.Empty, // namespaceURI
  456. "baz" // value
  457. );
  458. AssertAttribute (
  459. xmlReader, // xmlReader
  460. "quux", // name
  461. String.Empty, // prefix
  462. "quux", // localName
  463. String.Empty, // namespaceURI
  464. "quuux" // value
  465. );
  466. AssertAttribute (
  467. xmlReader, // xmlReader
  468. "notexist", // name
  469. String.Empty, // prefix
  470. "notexist", // localName
  471. String.Empty, // namespaceURI
  472. null // value
  473. );
  474. AssertAttribute (
  475. xmlReader, // xmlReader
  476. "x:foo", // name
  477. "x", // prefix
  478. "foo", // localName
  479. "urn:xfoo", // namespaceURI
  480. "x-foo" // value
  481. );
  482. AssertAttribute (
  483. xmlReader, // xmlReader
  484. "x:bar", // name
  485. "x", // prefix
  486. "bar", // localName
  487. "urn:xfoo", // namespaceURI
  488. null // value
  489. );
  490. AssertEndDocument (xmlReader);
  491. }
  492. [Test]
  493. public void ProcessingInstructionBeforeDocumentElement ()
  494. {
  495. string xml = "<?foo bar?><baz/>";
  496. RunTest (xml, new TestMethod (ProcessingInstructionBeforeDocumentElement));
  497. }
  498. public void ProcessingInstructionBeforeDocumentElement (XmlReader xmlReader)
  499. {
  500. AssertStartDocument (xmlReader);
  501. AssertNode (
  502. xmlReader, // xmlReader
  503. XmlNodeType.ProcessingInstruction, // nodeType
  504. 0, //depth
  505. false, // isEmptyElement
  506. "foo", // name
  507. String.Empty, // prefix
  508. "foo", // localName
  509. String.Empty, // namespaceURI
  510. "bar", // value
  511. 0 // attributeCount
  512. );
  513. AssertNode (
  514. xmlReader, // xmlReader
  515. XmlNodeType.Element, // nodeType
  516. 0, //depth
  517. true, // isEmptyElement
  518. "baz", // name
  519. String.Empty, // prefix
  520. "baz", // localName
  521. String.Empty, // namespaceURI
  522. String.Empty, // value
  523. 0 // attributeCount
  524. );
  525. AssertEndDocument (xmlReader);
  526. }
  527. [Test]
  528. public void CommentBeforeDocumentElement ()
  529. {
  530. string xml = "<!--foo--><bar/>";
  531. RunTest (xml, new TestMethod (CommentBeforeDocumentElement));
  532. }
  533. public void CommentBeforeDocumentElement (XmlReader xmlReader)
  534. {
  535. AssertStartDocument (xmlReader);
  536. AssertNode (
  537. xmlReader, // xmlReader
  538. XmlNodeType.Comment, // nodeType
  539. 0, //depth
  540. false, // isEmptyElement
  541. String.Empty, // name
  542. String.Empty, // prefix
  543. String.Empty, // localName
  544. String.Empty, // namespaceURI
  545. "foo", // value
  546. 0 // attributeCount
  547. );
  548. AssertNode (
  549. xmlReader, // xmlReader
  550. XmlNodeType.Element, // nodeType
  551. 0, //depth
  552. true, // isEmptyElement
  553. "bar", // name
  554. String.Empty, // prefix
  555. "bar", // localName
  556. String.Empty, // namespaceURI
  557. String.Empty, // value
  558. 0 // attributeCount
  559. );
  560. AssertEndDocument (xmlReader);
  561. }
  562. [Test]
  563. public void PredefinedEntities ()
  564. {
  565. string xml = "<foo>&lt;&gt;&amp;&apos;&quot;</foo>";
  566. RunTest (xml, new TestMethod (PredefinedEntities));
  567. }
  568. public void PredefinedEntities (XmlReader xmlReader)
  569. {
  570. AssertStartDocument (xmlReader);
  571. AssertNode (
  572. xmlReader, // xmlReader
  573. XmlNodeType.Element, // nodeType
  574. 0, //depth
  575. false, // isEmptyElement
  576. "foo", // name
  577. String.Empty, // prefix
  578. "foo", // localName
  579. String.Empty, // namespaceURI
  580. String.Empty, // value
  581. 0 // attributeCount
  582. );
  583. AssertNode (
  584. xmlReader, // xmlReader
  585. XmlNodeType.Text, // nodeType
  586. 1, //depth
  587. false, // isEmptyElement
  588. String.Empty, // name
  589. String.Empty, // prefix
  590. String.Empty, // localName
  591. String.Empty, // namespaceURI
  592. "<>&'\"", // value
  593. 0 // attributeCount
  594. );
  595. AssertNode (
  596. xmlReader, // xmlReader
  597. XmlNodeType.EndElement, // nodeType
  598. 0, //depth
  599. false, // isEmptyElement
  600. "foo", // name
  601. String.Empty, // prefix
  602. "foo", // localName
  603. String.Empty, // namespaceURI
  604. String.Empty, // value
  605. 0 // attributeCount
  606. );
  607. AssertEndDocument (xmlReader);
  608. }
  609. [Test]
  610. public void CharacterReferences ()
  611. {
  612. string xml = "<foo>&#70;&#x4F;&#x4f;</foo>";
  613. RunTest (xml, new TestMethod (CharacterReferences));
  614. }
  615. public void CharacterReferences (XmlReader xmlReader)
  616. {
  617. AssertStartDocument (xmlReader);
  618. AssertNode (
  619. xmlReader, // xmlReader
  620. XmlNodeType.Element, // nodeType
  621. 0, //depth
  622. false, // isEmptyElement
  623. "foo", // name
  624. String.Empty, // prefix
  625. "foo", // localName
  626. String.Empty, // namespaceURI
  627. String.Empty, // value
  628. 0 // attributeCount
  629. );
  630. AssertNode (
  631. xmlReader, // xmlReader
  632. XmlNodeType.Text, // nodeType
  633. 1, //depth
  634. false, // isEmptyElement
  635. String.Empty, // name
  636. String.Empty, // prefix
  637. String.Empty, // localName
  638. String.Empty, // namespaceURI
  639. "FOO", // value
  640. 0 // attributeCount
  641. );
  642. AssertNode (
  643. xmlReader, // xmlReader
  644. XmlNodeType.EndElement, // nodeType
  645. 0, //depth
  646. false, // isEmptyElement
  647. "foo", // name
  648. String.Empty, // prefix
  649. "foo", // localName
  650. String.Empty, // namespaceURI
  651. String.Empty, // value
  652. 0 // attributeCount
  653. );
  654. AssertEndDocument (xmlReader);
  655. }
  656. [Test]
  657. public void PredefinedEntitiesInAttribute ()
  658. {
  659. string xml = "<foo bar='&lt;&gt;&amp;&apos;&quot;'/>";
  660. RunTest (xml, new TestMethod (PredefinedEntitiesInAttribute ));
  661. }
  662. public void PredefinedEntitiesInAttribute (XmlReader xmlReader)
  663. {
  664. AssertStartDocument (xmlReader);
  665. AssertNode (
  666. xmlReader, // xmlReader
  667. XmlNodeType.Element, // nodeType
  668. 0, //depth
  669. true, // isEmptyElement
  670. "foo", // name
  671. String.Empty, // prefix
  672. "foo", // localName
  673. String.Empty, // namespaceURI
  674. String.Empty, // value
  675. 1 // attributeCount
  676. );
  677. AssertAttribute (
  678. xmlReader, // xmlReader
  679. "bar", // name
  680. String.Empty, // prefix
  681. "bar", // localName
  682. String.Empty, // namespaceURI
  683. "<>&'\"" // value
  684. );
  685. AssertEndDocument (xmlReader);
  686. }
  687. [Test]
  688. public void CharacterReferencesInAttribute ()
  689. {
  690. string xml = "<foo bar='&#70;&#x4F;&#x4f;'/>";
  691. RunTest (xml, new TestMethod (CharacterReferencesInAttribute));
  692. }
  693. public void CharacterReferencesInAttribute (XmlReader xmlReader)
  694. {
  695. AssertStartDocument (xmlReader);
  696. AssertNode (
  697. xmlReader, // xmlReader
  698. XmlNodeType.Element, // nodeType
  699. 0, //depth
  700. true, // isEmptyElement
  701. "foo", // name
  702. String.Empty, // prefix
  703. "foo", // localName
  704. String.Empty, // namespaceURI
  705. String.Empty, // value
  706. 1 // attributeCount
  707. );
  708. AssertAttribute (
  709. xmlReader, // xmlReader
  710. "bar", // name
  711. String.Empty, // prefix
  712. "bar", // localName
  713. String.Empty, // namespaceURI
  714. "FOO" // value
  715. );
  716. AssertEndDocument (xmlReader);
  717. }
  718. [Test]
  719. public void CDATA ()
  720. {
  721. string xml = "<foo><![CDATA[<>&]]></foo>";
  722. RunTest (xml, new TestMethod (CDATA));
  723. }
  724. public void CDATA (XmlReader xmlReader)
  725. {
  726. AssertStartDocument (xmlReader);
  727. AssertNode (
  728. xmlReader, // xmlReader
  729. XmlNodeType.Element, // nodeType
  730. 0, //depth
  731. false, // isEmptyElement
  732. "foo", // name
  733. String.Empty, // prefix
  734. "foo", // localName
  735. String.Empty, // namespaceURI
  736. String.Empty, // value
  737. 0 // attributeCount
  738. );
  739. AssertNode (
  740. xmlReader, // xmlReader
  741. XmlNodeType.CDATA, // nodeType
  742. 1, //depth
  743. false, // isEmptyElement
  744. String.Empty, // name
  745. String.Empty, // prefix
  746. String.Empty, // localName
  747. String.Empty, // namespaceURI
  748. "<>&", // value
  749. 0 // attributeCount
  750. );
  751. AssertNode (
  752. xmlReader, // xmlReader
  753. XmlNodeType.EndElement, // nodeType
  754. 0, //depth
  755. false, // isEmptyElement
  756. "foo", // name
  757. String.Empty, // prefix
  758. "foo", // localName
  759. String.Empty, // namespaceURI
  760. String.Empty, // value
  761. 0 // attributeCount
  762. );
  763. AssertEndDocument (xmlReader);
  764. }
  765. [Test]
  766. public void EmptyElementInDefaultNamespace ()
  767. {
  768. string xml = @"<foo xmlns='http://foo/' />";
  769. RunTest (xml, new TestMethod (EmptyElementInDefaultNamespace));
  770. }
  771. public void EmptyElementInDefaultNamespace (XmlReader xmlReader)
  772. {
  773. AssertStartDocument (xmlReader);
  774. AssertNode (
  775. xmlReader, // xmlReader
  776. XmlNodeType.Element, // nodeType
  777. 0, // depth
  778. true, // isEmptyElement
  779. "foo", // name
  780. String.Empty, // prefix
  781. "foo", // localName
  782. "http://foo/", // namespaceURI
  783. String.Empty, // value
  784. 1 // attributeCount
  785. );
  786. AssertAttribute (
  787. xmlReader, // xmlReader
  788. "xmlns", // name
  789. String.Empty, // prefix
  790. "xmlns", // localName
  791. "http://www.w3.org/2000/xmlns/", // namespaceURI
  792. "http://foo/" // value
  793. );
  794. AssertEquals ("http://foo/", xmlReader.LookupNamespace (String.Empty));
  795. AssertEndDocument (xmlReader);
  796. }
  797. [Test]
  798. public void ChildElementInNamespace ()
  799. {
  800. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz:quux xmlns:baz='http://baz/' /></foo:bar>";
  801. RunTest (xml, new TestMethod (ChildElementInNamespace));
  802. }
  803. public void ChildElementInNamespace (XmlReader xmlReader)
  804. {
  805. AssertStartDocument (xmlReader);
  806. AssertNode (
  807. xmlReader, // xmlReader
  808. XmlNodeType.Element, // nodeType
  809. 0, // depth
  810. false, // isEmptyElement
  811. "foo:bar", // name
  812. "foo", // prefix
  813. "bar", // localName
  814. "http://foo/", // namespaceURI
  815. String.Empty, // value
  816. 1 // attributeCount
  817. );
  818. AssertAttribute (
  819. xmlReader, // xmlReader
  820. "xmlns:foo", // name
  821. "xmlns", // prefix
  822. "foo", // localName
  823. "http://www.w3.org/2000/xmlns/", // namespaceURI
  824. "http://foo/" // value
  825. );
  826. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  827. AssertNode (
  828. xmlReader, // xmlReader
  829. XmlNodeType.Element, // nodeType
  830. 1, // depth
  831. true, // isEmptyElement
  832. "baz:quux", // name
  833. "baz", // prefix
  834. "quux", // localName
  835. "http://baz/", // namespaceURI
  836. String.Empty, // value
  837. 1 // attributeCount
  838. );
  839. AssertAttribute (
  840. xmlReader, // xmlReader
  841. "xmlns:baz", // name
  842. "xmlns", // prefix
  843. "baz", // localName
  844. "http://www.w3.org/2000/xmlns/", // namespaceURI
  845. "http://baz/" // value
  846. );
  847. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  848. AssertEquals ("http://baz/", xmlReader.LookupNamespace ("baz"));
  849. AssertNode (
  850. xmlReader, // xmlReader
  851. XmlNodeType.EndElement, // nodeType
  852. 0, // depth
  853. false, // isEmptyElement
  854. "foo:bar", // name
  855. "foo", // prefix
  856. "bar", // localName
  857. "http://foo/", // namespaceURI
  858. String.Empty, // value
  859. 0 // attributeCount
  860. );
  861. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  862. AssertNull (xmlReader.LookupNamespace ("baz"));
  863. AssertEndDocument (xmlReader);
  864. }
  865. [Test]
  866. public void ChildElementInDefaultNamespace ()
  867. {
  868. string xml = @"<foo:bar xmlns:foo='http://foo/'><baz xmlns='http://baz/' /></foo:bar>";
  869. RunTest (xml, new TestMethod (ChildElementInDefaultNamespace));
  870. }
  871. public void ChildElementInDefaultNamespace (XmlReader xmlReader)
  872. {
  873. AssertStartDocument (xmlReader);
  874. AssertNode (
  875. xmlReader, // xmlReader
  876. XmlNodeType.Element, // nodeType
  877. 0, // depth
  878. false, // isEmptyElement
  879. "foo:bar", // name
  880. "foo", // prefix
  881. "bar", // localName
  882. "http://foo/", // namespaceURI
  883. String.Empty, // value
  884. 1 // attributeCount
  885. );
  886. AssertAttribute (
  887. xmlReader, // xmlReader
  888. "xmlns:foo", // name
  889. "xmlns", // prefix
  890. "foo", // localName
  891. "http://www.w3.org/2000/xmlns/", // namespaceURI
  892. "http://foo/" // value
  893. );
  894. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  895. AssertNode (
  896. xmlReader, // xmlReader
  897. XmlNodeType.Element, // nodeType
  898. 1, // depth
  899. true, // isEmptyElement
  900. "baz", // name
  901. String.Empty, // prefix
  902. "baz", // localName
  903. "http://baz/", // namespaceURI
  904. String.Empty, // value
  905. 1 // attributeCount
  906. );
  907. AssertAttribute (
  908. xmlReader, // xmlReader
  909. "xmlns", // name
  910. String.Empty, // prefix
  911. "xmlns", // localName
  912. "http://www.w3.org/2000/xmlns/", // namespaceURI
  913. "http://baz/" // value
  914. );
  915. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  916. AssertEquals ("http://baz/", xmlReader.LookupNamespace (String.Empty));
  917. AssertNode (
  918. xmlReader, // xmlReader
  919. XmlNodeType.EndElement, // nodeType
  920. 0, // depth
  921. false, // isEmptyElement
  922. "foo:bar", // name
  923. "foo", // prefix
  924. "bar", // localName
  925. "http://foo/", // namespaceURI
  926. String.Empty, // value
  927. 0 // attributeCount
  928. );
  929. AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo"));
  930. AssertEndDocument (xmlReader);
  931. }
  932. [Test]
  933. public void AttributeInNamespace ()
  934. {
  935. string xml = @"<foo bar:baz='quux' xmlns:bar='http://bar/' />";
  936. RunTest (xml, new TestMethod (AttributeInNamespace));
  937. }
  938. public void AttributeInNamespace (XmlReader xmlReader)
  939. {
  940. AssertStartDocument (xmlReader);
  941. AssertNode (
  942. xmlReader, // xmlReader
  943. XmlNodeType.Element, // nodeType
  944. 0, // depth
  945. true, // isEmptyElement
  946. "foo", // name
  947. String.Empty, // prefix
  948. "foo", // localName
  949. String.Empty, // namespaceURI
  950. String.Empty, // value
  951. 2 // attributeCount
  952. );
  953. AssertAttribute (
  954. xmlReader, // xmlReader
  955. "bar:baz", // name
  956. "bar", // prefix
  957. "baz", // localName
  958. "http://bar/", // namespaceURI
  959. "quux" // value
  960. );
  961. AssertAttribute (
  962. xmlReader, // xmlReader
  963. "xmlns:bar", // name
  964. "xmlns", // prefix
  965. "bar", // localName
  966. "http://www.w3.org/2000/xmlns/", // namespaceURI
  967. "http://bar/" // value
  968. );
  969. AssertEquals ("http://bar/", xmlReader.LookupNamespace ("bar"));
  970. AssertEndDocument (xmlReader);
  971. }
  972. [Test]
  973. public void MoveToElementFromAttribute ()
  974. {
  975. string xml = @"<foo bar=""baz"" />";
  976. RunTest (xml, new TestMethod (MoveToElementFromAttribute));
  977. }
  978. public void MoveToElementFromAttribute (XmlReader xmlReader)
  979. {
  980. Assert (xmlReader.Read ());
  981. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  982. Assert (xmlReader.MoveToFirstAttribute ());
  983. AssertEquals (XmlNodeType.Attribute, xmlReader.NodeType);
  984. Assert (xmlReader.MoveToElement ());
  985. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  986. }
  987. [Test]
  988. public void MoveToElementFromElement ()
  989. {
  990. string xml = @"<foo bar=""baz"" />";
  991. RunTest (xml, new TestMethod (MoveToElementFromElement));
  992. }
  993. public void MoveToElementFromElement (XmlReader xmlReader)
  994. {
  995. Assert (xmlReader.Read ());
  996. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  997. Assert (!xmlReader.MoveToElement ());
  998. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  999. }
  1000. [Test]
  1001. public void MoveToFirstAttributeWithNoAttributes ()
  1002. {
  1003. string xml = @"<foo />";
  1004. RunTest (xml, new TestMethod (MoveToFirstAttributeWithNoAttributes));
  1005. }
  1006. public void MoveToFirstAttributeWithNoAttributes (XmlReader xmlReader)
  1007. {
  1008. Assert (xmlReader.Read ());
  1009. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1010. Assert (!xmlReader.MoveToFirstAttribute ());
  1011. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1012. }
  1013. [Test]
  1014. public void MoveToNextAttributeWithNoAttributes ()
  1015. {
  1016. string xml = @"<foo />";
  1017. RunTest (xml, new TestMethod (MoveToNextAttributeWithNoAttributes));
  1018. }
  1019. public void MoveToNextAttributeWithNoAttributes (XmlReader xmlReader)
  1020. {
  1021. Assert (xmlReader.Read ());
  1022. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1023. Assert (!xmlReader.MoveToNextAttribute ());
  1024. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1025. }
  1026. [Test]
  1027. public void MoveToNextAttribute()
  1028. {
  1029. string xml = @"<foo bar=""baz"" quux='quuux'/>";
  1030. RunTest (xml, new TestMethod (MoveToNextAttribute));
  1031. }
  1032. public void MoveToNextAttribute (XmlReader xmlReader)
  1033. {
  1034. AssertStartDocument (xmlReader);
  1035. AssertNode (
  1036. xmlReader, // xmlReader
  1037. XmlNodeType.Element, // nodeType
  1038. 0, //depth
  1039. true, // isEmptyElement
  1040. "foo", // name
  1041. String.Empty, // prefix
  1042. "foo", // localName
  1043. String.Empty, // namespaceURI
  1044. String.Empty, // value
  1045. 2 // attributeCount
  1046. );
  1047. AssertAttribute (
  1048. xmlReader, // xmlReader
  1049. "bar", // name
  1050. String.Empty, // prefix
  1051. "bar", // localName
  1052. String.Empty, // namespaceURI
  1053. "baz" // value
  1054. );
  1055. AssertAttribute (
  1056. xmlReader, // xmlReader
  1057. "quux", // name
  1058. String.Empty, // prefix
  1059. "quux", // localName
  1060. String.Empty, // namespaceURI
  1061. "quuux" // value
  1062. );
  1063. Assert (xmlReader.MoveToNextAttribute ());
  1064. AssertEquals ("bar", xmlReader.Name);
  1065. AssertEquals ("baz", xmlReader.Value);
  1066. Assert (xmlReader.MoveToNextAttribute ());
  1067. AssertEquals ("quux", xmlReader.Name);
  1068. AssertEquals ("quuux", xmlReader.Value);
  1069. Assert (!xmlReader.MoveToNextAttribute ());
  1070. Assert (xmlReader.MoveToElement ());
  1071. AssertNodeValues (
  1072. "#1",
  1073. xmlReader, // xmlReader
  1074. XmlNodeType.Element, // nodeType
  1075. 0, //depth
  1076. true, // isEmptyElement
  1077. "foo", // name
  1078. String.Empty, // prefix
  1079. "foo", // localName
  1080. String.Empty, // namespaceURI
  1081. String.Empty, // value
  1082. 2 // attributeCount
  1083. );
  1084. AssertEndDocument (xmlReader);
  1085. }
  1086. [Test]
  1087. // [Category ("NotDotNet")] // MS XmlNodeReader never moves to xml declaration.
  1088. [Ignore ("Too inconsistent reference implementations to determine which is correct behavior.")]
  1089. public void MoveToXmlDeclAttributes ()
  1090. {
  1091. string xml = "<?xml version=\"1.0\" standalone=\"yes\"?><root/>";
  1092. RunTest (xml, new TestMethod (MoveToXmlDeclAttributes));
  1093. }
  1094. public void MoveToXmlDeclAttributes (XmlReader xmlReader)
  1095. {
  1096. xmlReader.Read ();
  1097. this.AssertNodeValues ("#1", xmlReader,
  1098. XmlNodeType.XmlDeclaration,
  1099. 0,
  1100. false,
  1101. "xml",
  1102. String.Empty,
  1103. "xml",
  1104. String.Empty,
  1105. "version=\"1.0\" standalone=\"yes\"",
  1106. 2);
  1107. Assert ("MoveToFirstAttribute",
  1108. xmlReader.MoveToFirstAttribute ());
  1109. this.AssertNodeValues ("#2", xmlReader,
  1110. XmlNodeType.Attribute,
  1111. 0, // FIXME: might be 1
  1112. false,
  1113. "version",
  1114. String.Empty,
  1115. "version",
  1116. String.Empty,
  1117. "1.0",
  1118. 2);
  1119. xmlReader.ReadAttributeValue ();
  1120. this.AssertNodeValues ("#3", xmlReader,
  1121. XmlNodeType.Text,
  1122. 1, // FIXME might be 2
  1123. false,
  1124. String.Empty,
  1125. null, // FIXME: should be String.Empty,
  1126. String.Empty,
  1127. null, // FIXME: should be String.Empty,
  1128. "1.0",
  1129. 2);
  1130. xmlReader.MoveToNextAttribute ();
  1131. this.AssertNodeValues ("#4", xmlReader,
  1132. XmlNodeType.Attribute,
  1133. 0, // FIXME: might be 1
  1134. false,
  1135. "standalone",
  1136. String.Empty,
  1137. "standalone",
  1138. String.Empty,
  1139. "yes",
  1140. 2);
  1141. xmlReader.ReadAttributeValue ();
  1142. this.AssertNodeValues ("#5", xmlReader,
  1143. XmlNodeType.Text,
  1144. 1, // FIXME: might be 2
  1145. false,
  1146. String.Empty,
  1147. null, // FIXME: should be String.Empty,
  1148. String.Empty,
  1149. null, // FIXME: should be String.Empty,
  1150. "yes",
  1151. 2);
  1152. }
  1153. [Test]
  1154. public void AttributeOrder ()
  1155. {
  1156. string xml = @"<foo _1='1' _2='2' _3='3' />";
  1157. RunTest (xml, new TestMethod (AttributeOrder));
  1158. }
  1159. public void AttributeOrder (XmlReader xmlReader)
  1160. {
  1161. Assert (xmlReader.Read ());
  1162. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1163. Assert (xmlReader.MoveToFirstAttribute ());
  1164. AssertEquals ("_1", xmlReader.Name);
  1165. Assert (xmlReader.MoveToNextAttribute ());
  1166. AssertEquals ("_2", xmlReader.Name);
  1167. Assert (xmlReader.MoveToNextAttribute ());
  1168. AssertEquals ("_3", xmlReader.Name);
  1169. Assert (!xmlReader.MoveToNextAttribute ());
  1170. }
  1171. [Test]
  1172. [Category ("NotDotNet")]
  1173. public void IndexerAndAttributes ()
  1174. {
  1175. string xml = @"<?xml version='1.0' standalone='no'?><foo _1='1' _2='2' _3='3' />";
  1176. RunTest (xml, new TestMethod (IndexerAndAttributes));
  1177. }
  1178. public void IndexerAndAttributes (XmlReader xmlReader)
  1179. {
  1180. Assert (xmlReader.Read ());
  1181. AssertEquals ("1.0", xmlReader ["version"]);
  1182. AssertEquals ("1.0", xmlReader.GetAttribute ("version"));
  1183. // .NET 1.1 BUG. XmlTextReader returns null, while XmlNodeReader returns "".
  1184. AssertEquals (null, xmlReader ["encoding"]);
  1185. AssertEquals (null, xmlReader.GetAttribute ("encoding"));
  1186. AssertEquals ("no", xmlReader ["standalone"]);
  1187. AssertEquals ("no", xmlReader.GetAttribute ("standalone"));
  1188. AssertEquals ("1.0", xmlReader [0]);
  1189. AssertEquals ("1.0", xmlReader.GetAttribute (0));
  1190. AssertEquals ("no", xmlReader [1]);
  1191. AssertEquals ("no", xmlReader.GetAttribute (1));
  1192. Assert (xmlReader.Read ());
  1193. AssertEquals (XmlNodeType.Element, xmlReader.NodeType);
  1194. AssertEquals ("1", xmlReader ["_1"]);
  1195. Assert (xmlReader.MoveToFirstAttribute ());
  1196. AssertEquals ("_1", xmlReader.Name);
  1197. AssertEquals ("1", xmlReader ["_1"]);
  1198. Assert (xmlReader.MoveToNextAttribute ());
  1199. AssertEquals ("_2", xmlReader.Name);
  1200. AssertEquals ("1", xmlReader ["_1"]);
  1201. Assert (xmlReader.MoveToNextAttribute ());
  1202. AssertEquals ("_3", xmlReader.Name);
  1203. AssertEquals ("1", xmlReader ["_1"]);
  1204. Assert (!xmlReader.MoveToNextAttribute ());
  1205. }
  1206. [Test]
  1207. public void ProhibitedMultipleAttributes ()
  1208. {
  1209. string xml = @"<foo _1='1' _1='1' />";
  1210. try {
  1211. RunTest (xml, new TestMethod (ReadAll));
  1212. } catch (XmlException) {
  1213. }
  1214. xml = @"<foo _1='1' _1='2' />";
  1215. try {
  1216. RunTest (xml, new TestMethod (ReadAll));
  1217. } catch (XmlException) {
  1218. }
  1219. }
  1220. public void ReadAll (XmlReader xmlReader)
  1221. {
  1222. while (!xmlReader.EOF)
  1223. xmlReader.Read ();
  1224. }
  1225. [Test]
  1226. public void SurrogatePairContent ()
  1227. {
  1228. string xml = "<root xmlns='&#x10100;'/>";
  1229. RunTest (xml, new TestMethod (SurrogatePairContent));
  1230. }
  1231. public void SurrogatePairContent (XmlReader xmlReader)
  1232. {
  1233. xmlReader.Read ();
  1234. AssertEquals (true, xmlReader.MoveToAttribute ("xmlns"));
  1235. AssertEquals ("xmlns", xmlReader.Name);
  1236. AssertEquals (2, xmlReader.Value.Length);
  1237. AssertEquals (0xD800, (int) xmlReader.Value [0]);
  1238. AssertEquals (0xDD00, (int) xmlReader.Value [1]);
  1239. }
  1240. [Test]
  1241. public void ReadOuterXmlOnEndElement ()
  1242. {
  1243. string xml = "<root><foo></foo></root>";
  1244. RunTest (xml, new TestMethod (ReadOuterXmlOnEndElement));
  1245. }
  1246. public void ReadOuterXmlOnEndElement (XmlReader xmlReader)
  1247. {
  1248. xmlReader.Read ();
  1249. xmlReader.Read ();
  1250. xmlReader.Read ();
  1251. AssertEquals (String.Empty, xmlReader.ReadOuterXml ());
  1252. }
  1253. [Test]
  1254. public void ReadInnerXmlOnEndElement ()
  1255. {
  1256. string xml = "<root><foo></foo></root>";
  1257. RunTest (xml, new TestMethod (ReadInnerXmlOnEndElement));
  1258. }
  1259. private void ReadInnerXmlOnEndElement (XmlReader xmlReader)
  1260. {
  1261. xmlReader.Read ();
  1262. xmlReader.Read ();
  1263. xmlReader.Read ();
  1264. AssertEquals (String.Empty, xmlReader.ReadInnerXml ());
  1265. }
  1266. [Test]
  1267. public void LookupEmptyPrefix ()
  1268. {
  1269. string xml = "<root><foo></foo></root>";
  1270. RunTest (xml, new TestMethod (LookupEmptyPrefix));
  1271. }
  1272. void LookupEmptyPrefix (XmlReader xmlReader)
  1273. {
  1274. xmlReader.Read ();
  1275. AssertNull (xmlReader.LookupNamespace (String.Empty));
  1276. }
  1277. [Test]
  1278. public void ReadStartElement ()
  1279. {
  1280. string xml = "<root>test</root>";
  1281. RunTest (xml, new TestMethod (ReadStartElement));
  1282. }
  1283. void ReadStartElement (XmlReader xr)
  1284. {
  1285. xr.Read ();
  1286. xr.ReadStartElement ();
  1287. // consume Element node.
  1288. AssertEquals (XmlNodeType.Text, xr.NodeType);
  1289. }
  1290. [Test]
  1291. public void LookupNamespaceAtEndElement ()
  1292. {
  1293. string xml = "<root xmlns:x='urn:foo'><foo/></root>";
  1294. RunTest (xml, new TestMethod (LookupNamespaceAtEndElement));
  1295. }
  1296. void LookupNamespaceAtEndElement (XmlReader reader)
  1297. {
  1298. reader.Read ();
  1299. AssertEquals ("#1", "urn:foo", reader.LookupNamespace ("x"));
  1300. reader.Read ();
  1301. AssertEquals ("#2", "urn:foo", reader.LookupNamespace ("x"));
  1302. reader.Read ();
  1303. AssertEquals ("#3", "urn:foo", reader.LookupNamespace ("x"));
  1304. }
  1305. #if NET_2_0
  1306. [Test]
  1307. public void CreateSimple ()
  1308. {
  1309. XmlReaderSettings s = new XmlReaderSettings ();
  1310. s.ProhibitDtd = false;
  1311. XmlReader xr = XmlReader.Create ("Test/XmlFiles/nested-dtd-test.xml", s);
  1312. xr.Read ();
  1313. AssertEquals ("#1", XmlNodeType.DocumentType, xr.NodeType);
  1314. xr.Read ();
  1315. AssertEquals ("#2", XmlNodeType.Whitespace, xr.NodeType);
  1316. xr.Read ();
  1317. AssertEquals ("#3", XmlNodeType.Element, xr.NodeType);
  1318. }
  1319. [Test]
  1320. [ExpectedException (typeof (XmlException))]
  1321. public void CreateSimpleProhibitDtd ()
  1322. {
  1323. XmlReader xr = XmlReader.Create ("Test/XmlFiles/nested-dtd-test.xml");
  1324. xr.Read ();
  1325. }
  1326. [Test]
  1327. // a bit revised version of bug #78706
  1328. public void CreateFromUrlClose ()
  1329. {
  1330. string file = "Test/XmlFiles/78706.xml";
  1331. try {
  1332. if (!File.Exists (file))
  1333. File.Create (file).Close ();
  1334. XmlReaderSettings s = new XmlReaderSettings ();
  1335. s.CloseInput = false; // explicitly
  1336. XmlReader r = XmlReader.Create (file, s);
  1337. r.Close ();
  1338. XmlTextWriter w = new XmlTextWriter (file, null);
  1339. w.Close ();
  1340. } finally {
  1341. if (File.Exists (file))
  1342. File.Delete (file);
  1343. }
  1344. }
  1345. [Test]
  1346. public void ReadToDescendant ()
  1347. {
  1348. string xml = @"<root><foo/><bar/><foo> test text <bar><bar></bar></bar></foo></root>";
  1349. RunTest (xml, new TestMethod (ReadToDescendant));
  1350. }
  1351. void ReadToDescendant (XmlReader xmlReader)
  1352. {
  1353. // move to first <bar/>
  1354. Assert ("#1", xmlReader.ReadToDescendant ("bar"));
  1355. // no children in <bar/>. It is empty.
  1356. Assert ("#2", !xmlReader.ReadToDescendant ("bar"));
  1357. AssertEquals ("#2-2", "bar", xmlReader.Name);
  1358. // move to the second <foo>
  1359. xmlReader.Read ();
  1360. // move to the second <bar>
  1361. Assert ("#3", xmlReader.ReadToDescendant ("bar"));
  1362. // move to <bar> inside <bar>...</bar>
  1363. Assert ("#4", xmlReader.ReadToDescendant ("bar"));
  1364. // the next is EndElement of </bar>, so no move.
  1365. Assert ("#5", !xmlReader.ReadToDescendant ("bar"));
  1366. AssertEquals ("#5-2", XmlNodeType.EndElement, xmlReader.NodeType);
  1367. }
  1368. [Test]
  1369. public void ReadToDescepdant2 ()
  1370. {
  1371. string xml = "<root/>";
  1372. RunTest (xml, new TestMethod (ReadToDescendant2));
  1373. }
  1374. void ReadToDescendant2 (XmlReader xmlReader)
  1375. {
  1376. // make sure that it works when the reader is at Initial state.
  1377. Assert (xmlReader.ReadToDescendant ("root"));
  1378. }
  1379. [Test]
  1380. public void ReadToFollowing ()
  1381. {
  1382. string xml = @"<root><foo/><bar/><foo><bar><bar></bar></bar></foo></root>";
  1383. RunTest (xml, new TestMethod (ReadToFollowing));
  1384. }
  1385. public void ReadToFollowing (XmlReader xmlReader)
  1386. {
  1387. Assert ("#1", xmlReader.ReadToFollowing ("bar"));
  1388. Assert ("#2", xmlReader.ReadToFollowing ("bar"));
  1389. AssertEquals ("#2-2", 2, xmlReader.Depth);
  1390. Assert ("#3", xmlReader.ReadToFollowing ("bar"));
  1391. AssertEquals ("#3-2", 3, xmlReader.Depth);
  1392. Assert ("#4", !xmlReader.ReadToFollowing ("bar"));
  1393. }
  1394. [Test]
  1395. [Category ("NotDotNet")]
  1396. public void ReadToNextSiblingAtInitialState ()
  1397. {
  1398. string xml = @"<root></root>";
  1399. RunTest (xml, new TestMethod (ReadToNextSiblingAtInitialState ));
  1400. }
  1401. void ReadToNextSiblingAtInitialState (XmlReader xmlReader)
  1402. {
  1403. Assert ("#1", !xmlReader.ReadToNextSibling ("bar"));
  1404. Assert ("#2", !xmlReader.ReadToNextSibling ("root"));
  1405. }
  1406. [Test]
  1407. public void ReadToNextSibling ()
  1408. {
  1409. string xml = @"<root><foo/><bar attr='value'/><foo><pooh/><bar></bar><foo></foo><bar/></foo></root>";
  1410. RunTest (xml, new TestMethod (ReadToNextSibling));
  1411. }
  1412. void ReadToNextSibling (XmlReader xmlReader)
  1413. {
  1414. // It is funky, but without it MS.NET results in an infinite loop.
  1415. xmlReader.Read (); // root
  1416. xmlReader.Read (); // foo
  1417. Assert ("#3", xmlReader.ReadToNextSibling ("bar"));
  1418. AssertEquals ("#3-2", "value", xmlReader.GetAttribute ("attr"));
  1419. xmlReader.Read (); // foo
  1420. xmlReader.Read (); // pooh
  1421. Assert ("#4", xmlReader.ReadToNextSibling ("bar"));
  1422. Assert ("#4-2", !xmlReader.IsEmptyElement);
  1423. Assert ("#5", xmlReader.ReadToNextSibling ("bar"));
  1424. Assert ("#5-2", xmlReader.IsEmptyElement);
  1425. Assert ("#6", xmlReader.Read ()); // /foo
  1426. AssertNodeValues ("#7", xmlReader,
  1427. XmlNodeType.EndElement,
  1428. 1, // Depth
  1429. false, // IsEmptyElement
  1430. "foo", // Name
  1431. String.Empty, // Prefix
  1432. "foo", // LocalName
  1433. String.Empty, // NamespaceURI
  1434. String.Empty, // Value
  1435. false, // HasValue
  1436. 0, // AttributeCount
  1437. false); // HasAttributes
  1438. }
  1439. [Test]
  1440. public void ReadSubtree ()
  1441. {
  1442. string xml = @"<root><foo/><bar attr='value'></bar></root>";
  1443. RunTest (xml, new TestMethod (ReadSubtree));
  1444. }
  1445. void ReadSubtree (XmlReader reader)
  1446. {
  1447. reader.MoveToContent (); // root
  1448. reader.Read (); // foo
  1449. XmlReader st = reader.ReadSubtree (); // <foo/>
  1450. // MS bug: IsEmptyElement should be false here.
  1451. /*
  1452. AssertNodeValues ("#1", st,
  1453. XmlNodeType.None,
  1454. 0, // Depth
  1455. false, // IsEmptyElement
  1456. String.Empty, // Name
  1457. String.Empty, // Prefix
  1458. String.Empty, // LocalName
  1459. String.Empty, // NamespaceURI
  1460. String.Empty, // Value
  1461. false, // HasValue
  1462. 0, // AttributeCount
  1463. false); // HasAttributes
  1464. */
  1465. AssertEquals ("#1", XmlNodeType.None, st.NodeType);
  1466. st.Read ();
  1467. AssertNodeValues ("#2", st,
  1468. XmlNodeType.Element,
  1469. 0,
  1470. true, // IsEmptyElement
  1471. "foo", // Name
  1472. String.Empty, // Prefix
  1473. "foo", // LocalName
  1474. String.Empty, // NamespaceURI
  1475. String.Empty, // Value
  1476. false, // HasValue
  1477. 0, // AttributeCount
  1478. false); // HasAttributes
  1479. Assert ("#3", !st.Read ());
  1480. // At this state, reader is not positioned on <bar> yet
  1481. AssertNodeValues ("#3-2", reader,
  1482. XmlNodeType.Element,
  1483. 1, // Depth. It is 1 for main tree.
  1484. true, // IsEmptyElement
  1485. "foo", // Name
  1486. String.Empty, // Prefix
  1487. "foo", // LocalName
  1488. String.Empty, // NamespaceURI
  1489. String.Empty, // Value
  1490. false, // HasValue
  1491. 0, // AttributeCount
  1492. false); // HasAttributes
  1493. reader.Read ();
  1494. AssertNodeValues ("#4", reader,
  1495. XmlNodeType.Element,
  1496. 1, // Depth. It is 1 for main tree.
  1497. false, // IsEmptyElement
  1498. "bar", // Name
  1499. String.Empty, // Prefix
  1500. "bar", // LocalName
  1501. String.Empty, // NamespaceURI
  1502. String.Empty, // Value
  1503. false, // HasValue
  1504. 1, // AttributeCount
  1505. true); // HasAttributes
  1506. st = reader.ReadSubtree ();
  1507. st.Read (); // Initial -> Interactive
  1508. AssertNodeValues ("#5", st,
  1509. XmlNodeType.Element,
  1510. 0, // Depth. It is 0 for subtree.
  1511. false, // IsEmptyElement
  1512. "bar", // Name
  1513. String.Empty, // Prefix
  1514. "bar", // LocalName
  1515. String.Empty, // NamespaceURI
  1516. String.Empty, // Value
  1517. false, // HasValue
  1518. 1, // AttributeCount
  1519. true); // HasAttributes
  1520. st.Read ();
  1521. AssertNodeValues ("#6-1", st,
  1522. XmlNodeType.EndElement,
  1523. 0, // Depth. It is 0 for subtree.
  1524. false, // IsEmptyElement
  1525. "bar", // Name
  1526. String.Empty, // Prefix
  1527. "bar", // LocalName
  1528. String.Empty, // NamespaceURI
  1529. String.Empty, // Value
  1530. false, // HasValue
  1531. 0, // AttributeCount
  1532. false); // HasAttributes
  1533. AssertNodeValues ("#6-2", st,
  1534. XmlNodeType.EndElement,
  1535. 0, // Depth. It is 0 for subtree.
  1536. false, // IsEmptyElement
  1537. "bar", // Name
  1538. String.Empty, // Prefix
  1539. "bar", // LocalName
  1540. String.Empty, // NamespaceURI
  1541. String.Empty, // Value
  1542. false, // HasValue
  1543. 0, // AttributeCount
  1544. false); // HasAttributes
  1545. Assert ("#7", !st.Read ());
  1546. }
  1547. [Test]
  1548. public void ReadInteger ()
  1549. {
  1550. string xml1 = "<root>1</root>";
  1551. XmlReader xr;
  1552. xr = XmlReader.Create (new StringReader (xml1));
  1553. xr.Read ();
  1554. AssertEquals ("#1", "1", xr.ReadElementContentAsString ());
  1555. AssertNodeValues ("#1-2", xr,
  1556. XmlNodeType.None,
  1557. 0, // Depth. It is 0 for subtree.
  1558. false, // IsEmptyElement
  1559. String.Empty, // Name
  1560. String.Empty, // Prefix
  1561. String.Empty, // LocalName
  1562. String.Empty, // NamespaceURI
  1563. String.Empty, // Value
  1564. false, // HasValue
  1565. 0, // AttributeCount
  1566. false); // HasAttributes
  1567. xr = XmlReader.Create (new StringReader (xml1));
  1568. xr.Read ();
  1569. // this XmlReader has no schema, thus the value is untyped
  1570. AssertEquals ("#2", "1", xr.ReadElementContentAsObject ());
  1571. xr = XmlReader.Create (new StringReader (xml1));
  1572. xr.Read ();
  1573. xr.Read ();
  1574. AssertEquals ("#3", "1", xr.ReadContentAsString ());
  1575. xr = XmlReader.Create (new StringReader (xml1));
  1576. xr.Read ();
  1577. AssertEquals ("#4", 1, xr.ReadElementContentAsInt ());
  1578. xr = XmlReader.Create (new StringReader (xml1));
  1579. xr.Read ();
  1580. AssertEquals ("#5", 1, xr.ReadElementContentAs (typeof (int), null));
  1581. }
  1582. [Test]
  1583. [ExpectedException (typeof (XmlException))]
  1584. public void ReadContentAsIntFail ()
  1585. {
  1586. XmlReader xr = XmlReader.Create (
  1587. new StringReader ("<doc>1.0</doc>"));
  1588. xr.Read ();
  1589. xr.ReadElementContentAsInt ();
  1590. }
  1591. [Test]
  1592. public void ReadDateTime ()
  1593. {
  1594. DateTime time = new DateTime (2006, 1, 2, 3, 4, 56);
  1595. string xml1 = "<root>2006-01-02T03:04:56</root>";
  1596. XmlReader xr;
  1597. xr = XmlReader.Create (new StringReader (xml1));
  1598. xr.Read ();
  1599. // this XmlReader has no schema, thus the value is untyped
  1600. AssertEquals ("#1", "2006-01-02T03:04:56",
  1601. xr.ReadElementContentAsString ());
  1602. xr = XmlReader.Create (new StringReader (xml1));
  1603. xr.Read ();
  1604. xr.Read ();
  1605. AssertEquals ("#2", time, xr.ReadContentAsDateTime ());
  1606. xr = XmlReader.Create (new StringReader (xml1));
  1607. xr.Read ();
  1608. AssertEquals ("#3", time, xr.ReadElementContentAsDateTime ());
  1609. xr = XmlReader.Create (new StringReader (xml1));
  1610. xr.Read ();
  1611. AssertEquals ("#4", time, xr.ReadElementContentAs (typeof (DateTime), null));
  1612. }
  1613. [Test]
  1614. [ExpectedException (typeof (XmlException))]
  1615. public void ReadContentAsDateTimeFail ()
  1616. {
  1617. XmlReader xr = XmlReader.Create (
  1618. new StringReader ("<doc>P1Y2M3D</doc>"));
  1619. xr.Read ();
  1620. xr.ReadElementContentAsDateTime ();
  1621. }
  1622. [Test]
  1623. public void ReadContentAs_QNameEmptyNSResolver ()
  1624. {
  1625. XmlReader xr = XmlReader.Create (
  1626. new StringReader ("<doc xmlns:x='urn:foo'>x:el</doc>"));
  1627. xr.Read ();
  1628. object o = xr.ReadElementContentAs (
  1629. typeof (XmlQualifiedName), null);
  1630. // without IXmlNamespaceResolver, it still resolves
  1631. // x:el as valid QName.
  1632. AssertNotNull ("#1", o);
  1633. XmlQualifiedName q = o as XmlQualifiedName;
  1634. AssertEquals ("#2 : " + o.GetType (),
  1635. new XmlQualifiedName ("el", "urn:foo"), q);
  1636. }
  1637. [Test]
  1638. [ExpectedException (typeof (InvalidOperationException))]
  1639. public void ReadContentStringOnElementFail ()
  1640. {
  1641. XmlReader xr = XmlReader.Create (new StringReader ("<a>test</a>"));
  1642. xr.Read ();
  1643. xr.ReadContentAsString ();
  1644. }
  1645. [Test]
  1646. [ExpectedException (typeof (XmlException))]
  1647. public void ReadElementContentStringMixedContent ()
  1648. {
  1649. XmlReader xr = XmlReader.Create (
  1650. new StringReader ("<doc>123<child>456</child>789</doc>"));
  1651. xr.Read ();
  1652. // "child" is regarded as an invalid node.
  1653. string s = xr.ReadElementContentAsString ();
  1654. }
  1655. [Test]
  1656. public void ReadContentStringMixedContent ()
  1657. {
  1658. XmlReader xr = XmlReader.Create (
  1659. new StringReader ("<doc>123<child>456</child>789</doc>"));
  1660. xr.Read ();
  1661. xr.Read (); // from Text "123"
  1662. string s = xr.ReadContentAsString ();
  1663. AssertEquals ("#1", "123", s);
  1664. AssertEquals ("#2", XmlNodeType.Element, xr.NodeType);
  1665. }
  1666. [Test]
  1667. public void ReadElementContentAsString ()
  1668. {
  1669. XmlTextReader r = new XmlTextReader (
  1670. "<root/>", XmlNodeType.Document, null);
  1671. r.Read ();
  1672. AssertEquals ("#1",
  1673. String.Empty, r.ReadElementContentAsString ());
  1674. AssertEquals ("#2",
  1675. XmlNodeType.None, r.NodeType);
  1676. }
  1677. [Test]
  1678. public void ReadElementContentAs ()
  1679. {
  1680. // as System.Object
  1681. XmlTextReader r = new XmlTextReader (
  1682. "<root/>", XmlNodeType.Document, null);
  1683. r.Read ();
  1684. AssertEquals ("#1",
  1685. String.Empty, r.ReadElementContentAs (typeof (object), null));
  1686. AssertEquals ("#2",
  1687. XmlNodeType.None, r.NodeType);
  1688. // regardless of its value, the return value is string.
  1689. r = new XmlTextReader ("<root>1</root>", XmlNodeType.Document, null);
  1690. r.Read ();
  1691. AssertEquals ("#3",
  1692. "1", r.ReadElementContentAs (typeof (object), null));
  1693. AssertEquals ("#4",
  1694. XmlNodeType.None, r.NodeType);
  1695. }
  1696. [Test]
  1697. public void ReadContentStringOnAttribute ()
  1698. {
  1699. string xml = @"<root id='myId'><child /></root>";
  1700. RunTest (xml, new TestMethod (ReadContentStringOnAttribute));
  1701. }
  1702. void ReadContentStringOnAttribute (XmlReader reader)
  1703. {
  1704. reader.Read ();
  1705. Assert (reader.MoveToAttribute ("id"));
  1706. AssertEquals ("myId", reader.ReadContentAsString ());
  1707. }
  1708. #endif
  1709. }
  1710. }