InventoryTests.cs 17 KB

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