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 
5 using System;
6 using System.IO;
7 using System.Xml;
8 using Microsoft.Test.ModuleCore;
9 
10 namespace CoreXml.Test.XLinq
11 {
12     public partial class FunctionalTests : TestModule
13     {
14 
15         public partial class XNodeReaderTests : XLinqTestCase
16         {
17             //[TestCase(Name = "ReadToDescendant", Desc = "ReadToDescendant")]
18             public partial class TCReadToDescendant : BridgeHelpers
19             {
20                 #region XMLSTR
21                 private string _xmlStr = @"<?xml version='1.0'?>
22 													<root><!--Comment-->
23 														<elem><!-- Comment -->
24 															<child1 att='1'><?pi target?>
25 																<child2 xmlns='child2'>
26 																	<child3/>
27 																	blahblahblah<![CDATA[ blah ]]>
28 																	<child4/>
29 																</child2>
30 															<?pi target1?>
31 															</child1>
32 														</elem>
33 														<elem att='1'>
34 															<child1 att='1'>
35 																<child2 xmlns='child2'>
36 																	<child3/>
37 																	blahblahblah
38 																	<child4/>
39 																</child2>
40 															<?pi target1?>
41 															</child1>
42 														</elem>
43 														<elem xmlns='elem'>
44 															<child1 att='1'>
45 																<child2 xmlns='child2'>
46 																	<child3/>
47 																	blahblahblah2
48 																	<child4/>
49 																</child2>
50 															</child1>
51 														</elem>
52 														<elem xmlns='elem' att='1'>
53 															<child1 att='1'>
54 																<child2 xmlns='child2'>
55 																	<child3/>
56 																	blahblahblah2
57 																	<child4/>
58 																</child2>
59 															</child1>
60 														</elem>
61 														<e:elem xmlns:e='elem2'>
62 															<e:child1 att='1'>
63 																<e:child2 xmlns='child2'>
64 																	<e:child3/>
65 																	blahblahblah2
66 																	<e:child4/>
67 																</e:child2>
68 															</e:child1>
69 														</e:elem>
70 														<e:elem xmlns:e='elem2' att='1'>
71 															<e:child1 att='1'>
72 																<e:child2 xmlns='child2'>
73 																	<e:child3/>
74 																	blahblahblah2
75 																	<e:child4/>
76 																</e:child2>
77 															</e:child1>
78 														</e:elem>
79 													</root>";
80 
81                 #endregion
82 
83                 //[Variation("Simple positive test", Priority = 0, Params = new object[] { "NNS" })]
84                 //[Variation("Simple positive test", Priority = 0, Params = new object[] { "DNS" })]
85                 //[Variation("Simple positive test", Priority = 0, Params = new object[] { "NS" })]
v()86                 public void v()
87                 {
88                     string type = Variation.Params[0].ToString();
89 
90                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
91                     PositionOnElement(DataReader, "root");
92 
93                     switch (type)
94                     {
95                         case "NNS":
96                             DataReader.ReadToDescendant("elem");
97                             if (DataReader.HasAttributes)
98                             {
99                                 TestLog.WriteLine("Positioned on wrong element");
100                                 TestLog.WriteIgnore(DataReader.ReadInnerXml() + "\n");
101                                 throw new TestException(TestResult.Failed, "");
102                             }
103                             while (DataReader.Read()) ;
104                             DataReader.Dispose();
105                             return;
106 
107                         case "DNS":
108                             DataReader.ReadToDescendant("elem", "elem");
109                             if (DataReader.HasAttributes)
110                             {
111                                 if (DataReader.GetAttribute("xmlns") == null)
112                                 {
113                                     TestLog.WriteLine("Positioned on wrong element, not on DNS");
114                                     throw new TestException(TestResult.Failed, "");
115                                 }
116                             }
117                             while (DataReader.Read()) ;
118                             DataReader.Dispose();
119                             return;
120 
121                         case "NS":
122                             DataReader.ReadToDescendant("e:elem");
123                             if (DataReader.HasAttributes)
124                             {
125                                 if (DataReader.GetAttribute("xmlns:e") == null)
126                                 {
127                                     TestLog.WriteLine("Positioned on wrong element, not on NS");
128                                     throw new TestException(TestResult.Failed, "");
129                                 }
130                             }
131                             while (DataReader.Read()) ;
132                             DataReader.Dispose();
133                             return;
134                         default:
135                             throw new TestFailedException("Error in Test type");
136                     }
137                 }
138 
139                 //[Variation("Read on a deep tree at least more than 4K boundary", Priority = 2)]
v2()140                 public void v2()
141                 {
142                     ManagedNodeWriter mnw = new ManagedNodeWriter();
143                     mnw.PutPattern("X");
144 
145                     int count = 0;
146                     do
147                     {
148                         mnw.PutPattern("E/");
149                         count++;
150                     }
151                     while (mnw.GetNodes().Length < 4096);
152                     mnw.PutText("<a/>");
153                     mnw.Finish();
154 
155                     XmlReader DataReader = GetReader(new StringReader(mnw.GetNodes()));
156 
157                     PositionOnElement(DataReader, "ELEMENT_1");
158                     DataReader.ReadToDescendant("a");
159 
160                     TestLog.Compare(DataReader.Depth, count, "Depth is not correct");
161                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Nodetype is not correct");
162 
163                     while (DataReader.Read()) ;
164                     DataReader.Dispose();
165                 }
166 
167                 //[Variation("Read on descendant with same names", Priority = 1, Params = new object[] { "NNS" })]
168                 //[Variation("Read on descendant with same names", Priority = 1, Params = new object[] { "DNS" })]
169                 //[Variation("Read on descendant with same names", Priority = 1, Params = new object[] { "NS" })]
v3()170                 public void v3()
171                 {
172                     string type = Variation.Params[0].ToString();
173 
174                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
175                     PositionOnElement(DataReader, "root");
176 
177                     // Doing a sequential read.
178                     switch (type)
179                     {
180                         case "NNS":
181                             DataReader.ReadToDescendant("elem");
182                             int depth = DataReader.Depth;
183                             if (DataReader.HasAttributes)
184                             {
185                                 TestLog.WriteLine("Positioned on wrong element");
186                                 throw new TestException(TestResult.Failed, "");
187                             }
188                             TestLog.Compare(DataReader.ReadToDescendant("elem"), false, "There are no more descendants");
189                             TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
190                             while (DataReader.Read()) ;
191                             DataReader.Dispose();
192 
193                             return;
194 
195                         case "DNS":
196                             DataReader.ReadToDescendant("elem", "elem");
197                             if (DataReader.HasAttributes)
198                             {
199                                 if (DataReader.GetAttribute("xmlns") == null)
200                                 {
201                                     TestLog.WriteLine("Positioned on wrong element, not on DNS");
202                                     throw new TestException(TestResult.Failed, "");
203                                 }
204                             }
205                             TestLog.Compare(DataReader.ReadToDescendant("elem", "elem"), false, "There are no more descendants");
206                             TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
207                             while (DataReader.Read()) ;
208                             DataReader.Dispose();
209 
210                             return;
211 
212                         case "NS":
213                             DataReader.ReadToDescendant("e:elem");
214                             if (DataReader.HasAttributes)
215                             {
216                                 if (DataReader.GetAttribute("xmlns:e") == null)
217                                 {
218                                     TestLog.WriteLine("Positioned on wrong element, not on DNS");
219                                     throw new TestException(TestResult.Failed, "");
220                                 }
221                             }
222                             TestLog.Compare(DataReader.ReadToDescendant("e:elem"), false, "There are no more descendants");
223                             TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
224                             while (DataReader.Read()) ;
225                             DataReader.Dispose();
226 
227                             return;
228 
229                         default:
230                             throw new TestFailedException("Error in Test type");
231                     }
232                 }
233 
234                 //[Variation("If name not found, stop at end element of the subtree", Priority = 1)]
v4()235                 public void v4()
236                 {
237                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
238                     PositionOnElement(DataReader, "elem");
239 
240                     TestLog.Compare(DataReader.ReadToDescendant("abc"), false, "Reader returned true for an invalid name");
241                     TestLog.Compare(DataReader.NodeType, XmlNodeType.EndElement, "Wrong node type");
242 
243                     DataReader.Read();
244                     TestLog.Compare(DataReader.ReadToDescendant("abc", "elem"), false, "reader returned true for an invalid name,ns combination");
245 
246                     while (DataReader.Read()) ;
247                     DataReader.Dispose();
248                 }
249 
250                 //[Variation("Positioning on a level and try to find the name which is on a level higher", Priority = 1)]
v5()251                 public void v5()
252                 {
253                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
254                     PositionOnElement(DataReader, "child3");
255 
256                     TestLog.Compare(DataReader.ReadToDescendant("child1"), false, "Reader returned true for an invalid name");
257                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type");
258                     TestLog.Compare(DataReader.LocalName, "child3", "Wrong name");
259 
260                     PositionOnElement(DataReader, "child3");
261 
262                     TestLog.Compare(DataReader.ReadToDescendant("child2", "child2"), false, "Reader returned true for an invalid name,ns");
263                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type for name,ns");
264 
265                     while (DataReader.Read()) ;
266                     DataReader.Dispose();
267                 }
268 
269                 //[Variation("Read to Descendant on one level and again to level below it", Priority = 1)]
v6()270                 public void v6()
271                 {
272                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
273                     PositionOnElement(DataReader, "root");
274 
275                     TestLog.Compare(DataReader.ReadToDescendant("elem"), true, "Cant find elem");
276                     TestLog.Compare(DataReader.ReadToDescendant("child1"), true, "Cant find child1");
277                     TestLog.Compare(DataReader.ReadToDescendant("child2"), true, "Cant find child2");
278                     TestLog.Compare(DataReader.ReadToDescendant("child3"), true, "Cant find child3");
279                     TestLog.Compare(DataReader.ReadToDescendant("child4"), false, "Shouldn't find child4");
280                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
281                     DataReader.Read();
282                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
283 
284                     while (DataReader.Read()) ;
285                     DataReader.Dispose();
286                 }
287 
288                 //[Variation("Read to Descendant on one level and again to level below it, with namespace", Priority = 1)]
v7()289                 public void v7()
290                 {
291                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
292                     PositionOnElement(DataReader, "root");
293 
294                     TestLog.Compare(DataReader.ReadToDescendant("elem", "elem"), true, "Cant find elem");
295                     TestLog.Compare(DataReader.ReadToDescendant("child1", "elem"), true, "Cant find child1");
296                     TestLog.Compare(DataReader.ReadToDescendant("child2", "child2"), true, "Cant find child2");
297                     TestLog.Compare(DataReader.ReadToDescendant("child3", "child2"), true, "Cant find child3");
298                     TestLog.Compare(DataReader.ReadToDescendant("child4", "child2"), false, "Shouldn't find child4");
299                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
300                     DataReader.Read();
301                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
302 
303                     while (DataReader.Read()) ;
304                     DataReader.Dispose();
305                 }
306 
307                 //[Variation("Read to Descendant on one level and again to level below it, with prefix", Priority = 1)]
v8()308                 public void v8()
309                 {
310                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
311                     PositionOnElement(DataReader, "root");
312 
313                     TestLog.Compare(DataReader.ReadToDescendant("e:elem"), true, "Cant find elem");
314                     TestLog.Compare(DataReader.ReadToDescendant("e:child1"), true, "Cant find child1");
315                     TestLog.Compare(DataReader.ReadToDescendant("e:child2"), true, "Cant find child2");
316                     TestLog.Compare(DataReader.ReadToDescendant("e:child3"), true, "Cant find child3");
317                     TestLog.Compare(DataReader.ReadToDescendant("e:child4"), false, "Shouldn't find child4");
318                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on EndElement");
319                     DataReader.Read();
320                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Text, "Not on Element");
321 
322                     while (DataReader.Read()) ;
323                     DataReader.Dispose();
324                 }
325 
326                 //[Variation("Multiple Reads to children and then next siblings, NNS", Priority = 2)]
v9()327                 public void v9()
328                 {
329                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
330                     PositionOnElement(DataReader, "root");
331 
332                     TestLog.Compare(DataReader.ReadToDescendant("elem"), true, "Read fails elem");
333                     TestLog.Compare(DataReader.ReadToDescendant("child3"), true, "Read fails child3");
334                     TestLog.Compare(DataReader.ReadToNextSibling("child4"), true, "Read fails child4");
335 
336                     while (DataReader.Read()) ;
337                     DataReader.Dispose();
338                 }
339 
340                 //[Variation("Multiple Reads to children and then next siblings, DNS", Priority = 2)]
v10()341                 public void v10()
342                 {
343                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
344                     PositionOnElement(DataReader, "root");
345 
346                     TestLog.Compare(DataReader.ReadToDescendant("elem", "elem"), true, "Read fails elem");
347                     TestLog.Compare(DataReader.ReadToDescendant("child3", "child2"), true, "Read fails child3");
348                     TestLog.Compare(DataReader.ReadToNextSibling("child4", "child2"), true, "Read fails child4");
349 
350                     while (DataReader.Read()) ;
351                     DataReader.Dispose();
352                 }
353 
354                 //[Variation("Multiple Reads to children and then next siblings, NS", Priority = 2)]
v11()355                 public void v11()
356                 {
357                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
358                     PositionOnElement(DataReader, "root");
359 
360                     TestLog.Compare(DataReader.ReadToDescendant("e:elem"), true, "Read fails elem");
361                     TestLog.Compare(DataReader.ReadToDescendant("e:child3"), true, "Read fails child3");
362                     TestLog.Compare(DataReader.ReadToNextSibling("e:child4"), true, "Read fails child4");
363 
364                     while (DataReader.Read()) ;
365                     DataReader.Dispose();
366                 }
367 
368                 //[Variation("Call from different nodetypes", Priority = 1)]
v12()369                 public void v12()
370                 {
371                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
372 
373                     while (DataReader.Read())
374                     {
375                         if (DataReader.NodeType != XmlNodeType.Element)
376                         {
377                             TestLog.Compare(DataReader.ReadToDescendant("child1"), false, "Fails on node");
378                         }
379                         else
380                         {
381                             if (DataReader.HasAttributes)
382                             {
383                                 while (DataReader.MoveToNextAttribute())
384                                 {
385                                     TestLog.Compare(DataReader.ReadToDescendant("abc"), false, "Fails on attribute node");
386                                 }
387                             }
388                         }
389                     }
390 
391                     DataReader.Dispose();
392                 }
393 
394                 //[Variation("Only child has namespaces and read to it", Priority = 2)]
v14()395                 public void v14()
396                 {
397                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
398                     PositionOnElement(DataReader, "root");
399 
400                     TestLog.Compare(DataReader.ReadToDescendant("child2", "child2"), true, "Fails on attribute node");
401 
402                     DataReader.Dispose();
403                 }
404 
405                 //[Variation("Pass null to both arguments throws ArgumentException", Priority = 2)]
v15()406                 public void v15()
407                 {
408                     XmlReader DataReader = GetReader(new StringReader("<root><b/></root>"));
409                     DataReader.Read();
410                     try
411                     {
412                         DataReader.ReadToDescendant(null);
413                     }
414                     catch (ArgumentNullException)
415                     {
416                     }
417 
418                     try
419                     {
420                         DataReader.ReadToDescendant("b", null);
421                     }
422                     catch (ArgumentNullException)
423                     {
424                     }
425 
426                     while (DataReader.Read()) ;
427                     DataReader.Dispose();
428                 }
429 
430                 //[Variation("Different names, same uri works correctly", Priority = 2)]
v17()431                 public void v17()
432                 {
433                     XmlReader DataReader = GetReader(new StringReader("<root><child1 xmlns='foo'/>blah<child1 xmlns='bar'>blah</child1></root>"));
434                     DataReader.Read();
435 
436                     DataReader.ReadToDescendant("child1", "bar");
437                     TestLog.Compare(DataReader.IsEmptyElement, false, "Not on the correct node");
438 
439                     while (DataReader.Read()) ;
440                     DataReader.Dispose();
441                 }
442 
443 
444                 //[Variation("On Root Node", Priority = 0, Params = new object[] { "NNS" })]
445                 //[Variation("On Root Node", Priority = 0, Params = new object[] { "DNS" })]
446                 //[Variation("On Root Node", Priority = 0, Params = new object[] { "NS" })]
v18()447                 public void v18()
448                 {
449                     string type = Variation.Params[0].ToString();
450 
451                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
452                     switch (type)
453                     {
454                         case "NNS":
455                             DataReader.ReadToDescendant("elem");
456                             if (DataReader.HasAttributes)
457                             {
458                                 TestLog.WriteLine("Positioned on wrong element");
459                                 TestLog.WriteIgnore(DataReader.ReadInnerXml() + "\n");
460                                 throw new TestException(TestResult.Failed, "");
461                             }
462                             while (DataReader.Read()) ;
463                             DataReader.Dispose();
464                             return;
465 
466                         case "DNS":
467                             DataReader.ReadToDescendant("elem", "elem");
468                             if (DataReader.HasAttributes)
469                             {
470                                 if (DataReader.GetAttribute("xmlns") == null)
471                                 {
472                                     TestLog.WriteLine("Positioned on wrong element, not on DNS");
473                                     throw new TestException(TestResult.Failed, "");
474                                 }
475                             }
476                             while (DataReader.Read()) ;
477                             DataReader.Dispose();
478                             return;
479 
480                         case "NS":
481                             DataReader.ReadToDescendant("e:elem");
482                             if (DataReader.HasAttributes)
483                             {
484                                 if (DataReader.GetAttribute("xmlns:e") == null)
485                                 {
486                                     TestLog.WriteLine("Positioned on wrong element, not on NS");
487                                     throw new TestException(TestResult.Failed, "");
488                                 }
489                             }
490                             while (DataReader.Read()) ;
491                             DataReader.Dispose();
492                             return;
493                         default:
494                             throw new TestFailedException("Error in Test type");
495                     }
496                 }
497 
498                 //[Variation("427176	Assertion failed when call XmlReader.ReadToDescendant() for non-existing node", Priority = 1)]
v19()499                 public void v19()
500                 {
501                     XmlReader DataReader = GetReader(new StringReader("<a>b</a>"));
502                     TestLog.Compare(DataReader.ReadToDescendant("foo"), false, "Should fail without assert");
503 
504                     DataReader.Dispose();
505                 }
506             }
507         }
508     }
509 }
510