[corlib] Remove multiple appdomain support (AppDomain.CreateDomain, etc) from tvOS...
[mono.git] / mcs / class / corlib / Test / System / AppDomainSetupTest.cs
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                                 int os = (int)Environment.OSVersion.Platform;
27                                 return (os != 4);
28                         }
29                 }
30
31                 [Test]
32                 public void ConfigurationFile_Relative_ApplicationBase ()
33                 {
34                         string fileName = "blar.config";
35                         AppDomainSetup setup = new AppDomainSetup();
36                         string dir = "app_base";
37                         setup.ApplicationBase = dir;
38                         setup.ConfigurationFile = fileName;
39                         string baseDir = Path.GetFullPath(dir);
40                         string configFile = Path.Combine(baseDir, fileName);
41                         Assert.AreEqual(configFile, setup.ConfigurationFile, "Check relative to ApplicationBase");
42                 }
43
44                 [Test]
45                 public void ConfigurationFile_Null ()
46                 {
47                         AppDomainSetup setup = new AppDomainSetup();
48                         Assert.IsNull(setup.ConfigurationFile);
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (MemberAccessException))] // The ApplicationBase must be set before retrieving this property
53                 public void ConfigurationFile_Relative_NoApplicationBase ()
54                 {
55                         AppDomainSetup setup = new AppDomainSetup();
56                         setup.ConfigurationFile = "blar.config";
57                         string configFile = setup.ConfigurationFile;
58                         if (configFile == null) {
59                                 // avoid compiler warning
60                         }
61                 }
62
63                 [Test]
64                 public void ConfigurationFile_Absolute_NoApplicationBase ()
65                 {
66                         AppDomainSetup setup = new AppDomainSetup();
67                         string configFile = Path.GetFullPath("blar.config");
68                         setup.ConfigurationFile = configFile;
69                         Assert.AreEqual(configFile, setup.ConfigurationFile);
70                 }
71
72                 [Test]
73                 public void ApplicationBase1 ()
74                 {
75                         string expected_path = tmpPath.Replace(@"\", @"/");
76                         AppDomainSetup setup = new AppDomainSetup ();
77                         string fileUri = "file://" + expected_path;
78                         setup.ApplicationBase = fileUri;
79                         // with MS 1.1 SP1 the expected_path starts with "//" but this make
80                         // sense only under Windows (i.e. reversed \\ for local files)
81                         if (RunningOnWindows)
82                                 expected_path = "//" + expected_path;
83                         try {
84                                 // under 2.0 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 Windows
90                                 if (!RunningOnWindows)
91                                         throw;
92                         }
93                 }
94
95                 [Test]
96                 public void ApplicationBase2 ()
97                 {
98                         AppDomainSetup setup = new AppDomainSetup ();
99                         setup.ApplicationBase = curDir;
100                         Assert.AreEqual (curDir, setup.ApplicationBase);
101                 }
102
103                 [Test]
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]
113                 public void ApplicationBase4 ()
114                 {
115                         AppDomainSetup setup = new AppDomainSetup ();
116                         setup.ApplicationBase = "lala:la";
117                         try {
118                                 // under 2.0 the NotSupportedException is throw when getting 
119                                 // (and not setting) the ApplicationBase property
120                                 Assert.AreEqual (Path.GetFullPath ("lala:la"), setup.ApplicationBase);
121                         }
122                         catch (NotSupportedException) {
123                                 // however the path is invalid only on Windows
124                                 // (same exceptions as Path.GetFullPath)
125                                 if (!RunningOnWindows)
126                                         throw;
127                         }
128                 }
129
130                 [Test]
131                 public void ApplicationBase5 ()
132                 {
133                         // This is failing because of (probably) a windows-ism, so don't worry
134                         AppDomainSetup setup = new AppDomainSetup ();
135                         setup.ApplicationBase = "file:///lala:la";
136                         try {
137                                 // under 2.0 the NotSupportedException is throw when getting 
138                                 // (and not setting) the ApplicationBase property
139                                 Assert.AreEqual ("/lala:la", setup.ApplicationBase);
140                         }
141                         catch (NotSupportedException) {
142                                 // however the path is invalid only on Windows
143                                 // (same exceptions as Path.GetFullPath)
144                                 if (!RunningOnWindows)
145                                         throw;
146                         }
147                 }
148
149                 [Test]
150                 public void ApplicationBase6 ()
151                 {
152                         AppDomainSetup setup = new AppDomainSetup ();
153                         setup.ApplicationBase = "la?lala";
154                         // paths containing "?" are *always* bad on Windows
155                         // but are legal for linux so we return a full path
156                         if (RunningOnWindows) {
157                                 try {
158                                         // ArgumentException is throw when getting 
159                                         // (and not setting) the ApplicationBase property
160                                         Assert.Fail ("setup.ApplicationBase returned :" + setup.ApplicationBase);
161                                 }
162                                 catch (ArgumentException) {
163                                 }
164                                 catch (Exception e) {
165                                         Assert.Fail ("Unexpected exception: " + e.ToString ());
166                                 }
167                         } else {
168                                 Assert.AreEqual (Path.GetFullPath ("la?lala"), setup.ApplicationBase);
169                         }
170                 }
171
172 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
173                 [Test]
174 #if MOBILE
175                 [Category ("NotWorking")]
176 #endif
177                 public void AppDomainInitializer1 ()
178                 {
179                         AppDomainSetup s = new AppDomainSetup ();
180                         s.AppDomainInitializer = AppDomainInitialized1;
181                         s.AppDomainInitializerArguments = new string [] {"A", "B"};
182                         AppDomain domain = AppDomain.CreateDomain ("MyDomain", null, s);
183
184                         object data = domain.GetData ("Initialized");
185                         Assert.IsNotNull (data);
186                         Assert.IsTrue ((bool) data);
187                 }
188 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
189
190                 static void AppDomainInitialized1 (string [] args)
191                 {
192                         bool initialized = true;
193                         initialized &= args [0] == "A";
194                         initialized &= args [1] == "B";
195                         initialized &= AppDomain.CurrentDomain.FriendlyName == "MyDomain";
196                         
197                         AppDomain.CurrentDomain.SetData ("Initialized", initialized);
198                 }
199
200                 public void InstanceInitializer (string [] args)
201                 {
202                 }
203
204 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
205                 [Test]
206 #if MOBILE
207                 [Category ("NotWorking")]
208 #else
209                 [ExpectedException (typeof (ArgumentException))]
210 #endif
211                 public void AppDomainInitializerNonStaticMethod ()
212                 {
213                         AppDomainSetup s = new AppDomainSetup ();
214                         s.AppDomainInitializer = InstanceInitializer;
215                         AppDomain.CreateDomain ("MyDomain", null, s);
216                 }
217 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
218         }
219 }