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