InventoryTests.cs 17 KB

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