BasicInventoryTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 log4net.Config;
  29. using NUnit.Framework;
  30. using NUnit.Framework.SyntaxHelpers;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using log4net;
  34. using System.Reflection;
  35. namespace OpenSim.Data.Tests
  36. {
  37. public class BasicInventoryTest
  38. {
  39. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public IInventoryDataPlugin db;
  41. public UUID zero = UUID.Zero;
  42. public UUID folder1;
  43. public UUID folder2;
  44. public UUID folder3;
  45. public UUID owner1;
  46. public UUID owner2;
  47. public UUID owner3;
  48. public UUID item1;
  49. public UUID item2;
  50. public UUID item3;
  51. public UUID asset1;
  52. public UUID asset2;
  53. public UUID asset3;
  54. public string name1;
  55. public string name2;
  56. public string name3;
  57. public string niname1;
  58. public string iname1;
  59. public string iname2;
  60. public string iname3;
  61. public void SuperInit()
  62. {
  63. OpenSim.Tests.Common.TestLogging.LogToConsole();
  64. folder1 = UUID.Random();
  65. folder2 = UUID.Random();
  66. folder3 = UUID.Random();
  67. owner1 = UUID.Random();
  68. owner2 = UUID.Random();
  69. owner3 = UUID.Random();
  70. item1 = UUID.Random();
  71. item2 = UUID.Random();
  72. item3 = UUID.Random();
  73. asset1 = UUID.Random();
  74. asset2 = UUID.Random();
  75. asset3 = UUID.Random();
  76. name1 = "Root Folder for " + owner1.ToString();
  77. name2 = "First Level folder";
  78. name3 = "First Level folder 2";
  79. niname1 = "My Shirt";
  80. iname1 = "Shirt";
  81. iname2 = "Text Board";
  82. iname3 = "No Pants Barrel";
  83. }
  84. [Test]
  85. public void T001_LoadEmpty()
  86. {
  87. Assert.That(db.getInventoryFolder(zero), Is.Null);
  88. Assert.That(db.getInventoryFolder(folder1), Is.Null);
  89. Assert.That(db.getInventoryFolder(folder2), Is.Null);
  90. Assert.That(db.getInventoryFolder(folder3), Is.Null);
  91. Assert.That(db.getInventoryItem(zero), Is.Null);
  92. Assert.That(db.getInventoryItem(item1), Is.Null);
  93. Assert.That(db.getInventoryItem(item2), Is.Null);
  94. Assert.That(db.getInventoryItem(item3), Is.Null);
  95. Assert.That(db.getUserRootFolder(zero), Is.Null);
  96. Assert.That(db.getUserRootFolder(owner1), Is.Null);
  97. }
  98. // 01x - folder tests
  99. [Test]
  100. public void T010_FolderNonParent()
  101. {
  102. InventoryFolderBase f1 = NewFolder(folder2, folder1, owner1, name2);
  103. // the folder will go in
  104. db.addInventoryFolder(f1);
  105. InventoryFolderBase f1a = db.getUserRootFolder(owner1);
  106. Assert.That(f1a, Is.Null);
  107. }
  108. [Test]
  109. public void T011_FolderCreate()
  110. {
  111. InventoryFolderBase f1 = NewFolder(folder1, zero, owner1, name1);
  112. // TODO: this is probably wrong behavior, but is what we have
  113. // db.updateInventoryFolder(f1);
  114. // InventoryFolderBase f1a = db.getUserRootFolder(owner1);
  115. // Assert.That(uuid1, Is.EqualTo(f1a.ID))
  116. // Assert.That(name1, Text.Matches(f1a.Name), "Assert.That(name1, Text.Matches(f1a.Name))");
  117. // Assert.That(db.getUserRootFolder(owner1), Is.Null);
  118. // succeed with true
  119. db.addInventoryFolder(f1);
  120. InventoryFolderBase f1a = db.getUserRootFolder(owner1);
  121. Assert.That(folder1, Is.EqualTo(f1a.ID), "Assert.That(folder1, Is.EqualTo(f1a.ID))");
  122. Assert.That(name1, Text.Matches(f1a.Name), "Assert.That(name1, Text.Matches(f1a.Name))");
  123. }
  124. // we now have the following tree
  125. // folder1
  126. // +--- folder2
  127. // +--- folder3
  128. [Test]
  129. public void T012_FolderList()
  130. {
  131. InventoryFolderBase f2 = NewFolder(folder3, folder1, owner1, name3);
  132. db.addInventoryFolder(f2);
  133. Assert.That(db.getInventoryFolders(zero).Count, Is.EqualTo(1), "Assert.That(db.getInventoryFolders(zero).Count, Is.EqualTo(1))");
  134. Assert.That(db.getInventoryFolders(folder1).Count, Is.EqualTo(2), "Assert.That(db.getInventoryFolders(folder1).Count, Is.EqualTo(2))");
  135. Assert.That(db.getInventoryFolders(folder2).Count, Is.EqualTo(0), "Assert.That(db.getInventoryFolders(folder2).Count, Is.EqualTo(0))");
  136. Assert.That(db.getInventoryFolders(folder3).Count, Is.EqualTo(0), "Assert.That(db.getInventoryFolders(folder3).Count, Is.EqualTo(0))");
  137. Assert.That(db.getInventoryFolders(UUID.Random()).Count, Is.EqualTo(0), "Assert.That(db.getInventoryFolders(UUID.Random()).Count, Is.EqualTo(0))");
  138. }
  139. [Test]
  140. public void T013_FolderHierarchy()
  141. {
  142. Assert.That(db.getFolderHierarchy(zero).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(zero).Count, Is.EqualTo(0))");
  143. Assert.That(db.getFolderHierarchy(folder1).Count, Is.EqualTo(2), "Assert.That(db.getFolderHierarchy(folder1).Count, Is.EqualTo(2))");
  144. Assert.That(db.getFolderHierarchy(folder2).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(folder2).Count, Is.EqualTo(0))");
  145. Assert.That(db.getFolderHierarchy(folder3).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(folder3).Count, Is.EqualTo(0))");
  146. Assert.That(db.getFolderHierarchy(UUID.Random()).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(UUID.Random()).Count, Is.EqualTo(0))");
  147. }
  148. [Test]
  149. public void T014_MoveFolder()
  150. {
  151. InventoryFolderBase f2 = db.getInventoryFolder(folder2);
  152. f2.ParentID = folder3;
  153. db.moveInventoryFolder(f2);
  154. Assert.That(db.getInventoryFolders(zero).Count, Is.EqualTo(1), "Assert.That(db.getInventoryFolders(zero).Count, Is.EqualTo(1))");
  155. Assert.That(db.getInventoryFolders(folder1).Count, Is.EqualTo(1), "Assert.That(db.getInventoryFolders(folder1).Count, Is.EqualTo(1))");
  156. Assert.That(db.getInventoryFolders(folder2).Count, Is.EqualTo(0), "Assert.That(db.getInventoryFolders(folder2).Count, Is.EqualTo(0))");
  157. Assert.That(db.getInventoryFolders(folder3).Count, Is.EqualTo(1), "Assert.That(db.getInventoryFolders(folder3).Count, Is.EqualTo(1))");
  158. Assert.That(db.getInventoryFolders(UUID.Random()).Count, Is.EqualTo(0), "Assert.That(db.getInventoryFolders(UUID.Random()).Count, Is.EqualTo(0))");
  159. }
  160. [Test]
  161. public void T015_FolderHierarchy()
  162. {
  163. Assert.That(db.getFolderHierarchy(zero).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(zero).Count, Is.EqualTo(0))");
  164. Assert.That(db.getFolderHierarchy(folder1).Count, Is.EqualTo(2), "Assert.That(db.getFolderHierarchy(folder1).Count, Is.EqualTo(2))");
  165. Assert.That(db.getFolderHierarchy(folder2).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(folder2).Count, Is.EqualTo(0))");
  166. Assert.That(db.getFolderHierarchy(folder3).Count, Is.EqualTo(1), "Assert.That(db.getFolderHierarchy(folder3).Count, Is.EqualTo(1))");
  167. Assert.That(db.getFolderHierarchy(UUID.Random()).Count, Is.EqualTo(0), "Assert.That(db.getFolderHierarchy(UUID.Random()).Count, Is.EqualTo(0))");
  168. }
  169. // Item tests
  170. [Test]
  171. public void T100_NoItems()
  172. {
  173. Assert.That(db.getInventoryInFolder(zero).Count, Is.EqualTo(0), "Assert.That(db.getInventoryInFolder(zero).Count, Is.EqualTo(0))");
  174. Assert.That(db.getInventoryInFolder(folder1).Count, Is.EqualTo(0), "Assert.That(db.getInventoryInFolder(folder1).Count, Is.EqualTo(0))");
  175. Assert.That(db.getInventoryInFolder(folder2).Count, Is.EqualTo(0), "Assert.That(db.getInventoryInFolder(folder2).Count, Is.EqualTo(0))");
  176. Assert.That(db.getInventoryInFolder(folder3).Count, Is.EqualTo(0), "Assert.That(db.getInventoryInFolder(folder3).Count, Is.EqualTo(0))");
  177. }
  178. // TODO: Feeding a bad inventory item down the data path will
  179. // crash the system. This is largely due to the builder
  180. // routines. That should be fixed and tested for.
  181. [Test]
  182. public void T101_CreatItems()
  183. {
  184. db.addInventoryItem(NewItem(item1, folder3, owner1, iname1, asset1));
  185. db.addInventoryItem(NewItem(item2, folder3, owner1, iname2, asset2));
  186. db.addInventoryItem(NewItem(item3, folder3, owner1, iname3, asset3));
  187. Assert.That(db.getInventoryInFolder(folder3).Count, Is.EqualTo(3), "Assert.That(db.getInventoryInFolder(folder3).Count, Is.EqualTo(3))");
  188. }
  189. [Test]
  190. public void T102_CompareItems()
  191. {
  192. InventoryItemBase i1 = db.getInventoryItem(item1);
  193. InventoryItemBase i2 = db.getInventoryItem(item2);
  194. InventoryItemBase i3 = db.getInventoryItem(item3);
  195. Assert.That(i1.Name, Is.EqualTo(iname1), "Assert.That(i1.Name, Is.EqualTo(iname1))");
  196. Assert.That(i2.Name, Is.EqualTo(iname2), "Assert.That(i2.Name, Is.EqualTo(iname2))");
  197. Assert.That(i3.Name, Is.EqualTo(iname3), "Assert.That(i3.Name, Is.EqualTo(iname3))");
  198. Assert.That(i1.Owner, Is.EqualTo(owner1), "Assert.That(i1.Owner, Is.EqualTo(owner1))");
  199. Assert.That(i2.Owner, Is.EqualTo(owner1), "Assert.That(i2.Owner, Is.EqualTo(owner1))");
  200. Assert.That(i3.Owner, Is.EqualTo(owner1), "Assert.That(i3.Owner, Is.EqualTo(owner1))");
  201. Assert.That(i1.AssetID, Is.EqualTo(asset1), "Assert.That(i1.AssetID, Is.EqualTo(asset1))");
  202. Assert.That(i2.AssetID, Is.EqualTo(asset2), "Assert.That(i2.AssetID, Is.EqualTo(asset2))");
  203. Assert.That(i3.AssetID, Is.EqualTo(asset3), "Assert.That(i3.AssetID, Is.EqualTo(asset3))");
  204. }
  205. [Test]
  206. public void T103_UpdateItem()
  207. {
  208. // TODO: probably shouldn't have the ability to have an
  209. // owner of an item in a folder not owned by the user
  210. InventoryItemBase i1 = db.getInventoryItem(item1);
  211. i1.Name = niname1;
  212. i1.Description = niname1;
  213. i1.Owner = owner2;
  214. db.updateInventoryItem(i1);
  215. i1 = db.getInventoryItem(item1);
  216. Assert.That(i1.Name, Is.EqualTo(niname1), "Assert.That(i1.Name, Is.EqualTo(niname1))");
  217. Assert.That(i1.Description, Is.EqualTo(niname1), "Assert.That(i1.Description, Is.EqualTo(niname1))");
  218. Assert.That(i1.Owner, Is.EqualTo(owner2), "Assert.That(i1.Owner, Is.EqualTo(owner2))");
  219. }
  220. [Test]
  221. public void T104_RandomUpdateItem()
  222. {
  223. PropertyScrambler<InventoryFolderBase> folderScrambler =
  224. new PropertyScrambler<InventoryFolderBase>()
  225. .DontScramble(x => x.Owner)
  226. .DontScramble(x => x.ParentID)
  227. .DontScramble(x => x.ID);
  228. UUID owner = UUID.Random();
  229. UUID folder = UUID.Random();
  230. UUID rootId = UUID.Random();
  231. UUID rootAsset = UUID.Random();
  232. InventoryFolderBase f1 = NewFolder(folder, zero, owner, name1);
  233. folderScrambler.Scramble(f1);
  234. db.addInventoryFolder(f1);
  235. InventoryFolderBase f1a = db.getUserRootFolder(owner);
  236. Assert.That(f1a, Constraints.PropertyCompareConstraint(f1));
  237. folderScrambler.Scramble(f1a);
  238. db.updateInventoryFolder(f1a);
  239. InventoryFolderBase f1b = db.getUserRootFolder(owner);
  240. Assert.That(f1b, Constraints.PropertyCompareConstraint(f1a));
  241. //Now we have a valid folder to insert into, we can insert the item.
  242. PropertyScrambler<InventoryItemBase> inventoryScrambler =
  243. new PropertyScrambler<InventoryItemBase>()
  244. .DontScramble(x => x.ID)
  245. .DontScramble(x => x.AssetID)
  246. .DontScramble(x => x.Owner)
  247. .DontScramble(x => x.Folder);
  248. InventoryItemBase root = NewItem(rootId, folder, owner, iname1, rootAsset);
  249. inventoryScrambler.Scramble(root);
  250. db.addInventoryItem(root);
  251. InventoryItemBase expected = db.getInventoryItem(rootId);
  252. Assert.That(expected, Constraints.PropertyCompareConstraint(root)
  253. .IgnoreProperty(x => x.InvType)
  254. .IgnoreProperty(x => x.CreatorIdAsUuid)
  255. .IgnoreProperty(x => x.Description)
  256. .IgnoreProperty(x => x.CreatorId));
  257. inventoryScrambler.Scramble(expected);
  258. db.updateInventoryItem(expected);
  259. InventoryItemBase actual = db.getInventoryItem(rootId);
  260. Assert.That(actual, Constraints.PropertyCompareConstraint(expected)
  261. .IgnoreProperty(x => x.InvType)
  262. .IgnoreProperty(x => x.CreatorIdAsUuid)
  263. .IgnoreProperty(x => x.Description)
  264. .IgnoreProperty(x => x.CreatorId));
  265. }
  266. [Test]
  267. public void T999_StillNull()
  268. {
  269. // After all tests are run, these should still return no results
  270. Assert.That(db.getInventoryFolder(zero), Is.Null);
  271. Assert.That(db.getInventoryItem(zero), Is.Null);
  272. Assert.That(db.getUserRootFolder(zero), Is.Null);
  273. Assert.That(db.getInventoryInFolder(zero).Count, Is.EqualTo(0), "Assert.That(db.getInventoryInFolder(zero).Count, Is.EqualTo(0))");
  274. }
  275. private InventoryItemBase NewItem(UUID id, UUID parent, UUID owner, string name, UUID asset)
  276. {
  277. InventoryItemBase i = new InventoryItemBase();
  278. i.ID = id;
  279. i.Folder = parent;
  280. i.Owner = owner;
  281. i.CreatorId = owner.ToString();
  282. i.Name = name;
  283. i.Description = name;
  284. i.AssetID = asset;
  285. return i;
  286. }
  287. private InventoryFolderBase NewFolder(UUID id, UUID parent, UUID owner, string name)
  288. {
  289. InventoryFolderBase f = new InventoryFolderBase();
  290. f.ID = id;
  291. f.ParentID = parent;
  292. f.Owner = owner;
  293. f.Name = name;
  294. return f;
  295. }
  296. }
  297. }