2009-12-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Runtime.Serialization / SerializationCallbackTest.cs
blob7c0aa7cd6176681bd779b583fb2560a6422dd924
1 //
2 // System.Runtime.Serialization.SerializationCallbackTest.cs
3 //
4 // Author: Robert Jordan (robertj@gmx.net)
5 //
7 #if NET_2_0
9 using System;
10 using System.Diagnostics;
11 using System.IO;
12 using System.Reflection;
13 using System.Runtime.Serialization;
14 using System.Runtime.Serialization.Formatters.Binary;
15 using System.Text;
16 using NUnit.Framework;
18 namespace MonoTests.System.Runtime.Serialization
20 [TestFixture]
21 public class SerializationCallbackTest
23 [Test]
24 public void Test ()
26 Log.Clear ();
27 Driver (new BinaryFormatter (), new A (new B()));
28 //Console.WriteLine (Log.Text);
29 Assert.AreEqual (Log.Text, "A1B1A2B2A3B3B4A4");
32 [Test]
33 public void TestInheritance ()
35 Log.Clear ();
36 Driver (new BinaryFormatter (), new C (new B()));
37 //Console.WriteLine (Log.Text);
38 Assert.AreEqual (Log.Text, "A1C1B1A2C2B2A3B3B4A4");
41 [Test]
42 public void TestISerializable ()
44 Log.Clear ();
45 Driver (new BinaryFormatter (), new A (new D()));
46 //Console.WriteLine (Log.Text);
47 Assert.AreEqual (Log.Text, "A1B1A2B2A3B3B4A4");
50 void Driver (IFormatter formatter, A a)
52 MemoryStream stream = new MemoryStream();
53 formatter.Serialize(stream, a);
54 stream.Position = 0;
56 a.CheckSerializationStatus ();
57 a = (A) formatter.Deserialize (stream);
58 a.CheckDeserializationStatus ();
62 class Log
64 static StringBuilder b = new StringBuilder ();
66 public static void Write (string msg)
68 b.Append (msg);
71 public static void Clear ()
73 b = new StringBuilder ();
76 public static string Text {
77 get { return b.ToString (); }
81 [Serializable]
82 class A : IDeserializationCallback
84 public int Status = 0;
85 B inner;
87 public A (B inner)
89 this.inner = inner;
90 this.inner.Outer = this;
93 public void CheckSerializationStatus ()
95 Assert.AreEqual (2, Status, "#A01");
98 public void CheckDeserializationStatus ()
100 Assert.AreEqual (2, Status, "#A01");
103 [OnSerializing]
104 void OnSerializing (StreamingContext ctx)
106 Log.Write ("A1");
107 Assert.AreEqual (0, Status, "#A01");
108 Assert.AreEqual (0, inner.Status, "#A02");
109 Status++;
112 [OnSerialized]
113 void OnSerialized (StreamingContext ctx)
115 Log.Write ("A2");
116 Assert.AreEqual (1, Status, "#A03");
117 Assert.AreEqual (1, inner.Status, "#A04");
118 // must have no effect after deserialization
119 Status++;
122 [OnDeserializing]
123 void OnDeserializing (StreamingContext ctx)
125 Log.Write ("A3");
126 Assert.IsNull (inner, "#A05");
127 Assert.AreEqual(0, Status, "#A06");
128 // must have no effect after deserialization
129 Status = 42;
132 [OnDeserialized]
133 void OnDeserialized (StreamingContext ctx)
135 Log.Write ("A4");
136 Assert.IsNotNull (inner, "#A07");
137 Assert.AreEqual(1, Status, "#A08");
138 Assert.AreEqual(1, inner.Status, "#A10");
139 Status++;
142 void IDeserializationCallback.OnDeserialization (object sender)
144 // don't log the order because it's undefined
145 CheckDeserializationStatus ();
150 [Serializable]
151 class B : IDeserializationCallback
153 public int Status = 0;
154 public A Outer;
156 [OnSerializing]
157 void OnSerializing (StreamingContext ctx)
159 Log.Write ("B1");
160 Assert.AreEqual (0, Status, "#B01");
161 Assert.AreEqual (1, Outer.Status, "#B01.2");
162 Status++;
165 [OnSerialized]
166 void OnSerialized (StreamingContext ctx)
168 Log.Write ("B2");
169 Assert.AreEqual (1, Status, "#B02");
170 Assert.AreEqual (2, Outer.Status, "#B03");
171 // must have no effect after deserialization
172 Status++;
175 [OnDeserializing]
176 void OnDeserializing (StreamingContext ctx)
178 Log.Write ("B3");
179 Assert.IsNull (Outer, "#B05");
180 Assert.AreEqual (0, Status, "#B06");
181 // must have no effect after deserialization
182 Status = 42;
185 [OnDeserialized]
186 void OnDeserialized (StreamingContext ctx)
188 Log.Write ("B4");
191 void IDeserializationCallback.OnDeserialization (object sender)
193 // don't log the order because it's undefined
194 Assert.AreEqual (1, Status);
198 [Serializable]
199 class C : A
201 public C (B inner) : base (inner)
205 [OnSerializing]
206 void OnSerializing (StreamingContext ctx)
208 Log.Write ("C1");
209 Assert.AreEqual (1, Status, "#C01");
212 [OnSerialized]
213 void OnSerialized (StreamingContext ctx)
215 Log.Write ("C2");
216 Assert.AreEqual (2, Status, "#C02");
220 [Serializable]
221 class D : B, ISerializable
223 public D ()
227 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext ctx)
229 info.AddValue ("Status", Status);
230 info.AddValue ("Outer", Outer);
233 D (SerializationInfo info, StreamingContext ctx)
235 Status = info.GetInt32 ("Status");
236 Outer = (A) info.GetValue ("Outer", typeof (A));
241 #endif