2006-06-04 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Test / System / ActivatorTest.cs
1 //
2 // ActivatorTest.cs - NUnit Test Cases for System.Activator
3 //
4 // Authors:
5 //      Nick Drochak <ndrochak@gol.com>
6 //      Gert Driesen <drieseng@users.sourceforge.net>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.IO;
15 using System.Reflection;\r
16 using System.Reflection.Emit;\r
17 using System.Runtime.InteropServices;
18 using System.Runtime.Remoting;
19 using System.Runtime.Remoting.Channels;
20 using System.Security;
21 using System.Security.Permissions;
22
23 using NUnit.Framework;
24
25 // The class in this namespace is used by the main test class
26 namespace MonoTests.System.ActivatorTestInternal {
27
28         // We need a COM class to test the Activator class
29         [ComVisible (true)]
30         public class COMTest : MarshalByRefObject {
31
32                 private int id;
33                 public bool constructorFlag = false;
34
35                 public COMTest ()
36                 {
37                         id = 0;
38                 }
39
40                 public COMTest (int id)
41                 {
42                         this.id = id;
43                 }
44
45                 // This property is visible
46                 [ComVisible (true)]
47                 public int Id {
48                         get { return id; }
49                         set { id = value; }
50                 }
51         }
52
53         [ComVisible (false)]
54         public class NonCOMTest : COMTest {
55         }
56 }
57
58 namespace MonoTests.System {
59
60         using MonoTests.System.ActivatorTestInternal;
61
62         [TestFixture]
63         public class ActivatorTest {
64
65                 private string corlibLocation = typeof (string).Assembly.Location;
66                 private string testLocation = typeof (ActivatorTest).Assembly.Location;
67
68                 [Test]
69                 public void CreateInstance_Type()
70                 {
71                         COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));
72                         Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (ArgumentNullException))]
77                 public void CreateInstance_TypeNull ()
78                 {
79                         Activator.CreateInstance ((Type)null);
80                 }
81
82                 [Test]
83                 public void CreateInstance_StringString ()
84                 {
85                         ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");
86                         COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
87                         objCOMTest.Id = 2;
88                         Assert.AreEqual (2, objCOMTest.Id, "#A03");
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void CreateInstance_StringNull ()
94                 {
95                         Activator.CreateInstance ((string)null, null);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (TypeLoadException))]
100                 public void CreateInstance_StringTypeNameDoesNotExists ()
101                 {
102                         Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");
103                 }
104
105                 [Test]
106                 public void CreateInstance_TypeBool ()
107                 {
108                         COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
109                         Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
110                 }
111
112                 [Test]
113                 public void CreateInstance_TypeObjectArray ()
114                 {
115                         object[] objArray = new object[1] { 7 };
116                         COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);
117                         Assert.AreEqual (7, objCOMTest.Id, "#A05");
118                 }
119
120                 [Test]\r
121                 [ExpectedException (typeof (MissingMethodException))]\r
122                 public void CreateInstance_TypeBuilder ()\r
123                 {\r
124                         Type tb = typeof (TypeBuilder); // no public ctor - but why is it documented as NotSupportedException ?\r
125                         ConstructorInfo[] ctors = tb.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);\r
126                         Activator.CreateInstance (tb, new object [ctors [0].GetParameters ().Length]);\r
127                 }\r
128 \r
129                 [Test]\r
130                 [ExpectedException (typeof (NotSupportedException))]\r
131                 public void CreateInstance_TypedReference ()\r
132                 {\r
133                         Activator.CreateInstance (typeof (TypedReference), null);\r
134                 }\r
135 \r
136                 [Test]\r
137                 [ExpectedException (typeof (NotSupportedException))]\r
138                 public void CreateInstance_ArgIterator ()\r
139                 {\r
140                         Activator.CreateInstance (typeof (ArgIterator), null);\r
141                 }\r
142 \r
143                 [Test]\r
144                 [ExpectedException (typeof (NotSupportedException))]\r
145                 public void CreateInstance_Void ()\r
146                 {\r
147                         Activator.CreateInstance (typeof (void), null);\r
148                 }\r
149 \r
150                 [Test]\r
151                 [ExpectedException (typeof (NotSupportedException))]\r
152                 public void CreateInstance_RuntimeArgumentHandle ()\r
153                 {\r
154                         Activator.CreateInstance (typeof (RuntimeArgumentHandle), null);\r
155                 }\r
156 \r
157                 [Test]\r
158                 [ExpectedException (typeof (NotSupportedException))]\r
159                 public void CreateInstance_NotMarshalByReferenceWithActivationAttributes ()\r
160                 {\r
161                         Activator.CreateInstance (typeof (object), null, new object[1] { null });\r
162                 }\r
163 \r
164                 // TODO: Implemente the test methods for all the overriden functions using activationAttribute
165
166                 [Test]
167 #if NET_2_0
168                 [ExpectedException(typeof(MissingMethodException))]
169 #else
170                 [ExpectedException(typeof(MemberAccessException))]
171 #endif
172                 public void CreateInstanceAbstract1 () 
173                 {
174                         Activator.CreateInstance (typeof (Type));
175                 }
176
177                 [Test]
178 #if NET_2_0
179                 [ExpectedException(typeof(MissingMethodException))]
180 #else
181                 [ExpectedException(typeof(MemberAccessException))]
182 #endif
183                 public void CreateInstanceAbstract2 () 
184                 {
185                         Activator.CreateInstance (typeof (Type), true);
186                 }
187
188                 [Test]
189                 [ExpectedException(typeof(MissingMethodException))]
190                 public void CreateInstanceAbstract3 () 
191                 {
192                         Activator.CreateInstance (typeof (Type), null, null);
193                 }
194
195                 [Test]
196                 [ExpectedException(typeof(MissingMethodException))]
197                 public void CreateInstanceAbstract4() 
198                 {
199                         Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
200                 }
201
202                 [Test]
203 #if NET_2_0
204                 [ExpectedException (typeof (MissingMethodException))]
205 #else
206                 [ExpectedException (typeof (MemberAccessException))]
207 #endif
208                 public void CreateInstanceAbstract5 () 
209                 {
210                         Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
211                 }
212
213 #if NET_2_0
214                 [Test]
215                 public void CreateInstance_Nullable ()
216                 {
217                         Assert.AreEqual (5, Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }));
218                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }).GetType ());
219                         Assert.AreEqual (0, Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }));
220                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }).GetType ());
221                         Assert.AreEqual (null, Activator.CreateInstance (typeof (Nullable<int>)));
222                 }
223 #endif
224
225                 [Test]
226                 [ExpectedException (typeof (ArgumentNullException))]
227                 public void GetObject_TypeNull ()
228                 {
229                         Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (ArgumentNullException))]
234                 public void GetObject_UrlNull ()
235                 {
236                         Activator.GetObject (typeof (COMTest), null);
237                 }
238
239 /* This test is now executed in System.Runtime.Remoting unit tests 
240                 [Test]
241                 public void GetObject ()
242                 {
243                         // This will provide a COMTest object on  tcp://localhost:1234/COMTestUri
244                         COMTest objCOMTest = new COMTest (8);
245                         TcpChannel chnServer = new TcpChannel (1234);
246                         ChannelServices.RegisterChannel (chnServer);
247                         RemotingServices.SetObjectUriForMarshal (objCOMTest, "COMTestUri");
248                         RemotingServices.Marshal (objCOMTest);
249
250                         // This will get the remoting object
251                         object objRem = Activator.GetObject (typeof (COMTest), "tcp://localhost:1234/COMTestUri");
252                         Assert.IsNotNull (objRem, "#A07");
253                         COMTest remCOMTest = (COMTest) objRem;
254                         Assert.AreEqual (8, remCOMTest.Id, "#A08");
255
256                         ChannelServices.UnregisterChannel(chnServer);
257                 }
258 */
259                 // TODO: Implemente the test methods for all the overriden function using activationAttribute
260
261                 [Test]
262                 public void CreateInstanceFrom ()
263                 {
264                         ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
265                         Assert.IsNotNull (objHandle, "#A09");
266                         objHandle.Unwrap ();
267                         // TODO: Implement the test methods for all the overriden function using activationAttribute
268                 }
269
270                 // note: this only ensure that the ECMA key support unification (more test required, outside corlib, for other keys, like MS final).
271                 private const string CorlibPermissionPattern = "System.Security.Permissions.FileDialogPermission, mscorlib, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
272                 private const string SystemPermissionPattern = "System.Net.DnsPermission, System, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
273                 private const string fx10version = "1.0.3300.0";
274                 private const string fx11version = "1.0.5000.0";
275                 private const string fx20version = "2.0.0.0";
276
277                 private static object[] psNone = new object [1] { PermissionState.None };
278
279                 private void Unification (string fullname)
280                 {
281                         Type t = Type.GetType (fullname);
282                         IPermission p = (IPermission)Activator.CreateInstance (t, psNone);
283                         string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
284                         Assert.IsTrue ((p.ToString ().IndexOf (currentVersion) > 0), currentVersion);
285                 }
286
287                 [Test]
288                 public void Unification_FromFx10 ()
289                 {
290                         Unification (String.Format (CorlibPermissionPattern, fx10version));
291                         Unification (String.Format (SystemPermissionPattern, fx10version));
292                 }
293
294                 [Test]
295                 public void Unification_FromFx11 ()
296                 {
297                         Unification (String.Format (CorlibPermissionPattern, fx11version));
298                         Unification (String.Format (SystemPermissionPattern, fx11version));
299                 }
300
301                 [Test]
302                 public void Unification_FromFx20 ()
303                 {
304                         Unification (String.Format (CorlibPermissionPattern, fx20version));
305                         Unification (String.Format (SystemPermissionPattern, fx20version));
306                 }
307
308                 [Test]
309                 public void Unification_FromFx99_Corlib ()
310                 {
311                         Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
312 #if ONLY_1_1
313                         Unification (String.Format (SystemPermissionPattern, "9.99.999.9999"));
314 #endif
315                 }
316
317 #if NET_2_0
318                 [Test]
319                 [Category ("NotWorking")]
320                 public void Unification_FromFx99_System ()
321                 {
322                         Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));
323                 }
324 #endif
325         }
326 }