Simple impl to MockRailsEngineContext.Url
[castle.git] / Tools / DynamicProxy / Castle.DynamicProxy.Tests / ExplicitImplementationOfISerializable.cs
blob6dfecbcec6a80600a746387d3699b7e2621a24c1
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.Runtime.Serialization;
20 using Castle.DynamicProxy.Serialization;
22 using NUnit.Framework;
24 [TestFixture]
25 public class ExplicitImplementationOfISerializable
27 private GeneratorContext context;
28 private ProxyGenerator generator;
30 [SetUp]
31 public void Setup()
33 context = new GeneratorContext();
34 generator = new ProxyGenerator();
35 ProxyObjectReference.ResetScope();
38 [Test]
39 public void CreateClassThatImplementISerializable()
41 MyExplicitISerializable proxy = (MyExplicitISerializable)this.generator.CreateCustomClassProxy(typeof(MyExplicitISerializable),new StandardInterceptor(), this.context);
42 Assert.IsTrue(proxy.GetType().IsSerializable);
45 [Test]
46 public void SimpleProxySerialization()
48 MyExplicitISerializable proxy = (MyExplicitISerializable)
49 this.generator.CreateCustomClassProxy(
50 typeof(MyExplicitISerializable),new StandardInterceptor(), this.context);
52 DateTime original = proxy.Current;
53 MyExplicitISerializable serializedProxy = (MyExplicitISerializable) SerializableClassTestCase.SerializeAndDeserialize(proxy);
54 Assert.AreEqual(original, serializedProxy.Current);
57 #if !MONO
59 [Test]
60 public void ProxySerializationWhileOverridingExplicitInterface()
62 MyExplicitISerializable2 proxy = (MyExplicitISerializable2)
63 this.generator.CreateCustomClassProxy(
64 typeof(MyExplicitISerializable2),new StandardInterceptor(), this.context);
66 MyExplicitISerializable2 serializedProxy = (MyExplicitISerializable2) SerializableClassTestCase.SerializeAndDeserialize(proxy);
67 Assert.AreEqual(proxy.Info, serializedProxy.Info);
70 [Test]
71 public void ExplicitISerializeDoes_NOT_CallToBaseImplementation()
73 MyExplicitISerializable3 proxy = (MyExplicitISerializable3)
74 this.generator.CreateCustomClassProxy(
75 typeof(MyExplicitISerializable3),new StandardInterceptor(), this.context);
77 MyExplicitISerializable3 serializedProxy = (MyExplicitISerializable3) SerializableClassTestCase.SerializeAndDeserialize(proxy);
78 Assert.IsFalse( (proxy.Info+"_Serialized").Equals( serializedProxy.Info ) );
81 #endif
84 [Serializable]
85 public class MyExplicitISerializable
87 public DateTime Current;
89 public MyExplicitISerializable()
91 Current = DateTime.Now;
95 [Serializable]
96 public class MyExplicitISerializable2 : MyExplicitISerializable, ISerializable
98 public string Info;
100 public MyExplicitISerializable2()
102 Info = Current.ToString();
105 protected MyExplicitISerializable2(SerializationInfo serializationInfo, StreamingContext streamingContext)
107 Current = serializationInfo.GetDateTime("dt");
108 Info = serializationInfo.GetString("info");
111 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
113 info.AddValue("dt", Current, typeof(DateTime));
114 info.AddValue("info", Info, typeof(string));
119 [Serializable]
120 public class MyExplicitISerializable3 : MyExplicitISerializable, ISerializable
122 public string Info;
124 public MyExplicitISerializable3()
126 Info = Current.ToString();
129 protected MyExplicitISerializable3(SerializationInfo serializationInfo, StreamingContext streamingContext)
131 Current = serializationInfo.GetDateTime("dt");
132 Info = serializationInfo.GetString("info");
135 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
137 info.AddValue("dt", Current, typeof(DateTime));
138 info.AddValue("info", Info+"_Serialized", typeof(string));