2010-05-25 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Remoting / Test / GenericTest.cs
blobf755e4386a661cadfdf0d6fb396a78c53293849c
1 //
2 // MonoTests.Remoting.GenericTest.cs
3 //
4 // Authors:
5 // Robert Jordan <robertj@gmx.net>
6 //
8 #if NET_2_0
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;
20 namespace MonoTests.Remoting
22 public interface INested
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);
31 public interface ITest
33 V TestIface<V> (V v);
34 int TestDirectIfaceImpl (int i);
35 INested GetNested ();
36 INested GetNestedMbr ();
39 public class ServerBase<T> : MarshalByRefObject, ITest
41 public virtual V TestVirt<V> (V v)
43 return default (V);
46 public V TestIface<V> (V v)
48 return v;
51 int ITest.TestDirectIfaceImpl (int i)
53 return i;
56 public int TestDirectIfaceImpl (int i)
58 return -1;
61 public INested GetNested ()
63 return new Nested ();
66 public INested GetNested (string s)
68 return new Nested ();
71 public INested GetNestedMbr ()
73 return new NestedMbr ();
77 public class Server<T> : ServerBase<T>
79 public override V TestVirt<V> (V v)
81 return v;
85 [Serializable]
86 public class Nested : INested
88 public int Test ()
90 return 47;
93 int INested.Test ()
95 return 42;
98 public int Test (int i)
100 return i + 500;
103 int INested.Test (int a, int b)
105 return a + b;
108 public V Test <V> (V v)
110 return v;
113 V INested.Test <V, T> (V v, T t)
115 return default (V);
119 public class NestedMbr : MarshalByRefObject, INested
121 public int Test ()
123 return 47;
126 int INested.Test ()
128 return 42;
131 public int Test (int i)
133 return i + 500;
136 int INested.Test (int a, int b)
138 return a + b;
141 public V Test <V> (V v)
143 return v;
146 V INested.Test <V, T> (V v, T t)
148 return default (V);
153 [TestFixture]
154 public class GenericTest
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 ()
164 RunTests (RegisterAndConnect <Server<object>> ());
167 [Test]
168 public void TestTcpChannel ()
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);
176 try {
177 Register <Server<object>> ("gentcptest.rem");
178 RunTests (Connect <Server<object>> ("tcp://localhost:18191/gentcptest.rem"));
179 } finally {
180 ChannelServices.UnregisterChannel (chan);
184 static T RegisterAndConnect <T> () where T: MarshalByRefObject
186 AppDomain d = BaseCallTest.CreateDomain ("GenericTests");
187 return (T) d.CreateInstanceAndUnwrap (
188 typeof (T).Assembly.FullName,
189 typeof (T).FullName);
192 static void Register <T> (string uri) where T: MarshalByRefObject
194 object obj = Activator.CreateInstance (typeof(T));
195 RemotingServices.Marshal ((MarshalByRefObject)obj, uri);
198 static T Connect <T> (string uri) where T: MarshalByRefObject
200 return (T) RemotingServices.Connect (typeof (T), uri);
203 static void RunTests (ServerBase<object> rem)
205 Assert.AreEqual (42, rem.TestIface<int>(42),
206 "#1 calling TestIface on object instance");
208 Assert.AreEqual (42, rem.TestVirt<int>(42),
209 "#2 calling TestVirt");
211 ITest i = rem;
212 Assert.AreEqual (42, i.TestIface<int>(42),
213 "#3 calling TestIface on interface");
215 Assert.AreEqual (42, i.TestDirectIfaceImpl (42),
216 "#4 calling TestDirectIfaceImp");
218 INested cao = rem.GetNested ();
219 Assert.AreEqual (42, cao.Test (),
220 "#5a calling INested.Test ()");
222 Assert.AreEqual (42 + 500, cao.Test (42),
223 "#5 calling INested.Test (int)");
225 Assert.AreEqual (42, cao.Test (21, 21),
226 "#6 calling INested.Test (int, int)");
228 Assert.AreEqual (42, cao.Test<int> (42),
229 "#7 calling INested.Test<V>");
231 Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
232 "#8 calling INested.Test<V, T>");
234 cao = rem.GetNestedMbr ();
235 Assert.AreEqual (42, cao.Test (),
236 "#9a calling INested.Test ()");
238 Assert.AreEqual (42 + 500, cao.Test (42),
239 "#9 calling INested.Test (int)");
241 Assert.AreEqual (42, cao.Test (21, 21),
242 "#10 calling INested.Test (int, int)");
244 Assert.AreEqual (42, cao.Test<int> (42),
245 "#11 calling INested.Test<V>");
247 Assert.AreEqual (0, cao.Test<int, string> (42, "bar"),
248 "#12 calling INested.Test<V, T>");
253 #endif