From d2e5380cb2325ad42917c528c52a8ad42ec0176f Mon Sep 17 00:00:00 2001
From: Kunnis
Date: Sat, 15 Aug 2009 10:54:48 -0500
Subject: * Fixed MySQL/MySQLAssetData.cs to properly do updates * Removed an
 extra parameter from MySQL/MySQLInventoryData.cs * Fixed a bug in
 SQLite/SQLiteAssetData.cs that was causing a NRE when updating an asset. *
 Improved the BasicAssetTest.cs to do full create/update/get testing *
 Improved the BasicInventoryTest.cs to do full create/update/get of both a
 folder and an item * Moved the null ref tests to the start of the
 PropertyCompareConstraint.cs, so that it doesn't throw when passing in a null
 item

---
 OpenSim/Data/MySQL/MySQLAssetData.cs            |  9 +---
 OpenSim/Data/MySQL/MySQLInventoryData.cs        |  2 +-
 OpenSim/Data/SQLite/SQLiteAssetData.cs          |  2 +-
 OpenSim/Data/Tests/BasicAssetTest.cs            | 35 +++++++++++++---
 OpenSim/Data/Tests/BasicInventoryTest.cs        | 56 +++++++++++++++++++++----
 OpenSim/Data/Tests/PropertyCompareConstraint.cs | 28 ++++++-------
 6 files changed, 95 insertions(+), 37 deletions(-)

(limited to 'OpenSim/Data')

diff --git a/OpenSim/Data/MySQL/MySQLAssetData.cs b/OpenSim/Data/MySQL/MySQLAssetData.cs
index 5d87649..0865083 100644
--- a/OpenSim/Data/MySQL/MySQLAssetData.cs
+++ b/OpenSim/Data/MySQL/MySQLAssetData.cs
@@ -196,18 +196,11 @@ namespace OpenSim.Data.MySQL
         {
             lock (_dbConnection)
             {
-                //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID);
-                if (ExistsAsset(asset.FullID))
-                {
-                    //m_log.Info("[ASSET DB]: Asset exists already, ignoring.");
-                    return;
-                }
-
                 _dbConnection.CheckConnection();
 
                 MySqlCommand cmd =
                     new MySqlCommand(
-                        "insert INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, data)" +
+                        "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, data)" +
                         "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?data)",
                         _dbConnection.Connection);
 
diff --git a/OpenSim/Data/MySQL/MySQLInventoryData.cs b/OpenSim/Data/MySQL/MySQLInventoryData.cs
index 121ef7a..849c246 100644
--- a/OpenSim/Data/MySQL/MySQLInventoryData.cs
+++ b/OpenSim/Data/MySQL/MySQLInventoryData.cs
@@ -604,7 +604,7 @@ namespace OpenSim.Data.MySQL
             cmd.Parameters.AddWithValue("?agentID", folder.Owner.ToString());
             cmd.Parameters.AddWithValue("?parentFolderID", folder.ParentID.ToString());
             cmd.Parameters.AddWithValue("?folderName", folderName);
-            cmd.Parameters.AddWithValue("?type", (short) folder.Type);
+            cmd.Parameters.AddWithValue("?type", folder.Type);
             cmd.Parameters.AddWithValue("?version", folder.Version);
 
             try
diff --git a/OpenSim/Data/SQLite/SQLiteAssetData.cs b/OpenSim/Data/SQLite/SQLiteAssetData.cs
index b09c1c9..72af7a0 100644
--- a/OpenSim/Data/SQLite/SQLiteAssetData.cs
+++ b/OpenSim/Data/SQLite/SQLiteAssetData.cs
@@ -183,7 +183,7 @@ namespace OpenSim.Data.SQLite
             int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
 
             m_log.Info("[ASSET DB]: " +
-                                     string.Format("Loaded {6} {5} Asset: [{0}][{3}] \"{1}\":{2} ({7} bytes)",
+                                     string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
                                                    asset.FullID, asset.Name, asset.Description, asset.Type,
                                                    temporary, local, assetLength));
         }
diff --git a/OpenSim/Data/Tests/BasicAssetTest.cs b/OpenSim/Data/Tests/BasicAssetTest.cs
index eddb999..91b613a 100644
--- a/OpenSim/Data/Tests/BasicAssetTest.cs
+++ b/OpenSim/Data/Tests/BasicAssetTest.cs
@@ -84,7 +84,7 @@ namespace OpenSim.Data.Tests
             a1.Data = asset1;
             a2.Data = asset1;
             a3.Data = asset1;
-
+            
             a1.FullID = uuid1;
             a2.FullID = uuid2;
             a3.FullID = uuid3;
@@ -92,15 +92,40 @@ namespace OpenSim.Data.Tests
             db.CreateAsset(a1);
             db.CreateAsset(a2);
             db.CreateAsset(a3);
-
+            
             AssetBase a1a = db.FetchAsset(uuid1);
-            Assert.That(a1, Constraints.PropertyCompareConstraint(a1a));
+            Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
 
             AssetBase a2a = db.FetchAsset(uuid2);
-            Assert.That(a2, Constraints.PropertyCompareConstraint(a2a));
+            Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
 
             AssetBase a3a = db.FetchAsset(uuid3);
-            Assert.That(a3, Constraints.PropertyCompareConstraint(a3a));
+            Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
+
+            ScrambleForTesting.Scramble(a1a);
+            ScrambleForTesting.Scramble(a2a);
+            ScrambleForTesting.Scramble(a3a);
+
+            a1a.Data = asset1;
+            a2a.Data = asset1;
+            a3a.Data = asset1;
+
+            a1a.FullID = uuid1;
+            a2a.FullID = uuid2;
+            a3a.FullID = uuid3;
+
+            db.UpdateAsset(a1a);
+            db.UpdateAsset(a2a);
+            db.UpdateAsset(a3a);
+
+            AssetBase a1b = db.FetchAsset(uuid1);
+            Assert.That(a1b, Constraints.PropertyCompareConstraint(a1a));
+
+            AssetBase a2b = db.FetchAsset(uuid2);
+            Assert.That(a2b, Constraints.PropertyCompareConstraint(a2a));
+
+            AssetBase a3b = db.FetchAsset(uuid3);
+            Assert.That(a3b, Constraints.PropertyCompareConstraint(a3a));
         }
 
         [Test]
diff --git a/OpenSim/Data/Tests/BasicInventoryTest.cs b/OpenSim/Data/Tests/BasicInventoryTest.cs
index 967c6e7..21552c8 100644
--- a/OpenSim/Data/Tests/BasicInventoryTest.cs
+++ b/OpenSim/Data/Tests/BasicInventoryTest.cs
@@ -258,17 +258,59 @@ namespace OpenSim.Data.Tests
         [Test]
         public void T104_RandomUpdateItem()
         {
-            InventoryItemBase expected = db.getInventoryItem(item1);
+            UUID owner = UUID.Random();
+            UUID folder = UUID.Random();
+            UUID rootId = UUID.Random();
+            UUID rootAsset = UUID.Random();
+            InventoryFolderBase f1 = NewFolder(folder, zero, owner, name1);
+            ScrambleForTesting.Scramble(f1);
+            f1.Owner = owner;
+            f1.ParentID = zero;
+            f1.ID = folder;
+
+            // succeed with true
+            db.addInventoryFolder(f1);
+            InventoryFolderBase f1a = db.getUserRootFolder(owner);
+            Assert.That(f1a, Constraints.PropertyCompareConstraint(f1));
+
+            ScrambleForTesting.Scramble(f1a);
+            f1a.Owner = owner;
+            f1a.ParentID = zero;
+            f1a.ID = folder;
+            db.updateInventoryFolder(f1a);
+
+            InventoryFolderBase f1b = db.getUserRootFolder(owner);
+            Assert.That(f1b, Constraints.PropertyCompareConstraint(f1a));
+
+            //Now we have a valid folder to insert into, we can insert the item.
+            InventoryItemBase root = NewItem(rootId, folder, owner, iname1, rootAsset);
+            ScrambleForTesting.Scramble(root);
+            root.ID = rootId;
+            root.AssetID = rootAsset;
+            root.Owner = owner;
+            root.Folder = folder;
+            db.addInventoryItem(root);
+
+            InventoryItemBase expected = db.getInventoryItem(rootId);
+            Assert.That(expected, Constraints.PropertyCompareConstraint(root)
+                                    .IgnoreProperty(x => x.InvType)
+                                    .IgnoreProperty(x => x.CreatorIdAsUuid)
+                                    .IgnoreProperty(x => x.Description)
+                                    .IgnoreProperty(x => x.CreatorId));
+
             ScrambleForTesting.Scramble(expected);
-            expected.ID = item1;
+            expected.ID = rootId;
+            expected.AssetID = rootAsset;
+            expected.Owner = owner;
+            expected.Folder = folder;
             db.updateInventoryItem(expected);
 
-            InventoryItemBase actual = db.getInventoryItem(item1);
+            InventoryItemBase actual = db.getInventoryItem(rootId);
             Assert.That(actual, Constraints.PropertyCompareConstraint(expected)
-                .IgnoreProperty(x=>x.InvType)
-                .IgnoreProperty(x=>x.CreatorIdAsUuid)
-                .IgnoreProperty(x=>x.Description)
-                .IgnoreProperty(x=>x.CreatorId));
+                                    .IgnoreProperty(x => x.InvType)
+                                    .IgnoreProperty(x => x.CreatorIdAsUuid)
+                                    .IgnoreProperty(x => x.Description)
+                                    .IgnoreProperty(x => x.CreatorId));
         }
 
         [Test]
diff --git a/OpenSim/Data/Tests/PropertyCompareConstraint.cs b/OpenSim/Data/Tests/PropertyCompareConstraint.cs
index 5f53725..d64a51e 100644
--- a/OpenSim/Data/Tests/PropertyCompareConstraint.cs
+++ b/OpenSim/Data/Tests/PropertyCompareConstraint.cs
@@ -69,6 +69,19 @@ namespace OpenSim.Data.Tests
 
         private bool ObjectCompare(object expected, object actual, Stack<string> propertyNames)
         {
+            //If they are both null, they are equal
+            if (actual == null && expected == null)
+                return true;
+
+            //If only one is null, then they aren't
+            if (actual == null || expected == null)
+            {
+                failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
+                failingActual = actual;
+                failingExpected = expected;
+                return false;
+            }
+
             //prevent loops...
             if(propertyNames.Count > 50)
             {
@@ -195,21 +208,6 @@ namespace OpenSim.Data.Tests
                 object actualValue = property.GetValue(actual, null);
                 object expectedValue = property.GetValue(expected, null);
 
-                //If they are both null, they are equal
-                if (actualValue == null && expectedValue == null)
-                    continue;
-
-                //If only one is null, then they aren't
-                if (actualValue == null || expectedValue == null)
-                {
-                    propertyNames.Push(property.Name);
-                    failingPropertyName = string.Join(".", propertyNames.Reverse().ToArray());
-                    propertyNames.Pop();
-                    failingActual = actualValue;
-                    failingExpected = expectedValue;
-                    return false;
-                }
-
                 propertyNames.Push(property.Name);
                 if (!ObjectCompare(expectedValue, actualValue, propertyNames))
                     return false;
-- 
cgit v1.1