SoapFormatterTest.cs 11 KB

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