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