1 // created on 05/03/2003 at 13:18
2 // RemotingConfigurationTest.cs unit test class
3 // for System.Runtime.Remoting.RemotingConfiguration
5 // Author: Jean-Marc ANDRE <jean-marc.andre@polymtl.ca>
10 using System.Net.Sockets;
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;}
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;}
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
85 public class RemotingConfigurationTest
89 public void ApplicationId()
91 string _Id = RemotingConfiguration.ApplicationId;
93 Assert.IsTrue(_Id != null, "#A01");
96 // tests and set the ApplicationName
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 Assert.IsTrue(remAppNameTest != null, "#B01");
120 Assert.Fail ("#B02: " + e.Message);
124 RemotingServices.Disconnect(objAppNameTest);
125 ChannelServices.UnregisterChannel(chn);
129 // tests related to the SAO
131 public void RegisterWellKnownType()
133 TcpChannel chn = null;
136 chn = new TcpChannel(1234);
137 ChannelServices.RegisterChannel(chn);
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;
156 Assert.IsTrue(IsServerRegistered, "#A02");
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;
174 Assert.IsTrue(IsClientRegistered, "#A03");
176 WellKnownObject objWellKnown = new WellKnownObject();
179 Assert.IsTrue(objWellKnown != null, "#A04");
180 Assert.IsTrue(RemotingServices.IsTransparentProxy(objWellKnown), "#A05");
181 objWellKnown.Method1();
182 Assert.IsTrue(objWellKnown.Method1Called, "#A06");
186 ChannelServices.UnregisterChannel(chn);
191 // tests the CAO related methods
194 [ExpectedException(typeof(SocketException))]
196 [ExpectedException(typeof(RemotingException))]
198 public void RegisterActivatedType()
200 TcpChannel chn = null;
203 chn = new TcpChannel(1234);
204 ChannelServices.RegisterChannel(chn);
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;
222 Assert.IsTrue(IsServerRegistered, "#A07");
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;
238 Assert.IsTrue(IsClientRegistered); , "#A08");
240 // This will send a RemotingException since there is no service named DerivedActivatedObject
242 DerivedActivatedObject objDerivedActivated = new DerivedActivatedObject();
246 ChannelServices.UnregisterChannel(chn);
251 // Get the process ID
253 public void ProcessId()
255 string strProcessId = null;
256 strProcessId = RemotingConfiguration.ProcessId;
257 Assert.IsTrue(strProcessId != null, "#AO9");
261 public void IsActivationAllowed()
264 RemotingConfiguration.RegisterActivatedServiceType(typeof(ActivatedObject));
265 // ActivatedObject was previously registered as a CAO on the server
266 // so IsActivationAllowed() should return TRUE
267 Assert.IsTrue(RemotingConfiguration.IsActivationAllowed(typeof(ActivatedObject)), "#A10");
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 Assert.IsTrue(acte != null, "#A11");
284 Assert.AreEqual(typeof(DerivedActivatedObject), acte.ObjectType, "#A12");
286 acte = RemotingConfiguration.IsRemotelyActivatedClientType(typeof(DerivedActivatedObject).ToString(), assName.Name);
287 Assert.IsTrue(acte != null, "#A13");
288 Assert.AreEqual(typeof(DerivedActivatedObject), acte.ObjectType, "#A14");
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 Assert.IsTrue(acte != null, "#A11");
302 Assert.AreEqual(typeof(WellKnownObject), acte.ObjectType, "#A12");
304 acte = RemotingConfiguration.IsWellKnownClientType(typeof(WellKnownObject).ToString(), assName.Name);
305 Assert.IsTrue(acte != null, "#A13");
306 Assert.AreEqual(typeof(WellKnownObject), acte.ObjectType, "#A14");