1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
//
// System.Runtime.Serialization.SerializationCallbackTest.cs
//
// Author: Robert Jordan (robertj@gmx.net)
//
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Runtime.Serialization
{
[TestFixture]
public class SerializationCallbackTest
{
[Test]
public void Test ()
{
Log.Clear ();
Driver (new BinaryFormatter (), new A (new B()));
//Console.WriteLine (Log.Text);
Assert.AreEqual (Log.Text, "A1B1A2B2A3B3B4A4");
}
[Test]
public void TestInheritance ()
{
Log.Clear ();
Driver (new BinaryFormatter (), new C (new B()));
//Console.WriteLine (Log.Text);
Assert.AreEqual (Log.Text, "A1C1B1A2C2B2A3B3B4A4");
}
[Test]
public void TestISerializable ()
{
Log.Clear ();
Driver (new BinaryFormatter (), new A (new D()));
//Console.WriteLine (Log.Text);
Assert.AreEqual (Log.Text, "A1B1A2B2A3B3B4A4");
}
void Driver (IFormatter formatter, A a)
{
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, a);
stream.Position = 0;
a.CheckSerializationStatus ();
a = (A) formatter.Deserialize (stream);
a.CheckDeserializationStatus ();
}
}
class Log
{
static StringBuilder b = new StringBuilder ();
public static void Write (string msg)
{
b.Append (msg);
}
public static void Clear ()
{
b = new StringBuilder ();
}
public static string Text {
get { return b.ToString (); }
}
}
[Serializable]
class A : IDeserializationCallback
{
public int Status = 0;
B inner;
public A (B inner)
{
this.inner = inner;
this.inner.Outer = this;
}
public void CheckSerializationStatus ()
{
Assert.AreEqual (2, Status, "#A01");
}
public void CheckDeserializationStatus ()
{
Assert.AreEqual (2, Status, "#A01");
}
[OnSerializing]
void OnSerializing (StreamingContext ctx)
{
Log.Write ("A1");
Assert.AreEqual (0, Status, "#A01");
Assert.AreEqual (0, inner.Status, "#A02");
Status++;
}
[OnSerialized]
void OnSerialized (StreamingContext ctx)
{
Log.Write ("A2");
Assert.AreEqual (1, Status, "#A03");
Assert.AreEqual (1, inner.Status, "#A04");
// must have no effect after deserialization
Status++;
}
[OnDeserializing]
void OnDeserializing (StreamingContext ctx)
{
Log.Write ("A3");
Assert.IsNull (inner, "#A05");
Assert.AreEqual(0, Status, "#A06");
// must have no effect after deserialization
Status = 42;
}
[OnDeserialized]
void OnDeserialized (StreamingContext ctx)
{
Log.Write ("A4");
Assert.IsNotNull (inner, "#A07");
Assert.AreEqual(1, Status, "#A08");
Assert.AreEqual(1, inner.Status, "#A10");
Status++;
}
void IDeserializationCallback.OnDeserialization (object sender)
{
// don't log the order because it's undefined
CheckDeserializationStatus ();
}
}
[Serializable]
class B : IDeserializationCallback
{
public int Status = 0;
public A Outer;
[OnSerializing]
void OnSerializing (StreamingContext ctx)
{
Log.Write ("B1");
Assert.AreEqual (0, Status, "#B01");
Assert.AreEqual (1, Outer.Status, "#B01.2");
Status++;
}
[OnSerialized]
void OnSerialized (StreamingContext ctx)
{
Log.Write ("B2");
Assert.AreEqual (1, Status, "#B02");
Assert.AreEqual (2, Outer.Status, "#B03");
// must have no effect after deserialization
Status++;
}
[OnDeserializing]
void OnDeserializing (StreamingContext ctx)
{
Log.Write ("B3");
Assert.IsNull (Outer, "#B05");
Assert.AreEqual (0, Status, "#B06");
// must have no effect after deserialization
Status = 42;
}
[OnDeserialized]
void OnDeserialized (StreamingContext ctx)
{
Log.Write ("B4");
}
void IDeserializationCallback.OnDeserialization (object sender)
{
// don't log the order because it's undefined
Assert.AreEqual (1, Status);
}
}
[Serializable]
class C : A
{
public C (B inner) : base (inner)
{
}
[OnSerializing]
void OnSerializing (StreamingContext ctx)
{
Log.Write ("C1");
Assert.AreEqual (1, Status, "#C01");
}
[OnSerialized]
void OnSerialized (StreamingContext ctx)
{
Log.Write ("C2");
Assert.AreEqual (2, Status, "#C02");
}
}
[Serializable]
class D : B, ISerializable
{
public D ()
{
}
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext ctx)
{
info.AddValue ("Status", Status);
info.AddValue ("Outer", Outer);
}
D (SerializationInfo info, StreamingContext ctx)
{
Status = info.GetInt32 ("Status");
Outer = (A) info.GetValue ("Outer", typeof (A));
}
}
}
|