Mark tests as not working under TARGET_JVM
[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                 [Category ("TargetJvmNotWorking")]
190                 public void CreateInstanceAbstract2 () 
191                 {
192                         Activator.CreateInstance (typeof (Type), true);
193                 }
194
195                 [Test]
196                 [ExpectedException(typeof(MissingMethodException))]
197                 public void CreateInstanceAbstract3 () 
198                 {
199                         Activator.CreateInstance (typeof (Type), null, null);
200                 }
201
202                 [Test]
203                 [ExpectedException(typeof(MissingMethodException))]
204                 public void CreateInstanceAbstract4() 
205                 {
206                         Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
207                 }
208
209                 [Test]
210 #if NET_2_0
211                 [ExpectedException (typeof (MissingMethodException))]
212 #else
213                 [ExpectedException (typeof (MemberAccessException))]
214 #endif
215                 [Category ("TargetJvmNotWorking")]
216                 public void CreateInstanceAbstract5 () 
217                 {
218                         Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
219                 }
220
221 #if NET_2_0
222                 [Test]
223                 [Category ("TargetJvmNotWorking")]
224                 public void CreateInstance_Nullable ()
225                 {
226                         Assert.AreEqual (5, Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }));
227                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }).GetType ());
228                         Assert.AreEqual (0, Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }));
229                         Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }).GetType ());
230                         Assert.AreEqual (null, Activator.CreateInstance (typeof (Nullable<int>)));
231                 }
232 #endif
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentNullException))]
236                 public void GetObject_TypeNull ()
237                 {
238                         Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
239                 }
240
241                 [Test]
242                 [ExpectedException (typeof (ArgumentNullException))]
243                 [Category ("TargetJvmNotWorking")]
244                 public void GetObject_UrlNull ()
245                 {
246                         Activator.GetObject (typeof (COMTest), null);
247                 }
248
249 /* This test is now executed in System.Runtime.Remoting unit tests 
250                 [Test]
251                 public void GetObject ()
252                 {
253                         // This will provide a COMTest object on  tcp://localhost:1234/COMTestUri
254                         COMTest objCOMTest = new COMTest (8);
255                         TcpChannel chnServer = new TcpChannel (1234);
256                         ChannelServices.RegisterChannel (chnServer);
257                         RemotingServices.SetObjectUriForMarshal (objCOMTest, "COMTestUri");
258                         RemotingServices.Marshal (objCOMTest);
259
260                         // This will get the remoting object
261                         object objRem = Activator.GetObject (typeof (COMTest), "tcp://localhost:1234/COMTestUri");
262                         Assert.IsNotNull (objRem, "#A07");
263                         COMTest remCOMTest = (COMTest) objRem;
264                         Assert.AreEqual (8, remCOMTest.Id, "#A08");
265
266                         ChannelServices.UnregisterChannel(chnServer);
267                 }
268 */
269                 // TODO: Implemente the test methods for all the overriden function using activationAttribute
270
271                 [Test]
272                 public void CreateInstanceFrom ()
273                 {
274                         ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
275                         Assert.IsNotNull (objHandle, "#A09");
276                         objHandle.Unwrap ();
277                         // TODO: Implement the test methods for all the overriden function using activationAttribute
278                 }
279
280                 // note: this only ensure that the ECMA key support unification (more test required, outside corlib, for other keys, like MS final).
281                 private const string CorlibPermissionPattern = "System.Security.Permissions.FileDialogPermission, mscorlib, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
282                 private const string SystemPermissionPattern = "System.Net.DnsPermission, System, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
283                 private const string fx10version = "1.0.3300.0";
284                 private const string fx11version = "1.0.5000.0";
285                 private const string fx20version = "2.0.0.0";
286
287                 private static object[] psNone = new object [1] { PermissionState.None };
288
289                 private void Unification (string fullname)
290                 {
291                         Type t = Type.GetType (fullname);
292                         IPermission p = (IPermission)Activator.CreateInstance (t, psNone);
293                         string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
294                         Assert.IsTrue ((p.ToString ().IndexOf (currentVersion) > 0), currentVersion);
295                 }
296
297                 [Test]
298                 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
299                 public void Unification_FromFx10 ()
300                 {
301                         Unification (String.Format (CorlibPermissionPattern, fx10version));
302                         Unification (String.Format (SystemPermissionPattern, fx10version));
303                 }
304
305                 [Test]
306                 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
307                 public void Unification_FromFx11 ()
308                 {
309                         Unification (String.Format (CorlibPermissionPattern, fx11version));
310                         Unification (String.Format (SystemPermissionPattern, fx11version));
311                 }
312
313                 [Test]
314                 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
315                 public void Unification_FromFx20 ()
316                 {
317                         Unification (String.Format (CorlibPermissionPattern, fx20version));
318                         Unification (String.Format (SystemPermissionPattern, fx20version));
319                 }
320
321                 [Test]
322                 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
323                 public void Unification_FromFx99_Corlib ()
324                 {
325                         Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
326 #if ONLY_1_1
327                         Unification (String.Format (SystemPermissionPattern, "9.99.999.9999"));
328 #endif
329                 }
330
331 #if NET_2_0
332                 [Test]
333                 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
334                 [Category ("NotWorking")]
335                 public void Unification_FromFx99_System ()
336                 {
337                         Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));
338                 }
339
340                 class foo2<T, U> {}
341                 class foo1<T> : foo2<T, int> {}
342
343                 [Test, ExpectedException (typeof (ArgumentException))]
344                 public void GenericType_Open1 ()
345                 {
346                         Activator.CreateInstance (typeof (foo2<,>));
347                 }
348                 [Test, ExpectedException (typeof (ArgumentException))]
349                 public void GenericType_Open2 ()
350                 {
351                         Activator.CreateInstance (typeof (foo1<>));
352                 }
353                 [Test]
354                 public void GenericTypes_Closed ()
355                 {
356                         Assert.IsNotNull (Activator.CreateInstance (typeof (foo1<int>)), "foo1<int>");
357                         Assert.IsNotNull (Activator.CreateInstance (typeof (foo2<long, int>)), "foo2<long, int>");
358                 }
359 #endif
360         }
361 }