InventoryTests.cs 17 KB

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