XmlSchemaTests.cs 19 KB

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