UserInventoryTestUtils.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 OpenMetaverse;
  29. using OpenSim.Framework;
  30. using OpenSim.Region.Framework.Scenes;
  31. using OpenSim.Services.Interfaces;
  32. namespace OpenSim.Tests.Common
  33. {
  34. /// <summary>
  35. /// Utility functions for carrying out user inventory related tests.
  36. /// </summary>
  37. public static class UserInventoryTestUtils
  38. {
  39. public static readonly string PATH_DELIMITER = "/";
  40. public static InventoryItemBase CreateInventoryItem(
  41. Scene scene, string itemName, UUID itemId, string folderPath, UUID userId)
  42. {
  43. InventoryItemBase item = new InventoryItemBase();
  44. item.Name = itemName;
  45. item.AssetID = AssetHelpers.CreateAsset(scene, userId).FullID;
  46. item.ID = itemId;
  47. // Really quite bad since the objs folder could be moved in the future and confuse the tests
  48. InventoryFolderBase objsFolder = scene.InventoryService.GetFolderForType(userId, AssetType.Object);
  49. item.Folder = objsFolder.ID;
  50. scene.AddInventoryItem(userId, item);
  51. return item;
  52. }
  53. /// <summary>
  54. /// Create inventory folders starting from the user's root folder.
  55. /// </summary>
  56. ///
  57. /// Ignores any existing folders with the same name
  58. ///
  59. /// <param name="inventoryService"></param>
  60. /// <param name="userId"></param>
  61. /// <param name="path">
  62. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  63. /// </param>
  64. /// <returns>
  65. /// The folder created. If the path contains multiple folders then the last one created is returned.
  66. /// Will return null if the root folder could not be found.
  67. /// </returns>
  68. public static InventoryFolderBase CreateInventoryFolder(
  69. IInventoryService inventoryService, UUID userId, string path)
  70. {
  71. InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId);
  72. if (null == rootFolder)
  73. return null;
  74. return CreateInventoryFolder(inventoryService, rootFolder, path);
  75. }
  76. /// <summary>
  77. /// Create inventory folders starting from a given parent folder
  78. /// </summary>
  79. ///
  80. /// Ignores any existing folders with the same name
  81. ///
  82. /// <param name="inventoryService"></param>
  83. /// <param name="parentFolder"></param>
  84. /// <param name="path">
  85. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  86. /// </param>
  87. /// <returns>
  88. /// The folder created. If the path contains multiple folders then the last one created is returned.
  89. /// </returns>
  90. public static InventoryFolderBase CreateInventoryFolder(
  91. IInventoryService inventoryService, InventoryFolderBase parentFolder, string path)
  92. {
  93. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  94. InventoryFolderBase newFolder
  95. = new InventoryFolderBase(UUID.Random(), components[0], parentFolder.Owner, parentFolder.ID);
  96. inventoryService.AddFolder(newFolder);
  97. if (components.Length > 1)
  98. return CreateInventoryFolder(inventoryService, newFolder, components[1]);
  99. else
  100. return newFolder;
  101. }
  102. }
  103. }