InventoryTests.cs 17 KB

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