Merge pull request #600 from tr8dr/master
[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                 [Category ("MobileNotWorking")]
177                 public void TestSimpleMutualImport ()
178                 {
179                         XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
180                         try {
181                                 XmlSchema.Read (r, null).Compile (null);
182                         } finally {
183                                 r.Close ();
184                         }
185                 }
186
187                 [Test]
188                 public void TestQualification ()
189                 {
190                         XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
191                         schema.Compile (null);
192                         XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
193                         Assert.IsNotNull (el);
194                         XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
195                         XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
196                         XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
197                         Assert.AreEqual (QName ("Bar", ""), elp.QualifiedName);
198
199                         schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
200                         schema.Compile (null);
201                         el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
202                         Assert.IsNotNull (el);
203                         ct = el.ElementType as XmlSchemaComplexType;
204                         seq = ct.ContentTypeParticle as XmlSchemaSequence;
205                         elp = seq.Items [0] as XmlSchemaElement;
206                         Assert.AreEqual (QName ("Bar", "urn:bar"), elp.QualifiedName);
207                 }
208
209                 [Test]
210                 public void TestWriteNamespaces ()
211                 {
212                         XmlDocument doc = new XmlDocument ();
213                         XmlSchema xs;
214                         StringWriter sw;
215                         XmlTextWriter xw;
216
217                         // empty
218                         xs = new XmlSchema ();
219                         sw = new StringWriter ();
220                         xw = new XmlTextWriter (sw);
221                         xs.Write (xw);
222                         doc.LoadXml (sw.ToString ());
223                         Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#1");
224
225                         // TargetNamespace
226                         xs = new XmlSchema ();
227                         sw = new StringWriter ();
228                         xw = new XmlTextWriter (sw);
229                         xs.TargetNamespace = "urn:foo";
230                         xs.Write (xw);
231                         doc.LoadXml (sw.ToString ());
232                         Assert.AreEqual ("<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2");
233
234                         // Zero-length TargetNamespace
235                         xs = new XmlSchema ();
236                         sw = new StringWriter ();
237                         xw = new XmlTextWriter (sw);
238                         xs.TargetNamespace = string.Empty;
239                         xs.Write (xw);
240                         doc.LoadXml (sw.ToString ());
241                         Assert.AreEqual ("<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2b");
242
243                         // XmlSerializerNamespaces
244                         xs = new XmlSchema ();
245                         sw = new StringWriter ();
246                         xw = new XmlTextWriter (sw);
247                         xs.Namespaces.Add ("hoge", "urn:hoge");
248                         xs.Write (xw);
249                         doc.LoadXml (sw.ToString ());
250                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
251                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#3");
252
253                         // TargetNamespace + XmlSerializerNamespaces
254                         xs = new XmlSchema ();
255                         sw = new StringWriter ();
256                         xw = new XmlTextWriter (sw);
257                         xs.TargetNamespace = "urn:foo";
258                         xs.Namespaces.Add ("hoge", "urn:hoge");
259                         xs.Write (xw);
260                         doc.LoadXml (sw.ToString ());
261                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
262                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#4");
263
264                         // Add XmlSchema.Namespace to XmlSerializerNamespaces
265                         xs = new XmlSchema ();
266                         sw = new StringWriter ();
267                         xw = new XmlTextWriter (sw);
268                         xs.Namespaces.Add ("a", XmlSchema.Namespace);
269                         xs.Write (xw);
270                         doc.LoadXml (sw.ToString ());
271                         Assert.AreEqual ("<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#5");
272
273                         // UnhandledAttributes + XmlSerializerNamespaces
274                         xs = new XmlSchema ();
275                         sw = new StringWriter ();
276                         xw = new XmlTextWriter (sw);
277                         XmlAttribute attr = doc.CreateAttribute ("hoge");
278                         xs.UnhandledAttributes = new XmlAttribute [] {attr};
279                         xs.Namespaces.Add ("hoge", "urn:hoge");
280                         xs.Write (xw);
281                         doc.LoadXml (sw.ToString ());
282                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
283                         // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#6");
284
285                         // Adding xmlns to UnhandledAttributes -> no output
286                         xs = new XmlSchema ();
287                         sw = new StringWriter ();
288                         xw = new XmlTextWriter (sw);
289                         attr = doc.CreateAttribute ("xmlns");
290                         attr.Value = "urn:foo";
291                         xs.UnhandledAttributes = new XmlAttribute [] {attr};
292                         xs.Write (xw);
293                         doc.LoadXml (sw.ToString ());
294                         Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#7");
295                 }
296
297                 [Category ("NotWorking")]
298                 [Test]
299                 public void TestWriteNamespaces2 ()
300                 {
301                         string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
302                         XmlSchema xs = new XmlSchema ();
303                         XmlSerializerNamespaces nss =
304                                 new XmlSerializerNamespaces ();
305                         StringWriter sw;
306                         sw = new StringWriter ();
307                         xs.Write (new XmlTextWriter (sw));
308                         Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#1");
309
310                         xs.Namespaces = nss;
311                         sw = new StringWriter ();
312                         xs.Write (new XmlTextWriter (sw));
313                         Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#2");
314
315                         nss.Add ("foo", "urn:foo");
316                         sw = new StringWriter ();
317                         xs.Write (new XmlTextWriter (sw));
318                         // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
319                         // Assert.AreEqual (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#3");
320
321                         nss.Add ("", "urn:foo");
322                         sw = new StringWriter ();
323                         xs.Write (new XmlTextWriter (sw));
324                         // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
325                         // Assert.AreEqual (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#4");
326
327                         nss.Add ("q1", "urn:q1");
328                         sw = new StringWriter ();
329                         xs.Write (new XmlTextWriter (sw));
330                         //Not sure if testing for exact order of these name spaces is
331                         // relevent, so using less strict test that passes on MS.NET
332                         //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 ());
333                         Assert.IsTrue (sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1, "q1");
334                 }
335
336                 [Test]
337                 public void ReaderPositionAfterRead ()
338                 {
339                         string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'>  <xs:element name='test' type='xs:integer'/></xs:schema>";
340                         XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
341                         xtr.Read ();
342                         XmlSchema xs = XmlSchema.Read (xtr, null);
343                         Assert.AreEqual (XmlNodeType.EndElement, xtr.NodeType);
344                 }
345
346                 [Test]
347                 // bug #76865
348                 public void AmbiguityDetectionOnChameleonAnyOther ()
349                 {
350                         string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
351 <xs:complexType name='TestType'>
352   <xs:sequence>
353     <xs:any namespace='##other' minOccurs='0' />
354     <xs:element name='Item' /> 
355     <xs:any namespace='##other' minOccurs='0' />
356   </xs:sequence> 
357 </xs:complexType>
358 </xs:schema>";
359                         XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
360                 }
361
362                 [Test]
363                 // bug #77685
364                 public void ReadDoesNotIgnoreDocumentationEmptyElement ()
365                 {
366                         string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
367   <xs:element name='choice'>
368     <xs:annotation><xs:documentation /></xs:annotation>
369   </xs:element>
370 </xs:schema>";
371                         XmlTextReader tr = new XmlTextReader (
372                                 schemaxml, XmlNodeType.Document, null);
373                         XmlSchema schema = XmlSchema.Read (tr, null);
374                         XmlSchemaElement element =
375                                 schema.Items [0] as XmlSchemaElement;
376                         XmlSchemaAnnotation annotation = element.Annotation;
377                         XmlSchemaDocumentation doc =
378                                 annotation.Items [0] as XmlSchemaDocumentation;
379                         Assert.AreEqual (0, doc.Markup.Length);
380                 }
381
382
383                 [Test]
384                 // bug #77687
385                 public void CompileFillsSchemaPropertyInExternal ()
386                 {
387                         string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
388                         XmlTextReader tr = new XmlTextReader (schemaFileName);
389
390                         XmlSchema schema = XmlSchema.Read (tr, null);
391                         XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
392                         Assert.IsNull (inc.Schema);
393                         schema.Compile (null);
394                         tr.Close ();
395                         Assert.IsNotNull (inc.Schema);
396                 }
397
398                 [Test]
399                 // bug #78985 (contains two identical field path "@key" in 
400                 // two different keys where one is in scope within another)
401                 public void DuplicateKeyFieldAttributePath ()
402                 {
403                         string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
404                         string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
405                         XmlTextReader tr = new XmlTextReader (schemaFileName);
406
407                         XmlValidatingReader vr = new XmlValidatingReader (
408                                 new XmlTextReader (xmlFileName));
409                         vr.Schemas.Add (XmlSchema.Read (tr, null));
410                         while (!vr.EOF)
411                                 vr.Read ();
412                 }
413
414                 [Test]
415                 public void ThreeLevelNestedInclusion ()
416                 {
417                         XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
418                         try {
419                                 XmlSchema xs = XmlSchema.Read (r, null);
420                                 xs.Compile (null);
421                         } finally {
422                                 r.Close ();
423                         }
424                 }
425
426                 [Test] // bug #502115
427                 public void ExtensionRedefineAttribute1 ()
428                 {
429                         const string xml = "<Bar xmlns='foo'/>";
430
431                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-1.xsd");
432
433 #if NET_2_0
434                         XmlSchemaSet xss = new XmlSchemaSet ();
435                         xss.Add (schema);
436                         if (StrictMsCompliant) {
437                                 xss.Compile ();
438                         } else {
439                                 try {
440                                         xss.Compile ();
441                                         Assert.Fail ();
442                                 } catch (XmlSchemaException) {
443                                 }
444                                 return;
445                         }
446
447                         StringReader sr = new StringReader (xml);
448
449                         XmlReaderSettings settings = new XmlReaderSettings ();
450                         settings.ValidationType = ValidationType.Schema;
451                         settings.Schemas = xss;
452                         XmlReader vr = XmlReader.Create (sr, settings);
453 #else
454                         if (StrictMsCompliant) {
455                                 schema.Compile (null);
456                         } else {
457                                 try {
458                                         schema.Compile (null);
459                                         Assert.Fail ();
460                                 } catch (XmlSchemaException) {
461                                 }
462                                 return;
463                         }
464
465                         XmlValidatingReader vr = new XmlValidatingReader (xml,
466                                 XmlNodeType.Document, null);
467                         vr.Schemas.Add (schema);
468                         vr.ValidationType = ValidationType.Schema;
469 #endif
470
471                         try {
472                                 vr.Read ();
473                                 Assert.Fail ();
474                         } catch (XmlSchemaException) {
475                         }
476                 }
477
478                 [Test] // bug #502115
479                 public void ExtensionRedefineAttribute2 ()
480                 {
481                         const string xml = "<Bar xmlns='foo'/>";
482
483                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
484
485 #if NET_2_0
486                         XmlSchemaSet xss = new XmlSchemaSet ();
487                         xss.Add (schema);
488                         xss.Compile ();
489
490                         StringReader sr = new StringReader (xml);
491
492                         XmlReaderSettings settings = new XmlReaderSettings ();
493                         settings.ValidationType = ValidationType.Schema;
494                         settings.Schemas = xss;
495                         XmlReader vr = XmlReader.Create (sr, settings);
496 #else
497                         schema.Compile (null);
498
499                         XmlValidatingReader vr = new XmlValidatingReader (xml,
500                                 XmlNodeType.Document, null);
501                         vr.Schemas.Add (schema);
502                         vr.ValidationType = ValidationType.Schema;
503 #endif
504
505                         while (vr.Read ()) ;
506                 }
507
508                 [Test] // bug #502115
509                 public void ExtensionRedefineAttribute3 ()
510                 {
511                         const string xml = "<Bar xmlns='foo'/>";
512
513                         XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
514
515 #if NET_2_0
516                         XmlSchemaSet xss = new XmlSchemaSet ();
517                         xss.Add (schema);
518                         if (StrictMsCompliant) {
519                                 xss.Compile ();
520                         } else {
521                                 try {
522                                         xss.Compile ();
523                                         Assert.Fail ();
524                                 } catch (XmlSchemaException) {
525                                 }
526                                 return;
527                         }
528
529                         StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
530
531                         XmlReaderSettings settings = new XmlReaderSettings ();
532                         settings.ValidationType = ValidationType.Schema;
533                         settings.Schemas = xss;
534                         XmlReader vr = XmlReader.Create (sr, settings);
535 #else
536                         if (StrictMsCompliant) {
537                                 schema.Compile (null);
538                         } else {
539                                 try {
540                                         schema.Compile (null);
541                                         Assert.Fail ();
542                                 } catch (XmlSchemaException) {
543                                 }
544                                 return;
545                         }
546
547                         XmlValidatingReader vr = new XmlValidatingReader (xml,
548                                 XmlNodeType.Document, null);
549                         vr.Schemas.Add (schema);
550                         vr.ValidationType = ValidationType.Schema;
551 #endif
552
553                         while (vr.Read ()) ;
554                 }
555
556 #if NET_2_0
557
558                 internal class XmlTestResolver : XmlResolver
559                 {                       
560                         Uri receivedUri;
561
562                         public override ICredentials Credentials
563                         {
564                             set { throw new NotSupportedException (); }
565                         }
566
567                         public override Uri ResolveUri (Uri baseUri, string relativeUri)
568                         {
569                             return new Uri (relativeUri);
570                         }
571                         
572                         public Uri ReceivedUri
573                         {
574                                 get { return receivedUri; }
575                         }
576
577                         public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
578                         {
579                                 receivedUri = absoluteUri;
580                                 
581                                 return null;
582                         }
583                 }       
584                 
585                 [Test]
586                 public void TestResolveUri ()
587                 {
588                         XmlSchemaSet schemaSet = new XmlSchemaSet ();
589                         FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open, FileAccess.Read);
590                         schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
591
592                         XmlTestResolver resolver = new XmlTestResolver ();              
593                         
594                         XmlReaderSettings settings = new XmlReaderSettings ();                  
595                         settings.Schemas.XmlResolver = resolver; 
596                         settings.Schemas.Add (schemaSet);
597                         settings.ValidationType = ValidationType.Schema;
598                         settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
599                         XmlReader reader = XmlReader.Create (stream, settings);
600                         
601                         try
602                         {
603                                 reader.Read ();         
604                         }
605                         catch (XmlException)
606                         {
607                                 // do nothing - we are expecting this exception because the test xmlresolver returns null from its 
608                                 // GetEntity method.
609                         }
610                         
611                         Assert.AreEqual ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
612                 }
613
614                 [Test]
615                 public void TestImportNoSchemaLocation()
616                 {
617                         XmlSchemaSet schemaSet = new XmlSchemaSet ();
618                         schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
619                         schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
620                         
621                         XmlReaderSettings settings = new XmlReaderSettings ();
622                         settings.Schemas.Add (schemaSet);
623                         settings.ValidationType = ValidationType.Schema;
624                         
625                         XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
626                         
627                         // Parse the file. 
628                         while (reader.Read()) {}
629                 }
630 #endif
631
632                 [Test]
633                 public void TestImportSchemaThatIncludesAnother ()
634                 {
635                         XmlSchema xs = GetSchema ("Test/XmlFiles/xsd/importNamespaceTest2.xsd");
636                         xs.Compile (null);
637                 }
638         }
639 }