1 //
2 // AppDomainSetupTest.cs - NUnit Test Cases for the System.AppDomainSetup class
3 //
4 // Authors:
5 // 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 
12 using NUnit.Framework;
13 using System;
14 using System.IO;
15 
16 namespace MonoTests.System
17 {
18 	[TestFixture]
19 	public class AppDomainSetupTest {
20 
21 		static readonly string tmpPath = Path.GetTempPath ();
22 		static readonly string curDir = Directory.GetCurrentDirectory ();
23 
24 		private bool RunningOnWindows {
25 			get {
26 				return Path.DirectorySeparatorChar == '\\';
27 			}
28 		}
29 		private bool RunningOnMono {
30 			get {
31 				return (Type.GetType ("System.MonoType", false) != null);
32 			}
33 		}
34 
35 		[Test]
ConfigurationFile_Relative_ApplicationBase()36 		public void ConfigurationFile_Relative_ApplicationBase ()
37 		{
38 			string fileName = "blar.config";
39 			AppDomainSetup setup = new AppDomainSetup();
40 			string dir = "app_base";
41 			setup.ApplicationBase = dir;
42 			setup.ConfigurationFile = fileName;
43 			string baseDir = Path.GetFullPath(dir);
44 			string configFile = Path.Combine(baseDir, fileName);
45 			Assert.AreEqual(configFile, setup.ConfigurationFile, "Check relative to ApplicationBase");
46 		}
47 
48 		[Test]
ConfigurationFile_Null()49 		public void ConfigurationFile_Null ()
50 		{
51 			AppDomainSetup setup = new AppDomainSetup();
52 			Assert.IsNull(setup.ConfigurationFile);
53 		}
54 
55 		[Test]
56 		[ExpectedException (typeof (MemberAccessException))] // The ApplicationBase must be set before retrieving this property
ConfigurationFile_Relative_NoApplicationBase()57 		public void ConfigurationFile_Relative_NoApplicationBase ()
58 		{
59 			AppDomainSetup setup = new AppDomainSetup();
60 			setup.ConfigurationFile = "blar.config";
61 			string configFile = setup.ConfigurationFile;
62 			if (configFile == null) {
63 				// avoid compiler warning
64 			}
65 		}
66 
67 		[Test]
ConfigurationFile_Absolute_NoApplicationBase()68 		public void ConfigurationFile_Absolute_NoApplicationBase ()
69 		{
70 			AppDomainSetup setup = new AppDomainSetup();
71 			string configFile = Path.GetFullPath("blar.config");
72 			setup.ConfigurationFile = configFile;
73 			Assert.AreEqual(configFile, setup.ConfigurationFile);
74 		}
75 
76 		[Test]
ApplicationBase1()77 		public void ApplicationBase1 ()
78 		{
79 			string expected_path = tmpPath;
80 			AppDomainSetup setup = new AppDomainSetup ();
81 			string fileUri = "file://" + tmpPath.Replace(@"\", @"/");
82 			setup.ApplicationBase = fileUri;
83 			try {
84 				// under .NET the NotSupportedException is throw when getting
85 				// (and not setting) the ApplicationBase property
86 				Assert.AreEqual (expected_path, setup.ApplicationBase);
87 			}
88 			catch (NotSupportedException) {
89 				// however the path is invalid only on .NET
90 				if (RunningOnMono)
91 					throw;
92 			}
93 		}
94 
95 		[Test]
ApplicationBase2()96 		public void ApplicationBase2 ()
97 		{
98 			AppDomainSetup setup = new AppDomainSetup ();
99 			setup.ApplicationBase = curDir;
100 			Assert.AreEqual (curDir, setup.ApplicationBase);
101 		}
102 
103 		[Test]
ApplicationBase3()104 		public void ApplicationBase3 ()
105 		{
106 			AppDomainSetup setup = new AppDomainSetup ();
107 			string expected = Path.Combine (Environment.CurrentDirectory, "lalala");
108 			setup.ApplicationBase = "lalala";
109 			Assert.AreEqual (expected, setup.ApplicationBase);
110 		}
111 
112 		[Test]
ApplicationBase4()113 		public void ApplicationBase4 ()
114 		{
115 			AppDomainSetup setup = new AppDomainSetup ();
116 			setup.ApplicationBase = "lala:la";
117 			if (!RunningOnWindows) {
118 				Assert.AreEqual (Path.GetFullPath ("lala:la"), setup.ApplicationBase);
119 			} else {
120 				// On Windows we expect a NotSupportedException to be thrown because
121 				// of the illegal character (:) in the path
122 				try {
123 					Assert.Fail ("NotSupportedException expected but setup.ApplicationBase returned:" + setup.ApplicationBase);
124 				}
125 				catch (NotSupportedException) {
126 					// Expected
127 				}
128 			}
129 		}
130 
131 		[Test]
ApplicationBase5()132 		public void ApplicationBase5 ()
133 		{
134 			// This is failing because of (probably) a windows-ism, so don't worry
135 			AppDomainSetup setup = new AppDomainSetup ();
136 			setup.ApplicationBase = "file:///lala:la";
137 			string expected = "/lala:la";
138 			if (!RunningOnWindows) {
139 				Assert.AreEqual (expected, setup.ApplicationBase);
140 			} else {
141 				// On Windows we expect a NotSupportedException to be thrown because
142 				// of the illegal character (:) in the path
143 				try {
144 					Assert.Fail ("NotSupportedException expected but setup.ApplicationBase returned:" + setup.ApplicationBase);
145 				}
146 				catch (NotSupportedException) {
147 					// Expected
148 				}
149 			}
150 		}
151 
152 		[Test]
ApplicationBase6()153 		public void ApplicationBase6 ()
154 		{
155 			AppDomainSetup setup = new AppDomainSetup ();
156 			setup.ApplicationBase = "la?lala";
157 			// paths containing "?" are *always* bad on Windows
158 			// but are legal for linux so we return a full path
159 			if (!RunningOnWindows) {
160 				Assert.AreEqual (Path.GetFullPath ("la?lala"), setup.ApplicationBase);
161 			} else {
162 				// On Windows we expect a ArgumentException to be thrown because
163 				// of the illegal character (?) in the path
164 				try {
165 					Assert.Fail ("ArgumentException expected but setup.ApplicationBase returned:" + setup.ApplicationBase);
166 				}
167 				catch (ArgumentException) {
168 					// Expected
169 				}
170 			}
171 		}
172 
173 		[Test]
ApplicationBase7()174 		public void ApplicationBase7 ()
175 		{
176 			if (RunningOnWindows) {
177 				// Extended paths are Windows only
178 				AppDomainSetup setup = new AppDomainSetup ();
179 				string expected = @"\\?\" + curDir;
180 				setup.ApplicationBase = expected;
181 				Assert.AreEqual (expected, setup.ApplicationBase);
182 			}
183 		}
184 
185 		[Test]
ApplicationBase8()186 		public void ApplicationBase8 ()
187 		{
188 			if (RunningOnWindows) {
189 				// Extended paths are Windows only
190 				AppDomainSetup setup = new AppDomainSetup ();
191 				setup.ApplicationBase = @"\\?\C:\lala:la";
192 				try {
193 					Assert.Fail ("NotSupportedException expected but setup.ApplicationBase returned:" + setup.ApplicationBase);
194 				}
195 				catch (NotSupportedException) {
196 					// Expected
197 				}
198 			}
199 		}
200 
201 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
202 		[Test]
203 #if MOBILE
204 		[Category ("NotWorking")]
205 #endif
AppDomainInitializer1()206 		public void AppDomainInitializer1 ()
207 		{
208 			AppDomainSetup s = new AppDomainSetup ();
209 			s.AppDomainInitializer = AppDomainInitialized1;
210 			s.AppDomainInitializerArguments = new string [] {"A", "B"};
211 			AppDomain domain = AppDomain.CreateDomain ("MyDomain", null, s);
212 
213 			object data = domain.GetData ("Initialized");
214 			Assert.IsNotNull (data);
215 			Assert.IsTrue ((bool) data);
216 		}
217 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
218 
AppDomainInitialized1(string [] args)219 		static void AppDomainInitialized1 (string [] args)
220 		{
221 			bool initialized = true;
222 			initialized &= args [0] == "A";
223 			initialized &= args [1] == "B";
224 			initialized &= AppDomain.CurrentDomain.FriendlyName == "MyDomain";
225 
226 			AppDomain.CurrentDomain.SetData ("Initialized", initialized);
227 		}
228 
InstanceInitializer(string [] args)229 		public void InstanceInitializer (string [] args)
230 		{
231 		}
232 
233 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
234 		[Test]
235 #if MOBILE
236 		[Category ("NotWorking")]
237 #else
238 		[ExpectedException (typeof (ArgumentException))]
239 #endif
AppDomainInitializerNonStaticMethod()240 		public void AppDomainInitializerNonStaticMethod ()
241 		{
242 			AppDomainSetup s = new AppDomainSetup ();
243 			s.AppDomainInitializer = InstanceInitializer;
244 			AppDomain.CreateDomain ("MyDomain", null, s);
245 		}
246 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
247 	}
248 }
249