From 721c1085da22229f4d40529dd2738a2cf62a91b7 Mon Sep 17 00:00:00 2001 From: "Justin Clark-Casey (justincc)" Date: Fri, 21 May 2010 20:43:11 +0100 Subject: [PATCH] make oar object filename/pathname creation a helper method reused in both tests and oar code reduction in checking is outweighed by greater test clarity --- .../Serialization/ArchiveConstants.cs | 33 ++++++++++ .../World/Archiver/ArchiveHelpers.cs | 64 +++++++++++++++++++ .../World/Archiver/ArchiveReadRequest.cs | 1 - .../Archiver/ArchiveWriteRequestExecution.cs | 11 +--- .../World/Archiver/Tests/ArchiverTests.cs | 37 +++-------- 5 files changed, 108 insertions(+), 38 deletions(-) create mode 100644 OpenSim/Region/CoreModules/World/Archiver/ArchiveHelpers.cs diff --git a/OpenSim/Framework/Serialization/ArchiveConstants.cs b/OpenSim/Framework/Serialization/ArchiveConstants.cs index 1cd80db24d..475a9de0cb 100644 --- a/OpenSim/Framework/Serialization/ArchiveConstants.cs +++ b/OpenSim/Framework/Serialization/ArchiveConstants.cs @@ -25,6 +25,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System; using System.Collections.Generic; using OpenMetaverse; @@ -85,6 +86,11 @@ namespace OpenSim.Framework.Serialization /// public const string INVENTORY_NODE_NAME_COMPONENT_SEPARATOR = "__"; + /// + /// Template used for creating filenames in OpenSim Archives. + /// + public const string OAR_OBJECT_FILENAME_TEMPLATE = "{0}_{1:000}-{2:000}-{3:000}__{4}.xml"; + /// /// Extensions used for asset types in the archive /// @@ -139,5 +145,32 @@ namespace OpenSim.Framework.Serialization EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "texture.tga"] = (sbyte)AssetType.TextureTGA; EXTENSION_TO_ASSET_TYPE[ASSET_EXTENSION_SEPARATOR + "trashfolder.txt"] = (sbyte)AssetType.TrashFolder; } + + /// + /// Create the filename used to store an object in an OpenSim Archive. + /// + /// + /// + /// + /// + public static string CreateOarObjectFilename(string objectName, UUID uuid, Vector3 pos) + { + return string.Format( + OAR_OBJECT_FILENAME_TEMPLATE, objectName, + Math.Round(pos.X), Math.Round(pos.Y), Math.Round(pos.Z), + uuid); + } + + /// + /// Create the path used to store an object in an OpenSim Archives. + /// + /// + /// + /// + /// + public static string CreateOarObjectPath(string objectName, UUID uuid, Vector3 pos) + { + return OBJECTS_PATH + CreateOarObjectFilename(objectName, uuid, pos); + } } } diff --git a/OpenSim/Region/CoreModules/World/Archiver/ArchiveHelpers.cs b/OpenSim/Region/CoreModules/World/Archiver/ArchiveHelpers.cs new file mode 100644 index 0000000000..880bd7cdd4 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Archiver/ArchiveHelpers.cs @@ -0,0 +1,64 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSimulator Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenMetaverse; +using OpenSim.Framework.Serialization; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Archiver +{ + /// + /// Helper methods for archive manipulation + /// + /// This is a separate class from ArchiveConstants because we need to bring in very OpenSim specific classes. + public static class ArchiveHelpers + { + /// + /// Create the filename used for objects in OpenSim Archives. + /// + /// + /// + /// + /// + public static string CreateObjectFilename(SceneObjectGroup sog) + { + return ArchiveConstants.CreateOarObjectFilename(sog.Name, sog.UUID, sog.AbsolutePosition); + } + + /// + /// Create the path used to store an object in an OpenSim Archive. + /// + /// + /// + /// + /// + public static string CreateObjectPath(SceneObjectGroup sog) + { + return ArchiveConstants.CreateOarObjectPath(sog.Name, sog.UUID, sog.AbsolutePosition); + } + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs b/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs index c52f029c39..8d62fe1668 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs @@ -38,7 +38,6 @@ using OpenMetaverse; using OpenSim.Framework; using OpenSim.Framework.Serialization; using OpenSim.Framework.Serialization.External; - using OpenSim.Region.CoreModules.World.Terrain; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; diff --git a/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequestExecution.cs b/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequestExecution.cs index 9fc6ec47c2..586d98e322 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequestExecution.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequestExecution.cs @@ -145,17 +145,8 @@ namespace OpenSim.Region.CoreModules.World.Archiver { //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType()); - Vector3 position = sceneObject.AbsolutePosition; - string serializedObject = m_serialiser.SerializeGroupToXml2(sceneObject); - string filename - = string.Format( - "{0}{1}_{2:000}-{3:000}-{4:000}__{5}.xml", - ArchiveConstants.OBJECTS_PATH, sceneObject.Name, - Math.Round(position.X), Math.Round(position.Y), Math.Round(position.Z), - sceneObject.UUID); - - m_archiveWriter.WriteFile(filename, serializedObject); + m_archiveWriter.WriteFile(ArchiveHelpers.CreateObjectPath(sceneObject), serializedObject); } m_log.InfoFormat("[ARCHIVER]: Added scene objects to archive."); diff --git a/OpenSim/Region/CoreModules/World/Archiver/Tests/ArchiverTests.cs b/OpenSim/Region/CoreModules/World/Archiver/Tests/ArchiverTests.cs index f3e5458dce..4d04af16e7 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/Tests/ArchiverTests.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/Tests/ArchiverTests.cs @@ -26,6 +26,7 @@ */ using System; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading; @@ -129,7 +130,8 @@ namespace OpenSim.Region.CoreModules.World.Archiver.Tests //log4net.Config.XmlConfigurator.Configure(); SceneObjectPart part1 = CreateSceneObjectPart1(); - m_scene.AddNewSceneObject(new SceneObjectGroup(part1), false); + SceneObjectGroup sog1 = new SceneObjectGroup(part1); + m_scene.AddNewSceneObject(sog1, false); SceneObjectPart part2 = CreateSceneObjectPart2(); @@ -169,20 +171,13 @@ namespace OpenSim.Region.CoreModules.World.Archiver.Tests bool gotControlFile = false; bool gotNcAssetFile = false; - bool gotObject1File = false; - bool gotObject2File = false; string expectedNcAssetFileName = string.Format("{0}_{1}", ncAssetUuid, "notecard.txt"); - string expectedObject1FileName = string.Format( - "{0}_{1:000}-{2:000}-{3:000}__{4}.xml", - part1.Name, - Math.Round(part1.GroupPosition.X), Math.Round(part1.GroupPosition.Y), Math.Round(part1.GroupPosition.Z), - part1.UUID); - string expectedObject2FileName = string.Format( - "{0}_{1:000}-{2:000}-{3:000}__{4}.xml", - part2.Name, - Math.Round(part2.GroupPosition.X), Math.Round(part2.GroupPosition.Y), Math.Round(part2.GroupPosition.Z), - part2.UUID); + + List foundPaths = new List(); + List expectedPaths = new List(); + expectedPaths.Add(ArchiveHelpers.CreateObjectPath(sog1)); + expectedPaths.Add(ArchiveHelpers.CreateObjectPath(sog2)); string filePath; TarArchiveReader.TarEntryType tarEntryType; @@ -202,25 +197,13 @@ namespace OpenSim.Region.CoreModules.World.Archiver.Tests } else if (filePath.StartsWith(ArchiveConstants.OBJECTS_PATH)) { - string fileName = filePath.Remove(0, ArchiveConstants.OBJECTS_PATH.Length); - - if (fileName.StartsWith(part1.Name)) - { - Assert.That(fileName, Is.EqualTo(expectedObject1FileName)); - gotObject1File = true; - } - else if (fileName.StartsWith(part2.Name)) - { - Assert.That(fileName, Is.EqualTo(expectedObject2FileName)); - gotObject2File = true; - } + foundPaths.Add(filePath); } } Assert.That(gotControlFile, Is.True, "No control file in archive"); Assert.That(gotNcAssetFile, Is.True, "No notecard asset file in archive"); - Assert.That(gotObject1File, Is.True, "No object1 file in archive"); - Assert.That(gotObject2File, Is.True, "No object2 file in archive"); + Assert.That(foundPaths, Is.EquivalentTo(expectedPaths)); // TODO: Test presence of more files and contents of files. }