1 // MarshalByRefObjectTest.cs Test class for
2 // System.MarshalByRefObject class
4 // Jean-Marc Andre (jean-marc.andre@polymtl.ca)
8 using System
.Runtime
.Remoting
;
9 using System
.Runtime
.Remoting
.Lifetime
;
10 using System
.Runtime
.Serialization
;
11 using System
.Runtime
.Remoting
.Channels
;
12 using System
.Runtime
.Remoting
.Channels
.Tcp
;
14 // Just an internal test namespace for
15 // the MarshalByRefObjectTest class
16 namespace MonoTests
.System
.MarshalByRefObjectTestInternal
19 // Object from this class can be marshaled
20 public class MarshalObject
: MarshalByRefObject
22 public MarshalObject(){}
24 public MarshalObject(int id
)
29 // This method override the default one
30 // so we can set some properties of the lifetime
31 // of the remot object
32 public override object InitializeLifetimeService()
34 ILease lease
= (ILease
) base.InitializeLifetimeService();
36 // By default InitialLeaseTime is set to 5 minutes
37 // we set it at 10 seconds
38 if(lease
.CurrentState
== LeaseState
.Initial
)
40 lease
.InitialLeaseTime
= TimeSpan
.FromSeconds(10);
53 } // MonoTests.System.MarshalByRefObjectTestInternal
56 namespace MonoTests
.System
58 using MonoTests
.System
.MarshalByRefObjectTestInternal
;
59 using NUnit
.Framework
;
61 // The main test class
63 public class MarshalByRefObjectTest
65 public MarshalByRefObjectTest()
70 // This method test the CreateObjRef
72 public void CreateObjRef()
74 MarshalObject objMarshal
= new MarshalObject();
76 RemotingServices
.SetObjectUriForMarshal(objMarshal
, "MarshalByRefObjectTest.objMarshal1");
77 RemotingServices
.Marshal(objMarshal
);
79 ObjRef objRef
= objMarshal
.CreateObjRef(typeof(MarshalObject
));
80 Assert
.AreEqual(objRef
.URI
, RemotingServices
.GetObjectUri(objMarshal
), "#A01");
82 // TODO: When implemented in the mono RemotingServices class
83 //RemotingServices.Disconnect(objMarshal);
87 [ExpectedException(typeof(RemotingException
))]
88 public void CreateObjRefThrowException()
90 MarshalObject objMarshal
= new MarshalObject();
92 ObjRef objRef
= objMarshal
.CreateObjRef(typeof(MarshalObject
));
95 // This method both tests InitializeLifetimeService()
96 // and GetLifetimeService()
98 public void LifetimeService()
100 MarshalObject objMarshal
= new MarshalObject();
102 RemotingServices
.SetObjectUriForMarshal(objMarshal
, "MarshalByRefObjectTest.objMarshal2");
103 RemotingServices
.Marshal(objMarshal
);
105 objMarshal
.InitializeLifetimeService();
106 ILease lease
= (ILease
) objMarshal
.GetLifetimeService();
107 Assert
.AreEqual(lease
.InitialLeaseTime
, TimeSpan
.FromSeconds(10), "#A02");
109 // TODO: When implemented in the mono RemotingServices class
110 //RemotingServices.Disconnect(objMarshal);
113 // Here we test if we a published object can be get
114 // through a TcpChannel
116 public void GetObject()
118 MarshalObject objMarshal
= new MarshalObject(1);
120 RemotingServices
.SetObjectUriForMarshal(objMarshal
, "MarshalByRefObjectTest.objMarshal3");
121 RemotingServices
.Marshal(objMarshal
);
123 TcpChannel chn
= new TcpChannel(1294);
124 ChannelServices
.RegisterChannel(chn
);
126 object objRem
= Activator
.GetObject(typeof(MarshalObject
), "tcp://localhost:1294/MarshalByRefObjectTest.objMarshal3");
128 MarshalObject objMarshalRem
= (MarshalObject
) objRem
;
130 Assert
.AreEqual(1, objMarshalRem
.Id
, "#A03");
132 // TODO: When implemented in the mono RemotingServices class
133 //RemotingServices.Disconnect(objMarshal);
134 // chn.StopListening(null);
135 ChannelServices
.UnregisterChannel(chn
);