XmlSchemaTests.cs 20 KB

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