In Test/System.Data:
[mono-project.git] / mono / tests / cross-domain.cs
blob9632676d518c7e3f09e9275b12fd4ba05264bb22
1 using System;
2 using System.Net;
3 using System.Runtime.Remoting;
4 using System.Runtime.InteropServices;
5 using System.Text;
6 using System.Runtime.Serialization;
8 public class Test: MarshalByRefObject
9 {
10 public static int Main (string[] args)
12 AppDomain domain = AppDomain.CreateDomain ("testdomain1");
13 Test server = (Test) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, "Test");
14 return server.RunTest ();
17 public int RunTest ()
19 try
21 object t = null;
22 string s = (string)t;
23 AppDomain domain = AppDomain.CreateDomain ("testdomain");
24 Remo server = (Remo) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName,"Remo");
25 if (System.Threading.Thread.GetDomainID () == server.GetDomainId ())
26 throw new TestException ("Object not created in new domain", 1);
28 Dada d = new Dada ();
29 d.p = 22;
31 server.Run ();
32 server.Run2 (88, "hola");
33 server.Run3 (99, d, "adeu");
35 string r = server.Run4 (200, d, "que");
36 CheckValue (r, "vist", 140);
38 try {
39 server.Run5 (200, d, "que");
40 throw new TestException ("Exception expected", 150);
42 catch (Exception ex) {
43 CheckValue (ex.Message, "peta", 151);
46 Dada d2;
47 d = server.Run6 (99, out d2, "adeu");
48 CheckValue (d.p, 987, 161);
49 CheckValue (d2.p, 987, 162);
51 d.p = 22;
52 d2 = server.Run7 (d);
53 CheckValue (d.p, 22, 170);
54 CheckValue (d2.p, 33, 170);
56 byte[] ba = new byte[5];
57 for (byte n=0; n<ba.Length; n++)
58 ba [n] = n;
60 server.Run8 (ba);
62 for (int n=0; n<ba.Length; n++)
63 CheckValue (ba[n], (byte) (ba.Length - n), 180);
65 StringBuilder sb = new StringBuilder ("un");
66 server.Run9 (sb);
67 Test.CheckValue (sb, new StringBuilder ("un-dos"), 190);
70 catch (TestException ex)
72 Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
73 return ex.Code;
75 catch (Exception ex)
77 Console.WriteLine ("TEST ERROR: " + ex);
78 return -1;
80 return 0;
83 public static void CheckDomain (object ob, Type t, int ec)
85 if (ob == null) return;
86 if (ob.GetType () != t) {
87 if (t.ToString() == ob.GetType().ToString())
88 throw new TestException ("Parameter not properly marshalled", ec);
89 else
90 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
94 public static void CheckValue (object ob1, object ob2, int ec)
96 if ((ob1 == null || ob2 == null) && ob1 != ob2)
97 throw new TestException ("Null objects are not equal", ec);
99 if (ob2.GetType () != ob1.GetType ())
100 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
102 if (ob1 is StringBuilder) {
103 if (!((StringBuilder)ob1).Equals ((StringBuilder)ob2))
104 throw new TestException ("Objects are not equal", ec);
106 else if (!ob1.Equals (ob2))
107 throw new TestException ("Objects are not equal", ec);
111 public class Remo: MarshalByRefObject
113 int domid;
115 public Remo ()
117 domid = System.Threading.Thread.GetDomainID ();
120 public int GetDomainId ()
122 return domid;
125 public void CheckThisDomain (int ec)
127 if (domid != System.Threading.Thread.GetDomainID ())
128 throw new TestException ("Wrong domain", ec);
131 public void Run ()
133 CheckThisDomain (10);
136 public void Run2 (int a, string b)
138 CheckThisDomain (20);
139 Test.CheckValue (a, 88, 21);
140 Test.CheckValue (b, "hola", 22);
143 public void Run3 (int a, Dada d, string b)
145 CheckThisDomain (30);
146 Test.CheckValue (a, 99, 31);
147 Test.CheckValue (b, "adeu", 32);
148 Test.CheckValue (d.p, 22, 33);
151 public string Run4 (int a, Dada d, string b)
153 CheckThisDomain (40);
154 Test.CheckValue (a, 200, 41);
155 Test.CheckValue (b, "que", 42);
156 Test.CheckValue (d.p, 22, 43);
157 return "vist";
160 public Dada Run5 (int a, Dada d, string b)
162 CheckThisDomain (50);
163 Test.CheckValue (a, 200, 51);
164 Test.CheckValue (b, "que", 52);
165 Test.CheckValue (d.p, 22, 53);
166 Peta ();
167 return d;
170 public Dada Run6 (int a, out Dada d, string b)
172 CheckThisDomain (60);
173 Test.CheckValue (a, 99, 61);
174 Test.CheckValue (b, "adeu", 62);
176 d = new Dada ();
177 d.p = 987;
178 return d;
181 public Dada Run7 (Dada d)
183 CheckThisDomain (70);
184 Test.CheckValue (d.p, 22, 71);
185 d.p = 33;
186 return d;
189 public void Run8 ([In,Out] byte[] bytes)
191 CheckThisDomain (80);
192 Test.CheckDomain (bytes, typeof(byte[]), 81);
193 for (int n=0; n < bytes.Length; n++) {
194 Test.CheckValue (bytes[n], (byte)n, 82);
195 bytes[n] = (byte) (bytes.Length - n);
199 public void Run9 ([In,Out] StringBuilder sb)
201 CheckThisDomain (90);
202 Test.CheckValue (sb, new StringBuilder ("un"), 91);
203 sb.Append ("-dos");
206 public void Peta ()
208 throw new Exception ("peta");
212 [Serializable]
213 public class Dada
215 public int p;
218 [Serializable]
219 public class MyException: Exception
221 public MyException (string s): base (s) {}
224 [Serializable]
225 public class TestException: Exception
227 public int Code = -1;
229 public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
230 Code = i.GetInt32 ("Code");
233 public TestException (string txt, int code): base (txt + " (code: " + code + ")")
235 Code = code;
238 public override void GetObjectData (SerializationInfo info, StreamingContext context)
240 base.GetObjectData (info, context);
241 info.AddValue ("Code", Code);