**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationDirectoryTest.cs
blob4b245b9f80f99078c1c5e9a397e50f87d7f16608
1 //
2 // ApplicationDirectoryTest.cs - NUnit Test Cases for ApplicationDirectory
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 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.
29 using NUnit.Framework;
30 using System;
31 using System.IO;
32 using System.Security.Policy;
33 using System.Text;
35 namespace MonoTests.System.Security.Policy {
37 [TestFixture]
38 public class ApplicationDirectoryTest {
40 private string Invalid (bool exception)
42 StringBuilder sb = new StringBuilder ();
43 foreach (char c in Path.InvalidPathChars)
44 sb.Append (c);
45 if ((exception) && (sb.Length < 1))
46 throw new ArgumentException ("no invalid chars");
47 return sb.ToString ();
50 [Test]
51 [ExpectedException (typeof (ArgumentNullException))]
52 public void ApplicationDirectory_Null ()
54 new ApplicationDirectory (null);
57 [Test]
58 [ExpectedException (typeof (FormatException))]
59 public void ApplicationDirectory_Empty ()
61 new ApplicationDirectory (String.Empty);
64 [Test]
65 #if !NET_2_0
66 [ExpectedException (typeof (ArgumentException))]
67 #endif
68 public void ApplicationDirectory_Invalid ()
70 new ApplicationDirectory (Invalid (false));
73 [Test]
74 public void ApplicationDirectory_String ()
76 ApplicationDirectory ad = new ApplicationDirectory ("mono");
77 #if NET_2_0
78 Assert.AreEqual ("mono", ad.Directory, "Directory");
79 #else
80 Assert.AreEqual ("file://MONO", ad.Directory, "Directory");
81 #endif
84 [Test]
85 public void ApplicationDirectory_FileUrl ()
87 ApplicationDirectory ad = new ApplicationDirectory ("file://MONO");
88 Assert.AreEqual ("file://MONO", ad.Directory, "Directory");
91 [Test]
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");
98 [Test]
99 public void Copy ()
101 ApplicationDirectory ad = new ApplicationDirectory ("novell");
102 #if NET_2_0
103 Assert.AreEqual ("novell", ad.Directory, "Directory");
104 #else
105 Assert.AreEqual ("file://NOVELL", ad.Directory, "Directory");
106 #endif
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");
115 [Test]
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)");
130 [Test]
131 public void GetHashCode_ ()
133 string linux = "/unix/path/mono";
134 ApplicationDirectory ad1 = new ApplicationDirectory (linux);
135 #if NET_2_0
136 Assert.AreEqual (linux, ad1.Directory);
137 Assert.AreEqual (linux.GetHashCode (), ad1.GetHashCode (), "GetHashCode-Linux");
138 #else
139 Assert.AreEqual ("file://UNIX/PATH/mono", ad1.Directory);
140 Assert.AreEqual ("file://UNIX/PATH/mono".GetHashCode (), ad1.GetHashCode (), "GetHashCode-Linux");
141 #endif
143 string windows = "\\windows\\path\\mono";
144 ApplicationDirectory ad2 = new ApplicationDirectory (windows);
145 #if NET_2_0
146 Assert.AreEqual (windows, ad2.Directory);
147 Assert.AreEqual (windows.GetHashCode (), ad2.GetHashCode (), "GetHashCode-Windows");
148 #else
149 Assert.AreEqual ("file://WINDOWS/PATH/mono", ad2.Directory);
150 Assert.AreEqual ("file://WINDOWS/PATH/mono".GetHashCode (), ad2.GetHashCode (), "GetHashCode-Windows");
151 #endif
152 Assert.IsFalse ((ad1.GetHashCode () == ad2.GetHashCode ()), "GetHashCode-Compare");
155 [Test]
156 public void ToString_ ()
158 ApplicationDirectory ad = new ApplicationDirectory ("file://MONO");
159 string ts = ad.ToString ();
160 Assert.IsTrue (ts.StartsWith ("<System.Security.Policy.ApplicationDirectory"), "Tag");
161 Assert.IsTrue (ts.IndexOf ("version=\"1\"") > 0, "Directory");
162 Assert.IsTrue (ts.IndexOf ("<Directory>file://MONO</Directory>") > 0, "Directory");
165 #if NET_2_0
166 [Test]
167 [ExpectedException (typeof (ArgumentException))]
168 public void Equals_Invalid ()
170 // funny one
171 string appdir = Invalid (true);
172 // constructor is ok with an invalid path...
173 ApplicationDirectory ad = new ApplicationDirectory (appdir);
174 // we can copy it...
175 ApplicationDirectory copy = (ApplicationDirectory)ad.Copy ();
176 // we can't get it's hash code
177 Assert.AreEqual (appdir.GetHashCode (), ad.GetHashCode (), "GetHashCode");
178 // we can convert it to string...
179 Assert.IsTrue (ad.ToString ().IndexOf (appdir) > 0, "ToString");
180 // ... but it throws in Equals - with self!
181 Assert.IsTrue (ad.Equals (ad), "Equals(self)");
184 [Test]
185 [ExpectedException (typeof (ArgumentException))]
186 public void ToString_Invalid ()
188 new ApplicationDirectory (Invalid (true)).ToString ();
190 #endif