OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
AssetTests.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 System.Collections.Generic;
30 using log4net.Config;
31 using NUnit.Framework;
32 using NUnit.Framework.Constraints;
33 using OpenMetaverse;
34 using OpenSim.Framework;
35 using OpenSim.Tests.Common;
36 using System.Data.Common;
37 using log4net;
38 
39 // DBMS-specific:
40 using MySql.Data.MySqlClient;
41 using OpenSim.Data.MySQL;
42 
43 using Mono.Data.Sqlite;
44 using OpenSim.Data.SQLite;
45 
46 namespace OpenSim.Data.Tests
47 {
48  [TestFixture(Description = "Asset store tests (SQLite)")]
49  public class SQLiteAssetTests : AssetTests<SqliteConnection, SQLiteAssetData>
50  {
51  }
52 
53  [TestFixture(Description = "Asset store tests (MySQL)")]
54  public class MySqlAssetTests : AssetTests<MySqlConnection, MySQLAssetData>
55  {
56  }
57 
58  public class AssetTests<TConn, TAssetData> : BasicDataServiceTest<TConn, TAssetData>
59  where TConn : DbConnection, new()
60  where TAssetData : AssetDataBase, new()
61  {
62  TAssetData m_db;
63 
64  public UUID uuid1 = UUID.Random();
65  public UUID uuid2 = UUID.Random();
66  public UUID uuid3 = UUID.Random();
67 
68  public string critter1 = UUID.Random().ToString();
69  public string critter2 = UUID.Random().ToString();
70  public string critter3 = UUID.Random().ToString();
71 
72  public byte[] data1 = new byte[100];
73 
74  PropertyScrambler<AssetBase> scrambler = new PropertyScrambler<AssetBase>()
75  .DontScramble(x => x.ID)
76  .DontScramble(x => x.Type)
77  .DontScramble(x => x.FullID)
78  .DontScramble(x => x.Metadata.ID)
79  .DontScramble(x => x.Metadata.CreatorID)
80  .DontScramble(x => x.Metadata.ContentType)
81  .DontScramble(x => x.Metadata.FullID)
82  .DontScramble(x => x.Data);
83 
84  protected override void InitService(object service)
85  {
86  ClearDB();
87  m_db = (TAssetData)service;
88  m_db.Initialise(m_connStr);
89  }
90 
91  private void ClearDB()
92  {
93  DropTables("assets");
94  ResetMigrations("AssetStore");
95  }
96 
97 
98  [Test]
99  public void T001_LoadEmpty()
100  {
101  TestHelpers.InMethod();
102 
103  bool[] exist = m_db.AssetsExist(new[] { uuid1, uuid2, uuid3 });
104  Assert.IsFalse(exist[0]);
105  Assert.IsFalse(exist[1]);
106  Assert.IsFalse(exist[2]);
107  }
108 
109  [Test]
111  {
112  TestHelpers.InMethod();
113 
114  AssetBase a1 = new AssetBase(uuid1, "asset one", (sbyte)AssetType.Texture, critter1.ToString());
115  AssetBase a2 = new AssetBase(uuid2, "asset two", (sbyte)AssetType.Texture, critter2.ToString());
116  AssetBase a3 = new AssetBase(uuid3, "asset three", (sbyte)AssetType.Texture, critter3.ToString());
117  a1.Data = data1;
118  a2.Data = data1;
119  a3.Data = data1;
120 
121  scrambler.Scramble(a1);
122  scrambler.Scramble(a2);
123  scrambler.Scramble(a3);
124 
125  m_db.StoreAsset(a1);
126  m_db.StoreAsset(a2);
127  m_db.StoreAsset(a3);
128  a1.UploadAttempts = 0;
129  a2.UploadAttempts = 0;
130  a3.UploadAttempts = 0;
131 
132  AssetBase a1a = m_db.GetAsset(uuid1);
133  a1a.UploadAttempts = 0;
134  Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
135 
136  AssetBase a2a = m_db.GetAsset(uuid2);
137  a2a.UploadAttempts = 0;
138  Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
139 
140  AssetBase a3a = m_db.GetAsset(uuid3);
141  a3a.UploadAttempts = 0;
142  Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
143 
144  scrambler.Scramble(a1a);
145  scrambler.Scramble(a2a);
146  scrambler.Scramble(a3a);
147 
148  m_db.StoreAsset(a1a);
149  m_db.StoreAsset(a2a);
150  m_db.StoreAsset(a3a);
151  a1a.UploadAttempts = 0;
152  a2a.UploadAttempts = 0;
153  a3a.UploadAttempts = 0;
154 
155  AssetBase a1b = m_db.GetAsset(uuid1);
156  a1b.UploadAttempts = 0;
157  Assert.That(a1b, Constraints.PropertyCompareConstraint(a1a));
158 
159  AssetBase a2b = m_db.GetAsset(uuid2);
160  a2b.UploadAttempts = 0;
161  Assert.That(a2b, Constraints.PropertyCompareConstraint(a2a));
162 
163  AssetBase a3b = m_db.GetAsset(uuid3);
164  a3b.UploadAttempts = 0;
165  Assert.That(a3b, Constraints.PropertyCompareConstraint(a3a));
166 
167  bool[] exist = m_db.AssetsExist(new[] { uuid1, uuid2, uuid3 });
168  Assert.IsTrue(exist[0]);
169  Assert.IsTrue(exist[1]);
170  Assert.IsTrue(exist[2]);
171 
172  List<AssetMetadata> metadatas = m_db.FetchAssetMetadataSet(0, 1000);
173 
174  Assert.That(metadatas.Count >= 3, "FetchAssetMetadataSet() should have returned at least 3 assets!");
175 
176  // It is possible that the Asset table is filled with data, in which case we don't try to find "our"
177  // assets there:
178  if (metadatas.Count < 1000)
179  {
180  AssetMetadata metadata = metadatas.Find(x => x.FullID == uuid1);
181  Assert.That(metadata.Name, Is.EqualTo(a1b.Name));
182  Assert.That(metadata.Description, Is.EqualTo(a1b.Description));
183  Assert.That(metadata.Type, Is.EqualTo(a1b.Type));
184  Assert.That(metadata.Temporary, Is.EqualTo(a1b.Temporary));
185  Assert.That(metadata.FullID, Is.EqualTo(a1b.FullID));
186  }
187  }
188 
189  [Test]
191  {
192  TestHelpers.InMethod();
193 
194  // It is expected that eventually the CreatorID might be an arbitrary string (an URI)
195  // rather than a valid UUID (?). This test is to make sure that the database layer does not
196  // attempt to convert CreatorID to GUID, but just passes it both ways as a string.
197  AssetBase a1 = new AssetBase(uuid1, "asset one", (sbyte)AssetType.Texture, critter1);
198  AssetBase a2 = new AssetBase(uuid2, "asset two", (sbyte)AssetType.Texture, "This is not a GUID!");
199  AssetBase a3 = new AssetBase(uuid3, "asset three", (sbyte)AssetType.Texture, "");
200  a1.Data = data1;
201  a2.Data = data1;
202  a3.Data = data1;
203 
204  m_db.StoreAsset(a1);
205  a1.UploadAttempts = 0;
206  m_db.StoreAsset(a2);
207  a2.UploadAttempts = 0;
208  m_db.StoreAsset(a3);
209  a3.UploadAttempts = 0;
210 
211  AssetBase a1a = m_db.GetAsset(uuid1);
212  a1a.UploadAttempts = 0;
213  Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
214 
215  AssetBase a2a = m_db.GetAsset(uuid2);
216  a2a.UploadAttempts = 0;
217  Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
218 
219  AssetBase a3a = m_db.GetAsset(uuid3);
220  a3a.UploadAttempts = 0;
221  Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
222  }
223  }
224 }
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
Interactive OpenSim region server
Definition: OpenSim.cs:55
override void InitService(object service)
To be overridden in derived classes. Do whatever init with the m_service, like setting the conn strin...
Definition: AssetTests.cs:84