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
soprefactor
Justin Clark-Casey (justincc) 2010-05-21 20:43:11 +01:00
parent 682efe9463
commit 721c1085da
5 changed files with 108 additions and 38 deletions

View File

@ -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
/// </value>
public const string INVENTORY_NODE_NAME_COMPONENT_SEPARATOR = "__";
/// <summary>
/// Template used for creating filenames in OpenSim Archives.
/// </summary>
public const string OAR_OBJECT_FILENAME_TEMPLATE = "{0}_{1:000}-{2:000}-{3:000}__{4}.xml";
/// <value>
/// Extensions used for asset types in the archive
/// </value>
@ -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;
}
/// <summary>
/// Create the filename used to store an object in an OpenSim Archive.
/// </summary>
/// <param name="objectName"></param>
/// <param name="uuid"></param>
/// <param name="pos"></param>
/// <returns></returns>
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);
}
/// <summary>
/// Create the path used to store an object in an OpenSim Archives.
/// </summary>
/// <param name="objectName"></param>
/// <param name="uuid"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static string CreateOarObjectPath(string objectName, UUID uuid, Vector3 pos)
{
return OBJECTS_PATH + CreateOarObjectFilename(objectName, uuid, pos);
}
}
}

View File

@ -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
{
/// <summary>
/// Helper methods for archive manipulation
/// </summary>
/// This is a separate class from ArchiveConstants because we need to bring in very OpenSim specific classes.
public static class ArchiveHelpers
{
/// <summary>
/// Create the filename used for objects in OpenSim Archives.
/// </summary>
/// <param name="objectName"></param>
/// <param name="uuid"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static string CreateObjectFilename(SceneObjectGroup sog)
{
return ArchiveConstants.CreateOarObjectFilename(sog.Name, sog.UUID, sog.AbsolutePosition);
}
/// <summary>
/// Create the path used to store an object in an OpenSim Archive.
/// </summary>
/// <param name="objectName"></param>
/// <param name="uuid"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static string CreateObjectPath(SceneObjectGroup sog)
{
return ArchiveConstants.CreateOarObjectPath(sog.Name, sog.UUID, sog.AbsolutePosition);
}
}
}

View File

@ -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;

View File

@ -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.");

View File

@ -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<string> foundPaths = new List<string>();
List<string> expectedPaths = new List<string>();
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.
}