2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System / ActivatorTest.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Runtime.Remoting;
4 using System.Runtime.Remoting.Channels;
5 using System.Runtime.Remoting.Channels.Tcp;
6 using NUnit.Framework;
7
8 // The class in this namespace is used by the
9 // main test class
10 namespace MonoTests.System.ActivatorTestInternal
11   {
12   // We need a COM class to test the Activator class
13   [ComVisible(true)]
14     
15   public class COMTest: MarshalByRefObject
16     {
17     public COMTest()
18       {
19       id = 0;
20       }
21     // This property is visible
22     [ComVisible(true)]
23       public int Id
24       {
25       get { return id; }
26       set { id = value; }
27       }
28     
29     public COMTest(int id)
30       {
31       this.id = id;
32       }
33     
34     private int id;
35     public bool constructorFlag = false;
36     }
37   } // MonoTests.System.ActivatorTestInternal namespace
38
39 namespace MonoTests.System
40   {
41   using MonoTests.System.ActivatorTestInternal;
42
43   [TestFixture]
44   public class ActivatorTest
45     {
46     public ActivatorTest()
47       {}
48     
49     [Test]
50       [Ignore("Activator.CreateComInstanceForm is not yet implemented")]
51       // This test is ignored for the moment because 
52       // CreateComInstanceFrom() is not implemented yet
53       // by the mono Activator class
54       public void CreateComInstanceFrom()
55       {
56       ObjectHandle objHandle = Activator.CreateComInstanceFrom(strAssembly ,
57 "COMTest");
58       COMTest objCOMTest = (COMTest) objHandle.Unwrap();
59       objCOMTest.Id = 10;
60       Assertion.AssertEquals("#A01",10,objCOMTest.Id);
61       }
62
63     [Test]
64       // This method tests CreateInstance()
65       public void CreateInstance()
66       {
67       COMTest objCOMTest;
68       // object CreateInstance(Type type)
69       objCOMTest = (COMTest) Activator.CreateInstance(typeof(COMTest));
70       Assertion.AssertEquals("#A02",
71 "MonoTests.System.ActivatorTestInternal.COMTest",
72 (objCOMTest.GetType()).ToString());
73       // ObjectHandle CreateInstance(string, string) 
74        ObjectHandle objHandle;
75        objHandle = Activator.CreateInstance(null ,
76 "MonoTests.System.ActivatorTestInternal.COMTest");
77        objCOMTest = (COMTest) objHandle.Unwrap();
78        objCOMTest.Id = 2;
79        Assertion.AssertEquals("#A03", 2, objCOMTest.Id);
80       // object CreateInstance(Type, bool)
81        objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), false);
82        Assertion.AssertEquals("#A04",
83 "MonoTests.System.ActivatorTestInternal.COMTest",
84 (objCOMTest.GetType()).ToString());
85 //       // object CreateInstance(Type, object[])
86        object[] objArray = new object[1];
87        objArray[0] = 7;
88        objCOMTest = (COMTest) Activator.CreateInstance((typeof(COMTest)), objArray);
89        Assertion.AssertEquals("#A05", 7, objCOMTest.Id);
90        // Todo: Implemente the test methods for
91        // all the overriden functions using activationAttribute
92       }
93
94     // This method tests GetObject from the Activator class
95     [Test]
96       public void GetObject()
97       {
98       // object GetObject(Type, string)
99       
100       // This will provide a COMTest object on  tcp://localhost:1234/COMTestUri
101       COMTest objCOMTest = new COMTest(8);
102       TcpChannel chnServer = new TcpChannel(1234);
103       ChannelServices.RegisterChannel(chnServer);
104       RemotingServices.SetObjectUriForMarshal(objCOMTest, "COMTestUri");
105       RemotingServices.Marshal(objCOMTest);
106       
107       // This will get the remoting object
108       object objRem = Activator.GetObject(typeof(COMTest),
109 "tcp://localhost:1234/COMTestUri");
110       Assertion.Assert("#A07",objRem != null);
111       COMTest remCOMTest = (COMTest) objRem;
112       Assertion.AssertEquals("#A08", 8, remCOMTest.Id);
113
114       ChannelServices.UnregisterChannel(chnServer);
115        // Todo: Implemente the test methods for
116        // all the overriden function using activationAttribute
117       }
118
119     // This method tests the CreateInstanceFrom methods
120     // of the Activator class
121     [Test]
122       public void CreateInstanceFrom()
123       {
124       ObjectHandle objHandle;
125       objHandle = Activator.CreateInstanceFrom(strAssembly ,
126 "MonoTests.System.ActivatorTestInternal.COMTest");
127       Assertion.Assert("#A09", objHandle != null);
128                 objHandle.Unwrap();
129        // Todo: Implement the test methods for
130        // all the overriden function using activationAttribute
131       }
132     
133     // The name of the assembly file is incorrect.
134     // I used it to test these classes but you should
135     // replace it with the name of the mono tests assembly file
136     // The name of the assembly is used to get an object through
137     // Activator.CreateInstance(), Activator.CreateComInstanceFrom()...
138     private string strAssembly = "corlib_test.dll";
139     
140     }
141   
142   }