updating to the latest module.
[mono.git] / mcs / class / corlib / Test / System / MarshalByRefObjectTest.cs
1 // MarshalByRefObjectTest.cs Test class for\r
2 // System.MarshalByRefObject class\r
3 // \r
4 // Jean-Marc Andre (jean-marc.andre@polymtl.ca)\r
5 //\r
6 \r
7 using System;\r
8 using System.Runtime.Remoting;\r
9 using System.Runtime.Remoting.Lifetime;\r
10 using System.Runtime.Serialization;\r
11 using System.Runtime.Remoting.Channels;\r
12 using System.Runtime.Remoting.Channels.Tcp;\r
13 \r
14 // Just an internal test namespace for\r
15 // the MarshalByRefObjectTest class\r
16 namespace MonoTests.System.MarshalByRefObjectTestInternal\r
17   {\r
18 \r
19   // Object from this class can be marshaled\r
20   public class MarshalObject: MarshalByRefObject\r
21     {\r
22     public MarshalObject(){}\r
23 \r
24     public MarshalObject(int id)\r
25       {\r
26       this.id = id;\r
27       }\r
28 \r
29     // This method override the default one\r
30     // so we can set some properties of the lifetime\r
31     // of the remot object\r
32     public override object InitializeLifetimeService()\r
33       {\r
34       ILease lease = (ILease) base.InitializeLifetimeService();\r
35 \r
36       // By default InitialLeaseTime is set to 5 minutes\r
37       // we set it at 10 seconds\r
38       if(lease.CurrentState == LeaseState.Initial)\r
39         {\r
40         lease.InitialLeaseTime = TimeSpan.FromSeconds(10);\r
41         }\r
42       return lease;\r
43       }\r
44 \r
45     public int Id\r
46       {\r
47       get { return id;}\r
48       }\r
49 \r
50     private int id = 0;\r
51     } // MarshalObject\r
52 \r
53   } // MonoTests.System.MarshalByRefObjectTestInternal\r
54 \r
55 \r
56 namespace MonoTests.System\r
57   {\r
58   using MonoTests.System.MarshalByRefObjectTestInternal;\r
59   using NUnit.Framework;\r
60 \r
61   // The main test class\r
62   [TestFixture]\r
63   public class MarshalByRefObjectTest\r
64     {\r
65     public MarshalByRefObjectTest()\r
66       {\r
67 \r
68       }\r
69 \r
70     // This method test the CreateObjRef\r
71     [Test]\r
72       public void CreateObjRef()\r
73       {\r
74       MarshalObject objMarshal = new MarshalObject();\r
75 \r
76       RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal1");\r
77       RemotingServices.Marshal(objMarshal);\r
78 \r
79       ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));\r
80       Assertion.AssertEquals("#A01", objRef.URI, RemotingServices.GetObjectUri(objMarshal));\r
81 \r
82       // TODO: When implemented in the mono RemotingServices class\r
83       //RemotingServices.Disconnect(objMarshal);\r
84       }\r
85 \r
86     [Test]\r
87       [ExpectedException(typeof(RemotingException))]\r
88       public void CreateObjRefThrowException()\r
89       {\r
90       MarshalObject objMarshal = new MarshalObject();\r
91 \r
92       ObjRef objRef = objMarshal.CreateObjRef(typeof(MarshalObject));\r
93       }\r
94 \r
95     // This method both tests InitializeLifetimeService()\r
96     // and GetLifetimeService()\r
97     [Test]\r
98       public void LifetimeService()\r
99       {\r
100       MarshalObject objMarshal = new MarshalObject();\r
101 \r
102       RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal2");\r
103       RemotingServices.Marshal(objMarshal);\r
104       \r
105       objMarshal.InitializeLifetimeService();\r
106       ILease lease = (ILease) objMarshal.GetLifetimeService();\r
107       Assertion.AssertEquals("#A02", lease.InitialLeaseTime, TimeSpan.FromSeconds(10));\r
108       \r
109       // TODO: When implemented in the mono RemotingServices class\r
110       //RemotingServices.Disconnect(objMarshal);\r
111       }\r
112 \r
113     // Here we test if we a published object can be get \r
114     // through a TcpChannel\r
115     [Test]\r
116       public void GetObject()\r
117       {\r
118       MarshalObject objMarshal = new MarshalObject(1);\r
119 \r
120       RemotingServices.SetObjectUriForMarshal(objMarshal, "MarshalByRefObjectTest.objMarshal3");\r
121       RemotingServices.Marshal(objMarshal);\r
122 \r
123       TcpChannel chn = new TcpChannel(1294);\r
124       ChannelServices.RegisterChannel(chn);\r
125       \r
126       object objRem = Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1294/MarshalByRefObjectTest.objMarshal3");\r
127 \r
128       MarshalObject objMarshalRem = (MarshalObject) objRem;\r
129 \r
130       Assertion.AssertEquals("#A03", 1, objMarshalRem.Id);\r
131 \r
132       // TODO: When implemented in the mono RemotingServices class\r
133       //RemotingServices.Disconnect(objMarshal);\r
134 //      chn.StopListening(null);\r
135       ChannelServices.UnregisterChannel(chn);\r
136 \r
137       }\r
138     }\r
139   }\r