2 // ApplicationDirectoryTest.cs - NUnit Test Cases for ApplicationDirectory
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit
.Framework
;
32 using System
.Security
.Policy
;
35 namespace MonoTests
.System
.Security
.Policy
{
38 public class ApplicationDirectoryTest
{
40 private string Invalid (bool exception
)
42 StringBuilder sb
= new StringBuilder ();
43 foreach (char c
in Path
.InvalidPathChars
)
45 if ((exception
) && (sb
.Length
< 1))
46 throw new ArgumentException ("no invalid chars");
47 return sb
.ToString ();
51 [ExpectedException (typeof (ArgumentNullException
))]
52 public void ApplicationDirectory_Null ()
54 new ApplicationDirectory (null);
58 [ExpectedException (typeof (FormatException
))]
59 public void ApplicationDirectory_Empty ()
61 new ApplicationDirectory (String
.Empty
);
66 [ExpectedException (typeof (ArgumentException
))]
68 public void ApplicationDirectory_Invalid ()
70 new ApplicationDirectory (Invalid (false));
74 public void ApplicationDirectory_String ()
76 ApplicationDirectory ad
= new ApplicationDirectory ("mono");
78 Assert
.AreEqual ("mono", ad
.Directory
, "Directory");
80 Assert
.AreEqual ("file://MONO", ad
.Directory
, "Directory");
85 public void ApplicationDirectory_FileUrl ()
87 ApplicationDirectory ad
= new ApplicationDirectory ("file://MONO");
88 Assert
.AreEqual ("file://MONO", ad
.Directory
, "Directory");
92 public void ApplicationDirectory_HttpUrl ()
94 ApplicationDirectory ad
= new ApplicationDirectory ("http://www.go-mono.com/");
95 Assert
.AreEqual ("http://www.go-mono.com/", ad
.Directory
, "Directory");
101 ApplicationDirectory ad
= new ApplicationDirectory ("novell");
103 Assert
.AreEqual ("novell", ad
.Directory
, "Directory");
105 Assert
.AreEqual ("file://NOVELL", ad
.Directory
, "Directory");
107 ApplicationDirectory copy
= (ApplicationDirectory
)ad
.Copy ();
108 Assert
.IsTrue (ad
.Equals (copy
), "ad.Equals(copy)");
109 Assert
.IsTrue (copy
.Equals (ad
), "copy.Equals(ad)");
110 Assert
.IsFalse (Object
.ReferenceEquals (ad
, copy
), "Copy");
111 Assert
.AreEqual (ad
.GetHashCode (), copy
.GetHashCode (), "GetHashCode");
112 Assert
.AreEqual (ad
.ToString (), copy
.ToString (), "ToString");
116 public void Equals ()
118 ApplicationDirectory ad1
= new ApplicationDirectory ("mono");
119 Assert
.IsFalse (ad1
.Equals (null), "Equals(null)");
120 Assert
.IsFalse (ad1
.Equals (String
.Empty
), "Equals(String.Empty)");
121 Assert
.IsFalse (ad1
.Equals ("mono"), "Equals(mono)");
122 Assert
.IsTrue (ad1
.Equals (ad1
), "Equals(self)");
123 ApplicationDirectory ad2
= new ApplicationDirectory (ad1
.Directory
);
124 Assert
.IsTrue (ad1
.Equals (ad2
), "Equals(ad2)");
125 Assert
.IsTrue (ad2
.Equals (ad1
), "Equals(ad2)");
126 ApplicationDirectory ad3
= new ApplicationDirectory ("..");
127 Assert
.IsFalse (ad2
.Equals (ad3
), "Equals(ad3)");
131 public void GetHashCode_ ()
133 string linux
= "/unix/path/mono";
134 ApplicationDirectory ad1
= new ApplicationDirectory (linux
);
136 Assert
.AreEqual (linux
, ad1
.Directory
);
137 Assert
.AreEqual (linux
.GetHashCode (), ad1
.GetHashCode (), "GetHashCode-Linux");
139 Assert
.AreEqual ("file://UNIX/PATH/mono", ad1
.Directory
);
140 Assert
.AreEqual ("file://UNIX/PATH/mono".GetHashCode (), ad1
.GetHashCode (), "GetHashCode-Linux");
143 string windows
= "\\windows\\path\\mono";
144 ApplicationDirectory ad2
= new ApplicationDirectory (windows
);
146 Assert
.AreEqual (windows
, ad2
.Directory
);
147 Assert
.AreEqual (windows
.GetHashCode (), ad2
.GetHashCode (), "GetHashCode-Windows");
149 Assert
.AreEqual ("file://WINDOWS/PATH/mono", ad2
.Directory
);
150 Assert
.AreEqual ("file://WINDOWS/PATH/mono".GetHashCode (), ad2
.GetHashCode (), "GetHashCode-Windows");
155 public void ToString_ ()
157 ApplicationDirectory ad
= new ApplicationDirectory ("file://MONO");
158 string ts
= ad
.ToString ();
159 Assert
.IsTrue (ts
.StartsWith ("<System.Security.Policy.ApplicationDirectory"), "Tag");
160 Assert
.IsTrue (ts
.IndexOf ("version=\"1\"") > 0, "Directory");
161 Assert
.IsTrue (ts
.IndexOf ("<Directory>file://MONO</Directory>") > 0, "Directory");
166 [ExpectedException (typeof (ArgumentException
))]
167 public void Equals_Invalid ()
170 string appdir
= Invalid (true);
171 // constructor is ok with an invalid path...
172 ApplicationDirectory ad
= new ApplicationDirectory (appdir
);
174 ApplicationDirectory copy
= (ApplicationDirectory
)ad
.Copy ();
175 // we can't get it's hash code
176 Assert
.AreEqual (appdir
.GetHashCode (), ad
.GetHashCode (), "GetHashCode");
177 // we can convert it to string...
178 Assert
.IsTrue (ad
.ToString ().IndexOf (appdir
) > 0, "ToString");
179 // ... but it throws in Equals - with self!
180 Assert
.IsTrue (ad
.Equals (ad
), "Equals(self)");
184 [ExpectedException (typeof (ArgumentException
))]
185 public void ToString_Invalid ()
187 new ApplicationDirectory (Invalid (true)).ToString ();