SoapFormatterTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // project created on 09/05/2003 at 18:07
  2. using System;
  3. using System.Collections;
  4. using System.Reflection;
  5. using System.Runtime.Remoting;
  6. using System.Runtime.Remoting.Messaging;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters;
  9. using System.Runtime.Serialization.Formatters.Soap;
  10. using System.IO;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Runtime.Serialization.Formatters.Soap {
  13. internal class NonSerializableObject {
  14. }
  15. public delegate void TrucDlg(string s);
  16. [Serializable]
  17. public class MoreComplexObject {
  18. public event TrucDlg TrucEvent;
  19. private string _string;
  20. private Queue _queue = new Queue();
  21. public Hashtable _table = new Hashtable();
  22. public string ObjString {
  23. get { return _string; }
  24. }
  25. public MoreComplexObject() {
  26. TrucEvent += new TrucDlg(WriteString);
  27. _queue.Enqueue(1);
  28. _queue.Enqueue(null);
  29. _queue.Enqueue("foo");
  30. _table["foo"]="barr";
  31. _table[1]="foo";
  32. _table['c'] = "barr";
  33. _table["barr"] = 1234567890;
  34. }
  35. public void OnTrucEvent(string s) {
  36. TrucEvent(s);
  37. }
  38. public void WriteString(string s) {
  39. _string = s;
  40. }
  41. public override bool Equals(object obj) {
  42. MoreComplexObject objReturn = obj as MoreComplexObject;
  43. if(objReturn == null) return false;
  44. if(objReturn._string != this._string) return false;
  45. IEnumerator myEnum = this._table.GetEnumerator();
  46. foreach(DictionaryEntry e in objReturn._table) {
  47. myEnum.MoveNext();
  48. DictionaryEntry s = (DictionaryEntry) myEnum.Current;
  49. Assertion.AssertEquals("#_table", s.Key, e.Key);
  50. Assertion.AssertEquals("#_table", s.Value, e.Value);
  51. if(s.Key.ToString() != e.Key.ToString() || s.Value.ToString() != e.Value.ToString()) return false;
  52. }
  53. // Assertion.Assert("#_table is null", objReturn._table != null);
  54. // Console.WriteLine("_table[foo]: {0}", objReturn._table["foo"]);
  55. // Assertion.AssertEquals("#_table[\"foo\"]", "barr", objReturn._table["foo"]);
  56. // Console.WriteLine("_table[1]: {0}", objReturn._table[1]);
  57. // Assertion.AssertEquals("#_table[1]", "foo", objReturn._table[1]);
  58. // Console.WriteLine("_table['c']: {0}", objReturn._table['c']);
  59. // Assertion.AssertEquals("#_table['c']", "barr", objReturn._table['c']);
  60. // Console.WriteLine("_table[barr]: {0}", objReturn._table["barr"]);
  61. // Assertion.AssertEquals("#_table[\"barr\"]", 1234567890, objReturn._table["barr"]);
  62. return SoapFormatterTest.CheckArray(this._queue.ToArray(), objReturn._queue.ToArray());
  63. }
  64. }
  65. [Serializable]
  66. internal class MarshalObject: MarshalByRefObject {
  67. private string _name;
  68. private long _id;
  69. public MarshalObject() {
  70. }
  71. public MarshalObject(string name, long id) {
  72. _name = name;
  73. _id = id;
  74. }
  75. }
  76. [Serializable]
  77. internal class SimpleObject {
  78. private string _name;
  79. private int _id;
  80. public SimpleObject(string name, int id) {
  81. _name = name;
  82. _id = id;
  83. }
  84. public override bool Equals(object obj) {
  85. SimpleObject objCmp = obj as SimpleObject;
  86. if(objCmp == null) return false;
  87. if(objCmp._name != this._name) return false;
  88. if(objCmp._id != this._id) return false;
  89. return true;
  90. }
  91. }
  92. [Serializable]
  93. internal class Version1 {
  94. public int _value;
  95. public Version1(int value) {
  96. _value = value;
  97. }
  98. }
  99. [Serializable]
  100. internal class Version2: ISerializable {
  101. public int _value;
  102. public string _foo;
  103. public Version2(int value, string foo) {
  104. _value = value;
  105. _foo = foo;
  106. }
  107. public void GetObjectData(SerializationInfo info, StreamingContext context) {
  108. info.AddValue("_value", _value);
  109. info.AddValue("_foo", _foo);
  110. }
  111. private Version2(SerializationInfo info, StreamingContext context) {
  112. _value = info.GetInt32("_value");
  113. try{
  114. _foo = info.GetString("_foo");
  115. }
  116. catch(SerializationException) {
  117. _foo = "Default value";
  118. }
  119. }
  120. }
  121. public class Version1ToVersion2Binder: SerializationBinder {
  122. public override Type BindToType (string assemblyName, string typeName) {
  123. Type returnType = null;
  124. string typeVersion1 = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version1";
  125. string assemName = Assembly.GetExecutingAssembly().FullName;
  126. if(typeName == typeVersion1) {
  127. typeName = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version2";
  128. }
  129. string typeFormat = String.Format("{0}, {1}", typeName, assemName);
  130. returnType = Type.GetType( typeFormat);
  131. return returnType;
  132. }
  133. }
  134. [TestFixture]
  135. public class SoapFormatterTest
  136. {
  137. private SoapFormatter _soapFormatter;
  138. private SoapFormatter _soapFormatterDeserializer;
  139. private RemotingSurrogateSelector _surrogate;
  140. private object Serialize(object objGraph) {
  141. MemoryStream stream = new MemoryStream();
  142. Assertion.Assert(objGraph != null);
  143. Assertion.Assert(stream != null);
  144. _soapFormatter.SurrogateSelector = _surrogate;
  145. _soapFormatter.Serialize(stream, objGraph);
  146. // stream.Position = 0;
  147. // StreamReader r = new StreamReader(stream);
  148. // Console.WriteLine(r.ReadToEnd());
  149. stream.Position = 0;
  150. object objReturn = _soapFormatterDeserializer.Deserialize(stream);
  151. Assertion.Assert(objReturn != null);
  152. Assertion.AssertEquals("#Tests "+objGraph.GetType(), objGraph.GetType(), objReturn.GetType());
  153. stream = new MemoryStream();
  154. _soapFormatter.Serialize(stream, objReturn);
  155. stream.Position = 0;
  156. // StreamReader r = new StreamReader(stream);
  157. // Console.WriteLine(r.ReadToEnd());
  158. return objReturn;
  159. }
  160. [SetUp]
  161. public void GetReady() {
  162. StreamingContext context = new StreamingContext(StreamingContextStates.All);
  163. _surrogate = new RemotingSurrogateSelector();
  164. _soapFormatter = new SoapFormatter(_surrogate, context);
  165. _soapFormatterDeserializer = new SoapFormatter(null, context);
  166. }
  167. [TearDown]
  168. public void Clean() {
  169. }
  170. [Test]
  171. public void TestValueTypes() {
  172. object objReturn;
  173. objReturn = Serialize((short)1);
  174. Assertion.AssertEquals("#int16", objReturn, 1);
  175. objReturn = Serialize(1);
  176. Assertion.AssertEquals("#int32", objReturn, 1);
  177. objReturn = Serialize((Single)0.1234);
  178. Assertion.AssertEquals("#Single", objReturn, 0.1234);
  179. objReturn = Serialize((Double)1234567890.0987654321);
  180. Assertion.AssertEquals("#iDouble", objReturn, 1234567890.0987654321);
  181. objReturn = Serialize(true);
  182. Assertion.AssertEquals("#Bool", objReturn, true);
  183. objReturn = Serialize((Int64) 1234567890);
  184. Assertion.AssertEquals("#Int64", objReturn, 1234567890);
  185. objReturn = Serialize('c');
  186. Assertion.AssertEquals("#Char", objReturn, 'c');
  187. }
  188. [Test]
  189. public void TestObjects() {
  190. object objReturn;
  191. objReturn = Serialize("");
  192. objReturn = Serialize("hello world!");
  193. Assertion.AssertEquals("#string", "hello world!", objReturn);
  194. SoapMessage soapMsg = new SoapMessage();
  195. soapMsg.Headers = new Header[0];
  196. soapMsg.MethodName = "Equals";
  197. soapMsg.ParamNames = new String[0];
  198. soapMsg.ParamTypes = new Type[0];
  199. soapMsg.ParamValues = new object[0];
  200. soapMsg.XmlNameSpace = SoapServices.CodeXmlNamespaceForClrTypeNamespace("String", "System");
  201. _soapFormatterDeserializer.TopObject = new SoapMessage();
  202. objReturn = Serialize(soapMsg);
  203. _soapFormatterDeserializer.TopObject = null;
  204. SimpleObject obj = new SimpleObject("simple object", 1);
  205. objReturn = Serialize(obj);
  206. Assertion.AssertEquals("#SimpleObject", obj, objReturn);
  207. objReturn = Serialize(typeof(SimpleObject));
  208. Assertion.AssertEquals("#Type", typeof(SimpleObject), (Type)objReturn);
  209. objReturn = Serialize(obj.GetType().Assembly);
  210. Assertion.AssertEquals("#Assembly", obj.GetType().Assembly, objReturn);
  211. }
  212. public static bool CheckArray(object objTest, object objReturn) {
  213. Array objTestAsArray = objTest as Array;
  214. Array objReturnAsArray = objReturn as Array;
  215. Assertion.Assert("#Not an Array "+objTest, objReturnAsArray != null);
  216. Assertion.AssertEquals("#Different lengths "+objTest, objTestAsArray.Length, objReturnAsArray.Length);
  217. IEnumerator iEnum = objReturnAsArray.GetEnumerator();
  218. iEnum.Reset();
  219. object obj2;
  220. foreach(object obj1 in objTestAsArray) {
  221. iEnum.MoveNext();
  222. obj2 = iEnum.Current;
  223. Assertion.AssertEquals("#The content of the 2 arrays is different", obj1, obj2);
  224. }
  225. return true;
  226. }
  227. [Test]
  228. public void TestArray() {
  229. object objReturn;
  230. object objTest;
  231. objReturn = Serialize(new int[]{});
  232. objTest = new int[]{1, 2, 3, 4};
  233. objReturn = Serialize(objTest);
  234. CheckArray(objTest, objReturn);
  235. objReturn = Serialize(new long[]{1, 2, 3, 4});
  236. objTest = new object[]{1, null, ":-)", 1234567890};
  237. objReturn = Serialize(objTest);
  238. objTest = new int[,]{{0, 1}, {2, 3}, {123, 4}};
  239. objReturn = Serialize(objTest);
  240. CheckArray(objTest, objReturn);
  241. object[,,] objArray = new object[3,2,1];
  242. objArray[0,0,0] = 1;
  243. objArray[2,1,0] = "end";
  244. objReturn = Serialize(objArray);
  245. CheckArray(objArray, objReturn);
  246. }
  247. [Test]
  248. public void TestMarshalByRefObject() {
  249. Serialize(new MarshalObject("thing", 1234567890));
  250. }
  251. [Test]
  252. [ExpectedException(typeof(ArgumentNullException))]
  253. public void TestNullObject() {
  254. MemoryStream stream = new MemoryStream();
  255. _soapFormatter.Serialize(stream, null);
  256. }
  257. [Test]
  258. [ExpectedException(typeof(SerializationException))]
  259. public void TestNonSerialisable() {
  260. Serialize(new NonSerializableObject());
  261. }
  262. [Test]
  263. public void TestMoreComplexObject() {
  264. MoreComplexObject objReturn;
  265. MoreComplexObject objTest = new MoreComplexObject();
  266. objReturn = (MoreComplexObject) Serialize(objTest);
  267. Assertion.AssertEquals("#Equals", objTest, objReturn);
  268. objReturn.OnTrucEvent("bidule");
  269. Assertion.AssertEquals("#dlg", "bidule", objReturn.ObjString);
  270. }
  271. [Test]
  272. public void TestSerializationbinder() {
  273. Object objReturn;
  274. MemoryStream stream = new MemoryStream();
  275. Version1 objVer1 = new Version1(123);
  276. _soapFormatter.SurrogateSelector = _surrogate;
  277. _soapFormatter.Serialize(stream, objVer1);
  278. stream.Position = 0;
  279. _soapFormatterDeserializer.Binder = new Version1ToVersion2Binder();
  280. objReturn = _soapFormatterDeserializer.Deserialize(stream);
  281. Assertion.AssertEquals("#Version1 Version2", "Version2", objReturn.GetType().Name);
  282. Assertion.AssertEquals("#_value", 123, ((Version2) objReturn)._value);
  283. Assertion.AssertEquals("#_foo", "Default value", ((Version2) objReturn)._foo);
  284. }
  285. }
  286. }