**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Runtime.Remoting / RemotingConfigurationTest.cs
blob2f341c6d67e47b5c09f277e9fbb0340c167c1dfe
1 // created on 05/03/2003 at 13:18
2 // RemotingConfigurationTest.cs unit test class
3 // for System.Runtime.Remoting.RemotingConfiguration
4 //
5 // Author: Jean-Marc ANDRE <jean-marc.andre@polymtl.ca>
6 //
8 using System;
9 #if NET_1_1
10 using System.Net.Sockets;
11 #endif
12 using System.Reflection;
13 using System.Runtime.Remoting;
14 using System.Runtime.Remoting.Channels;
15 using System.Runtime.Remoting.Channels.Tcp;
16 using System.Runtime.Remoting.Channels.Http;
17 using NUnit.Framework;
19 // internal namespace with classes I'll use during the unit tests
20 namespace MonoTests.System.Runtime.RemotingConfigurationInternal
22 public class MarshalObject: MarshalByRefObject
24 private bool _method1Called = false;
26 public bool Method1Called
28 get{ return _method1Called;}
31 public void Method1()
33 _method1Called = true;
38 public class DerivedMarshalObject: MarshalObject
43 public class WellKnownObject: MarshalByRefObject
45 private bool _method1Called = false;
47 public bool Method1Called
49 get{ return _method1Called;}
52 public void Method1()
54 _method1Called = true;
58 public class DerivedWellKnownObject: WellKnownObject
63 public class ActivatedObject: MarshalByRefObject
68 public class DerivedActivatedObject: ActivatedObject
73 public class AppNameTest: MarshalByRefObject
79 namespace MonoTests.System.Runtime.Remoting
81 using MonoTests.System.Runtime.RemotingConfigurationInternal;
83 // The unit test class
84 [TestFixture]
85 public class RemotingConfigurationTest
88 [Test]
89 public void ApplicationId()
91 string _Id = RemotingConfiguration.ApplicationId;
93 Assertion.Assert("#A01", _Id != null);
96 // tests and set the ApplicationName
97 [Test]
98 public void ApplicationName()
100 TcpChannel chn = null;
101 AppNameTest objAppNameTest = null;
104 chn = new TcpChannel(1234);
105 ChannelServices.RegisterChannel(chn);
107 // the URL of the application's marshaled objects should be
108 // tcp://localhost:1234/RemotingConfigurationTest/<object>
109 RemotingConfiguration.ApplicationName = "RemotingConfigurationTest";
111 objAppNameTest = new AppNameTest();
112 RemotingServices.Marshal(objAppNameTest, "AppNameTest.rem");
114 AppNameTest remAppNameTest = (AppNameTest) Activator.GetObject(typeof(AppNameTest), "tcp://localhost:1234/" + RemotingConfiguration.ApplicationName + "AppNameTest.rem");
116 Assertion.Assert("#B01", remAppNameTest != null);
118 catch(Exception e)
120 Assertion.Assert("#B02: " + e.Message, false);
122 finally
124 RemotingServices.Disconnect(objAppNameTest);
125 ChannelServices.UnregisterChannel(chn);
129 // tests related to the SAO
130 [Test]
131 public void RegisterWellKnownType()
133 TcpChannel chn = null;
136 chn = new TcpChannel(1234);
137 ChannelServices.RegisterChannel(chn);
139 // register the SAO
140 if(RemotingConfiguration.ApplicationName == null) RemotingConfiguration.ApplicationName = "RemotingConfigurationTest";
141 RemotingConfiguration.RegisterWellKnownServiceType(typeof(DerivedWellKnownObject), "WellKnownObject.rem", WellKnownObjectMode.Singleton);
143 // get the registered services
144 WellKnownServiceTypeEntry[] ast = RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
146 bool IsServerRegistered = false;
147 foreach(WellKnownServiceTypeEntry aste in ast)
149 if(aste.ObjectType == typeof(DerivedWellKnownObject))
151 IsServerRegistered = true;
152 break;
156 Assertion.Assert("#A02", IsServerRegistered);
158 // register the client
159 RemotingConfiguration.RegisterWellKnownClientType(typeof(WellKnownObject), "tcp://localhost:1234/"+RemotingConfiguration.ApplicationName+"/WellKnownObject.rem");
161 // get the registered client
162 WellKnownClientTypeEntry[] act = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
164 bool IsClientRegistered = false;
165 foreach(WellKnownClientTypeEntry acte in act)
167 if(acte.ObjectType == typeof(WellKnownObject))
169 IsClientRegistered = true;
170 break;
174 Assertion.Assert("#A03", IsClientRegistered);
176 WellKnownObject objWellKnown = new WellKnownObject();
179 Assertion.Assert("#A04", objWellKnown != null);
180 Assertion.Assert("#A05", RemotingServices.IsTransparentProxy(objWellKnown));
181 objWellKnown.Method1();
182 Assertion.Assert("#A06", objWellKnown.Method1Called);
184 finally
186 ChannelServices.UnregisterChannel(chn);
191 // tests the CAO related methods
192 [Test]
193 #if NET_1_1
194 [ExpectedException(typeof(SocketException))]
195 #else
196 [ExpectedException(typeof(RemotingException))]
197 #endif
198 public void RegisterActivatedType()
200 TcpChannel chn = null;
203 chn = new TcpChannel(1234);
204 ChannelServices.RegisterChannel(chn);
206 // register the CAO
207 RemotingConfiguration.RegisterActivatedServiceType(typeof(ActivatedObject));
209 // get the registered CAO
210 ActivatedServiceTypeEntry[] ast = RemotingConfiguration.GetRegisteredActivatedServiceTypes();
212 bool IsServerRegistered = false;
213 foreach(ActivatedServiceTypeEntry aste in ast)
215 if(aste.ObjectType == typeof(ActivatedObject))
217 IsServerRegistered = true;
218 break;
222 Assertion.Assert("#A07", IsServerRegistered);
224 RemotingConfiguration.RegisterActivatedClientType(typeof(DerivedActivatedObject), "tcp://localhost:1234");
226 ActivatedClientTypeEntry[] act = RemotingConfiguration.GetRegisteredActivatedClientTypes();
228 bool IsClientRegistered = false;
229 foreach(ActivatedClientTypeEntry acte in act)
231 if(acte.ObjectType == typeof(DerivedActivatedObject))
233 IsClientRegistered = true;
234 break;
238 Assertion.Assert("#A08", IsClientRegistered);
240 // This will send a RemotingException since there is no service named DerivedActivatedObject
241 // on the server
242 DerivedActivatedObject objDerivedActivated = new DerivedActivatedObject();
244 finally
246 ChannelServices.UnregisterChannel(chn);
251 // Get the process ID
252 [Test]
253 public void ProcessId()
255 string strProcessId = null;
256 strProcessId = RemotingConfiguration.ProcessId;
257 Assertion.Assert("#AO9", strProcessId != null);
260 [Test]
261 public void IsActivationAllowed()
263 // register the CAO
264 RemotingConfiguration.RegisterActivatedServiceType(typeof(ActivatedObject));
265 // ActivatedObject was previously registered as a CAO on the server
266 // so IsActivationAllowed() should return TRUE
267 Assertion.Assert("#A10", RemotingConfiguration.IsActivationAllowed(typeof(ActivatedObject)));
270 [Test]
271 public void IsRemotelyActivatedClientType()
273 Assembly ass = Assembly.GetExecutingAssembly();
274 AssemblyName assName = ass.GetName();
276 ActivatedClientTypeEntry acte = null;
278 RemotingConfiguration.RegisterActivatedClientType(typeof(DerivedActivatedObject), "tcp://localhost:1234");
280 // DerivedActivatedObject was registered as a CAO on the client
281 acte = RemotingConfiguration.IsRemotelyActivatedClientType(typeof(DerivedActivatedObject));
283 Assertion.Assert("#A11", acte != null);
284 Assertion.AssertEquals("#A12", typeof(DerivedActivatedObject), acte.ObjectType);
286 acte = RemotingConfiguration.IsRemotelyActivatedClientType(typeof(DerivedActivatedObject).ToString(), assName.Name);
287 Assertion.Assert("#A13", acte != null);
288 Assertion.AssertEquals("#A14", typeof(DerivedActivatedObject), acte.ObjectType);
291 [Test]
292 public void IsWellKnownClientType()
294 Assembly ass = Assembly.GetExecutingAssembly();
295 AssemblyName assName = ass.GetName();
297 WellKnownClientTypeEntry acte = null;
298 // WellKnownObject was registered as a SAO on th client
299 acte = RemotingConfiguration.IsWellKnownClientType(typeof(WellKnownObject));
301 Assertion.Assert("#A11", acte != null);
302 Assertion.AssertEquals("#A12", typeof(WellKnownObject), acte.ObjectType);
304 acte = RemotingConfiguration.IsWellKnownClientType(typeof(WellKnownObject).ToString(), assName.Name);
305 Assertion.Assert("#A13", acte != null);
306 Assertion.AssertEquals("#A14", typeof(WellKnownObject), acte.ObjectType);