-using System;
+//\r
+// ActivatorTest.cs - NUnit Test Cases for System.Activator\r
+//\r
+// Authors:\r
+// Nick Drochak <ndrochak@gol.com>\r
+// Gert Driesen <drieseng@users.sourceforge.net>\r
+// Sebastien Pouliot <sebastien@ximian.com>\r
+//\r
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)\r
+//\r
+\r
+using System;\r
+using System.Globalization;\r
+using System.IO;\r
+using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
-using System.Runtime.Remoting.Channels.Tcp;
+using System.Security;
+using System.Security.Permissions;
+
using NUnit.Framework;
-// The class in this namespace is used by the
-// main test class
-namespace MonoTests.System.ActivatorTestInternal
- {
- // We need a COM class to test the Activator class
- [ComVisible(true)]
-
- public class COMTest: MarshalByRefObject
- {
- public COMTest()
- {
- id = 0;
- }
- // This property is visible
- [ComVisible(true)]
- public int Id
- {
- get { return id; }
- set { id = value; }
- }
-
- public COMTest(int id)
- {
- this.id = id;
- }
-
- private int id;
- public bool constructorFlag = false;
- }
- } // MonoTests.System.ActivatorTestInternal namespace
-
-namespace MonoTests.System
- {
- using MonoTests.System.ActivatorTestInternal;
-
- [TestFixture]
- public class ActivatorTest
- {
- public ActivatorTest()
- {}
-
- [Test]
- [Ignore("Activator.CreateComInstanceForm is not yet implemented")]
- // This test is ignored for the moment because
- // CreateComInstanceFrom() is not implemented yet
- // by the mono Activator class
- public void CreateComInstanceFrom()
- {
- ObjectHandle objHandle = Activator.CreateComInstanceFrom(strAssembly ,
-"COMTest");
- COMTest objCOMTest = (COMTest) objHandle.Unwrap();
- objCOMTest.Id = 10;
- Assertion.AssertEquals("#A01",10,objCOMTest.Id);
- }
-
- [Test]
- // This method tests CreateInstance()
- public void CreateInstance()
- {
- COMTest objCOMTest;
- // object CreateInstance(Type type)
- objCOMTest = (COMTest) Activator.CreateInstance(typeof(COMTest));
- Assertion.AssertEquals("#A02",
-"MonoTests.System.ActivatorTestInternal.COMTest",
-(objCOMTest.GetType()).ToString());
- // ObjectHandle CreateInstance(string, string)
- ObjectHandle objHandle;
- objHandle = Activator.CreateInstance(null ,
-"MonoTests.System.ActivatorTestInternal.COMTest");
- objCOMTest = (COMTest) objHandle.Unwrap();
- objCOMTest.Id = 2;
- Assertion.AssertEquals("#A03", 2, objCOMTest.Id);
- // object CreateInstance(Type, bool)
- objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), false);
- Assertion.AssertEquals("#A04",
-"MonoTests.System.ActivatorTestInternal.COMTest",
-(objCOMTest.GetType()).ToString());
-// // object CreateInstance(Type, object[])
- object[] objArray = new object[1];
- objArray[0] = 7;
- objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), objArray);
- Assertion.AssertEquals("#A05", 7, objCOMTest.Id);
- // Todo: Implemente the test methods for
- // all the overriden functions using activationAttribute
- }
-
- [Test]
+// The class in this namespace is used by the main test class
+namespace MonoTests.System.ActivatorTestInternal {
+
+ // We need a COM class to test the Activator class
+ [ComVisible (true)]
+ public class COMTest : MarshalByRefObject {\r
+\r
+ private int id;\r
+ public bool constructorFlag = false;
+
+ public COMTest ()
+ {
+ id = 0;
+ }\r
+\r
+ public COMTest (int id)\r
+ {\r
+ this.id = id;\r
+ }
+
+ // This property is visible
+ [ComVisible (true)]
+ public int Id {
+ get { return id; }
+ set { id = value; }
+ }
+ }\r
+\r
+ [ComVisible (false)]\r
+ public class NonCOMTest : COMTest {\r
+ }
+}
+
+namespace MonoTests.System {
+
+ using MonoTests.System.ActivatorTestInternal;\r
+\r
+ [TestFixture]\r
+ public class ActivatorTest {\r
+\r
+ private string corlibLocation = typeof (string).Assembly.Location;\r
+ private string testLocation = typeof (ActivatorTest).Assembly.Location;
+
+ [Test]
+ [Ignore ("doesn't even work on MS runtime")]
+ // This test is ignored for the moment because CreateComInstanceFrom() is not
+ // implemented yet by the mono Activator class
+ public void CreateComInstanceFrom ()
+ {
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");
+ COMTest objCOMTest = (COMTest) objHandle.Unwrap ();
+ objCOMTest.Id = 10;\r
+ Assert.AreEqual (10, objCOMTest.Id, "#A01");
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void CreateComInstanceFrom_AssemblyNull ()
+ {
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (null, "MonoTests.System.ActivatorTestInternal.COMTest");
+ }
+
+ [Test]\r
+ [ExpectedException (typeof (ArgumentException))]\r
+ public void CreateComInstanceFrom_AssemblyEmpty ()\r
+ {\r
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (String.Empty, "MonoTests.System.ActivatorTestInternal.COMTest");\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (FileNotFoundException))]\r
+ [Category ("NotWorking")]\r
+ public void CreateComInstanceFrom_AssemblyNotFound ()\r
+ {\r
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (testLocation + "1", "MonoTests.System.ActivatorTestInternal.COMTest");\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (TypeLoadException))]\r
+ [Category ("NotWorking")]\r
+ public void CreateComInstanceFrom_TypeNameNotComVisible ()\r
+ {\r
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.NonCOMTest");\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (TypeLoadException))]\r
+ [Category ("NotWorking")]\r
+ public void CreateComInstanceFrom_TypeNameDoesNotExists ()\r
+ {\r
+ ObjectHandle objHandle = Activator.CreateComInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");\r
+ }\r
+
+ [Test]
+ public void CreateInstance_Type()
+ {
+ COMTest objCOMTest = (COMTest) Activator.CreateInstance (typeof (COMTest));\r
+ Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", (objCOMTest.GetType ()).ToString (), "#A02");
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (ArgumentNullException))]\r
+ public void CreateInstance_TypeNull ()\r
+ {\r
+ Activator.CreateInstance ((Type)null);\r
+ }\r
+\r
+ [Test]\r
+ public void CreateInstance_StringString ()\r
+ {\r
+ ObjectHandle objHandle = Activator.CreateInstance (null, "MonoTests.System.ActivatorTestInternal.COMTest");\r
+ COMTest objCOMTest = (COMTest)objHandle.Unwrap ();
+ objCOMTest.Id = 2;\r
+ Assert.AreEqual (2, objCOMTest.Id, "#A03");
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (ArgumentNullException))]\r
+ public void CreateInstance_StringNull ()\r
+ {\r
+ Activator.CreateInstance ((string)null, null);\r
+ }\r
+\r
+ [Test]\r
+ [ExpectedException (typeof (TypeLoadException))]\r
+ public void CreateInstance_StringTypeNameDoesNotExists ()\r
+ {\r
+ Activator.CreateInstance ((string)null, "MonoTests.System.ActivatorTestInternal.DoesntExistsCOMTest");\r
+ }\r
+\r
+ [Test]\r
+ public void CreateInstance_TypeBool ()\r
+ {\r
+ COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), false);
+ Assert.AreEqual ("MonoTests.System.ActivatorTestInternal.COMTest", objCOMTest.GetType ().ToString (), "#A04");
+ }\r
+\r
+ [Test]\r
+ public void CreateInstance_TypeObjectArray ()\r
+ {\r
+ object[] objArray = new object[1] { 7 };\r
+ COMTest objCOMTest = (COMTest)Activator.CreateInstance (typeof (COMTest), objArray);\r
+ Assert.AreEqual (7, objCOMTest.Id, "#A05");
+ }\r
+\r
+ // TODO: Implemente the test methods for all the overriden functions using activationAttribute
+
+ [Test]
#if NET_2_0
- [ExpectedException(typeof(MissingMethodException))]
+ [ExpectedException(typeof(MissingMethodException))]
#else
- [ExpectedException(typeof(MemberAccessException))]
+ [ExpectedException(typeof(MemberAccessException))]
#endif
- public void CreateInstanceAbstract1() {
- Activator.CreateInstance(typeof(Type));
- }
+ public void CreateInstanceAbstract1 ()
+ {
+ Activator.CreateInstance (typeof (Type));
+ }
- [Test]
+ [Test]
#if NET_2_0
- [ExpectedException(typeof(MissingMethodException))]
+ [ExpectedException(typeof(MissingMethodException))]
#else
- [ExpectedException(typeof(MemberAccessException))]
+ [ExpectedException(typeof(MemberAccessException))]
#endif
- public void CreateInstanceAbstract2() {
- Activator.CreateInstance(typeof(Type), true);
- }
-
- [Test]
- [ExpectedException(typeof(MissingMethodException))]
- public void CreateInstanceAbstract3() {
- Activator.CreateInstance(typeof(Type), null, null);\r
- }
-
- [Test]
- [ExpectedException(typeof(MissingMethodException))]
- public void CreateInstanceAbstract4() {
- Activator.CreateInstance(typeof(Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);\r
- }
-
- [Test]
+ public void CreateInstanceAbstract2 ()
+ {
+ Activator.CreateInstance (typeof (Type), true);
+ }
+
+ [Test]
+ [ExpectedException(typeof(MissingMethodException))]
+ public void CreateInstanceAbstract3 ()
+ {
+ Activator.CreateInstance (typeof (Type), null, null);\r
+ }
+
+ [Test]
+ [ExpectedException(typeof(MissingMethodException))]
+ public void CreateInstanceAbstract4()
+ {
+ Activator.CreateInstance (typeof (Type), BindingFlags.CreateInstance | (BindingFlags.Public | BindingFlags.Instance), null, null, CultureInfo.InvariantCulture, null);\r
+ }
+
+ [Test]
#if NET_2_0
- [ExpectedException(typeof(MissingMethodException))]
+ [ExpectedException (typeof (MissingMethodException))]
#else
- [ExpectedException(typeof(MemberAccessException))]
+ [ExpectedException (typeof (MemberAccessException))]
#endif
- public void CreateInstanceAbstract5() {
- Activator.CreateInstance(typeof(Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);\r
- }
-
- // This method tests GetObject from the Activator class
- [Test]
- public void GetObject()
- {
- // object GetObject(Type, string)
-
- // This will provide a COMTest object on tcp://localhost:1234/COMTestUri
- COMTest objCOMTest = new COMTest(8);
- TcpChannel chnServer = new TcpChannel(1234);
- ChannelServices.RegisterChannel(chnServer);
- RemotingServices.SetObjectUriForMarshal(objCOMTest, "COMTestUri");
- RemotingServices.Marshal(objCOMTest);
-
- // This will get the remoting object
- object objRem = Activator.GetObject(typeof(COMTest),
-"tcp://localhost:1234/COMTestUri");
- Assertion.Assert("#A07",objRem != null);
- COMTest remCOMTest = (COMTest) objRem;
- Assertion.AssertEquals("#A08", 8, remCOMTest.Id);
-
- ChannelServices.UnregisterChannel(chnServer);
- // Todo: Implemente the test methods for
- // all the overriden function using activationAttribute
- }
-
- // This method tests the CreateInstanceFrom methods
- // of the Activator class
- [Test]
- public void CreateInstanceFrom()
- {
- ObjectHandle objHandle;
- objHandle = Activator.CreateInstanceFrom(strAssembly ,
-"MonoTests.System.ActivatorTestInternal.COMTest");
- Assertion.Assert("#A09", objHandle != null);
- objHandle.Unwrap();
- // Todo: Implement the test methods for
- // all the overriden function using activationAttribute
- }
-
- // The name of the assembly file is incorrect.
- // I used it to test these classes but you should
- // replace it with the name of the mono tests assembly file
- // The name of the assembly is used to get an object through
- // Activator.CreateInstance(), Activator.CreateComInstanceFrom()...
- private string strAssembly = "corlib_test.dll";
-
- }
-
- }
+ public void CreateInstanceAbstract5 ()
+ {
+ Activator.CreateInstance (typeof (Type), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);\r
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void GetObject_TypeNull ()
+ {
+ Activator.GetObject (null, "tcp://localhost:1234/COMTestUri");
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void GetObject_UrlNull ()
+ {
+ Activator.GetObject (typeof (COMTest), null);
+ }
+
+/* This test is now executed in System.Runtime.Remoting unit tests
+ [Test]
+ public void GetObject ()
+ {
+ // This will provide a COMTest object on tcp://localhost:1234/COMTestUri
+ COMTest objCOMTest = new COMTest (8);
+ TcpChannel chnServer = new TcpChannel (1234);
+ ChannelServices.RegisterChannel (chnServer);
+ RemotingServices.SetObjectUriForMarshal (objCOMTest, "COMTestUri");
+ RemotingServices.Marshal (objCOMTest);
+
+ // This will get the remoting object
+ object objRem = Activator.GetObject (typeof (COMTest), "tcp://localhost:1234/COMTestUri");\r
+ Assert.IsNotNull (objRem, "#A07");
+ COMTest remCOMTest = (COMTest) objRem;\r
+ Assert.AreEqual (8, remCOMTest.Id, "#A08");
+
+ ChannelServices.UnregisterChannel(chnServer);
+ }\r
+*/\r
+ // TODO: Implemente the test methods for all the overriden function using activationAttribute
+
+ [Test]
+ public void CreateInstanceFrom ()
+ {
+ ObjectHandle objHandle = Activator.CreateInstanceFrom (testLocation, "MonoTests.System.ActivatorTestInternal.COMTest");\r
+ Assert.IsNotNull (objHandle, "#A09");
+ objHandle.Unwrap ();
+ // TODO: Implement the test methods for all the overriden function using activationAttribute
+ }\r
+\r
+ // note: this only ensure that the ECMA key support unification (more test required, outside corlib, for other keys, like MS final).\r
+ private const string CorlibPermissionPattern = "System.Security.Permissions.FileDialogPermission, mscorlib, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";\r
+ private const string SystemPermissionPattern = "System.Net.DnsPermission, System, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089";\r
+ private const string fx10version = "1.0.3300.0";\r
+ private const string fx11version = "1.0.5000.0";\r
+ private const string fx20version = "2.0.0.0";\r
+\r
+ private static object[] psNone = new object [1] { PermissionState.None };\r
+\r
+ private void Unification (string fullname)\r
+ {\r
+ Type t = Type.GetType (fullname);\r
+ IPermission p = (IPermission)Activator.CreateInstance (t, psNone);\r
+ string currentVersion = typeof (string).Assembly.GetName ().Version.ToString ();\r
+ Assert.IsTrue ((p.ToString ().IndexOf (currentVersion) > 0), currentVersion);\r
+ }\r
+\r
+ [Test]\r
+ public void Unification_FromFx10 ()\r
+ {\r
+ Unification (String.Format (CorlibPermissionPattern, fx10version));\r
+ Unification (String.Format (SystemPermissionPattern, fx10version));\r
+ }\r
+\r
+ [Test]\r
+ public void Unification_FromFx11 ()\r
+ {\r
+ Unification (String.Format (CorlibPermissionPattern, fx11version));\r
+ Unification (String.Format (SystemPermissionPattern, fx11version));\r
+ }\r
+\r
+ [Test]\r
+ public void Unification_FromFx20 ()\r
+ {\r
+ Unification (String.Format (CorlibPermissionPattern, fx20version));\r
+ Unification (String.Format (SystemPermissionPattern, fx20version));\r
+ }\r
+\r
+ [Test]\r
+ public void Unification_FromFx99_Corlib ()\r
+ {\r
+ Unification (String.Format (CorlibPermissionPattern, "9.99.999.9999"));
+#if NET_1_1
+ Unification (String.Format (SystemPermissionPattern, "9.99.999.9999"));
+#endif\r
+ }\r
+\r
+#if NET_2_0
+ [Test]
+ [Category ("NotWorking")]\r
+ public void Unification_FromFx99_System ()\r
+ {\r
+ Assert.IsNull (Type.GetType (String.Format (SystemPermissionPattern, "9.99.999.9999")));\r
+ }
+#endif\r
+ }
+}