Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.XML / Test / System.Xml / XmlUrlResolverTests.cs
1 //\r
2 // System.Xml.XmlUrlResolver.cs\r
3 //\r
4 // Authors:\r
5 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)\r
6 //\r
7 // (C) 2003 Atsushi Enomoto\r
8 //\r
9 using System;\r
10 using System.IO;\r
11 using System.Xml;\r
12 using NUnit.Framework;\r
13 #if NET_4_5\r
14 using System.Reflection;\r
15 #endif\r
16 \r
17 namespace MonoTests.System.Xml\r
18 {\r
19         [TestFixture]\r
20         public class XmlUrlResolverTests\r
21         {\r
22                 XmlUrlResolver resolver;\r
23 \r
24                 [SetUp]\r
25                 public void GetReady ()\r
26                 {\r
27                         resolver = new XmlUrlResolver ();\r
28                 }\r
29 \r
30                 [Test]\r
31                 public void FileUri ()\r
32                 {\r
33                         Uri resolved = resolver.ResolveUri (null, "Test/XmlFiles/xsd/xml.xsd");\r
34                         Assert.AreEqual ("file", resolved.Scheme);\r
35                         Stream s = resolver.GetEntity (resolved, null, typeof (Stream)) as Stream;\r
36                 }\r
37 \r
38                 [Test]\r
39                 [Category ("NotDotNet")]\r
40                 public void FileUri2 ()\r
41                 {\r
42                         Assert.AreEqual (resolver.ResolveUri (new Uri ("file://usr/local/src"), null).ToString (), "file://usr/local/src");\r
43                         // MS.NET returns the Uri.ToString() as \r
44                         // file://usr/local/src, but it is apparently \r
45                         // incorrect in the context of Unix path.\r
46                         Assert.AreEqual (resolver.ResolveUri (new Uri ("file:///usr/local/src"), null).ToString (), "file:///usr/local/src");\r
47                 }\r
48 \r
49                 [Test]\r
50                 public void HttpUri ()\r
51                 {\r
52                         Assert.AreEqual (resolver.ResolveUri (null, "http://test.xml").ToString (), "http://test.xml/");\r
53                 }\r
54 \r
55                 [Test]\r
56                 public void HttpUri2 ()\r
57                 {\r
58                         Assert.AreEqual (resolver.ResolveUri (new Uri ("http://go-mono.com"), null).ToString (), "http://go-mono.com/");\r
59                 }\r
60 \r
61                 [Test]\r
62 #if !NET_2_0\r
63                 [Category ("NotDotNet")] // It should throw ArgumentNullException.\r
64 #endif\r
65                 [ExpectedException (typeof (ArgumentNullException))]\r
66                 public void ResolveUriWithNullArgs ()\r
67                 {\r
68                         resolver.ResolveUri (null, null);\r
69                         Assert.Fail ("Should be error (MS.NET throws ArgumentException here).");\r
70                 }\r
71 \r
72 //              [Test] Uncomment if you want to test.\r
73                 public void GetEntityWithNullArgs ()\r
74                 {\r
75                         Uri uri = new Uri ("http://www.go-mono.com/index.rss");\r
76                         resolver.GetEntity (uri, null, null);\r
77                 }\r
78 \r
79 #if NET_2_0\r
80                 [Test]\r
81                 [ExpectedException (typeof (ArgumentException))]\r
82                 public void GetEntityWithRelativeFileUri ()\r
83                 {\r
84                         resolver.GetEntity (new Uri ("file.txt", UriKind.Relative), null, typeof (Stream));\r
85                 }\r
86 #endif\r
87 \r
88                 [Test]\r
89                 [ExpectedException (typeof (XmlException))]\r
90                 public void GetEntityWithNonStreamReturnType ()\r
91                 {\r
92                         resolver.GetEntity (new Uri ("http://www.go-mono.com/"), null, typeof (File));\r
93                 }\r
94 \r
95                 [Test] // bug #998\r
96                 public void NullAbsoluteUriWithCustomSchemedRelativeUri ()\r
97                 {\r
98                         XmlResolver res = new XmlUrlResolver ();\r
99                         var uri = res.ResolveUri (null, "view:Standard.xslt");\r
100                         Assert.AreEqual ("view", uri.Scheme, "#1");\r
101                         Assert.AreEqual ("Standard.xslt", uri.AbsolutePath, "#2");\r
102                         Assert.AreEqual ("view:Standard.xslt", uri.AbsoluteUri, "#2");\r
103                 }\r
104 \r
105 #if NET_4_5\r
106                 [Test]\r
107                 [Category("Async")]\r
108                 public void TestAsync ()\r
109                 {\r
110                         var loc = Assembly.GetExecutingAssembly ().Location;\r
111                         Uri resolved = resolver.ResolveUri (null, loc);\r
112                         Assert.AreEqual ("file", resolved.Scheme);\r
113                         var task = resolver.GetEntityAsync (resolved, null, typeof (Stream));\r
114                         Assert.That (task.Wait (3000));\r
115                         Assert.IsInstanceOfType (typeof (Stream), task.Result);\r
116                 }\r
117 \r
118                 [Test]\r
119                 [Category("Async")]\r
120                 public void TestAsyncError ()\r
121                 {\r
122                         var loc = Assembly.GetExecutingAssembly ().Location;\r
123                         Uri resolved = resolver.ResolveUri (null, loc);\r
124                         Assert.AreEqual ("file", resolved.Scheme);\r
125                         var task = resolver.GetEntityAsync (resolved, null, typeof (File));\r
126                         try {\r
127                                 task.Wait (3000);\r
128                                 Assert.Fail ("#1");\r
129                         } catch (Exception ex) {\r
130                                 if (ex is AggregateException)\r
131                                         ex = ((AggregateException) ex).InnerException;\r
132                                 Assert.IsInstanceOfType (typeof (XmlException), ex);\r
133                         }\r
134                 }\r
135 #endif\r
136         }\r
137 }\r