2 // System.Runtime.Remoting.RemotingServices NUnit V2.0 test class
4 // Author Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)
6 // ToDo: I didn't write test functions for the method not yep
10 using System
.Collections
;
11 using NUnit
.Framework
;
12 using System
.Reflection
;
13 using System
.Runtime
.Remoting
;
14 using System
.Threading
;
15 using System
.Runtime
.Remoting
.Activation
;
16 using System
.Runtime
.Remoting
.Messaging
;
17 using System
.Runtime
.Remoting
.Proxies
;
18 using System
.Runtime
.Remoting
.Channels
;
19 using System
.Runtime
.Remoting
.Channels
.Tcp
;
21 namespace MonoTests
.System
.Runtime
.Remoting
.RemotingServicesInternal
23 // We need our own proxy to intercept messages to remote object
24 // and forward them using RemotingServices.ExecuteMessage
25 public class MyProxy
: RealProxy
27 MarshalByRefObject target
;
30 bool methodOverloaded
= false;
32 public MethodBase MthBase
34 get{ return _mthBase;}
37 public bool IsMethodOverloaded
39 get{return methodOverloaded;}
42 public MyProxy(Type serverType
, MarshalByRefObject target
): base(serverType
)
46 IChannel
[] registeredChannels
= ChannelServices
.RegisteredChannels
;
49 // A new IMessageSink chain has to be created
50 // since the RemotingServices.GetEnvoyChainForProxy() is not yet
52 foreach(IChannel channel
in registeredChannels
)
54 IChannelSender channelSender
= channel
as IChannelSender
;
55 if(channelSender
!= null)
57 _sink
= (IMessageSink
) channelSender
.CreateMessageSink(RemotingServices
.GetObjectUri(target
), null, out ObjectURI
);
63 // Messages will be intercepted here and redirected
65 public override IMessage
Invoke(IMessage msg
)
67 if(msg
is IConstructionCallMessage
)
69 IActivator remActivator
= (IActivator
) RemotingServices
.Connect(typeof(IActivator
), "tcp://localhost:1234/RemoteActivationService.rem");
70 IConstructionReturnMessage crm
= remActivator
.Activate((IConstructionCallMessage
)msg
);
75 methodOverloaded
= RemotingServices
.IsMethodOverloaded((IMethodMessage
)msg
);
77 _mthBase
= RemotingServices
.GetMethodBaseFromMethodMessage((IMethodMessage
)msg
);
78 MethodCallMessageWrapper mcm
= new MethodCallMessageWrapper((IMethodCallMessage
) msg
);
79 mcm
.Uri
= RemotingServices
.GetObjectUri((MarshalByRefObject
)target
);
80 MarshalByRefObject objRem
= (MarshalByRefObject
)Activator
.CreateInstance(GetProxiedType());
81 RemotingServices
.ExecuteMessage((MarshalByRefObject
)objRem
, (IMethodCallMessage
)msg
);
82 IMessage rtnMsg
= null;
86 rtnMsg
= _sink
.SyncProcessMessage(msg
);
90 Console
.WriteLine(e
.Message
);
98 // This class is used to create "CAO"
99 public class MarshalObjectFactory
: MarshalByRefObject
101 public MarshalObject
GetNewMarshalObject()
103 return new MarshalObject();
107 // A class used by the tests
108 public class MarshalObject
: ContextBoundObject
110 public MarshalObject()
115 public MarshalObject(int id
, string uri
)
130 public void Method1()
133 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
136 public void Method2()
138 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
141 public void Method2(int i
)
143 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
148 public void Method3()
150 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
154 public static int Called
159 public static bool IsMethodOneWay
161 get{return methodOneWay;}
165 private static int _called
;
168 private static bool methodOneWay
= false;
171 // Another class used by the tests
172 public class DerivedMarshalObject
: MarshalObject
174 public DerivedMarshalObject(){}
176 public DerivedMarshalObject(int id
, string uri
): base(id
, uri
) {}
178 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
180 namespace MonoTests
.System
.Runtime
.Remoting
182 using MonoTests
.System
.Runtime
.Remoting
.RemotingServicesInternal
;
184 // The main test class
186 public class RemotingServicesTest
: Assertion
188 private static int MarshalObjectId
= 0;
190 public RemotingServicesTest()
195 // Helper function that create a new
196 // MarshalObject with an unique ID
197 private static MarshalObject
NewMarshalObject()
199 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId
.ToString();
200 MarshalObject objMarshal
= new MarshalObject(MarshalObjectId
, uri
);
207 // Another helper function
208 private DerivedMarshalObject
NewDerivedMarshalObject()
210 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId
.ToString();
211 DerivedMarshalObject objMarshal
= new DerivedMarshalObject(MarshalObjectId
, uri
);
218 // The two folling method test RemotingServices.Marshal()
220 public void Marshal1()
223 MarshalObject objMarshal
= NewMarshalObject();
224 ObjRef objRef
= RemotingServices
.Marshal(objMarshal
);
226 Assert("#A01", objRef
.URI
!= null);
228 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Unmarshal(objRef
);
229 AssertEquals("#A02", objMarshal
.Id
, objRem
.Id
);
232 AssertEquals("#A03", objMarshal
.Id
, objRem
.Id
);
234 // TODO: uncomment when RemotingServices.Disconnect is implemented
235 //RemotingServices.Disconnect(objMarshal);
237 objMarshal
= NewMarshalObject();
239 objRef
= RemotingServices
.Marshal(objMarshal
, objMarshal
.Uri
);
241 Assert("#A04", objRef
.URI
.EndsWith(objMarshal
.Uri
));
242 // TODO: uncomment when RemotingServices.Disconnect is implemented
243 //RemotingServices.Disconnect(objMarshal);
247 public void Marshal2()
249 DerivedMarshalObject derivedObjMarshal
= NewDerivedMarshalObject();
251 ObjRef objRef
= RemotingServices
.Marshal(derivedObjMarshal
, derivedObjMarshal
.Uri
, typeof(MarshalObject
));
253 // Check that the type of the marshaled object is MarshalObject
254 Assert("#A05", objRef
.TypeInfo
.TypeName
.StartsWith((typeof(MarshalObject
)).ToString()));
256 // TODO: uncomment when RemotingServices.Disconnect is implemented
257 //RemotingServices.Disconnect(derivedObjMarshal);
260 // Tests RemotingServices.GetObjectUri()
262 public void GetObjectUri()
264 MarshalObject objMarshal
= NewMarshalObject();
266 Assert("#A06", RemotingServices
.GetObjectUri(objMarshal
) == null);
268 ObjRef objRef
= RemotingServices
.Marshal(objMarshal
);
270 Assert("#A07", RemotingServices
.GetObjectUri(objMarshal
) != null);
271 // TODO: uncomment when RemotingServices.Disconnect is implemented
272 //RemotingServices.Disconnect(objMarshal);
275 // Tests RemotingServices.Connect
277 public void Connect()
279 MarshalObject objMarshal
= NewMarshalObject();
281 IDictionary props
= new Hashtable();
282 props
["name"] = objMarshal
.Uri
;
283 props
["port"] = 1236;
284 TcpChannel chn
= new TcpChannel(props
, null, null);
285 ChannelServices
.RegisterChannel(chn
);
287 RemotingServices
.Marshal(objMarshal
,objMarshal
.Uri
);
289 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect(typeof(MarshalObject
), "tcp://localhost:1236/" + objMarshal
.Uri
);
291 Assert("#A08", RemotingServices
.IsTransparentProxy(objRem
));
293 ChannelServices
.UnregisterChannel(chn
);
295 // TODO: uncomment when RemotingServices.Disconnect is implemented
296 //RemotingServices.Disconnect(objMarshal);
299 // Tests RemotingServices.Marshal()
301 [ExpectedException(typeof(RemotingException
))]
302 public void MarshalThrowException()
304 MarshalObject objMarshal
= NewMarshalObject();
306 IDictionary props
= new Hashtable();
307 props
["name"] = objMarshal
.Uri
;
308 props
["port"] = 1237;
309 TcpChannel chn
= new TcpChannel(props
, null, null);
310 ChannelServices
.RegisterChannel(chn
);
312 RemotingServices
.Marshal(objMarshal
,objMarshal
.Uri
);
314 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect(typeof(MarshalObject
), "tcp://localhost:1237/" + objMarshal
.Uri
);
315 // This line sould throw a RemotingException
316 // It is forbidden to export an object which is not
320 RemotingServices
.Marshal(objRem
, objMarshal
.Uri
);
324 ChannelServices
.UnregisterChannel(chn
);
326 // TODO: uncomment when RemotingServices.Disconnect is implemented
327 //RemotingServices.Disconnect(objMarshal);
333 // Tests RemotingServices.ExecuteMessage()
334 // also tests GetMethodBaseFromMessage()
335 // IsMethodOverloaded()
337 public void ExecuteMessage()
339 TcpChannel chn
= null;
342 chn
= new TcpChannel(1235);
343 ChannelServices
.RegisterChannel(chn
);
345 MarshalObject objMarshal
= NewMarshalObject();
346 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), objMarshal
.Uri
, WellKnownObjectMode
.SingleCall
);
348 // use a proxy to catch the Message
349 MyProxy proxy
= new MyProxy(typeof(MarshalObject
), (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1235/" + objMarshal
.Uri
));
351 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy();
355 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
356 AssertEquals("#A09","Method1",proxy
.MthBase
.Name
);
357 Assert("#A09.1", !proxy
.IsMethodOverloaded
);
360 Assert("#A09.2", proxy
.IsMethodOverloaded
);
362 // Tests RemotingServices.ExecuteMessage();
363 // If ExecuteMessage does it job well, Method1 should be called 2 times
364 AssertEquals("#A10", 2, MarshalObject
.Called
);
368 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
372 // Tests the IsOneWay method
374 public void IsOneWay()
376 TcpChannel chn
= null;
379 chn
= new TcpChannel(1238);
380 ChannelServices
.RegisterChannel(chn
);
381 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MarshalObject.rem", WellKnownObjectMode
.Singleton
);
383 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1238/MarshalObject.rem");
385 Assert("#A10.1", RemotingServices
.IsTransparentProxy(objRem
));
389 Assert("#A10.2", !MarshalObject
.IsMethodOneWay
);
392 Assert("#A10.2", MarshalObject
.IsMethodOneWay
);
396 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
401 public void GetObjRefForProxy()
403 TcpChannel chn
= null;
406 chn
= new TcpChannel(1239);
407 ChannelServices
.RegisterChannel(chn
);
409 // Register le factory as a SAO
410 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObjectFactory
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode
.Singleton
);
412 MarshalObjectFactory objFactory
= (MarshalObjectFactory
) Activator
.GetObject(typeof(MarshalObjectFactory
), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
415 MarshalObject objRem
= objFactory
.GetNewMarshalObject();
417 ObjRef objRefRem
= RemotingServices
.GetObjRefForProxy((MarshalByRefObject
)objRem
);
419 Assert("#A11", objRefRem
!= null);
423 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
427 // Tests GetRealProxy
429 public void GetRealProxy()
431 TcpChannel chn
= null;
434 chn
= new TcpChannel(1241);
435 ChannelServices
.RegisterChannel(chn
);
437 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode
.Singleton
);
439 MyProxy proxy
= new MyProxy(typeof(MarshalObject
), (MarshalByRefObject
)Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
440 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy();
442 RealProxy rp
= RemotingServices
.GetRealProxy(objRem
);
444 Assert("#A12", rp
!= null);
445 AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp
.GetType().ToString());
449 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
453 // Tests SetObjectUriForMarshal()
455 public void SetObjectUriForMarshal()
457 TcpChannel chn
= null;
460 chn
= new TcpChannel(1242);
461 ChannelServices
.RegisterChannel(chn
);
463 MarshalObject objRem
= NewMarshalObject();
464 RemotingServices
.SetObjectUriForMarshal(objRem
, objRem
.Uri
);
465 RemotingServices
.Marshal(objRem
);
467 objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1242/"+objRem
.Uri
);
468 Assert("#A14", objRem
!= null);
472 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
477 // Tests GetServeurTypeForUri()
479 public void GetServeurTypeForUri()
481 TcpChannel chn
= null;
482 Type type
= typeof(MarshalObject
);
485 chn
= new TcpChannel(1243);
486 ChannelServices
.RegisterChannel(chn
);
488 MarshalObject objRem
= NewMarshalObject();
489 RemotingServices
.SetObjectUriForMarshal(objRem
, objRem
.Uri
);
490 RemotingServices
.Marshal(objRem
);
492 Type typeRem
= RemotingServices
.GetServerTypeForUri(RemotingServices
.GetObjectUri(objRem
));
493 AssertEquals("#A15", type
, typeRem
);
497 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
501 // Tests IsObjectOutOfDomain
502 // Tests IsObjectOutOfContext
504 public void IsObjectOutOf()
506 TcpChannel chn
= null;
509 chn
= new TcpChannel(1245);
510 ChannelServices
.RegisterChannel(chn
);
512 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MarshalObject.rem", WellKnownObjectMode
.Singleton
);
514 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1245/MarshalObject.rem");
516 Assert("#A16", RemotingServices
.IsObjectOutOfAppDomain(objRem
));
517 Assert("#A17", RemotingServices
.IsObjectOutOfContext(objRem
));
519 MarshalObject objMarshal
= new MarshalObject();
520 Assert("#A18", !RemotingServices
.IsObjectOutOfAppDomain(objMarshal
));
521 Assert("#A19", !RemotingServices
.IsObjectOutOfContext(objMarshal
));
525 ChannelServices
.UnregisterChannel(chn
);
528 } // end class RemotingServicesTest
529 } // end of namespace MonoTests.System.Runtime.Remoting.RemotingServicesTest