X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Runtime.Serialization.Formatters.Binary%2FBinaryFormatterTest.cs;h=6d4b997fb45317b67a5c928e23f97d46332aa4e8;hb=e82dc02e27ea872f3f1bd19d8a6c9770ae12716d;hp=52c6a27c7d1cf17810d257805a67add15ee076e7;hpb=99c3107f1d47d707e6b7786422154292cdbcf938;p=mono.git diff --git a/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs b/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs index 52c6a27c7d1..6d4b997fb45 100644 --- a/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs +++ b/mcs/class/corlib/Test/System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs @@ -62,6 +62,45 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary } } + namespace NestedA + { + [Serializable] + public class QualifiedFieldTest + { + int value = 0; + + public int ValueA { + get { return value; } + set { this.value = value; } + } + } + } + + namespace NestedB + { + [Serializable] + public class QualifiedFieldTest : NestedA.QualifiedFieldTest + { + int value = 0; + + public int ValueB { + get { return value; } + set { this.value = value; } + } + } + } + + [Serializable] + public class QualifiedFieldTest : NestedB.QualifiedFieldTest + { + int value = 0; + + public int Value { + get { return value; } + set { this.value = value; } + } + } + class SurrogateSelector: ISurrogateSelector { public void ChainSelector (ISurrogateSelector selector) @@ -214,6 +253,96 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary } } + class Class + { + public string Name; + + public virtual Instance NewInstance() + { + return new Instance { Class = this }; + } + } + + class Instance + { + public Class Class; + } + + + [Serializable] + class ClassSerializationProxy : IObjectReference + { + string className; + + public ClassSerializationProxy (Class klass) + { + this.className = klass.Name; + } + + public object GetRealObject(StreamingContext context) + { + return new Class { Name = className }; + } + } + + [Serializable] + class ObjectStreamClass : Class, IObjectReference, ISerializable + { + Class klass; + + public ObjectStreamClass (Class klass) + { + this.klass = klass; + } + + public ObjectStreamClass(SerializationInfo info, StreamingContext context) + { + klass = (Class)info.GetValue("0", typeof(object)); + } + + public object GetRealObject (StreamingContext context) + { + return this; + } + + public void GetObjectData (SerializationInfo info, StreamingContext context) + { + info.AddValue ("0", new ClassSerializationProxy (klass)); + } + + public override Instance NewInstance() + { + return klass.NewInstance(); + } + } + + [Serializable] + class DynamicProxy: IObjectReference, ISerializable + { + Instance obj; + + public DynamicProxy (Instance obj) + { + this.obj = obj; + } + + public DynamicProxy (SerializationInfo info, StreamingContext context) + { + ObjectStreamClass osc = (ObjectStreamClass) info.GetValue("0", typeof(object)); + obj = osc.NewInstance(); + } + + public object GetRealObject (StreamingContext context) + { + return obj; + } + + public void GetObjectData (SerializationInfo info, StreamingContext context) + { + info.AddValue ("0", new ObjectStreamClass (obj.Class)); + } + } + [TestFixture] public class BinaryFormatterTest { @@ -221,11 +350,7 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary public void Constructor_Default () { BinaryFormatter bf = new BinaryFormatter (); -#if NET_2_0 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat"); -#else - Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat"); -#endif Assert.IsNull (bf.Binder, "Binder"); Assert.AreEqual (StreamingContextStates.All, bf.Context.State, "Context"); Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel"); @@ -238,11 +363,7 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary { SurrogateSelector ss = new SurrogateSelector (); BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine)); -#if NET_2_0 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat"); -#else - Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat"); -#endif Assert.IsNull (bf.Binder, "Binder"); Assert.AreEqual (StreamingContextStates.CrossMachine, bf.Context.State, "Context"); Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel"); @@ -370,6 +491,23 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary return ms; } + [Test] + public void QualifiedField() + { + QualifiedFieldTest a = new QualifiedFieldTest (); + a.ValueA = 1; + a.ValueB = 2; + a.Value = 3; + Stream ms = new MemoryStream (); + BinaryFormatter bf = new BinaryFormatter (); + bf.Serialize(ms, a); + ms.Position = 0; + QualifiedFieldTest b = (QualifiedFieldTest)bf.Deserialize (ms); + Assert.AreEqual (a.ValueA, b.ValueA, "#1"); + Assert.AreEqual (a.ValueB, b.ValueB, "#2"); + Assert.AreEqual (a.Value, b.Value, "#3"); + } + #if NET_4_0 [Test] public void SerializationBindToName () @@ -452,6 +590,23 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary ms.Close (); } + [Test] + public void NestedObjectReferences () + { + MemoryStream ms = new MemoryStream (); + + var cls = new Class { Name = "MyClass" }; + var ob = cls.NewInstance (); + + BinaryFormatter bf = new BinaryFormatter(); + bf.Serialize (ms, new DynamicProxy (ob)); + + ms.Position = 0; + + Instance ins = (Instance) bf.Deserialize (ms); + Assert.AreEqual ("MyClass", ins.Class.Name); + } + class SimpleSerializationBinder2 : SerializationBinder { public override void BindToName (Type serializedType, out string assemblyName, out string typeName) @@ -504,6 +659,68 @@ namespace MonoTests.System.Runtime.Serialization.Formatters.Binary info.AddValue ("Id", Id); } } + + [Serializable] + public class OtherClass + { + } + + [Serializable] + public class BaseClass + { + public OtherClass Other { get; set; } + } + + public class CustomSerBinder: SerializationBinder + { + public override void BindToName (Type serializedType, out string assemblyName, out string typeName) + { + assemblyName = null; + if (serializedType == typeof (BaseClass)) + typeName = "base"; + else if (serializedType == typeof (OtherClass)) + typeName = "other"; + else + throw new ArgumentException ("Unknown type", "serializedType"); + } + + public override Type BindToType (string assemblyName, string typeName) + { + switch (typeName) { + case "base": + return typeof (BaseClass); + case "other": + return typeof (OtherClass); + default: + throw new ArgumentException ("Unknown type name", "typeName"); + } + } + } + + [Test] + public void BinderShouldBeUsedForProperties () + { + using (var serStream = new MemoryStream ()) { + var binder = new CustomSerBinder (); + + // serialize + var original = new BaseClass () { + Other = new OtherClass () + }; + var formatter = new BinaryFormatter (); + formatter.Binder = binder; + formatter.Serialize (serStream, original); + + // deserialize, making sure we're using a new formatter, just to be thorough + formatter = new BinaryFormatter (); + formatter.Binder = binder; + serStream.Seek (0, SeekOrigin.Begin); + var deserialized = formatter.Deserialize (serStream); + + Assert.AreEqual (original.GetType (), deserialized.GetType ()); + Assert.AreEqual (original.Other.GetType (), ((BaseClass)deserialized).Other.GetType ()); + } + } #endif } }