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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// project created on 09/05/2003 at 18:07
using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Globalization;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.IO;
using NUnit.Framework;
using System.Collections.Generic;
namespace MonoTests.System.Runtime.Serialization.Formatters.Soap {
internal class NonSerializableObject {
}
public delegate void TrucDlg(string s);
[Serializable]
public class MoreComplexObject {
public event TrucDlg TrucEvent;
private string _string;
private string[] _strings = new string[]{};
private Queue _queue = new Queue();
public Dictionary<object, object> _table = new Dictionary<object, object> ();
public string ObjString {
get { return _string; }
}
public MoreComplexObject() {
TrucEvent += new TrucDlg(WriteString);
_queue.Enqueue(1);
_queue.Enqueue(null);
_queue.Enqueue("foo");
_table["foo"]="barr";
_table[1]="foo";
_table['c'] = "barr";
_table["barr"] = 1234567890;
}
public void OnTrucEvent(string s) {
TrucEvent(s);
}
public void WriteString(string s) {
_string = s;
}
public override bool Equals(object obj) {
MoreComplexObject objReturn = obj as MoreComplexObject;
if(objReturn == null) return false;
if(objReturn._string != this._string) return false;
Assert.AreEqual (_table.Count, objReturn._table.Count, "#1");
foreach(var e in objReturn._table) {
Assert.AreEqual (e.Value, _table[e.Key], e.Key.ToString ());
}
return SoapFormatterTest.CheckArray(this._queue.ToArray(), objReturn._queue.ToArray());
}
}
[Serializable]
internal class MarshalObject: MarshalByRefObject {
private string _name;
private long _id;
public MarshalObject() {
}
public MarshalObject(string name, long id) {
_name = name;
_id = id;
}
}
[Serializable]
internal class SimpleObject {
private string _name;
private int _id;
public SimpleObject(string name, int id) {
_name = name;
_id = id;
}
public override bool Equals(object obj) {
SimpleObject objCmp = obj as SimpleObject;
if(objCmp == null) return false;
if(objCmp._name != this._name) return false;
if(objCmp._id != this._id) return false;
return true;
}
}
[Serializable]
internal class Version1 {
public int _value;
public Version1(int value) {
_value = value;
}
}
[Serializable]
internal class Version2: ISerializable {
public int _value;
public string _foo;
public Version2(int value, string foo) {
_value = value;
_foo = foo;
}
public void GetObjectData(SerializationInfo info, StreamingContext context) {
info.AddValue("_value", _value);
info.AddValue("_foo", _foo);
}
private Version2(SerializationInfo info, StreamingContext context) {
_value = info.GetInt32("_value");
try{
_foo = info.GetString("_foo");
}
catch(SerializationException) {
_foo = "Default value";
}
}
}
public class Version1ToVersion2Binder: SerializationBinder {
public override Type BindToType (string assemblyName, string typeName) {
Type returnType = null;
string typeVersion1 = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version1";
string assemName = Assembly.GetExecutingAssembly().FullName;
if(typeName == typeVersion1) {
typeName = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version2";
}
string typeFormat = String.Format("{0}, {1}", typeName, assemName);
returnType = Type.GetType( typeFormat);
return returnType;
}
}
[TestFixture]
public class SoapFormatterTest
{
private SoapFormatter _soapFormatter;
private SoapFormatter _soapFormatterDeserializer;
private RemotingSurrogateSelector _surrogate;
#if DEBUG
private void Out(MemoryStream stream, object objGraph) {
Console.WriteLine("\n---------------------\n{0}\n", objGraph.ToString());
stream.Position = 0;
StreamReader r = new StreamReader(stream);
Console.WriteLine(r.ReadToEnd());
}
#endif
private object Serialize(object objGraph) {
MemoryStream stream = new MemoryStream();
Assert.IsTrue(objGraph != null);
Assert.IsTrue(stream != null);
_soapFormatter.SurrogateSelector = _surrogate;
_soapFormatter.Serialize(stream, objGraph);
#if DEBUG
Out(stream, objGraph);
#endif
stream.Position = 0;
object objReturn = _soapFormatterDeserializer.Deserialize(stream);
Assert.IsTrue(objReturn != null);
Assert.AreEqual(objGraph.GetType(), objReturn.GetType(), "#Tests "+objGraph.GetType());
stream = new MemoryStream();
_soapFormatter.Serialize(stream, objReturn);
stream.Position = 0;
return objReturn;
}
[SetUp]
public void GetReady() {
StreamingContext context = new StreamingContext(StreamingContextStates.All);
_surrogate = new RemotingSurrogateSelector();
_soapFormatter = new SoapFormatter(_surrogate, context);
_soapFormatterDeserializer = new SoapFormatter(null, context);
}
[TearDown]
public void Clean() {
}
[Test]
public void TestValueTypes() {
object objReturn;
objReturn = Serialize((short)1);
Assert.AreEqual(objReturn, 1, "#int16");
objReturn = Serialize(1);
Assert.AreEqual(objReturn, 1, "#int32");
objReturn = Serialize((Single)0.1234);
Assert.AreEqual(objReturn, 0.123400003f, "#Single");
objReturn = Serialize((Double)1234567890.0987654321);
Assert.AreEqual(objReturn, 1234567890.0987654321, "#iDouble");
objReturn = Serialize(true);
Assert.AreEqual(objReturn, true, "#Bool");
objReturn = Serialize((Int64) 1234567890);
Assert.AreEqual(objReturn, 1234567890, "#Int64");
objReturn = Serialize('c');
Assert.AreEqual(objReturn, 'c', "#Char");
}
[Test]
public void TestObjects() {
object objReturn;
objReturn = Serialize("");
objReturn = Serialize("hello world!");
Assert.AreEqual("hello world!", objReturn, "#string");
SoapMessage soapMsg = new SoapMessage();
soapMsg.Headers = new Header[0];
soapMsg.MethodName = "Equals";
soapMsg.ParamNames = new String[0];
soapMsg.ParamTypes = new Type[0];
soapMsg.ParamValues = new object[0];
soapMsg.XmlNameSpace = SoapServices.CodeXmlNamespaceForClrTypeNamespace("String", "System");
_soapFormatterDeserializer.TopObject = new SoapMessage();
objReturn = Serialize(soapMsg);
_soapFormatterDeserializer.TopObject = null;
SimpleObject obj = new SimpleObject("simple object", 1);
objReturn = Serialize(obj);
Assert.AreEqual(obj, objReturn, "#SimpleObject");
objReturn = Serialize(typeof(SimpleObject));
Assert.AreEqual(typeof(SimpleObject), (Type)objReturn, "#Type");
objReturn = Serialize(obj.GetType().Assembly);
Assert.AreEqual(obj.GetType().Assembly, objReturn, "#Assembly");
}
public static bool CheckArray(object objTest, object objReturn) {
Array objTestAsArray = objTest as Array;
Array objReturnAsArray = objReturn as Array;
Assert.IsTrue(objReturnAsArray != null, "#Not an Array "+objTest);
Assert.AreEqual(objTestAsArray.Length, objReturnAsArray.Length, "#Different lengths "+objTest);
IEnumerator iEnum = objReturnAsArray.GetEnumerator();
iEnum.Reset();
object obj2;
foreach(object obj1 in objTestAsArray) {
iEnum.MoveNext();
obj2 = iEnum.Current;
Assert.AreEqual(obj1, obj2, "#The content of the 2 arrays is different");
}
return true;
}
[Test]
public void TestArray() {
object objReturn;
object objTest;
objReturn = Serialize(new int[]{});
objTest = new int[]{1, 2, 3, 4};
objReturn = Serialize(objTest);
CheckArray(objTest, objReturn);
objReturn = Serialize(new long[]{1, 2, 3, 4});
objTest = new object[]{1, null, ":-)", 1234567890};
objReturn = Serialize(objTest);
objTest = new int[,]{{0, 1}, {2, 3}, {123, 4}};
objReturn = Serialize(objTest);
CheckArray(objTest, objReturn);
objTest = new string[]{};
objReturn = Serialize(objTest);
CheckArray(objTest, objReturn);
object[,,] objArray = new object[3,2,1];
objArray[0,0,0] = 1;
objArray[2,1,0] = "end";
objReturn = Serialize(objArray);
CheckArray(objArray, objReturn);
}
[Test]
public void TestMarshalByRefObject() {
Serialize(new MarshalObject("thing", 1234567890));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestNullObject() {
MemoryStream stream = new MemoryStream();
_soapFormatter.Serialize(stream, null);
}
[Test]
[ExpectedException(typeof(SerializationException))]
public void TestNonSerialisable() {
Serialize(new NonSerializableObject());
}
[Test]
public void TestMoreComplexObject() {
MoreComplexObject objReturn;
MoreComplexObject objTest = new MoreComplexObject();
objReturn = (MoreComplexObject) Serialize(objTest);
Assert.AreEqual(objTest, objReturn, "#Equals");
objReturn.OnTrucEvent("bidule");
Assert.AreEqual("bidule", objReturn.ObjString, "#dlg");
}
[Test]
public void TestSerializationbinder() {
Object objReturn;
MemoryStream stream = new MemoryStream();
Version1 objVer1 = new Version1(123);
_soapFormatter.SurrogateSelector = _surrogate;
_soapFormatter.Serialize(stream, objVer1);
stream.Position = 0;
_soapFormatterDeserializer.Binder = new Version1ToVersion2Binder();
objReturn = _soapFormatterDeserializer.Deserialize(stream);
Assert.AreEqual("Version2", objReturn.GetType().Name, "#Version1 Version2");
Assert.AreEqual(123, ((Version2) objReturn)._value, "#_value");
Assert.AreEqual("Default value", ((Version2) objReturn)._foo, "#_foo");
}
[Test]
public void TestMethodSignatureSerialization ()
{
Header h = new Header ("__MethodSignature", new Type [] { typeof(string),typeof(SignatureTest[]) }, false, "http://schemas.microsoft.com/clr/soap/messageProperties");
SoapMessage msg = new SoapMessage ();
msg.MethodName = "Run";
msg.ParamNames = new string [] { "nom" };
msg.ParamTypes = new Type [] { typeof(SignatureTest) };
msg.ParamValues = new object[] { new SignatureTest () };
msg.Headers = new Header[] { h};
MemoryStream ms = new MemoryStream ();
SoapFormatter sf = new SoapFormatter ();
sf.Serialize (ms, msg);
ms.Position = 0;
SoapMessage t = new SoapMessage ();
sf.TopObject = t;
t = (SoapMessage) sf.Deserialize (ms);
Assert.IsNotNull (t.Headers[0].Value, "#1");
Assert.AreEqual (t.Headers[0].Value.GetType (), typeof(Type[]), "#2");
Type[] ts = (Type[]) t.Headers[0].Value;
Assert.AreEqual (2, ts.Length, "#3");
Assert.IsNotNull (ts[0], "#4");
Assert.IsNotNull (ts[1], "#5");
Console.WriteLine ("PPP:" + ts[0].GetType());
Assert.AreEqual (typeof(string), ts[0], "#6");
Assert.AreEqual (typeof(SignatureTest[]), ts[1], "#7");
}
[Test]
public void TestCulture ()
{
var currentCulture = Thread.CurrentThread.CurrentCulture;
try {
Thread.CurrentThread.CurrentCulture = new CultureInfo ("de-DE");
var ms = new MemoryStream ();
var test = new CultureTest ();
_soapFormatter.Serialize(ms, test);
ms.Position = 0;
_soapFormatter.Deserialize(ms);
} finally {
Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
[Serializable]
public class CultureTest
{
[OnDeserialized]
public void OnDeserialization (StreamingContext context)
{
var ci = Thread.CurrentThread.CurrentCulture;
Assert.AreEqual("German (Germany)", ci.EnglishName, "#1");
}
[OnSerialized]
public void OnSerialized (StreamingContext context)
{
var ci = Thread.CurrentThread.CurrentCulture;
Assert.AreEqual("German (Germany)", ci.EnglishName, "#2");
}
}
}
[Serializable]
public class SignatureTest
{
public SoapQName qn = new SoapQName ("e", "name", "espai");
}
}
|