PageRenderTime 82ms CodeModel.GetById 41ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/src/UnitTests/Proxy/ProxySerializationTests.cs

http://github.com/philiplaureano/LinFu
C# | 62 lines | 48 code | 10 blank | 4 comment | 2 complexity | 0339578a233fc7ee47e6879de9ef9629 MD5 | raw file
 1using System;
 2using System.Diagnostics;
 3using System.IO;
 4using System.Reflection;
 5using System.Runtime.Serialization;
 6using System.Runtime.Serialization.Formatters.Binary;
 7using LinFu.AOP.Cecil;
 8using LinFu.Proxy;
 9using LinFu.Proxy.Interfaces;
10using Xunit;
11using SampleLibrary;
12using SampleLibrary.Proxy;
13
14namespace LinFu.UnitTests.Proxy
15{
16    public class ProxySerializationTests : BaseTestFixture
17    {
18        [Fact]
19        public void ShouldSupportSerialization()
20        {
21            var factory = new ProxyFactory();
22
23            var interceptor = new SerializableInterceptor();
24            interceptor.Identifier = Guid.NewGuid();
25
26            var proxy = factory.CreateProxy<ISampleService>(interceptor);
27            var proxyType = proxy.GetType();
28
29            var proxyAssembly = proxyType.Assembly.Location;
30
31            // The proxy type should have a default constructor
32            // and a serialization constructor
33            var constructorFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
34            var constructors = proxyType.GetConstructors(constructorFlags);
35            Assert.True(constructors.Length == 2);
36
37            var serializationConstructor = proxyType.GetConstructor(constructorFlags, null,
38                new[]
39                {
40                    typeof(SerializationInfo),
41                    typeof(StreamingContext)
42                }, null);
43            
44            Assert.NotNull(serializationConstructor);
45
46            // Serialize the proxy
47            var stream = new MemoryStream();
48            var formatter = new BinaryFormatter();
49            formatter.Serialize(stream, proxy);
50
51            // Deserialize the proxy from the stream
52            stream.Seek(0, SeekOrigin.Begin);
53            var restoredProxy = (IProxy) formatter.Deserialize(stream);
54            Assert.NotNull(restoredProxy);
55            Assert.NotNull(restoredProxy.Interceptor);
56            Assert.True(restoredProxy.Interceptor.GetType() == typeof(SerializableInterceptor));
57
58            var otherInterceptor = (SerializableInterceptor) restoredProxy.Interceptor;
59            Assert.Equal(otherInterceptor.Identifier, interceptor.Identifier);
60        }
61    }
62}