2181a59b191de8a5f4d1aadbf0191f9be912843b
[mono.git] / mcs / class / System.Runtime.Remoting / Test / ActivationTests.cs
1 //
2 // MonoTests.Remoting.TcpCalls.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (C) Copyright, Ximian, Inc.
7 //
8
9 using System;
10 using System.Runtime.Remoting;
11 using System.Runtime.Remoting.Channels;
12 using System.Runtime.Remoting.Channels.Tcp;
13 using System.Runtime.Remoting.Channels.Http;
14 using NUnit.Framework;
15
16 namespace MonoTests.Remoting
17 {
18         [TestFixture]
19         public class ActivationTests
20         {
21                 ActivationServer server;
22                 TcpChannel tcp;
23                 HttpChannel http;
24                         
25                 [TestFixtureSetUp]
26                 public void Run()
27                 {
28                         try
29                         {
30                                 tcp =  new TcpChannel (0);
31                                 http =  new HttpChannel (0);
32                         
33                                 ChannelServices.RegisterChannel (tcp);
34                                 ChannelServices.RegisterChannel (http);
35                         
36                                 AppDomain domain = AppDomain.CreateDomain ("testdomain_activation");
37                                 server = (ActivationServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.ActivationServer");
38                                 
39                                 RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject1), "tcp://localhost:9433");
40                                 RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject2), "http://localhost:9434");
41                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall1), "tcp://localhost:9433/wkoSingleCall1");
42                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton1), "tcp://localhost:9433/wkoSingleton1");
43                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall2), "http://localhost:9434/wkoSingleCall2");
44                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton2), "http://localhost:9434/wkoSingleton2");
45                         }
46                         catch (Exception ex)
47                         {
48                                 Console.WriteLine (ex);
49                         }
50                 }
51                 
52                 [Test]
53                 public void TestCreateTcpCao ()
54                 {
55                         CaObject1 ca = new CaObject1 ();
56                         CaObject1 ca2 = new CaObject1 ();
57                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
58                         RunTestCreateCao (ca, ca2);
59                 }
60                 
61                 [Test]
62                 public void TestCreateHttpCao ()
63                 {
64                         CaObject2 ca = new CaObject2 ();
65                         CaObject2 ca2 = new CaObject2 ();
66                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
67                         RunTestCreateCao (ca, ca2);
68                 }
69                 
70                 public void RunTestCreateCao (BaseObject ca, BaseObject ca2)
71                 {
72                         Assert.AreEqual (0, ca.counter, "#1");
73                         
74                         ca.counter++;
75                         Assert.AreEqual (1, ca.counter, "#2");
76                         
77                         Assert.AreEqual (0, ca2.counter, "#3");
78                         
79                         ca2.counter++;
80                         Assert.AreEqual (1, ca2.counter, "#4");
81                         
82                         Assert.AreEqual (1, ca.counter, "#5");
83                 }
84
85                 [Test]
86                 public void TestCreateTcpWkoSingleCall ()
87                 {
88                         WkObjectSinglecall1 ca = new WkObjectSinglecall1 ();
89                         WkObjectSinglecall1 ca2 = new WkObjectSinglecall1 ();
90                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
91                         RunTestCreateWkoSingleCall (ca, ca2);
92                 }
93                 
94                 [Test]
95                 public void TestCreateTcpWkoSingleton ()
96                 {
97                         WkObjectSingleton1 ca = new WkObjectSingleton1 ();
98                         WkObjectSingleton1 ca2 = new WkObjectSingleton1 ();
99                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
100                         RunTestCreateWkoSingleton (ca, ca2);
101                 }
102
103                 [Test]
104                 public void TestCreateHttpWkoSingleCall ()
105                 {
106                         WkObjectSinglecall2 ca = new WkObjectSinglecall2 ();
107                         WkObjectSinglecall2 ca2 = new WkObjectSinglecall2 ();
108                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
109                         RunTestCreateWkoSingleCall (ca, ca2);
110                 }
111                 
112                 [Test]
113                 public void TestCreateHttpWkoSingleton ()
114                 {
115                         WkObjectSingleton2 ca = new WkObjectSingleton2 ();
116                         WkObjectSingleton2 ca2 = new WkObjectSingleton2 ();
117                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
118                         RunTestCreateWkoSingleton (ca, ca2);
119                 }
120                 
121                 public void RunTestCreateWkoSingleCall (BaseObject ca, BaseObject ca2)
122                 {
123                         Assert.AreEqual (0, ca.counter, "#1");
124                         ca.counter++;
125                         Assert.AreEqual (0, ca.counter, "#2");
126
127                         Assert.AreEqual (0, ca2.counter, "#3");
128                         ca2.counter++;
129                         Assert.AreEqual (0, ca2.counter, "#4");
130                 }
131
132                 public void RunTestCreateWkoSingleton (BaseObject ca, BaseObject ca2)
133                 {
134                         Assert.AreEqual (0, ca.counter, "#1");
135                         ca.counter++;
136                         Assert.AreEqual (1, ca.counter, "#2");
137                         ca.counter++;
138                         Assert.AreEqual (2, ca2.counter, "#3");
139                         ca2.counter++;
140                         Assert.AreEqual (3, ca2.counter, "#4");
141                 }
142
143                 [TestFixtureTearDown]
144                 public void End ()
145                 {
146                         ChannelServices.UnregisterChannel (tcp);
147                         ChannelServices.UnregisterChannel (http);
148                         server.Stop ();
149                 }
150         }
151         
152         public class ActivationServer: MarshalByRefObject
153         {
154                 TcpChannel tcp;
155                 HttpChannel http;
156                 
157                 public ActivationServer ()
158                 {
159                         tcp =  new TcpChannel (9433);
160                         http =  new HttpChannel (9434);
161                         
162                         ChannelServices.RegisterChannel (tcp);
163                         ChannelServices.RegisterChannel (http);
164                         
165                         RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject1));
166                         RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject2));
167                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall1), "wkoSingleCall1", WellKnownObjectMode.SingleCall);
168                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton1), "wkoSingleton1", WellKnownObjectMode.Singleton);
169                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall2), "wkoSingleCall2", WellKnownObjectMode.SingleCall);
170                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton2), "wkoSingleton2", WellKnownObjectMode.Singleton);
171                 }
172                 
173                 public void Stop ()
174                 {
175                         ChannelServices.UnregisterChannel (tcp);
176                         ChannelServices.UnregisterChannel (http);
177                 }
178         }
179         
180         public class BaseObject: MarshalByRefObject
181         {
182                 public int counter;
183                 public static int CreationCount;
184                 
185                 public BaseObject ()
186                 {
187                         CreationCount++;
188                 }
189         }
190         
191         public class CaObject1: BaseObject
192         {
193         }
194         
195         public class CaObject2: BaseObject
196         {
197         }
198         
199         public class WkObjectSinglecall1: BaseObject
200         {
201         }
202         
203         public class WkObjectSingleton1: BaseObject
204         {
205         }
206         
207         public class WkObjectSinglecall2: BaseObject
208         {
209         }
210         
211         public class WkObjectSingleton2: BaseObject
212         {
213         }
214 }