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;
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;
64 class CustomUserType : Type
66 public override Assembly Assembly
68 get { throw new NotImplementedException (); }
71 public override string AssemblyQualifiedName
73 get { throw new NotImplementedException (); }
76 public override Type BaseType
78 get { throw new NotImplementedException (); }
81 public override string FullName
83 get { throw new NotImplementedException (); }
86 public override Guid GUID
88 get { throw new NotImplementedException (); }
91 protected override TypeAttributes GetAttributeFlagsImpl ()
93 throw new NotImplementedException ();
96 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
98 throw new NotImplementedException ();
101 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
103 throw new NotImplementedException ();
106 public override Type GetElementType ()
108 throw new NotImplementedException ();
111 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
113 throw new NotImplementedException ();
116 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
118 throw new NotImplementedException ();
121 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
123 throw new NotImplementedException ();
126 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
128 throw new NotImplementedException ();
131 public override Type GetInterface (string name, bool ignoreCase)
133 throw new NotImplementedException ();
136 public override Type[] GetInterfaces ()
138 throw new NotImplementedException ();
141 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
143 throw new NotImplementedException ();
146 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
148 throw new NotImplementedException ();
151 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
153 throw new NotImplementedException ();
156 public override Type GetNestedType (string name, BindingFlags bindingAttr)
158 throw new NotImplementedException ();
161 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
163 throw new NotImplementedException ();
166 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
168 throw new NotImplementedException ();
171 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
173 throw new NotImplementedException ();
176 protected override bool HasElementTypeImpl ()
178 throw new NotImplementedException ();
181 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
183 throw new NotImplementedException ();
186 protected override bool IsArrayImpl ()
188 throw new NotImplementedException ();
191 protected override bool IsByRefImpl ()
193 throw new NotImplementedException ();
196 protected override bool IsCOMObjectImpl ()
198 throw new NotImplementedException ();
201 protected override bool IsPointerImpl ()
203 throw new NotImplementedException ();
206 protected override bool IsPrimitiveImpl ()
208 throw new NotImplementedException ();
211 public override Module Module
213 get { throw new NotImplementedException (); }
216 public override string Namespace
218 get { throw new NotImplementedException (); }
221 public override Type UnderlyingSystemType
228 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
230 throw new NotImplementedException ();
233 public override object[] GetCustomAttributes (bool inherit)
235 throw new NotImplementedException ();
238 public override bool IsDefined (Type attributeType, bool inherit)
240 throw new NotImplementedException ();
243 public override string Name
245 get { throw new NotImplementedException (); }
251 public class ActivatorTest {
253 private string testLocation = typeof (ActivatorTest).Assembly.Location;
256 public void CreateInstance_Type()
258 COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));
259 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
263 [ExpectedException (typeof (ArgumentNullException))]
264 public void CreateInstance_TypeNull ()
266 Activator.CreateInstance ((Type)null);
270 [ExpectedException (typeof (ArgumentException))]
271 public void CreateInstance_CustomType ()
273 Activator.CreateInstance (new CustomUserType ());
277 public void CreateInstance_StringString ()
279 ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");
280 COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
282 Assert.AreEqual (2, objCOMTest.Id, "#A03");
286 [ExpectedException (typeof (ArgumentNullException))]
287 public void CreateInstance_StringNull ()
289 Activator.CreateInstance ((string)null, null);
293 [ExpectedException (typeof (TypeLoadException))]
294 public void CreateInstance_StringTypeNameDoesNotExists ()
296 Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");
300 public void CreateInstance_TypeBool ()
302 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
303 Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
307 public void CreateInstance_TypeObjectArray ()
309 object[] objArray = new object[1] { 7 };
310 COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);
311 Assert.AreEqual (7, objCOMTest.Id, "#A05");
316 [ExpectedException (typeof (MissingMethodException))]
317 public void CreateInstance_TypeBuilder ()
319 Type tb = typeof (TypeBuilder); // no public ctor - but why is it documented as NotSupportedException ?
320 ConstructorInfo[] ctors = tb.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
321 Activator.CreateInstance (tb, new object [ctors [0].GetParameters ().Length]);
325 [ExpectedException (typeof (NotSupportedException))]
326 public void CreateInstance_TypedReference ()
328 Activator.CreateInstance (typeof (TypedReference), null);
332 [ExpectedException (typeof (NotSupportedException))]
333 public void CreateInstance_ArgIterator ()
335 Activator.CreateInstance (typeof (ArgIterator), null);
340 [ExpectedException (typeof (NotSupportedException))]
341 public void CreateInstance_Void ()
343 Activator.CreateInstance (typeof (void), null);
347 [ExpectedException (typeof (NotSupportedException))]
348 public void CreateInstance_RuntimeArgumentHandle ()
350 Activator.CreateInstance (typeof (RuntimeArgumentHandle), null);
354 [ExpectedException (typeof (NotSupportedException))]
355 public void CreateInstance_NotMarshalByReferenceWithActivationAttributes ()
357 Activator.CreateInstance (typeof (object), null, new object[1] { null });
360 // TODO: Implemente the test methods for all the overriden functions using activationAttribute
363 [ExpectedException(typeof(MissingMethodException))]
364 public void CreateInstanceAbstract1 ()
366 Activator.CreateInstance (typeof (Type));
370 [ExpectedException(typeof(MissingMethodException))]
371 public void CreateInstanceAbstract2 ()
373 Activator.CreateInstance (typeof (Type), true);
377 [ExpectedException(typeof(MissingMethodException))]
378 public void CreateInstanceAbstract3 ()
380 Activator.CreateInstance (typeof (Type), null, null);
384 [ExpectedException(typeof(MissingMethodException))]
385 public void CreateInstanceAbstract4()
387 Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);
391 [ExpectedException (typeof (MissingMethodException))]
392 public void CreateInstanceAbstract5 ()
394 Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
398 public void CreateInstance_Nullable ()
400 Assert.AreEqual (5, Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }));
401 Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { 5 }).GetType ());
402 Assert.AreEqual (0, Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }));
403 Assert.AreEqual (typeof (int), Activator.CreateInstance (typeof (Nullable<int>), new object [] { null }).GetType ());
404 Assert.AreEqual (null, Activator.CreateInstance (typeof (Nullable<int>)));
408 [ExpectedException (typeof (ArgumentNullException))]
409 public void GetObject_TypeNull ()
411 Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
416 [ExpectedException (typeof (ArgumentNullException))]
417 public void GetObject_UrlNull ()
419 Activator.GetObject (typeof (COMTest), null);
423 /* This test is now executed in System.Runtime.Remoting unit tests
425 public void GetObject ()
427 // This will provide a COMTest object on tcp://localhost:1234/COMTestUri
428 COMTest objCOMTest = new COMTest (8);
429 TcpChannel chnServer = new TcpChannel (1234);
430 ChannelServices.RegisterChannel (chnServer);
431 RemotingServices.SetObjectUriForMarshal (objCOMTest, "COMTestUri");
432 RemotingServices.Marshal (objCOMTest);
434 // This will get the remoting object
435 object objRem = Activator.GetObject (typeof (COMTest), "tcp://localhost:1234/COMTestUri");
436 Assert.IsNotNull (objRem, "#A07");
437 COMTest remCOMTest = (COMTest) objRem;
438 Assert.AreEqual (8, remCOMTest.Id, "#A08");
440 ChannelServices.UnregisterChannel(chnServer);
443 // TODO: Implemente the test methods for all the overriden function using activationAttribute
446 public void CreateInstanceFrom ()
448 ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
449 Assert.IsNotNull (objHandle, "#A09");
451 // TODO: Implement the test methods for all the overriden function using activationAttribute
456 // note: this only ensure that the ECMA key support unification (more test required, outside corlib, for other keys, like MS final).
457 private const string CorlibPermissionPattern = "System.Security.Permissions.FileDialogPermission, mscorlib, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
458 private const string SystemPermissionPattern = "System.Net.DnsPermission, System, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";
459 private const string fx10version = "1.0.3300.0";
460 private const string fx11version = "1.0.5000.0";
461 private const string fx20version = "2.0.0.0";
463 private static object[] psNone = new object [1] { PermissionState.None };
465 private void Unification (string fullname)
467 Type t = Type.GetType (fullname);
468 IPermission p = (IPermission)Activator.CreateInstance (t, psNone);
469 string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();
470 Assert.IsTrue ((p.ToString ().IndexOf (currentVersion) > 0), currentVersion);
474 public void Unification_FromFx10 ()
476 Unification (String.Format (CorlibPermissionPattern, fx10version));
477 Unification (String.Format (SystemPermissionPattern, fx10version));
481 public void Unification_FromFx11 ()
483 Unification (String.Format (CorlibPermissionPattern, fx11version));
484 Unification (String.Format (SystemPermissionPattern, fx11version));
488 public void Unification_FromFx20 ()
490 Unification (String.Format (CorlibPermissionPattern, fx20version));
491 Unification (String.Format (SystemPermissionPattern, fx20version));
495 public void Unification_FromFx99_Corlib ()
497 Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
501 [Category ("NotWorking")]
502 public void Unification_FromFx99_System ()
504 Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));
508 class foo1<T> : foo2<T, int> {}
510 [Test, ExpectedException (typeof (ArgumentException))]
511 public void GenericType_Open1 ()
513 Activator.CreateInstance (typeof (foo2<,>));
515 [Test, ExpectedException (typeof (ArgumentException))]
516 public void GenericType_Open2 ()
518 Activator.CreateInstance (typeof (foo1<>));
521 public void GenericTypes_Closed ()
523 Assert.IsNotNull (Activator.CreateInstance (typeof (foo1<int>)), "foo1<int>");
524 Assert.IsNotNull (Activator.CreateInstance (typeof (foo2<long, int>)), "foo2<long, int>");
528 public void CreateInstanceCrossDomain ()
530 Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object");
531 Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object", false,
532 BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture,
538 public void CreateInstanceCustomDomain ()
540 // FIXME: below works as a standalone case, but does not as a unit test (causes JIT error).
541 Activator.CreateInstance (AppDomain.CreateDomain ("foo"), "mscorlib.dll", "System.Object", false,
542 BindingFlags.Public | BindingFlags.Instance, null, null, null,
547 public void CreateInstanceCrossDomainNonSerializableArgs ()
549 // I'm not sure why this is possible ...
550 Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.WeakReference", false,
551 BindingFlags.Public | BindingFlags.Instance, null, new object [] {ModuleHandle.EmptyHandle}, null, null, null);
555 [ExpectedException (typeof (NotSupportedException))]
556 public void CreateInstanceNonSerializableAtts ()
558 // even on invalid success it causes different exception though.
559 Activator.CreateInstance ("mscorlib.dll", "System.Object", false,
560 BindingFlags.Public | BindingFlags.Instance, null, null, null,
561 new object [] {ModuleHandle.EmptyHandle}, null);
565 [ExpectedException (typeof (NotSupportedException))]
566 public void CreateInstanceCrossDomainNonSerializableAtts ()
568 // even on invalid success it causes different exception though.
569 Activator.CreateInstance (AppDomain.CurrentDomain, "mscorlib.dll", "System.Object", false,
570 BindingFlags.Public | BindingFlags.Instance, null, null, null,
571 new object [] {ModuleHandle.EmptyHandle}, null);
574 public class ParamsConstructor {
580 public ParamsConstructor (int a, params string [] s)
584 Assert.IsNotNull (s);
599 public void CreateInstanceParamsConstructor ()
601 var a = (ParamsConstructor) Activator.CreateInstance (
602 typeof (ParamsConstructor), new object [] { 42, "foo", "bar" });
604 Assert.AreEqual (42, a.A);
605 Assert.AreEqual ("foo", a.X);
606 Assert.AreEqual ("bar", a.Y);
608 a = (ParamsConstructor) Activator.CreateInstance (
609 typeof (ParamsConstructor), new object [] { 42, "foo" });
611 Assert.AreEqual (42, a.A);
612 Assert.AreEqual ("foo", a.X);
613 Assert.AreEqual (null, a.Y);
615 a = (ParamsConstructor) Activator.CreateInstance (
616 typeof (ParamsConstructor), new object [] { 42 });
618 Assert.AreEqual (42, a.A);
619 Assert.AreEqual (null, a.X);
620 Assert.AreEqual (null, a.Y);
622 var b = Activator.CreateInstance (typeof (SimpleParamsObjectConstructor), 1, 2, 3, 4, 5);
623 Assert.IsNotNull (b);
626 class SimpleParamsObjectConstructor
628 public SimpleParamsObjectConstructor (params object[] parameters)
633 class SimpleParamsConstructor {
638 public SimpleParamsConstructor (params string [] s)
640 Assert.IsNotNull (s);
655 public void CreateInstanceSimpleParamsConstructor ()
657 var a = (SimpleParamsConstructor) Activator.CreateInstance (
658 typeof (SimpleParamsConstructor), new object [] { "foo", "bar" });
660 Assert.AreEqual ("foo", a.X);
661 Assert.AreEqual ("bar", a.Y);
663 a = (SimpleParamsConstructor) Activator.CreateInstance (
664 typeof (SimpleParamsConstructor), new object [0]);
666 Assert.AreEqual (null, a.X);
667 Assert.AreEqual (null, a.Y);
670 class ParamsConstructorWithObjectConversion
672 public ParamsConstructorWithObjectConversion (params int[] x)
678 public void CreateInstanceParamsConstructorWithObjectConversion ()
680 var a = Activator.CreateInstance (typeof(ParamsConstructorWithObjectConversion), new object[] { (object) 2 });
681 Assert.IsNotNull (a);