PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/source/library/Interlace.Tests/Amf/ObjectTests.cs

https://bitbucket.org/VahidN/interlace
C# | 235 lines | 163 code | 44 blank | 28 comment | 4 complexity | e8e23cc05c99c5fa4e850df1951c34b4 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using MbUnit.Framework;
  32. using Interlace.Amf;
  33. #endregion
  34. namespace Interlace.Tests.Amf
  35. {
  36. [TestFixture]
  37. public class ObjectTests
  38. {
  39. [AmfClass("bitplantation.EmptyTest")]
  40. class BitPlantationEmptyTest
  41. {
  42. int _b;
  43. [AmfProperty("_b")]
  44. public int B
  45. {
  46. get { return _b; }
  47. set { _b = value; }
  48. }
  49. }
  50. [AmfClass("bitplantation.Test")]
  51. class BitPlantationTest
  52. {
  53. string _a;
  54. int _b;
  55. [AmfProperty]
  56. public string A
  57. {
  58. get { return _a; }
  59. set { _a = value; }
  60. }
  61. [AmfProperty("_b")]
  62. public int B
  63. {
  64. get { return _b; }
  65. set { _b = value; }
  66. }
  67. }
  68. [AmfClass("bitplantation.DynTest")]
  69. class BitPlantationDynTest : AmfObject
  70. {
  71. string _a;
  72. int _b;
  73. [AmfProperty]
  74. public string A
  75. {
  76. get { return _a; }
  77. set { _a = value; }
  78. }
  79. [AmfProperty("_b")]
  80. public int B
  81. {
  82. get { return _b; }
  83. set { _b = value; }
  84. }
  85. }
  86. public object HelperDeserialize(byte[] data)
  87. {
  88. AmfRegistry registry = new AmfRegistry();
  89. registry.RegisterClassAlias(typeof(BitPlantationEmptyTest));
  90. registry.RegisterClassAlias(typeof(BitPlantationTest));
  91. registry.RegisterClassAlias(typeof(BitPlantationDynTest));
  92. return AmfReader.Read(registry, data);
  93. }
  94. [Test]
  95. public void TestObjects()
  96. {
  97. BitPlantationEmptyTest emptyTest = HelperDeserialize(
  98. new byte[] {0x0a, 0x13, 0x2f, 0x62, 0x69, 0x74, 0x70, 0x6c,
  99. 0x61, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e,
  100. 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x54, 0x65,
  101. 0x73, 0x74, 0x05, 0x5f, 0x62, 0x04, 0x02}) as BitPlantationEmptyTest;
  102. Assert.AreEqual(emptyTest.B, 2);
  103. BitPlantationTest test = HelperDeserialize(
  104. new byte[] {0x0a, 0x23, 0x25, 0x62, 0x69, 0x74, 0x70, 0x6c,
  105. 0x61, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e,
  106. 0x2e, 0x54, 0x65, 0x73, 0x74, 0x03, 0x41, 0x05,
  107. 0x5f, 0x62, 0x06, 0x07, 0x63, 0x61, 0x74, 0x04,
  108. 0x02}) as BitPlantationTest;
  109. Assert.AreEqual(test.A, "cat");
  110. Assert.AreEqual(test.B, 2);
  111. BitPlantationDynTest dynTest = HelperDeserialize(
  112. new byte[] {0x0a, 0x2b, 0x2b, 0x62, 0x69, 0x74, 0x70, 0x6c,
  113. 0x61, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e,
  114. 0x2e, 0x44, 0x79, 0x6e, 0x54, 0x65, 0x73, 0x74,
  115. 0x03, 0x41, 0x05, 0x5f, 0x62, 0x06, 0x07, 0x63,
  116. 0x61, 0x74, 0x04, 0x02, 0x1d, 0x4f, 0x74, 0x68,
  117. 0x65, 0x72, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62,
  118. 0x75, 0x74, 0x65, 0x04, 0x01, 0x21, 0x44, 0x79,
  119. 0x6e, 0x61, 0x6d, 0x69, 0x63, 0x41, 0x74, 0x74,
  120. 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x06, 0x09,
  121. 0x74, 0x65, 0x73, 0x74, 0x01}) as BitPlantationDynTest;
  122. Assert.AreEqual(dynTest.A, "cat");
  123. Assert.AreEqual(dynTest.B, 2);
  124. Assert.AreEqual(dynTest.Properties["DynamicAttribute"], "test");
  125. Assert.AreEqual(dynTest.Properties["OtherAttribute"], 1);
  126. }
  127. T RoundTrip<T>(T obj)
  128. {
  129. AmfRegistry registry = new AmfRegistry();
  130. registry.RegisterClassAlias(typeof(BitPlantationEmptyTest));
  131. registry.RegisterClassAlias(typeof(BitPlantationTest));
  132. registry.RegisterClassAlias(typeof(BitPlantationDynTest));
  133. byte[] data = AmfWriter.Write(registry, obj);
  134. return (T)AmfReader.Read(registry, data);
  135. }
  136. [Test]
  137. public void TestRoundTripping()
  138. {
  139. // Test an empty class:
  140. BitPlantationEmptyTest beforeEmpty = new BitPlantationEmptyTest();
  141. beforeEmpty.B = 2;
  142. BitPlantationEmptyTest afterEmpty = RoundTrip(beforeEmpty);
  143. Assert.AreEqual(afterEmpty.B, 2);
  144. // Test a static class:
  145. BitPlantationTest before = new BitPlantationTest();
  146. before.A = "cat";
  147. before.B = 2;
  148. BitPlantationTest after = RoundTrip(before);
  149. // Test a dynamic class:
  150. BitPlantationDynTest beforeDyn = new BitPlantationDynTest();
  151. beforeDyn.A = "cat";
  152. beforeDyn.B = 2;
  153. beforeDyn.Properties["DynamicAttribute"] = "test";
  154. beforeDyn.Properties["OtherAttribute"] = 1;
  155. BitPlantationDynTest afterDyn = RoundTrip(beforeDyn);
  156. Assert.AreEqual(afterDyn.A, "cat");
  157. Assert.AreEqual(afterDyn.B, 2);
  158. Assert.AreEqual(afterDyn.Properties["DynamicAttribute"], "test");
  159. Assert.AreEqual(afterDyn.Properties["OtherAttribute"], 1);
  160. }
  161. }
  162. [AmfClass("bitplantation.SinglePropertyTest")]
  163. public class BitPlantationSinglePropertyTest
  164. {
  165. string _a;
  166. public BitPlantationSinglePropertyTest()
  167. {
  168. }
  169. public BitPlantationSinglePropertyTest(string a)
  170. {
  171. _a = a;
  172. }
  173. [AmfProperty]
  174. public string A
  175. {
  176. get { return _a; }
  177. set { _a = value; }
  178. }
  179. public override bool Equals(object obj)
  180. {
  181. BitPlantationSinglePropertyTest rhs = obj as BitPlantationSinglePropertyTest;
  182. if (rhs == null) return false;
  183. return object.Equals(_a, rhs._a);
  184. }
  185. public override int GetHashCode()
  186. {
  187. if (_a == null) return 0;
  188. return _a.GetHashCode();
  189. }
  190. }
  191. }