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
)
69 if(msg
is IConstructionCallMessage
)
71 IActivator remActivator
= (IActivator
) RemotingServices
.Connect(typeof(IActivator
), "tcp://localhost:1234/RemoteActivationService.rem");
72 IConstructionReturnMessage crm
= remActivator
.Activate((IConstructionCallMessage
)msg
);
77 methodOverloaded
= RemotingServices
.IsMethodOverloaded((IMethodMessage
)msg
);
79 _mthBase
= RemotingServices
.GetMethodBaseFromMethodMessage((IMethodMessage
)msg
);
80 MethodCallMessageWrapper mcm
= new MethodCallMessageWrapper((IMethodCallMessage
) msg
);
81 mcm
.Uri
= RemotingServices
.GetObjectUri((MarshalByRefObject
)target
);
82 MarshalByRefObject objRem
= (MarshalByRefObject
)Activator
.CreateInstance(GetProxiedType());
83 RemotingServices
.ExecuteMessage((MarshalByRefObject
)objRem
, (IMethodCallMessage
)msg
);
84 IMessage rtnMsg
= null;
88 rtnMsg
= _sink
.SyncProcessMessage(msg
);
92 Console
.WriteLine(e
.Message
);
95 Console
.WriteLine ("RR:" + rtnMsg
);
101 Console
.WriteLine (ex
);
107 // This class is used to create "CAO"
108 public class MarshalObjectFactory
: MarshalByRefObject
110 public MarshalObject
GetNewMarshalObject()
112 return new MarshalObject();
116 // A class used by the tests
117 public class MarshalObject
: ContextBoundObject
119 public MarshalObject()
124 public MarshalObject(int id
, string uri
)
139 public void Method1()
142 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
145 public void Method2()
147 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
150 public void Method2(int i
)
152 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
157 public void Method3()
159 methodOneWay
= RemotingServices
.IsOneWay(MethodBase
.GetCurrentMethod());
163 public static int Called
168 public static bool IsMethodOneWay
170 get{return methodOneWay;}
174 private static int _called
;
177 private static bool methodOneWay
= false;
180 // Another class used by the tests
181 public class DerivedMarshalObject
: MarshalObject
183 public DerivedMarshalObject(){}
185 public DerivedMarshalObject(int id
, string uri
): base(id
, uri
) {}
196 public class CC
: MarshalByRefObject
200 public class DD
: MarshalByRefObject
205 } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
207 namespace MonoTests
.Remoting
209 using MonoTests
.System
.Runtime
.Remoting
.RemotingServicesInternal
;
211 // The main test class
213 public class RemotingServicesTest
: Assertion
215 private static int MarshalObjectId
= 0;
217 public RemotingServicesTest()
222 // Helper function that create a new
223 // MarshalObject with an unique ID
224 private static MarshalObject
NewMarshalObject()
226 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId
.ToString();
227 MarshalObject objMarshal
= new MarshalObject(MarshalObjectId
, uri
);
234 // Another helper function
235 private DerivedMarshalObject
NewDerivedMarshalObject()
237 string uri
= "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId
.ToString();
238 DerivedMarshalObject objMarshal
= new DerivedMarshalObject(MarshalObjectId
, uri
);
245 // The two folling method test RemotingServices.Marshal()
247 public void Marshal1()
250 MarshalObject objMarshal
= NewMarshalObject();
251 ObjRef objRef
= RemotingServices
.Marshal(objMarshal
);
253 Assert("#A01", objRef
.URI
!= null);
255 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Unmarshal(objRef
);
256 AssertEquals("#A02", objMarshal
.Id
, objRem
.Id
);
259 AssertEquals("#A03", objMarshal
.Id
, objRem
.Id
);
261 // TODO: uncomment when RemotingServices.Disconnect is implemented
262 //RemotingServices.Disconnect(objMarshal);
264 objMarshal
= NewMarshalObject();
266 objRef
= RemotingServices
.Marshal(objMarshal
, objMarshal
.Uri
);
268 Assert("#A04", objRef
.URI
.EndsWith(objMarshal
.Uri
));
269 // TODO: uncomment when RemotingServices.Disconnect is implemented
270 //RemotingServices.Disconnect(objMarshal);
274 public void Marshal2()
276 DerivedMarshalObject derivedObjMarshal
= NewDerivedMarshalObject();
278 ObjRef objRef
= RemotingServices
.Marshal(derivedObjMarshal
, derivedObjMarshal
.Uri
, typeof(MarshalObject
));
280 // Check that the type of the marshaled object is MarshalObject
281 Assert("#A05", objRef
.TypeInfo
.TypeName
.StartsWith((typeof(MarshalObject
)).ToString()));
283 // TODO: uncomment when RemotingServices.Disconnect is implemented
284 //RemotingServices.Disconnect(derivedObjMarshal);
287 // Tests RemotingServices.GetObjectUri()
289 public void GetObjectUri()
291 MarshalObject objMarshal
= NewMarshalObject();
293 Assert("#A06", RemotingServices
.GetObjectUri(objMarshal
) == null);
295 ObjRef objRef
= RemotingServices
.Marshal(objMarshal
);
297 Assert("#A07", RemotingServices
.GetObjectUri(objMarshal
) != null);
298 // TODO: uncomment when RemotingServices.Disconnect is implemented
299 //RemotingServices.Disconnect(objMarshal);
302 // Tests RemotingServices.Connect
304 public void Connect()
306 MarshalObject objMarshal
= NewMarshalObject();
308 IDictionary props
= new Hashtable();
309 props
["name"] = objMarshal
.Uri
;
310 props
["port"] = 1236;
311 TcpChannel chn
= new TcpChannel(props
, null, null);
312 ChannelServices
.RegisterChannel(chn
);
314 RemotingServices
.Marshal(objMarshal
,objMarshal
.Uri
);
316 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect(typeof(MarshalObject
), "tcp://localhost:1236/" + objMarshal
.Uri
);
318 Assert("#A08", RemotingServices
.IsTransparentProxy(objRem
));
320 ChannelServices
.UnregisterChannel(chn
);
322 RemotingServices
.Disconnect(objMarshal
);
325 // Tests RemotingServices.Marshal()
327 [ExpectedException(typeof(RemotingException
))]
328 public void MarshalThrowException()
330 MarshalObject objMarshal
= NewMarshalObject();
332 IDictionary props
= new Hashtable();
333 props
["name"] = objMarshal
.Uri
;
334 props
["port"] = 1237;
335 TcpChannel chn
= new TcpChannel(props
, null, null);
336 ChannelServices
.RegisterChannel(chn
);
338 RemotingServices
.Marshal(objMarshal
,objMarshal
.Uri
);
340 MarshalObject objRem
= (MarshalObject
) RemotingServices
.Connect(typeof(MarshalObject
), "tcp://localhost:1237/" + objMarshal
.Uri
);
341 // This line sould throw a RemotingException
342 // It is forbidden to export an object which is not
346 RemotingServices
.Marshal(objRem
, objMarshal
.Uri
);
350 ChannelServices
.UnregisterChannel(chn
);
352 // TODO: uncomment when RemotingServices.Disconnect is implemented
353 //RemotingServices.Disconnect(objMarshal);
359 // Tests RemotingServices.ExecuteMessage()
360 // also tests GetMethodBaseFromMessage()
361 // IsMethodOverloaded()
363 public void ExecuteMessage()
365 TcpChannel chn
= null;
368 chn
= new TcpChannel(1235);
369 ChannelServices
.RegisterChannel(chn
);
371 MarshalObject objMarshal
= NewMarshalObject();
372 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), objMarshal
.Uri
, WellKnownObjectMode
.SingleCall
);
374 // use a proxy to catch the Message
375 MyProxy proxy
= new MyProxy(typeof(MarshalObject
), (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1235/" + objMarshal
.Uri
));
377 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy();
381 // Tests RemotingServices.GetMethodBaseFromMethodMessage()
382 AssertEquals("#A09","Method1",proxy
.MthBase
.Name
);
383 Assert("#A09.1", !proxy
.IsMethodOverloaded
);
386 Assert("#A09.2", proxy
.IsMethodOverloaded
);
388 // Tests RemotingServices.ExecuteMessage();
389 // If ExecuteMessage does it job well, Method1 should be called 2 times
390 AssertEquals("#A10", 2, MarshalObject
.Called
);
394 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
398 // Tests the IsOneWay method
400 public void IsOneWay()
402 TcpChannel chn
= null;
405 chn
= new TcpChannel(1238);
406 ChannelServices
.RegisterChannel(chn
);
407 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MarshalObject.rem", WellKnownObjectMode
.Singleton
);
409 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1238/MarshalObject.rem");
411 Assert("#A10.1", RemotingServices
.IsTransparentProxy(objRem
));
415 Assert("#A10.2", !MarshalObject
.IsMethodOneWay
);
418 Assert("#A10.3", MarshalObject
.IsMethodOneWay
);
422 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
427 public void GetObjRefForProxy()
429 TcpChannel chn
= null;
432 chn
= new TcpChannel(1239);
433 ChannelServices
.RegisterChannel(chn
);
435 // Register le factory as a SAO
436 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObjectFactory
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode
.Singleton
);
438 MarshalObjectFactory objFactory
= (MarshalObjectFactory
) Activator
.GetObject(typeof(MarshalObjectFactory
), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
441 MarshalObject objRem
= objFactory
.GetNewMarshalObject();
443 ObjRef objRefRem
= RemotingServices
.GetObjRefForProxy((MarshalByRefObject
)objRem
);
445 Assert("#A11", objRefRem
!= null);
449 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
453 // Tests GetRealProxy
455 public void GetRealProxy()
457 TcpChannel chn
= null;
460 chn
= new TcpChannel(1241);
461 ChannelServices
.RegisterChannel(chn
);
463 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode
.Singleton
);
465 MyProxy proxy
= new MyProxy(typeof(MarshalObject
), (MarshalByRefObject
)Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
466 MarshalObject objRem
= (MarshalObject
) proxy
.GetTransparentProxy();
468 RealProxy rp
= RemotingServices
.GetRealProxy(objRem
);
470 Assert("#A12", rp
!= null);
471 AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp
.GetType().ToString());
475 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
479 // Tests SetObjectUriForMarshal()
481 public void SetObjectUriForMarshal()
483 TcpChannel chn
= null;
486 chn
= new TcpChannel(1242);
487 ChannelServices
.RegisterChannel(chn
);
489 MarshalObject objRem
= NewMarshalObject();
490 RemotingServices
.SetObjectUriForMarshal(objRem
, objRem
.Uri
);
491 RemotingServices
.Marshal(objRem
);
493 objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1242/"+objRem
.Uri
);
494 Assert("#A14", objRem
!= null);
498 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
503 // Tests GetServeurTypeForUri()
505 public void GetServeurTypeForUri()
507 TcpChannel chn
= null;
508 Type type
= typeof(MarshalObject
);
511 chn
= new TcpChannel(1243);
512 ChannelServices
.RegisterChannel(chn
);
514 MarshalObject objRem
= NewMarshalObject();
515 RemotingServices
.SetObjectUriForMarshal(objRem
, objRem
.Uri
);
516 RemotingServices
.Marshal(objRem
);
518 Type typeRem
= RemotingServices
.GetServerTypeForUri(RemotingServices
.GetObjectUri(objRem
));
519 AssertEquals("#A15", type
, typeRem
);
523 if(chn
!= null) ChannelServices
.UnregisterChannel(chn
);
527 // Tests IsObjectOutOfDomain
528 // Tests IsObjectOutOfContext
530 public void IsObjectOutOf()
532 TcpChannel chn
= null;
535 chn
= new TcpChannel(1245);
536 ChannelServices
.RegisterChannel(chn
);
538 RemotingConfiguration
.RegisterWellKnownServiceType(typeof(MarshalObject
), "MarshalObject2.rem", WellKnownObjectMode
.Singleton
);
540 MarshalObject objRem
= (MarshalObject
) Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1245/MarshalObject2.rem");
542 Assert("#A16", RemotingServices
.IsObjectOutOfAppDomain(objRem
));
543 Assert("#A17", RemotingServices
.IsObjectOutOfContext(objRem
));
545 MarshalObject objMarshal
= new MarshalObject();
546 Assert("#A18", !RemotingServices
.IsObjectOutOfAppDomain(objMarshal
));
547 Assert("#A19", !RemotingServices
.IsObjectOutOfContext(objMarshal
));
551 ChannelServices
.UnregisterChannel(chn
);
556 public void ConnectProxyCast ()
559 RemotingConfiguration
.Configure (null);
561 o
= RemotingServices
.Connect (typeof(MarshalByRefObject
), "tcp://localhost:3434/ff1.rem");
562 Assert ("#m1", o
is DD
);
563 Assert ("#m2", o
is A
);
564 Assert ("#m3", o
is B
);
565 Assert ("#m4", !(o
is CC
));
567 o
= RemotingServices
.Connect (typeof(A
), "tcp://localhost:3434/ff3.rem");
568 Assert ("#a1", o
is DD
);
569 Assert ("#a2", o
is A
);
570 Assert ("#a3", o
is B
);
571 Assert ("#a4", !(o
is CC
));
573 o
= RemotingServices
.Connect (typeof(DD
), "tcp://localhost:3434/ff4.rem");
574 Assert ("#d1", o
is DD
);
575 Assert ("#d2", o
is A
);
576 Assert ("#d3", o
is B
);
577 Assert ("#d4", !(o
is CC
));
579 o
= RemotingServices
.Connect (typeof(CC
), "tcp://localhost:3434/ff5.rem");
580 Assert ("#c1", !(o
is DD
));
581 Assert ("#c2", o
is A
);
582 Assert ("#c3", o
is B
);
583 Assert ("#c4", o
is CC
);
586 } // end class RemotingServicesTest
587 } // end of namespace MonoTests.Remoting