1
0

SceneObjectSerializationTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Region.Framework.Scenes;
  39. using OpenSim.Region.Framework.Scenes.Serialization;
  40. using OpenSim.Services.Interfaces;
  41. using OpenSim.Tests.Common;
  42. namespace OpenSim.Region.Framework.Scenes.Tests
  43. {
  44. /// <summary>
  45. /// Basic scene object serialization tests.
  46. /// </summary>
  47. [TestFixture]
  48. public class SceneObjectSerializationTests : OpenSimTestCase
  49. {
  50. /// <summary>
  51. /// Serialize and deserialize.
  52. /// </summary>
  53. [Test]
  54. public void TestSerialDeserial()
  55. {
  56. TestHelpers.InMethod();
  57. Scene scene = new SceneHelpers().SetupScene();
  58. int partsToTestCount = 3;
  59. SceneObjectGroup so
  60. = SceneHelpers.CreateSceneObject(partsToTestCount, TestHelpers.ParseTail(0x1), "obj1", 0x10);
  61. SceneObjectPart[] parts = so.Parts;
  62. so.Name = "obj1";
  63. so.Description = "xpto";
  64. string xml = SceneObjectSerializer.ToXml2Format(so);
  65. Assert.That(!string.IsNullOrEmpty(xml), "SOG serialization resulted in empty or null string");
  66. XmlDocument doc = new XmlDocument();
  67. doc.LoadXml(xml);
  68. XmlNodeList nodes = doc.GetElementsByTagName("SceneObjectPart");
  69. Assert.That(nodes.Count, Is.EqualTo(3), "SOG serialization resulted in wrong number of SOPs");
  70. SceneObjectGroup so2 = SceneObjectSerializer.FromXml2Format(xml);
  71. Assert.IsNotNull(so2, "SOG deserialization resulted in null object");
  72. Assert.That(so2.Name == so.Name, "Name of deserialized object does not match original name");
  73. Assert.That(so2.Description == so.Description, "Description of deserialized object does not match original name");
  74. }
  75. /// <summary>
  76. /// This checks for a bug reported in mantis #7514
  77. /// </summary>
  78. [Test]
  79. public void TestNamespaceAttribute()
  80. {
  81. TestHelpers.InMethod();
  82. Scene scene = new SceneHelpers().SetupScene();
  83. UserAccount account = new UserAccount(UUID.Zero, UUID.Random(), "Test", "User", string.Empty);
  84. scene.UserAccountService.StoreUserAccount(account);
  85. int partsToTestCount = 1;
  86. SceneObjectGroup so
  87. = SceneHelpers.CreateSceneObject(partsToTestCount, TestHelpers.ParseTail(0x1), "obj1", 0x10);
  88. SceneObjectPart[] parts = so.Parts;
  89. so.Name = "obj1";
  90. so.Description = "xpto";
  91. so.OwnerID = account.PrincipalID;
  92. so.RootPart.CreatorID = so.OwnerID;
  93. string xml = SceneObjectSerializer.ToXml2Format(so);
  94. Assert.That(!string.IsNullOrEmpty(xml), "SOG serialization resulted in empty or null string");
  95. xml = ExternalRepresentationUtils.RewriteSOP(xml, "Test Scene", "http://localhost", scene.UserAccountService, UUID.Zero);
  96. //Console.WriteLine(xml);
  97. XmlDocument doc = new XmlDocument();
  98. doc.LoadXml(xml);
  99. XmlNodeList nodes = doc.GetElementsByTagName("SceneObjectPart");
  100. Assert.That(nodes.Count, Is.GreaterThan(0), "SOG serialization resulted in no SOPs");
  101. foreach (XmlAttribute a in nodes[0].Attributes)
  102. {
  103. int count = a.Name.Count(c => c == ':');
  104. Assert.That(count, Is.EqualTo(1), "Cannot have multiple ':' in attribute name in SOP");
  105. }
  106. nodes = doc.GetElementsByTagName("CreatorData");
  107. Assert.That(nodes.Count, Is.GreaterThan(0), "SOG serialization resulted in no CreatorData");
  108. foreach (XmlAttribute a in nodes[0].Attributes)
  109. {
  110. int count = a.Name.Count(c => c == ':');
  111. Assert.That(count, Is.EqualTo(1), "Cannot have multiple ':' in attribute name in CreatorData");
  112. }
  113. SceneObjectGroup so2 = SceneObjectSerializer.FromXml2Format(xml);
  114. Assert.IsNotNull(so2, "SOG deserialization resulted in null object");
  115. Assert.AreNotEqual(so.RootPart.CreatorIdentification, so2.RootPart.CreatorIdentification, "RewriteSOP failed to transform CreatorData.");
  116. Assert.That(so2.RootPart.CreatorIdentification.Contains("http://"), "RewriteSOP failed to add the homeURL to CreatorData");
  117. }
  118. }
  119. }