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;
\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;
23 using NUnit.Framework;
25 // The class in this namespace is used by the main test class
26 namespace MonoTests.System.ActivatorTestInternal {
28 // We need a COM class to test the Activator class
30 public class COMTest : MarshalByRefObject {
33 public bool constructorFlag = false;
40 public COMTest (int id)
45 // This property is visible
54 public class NonCOMTest : COMTest {
58 namespace MonoTests.System {
60 using MonoTests.System.ActivatorTestInternal;
63 public class ActivatorTest {
65 private string corlibLocation = typeof (string).Assembly.Location;
66 private string testLocation = typeof (ActivatorTest).Assembly.Location;
69 public void CreateInstance_Type()
71 COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));
72 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
76 [ExpectedException (typeof (ArgumentNullException))]
77 public void CreateInstance_TypeNull ()
79 Activator.CreateInstance ((Type)null);
83 public void CreateInstance_StringString ()
85 ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");
86 COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
88 Assert.AreEqual (2, objCOMTest.Id, "#A03");
92 [ExpectedException (typeof (ArgumentNullException))]
93 public void CreateInstance_StringNull ()
95 Activator.CreateInstance ((string)null, null);
99 [ExpectedException (typeof (TypeLoadException))]
100 public void CreateInstance_StringTypeNameDoesNotExists ()
102 Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");
106 public void CreateInstance_TypeBool ()
108 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
109 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
113 public void CreateInstance_TypeObjectArray ()
115 object[] objArray = new object[1] { 7 };
116 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);
117 Assert.AreEqual (7, objCOMTest.Id, "#A05");
121 [ExpectedException (typeof (MissingMethodException))]
\r
122 public void CreateInstance_TypeBuilder ()
\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
130 [ExpectedException (typeof (NotSupportedException))]
\r
131 public void CreateInstance_TypedReference ()
\r
133 Activator.CreateInstance (typeof (TypedReference), null);
\r
137 [ExpectedException (typeof (NotSupportedException))]
\r
138 public void CreateInstance_ArgIterator ()
\r
140 Activator.CreateInstance (typeof (ArgIterator), null);
\r
144 [ExpectedException (typeof (NotSupportedException))]
\r
145 public void CreateInstance_Void ()
\r
147 Activator.CreateInstance (typeof (void), null);
\r
151 [ExpectedException (typeof (NotSupportedException))]
\r
152 public void CreateInstance_RuntimeArgumentHandle ()
\r
154 Activator.CreateInstance (typeof (RuntimeArgumentHandle), null);
\r
158 [ExpectedException (typeof (NotSupportedException))]
\r
159 public void CreateInstance_NotMarshalByReferenceWithActivationAttributes ()
\r
161 Activator.CreateInstance (typeof (object), null, new object[1] { null });
\r
164 // TODO: Implemente the test methods for all the overriden functions using activationAttribute
168 [ExpectedException(typeof(MissingMethodException))]
170 [ExpectedException(typeof(MemberAccessException))]
172 public void CreateInstanceAbstract1 ()
174 Activator.CreateInstance (typeof (Type));
179 [ExpectedException(typeof(MissingMethodException))]
181 [ExpectedException(typeof(MemberAccessException))]
183 public void CreateInstanceAbstract2 ()
185 Activator.CreateInstance (typeof (Type), true);
189 [ExpectedException(typeof(MissingMethodException))]
190 public void CreateInstanceAbstract3 ()
192 Activator.CreateInstance (typeof (Type), null, null);
196 [ExpectedException(typeof(MissingMethodException))]
197 public void CreateInstanceAbstract4()
199 Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
204 [ExpectedException (typeof (MissingMethodException))]
206 [ExpectedException (typeof (MemberAccessException))]
208 public void CreateInstanceAbstract5 ()
210 Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
215 public void CreateInstance_Nullable ()
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>)));
226 [ExpectedException (typeof (ArgumentNullException))]
227 public void GetObject_TypeNull ()
229 Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
233 [ExpectedException (typeof (ArgumentNullException))]
234 public void GetObject_UrlNull ()
236 Activator.GetObject (typeof (COMTest), null);
239 /* This test is now executed in System.Runtime.Remoting unit tests
241 public void GetObject ()
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);
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");
256 ChannelServices.UnregisterChannel(chnServer);
259 // TODO: Implemente the test methods for all the overriden function using activationAttribute
262 public void CreateInstanceFrom ()
264 ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
265 Assert.IsNotNull (objHandle, "#A09");
267 // TODO: Implement the test methods for all the overriden function using activationAttribute
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";
277 private static object[] psNone = new object [1] { PermissionState.None };
279 private void Unification (string fullname)
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);
288 public void Unification_FromFx10 ()
290 Unification (String.Format (CorlibPermissionPattern, fx10version));
291 Unification (String.Format (SystemPermissionPattern, fx10version));
295 public void Unification_FromFx11 ()
297 Unification (String.Format (CorlibPermissionPattern, fx11version));
298 Unification (String.Format (SystemPermissionPattern, fx11version));
302 public void Unification_FromFx20 ()
304 Unification (String.Format (CorlibPermissionPattern, fx20version));
305 Unification (String.Format (SystemPermissionPattern, fx20version));
309 public void Unification_FromFx99_Corlib ()
311 Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
313 Unification (String.Format (SystemPermissionPattern, "9.99.999.9999"));
319 [Category ("NotWorking")]
320 public void Unification_FromFx99_System ()
322 Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));