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