merge -r 58060:58217
[mono.git] / mcs / class / System.Web / Test / System.Web / VirtualPathUtilityTest.cs
1 //
2 // System.Web.VirtualPathUtilityTest.cs - Unit tests for System.Web.VirtualPathUtility
3 //
4 // Author:
5 //      Chris Toshok  <toshok@novell.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Web;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web {
37
38         [TestFixture]
39         public class VirtualPathUtilityTest {
40                 [Test]
41                 public void AppendTrailingSlash ()
42                 {
43                         Assert.AreEqual ("/hithere/", VirtualPathUtility.AppendTrailingSlash ("/hithere"), "A1");
44                         Assert.AreEqual ("/hithere/", VirtualPathUtility.AppendTrailingSlash ("/hithere/"), "A2");
45                         Assert.AreEqual ("/", VirtualPathUtility.AppendTrailingSlash ("/"), "A3");
46                         Assert.AreEqual ("", VirtualPathUtility.AppendTrailingSlash (""), "A4");
47                         Assert.AreEqual (null, VirtualPathUtility.AppendTrailingSlash (null), "A5");
48                 }
49
50                 [Test]
51                 public void Combine ()
52                 {
53                         Assert.AreEqual ("/there", VirtualPathUtility.Combine ("/hi", "there"), "A1");
54                         Assert.AreEqual ("/hi/you", VirtualPathUtility.Combine ("/hi/there", "you"), "A2");
55                         Assert.AreEqual ("/hi/there/you", VirtualPathUtility.Combine ("/hi/there/", "you"), "A3");
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentException))]
60                 public void Combine_ArgException1 ()
61                 {
62                         Assert.AreEqual ("hi/there/you", VirtualPathUtility.Combine ("hi/there", "you"), "A1");
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentException))]
67                 public void Combine_ArgException2 ()
68                 {
69                         Assert.AreEqual ("hi/there", VirtualPathUtility.Combine ("hi/there", null), "A1");
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentNullException))]
74                 public void Combine_ArgException3 ()
75                 {
76                         Assert.AreEqual ("hi/there", VirtualPathUtility.Combine (null, "there"), "A1");
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (ArgumentNullException))]
81                 /* stack trace is:
82                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
83                    at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
84                    at MonoTests.System.Web.VirtualPathUtilityTest.Combine()
85                 */
86                 public void Combine_ArgException4 ()
87                 {
88                         Assert.AreEqual ("/you", VirtualPathUtility.Combine ("", "you"), "A1");
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 /* stack trace is:
94                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
95                    at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
96                    at MonoTests.System.Web.VirtualPathUtilityTest.Combine()
97                 */
98                 public void Combine_ArgException5 ()
99                 {
100                         Assert.AreEqual ("/hi", VirtualPathUtility.Combine ("/hi", ""), "A1");
101                 }
102
103                 [Test]
104                 public void GetDirectory ()
105                 {
106                         Assert.AreEqual ("/hi/", VirtualPathUtility.GetDirectory ("/hi/there"), "A1");
107                         Assert.AreEqual ("/hi/", VirtualPathUtility.GetDirectory ("/hi/there/"), "A2");
108                         Assert.AreEqual (null, VirtualPathUtility.GetDirectory ("/"), "A3");
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof (ArgumentNullException))]
113                 /* stack trace is:
114                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
115                    at System.Web.VirtualPathUtility.GetDirectory(String virtualPath)
116                    at MonoTests.System.Web.VirtualPathUtilityTest.GetDirectory()
117                  */
118                 public void GetDirectory_ArgException1 ()
119                 {
120                         Assert.AreEqual ("", VirtualPathUtility.GetDirectory (""), "A1");
121                 }
122
123                 [Test]
124                 public void GetExtension ()
125                 {
126                         Assert.AreEqual (".aspx", VirtualPathUtility.GetExtension ("/hi/index.aspx"), "A1");
127                         Assert.AreEqual (".aspx", VirtualPathUtility.GetExtension ("index.aspx"), "A2");
128                         Assert.AreEqual ("", VirtualPathUtility.GetExtension ("/hi/index"), "A3");
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (ArgumentNullException))]
133                 public void GetExtension_ArgException1 ()
134                 {
135                         Assert.AreEqual (null, VirtualPathUtility.GetExtension (null), "A1");
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentNullException))]
140                 public void GetExtension_ArgException2 ()
141                 {
142                         Assert.AreEqual ("", VirtualPathUtility.GetExtension (""), "A1");
143                 }
144
145                 [Test]
146                 public void GetFileName ()
147                 {
148                         Assert.AreEqual ("index.aspx", VirtualPathUtility.GetFileName ("/hi/index.aspx"), "A1");
149                         Assert.AreEqual ("hi", VirtualPathUtility.GetFileName ("/hi/"), "A2");
150                 }
151
152                 [Test]
153                 [ExpectedException (typeof (ArgumentNullException))]
154                 public void GetFileName_ArgException1 ()
155                 {
156                         Assert.AreEqual (null, VirtualPathUtility.GetFileName (null), "A1");
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (ArgumentNullException))]
161                 public void GetFileName_ArgException2 ()
162                 {
163                         Assert.AreEqual ("", VirtualPathUtility.GetFileName (""), "A1");
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (ArgumentException))]
168                 public void GetFileName_ArgException3 ()
169                 {
170                         Assert.AreEqual ("index.aspx", VirtualPathUtility.GetFileName ("index.aspx"), "A1");
171                 }
172
173                 [Test]
174                 public void IsAbsolute ()
175                 {
176                         Assert.IsTrue (VirtualPathUtility.IsAbsolute ("/"), "A1");
177                         Assert.IsTrue (VirtualPathUtility.IsAbsolute ("/hi/there"), "A2");
178                         Assert.IsFalse (VirtualPathUtility.IsAbsolute ("hi/there"), "A3");
179                         Assert.IsFalse (VirtualPathUtility.IsAbsolute ("./hi"), "A4");
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentNullException))]
184                 public void IsAbsolute_ArgException1 ()
185                 {
186                         Assert.IsFalse (VirtualPathUtility.IsAbsolute (""), "A1");
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof (ArgumentNullException))]
191                 public void IsAbsolute_ArgException2 ()
192                 {
193                         Assert.IsFalse (VirtualPathUtility.IsAbsolute (null), "A1");
194                 }
195
196                 [Test]
197                 public void IsAppRelative ()
198                 {
199                         Assert.IsTrue (VirtualPathUtility.IsAppRelative ("~/Stuff"), "A1");
200                         Assert.IsFalse (VirtualPathUtility.IsAppRelative ("./Stuff"), "A2");
201                         Assert.IsFalse (VirtualPathUtility.IsAppRelative ("/Stuff"), "A3");
202                         Assert.IsFalse (VirtualPathUtility.IsAppRelative ("/"), "A4");
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (ArgumentNullException))]
207                 public void IsAppRelative_ArgException1 ()
208                 {
209                         Assert.IsFalse (VirtualPathUtility.IsAppRelative (""), "A1");
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (ArgumentNullException))]
214                 public void IsAppRelative_ArgException2 ()
215                 {
216                         Assert.IsFalse (VirtualPathUtility.IsAppRelative (null), "A1");
217                 }
218
219 #if false
220                 [Test]
221                 /* this test when run on MS generates the following stack trace (NRE):
222                    at System.Web.Util.UrlPath.MakeVirtualPathAppAbsolute(String virtualPath, String applicationPath)
223                    at System.Web.Util.UrlPath.MakeRelative(String from, String to)
224                    at System.Web.VirtualPathUtility.MakeRelative(String fromPath, String toPath)
225                    at MonoTests.System.Web.VirtualPathUtilityTest.MakeRelative()
226                 */
227                 public void MakeRelative ()
228                 {
229                         Assert.AreEqual ("../bar", VirtualPathUtility.MakeRelative ("~/foo/hi", "~/foo/bar"), "A1");
230                 }
231 #endif
232
233                 [Test]
234                 public void RemoveTrailingSlash ()
235                 {
236                         Assert.AreEqual ("/hi/there", VirtualPathUtility.RemoveTrailingSlash ("/hi/there/"), "A1");
237                         Assert.AreEqual ("/hi/there", VirtualPathUtility.RemoveTrailingSlash ("/hi/there"), "A2");
238                         Assert.AreEqual ("/", VirtualPathUtility.RemoveTrailingSlash ("/"), "A3");
239                         Assert.AreEqual (null, VirtualPathUtility.RemoveTrailingSlash (""), "A4");
240                         Assert.AreEqual (null, VirtualPathUtility.RemoveTrailingSlash (null), "A5");
241                 }
242         }
243 }
244
245 #endif