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