2 // System.Runtime.Serialization.SerializationCallbackTest.cs
4 // Author: Robert Jordan (robertj@gmx.net)
10 using System
.Diagnostics
;
12 using System
.Reflection
;
13 using System
.Runtime
.Serialization
;
14 using System
.Runtime
.Serialization
.Formatters
.Binary
;
16 using NUnit
.Framework
;
18 namespace MonoTests
.System
.Runtime
.Serialization
21 public class SerializationCallbackTest
27 Driver (new BinaryFormatter (), new A (new B()));
28 //Console.WriteLine (Log.Text);
29 Assert
.AreEqual (Log
.Text
, "A1B1A2B2A3B3B4A4");
33 public void TestInheritance ()
36 Driver (new BinaryFormatter (), new C (new B()));
37 //Console.WriteLine (Log.Text);
38 Assert
.AreEqual (Log
.Text
, "A1C1B1A2C2B2A3B3B4A4");
42 public void TestISerializable ()
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
);
56 a
.CheckSerializationStatus ();
57 a
= (A
) formatter
.Deserialize (stream
);
58 a
.CheckDeserializationStatus ();
64 static StringBuilder b
= new StringBuilder ();
66 public static void Write (string msg
)
71 public static void Clear ()
73 b
= new StringBuilder ();
76 public static string Text
{
77 get { return b.ToString (); }
82 class A
: IDeserializationCallback
84 public int Status
= 0;
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");
104 void OnSerializing (StreamingContext ctx
)
107 Assert
.AreEqual (0, Status
, "#A01");
108 Assert
.AreEqual (0, inner
.Status
, "#A02");
113 void OnSerialized (StreamingContext ctx
)
116 Assert
.AreEqual (1, Status
, "#A03");
117 Assert
.AreEqual (1, inner
.Status
, "#A04");
118 // must have no effect after deserialization
123 void OnDeserializing (StreamingContext ctx
)
126 Assert
.IsNull (inner
, "#A05");
127 Assert
.AreEqual(0, Status
, "#A06");
128 // must have no effect after deserialization
133 void OnDeserialized (StreamingContext ctx
)
136 Assert
.IsNotNull (inner
, "#A07");
137 Assert
.AreEqual(1, Status
, "#A08");
138 Assert
.AreEqual(1, inner
.Status
, "#A10");
142 void IDeserializationCallback
.OnDeserialization (object sender
)
144 // don't log the order because it's undefined
145 CheckDeserializationStatus ();
151 class B
: IDeserializationCallback
153 public int Status
= 0;
157 void OnSerializing (StreamingContext ctx
)
160 Assert
.AreEqual (0, Status
, "#B01");
161 Assert
.AreEqual (1, Outer
.Status
, "#B01.2");
166 void OnSerialized (StreamingContext ctx
)
169 Assert
.AreEqual (1, Status
, "#B02");
170 Assert
.AreEqual (2, Outer
.Status
, "#B03");
171 // must have no effect after deserialization
176 void OnDeserializing (StreamingContext ctx
)
179 Assert
.IsNull (Outer
, "#B05");
180 Assert
.AreEqual (0, Status
, "#B06");
181 // must have no effect after deserialization
186 void OnDeserialized (StreamingContext ctx
)
191 void IDeserializationCallback
.OnDeserialization (object sender
)
193 // don't log the order because it's undefined
194 Assert
.AreEqual (1, Status
);
201 public C (B inner
) : base (inner
)
206 void OnSerializing (StreamingContext ctx
)
209 Assert
.AreEqual (1, Status
, "#C01");
213 void OnSerialized (StreamingContext ctx
)
216 Assert
.AreEqual (2, Status
, "#C02");
221 class D
: B
, ISerializable
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
));