1
0

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