* corlib_test.dll.sources: Added ObjectManagerTest.cs in
authorGert Driesen <drieseng@users.sourceforge.net>
Thu, 15 Dec 2005 17:53:24 +0000 (17:53 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Thu, 15 Dec 2005 17:53:24 +0000 (17:53 -0000)
System.Runtime.Serialization.
* ObjectManagerTest.cs: New test for bug #76931.

svn path=/trunk/mcs/; revision=54475

mcs/class/corlib/ChangeLog
mcs/class/corlib/Test/System.Runtime.Serialization/ChangeLog
mcs/class/corlib/Test/System.Runtime.Serialization/ObjectManagerTest.cs [new file with mode: 0644]
mcs/class/corlib/corlib_test.dll.sources

index 2a7884a8dfa13afa2d35069282d01252e9fa8ff9..90f1bd8bf4c2b7cb280ec7826a8a026019ba0938 100644 (file)
@@ -1,3 +1,8 @@
+2005-12-15  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * corlib_test.dll.sources: Added ObjectManagerTest.cs in
+       System.Runtime.Serialization.
+
 2005-12-07  Zoltan Varga  <vargaz@gmail.com>
 
        * corlib.dll.sources: Remove obsolete System.Runtime.InteropServices/NewConstraintAttribute.cs.
index 8ce8c9838cbedf1a4f786c3b95a6b9c4be730c15..c73a2c85d6258eac4624ba3e2d268f530e63bf0e 100644 (file)
@@ -1,3 +1,7 @@
+2005-12-15  Gert Driesen  <drieseng@users.sourceforge.net>\r
+\r
+       * ObjectManagerTest.cs: New test for bug #76931.\r
+\r
 2005-03-23  Lluis Sanchez Gual  <lluis@ximian.com>\r
 \r
        * ArraySerializationTest.cs: New serialization tests.\r
diff --git a/mcs/class/corlib/Test/System.Runtime.Serialization/ObjectManagerTest.cs b/mcs/class/corlib/Test/System.Runtime.Serialization/ObjectManagerTest.cs
new file mode 100644 (file)
index 0000000..df5bbdd
--- /dev/null
@@ -0,0 +1,148 @@
+//
+// System.Runtime.Serialization.ObjectManagerTest.cs
+//
+// Author: Martin Baulig (martin@ximian.com)
+//
+// (C) Novell
+//
+
+using System;
+using System.IO;
+using System.Text;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Runtime.Serialization
+{
+       [TestFixture]
+       public class ObjectManagerTest
+       {
+               [Test] // bug 76931
+               public void TestSerialization ()
+               {
+                       using (MemoryStream ms = new MemoryStream ()) {
+                               Bar bar = new Bar (8, 3, 5, 21);
+                               bar.Save (ms);
+
+                               ms.Position = 0;
+
+                               bar = Bar.Load (ms);
+                               
+                               Assert.AreEqual ("Bar [Foo (16),(Foo (6),Foo (10),Foo (42)]",
+                                       bar.ToString (), "#1");
+                       }
+               }
+       }
+
+       public class Foo
+       {
+               public int Data;
+
+               public Foo (int data)
+               {
+                       this.Data = data;
+               }
+
+               public override string ToString ()
+               {
+                       return String.Format ("Foo ({0})", Data);
+               }
+
+               internal class SerializationSurrogate : ISerializationSurrogate
+               {
+                       public void GetObjectData (object obj, SerializationInfo info, StreamingContext context)
+                       {
+                               Foo foo = (Foo) obj;
+
+                               info.AddValue ("data", foo.Data);
+                       }
+
+                       public object SetObjectData (object obj, SerializationInfo info,
+                                                        StreamingContext context,
+                                                        ISurrogateSelector selector)
+                       {
+                               Foo foo = (Foo) obj;
+
+                               foo.Data = info.GetInt32 ("data");
+
+                               return new Foo (2 * foo.Data);
+                       }
+               }
+       }
+
+       [Serializable]
+       public class Bar
+       {
+               public readonly Foo Foo;
+               public readonly Foo[] Array;
+
+               public Bar (int a, params int[] b)
+               {
+                       Foo = new Foo (a);
+                       Array = new Foo[b.Length];
+                       for (int i = 0; i < b.Length; i++)
+                               Array[i] = new Foo (b[i]);
+               }
+
+               public void Save (Stream stream)
+               {
+                       SurrogateSelector ss = new SurrogateSelector ();
+
+                       StreamingContext context = new StreamingContext (
+                               StreamingContextStates.Persistence, this);
+
+                       ss.AddSurrogate (typeof (Foo), context, new Foo.SerializationSurrogate ());
+
+                       BinaryFormatter formatter = new BinaryFormatter (ss, context);
+
+                       formatter.Serialize (stream, this);
+               }
+
+               public static Bar Load (Stream stream)
+               {
+                       SurrogateSelector ss = new SurrogateSelector ();
+
+                       StreamingContext context = new StreamingContext (
+                               StreamingContextStates.Persistence, null);
+
+                       ss.AddSurrogate (typeof (Foo), context, new Foo.SerializationSurrogate ());
+
+                       BinaryFormatter formatter = new BinaryFormatter (ss, context);
+
+                       return (Bar) formatter.Deserialize (stream);
+               }
+
+               public override string ToString ()
+               {
+                       StringBuilder sb = new StringBuilder ();
+                       sb.Append ("Bar [");
+                       sb.Append (Foo);
+                       sb.Append (",(");
+                       for (int i = 0; i < Array.Length; i++) {
+                               if (i > 0)
+                                       sb.Append (",");
+                               sb.Append (Array[i]);
+                       }
+                       sb.Append ("]");
+                       return sb.ToString ();
+               }
+       }
+
+       class X
+       {
+               static void Main ()
+               {
+                       using (MemoryStream ms = new MemoryStream ()) {
+                               Bar bar = new Bar (8, 3, 5, 21);
+                               bar.Save (ms);
+
+                               ms.Position = 0;
+
+                               bar = Bar.Load (ms);
+                               Console.WriteLine (bar);
+                       }
+               }
+       }
+}
index e2bc51f3cda0908032f5382a636443d6abadb29b..3ea294e0f20a1b1dc023a7f3ad6b924eb1dbf4ef 100644 (file)
@@ -132,6 +132,7 @@ System.Runtime.Serialization/FormatterServicesTests.cs
 System.Runtime.Serialization/ObjectIDGeneratorTests.cs
 System.Runtime.Serialization/SerializationTest.cs
 System.Runtime.Serialization/ArraySerializationTest.cs
+System.Runtime.Serialization/ObjectManagerTest.cs
 System.Runtime.Serialization.Formatters.Binary/BinaryFormatterTest.cs
 System.Runtime.Versioning/ResourceConsumptionAttributeTest.cs
 System.Runtime.Versioning/ResourceExposureAttributeTest.cs