SceneObjectSerializationTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  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 OpenSimulator Project 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 DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Xml;
  32. using System.Linq;
  33. using Nini.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Serialization.External;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.Framework.Scenes.Serialization;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Tests.Common;
  43. namespace OpenSim.Region.Framework.Scenes.Tests
  44. {
  45. /// <summary>
  46. /// Basic scene object serialization tests.
  47. /// </summary>
  48. [TestFixture]
  49. public class SceneObjectSerializationTests : OpenSimTestCase
  50. {
  51. /// <summary>
  52. /// Serialize and deserialize.
  53. /// </summary>
  54. [Test]
  55. public void TestSerialDeserial()
  56. {
  57. TestHelpers.InMethod();
  58. Scene scene = new SceneHelpers().SetupScene();
  59. int partsToTestCount = 3;
  60. SceneObjectGroup so
  61. = SceneHelpers.CreateSceneObject(partsToTestCount, TestHelpers.ParseTail(0x1), "obj1", 0x10);
  62. SceneObjectPart[] parts = so.Parts;
  63. so.Name = "obj1";
  64. so.Description = "xpto";
  65. string xml = SceneObjectSerializer.ToXml2Format(so);
  66. Assert.That(!string.IsNullOrEmpty(xml), "SOG serialization resulted in empty or null string");
  67. XmlDocument doc = new XmlDocument();
  68. doc.LoadXml(xml);
  69. XmlNodeList nodes = doc.GetElementsByTagName("SceneObjectPart");
  70. Assert.That(nodes.Count, Is.EqualTo(3), "SOG serialization resulted in wrong number of SOPs");
  71. SceneObjectGroup so2 = SceneObjectSerializer.FromXml2Format(xml);
  72. Assert.IsNotNull(so2, "SOG deserialization resulted in null object");
  73. Assert.That(so2.Name == so.Name, "Name of deserialized object does not match original name");
  74. Assert.That(so2.Description == so.Description, "Description of deserialized object does not match original name");
  75. }
  76. /// <summary>
  77. /// This checks for a bug reported in mantis #7514
  78. /// </summary>
  79. [Test]
  80. public void TestNamespaceAttribute()
  81. {
  82. TestHelpers.InMethod();
  83. Scene scene = new SceneHelpers().SetupScene();
  84. UserAccount account = new UserAccount(UUID.Zero, UUID.Random(), "Test", "User", string.Empty);
  85. scene.UserAccountService.StoreUserAccount(account);
  86. int partsToTestCount = 1;
  87. SceneObjectGroup so
  88. = SceneHelpers.CreateSceneObject(partsToTestCount, TestHelpers.ParseTail(0x1), "obj1", 0x10);
  89. SceneObjectPart[] parts = so.Parts;
  90. so.Name = "obj1";
  91. so.Description = "xpto";
  92. so.OwnerID = account.PrincipalID;
  93. so.RootPart.CreatorID = so.OwnerID;
  94. string xml = SceneObjectSerializer.ToXml2Format(so);
  95. Assert.That(!string.IsNullOrEmpty(xml), "SOG serialization resulted in empty or null string");
  96. xml = ExternalRepresentationUtils.RewriteSOP(xml, "Test Scene", "http://localhost", scene.UserAccountService, UUID.Zero);
  97. //Console.WriteLine(xml);
  98. XmlDocument doc = new XmlDocument();
  99. doc.LoadXml(xml);
  100. XmlNodeList nodes = doc.GetElementsByTagName("SceneObjectPart");
  101. Assert.That(nodes.Count, Is.GreaterThan(0), "SOG serialization resulted in no SOPs");
  102. foreach (XmlAttribute a in nodes[0].Attributes)
  103. {
  104. int count = a.Name.Count(c => c == ':');
  105. Assert.That(count, Is.EqualTo(1), "Cannot have multiple ':' in attribute name in SOP");
  106. }
  107. nodes = doc.GetElementsByTagName("CreatorData");
  108. Assert.That(nodes.Count, Is.GreaterThan(0), "SOG serialization resulted in no CreatorData");
  109. foreach (XmlAttribute a in nodes[0].Attributes)
  110. {
  111. int count = a.Name.Count(c => c == ':');
  112. Assert.That(count, Is.EqualTo(1), "Cannot have multiple ':' in attribute name in CreatorData");
  113. }
  114. SceneObjectGroup so2 = SceneObjectSerializer.FromXml2Format(xml);
  115. Assert.IsNotNull(so2, "SOG deserialization resulted in null object");
  116. Assert.AreNotEqual(so.RootPart.CreatorIdentification, so2.RootPart.CreatorIdentification, "RewriteSOP failed to transform CreatorData.");
  117. Assert.That(so2.RootPart.CreatorIdentification.Contains("http://"), "RewriteSOP failed to add the homeURL to CreatorData");
  118. }
  119. }
  120. }