forked from castleproject/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializableClassTestCase.cs
626 lines (501 loc) · 23 KB
/
SerializableClassTestCase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if FEATURE_SERIALIZATION
namespace Castle.DynamicProxy.Tests
{
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Castle.DynamicProxy.Serialization;
using Castle.DynamicProxy.Tests.Classes;
using Castle.DynamicProxy.Tests.BugsReported;
using Castle.DynamicProxy.Tests.InterClasses;
using Castle.DynamicProxy.Tests.Serialization;
using NUnit.Framework;
[TestFixture]
public class SerializableClassTestCase : BasePEVerifyTestCase
{
[Test]
public void BaseTypeForInterfaceProxy_is_honored_after_deserialization()
{
var options = new ProxyGenerationOptions
{
BaseTypeForInterfaceProxy = typeof(SimpleClass)
};
var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IService), Type.EmptyTypes, options);
var newProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(typeof(SimpleClass), newProxy.GetType().BaseType);
}
[Test(Description = "DYNPROXY-133")]
public void Can_proxy_class_with_explicit_GetObjectData()
{
generator.CreateClassProxy<SerializableExplicitImpl>();
}
[Test]
public void ClassProxyWithTargetSerialization()
{
var proxy = generator.CreateClassProxyWithTarget(new MySerializableClass(), new StandardInterceptor());
var current = proxy.Current;
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(current, otherProxy.Current);
}
[Test]
public void CreateSerializable()
{
var proxy = (MySerializableClass)
generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());
Assert.IsTrue(proxy.GetType().IsSerializable);
}
[Test]
public void CustomMarkerInterface()
{
var proxy = generator.CreateClassProxy(typeof(ClassWithMarkerInterface),
new[] { typeof(IMarkerInterface) },
new StandardInterceptor());
Assert.IsNotNull(proxy);
Assert.IsTrue(proxy is IMarkerInterface);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMarkerInterface);
}
[Test]
public void DeserializationWithSpecificModuleScope()
{
ProxyObjectReference.SetScope(generator.ProxyBuilder.ModuleScope);
var first = generator.CreateClassProxy<MySerializableClass>(new StandardInterceptor());
var second = SerializeAndDeserialize(first);
Assert.AreSame(first.GetType(), second.GetType());
}
[Test]
public void HashtableSerialization()
{
var proxy = generator.CreateClassProxy(
typeof(Hashtable), new StandardInterceptor());
Assert.IsTrue(typeof(Hashtable).IsAssignableFrom(proxy.GetType()));
(proxy as Hashtable).Add("key", "helloooo!");
var otherProxy = (Hashtable)SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy.ContainsKey("key"));
Assert.AreEqual("helloooo!", otherProxy["key"]);
}
[Test]
public void ImplementsISerializable()
{
var proxy = (MySerializableClass)
generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());
Assert.IsTrue(proxy is ISerializable);
}
public override void Init()
{
base.Init();
ProxyObjectReference.ResetScope();
}
[Test]
public void MixinFieldsSetOnDeserialization_ClassProxy()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new SerializableMixin());
var proxy = (MySerializableClass)generator.CreateClassProxy(
typeof(MySerializableClass),
new Type[0],
options,
new StandardInterceptor());
Assert.IsTrue(proxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin);
}
[Test]
public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithTarget()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new SerializableMixin());
var proxy = (IService)generator.CreateInterfaceProxyWithTarget(
typeof(IService),
new ServiceImpl(),
options,
new StandardInterceptor());
Assert.IsTrue(proxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin);
}
[Test]
public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithTargetInterface()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new SerializableMixin());
var proxy = (IService)generator.CreateInterfaceProxyWithTargetInterface(
typeof(IService),
new ServiceImpl(),
options,
new StandardInterceptor());
Assert.IsTrue(proxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin);
}
[Test]
public void MixinFieldsSetOnDeserialization_InterfaceProxy_WithoutTarget()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new SerializableMixin());
var proxy = (IService)generator.CreateInterfaceProxyWithoutTarget(
typeof(IService),
new Type[0],
options,
new StandardInterceptor());
Assert.IsTrue(proxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)proxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)proxy).GetExecutingObject() is SerializableMixin);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMixedInterface);
Assert.IsNotNull(((IMixedInterface)otherProxy).GetExecutingObject());
Assert.IsTrue(((IMixedInterface)otherProxy).GetExecutingObject() is SerializableMixin);
}
[Test]
public void MixinsAppliedOnDeserialization()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new SerializableMixin());
var proxy = (MySerializableClass)generator.CreateClassProxy(
typeof(MySerializableClass),
new Type[0],
options,
new StandardInterceptor());
Assert.IsTrue(proxy is IMixedInterface);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMixedInterface);
}
[Test]
public void ProxyGenerationOptionsRespectedOnDeserialization()
{
var hook = new MethodFilterHook("(get_Current)|(GetExecutingObject)");
var options = new ProxyGenerationOptions(hook);
options.AddMixinInstance(new SerializableMixin());
options.Selector = new SerializableInterceptorSelector();
var proxy = (MySerializableClass)generator.CreateClassProxy(
typeof(MySerializableClass),
new Type[0],
options,
new StandardInterceptor());
Assert.AreEqual(proxy.GetType(), proxy.GetType().GetMethod("get_Current").DeclaringType);
Assert.AreNotEqual(proxy.GetType(), proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
Assert.AreEqual(proxy.GetType().BaseType, proxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
var options2 = (ProxyGenerationOptions)proxy.GetType().
GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; }));
Assert.IsNotNull(options2.Selector);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("get_Current").DeclaringType);
Assert.AreNotEqual(otherProxy.GetType(), otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
Assert.AreEqual(otherProxy.GetType().BaseType,
otherProxy.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
options2 = (ProxyGenerationOptions)otherProxy.GetType().
GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; }));
Assert.IsNotNull(options2.Selector);
}
[Test]
public void ProxyGenerationOptionsRespectedOnDeserializationComplex()
{
var hook = new MethodFilterHook("(get_Current)|(GetExecutingObject)");
var options = new ProxyGenerationOptions(hook);
options.AddMixinInstance(new SerializableMixin());
options.Selector = new SerializableInterceptorSelector();
var holder = new ComplexHolder();
holder.Type = typeof(MySerializableClass);
holder.Element = generator.CreateClassProxy(typeof(MySerializableClass), new Type[0], options,
new StandardInterceptor());
// check holder elements
Assert.AreEqual(typeof(MySerializableClass), holder.Type);
Assert.IsNotNull(holder.Element);
Assert.IsTrue(holder.Element is MySerializableClass);
Assert.AreNotEqual(typeof(MySerializableClass), holder.Element.GetType());
// check whether options were applied correctly
Assert.AreEqual(holder.Element.GetType(), holder.Element.GetType().GetMethod("get_Current").DeclaringType);
Assert.AreNotEqual(holder.Element.GetType(),
holder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
Assert.AreEqual(holder.Element.GetType().BaseType,
holder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
var options2 = (ProxyGenerationOptions)holder.Element.GetType().
GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; }));
Assert.IsNotNull(options2.Selector);
var otherHolder = SerializeAndDeserialize(holder);
// check holder elements
Assert.AreEqual(typeof(MySerializableClass), otherHolder.Type);
Assert.IsNotNull(otherHolder.Element);
Assert.IsTrue(otherHolder.Element is MySerializableClass);
Assert.AreNotEqual(typeof(MySerializableClass), otherHolder.Element.GetType());
// check whether options were applied correctly
Assert.AreEqual(otherHolder.Element.GetType(), otherHolder.Element.GetType().GetMethod("get_Current").DeclaringType);
Assert.AreNotEqual(otherHolder.Element.GetType(),
otherHolder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
Assert.AreEqual(otherHolder.Element.GetType().BaseType,
otherHolder.Element.GetType().GetMethod("CalculateSumDistanceNow").DeclaringType);
options2 = (ProxyGenerationOptions)otherHolder.Element.GetType().
GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
Assert.IsNotNull(Array.Find(options2.MixinsAsArray(), delegate(object o) { return o is SerializableMixin; }));
Assert.IsNotNull(options2.Selector);
}
[Test]
public void ProxyKnowsItsGenerationOptions()
{
var hook = new MethodFilterHook(".*");
var options = new ProxyGenerationOptions(hook);
options.AddMixinInstance(new SerializableMixin());
var proxy = generator.CreateClassProxy(
typeof(MySerializableClass),
new Type[0],
options,
new StandardInterceptor());
var field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic);
Assert.IsNotNull(field);
Assert.AreSame(options, field.GetValue(proxy));
base.Init();
proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(IService), new StandardInterceptor());
field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic);
Assert.AreSame(ProxyGenerationOptions.Default, field.GetValue(proxy));
base.Init();
proxy = generator.CreateInterfaceProxyWithTarget(typeof(IService), new ServiceImpl(), options,
new StandardInterceptor());
field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic);
Assert.AreSame(options, field.GetValue(proxy));
base.Init();
proxy = generator.CreateInterfaceProxyWithTargetInterface(typeof(IService), new ServiceImpl(),
new StandardInterceptor());
field = proxy.GetType().GetField("proxyGenerationOptions", BindingFlags.Static | BindingFlags.NonPublic);
Assert.AreSame(ProxyGenerationOptions.Default, field.GetValue(proxy));
}
[Test]
public void ReusingModuleScopeFromProxyObjectReference()
{
var generatorWithSpecificModuleScope =
new ProxyGenerator(new DefaultProxyBuilder(ProxyObjectReference.ModuleScope));
Assert.AreSame(generatorWithSpecificModuleScope.ProxyBuilder.ModuleScope, ProxyObjectReference.ModuleScope);
var first =
generatorWithSpecificModuleScope.CreateClassProxy<MySerializableClass>(new StandardInterceptor());
var second = SerializeAndDeserialize(first);
Assert.AreSame(first.GetType(), second.GetType());
}
[Test]
public void SerializatingObjectsWithoutDefaultConstructor()
{
var proxy = (C)generator.CreateClassProxy(typeof(C), new object[] { 1 }, new StandardInterceptor());
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(proxy.I, otherProxy.I);
Assert.AreSame(otherProxy, otherProxy.This);
}
[Test]
public void SerializationDelegate()
{
var proxy = (MySerializableClass2)
generator.CreateClassProxy(typeof(MySerializableClass2), new StandardInterceptor());
var current = proxy.Current;
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(current, otherProxy.Current);
}
[Test]
public void SerializeClassWithDirectAndIndirectSelfReference()
{
var proxy =
(ClassWithDirectAndIndirectSelfReference)
generator.CreateClassProxy(typeof(ClassWithDirectAndIndirectSelfReference),
new Type[0], new StandardInterceptor());
Assert.AreSame(proxy, proxy.This);
var otherProxy =
SerializeAndDeserialize(proxy);
Assert.AreSame(otherProxy, otherProxy.List[0]);
Assert.AreSame(otherProxy, otherProxy.This);
}
[Test]
public void SerializeClassWithIndirectSelfReference()
{
var proxy =
(ClassWithIndirectSelfReference)generator.CreateClassProxy(typeof(ClassWithIndirectSelfReference),
new Type[0], new StandardInterceptor());
Assert.AreSame(proxy, proxy.List[0]);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreSame(otherProxy, otherProxy.List[0]);
}
[Test]
public void SerializeObjectsWithDelegateToOtherObject()
{
var eventHandlerInstance = new EventHandlerClass();
var proxy =
(DelegateHolder)generator.CreateClassProxy(typeof(DelegateHolder), new IInterceptor[] { new StandardInterceptor() });
proxy.DelegateMember = new EventHandler(eventHandlerInstance.TestHandler);
proxy.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 });
proxy.ComplexTypeMember.Add(eventHandlerInstance);
Assert.IsNotNull(proxy.DelegateMember);
Assert.IsNotNull(proxy.DelegateMember.Target);
Assert.IsNotNull(proxy.ComplexTypeMember);
Assert.AreEqual(4, proxy.ComplexTypeMember.Count);
Assert.AreEqual(1, proxy.ComplexTypeMember[0]);
Assert.AreEqual(2, proxy.ComplexTypeMember[1]);
Assert.AreEqual(3, proxy.ComplexTypeMember[2]);
Assert.AreSame(proxy.ComplexTypeMember[3], proxy.DelegateMember.Target);
var otherProxy = (SerializeAndDeserialize(proxy));
Assert.IsNotNull(otherProxy.DelegateMember);
Assert.IsNotNull(otherProxy.DelegateMember.Target);
Assert.IsNotNull(otherProxy.ComplexTypeMember);
Assert.AreEqual(4, otherProxy.ComplexTypeMember.Count);
Assert.AreEqual(1, otherProxy.ComplexTypeMember[0]);
Assert.AreEqual(2, otherProxy.ComplexTypeMember[1]);
Assert.AreEqual(3, otherProxy.ComplexTypeMember[2]);
Assert.AreSame(otherProxy.ComplexTypeMember[3], otherProxy.DelegateMember.Target);
}
[Test]
public void SerializeObjectsWithDelegateToThisObject()
{
var proxy =
(DelegateHolder)generator.CreateClassProxy(typeof(DelegateHolder), new IInterceptor[] { new StandardInterceptor() });
proxy.DelegateMember = new EventHandler(proxy.TestHandler);
proxy.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 });
Assert.IsNotNull(proxy.DelegateMember);
Assert.AreSame(proxy, proxy.DelegateMember.Target);
Assert.IsNotNull(proxy.ComplexTypeMember);
Assert.AreEqual(3, proxy.ComplexTypeMember.Count);
Assert.AreEqual(1, proxy.ComplexTypeMember[0]);
Assert.AreEqual(2, proxy.ComplexTypeMember[1]);
Assert.AreEqual(3, proxy.ComplexTypeMember[2]);
var otherProxy = (SerializeAndDeserialize(proxy));
Assert.IsNotNull(otherProxy.DelegateMember);
Assert.AreSame(otherProxy, otherProxy.DelegateMember.Target);
Assert.IsNotNull(otherProxy.ComplexTypeMember);
Assert.AreEqual(3, otherProxy.ComplexTypeMember.Count);
Assert.AreEqual(1, otherProxy.ComplexTypeMember[0]);
Assert.AreEqual(2, otherProxy.ComplexTypeMember[1]);
Assert.AreEqual(3, otherProxy.ComplexTypeMember[2]);
}
[Test]
public void SerializeObjectsWithIndirectDelegateToMember()
{
var proxy = (IndirectDelegateHolder)generator.CreateClassProxy(typeof(IndirectDelegateHolder),
new IInterceptor[] { new StandardInterceptor() });
proxy.DelegateHolder.DelegateMember = new EventHandler(proxy.DelegateHolder.TestHandler);
proxy.DelegateHolder.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 });
Assert.IsNotNull(proxy.DelegateHolder.DelegateMember);
Assert.AreSame(proxy.DelegateHolder, proxy.DelegateHolder.DelegateMember.Target);
Assert.IsNotNull(proxy.DelegateHolder.ComplexTypeMember);
Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember.Count);
Assert.AreEqual(1, proxy.DelegateHolder.ComplexTypeMember[0]);
Assert.AreEqual(2, proxy.DelegateHolder.ComplexTypeMember[1]);
Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember[2]);
var otherProxy = (SerializeAndDeserialize(proxy));
Assert.IsNotNull(otherProxy.DelegateHolder.DelegateMember);
Assert.AreSame(otherProxy.DelegateHolder, otherProxy.DelegateHolder.DelegateMember.Target);
Assert.IsNotNull(otherProxy.DelegateHolder.ComplexTypeMember);
Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember.Count);
Assert.AreEqual(1, otherProxy.DelegateHolder.ComplexTypeMember[0]);
Assert.AreEqual(2, otherProxy.DelegateHolder.ComplexTypeMember[1]);
Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember[2]);
}
[Test]
public void SerializeObjectsWithIndirectDelegateToThisObject()
{
var proxy = (IndirectDelegateHolder)generator.CreateClassProxy(typeof(IndirectDelegateHolder),
new IInterceptor[] { new StandardInterceptor() });
proxy.DelegateHolder.DelegateMember = new EventHandler(proxy.TestHandler);
proxy.DelegateHolder.ComplexTypeMember = new ArrayList(new[] { 1, 2, 3 });
Assert.IsNotNull(proxy.DelegateHolder.DelegateMember);
Assert.AreSame(proxy, proxy.DelegateHolder.DelegateMember.Target);
Assert.IsNotNull(proxy.DelegateHolder.ComplexTypeMember);
Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember.Count);
Assert.AreEqual(1, proxy.DelegateHolder.ComplexTypeMember[0]);
Assert.AreEqual(2, proxy.DelegateHolder.ComplexTypeMember[1]);
Assert.AreEqual(3, proxy.DelegateHolder.ComplexTypeMember[2]);
var otherProxy = (SerializeAndDeserialize(proxy));
Assert.IsNotNull(otherProxy.DelegateHolder.DelegateMember);
Assert.AreSame(otherProxy, otherProxy.DelegateHolder.DelegateMember.Target);
Assert.IsNotNull(otherProxy.DelegateHolder.ComplexTypeMember);
Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember.Count);
Assert.AreEqual(1, otherProxy.DelegateHolder.ComplexTypeMember[0]);
Assert.AreEqual(2, otherProxy.DelegateHolder.ComplexTypeMember[1]);
Assert.AreEqual(3, otherProxy.DelegateHolder.ComplexTypeMember[2]);
}
[Test]
public void SimpleInterfaceProxy()
{
var proxy =
generator.CreateInterfaceProxyWithTarget(typeof(IMyInterface2), new MyInterfaceImpl(), new StandardInterceptor());
Assert.IsTrue(proxy.GetType().IsSerializable);
var inter = (IMyInterface2)proxy;
inter.Name = "opa";
Assert.AreEqual("opa", inter.Name);
inter.Started = true;
Assert.AreEqual(true, inter.Started);
var otherProxy = (IMyInterface2)SerializeAndDeserialize(proxy);
Assert.AreEqual(inter.Name, otherProxy.Name);
Assert.AreEqual(inter.Started, otherProxy.Started);
}
[Test]
public void SimpleInterfaceProxy_WithoutTarget()
{
var proxy =
generator.CreateInterfaceProxyWithoutTarget(typeof(IMyInterface2), new[] { typeof(IMyInterface) },
new StandardInterceptor());
Assert.IsTrue(proxy is IMyInterface2);
Assert.IsTrue(proxy is IMyInterface);
var otherProxy = SerializeAndDeserialize(proxy);
Assert.IsTrue(otherProxy is IMyInterface2);
Assert.IsTrue(otherProxy is IMyInterface);
}
[Test]
public void SimpleProxySerialization()
{
var proxy = (MySerializableClass)
generator.CreateClassProxy(typeof(MySerializableClass), new StandardInterceptor());
var current = proxy.Current;
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(current, otherProxy.Current);
}
[Test]
public void BadSerializable()
{
const int expectedValue = 13;
var proxy = generator.CreateClassProxy<BadSerializable>();
proxy.Value = expectedValue;
var otherProxy = SerializeAndDeserialize(proxy);
Assert.AreEqual(expectedValue, otherProxy.Value);
}
public override void TearDown()
{
base.TearDown();
ProxyObjectReference.ResetScope();
}
public static T SerializeAndDeserialize<T>(T proxy)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, proxy);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
}
}
#endif