PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.Xml.XDocument/tests/xNodeReader/ReadToNextSibling.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 353 lines | 285 code | 50 blank | 18 comment | 27 complexity | 171b3eb2106e84daf0b46cc4a0c51c05 MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.IO;
  6. using System.Xml;
  7. using Microsoft.Test.ModuleCore;
  8. namespace CoreXml.Test.XLinq
  9. {
  10. public partial class FunctionalTests : TestModule
  11. {
  12. public partial class XNodeReaderTests : XLinqTestCase
  13. {
  14. //[TestCase(Name = "ReadToNextSibling", Desc = "ReadToNextSibling")]
  15. public partial class TCReadToNextSibling : BridgeHelpers
  16. {
  17. #region XMLSTR
  18. private string _xmlStr = @"<?xml version='1.0'?>
  19. <root><!--Comment-->
  20. <elem>
  21. <child1 att='1'>
  22. <child2 xmlns='child2'>
  23. <child3/>
  24. blahblahblah
  25. <child4/>
  26. </child2>
  27. <?pi target1?>
  28. </child1>
  29. </elem>
  30. <elem att='1'>
  31. <child1 att='1'>
  32. <child2 xmlns='child2'>
  33. <child3/>
  34. blahblahblah
  35. <child4/>
  36. </child2>
  37. <?pi target1?>
  38. </child1>
  39. </elem>
  40. <elem xmlns='elem'>
  41. <child1 att='1'>
  42. <child2 xmlns='child2'>
  43. <child3/>
  44. blahblahblah2
  45. <child4/>
  46. </child2>
  47. </child1>
  48. </elem>
  49. <elem xmlns='elem' att='1'>
  50. <child1 att='1'>
  51. <child2 xmlns='child2'>
  52. <child3/>
  53. blahblahblah2
  54. <child4/>
  55. </child2>
  56. </child1>
  57. </elem>
  58. <e:elem xmlns:e='elem2'>
  59. <e:child1 att='1'>
  60. <e:child2 xmlns='child2'>
  61. <e:child3/>
  62. blahblahblah2
  63. <e:child4/>
  64. </e:child2>
  65. </e:child1>
  66. </e:elem>
  67. <e:elem xmlns:e='elem2' att='1'>
  68. <e:child1 att='1'>
  69. <e:child2 xmlns='child2'>
  70. <e:child3/>
  71. blahblahblah2
  72. <e:child4/>
  73. </e:child2>
  74. </e:child1>
  75. </e:elem>
  76. </root>";
  77. #endregion
  78. //[Variation("Simple positive test 1", Priority = 0, Params = new object[] { "NNS" })]
  79. //[Variation("Simple positive test 2", Priority = 0, Params = new object[] { "DNS" })]
  80. //[Variation("Simple positive test 3", Priority = 0, Params = new object[] { "NS" })]
  81. public void v()
  82. {
  83. string type = Variation.Params[0].ToString();
  84. XmlReader DataReader = GetReader(new StringReader(_xmlStr));
  85. PositionOnElement(DataReader, "root");
  86. switch (type)
  87. {
  88. case "NNS":
  89. DataReader.ReadToDescendant("elem");
  90. DataReader.ReadToNextSibling("elem");
  91. if (DataReader.HasAttributes)
  92. {
  93. TestLog.Compare(DataReader.GetAttribute("att"), "1", "Not the expected attribute");
  94. }
  95. else
  96. {
  97. TestLog.WriteLine("Positioned on wrong element");
  98. throw new TestException(TestResult.Failed, "");
  99. }
  100. while (DataReader.Read()) ;
  101. DataReader.Dispose();
  102. return;
  103. case "DNS":
  104. DataReader.ReadToDescendant("elem", "elem");
  105. DataReader.ReadToNextSibling("elem", "elem");
  106. if (DataReader.HasAttributes)
  107. {
  108. if (DataReader.GetAttribute("att") == null)
  109. {
  110. TestLog.WriteLine("Positioned on wrong element, not on DNS");
  111. throw new TestException(TestResult.Failed, "");
  112. }
  113. }
  114. else
  115. {
  116. TestLog.WriteLine("Positioned on wrong element");
  117. throw new TestException(TestResult.Failed, "");
  118. }
  119. while (DataReader.Read()) ;
  120. DataReader.Dispose();
  121. return;
  122. case "NS":
  123. DataReader.ReadToDescendant("e:elem");
  124. DataReader.ReadToNextSibling("e:elem");
  125. if (DataReader.HasAttributes)
  126. {
  127. if (DataReader.GetAttribute("xmlns:e") == null)
  128. {
  129. TestLog.WriteLine("Positioned on wrong element, not on DNS");
  130. throw new TestException(TestResult.Failed, "");
  131. }
  132. }
  133. else
  134. {
  135. TestLog.WriteLine("Positioned on wrong element");
  136. throw new TestException(TestResult.Failed, "");
  137. }
  138. while (DataReader.Read()) ;
  139. DataReader.Dispose();
  140. return;
  141. default:
  142. throw new TestFailedException("Error in Test type");
  143. }
  144. }
  145. //[Variation("Read on a deep tree at least more than 4K boundary", Priority = 2)]
  146. public void v2()
  147. {
  148. ManagedNodeWriter mnw = new ManagedNodeWriter();
  149. mnw.PutPattern("X");
  150. int count = 0;
  151. do
  152. {
  153. mnw.PutPattern("E/");
  154. count++;
  155. } while (mnw.GetNodes().Length < 4096);
  156. mnw.PutText("<a/><b/>");
  157. mnw.Finish();
  158. XmlReader DataReader = GetReader(new StringReader(mnw.GetNodes()));
  159. PositionOnElement(DataReader, "ELEMENT_1");
  160. TestLog.Compare(DataReader.ReadToDescendant("a"), true, "Couldnt go to Descendant");
  161. int depth = DataReader.Depth;
  162. TestLog.Compare(DataReader.ReadToNextSibling("b"), true, "Couldnt go to NextSibling");
  163. TestLog.Compare(DataReader.Depth, depth, "Depth is not correct");
  164. TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Nodetype is not correct");
  165. while (DataReader.Read()) ;
  166. DataReader.Dispose();
  167. }
  168. //[Variation("Read to next sibling with same names 1", Priority = 1, Params = new object[] { "NNS", "<root><a att='1'/><a att='2'/><a att='3'/></root>" })]
  169. //[Variation("Read on next sibling with same names 2", Priority = 1, Params = new object[] { "DNS", "<root xmlns='a'><a att='1'/><a att='2'/><a att='3'/></root>" })]
  170. //[Variation("Read on next sibling with same names 3", Priority = 1, Params = new object[] { "NS", "<root xmlns:a='a'><a:a att='1'/><a:a att='2'/><a:a att='3'/></root>" })]
  171. public void v3()
  172. {
  173. string type = Variation.Params[0].ToString();
  174. string xml = Variation.Params[1].ToString();
  175. XmlReader DataReader = GetReader(new StringReader(xml));
  176. DataReader.Read();
  177. // Doing a sequential read.
  178. switch (type)
  179. {
  180. case "NNS":
  181. DataReader.ReadToDescendant("a");
  182. DataReader.ReadToNextSibling("a");
  183. DataReader.ReadToNextSibling("a");
  184. TestLog.Compare(DataReader.GetAttribute("att"), "3", "Wrong node");
  185. while (DataReader.Read()) ;
  186. DataReader.Dispose();
  187. return;
  188. case "DNS":
  189. DataReader.ReadToDescendant("a", "a");
  190. DataReader.ReadToNextSibling("a", "a");
  191. DataReader.ReadToNextSibling("a", "a");
  192. TestLog.Compare(DataReader.GetAttribute("att"), "3", "Wrong node");
  193. while (DataReader.Read()) ;
  194. DataReader.Dispose();
  195. return;
  196. case "NS":
  197. DataReader.ReadToDescendant("a:a");
  198. DataReader.ReadToNextSibling("a:a");
  199. DataReader.ReadToNextSibling("a:a");
  200. TestLog.Compare(DataReader.GetAttribute("att"), "3", "Wrong node");
  201. while (DataReader.Read()) ;
  202. DataReader.Dispose();
  203. return;
  204. default:
  205. throw new TestFailedException("Error in Test type");
  206. }
  207. }
  208. //[Variation("If name not found, stop at end element of the subtree", Priority = 1)]
  209. public void v4()
  210. {
  211. XmlReader DataReader = GetReader(new StringReader(_xmlStr));
  212. PositionOnElement(DataReader, "elem");
  213. int depth = DataReader.Depth;
  214. TestLog.Compare(DataReader.ReadToNextSibling("abc"), false, "Reader returned true for an invalid name");
  215. TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
  216. TestLog.Compare(DataReader.Depth, depth - 1, "Wrong Depth");
  217. while (DataReader.Read()) ;
  218. DataReader.Dispose();
  219. }
  220. //[Variation("Positioning on a level and try to find the name which is on a level higher", Priority = 1)]
  221. public void v5()
  222. {
  223. XmlReader DataReader = GetReader(new StringReader(_xmlStr));
  224. PositionOnElement(DataReader, "child3");
  225. TestLog.Compare(DataReader.ReadToNextSibling("child1"), false, "Reader returned true for an invalid name");
  226. TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
  227. PositionOnElement(DataReader, "child3");
  228. TestLog.Compare(DataReader.ReadToNextSibling("child2", "child2"), false, "Reader returned true for an invalid name,ns");
  229. TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type for name,ns");
  230. while (DataReader.Read()) ;
  231. DataReader.Dispose();
  232. }
  233. //[Variation("Read to next sibling on one level and again to level below it", Priority = 1)]
  234. public void v6()
  235. {
  236. XmlReader DataReader = GetReader(new StringReader(_xmlStr));
  237. PositionOnElement(DataReader, "root");
  238. TestLog.Compare(DataReader.ReadToDescendant("elem"), true, "Cant find elem");
  239. TestLog.Compare(DataReader.ReadToNextSibling("elem", "elem"), true, "Cant find elem,elem");
  240. TestLog.Compare(DataReader.ReadToNextSibling("e:elem"), true, "Cant find e:elem");
  241. while (DataReader.Read()) ;
  242. DataReader.Dispose();
  243. }
  244. //[Variation("Call from different nodetypes", Priority = 1)]
  245. public void v12()
  246. {
  247. XmlReader DataReader = GetReader(new StringReader(_xmlStr));
  248. while (DataReader.Read())
  249. {
  250. if (DataReader.NodeType != XmlNodeType.Element)
  251. {
  252. TestLog.Compare(DataReader.ReadToNextSibling("abc"), false, "Fails on node");
  253. }
  254. else
  255. {
  256. if (DataReader.HasAttributes)
  257. {
  258. while (DataReader.MoveToNextAttribute())
  259. {
  260. TestLog.Compare(DataReader.ReadToNextSibling("abc"), false, "Fails on attribute node");
  261. }
  262. }
  263. }
  264. }
  265. DataReader.Dispose();
  266. }
  267. //[Variation("Pass null to both arguments throws ArgumentException", Priority = 2)]
  268. public void v16()
  269. {
  270. XmlReader DataReader = GetReader(new StringReader("<root><e/></root>"));
  271. DataReader.Read();
  272. try
  273. {
  274. DataReader.ReadToNextSibling(null);
  275. }
  276. catch (ArgumentNullException)
  277. {
  278. }
  279. try
  280. {
  281. DataReader.ReadToNextSibling("e", null);
  282. }
  283. catch (ArgumentNullException)
  284. {
  285. }
  286. while (DataReader.Read()) ;
  287. DataReader.Dispose();
  288. }
  289. //[Variation("Different names, same uri works correctly", Priority = 2)]
  290. public void v17()
  291. {
  292. XmlReader DataReader = GetReader(new StringReader("<root><child1 xmlns='foo'/>blah<child1 xmlns='bar'>blah</child1></root>"));
  293. DataReader.Read();
  294. DataReader.ReadToDescendant("child1", "foo");
  295. DataReader.ReadToNextSibling("child1", "bar");
  296. TestLog.Compare(DataReader.IsEmptyElement, false, "Not on the correct node");
  297. while (DataReader.Read()) ;
  298. DataReader.Dispose();
  299. }
  300. }
  301. }
  302. }
  303. }