Updating custom persister in tests to reflect recent changes in NH, will be ignored...
[castle.git] / Tools / DynamicProxy / Castle.DynamicProxy.Tests / SerializableClassTestCase.cs
blobc06bac928f87009badd6708a553a9a457f6197e7
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.DynamicProxy.Test
17 using System;
18 using System.Collections;
19 using System.IO;
20 using System.Runtime.Serialization.Formatters.Binary;
21 using Castle.DynamicProxy.Serialization;
22 using NUnit.Framework;
24 using Castle.DynamicProxy.Test.Classes;
25 using Castle.DynamicProxy.Test.Mixins;
26 using Castle.DynamicProxy.Test.ClassInterfaces;
27 using Castle.DynamicProxy.Builder.CodeGenerators;
29 /// <summary>
30 /// Summary description for SerializableClassTestCase.
31 /// </summary>
32 [TestFixture]
33 public class SerializableClassTestCase
35 ProxyGenerator generator;
37 [SetUp]
38 public void Init()
40 generator = new ProxyGenerator();
43 [Test]
44 public void CreateSerializable()
46 ProxyObjectReference.ResetScope();
48 MySerializableClass proxy = (MySerializableClass)
49 generator.CreateClassProxy( typeof(MySerializableClass), new StandardInterceptor() );
51 Assert.IsTrue( proxy.GetType().IsSerializable );
54 [Test]
55 public void SimpleProxySerialization()
57 ProxyObjectReference.ResetScope();
59 MySerializableClass proxy = (MySerializableClass)
60 generator.CreateClassProxy( typeof(MySerializableClass), new StandardInterceptor() );
62 DateTime current = proxy.Current;
64 MySerializableClass otherProxy = (MySerializableClass) SerializeAndDeserialize(proxy);
66 Assert.AreEqual( current, otherProxy.Current );
69 [Test]
70 public void SerializationDelegate()
72 ProxyObjectReference.ResetScope();
74 MySerializableClass2 proxy = (MySerializableClass2)
75 generator.CreateClassProxy( typeof(MySerializableClass2), new StandardInterceptor() );
77 DateTime current = proxy.Current;
79 MySerializableClass2 otherProxy = (MySerializableClass2) SerializeAndDeserialize(proxy);
81 Assert.AreEqual( current, otherProxy.Current );
84 [Test]
85 public void SimpleInterfaceProxy()
87 ProxyObjectReference.ResetScope();
89 object proxy = generator.CreateProxy(
90 typeof(IMyInterface), new StandardInterceptor( ), new MyInterfaceImpl() );
92 Assert.IsTrue( proxy.GetType().IsSerializable );
94 IMyInterface inter = (IMyInterface) proxy;
96 inter.Name = "opa";
97 Assert.AreEqual( "opa", inter.Name );
98 inter.Started = true;
99 Assert.AreEqual( true, inter.Started );
101 IMyInterface otherProxy = (IMyInterface) SerializeAndDeserialize(proxy);
103 Assert.AreEqual( inter.Name, otherProxy.Name );
104 Assert.AreEqual( inter.Started, otherProxy.Started );
107 [Test]
108 public void CustomMarkerInterface()
110 ProxyObjectReference.ResetScope();
112 ClassProxyGenerator classGenerator = new ClassProxyGenerator(
113 new ModuleScope(), new GeneratorContext() );
115 Type proxyType = classGenerator.GenerateCode( typeof(ClassWithMarkerInterface), new Type[] { typeof(IMarkerInterface) } );
117 object proxy = Activator.CreateInstance( proxyType, new object[] { new StandardInterceptor() } );
119 Assert.IsNotNull( proxy );
120 Assert.IsTrue( proxy is IMarkerInterface );
122 object otherProxy = SerializeAndDeserialize(proxy);
124 Assert.IsTrue( otherProxy is IMarkerInterface );
127 #if !MONO
128 [Test]
129 #endif
130 [Category("DotNetOnly")]
131 public void MixinSerialization()
133 ProxyObjectReference.ResetScope();
135 GeneratorContext context = new GeneratorContext();
136 SimpleMixin mixin1 = new SimpleMixin();
137 OtherMixin mixin2 = new OtherMixin();
139 context.AddMixinInstance( mixin1 );
140 context.AddMixinInstance( mixin2 );
142 object proxy = generator.CreateCustomClassProxy(
143 typeof(SimpleClass), new StandardInterceptor(), context );
145 Assert.IsTrue( typeof(SimpleClass).IsAssignableFrom( proxy.GetType() ) );
147 (proxy as SimpleClass).DoSome();
149 ISimpleMixin mixin = proxy as ISimpleMixin;
150 Assert.AreEqual(1, mixin.DoSomething());
152 IOtherMixin other = proxy as IOtherMixin;
153 Assert.AreEqual(3, other.Sum(1,2));
155 SimpleClass otherProxy = (SimpleClass) SerializeAndDeserialize(proxy);
157 otherProxy.DoSome();
159 mixin = otherProxy as ISimpleMixin;
160 Assert.AreEqual(1, mixin.DoSomething());
162 other = otherProxy as IOtherMixin;
163 Assert.AreEqual(3, other.Sum(1,2));
166 [Test]
167 [Ignore("XmlSerializer does not respect the ObjectReference protocol so it wont work")]
168 public void XmlSerialization()
170 ProxyObjectReference.ResetScope();
172 GeneratorContext context = new GeneratorContext();
173 SimpleMixin mixin1 = new SimpleMixin();
174 OtherMixin mixin2 = new OtherMixin();
176 context.AddMixinInstance( mixin1 );
177 context.AddMixinInstance( mixin2 );
179 object proxy = generator.CreateCustomClassProxy(
180 typeof(SimpleClass), new StandardInterceptor(), context );
182 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(SimpleClass));
183 MemoryStream stream = new MemoryStream();
184 serializer.Serialize(stream, proxy);
185 stream.Position = 0;
186 SimpleClass otherProxy = (SimpleClass) serializer.Deserialize( stream );
188 ISimpleMixin mixin = otherProxy as ISimpleMixin;
189 Assert.AreEqual(1, mixin.DoSomething());
191 IOtherMixin other = otherProxy as IOtherMixin;
192 Assert.AreEqual(3, other.Sum(1,2));
195 [Test]
196 public void HashtableSerialization()
198 ProxyObjectReference.ResetScope();
200 object proxy = generator.CreateClassProxy(
201 typeof(Hashtable), new StandardInterceptor() );
203 Assert.IsTrue( typeof(Hashtable).IsAssignableFrom( proxy.GetType() ) );
205 (proxy as Hashtable).Add("key", "helloooo!");
207 Hashtable otherProxy = (Hashtable) SerializeAndDeserialize(proxy);
209 Assert.IsTrue(otherProxy.ContainsKey("key"));
210 Assert.AreEqual("helloooo!", otherProxy["key"]);
213 public static object SerializeAndDeserialize( object proxy )
215 MemoryStream stream = new MemoryStream();
216 BinaryFormatter formatter = new BinaryFormatter();
217 formatter.Serialize( stream, proxy );
218 stream.Position = 0;
219 return formatter.Deserialize( stream );