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
);
65 public void ApplicationDirectory_Invalid ()
67 new ApplicationDirectory (Invalid (false));
71 public void ApplicationDirectory_String ()
73 ApplicationDirectory ad
= new ApplicationDirectory ("mono");
74 Assert
.AreEqual ("mono", ad
.Directory
, "Directory");
78 public void ApplicationDirectory_FileUrl ()
80 ApplicationDirectory ad
= new ApplicationDirectory ("file://MONO");
81 Assert
.AreEqual ("file://MONO", ad
.Directory
, "Directory");
85 public void ApplicationDirectory_HttpUrl ()
87 ApplicationDirectory ad
= new ApplicationDirectory ("http://www.example.com/");
88 Assert
.AreEqual ("http://www.example.com/", ad
.Directory
, "Directory");
94 ApplicationDirectory ad
= new ApplicationDirectory ("novell");
95 Assert
.AreEqual ("novell", ad
.Directory
, "Directory");
96 ApplicationDirectory copy
= (ApplicationDirectory
)ad
.Copy ();
97 Assert
.IsTrue (ad
.Equals (copy
), "ad.Equals(copy)");
98 Assert
.IsTrue (copy
.Equals (ad
), "copy.Equals(ad)");
99 Assert
.IsFalse (Object
.ReferenceEquals (ad
, copy
), "Copy");
100 Assert
.AreEqual (ad
.GetHashCode (), copy
.GetHashCode (), "GetHashCode");
101 Assert
.AreEqual (ad
.ToString (), copy
.ToString (), "ToString");
105 public void Equals ()
107 ApplicationDirectory ad1
= new ApplicationDirectory ("mono");
108 Assert
.IsFalse (ad1
.Equals (null), "Equals(null)");
109 Assert
.IsFalse (ad1
.Equals (String
.Empty
), "Equals(String.Empty)");
110 Assert
.IsFalse (ad1
.Equals ("mono"), "Equals(mono)");
111 Assert
.IsTrue (ad1
.Equals (ad1
), "Equals(self)");
112 ApplicationDirectory ad2
= new ApplicationDirectory (ad1
.Directory
);
113 Assert
.IsTrue (ad1
.Equals (ad2
), "Equals(ad2)");
114 Assert
.IsTrue (ad2
.Equals (ad1
), "Equals(ad2)");
115 ApplicationDirectory ad3
= new ApplicationDirectory ("..");
116 Assert
.IsFalse (ad2
.Equals (ad3
), "Equals(ad3)");
120 public void GetHashCode_ ()
122 string linux
= "/unix/path/mono";
123 ApplicationDirectory ad1
= new ApplicationDirectory (linux
);
124 Assert
.AreEqual (linux
, ad1
.Directory
);
125 Assert
.AreEqual (linux
.GetHashCode (), ad1
.GetHashCode (), "GetHashCode-Linux");
127 string windows
= "\\windows\\path\\mono";
128 ApplicationDirectory ad2
= new ApplicationDirectory (windows
);
129 Assert
.AreEqual (windows
, ad2
.Directory
);
130 Assert
.AreEqual (windows
.GetHashCode (), ad2
.GetHashCode (), "GetHashCode-Windows");
134 public void ToString_ ()
136 ApplicationDirectory ad
= new ApplicationDirectory ("file://MONO");
137 string ts
= ad
.ToString ();
138 Assert
.IsTrue (ts
.StartsWith ("<System.Security.Policy.ApplicationDirectory"), "Tag");
139 Assert
.IsTrue (ts
.IndexOf ("version=\"1\"") > 0, "Directory");
140 Assert
.IsTrue (ts
.IndexOf ("<Directory>file://MONO</Directory>") > 0, "Directory");
144 [ExpectedException (typeof (ArgumentException
))]
145 public void Equals_Invalid ()
148 string appdir
= Invalid (true);
149 // constructor is ok with an invalid path...
150 ApplicationDirectory ad
= new ApplicationDirectory (appdir
);
152 ApplicationDirectory copy
= (ApplicationDirectory
)ad
.Copy ();
153 // we can't get it's hash code
154 Assert
.AreEqual (appdir
.GetHashCode (), ad
.GetHashCode (), "GetHashCode");
155 // we can convert it to string...
156 Assert
.IsTrue (ad
.ToString ().IndexOf (appdir
) > 0, "ToString");
157 // ... but it throws in Equals - with self!
158 Assert
.IsTrue (ad
.Equals (ad
), "Equals(self)");
162 [ExpectedException (typeof (ArgumentException
))]
163 public void ToString_Invalid ()
165 new ApplicationDirectory (Invalid (true)).ToString ();