2 // ActivatorTest.cs - NUnit Test Cases for System.Activator
5 // Nick Drochak <ndrochak@gol.com>
6 // Gert Driesen <drieseng@users.sourceforge.net>
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 using System.Globalization;
15 using System.Reflection;
16 #if !TARGET_JVM // Reflection.Emit not supported for TARGET_JVM
17 using System.Reflection.Emit;
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;
25 using NUnit.Framework;
27 // The class in this namespace is used by the main test class
28 namespace MonoTests.System.ActivatorTestInternal {
30 // We need a COM class to test the Activator class
32 public class COMTest : MarshalByRefObject {
35 public bool constructorFlag = false;
42 public COMTest (int id)
47 // This property is visible
56 public class NonCOMTest : COMTest {
60 namespace MonoTests.System {
62 using MonoTests.System.ActivatorTestInternal;
65 public class ActivatorTest {
67 private string corlibLocation = typeof (string).Assembly.Location;
68 private string testLocation = typeof (ActivatorTest).Assembly.Location;
71 public void CreateInstance_Type()
73 COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));
74 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
78 [ExpectedException (typeof (ArgumentNullException))]
79 public void CreateInstance_TypeNull ()
81 Activator.CreateInstance ((Type)null);
85 public void CreateInstance_StringString ()
87 ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");
88 COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
90 Assert.AreEqual (2, objCOMTest.Id, "#A03");
94 [ExpectedException (typeof (ArgumentNullException))]
95 public void CreateInstance_StringNull ()
97 Activator.CreateInstance ((string)null, null);
101 [ExpectedException (typeof (TypeLoadException))]
102 public void CreateInstance_StringTypeNameDoesNotExists ()
104 Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");
108 public void CreateInstance_TypeBool ()
110 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
111 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
115 public void CreateInstance_TypeObjectArray ()
117 object[] objArray = new object[1] { 7 };
118 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);
119 Assert.AreEqual (7, objCOMTest.Id, "#A05");
122 #if !TARGET_JVM // Reflection.Emit not supported for TARGET_JVM
124 [ExpectedException (typeof (MissingMethodException))]
125 public void CreateInstance_TypeBuilder ()
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]);
133 [ExpectedException (typeof (NotSupportedException))]
134 public void CreateInstance_TypedReference ()
136 Activator.CreateInstance (typeof (TypedReference), null);
140 [ExpectedException (typeof (NotSupportedException))]
141 public void CreateInstance_ArgIterator ()
143 Activator.CreateInstance (typeof (ArgIterator), null);
148 [ExpectedException (typeof (NotSupportedException))]
149 public void CreateInstance_Void ()
151 Activator.CreateInstance (typeof (void), null);
154 #if !TARGET_JVM // RuntimeArgumentHandle not supported for TARGET_JVM
156 [ExpectedException (typeof (NotSupportedException))]
157 public void CreateInstance_RuntimeArgumentHandle ()
159 Activator.CreateInstance (typeof (RuntimeArgumentHandle), null);
164 [ExpectedException (typeof (NotSupportedException))]
165 public void CreateInstance_NotMarshalByReferenceWithActivationAttributes ()
167 Activator.CreateInstance (typeof (object), null, new object[1] { null });
170 // TODO: Implemente the test methods for all the overriden functions using activationAttribute
174 [ExpectedException(typeof(MissingMethodException))]
176 [ExpectedException(typeof(MemberAccessException))]
178 public void CreateInstanceAbstract1 ()
180 Activator.CreateInstance (typeof (Type));
185 [ExpectedException(typeof(MissingMethodException))]
187 [ExpectedException(typeof(MemberAccessException))]
189 [Category ("TargetJvmNotWorking")]
190 public void CreateInstanceAbstract2 ()
192 Activator.CreateInstance (typeof (Type), true);
196 [ExpectedException(typeof(MissingMethodException))]
197 public void CreateInstanceAbstract3 ()
199 Activator.CreateInstance (typeof (Type), null, null);
203 [ExpectedException(typeof(MissingMethodException))]
204 public void CreateInstanceAbstract4()
206 Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
211 [ExpectedException (typeof (MissingMethodException))]
213 [ExpectedException (typeof (MemberAccessException))]
215 [Category ("TargetJvmNotWorking")]
216 public void CreateInstanceAbstract5 ()
218 Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
223 [Category ("TargetJvmNotWorking")]
224 public void CreateInstance_Nullable ()
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>)));
235 [ExpectedException (typeof (ArgumentNullException))]
236 public void GetObject_TypeNull ()
238 Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
242 [ExpectedException (typeof (ArgumentNullException))]
243 [Category ("TargetJvmNotWorking")]
244 public void GetObject_UrlNull ()
246 Activator.GetObject (typeof (COMTest), null);
249 /* This test is now executed in System.Runtime.Remoting unit tests
251 public void GetObject ()
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);
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");
266 ChannelServices.UnregisterChannel(chnServer);
269 // TODO: Implemente the test methods for all the overriden function using activationAttribute
272 public void CreateInstanceFrom ()
274 ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
275 Assert.IsNotNull (objHandle, "#A09");
277 // TODO: Implement the test methods for all the overriden function using activationAttribute
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";
287 private static object[] psNone = new object [1] { PermissionState.None };
289 private void Unification (string fullname)
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);
298 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
299 public void Unification_FromFx10 ()
301 Unification (String.Format (CorlibPermissionPattern, fx10version));
302 Unification (String.Format (SystemPermissionPattern, fx10version));
306 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
307 public void Unification_FromFx11 ()
309 Unification (String.Format (CorlibPermissionPattern, fx11version));
310 Unification (String.Format (SystemPermissionPattern, fx11version));
314 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
315 public void Unification_FromFx20 ()
317 Unification (String.Format (CorlibPermissionPattern, fx20version));
318 Unification (String.Format (SystemPermissionPattern, fx20version));
322 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
323 public void Unification_FromFx99_Corlib ()
325 Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
327 Unification (String.Format (SystemPermissionPattern, "9.99.999.9999"));
333 [Category ("TargetJvmNotSupported")] // No support under TARGET_JVM for assemlies versioning
334 [Category ("NotWorking")]
335 public void Unification_FromFx99_System ()
337 Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));
341 class foo1<T> : foo2<T, int> {}
343 [Test, ExpectedException (typeof (ArgumentException))]
344 public void GenericType_Open1 ()
346 Activator.CreateInstance (typeof (foo2<,>));
348 [Test, ExpectedException (typeof (ArgumentException))]
349 public void GenericType_Open2 ()
351 Activator.CreateInstance (typeof (foo1<>));
354 public void GenericTypes_Closed ()
356 Assert.IsNotNull (Activator.CreateInstance (typeof (foo1<int>)), "foo1<int>");
357 Assert.IsNotNull (Activator.CreateInstance (typeof (foo2<long, int>)), "foo2<long, int>");