XmlUrlResolverTests.cs 3.6 KB

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