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