2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / XmlSchemaTests.cs
1 //
2 // System.Xml.XmlSchemaTests.cs
3 //
4 // Author:
5 //   Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // (C) 2002 Atsushi Enomoto
8 //
9
10 using System;
11 using System.IO;
12 using System.Net;
13 using System.Xml;
14 using System.Xml.Schema;
15 using System.Xml.Serialization;
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Xml
19 {       
20         [TestFixture]
21         public class XmlSchemaTests : XmlSchemaAssertion
22         {
23                 static readonly bool StrictMsCompliant = Environment.GetEnvironmentVariable ("MONO_STRICT_MS_COMPLIANT") == "yes";
24
25                 [Test]
26                 public void TestRead ()
27                 {
28                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
29                         Assert.AreEqual (6, schema.Items.Count);
30
31                         bool fooValidated = false;
32                         bool barValidated = false;
33                         string ns = "urn:bar";
34
35                         foreach (XmlSchemaObject obj in schema.Items) {
36                                 XmlSchemaElement element = obj as XmlSchemaElement;
37                                 if (element == null)
38                                         continue;
39                                 if (element.Name == "Foo") {
40                                         AssertElement (element, "Foo", 
41                                                 XmlQualifiedName.Empty, null,
42                                                 QName ("string", XmlSchema.Namespace), null);
43                                         fooValidated = true;
44                                 }
45                                 if (element.Name == "Bar") {
46                                         AssertElement (element, "Bar",
47                                                 XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
48                                         barValidated = true;
49                                 }
50                         }
51                         Assert.IsTrue (fooValidated);
52                         Assert.IsTrue (barValidated);
53                 }
54
55                 [Test]
56                 public void TestReadFlags ()
57                 {
58                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
59                         schema.Compile (null);
60                         XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
61                         Assert.IsNotNull (el);
62                         Assert.AreEqual (XmlSchemaDerivationMethod.Extension, el.Block);
63
64                         el = schema.Items [1] as XmlSchemaElement;
65                         Assert.IsNotNull (el);
66                         Assert.AreEqual (XmlSchemaDerivationMethod.Extension |
67                                 XmlSchemaDerivationMethod.Restriction, el.Block);
68                 }
69
70                 [Test]
71                 public void TestWriteFlags ()
72                 {
73                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
74                         StringWriter sw = new StringWriter ();
75                         XmlTextWriter xtw = new XmlTextWriter (sw);
76                         schema.Write (xtw);
77                 }
78
79                 [Test]
80                 public void TestCompile ()
81                 {
82                         XmlQualifiedName qname;
83                         XmlSchemaComplexContentExtension xccx;
84                         XmlSchemaComplexType cType;
85                         XmlSchemaSequence seq;
86
87                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
88 //                      Assert.IsTrue (!schema.IsCompiled);
89                         schema.Compile (null);
90                         Assert.IsTrue (schema.IsCompiled);
91                         string ns = "urn:bar";
92
93                         XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
94                         Assert.IsNotNull (foo);
95                         XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
96                         Assert.IsNotNull (stringDatatype);
97
98                         // HogeType
99                         qname = QName ("HogeType", ns);
100                         cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
101                         Assert.IsNotNull (cType);
102                         Assert.IsNull (cType.ContentModel);
103                         AssertCompiledComplexType (cType, qname, 0, 0,
104                                 false, null, true, XmlSchemaContentType.ElementOnly);
105                         seq = cType.ContentTypeParticle as XmlSchemaSequence;
106                         Assert.IsNotNull (seq);
107                         Assert.AreEqual (2, seq.Items.Count);
108                         XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
109                         AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
110
111                         // FugaType
112                         qname = QName ("FugaType", ns);
113                         cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
114                         Assert.IsNotNull (cType);
115                         xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
116                         AssertCompiledComplexContentExtension (
117                                 xccx, 0, false, QName ("HogeType", ns));
118
119                         AssertCompiledComplexType (cType, qname, 0, 0,
120                                 false, typeof (XmlSchemaComplexContent),
121                                 true, XmlSchemaContentType.ElementOnly);
122                         Assert.IsNotNull (cType.BaseSchemaType);
123
124                         seq = xccx.Particle as XmlSchemaSequence;
125                         Assert.IsNotNull (seq);
126                         Assert.AreEqual (1, seq.Items.Count);
127                         XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
128                         Assert.IsNotNull (refBaz);
129                         AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
130
131                         qname = QName ("Bar", ns);
132                         XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
133                         AssertCompiledElement (element, qname, cType);
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (XmlSchemaException))]
138                 public void TestCompile_ZeroLength_TargetNamespace ()
139                 {
140                         XmlSchema schema = new XmlSchema ();
141                         schema.TargetNamespace = string.Empty;
142                         Assert.IsTrue (!schema.IsCompiled);
143
144                         // MS.NET 1.x: The Namespace '' is an invalid URI.
145                         // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
146                         schema.Compile (null);
147                 }
148
149                 [Test]
150                 [ExpectedException (typeof (XmlSchemaException))]
151                 public void TestCompileNonSchema ()
152                 {
153                         XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
154                         XmlSchema schema = XmlSchema.Read (xtr, null);
155                         xtr.Close ();
156                 }
157
158                 [Test]
159                 public void TestSimpleImport ()
160                 {
161                         XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
162                         Assert.AreEqual ("urn:foo", schema.TargetNamespace);
163                         XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
164                         Assert.IsNotNull (import);
165
166                         schema.Compile (null);
167                         Assert.AreEqual (4, schema.Elements.Count);
168                         Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
169                         Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
170                         Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
171                         Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
172                         
173                 }
174
175                 [Test]
176                 public void TestSimpleMutualImport ()
177                 {
178                         XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
179                         try {
180                                 XmlSchema.Read (r, null).Compile (null);
181                         } finally {
182                                 r.Close ();
183                         }
184                 }
185
186                 [Test]
187                 public void TestQualification ()
188                 {
189                         XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
190                         schema.Compile (null);
191                         XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
192                         Assert.IsNotNull (el);
193                         XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
194                         XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
195                         XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
196                         Assert.AreEqual (QName ("Bar", ""), elp.QualifiedName);
197
198                         schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
199                         schema.Compile (null);
200                         el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
201                         Assert.IsNotNull (el);
202                         ct = el.ElementType as XmlSchemaComplexType;
203                         seq = ct.ContentTypeParticle as XmlSchemaSequence;
204                         elp = seq.Items [0] as XmlSchemaElement;
205                         Assert.AreEqual (QName ("Bar", "urn:bar"), elp.QualifiedName);
206                 }
207
208                 [Test]
209                 public void TestWriteNamespaces ()
210                 {
211                         XmlDocument doc = new XmlDocument ();
212                         XmlSchema xs;
213                         StringWriter sw;
214                         XmlTextWriter xw;
215
216                         // empty
217                         xs = new XmlSchema ();
218                         sw = new StringWriter ();
219                         xw = new XmlTextWriter (sw);
220                         xs.Write (xw);
221                         doc.LoadXml (sw.ToString ());
222                         Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#1");
223
224                         // TargetNamespace
225                         xs = new XmlSchema ();
226                         sw = new StringWriter ();
227                         xw = new XmlTextWriter (sw);
228                         xs.TargetNamespace = "urn:foo";
229                         xs.Write (xw);
230                         doc.LoadXml (sw.ToString ());
231                         Assert.AreEqual ("<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2");
232
233                         // Zero-length TargetNamespace
234                         xs = new XmlSchema ();
235                         sw = new StringWriter ();
236                         xw = new XmlTextWriter (sw);
237                         xs.TargetNamespace = string.Empty;
238                         xs.Write (xw);
239                         doc.LoadXml (sw.ToString ());
240                         Assert.AreEqual ("<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2b");
241
242                         // XmlSerializerNamespaces
243                         xs = new XmlSchema ();
244                         sw = new StringWriter ();
245                         xw = new XmlTextWriter (sw);
246                         xs.Namespaces.Add ("hoge", "urn:hoge");
247                         xs.Write (xw);
248                         doc.LoadXml (sw.ToString ());
249                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
250                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#3");
251
252                         // TargetNamespace + XmlSerializerNamespaces
253                         xs = new XmlSchema ();
254                         sw = new StringWriter ();
255                         xw = new XmlTextWriter (sw);
256                         xs.TargetNamespace = "urn:foo";
257                         xs.Namespaces.Add ("hoge", "urn:hoge");
258                         xs.Write (xw);
259                         doc.LoadXml (sw.ToString ());
260                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
261                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#4");
262
263                         // Add XmlSchema.Namespace to XmlSerializerNamespaces
264                         xs = new XmlSchema ();
265                         sw = new StringWriter ();
266                         xw = new XmlTextWriter (sw);
267                         xs.Namespaces.Add ("a", XmlSchema.Namespace);
268                         xs.Write (xw);
269                         doc.LoadXml (sw.ToString ());
270                         Assert.AreEqual ("<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#5");
271
272                         // UnhandledAttributes + XmlSerializerNamespaces
273                         xs = new XmlSchema ();
274                         sw = new StringWriter ();
275                         xw = new XmlTextWriter (sw);
276                         XmlAttribute attr = doc.CreateAttribute ("hoge");
277                         xs.UnhandledAttributes = new XmlAttribute [] {attr};
278                         xs.Namespaces.Add ("hoge", "urn:hoge");
279                         xs.Write (xw);
280                         doc.LoadXml (sw.ToString ());
281                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
282                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#6");
283
284                         // Adding xmlns to UnhandledAttributes -> no output
285                         xs = new XmlSchema ();
286                         sw = new StringWriter ();
287                         xw = new XmlTextWriter (sw);
288                         attr = doc.CreateAttribute ("xmlns");
289                         attr.Value = "urn:foo";
290                         xs.UnhandledAttributes = new XmlAttribute [] {attr};
291                         xs.Write (xw);
292                         doc.LoadXml (sw.ToString ());
293                         Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#7");
294                 }
295
296                 [Category ("NotWorking")]
297                 [Test]
298                 public void TestWriteNamespaces2 ()
299                 {
300                         string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
301                         XmlSchema xs = new XmlSchema ();
302                         XmlSerializerNamespaces nss =
303                                 new XmlSerializerNamespaces ();
304                         StringWriter sw;
305                         sw = new StringWriter ();
306                         xs.Write (new XmlTextWriter (sw));
307                         Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#1");
308
309                         xs.Namespaces = nss;
310                         sw = new StringWriter ();
311                         xs.Write (new XmlTextWriter (sw));
312                         Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#2");
313
314                         nss.Add ("foo", "urn:foo");
315                         sw = new StringWriter ();
316                         xs.Write (new XmlTextWriter (sw));
317                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
318                         // Assert.AreEqual (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#3");
319
320                         nss.Add ("", "urn:foo");
321                         sw = new StringWriter ();
322                         xs.Write (new XmlTextWriter (sw));
323                         // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
324                         // Assert.AreEqual (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#4");
325
326                         nss.Add ("q1", "urn:q1");
327                         sw = new StringWriter ();
328                         xs.Write (new XmlTextWriter (sw));
329                         //Not sure if testing for exact order of these name spaces is
330                         // relevent, so using less strict test that passes on MS.NET
331                         //Assert.AreEqual (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
332                         Assert.IsTrue (sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1, "q1");
333                 }
334
335                 [Test]
336                 public void ReaderPositionAfterRead ()
337                 {
338                         string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>  <xs:element name='test' type='xs:integer'/></xs:schema>";
339                         XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
340                         xtr.Read ();
341                         XmlSchema xs = XmlSchema.Read (xtr, null);
342                         Assert.AreEqual (XmlNodeType.EndElement, xtr.NodeType);
343                 }
344
345                 [Test]
346                 // bug #76865
347                 public void AmbiguityDetectionOnChameleonAnyOther ()
348                 {
349                         string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
350 <xs:complexType name='TestType'>
351   <xs:sequence>
352     <xs:any namespace='##other' minOccurs='0' />
353     <xs:element name='Item' /> 
354     <xs:any namespace='##other' minOccurs='0' />
355   </xs:sequence> 
356 </xs:complexType>
357 </xs:schema>";
358                         XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
359                 }
360
361                 [Test]
362                 // bug #77685
363                 public void ReadDoesNotIgnoreDocumentationEmptyElement ()
364                 {
365                         string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
366   <xs:element name='choice'>
367     <xs:annotation><xs:documentation /></xs:annotation>
368   </xs:element>
369 </xs:schema>";
370                         XmlTextReader tr = new XmlTextReader (
371                                 schemaxml, XmlNodeType.Document, null);
372                         XmlSchema schema = XmlSchema.Read (tr, null);
373                         XmlSchemaElement element =
374                                 schema.Items [0] as XmlSchemaElement;
375                         XmlSchemaAnnotation annotation = element.Annotation;
376                         XmlSchemaDocumentation doc =
377                                 annotation.Items [0] as XmlSchemaDocumentation;
378                         Assert.AreEqual (0, doc.Markup.Length);
379                 }
380
381
382                 [Test]
383                 // bug #77687
384                 public void CompileFillsSchemaPropertyInExternal ()
385                 {
386                         string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
387                         XmlTextReader tr = new XmlTextReader (schemaFileName);
388
389                         XmlSchema schema = XmlSchema.Read (tr, null);
390                         XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
391                         Assert.IsNull (inc.Schema);
392                         schema.Compile (null);
393                         tr.Close ();
394                         Assert.IsNotNull (inc.Schema);
395                 }
396
397                 [Test]
398                 // bug #78985 (contains two identical field path "@key" in 
399                 // two different keys where one is in scope within another)
400                 public void DuplicateKeyFieldAttributePath ()
401                 {
402                         string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
403                         string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
404                         XmlTextReader tr = new XmlTextReader (schemaFileName);
405
406                         XmlValidatingReader vr = new XmlValidatingReader (
407                                 new XmlTextReader (xmlFileName));
408                         vr.Schemas.Add (XmlSchema.Read (tr, null));
409                         while (!vr.EOF)
410                                 vr.Read ();
411                 }
412
413                 [Test]
414                 public void ThreeLevelNestedInclusion ()
415                 {
416                         XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
417                         try {
418                                 XmlSchema xs = XmlSchema.Read (r, null);
419                                 xs.Compile (null);
420                         } finally {
421                                 r.Close ();
422                         }
423                 }
424
425                 [Test] // bug #502115
426                 public void ExtensionRedefineAttribute1 ()
427                 {
428                         const string xml = "<Bar xmlns='foo'/>";
429
430                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-1.xsd");
431
432 #if NET_2_0
433                         XmlSchemaSet xss = new XmlSchemaSet ();
434                         xss.Add (schema);
435                         if (StrictMsCompliant) {
436                                 xss.Compile ();
437                         } else {
438                                 try {
439                                         xss.Compile ();
440                                         Assert.Fail ();
441                                 } catch (XmlSchemaException) {
442                                 }
443                                 return;
444                         }
445
446                         StringReader sr = new StringReader (xml);
447
448                         XmlReaderSettings settings = new XmlReaderSettings ();
449                         settings.ValidationType = ValidationType.Schema;
450                         settings.Schemas = xss;
451                         XmlReader vr = XmlReader.Create (sr, settings);
452 #else
453                         if (StrictMsCompliant) {
454                                 schema.Compile (null);
455                         } else {
456                                 try {
457                                         schema.Compile (null);
458                                         Assert.Fail ();
459                                 } catch (XmlSchemaException) {
460                                 }
461                                 return;
462                         }
463
464                         XmlValidatingReader vr = new XmlValidatingReader (xml,
465                                 XmlNodeType.Document, null);
466                         vr.Schemas.Add (schema);
467                         vr.ValidationType = ValidationType.Schema;
468 #endif
469
470                         try {
471                                 vr.Read ();
472                                 Assert.Fail ();
473                         } catch (XmlSchemaException) {
474                         }
475                 }
476
477                 [Test] // bug #502115
478                 public void ExtensionRedefineAttribute2 ()
479                 {
480                         const string xml = "<Bar xmlns='foo'/>";
481
482                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
483
484 #if NET_2_0
485                         XmlSchemaSet xss = new XmlSchemaSet ();
486                         xss.Add (schema);
487                         xss.Compile ();
488
489                         StringReader sr = new StringReader (xml);
490
491                         XmlReaderSettings settings = new XmlReaderSettings ();
492                         settings.ValidationType = ValidationType.Schema;
493                         settings.Schemas = xss;
494                         XmlReader vr = XmlReader.Create (sr, settings);
495 #else
496                         schema.Compile (null);
497
498                         XmlValidatingReader vr = new XmlValidatingReader (xml,
499                                 XmlNodeType.Document, null);
500                         vr.Schemas.Add (schema);
501                         vr.ValidationType = ValidationType.Schema;
502 #endif
503
504                         while (vr.Read ()) ;
505                 }
506
507                 [Test] // bug #502115
508                 public void ExtensionRedefineAttribute3 ()
509                 {
510                         const string xml = "<Bar xmlns='foo'/>";
511
512                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
513
514 #if NET_2_0
515                         XmlSchemaSet xss = new XmlSchemaSet ();
516                         xss.Add (schema);
517                         if (StrictMsCompliant) {
518                                 xss.Compile ();
519                         } else {
520                                 try {
521                                         xss.Compile ();
522                                         Assert.Fail ();
523                                 } catch (XmlSchemaException) {
524                                 }
525                                 return;
526                         }
527
528                         StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
529
530                         XmlReaderSettings settings = new XmlReaderSettings ();
531                         settings.ValidationType = ValidationType.Schema;
532                         settings.Schemas = xss;
533                         XmlReader vr = XmlReader.Create (sr, settings);
534 #else
535                         if (StrictMsCompliant) {
536                                 schema.Compile (null);
537                         } else {
538                                 try {
539                                         schema.Compile (null);
540                                         Assert.Fail ();
541                                 } catch (XmlSchemaException) {
542                                 }
543                                 return;
544                         }
545
546                         XmlValidatingReader vr = new XmlValidatingReader (xml,
547                                 XmlNodeType.Document, null);
548                         vr.Schemas.Add (schema);
549                         vr.ValidationType = ValidationType.Schema;
550 #endif
551
552                         while (vr.Read ()) ;
553                 }
554
555 #if NET_2_0
556
557                 internal class XmlTestResolver : XmlResolver
558                 {                       
559                         Uri receivedUri;
560
561                         public override ICredentials Credentials
562                         {
563                             set { throw new NotSupportedException (); }
564                         }
565
566                         public override Uri ResolveUri (Uri baseUri, string relativeUri)
567                         {
568                             return new Uri (relativeUri);
569                         }
570                         
571                         public Uri ReceivedUri
572                         {
573                                 get { return receivedUri; }
574                         }
575
576                         public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
577                         {
578                                 receivedUri = absoluteUri;
579                                 
580                                 return null;
581                         }
582                 }       
583                 
584                 [Test]
585                 public void TestResolveUri ()
586                 {
587                         XmlSchemaSet schemaSet = new XmlSchemaSet ();
588                         FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open);
589                         schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
590
591                         XmlTestResolver resolver = new XmlTestResolver ();              
592                         
593                         XmlReaderSettings settings = new XmlReaderSettings ();                  
594                         settings.Schemas.XmlResolver = resolver; 
595                         settings.Schemas.Add (schemaSet);
596                         settings.ValidationType = ValidationType.Schema;
597                         settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
598                         XmlReader reader = XmlReader.Create (stream, settings);
599                         
600                         try
601                         {
602                                 reader.Read ();         
603                         }
604                         catch (XmlException)
605                         {
606                                 // do nothing - we are expecting this exception because the test xmlresolver returns null from its 
607                                 // GetEntity method.
608                         }
609                         
610                         Assert.AreEqual ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
611                 }
612
613                 [Test]
614                 public void TestImportNoSchemaLocation()
615                 {
616                         XmlSchemaSet schemaSet = new XmlSchemaSet ();
617                         schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
618                         schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
619                         
620                         XmlReaderSettings settings = new XmlReaderSettings ();
621                         settings.Schemas.Add (schemaSet);
622                         settings.ValidationType = ValidationType.Schema;
623                         
624                         XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
625                         
626                         // Parse the file. 
627                         while (reader.Read()) {}
628                 }
629 #endif
630         }
631 }