XmlUrlResolverTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.Xml.XmlUrlResolver.cs
  3. //
  4. // Authors:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C) 2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Xml;
  12. using NUnit.Framework;
  13. using System.Reflection;
  14. using MonoTests.Helpers;
  15. namespace MonoTests.System.Xml
  16. {
  17. [TestFixture]
  18. public class XmlUrlResolverTests
  19. {
  20. XmlUrlResolver resolver;
  21. [SetUp]
  22. public void GetReady ()
  23. {
  24. resolver = new XmlUrlResolver ();
  25. }
  26. [Test]
  27. public void FileUri ()
  28. {
  29. Uri resolved = resolver.ResolveUri (null, TestResourceHelper.GetFullPathOfResource ("Test/XmlFiles/xsd/xml.xsd"));
  30. Assert.AreEqual ("file", resolved.Scheme);
  31. Stream s = resolver.GetEntity (resolved, null, typeof (Stream)) as Stream;
  32. }
  33. [Test]
  34. public void FileUri2 ()
  35. {
  36. Assert.AreEqual (resolver.ResolveUri (new Uri ("file://usr/local/src"), null).ToString (), "file://usr/local/src");
  37. // MS.NET returns the Uri.ToString() as
  38. // file://usr/local/src, but it is apparently
  39. // incorrect in the context of Unix path.
  40. Assert.AreEqual (resolver.ResolveUri (new Uri ("file:///usr/local/src"), null).ToString (), "file:///usr/local/src");
  41. }
  42. [Test]
  43. public void HttpUri ()
  44. {
  45. Assert.AreEqual (resolver.ResolveUri (null, "http://test.xml").ToString (), "http://test.xml/");
  46. }
  47. [Test]
  48. public void HttpUri2 ()
  49. {
  50. Assert.AreEqual (resolver.ResolveUri (new Uri ("http://go-mono.com"), null).ToString (), "http://go-mono.com/");
  51. }
  52. [Test]
  53. [Category ("NotDotNet")] // It should throw ArgumentNullException.
  54. [Ignore(".NET implementation does not throw ArgumentNullException.")]
  55. [ExpectedException (typeof (ArgumentNullException))]
  56. public void ResolveUriWithNullArgs ()
  57. {
  58. resolver.ResolveUri (null, null);
  59. Assert.Fail ("Should be error (MS.NET throws ArgumentException here).");
  60. }
  61. // [Test] Uncomment if you want to test.
  62. public void GetEntityWithNullArgs ()
  63. {
  64. Uri uri = new Uri ("http://www.go-mono.com/index.rss");
  65. resolver.GetEntity (uri, null, null);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (InvalidOperationException))]
  69. public void GetEntityWithRelativeFileUri ()
  70. {
  71. resolver.GetEntity (new Uri ("file.txt", UriKind.Relative), null, typeof (Stream));
  72. }
  73. [Test]
  74. [ExpectedException (typeof (XmlException))]
  75. public void GetEntityWithNonStreamReturnType ()
  76. {
  77. resolver.GetEntity (new Uri ("http://www.go-mono.com/"), null, typeof (File));
  78. }
  79. [Test] // bug #998
  80. public void NullAbsoluteUriWithCustomSchemedRelativeUri ()
  81. {
  82. XmlResolver res = new XmlUrlResolver ();
  83. var uri = res.ResolveUri (null, "view:Standard.xslt");
  84. Assert.AreEqual ("view", uri.Scheme, "#1");
  85. Assert.AreEqual ("Standard.xslt", uri.AbsolutePath, "#2");
  86. Assert.AreEqual ("view:Standard.xslt", uri.AbsoluteUri, "#2");
  87. }
  88. [Test]
  89. [Category ("StaticLinkedAotNotWorking")] // Can't find .dll files when bundled in .exe
  90. public void TestAsync ()
  91. {
  92. var loc = Assembly.GetExecutingAssembly ().Location;
  93. Uri resolved = resolver.ResolveUri (null, loc);
  94. Assert.AreEqual ("file", resolved.Scheme);
  95. var task = resolver.GetEntityAsync (resolved, null, typeof (Stream));
  96. Assert.IsTrue (task.Wait (3000));
  97. Assert.IsTrue (task.Result is Stream);
  98. }
  99. [Test]
  100. public void TestAsyncError ()
  101. {
  102. var loc = Assembly.GetExecutingAssembly ().Location;
  103. Uri resolved = resolver.ResolveUri (null, loc);
  104. Assert.AreEqual ("file", resolved.Scheme);
  105. var task = resolver.GetEntityAsync (resolved, null, typeof (File));
  106. try {
  107. task.Wait (3000);
  108. Assert.Fail ("#1");
  109. } catch (Exception ex) {
  110. if (ex is AggregateException)
  111. ex = ((AggregateException) ex).InnerException;
  112. Assert.IsTrue (ex is XmlException);
  113. }
  114. }
  115. }
  116. }