/trunk/source/Habanero.Test.Bo/TestBusinessObjectSerialisation.cs

# · C# · 229 lines · 164 code · 26 blank · 39 comment · 0 complexity · 97e9697d28bf7a641ac887ca84c12fd7 MD5 · raw file

  1. //---------------------------------------------------------------------------------
  2. // Copyright (C) 2008 Chillisoft Solutions
  3. //
  4. // This file is part of the Habanero framework.
  5. //
  6. // Habanero is a free framework: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // The Habanero framework is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with the Habanero framework. If not, see <http://www.gnu.org/licenses/>.
  18. //---------------------------------------------------------------------------------
  19. using System;
  20. using System.IO;
  21. using System.Runtime.Serialization;
  22. using System.Runtime.Serialization.Formatters.Binary;
  23. using Habanero.Base;
  24. using Habanero.BO;
  25. using Habanero.BO.ClassDefinition;
  26. using Habanero.Test.Structure;
  27. using NUnit.Framework;
  28. namespace Habanero.Test.BO
  29. {
  30. [TestFixture]
  31. public class TestBusinessObjectSerialisation
  32. {
  33. [Test]
  34. public void Test_SerialiseDeserialise_ReturnsCorrectType()
  35. {
  36. //---------------Set up test pack-------------------
  37. ClassDef.ClassDefs.Clear();
  38. BORegistry.DataAccessor = new DataAccessorInMemory();
  39. Structure.Car.LoadDefaultClassDef();
  40. OrganisationPerson.LoadDefaultClassDef();
  41. Person.LoadDefaultClassDef();
  42. Person originalPerson = Person.CreateSavedPerson();
  43. IFormatter formatter = new BinaryFormatter();
  44. MemoryStream memoryStream = new MemoryStream();
  45. BusinessObjectManager.Instance.ClearLoadedObjects();
  46. //---------------Execute Test ----------------------
  47. formatter.Serialize(memoryStream, originalPerson);
  48. memoryStream.Seek(0, SeekOrigin.Begin);
  49. Object deserialisedPerson = formatter.Deserialize(memoryStream);
  50. //---------------Test Result -----------------------
  51. Assert.IsInstanceOfType(typeof(Person),deserialisedPerson);
  52. }
  53. [Test]
  54. public void Test_SerialiseDeserialise_ReturnsCorrectClassDef()
  55. {
  56. //---------------Set up test pack-------------------
  57. ClassDef.ClassDefs.Clear();
  58. BORegistry.DataAccessor = new DataAccessorInMemory();
  59. Structure.Car.LoadDefaultClassDef();
  60. OrganisationPerson.LoadDefaultClassDef();
  61. ClassDef classDef = Person.LoadDefaultClassDef();
  62. Person originalPerson = Person.CreateSavedPerson();
  63. IFormatter formatter = new BinaryFormatter();
  64. MemoryStream memoryStream = new MemoryStream();
  65. BusinessObjectManager.Instance.ClearLoadedObjects();
  66. //---------------Execute Test ----------------------
  67. formatter.Serialize(memoryStream, originalPerson);
  68. memoryStream.Seek(0, SeekOrigin.Begin);
  69. Person deserialisedPerson = (Person)formatter.Deserialize(memoryStream);
  70. //---------------Test Result -----------------------
  71. Assert.AreSame(classDef, deserialisedPerson.ClassDef);
  72. }
  73. [Test]
  74. public void Test_SerialiseAndDeserialise()
  75. {
  76. //---------------Set up test pack-------------------
  77. ClassDef.ClassDefs.Clear();
  78. BORegistry.DataAccessor = new DataAccessorInMemory();
  79. Structure.Car.LoadDefaultClassDef();
  80. OrganisationPerson.LoadDefaultClassDef();
  81. Person.LoadDefaultClassDef();
  82. Person originalPerson = Person.CreateSavedPerson();
  83. IFormatter formatter = new BinaryFormatter();
  84. MemoryStream memoryStream = new MemoryStream();
  85. BusinessObjectManager.Instance.ClearLoadedObjects();
  86. //---------------Execute Test ----------------------
  87. formatter.Serialize(memoryStream, originalPerson);
  88. memoryStream.Seek(0, SeekOrigin.Begin);
  89. Person deserialisedPerson = (Person) formatter.Deserialize(memoryStream);
  90. //---------------Test Result -----------------------
  91. Assert.AreNotSame(deserialisedPerson, originalPerson);
  92. AssertPersonsAreEqual(originalPerson, deserialisedPerson);
  93. }
  94. [Test]
  95. public void Test_Serialise_AddBOPropAndDeserialise()
  96. {
  97. //---------------Set up test pack-------------------
  98. ClassDef.ClassDefs.Clear();
  99. BORegistry.DataAccessor = new DataAccessorInMemory();
  100. Structure.Car.LoadDefaultClassDef();
  101. OrganisationPerson.LoadDefaultClassDef();
  102. ClassDef personClassDef = Person.LoadDefaultClassDef();
  103. Person originalPerson = Person.CreateSavedPerson();
  104. IFormatter formatter = new BinaryFormatter();
  105. MemoryStream memoryStream = new MemoryStream();
  106. BusinessObjectManager.Instance.ClearLoadedObjects();
  107. //---------------Execute Test ----------------------
  108. formatter.Serialize(memoryStream, originalPerson);
  109. const string newpropertyName = "NewProperty";
  110. const string defaultValue = "some Default";
  111. personClassDef.PropDefcol.Add(new PropDef(newpropertyName, typeof(string),PropReadWriteRule.ReadWrite, defaultValue));
  112. memoryStream.Seek(0, SeekOrigin.Begin);
  113. Person deserialisedPerson = (Person) formatter.Deserialize(memoryStream);
  114. //---------------Test Result -----------------------
  115. Assert.AreNotSame(deserialisedPerson, originalPerson);
  116. AssertPersonsAreEqual(originalPerson, deserialisedPerson);
  117. Assert.AreEqual(defaultValue, deserialisedPerson.GetPropertyValue(newpropertyName));
  118. }
  119. private static void AssertPersonsAreEqual(IBusinessObject originalPerson, IBusinessObject deserialisedPerson)
  120. {
  121. foreach (IBOProp prop in originalPerson.Props)
  122. {
  123. Assert.AreEqual(prop.Value, deserialisedPerson.GetPropertyValue(prop.PropertyName));
  124. }
  125. }
  126. [Test]
  127. public void Test_SerialiseDeserialise_ReturnsCorrectStatus()
  128. {
  129. //---------------Set up test pack-------------------
  130. ClassDef.ClassDefs.Clear();
  131. BORegistry.DataAccessor = new DataAccessorInMemory();
  132. Structure.Car.LoadDefaultClassDef();
  133. OrganisationPerson.LoadDefaultClassDef();
  134. Person.LoadDefaultClassDef();
  135. Person originalPerson = Person.CreateSavedPerson();
  136. IFormatter formatter = new BinaryFormatter();
  137. MemoryStream memoryStream = new MemoryStream();
  138. BusinessObjectManager.Instance.ClearLoadedObjects();
  139. //---------------Execute Test ----------------------
  140. formatter.Serialize(memoryStream, originalPerson);
  141. memoryStream.Seek(0, SeekOrigin.Begin);
  142. Person deserialisedPerson = (Person)formatter.Deserialize(memoryStream);
  143. //---------------Test Result -----------------------
  144. Assert.AreEqual(originalPerson.Status,deserialisedPerson.Status);
  145. Assert.AreSame(deserialisedPerson, deserialisedPerson.Status.BusinessObject);
  146. }
  147. [Test]
  148. public void TestSerialiseDeserialiseBusinessObjectCollection()
  149. {
  150. //---------------Set up test pack-------------------
  151. ClassDef.ClassDefs.Clear();
  152. BORegistry.DataAccessor = new DataAccessorInMemory();
  153. Structure.Car.LoadDefaultClassDef();
  154. OrganisationPerson.LoadDefaultClassDef();
  155. Person.LoadDefaultClassDef();
  156. BusinessObjectCollection<Person> originalPeople = new BusinessObjectCollection<Person>();
  157. Person person1 = Person.CreateSavedPerson();
  158. originalPeople.Add(person1);
  159. Person person2 = Person.CreateSavedPerson();
  160. originalPeople.Add(person2);
  161. Person person3 = Person.CreateSavedPerson();
  162. originalPeople.Add(person3);
  163. IFormatter formatter = new BinaryFormatter();
  164. MemoryStream memoryStream = new MemoryStream();
  165. BusinessObjectManager.Instance.ClearLoadedObjects();
  166. //---------------Execute Test ----------------------
  167. formatter.Serialize(memoryStream, originalPeople);
  168. memoryStream.Seek(0, SeekOrigin.Begin);
  169. BusinessObjectCollection<Person> deserialisedPeople = (BusinessObjectCollection<Person>)formatter.Deserialize(memoryStream);
  170. //---------------Test Result -----------------------
  171. Assert.AreEqual(originalPeople.Count, deserialisedPeople.Count);
  172. Assert.AreNotSame(originalPeople,deserialisedPeople);
  173. AssertPersonsAreEqual(deserialisedPeople[0], originalPeople[0]);
  174. AssertPersonsAreEqual(deserialisedPeople[1], originalPeople[1]);
  175. AssertPersonsAreEqual(deserialisedPeople[2], originalPeople[2]);
  176. }
  177. [Test]
  178. public void TestSerialiseDeserialiseBusinessObjectCollection_CreatedBusObjAreIncluded()
  179. {
  180. //---------------Set up test pack-------------------
  181. ClassDef.ClassDefs.Clear();
  182. BORegistry.DataAccessor = new DataAccessorInMemory();
  183. Structure.Car.LoadDefaultClassDef();
  184. OrganisationPerson.LoadDefaultClassDef();
  185. Person.LoadDefaultClassDef();
  186. BusinessObjectCollection<Person> originalPeople = new BusinessObjectCollection<Person>();
  187. originalPeople.CreateBusinessObject();
  188. originalPeople.CreateBusinessObject();
  189. IFormatter formatter = new BinaryFormatter();
  190. MemoryStream memoryStream = new MemoryStream();
  191. BusinessObjectManager.Instance.ClearLoadedObjects();
  192. //---------------Execute Test ----------------------
  193. formatter.Serialize(memoryStream, originalPeople);
  194. memoryStream.Seek(0, SeekOrigin.Begin);
  195. BusinessObjectCollection<Person> deserialisedPeople = (BusinessObjectCollection<Person>)formatter.Deserialize(memoryStream);
  196. //---------------Test Result -----------------------
  197. Assert.AreEqual(originalPeople.Count, deserialisedPeople.Count);
  198. Assert.AreEqual(originalPeople.CreatedBusinessObjects.Count, deserialisedPeople.CreatedBusinessObjects.Count);
  199. AssertPersonsAreEqual(deserialisedPeople.CreatedBusinessObjects[0], originalPeople.CreatedBusinessObjects[0]);
  200. AssertPersonsAreEqual(deserialisedPeople.CreatedBusinessObjects[1], originalPeople.CreatedBusinessObjects[1]);
  201. }
  202. }
  203. }