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