2009-05-24 Robert Jordan <robertj@gmx.net>
[mono.git] / mcs / class / System.Runtime.Remoting / Test / GenericTest.cs
1 //
2 // MonoTests.Remoting.GenericTest.cs
3 //
4 // Authors:
5 //     Robert Jordan  <robertj@gmx.net>
6 //
7
8 #if NET_2_0
9
10 using System;
11 using System.Collections;
12 using System.Runtime.Remoting;
13 using System.Runtime.Remoting.Channels;
14 using System.Runtime.Remoting.Channels.Tcp;
15 using System.Runtime.Remoting.Channels.Http;
16 using System.Runtime.Remoting.Channels.Ipc;
17 using System.Threading;
18 using NUnit.Framework;
19
20 namespace MonoTests.Remoting
21 {
22         public interface INested
23         {
24                 int Test ();
25                 int Test (int i);
26                 int Test (int a, int b);
27                 V Test <V> (V v);
28                 V Test <V, T> (V v, T t);
29         }
30
31         public interface ITest
32         {
33                 V TestIface<V> (V v);
34                 int TestDirectIfaceImpl (int i);
35                 INested GetNested ();
36                 INested GetNestedMbr ();
37         }
38
39         public class ServerBase<T> : MarshalByRefObject, ITest
40         {
41                 public virtual V TestVirt<V> (V v)
42                 {
43                         return default (V);
44                 }
45
46                 public V TestIface<V> (V v)
47                 {
48                         return v;
49                 }
50
51                 int ITest.TestDirectIfaceImpl (int i)
52                 {
53                         return i;
54                 }
55
56                 public int TestDirectIfaceImpl (int i)
57                 {
58                         return -1;
59                 }
60
61                 public INested GetNested ()
62                 {
63                         return new Nested ();
64                 }
65
66                 public INested GetNested (string s)
67                 {
68                         return new Nested ();
69                 }
70
71                 public INested GetNestedMbr ()
72                 {
73                         return new NestedMbr ();
74                 }
75         }
76
77         public class Server<T> : ServerBase<T>
78         {
79                 public override V TestVirt<V> (V v)
80                 {
81                         return v;
82                 }
83         }
84
85         [Serializable]
86         public class Nested : INested
87         {
88                 public int Test ()
89                 {
90                         return 47;
91                 }
92
93                 int INested.Test ()
94                 {
95                         return 42;
96                 }
97                 
98                 public int Test (int i)
99                 {
100                         return i + 500;
101                 }
102
103                 int INested.Test (int a, int b)
104                 {
105                         return a + b;
106                 }
107
108                 public V Test <V> (V v)
109                 {
110                         return v;
111                 }
112
113                 V INested.Test <V, T> (V v, T t)
114                 {
115                         return default (V);
116                 }
117         }
118
119         public class NestedMbr : MarshalByRefObject, INested
120         {
121                 public int Test ()
122                 {
123                         return 47;
124                 }
125                 
126                 int INested.Test ()
127                 {
128                         return 42;
129                 }
130
131                 public int Test (int i)
132                 {
133                         return i + 500;
134                 }
135
136                 int INested.Test (int a, int b)
137                 {
138                         return a + b;
139                 }
140
141                 public V Test <V> (V v)
142                 {
143                         return v;
144                 }
145
146                 V INested.Test <V, T> (V v, T t)
147                 {
148                         return default (V);
149                 }
150         }
151
152
153         [TestFixture]
154         public class GenericTest
155         {
156                 // Under MS.NET, INested.Test<V>(V v) isn't supported over the
157                 // xappdom channel anymore (as of .NET 3.5). The stacktrace
158                 // looks like if INested.Test(int) is invoked in place of
159                 // INested.Test<int>(int).
160                 [Category("NotDotNet")]
161                 [Test]
162                 public void TestCrossAppDomainChannel ()
163                 {
164                         RunTests (RegisterAndConnect <Server<object>> ());
165                 }
166
167                 [Test]
168                 public void TestTcpChannel ()
169                 {
170                         IDictionary props = new Hashtable ();
171                         props ["name"] = Guid.NewGuid ().ToString("N");
172                         props ["port"] = 18191;
173                         TcpChannel chan = new TcpChannel (props, null, null);
174                         ChannelServices.RegisterChannel (chan);
175                         
176                         try {
177                                 Register <Server<object>> ("gentcptest.rem");
178                                 RunTests (Connect <Server<object>> ("tcp://localhost:18191/gentcptest.rem"));
179                         } finally {
180                                 ChannelServices.UnregisterChannel (chan);
181                         }
182                 }
183
184                 static T RegisterAndConnect <T> () where T: MarshalByRefObject
185                 {
186                         AppDomain d = BaseCallTest.CreateDomain ("GenericTests");
187                         return (T) d.CreateInstanceAndUnwrap (
188                                 typeof (T).Assembly.FullName,
189                                 typeof (T).FullName);
190                 }
191
192                 static void Register <T> (string uri) where T: MarshalByRefObject
193                 {
194                         object obj = Activator.CreateInstance (typeof(T));
195                         RemotingServices.Marshal ((MarshalByRefObject)obj, uri);
196                 }
197
198                 static T Connect <T> (string uri) where T: MarshalByRefObject
199                 {
200                         return (T) RemotingServices.Connect (typeof (T), uri);
201                 }
202
203                 static void RunTests (ServerBase<object> rem)
204                 {
205                         Assert.AreEqual (42, rem.TestIface<int>(42),
206                                          "#1 calling TestIface on object instance");
207
208                         Assert.AreEqual (42, rem.TestVirt<int>(42),
209                                          "#2 calling TestVirt");
210
211                         ITest i = rem;
212                         Assert.AreEqual (42, i.TestIface<int>(42),
213                                          "#3 calling TestIface on interface");
214
215                         Assert.AreEqual (42, i.TestDirectIfaceImpl (42),
216                                          "#4 calling TestDirectIfaceImp");
217
218                         INested cao = rem.GetNested ();
219                         Assert.AreEqual (42, cao.Test (),
220                                          "#5a calling INested.Test ()");
221
222                         Assert.AreEqual (42 + 500, cao.Test (42),
223                                          "#5 calling INested.Test (int)");
224
225                         Assert.AreEqual (42, cao.Test (21, 21),
226                                          "#6 calling INested.Test (int, int)");
227
228                         Assert.AreEqual (42, cao.Test<int> (42),
229                                          "#7 calling INested.Test<V>");
230
231                         Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
232                                          "#8 calling INested.Test<V, T>");
233
234                         cao = rem.GetNestedMbr ();
235                         Assert.AreEqual (42, cao.Test (),
236                                          "#9a calling INested.Test ()");
237
238                         Assert.AreEqual (42 + 500, cao.Test (42),
239                                          "#9 calling INested.Test (int)");
240
241                         Assert.AreEqual (42, cao.Test (21, 21),
242                                          "#10 calling INested.Test (int, int)");
243
244                         Assert.AreEqual (42, cao.Test<int> (42),
245                                          "#11 calling INested.Test<V>");
246
247                         Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
248                                          "#12 calling INested.Test<V, T>");
249                 }
250         }
251 }
252
253 #endif