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 = "ReadToFollowing", Desc = "ReadToFollowing")]
18             public partial class TCReadToFollowing : 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" })]
v1()86                 public void v1()
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.ReadToFollowing("elem");
97                             TestLog.Compare(DataReader.HasAttributes, false, "Positioned on wrong element");
98                             while (DataReader.Read()) ;
99                             DataReader.Dispose();
100                             return;
101 
102                         case "DNS":
103                             DataReader.ReadToFollowing("elem", "elem");
104                             if (DataReader.HasAttributes)
105                                 TestLog.Compare(DataReader.GetAttribute("xmlns") != null, "Positioned on wrong element");
106                             while (DataReader.Read()) ;
107                             DataReader.Dispose();
108                             return;
109 
110                         case "NS":
111                             DataReader.ReadToFollowing("e:elem");
112                             if (DataReader.HasAttributes)
113                                 TestLog.Compare(DataReader.GetAttribute("xmlns:e") != null, "Positioned on wrong element");
114                             while (DataReader.Read()) ;
115                             DataReader.Dispose();
116                             return;
117                         default:
118                             throw new TestFailedException("Error in Test type");
119                     }
120                 }
121 
122                 //[Variation("Read on following with same names", Priority = 1, Params = new object[] { "NNS" })]
123                 //[Variation("Read on following with same names", Priority = 1, Params = new object[] { "DNS" })]
124                 //[Variation("Read on following with same names", Priority = 1, Params = new object[] { "NS" })]
v2()125                 public void v2()
126                 {
127                     string type = Variation.Params[0].ToString();
128 
129                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
130                     PositionOnElement(DataReader, "root");
131 
132                     // Doing a sequential read.
133                     switch (type)
134                     {
135                         case "NNS":
136                             DataReader.ReadToFollowing("elem");
137                             int depth = DataReader.Depth;
138                             TestLog.Compare(DataReader.HasAttributes, false, "Positioned on wrong element");
139                             TestLog.Compare(DataReader.ReadToFollowing("elem"), true, "There are no more elem nodes");
140                             TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type");
141                             while (DataReader.Read()) ;
142                             DataReader.Dispose();
143                             return;
144 
145                         case "DNS":
146                             DataReader.ReadToFollowing("elem", "elem");
147                             if (DataReader.HasAttributes)
148                                 TestLog.Compare(DataReader.GetAttribute("xmlns") != null, "Positioned on wrong element, not on NS");
149                             TestLog.Compare(DataReader.ReadToFollowing("elem", "elem"), true, "There are no more elem,elem nodes");
150                             TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type");
151                             while (DataReader.Read()) ;
152                             DataReader.Dispose();
153                             return;
154 
155                         case "NS":
156                             DataReader.ReadToFollowing("e:elem");
157                             if (DataReader.HasAttributes)
158                                 TestLog.Compare(DataReader.GetAttribute("xmlns:e") != null, "Positioned on wrong element, not on NS");
159                             TestLog.Compare(DataReader.ReadToFollowing("e:elem"), true, "There are no more descendants");
160                             TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Wrong node type");
161                             while (DataReader.Read()) ;
162                             DataReader.Dispose();
163 
164                             return;
165 
166                         default:
167                             throw new TestFailedException("Error in Test type");
168                     }
169                 }
170 
171                 //[Variation("If name not found, go to eof", Priority = 1)]
v4()172                 public void v4()
173                 {
174                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
175                     PositionOnElement(DataReader, "elem");
176 
177                     TestLog.Compare(DataReader.ReadToFollowing("abc"), false, "Reader returned true for an invalid name");
178                     TestLog.Compare(DataReader.NodeType, XmlNodeType.None, "Wrong node type");
179 
180                     while (DataReader.Read()) ;
181                     DataReader.Dispose();
182                 }
183 
184                 //[Variation("If localname not found go to eof", Priority = 1)]
v5_1()185                 public void v5_1()
186                 {
187                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
188                     PositionOnElement(DataReader, "elem");
189 
190                     TestLog.Compare(DataReader.ReadToFollowing("abc", "elem"), false, "Reader returned true for an invalid localname");
191                     TestLog.Compare(DataReader.NodeType, XmlNodeType.None, "Wrong node type");
192 
193                     while (DataReader.Read()) ;
194                     DataReader.Dispose();
195                 }
196 
197                 //[Variation("If uri not found, go to eof", Priority = 1)]
v5_2()198                 public void v5_2()
199                 {
200                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
201 
202                     TestLog.Compare(DataReader.ReadToFollowing("elem", "abc"), false, "Reader returned true for an invalid uri");
203                     TestLog.Compare(DataReader.NodeType, XmlNodeType.None, "Wrong node type");
204 
205                     while (DataReader.Read()) ;
206                     DataReader.Dispose();
207                 }
208 
209                 //[Variation("Read to Following on one level and again to level below it", Priority = 1)]
v6()210                 public void v6()
211                 {
212                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
213                     PositionOnElement(DataReader, "root");
214 
215                     TestLog.Compare(DataReader.ReadToFollowing("elem"), true, "Cant find elem");
216                     TestLog.Compare(DataReader.ReadToFollowing("child1"), true, "Cant find child1");
217                     TestLog.Compare(DataReader.ReadToFollowing("child2"), true, "Cant find child2");
218                     TestLog.Compare(DataReader.ReadToFollowing("child3"), true, "Cant find child3");
219                     TestLog.Compare(DataReader.ReadToFollowing("child4"), true, "Shouldn't find child4");
220                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on Element");
221 
222                     while (DataReader.Read()) ;
223                     DataReader.Dispose();
224                 }
225 
226                 //[Variation("Read to Following on one level and again to level below it, with namespace", Priority = 1)]
v7()227                 public void v7()
228                 {
229                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
230                     PositionOnElement(DataReader, "root");
231 
232                     TestLog.Compare(DataReader.ReadToFollowing("elem", "elem"), true, "Cant find elem");
233                     TestLog.Compare(DataReader.ReadToFollowing("child1", "elem"), true, "Cant find child1");
234                     TestLog.Compare(DataReader.ReadToFollowing("child2", "child2"), true, "Cant find child2");
235                     TestLog.Compare(DataReader.ReadToFollowing("child3", "child2"), true, "Cant find child3");
236                     TestLog.Compare(DataReader.ReadToFollowing("child4", "child2"), true, "Cant find child4");
237                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on Element");
238 
239                     while (DataReader.Read()) ;
240                     DataReader.Dispose();
241                 }
242 
243                 //[Variation("Read to Following on one level and again to level below it, with prefix", Priority = 1)]
v8()244                 public void v8()
245                 {
246                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
247                     PositionOnElement(DataReader, "root");
248 
249                     TestLog.Compare(DataReader.ReadToFollowing("e:elem"), true, "Cant find elem");
250                     TestLog.Compare(DataReader.ReadToFollowing("e:child1"), true, "Cant find child1");
251                     TestLog.Compare(DataReader.ReadToFollowing("e:child2"), true, "Cant find child2");
252                     TestLog.Compare(DataReader.ReadToFollowing("e:child3"), true, "Cant find child3");
253                     TestLog.Compare(DataReader.ReadToFollowing("e:child4"), true, "Cant fine child4");
254                     TestLog.Compare(DataReader.NodeType, XmlNodeType.Element, "Not on Element");
255 
256                     while (DataReader.Read()) ;
257                     DataReader.Dispose();
258                 }
259 
260                 //[Variation("Multiple Reads to children and then next siblings, NNS", Priority = 2)]
v9()261                 public void v9()
262                 {
263                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
264                     PositionOnElement(DataReader, "root");
265 
266                     TestLog.Compare(DataReader.ReadToFollowing("elem"), true, "Read fails elem");
267                     TestLog.Compare(DataReader.ReadToFollowing("child3"), true, "Read fails child3");
268                     TestLog.Compare(DataReader.ReadToNextSibling("child4"), true, "Read fails child4");
269 
270                     while (DataReader.Read()) ;
271                     DataReader.Dispose();
272                 }
273 
274                 //[Variation("Multiple Reads to children and then next siblings, DNS", Priority = 2)]
v10()275                 public void v10()
276                 {
277                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
278                     PositionOnElement(DataReader, "root");
279 
280                     TestLog.Compare(DataReader.ReadToFollowing("elem", "elem"), true, "Read fails elem");
281                     TestLog.Compare(DataReader.ReadToFollowing("child3", "child2"), true, "Read fails child3");
282                     TestLog.Compare(DataReader.ReadToNextSibling("child4", "child2"), true, "Read fails child4");
283 
284                     while (DataReader.Read()) ;
285                     DataReader.Dispose();
286                 }
287 
288                 //[Variation("Multiple Reads to children and then next siblings, NS", Priority = 2)]
v11()289                 public void v11()
290                 {
291                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
292                     PositionOnElement(DataReader, "root");
293 
294                     TestLog.Compare(DataReader.ReadToFollowing("e:elem"), true, "Read fails elem");
295                     TestLog.Compare(DataReader.ReadToFollowing("e:child3"), true, "Read fails child3");
296                     TestLog.Compare(DataReader.ReadToNextSibling("e:child4"), true, "Read fails child4");
297 
298                     while (DataReader.Read()) ;
299                     DataReader.Dispose();
300                 }
301 
302                 //[Variation("Only child has namespaces and read to it", Priority = 2)]
v14()303                 public void v14()
304                 {
305                     XmlReader DataReader = GetReader(new StringReader(_xmlStr));
306                     PositionOnElement(DataReader, "root");
307 
308                     TestLog.Compare(DataReader.ReadToFollowing("child2", "child2"), true, "Fails on child2");
309 
310                     DataReader.Dispose();
311                 }
312 
313                 //[Variation("Pass null to both arguments throws ArgumentException", Priority = 2)]
v15()314                 public void v15()
315                 {
316                     XmlReader DataReader = GetReader(new StringReader("<root><b/></root>"));
317                     DataReader.Read();
318                     try
319                     {
320                         DataReader.ReadToFollowing(null);
321                     }
322                     catch (ArgumentNullException)
323                     {
324                     }
325 
326                     try
327                     {
328                         DataReader.ReadToFollowing("b", null);
329                     }
330                     catch (ArgumentNullException)
331                     {
332                     }
333 
334                     while (DataReader.Read()) ;
335                     DataReader.Dispose();
336                 }
337 
338                 //[Variation("Different names, same uri works correctly", Priority = 2)]
v17()339                 public void v17()
340                 {
341                     XmlReader DataReader = GetReader(new StringReader("<root><child1 xmlns='foo'/>blah<child1 xmlns='bar'>blah</child1></root>"));
342                     DataReader.Read();
343 
344                     DataReader.ReadToFollowing("child1", "bar");
345                     TestLog.Compare(DataReader.IsEmptyElement, false, "Not on the correct node");
346 
347                     while (DataReader.Read()) ;
348                     DataReader.Dispose();
349                 }
350             }
351         }
352     }
353 }
354