XmlSchemaTests.cs 19 KB

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