* Added Expression based ignores to the PropertyScrambler, which makes a lot of the tests clearer because I'm not constantly resetting properties.

arthursv
Kunnis 2009-08-16 03:35:31 -05:00 committed by Teravus Ovares (Dan Olivares)
parent 5dde4a4cfa
commit dd78c250ae
9 changed files with 229 additions and 224 deletions

View File

@ -69,18 +69,20 @@ namespace OpenSim.Data.Tests
AssetBase a1 = new AssetBase(uuid1, "asset one");
AssetBase a2 = new AssetBase(uuid2, "asset two");
AssetBase a3 = new AssetBase(uuid3, "asset three");
ScrambleForTesting.Scramble(a1);
ScrambleForTesting.Scramble(a2);
ScrambleForTesting.Scramble(a3);
a1.Data = asset1;
a2.Data = asset1;
a3.Data = asset1;
a1.FullID = uuid1;
a2.FullID = uuid2;
a3.FullID = uuid3;
PropertyScrambler<AssetBase> scrambler = new PropertyScrambler<AssetBase>()
.DontScramble(x => x.Data)
.DontScramble(x => x.ID)
.DontScramble(x => x.FullID)
.DontScramble(x => x.Metadata.ID)
.DontScramble(x => x.Metadata.FullID);
scrambler.Scramble(a1);
scrambler.Scramble(a2);
scrambler.Scramble(a3);
db.CreateAsset(a1);
db.CreateAsset(a2);
@ -95,17 +97,9 @@ namespace OpenSim.Data.Tests
AssetBase a3a = db.FetchAsset(uuid3);
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;
scrambler.Scramble(a1a);
scrambler.Scramble(a2a);
scrambler.Scramble(a3a);
db.UpdateAsset(a1a);
db.UpdateAsset(a2a);

View File

@ -40,7 +40,6 @@ namespace OpenSim.Data.Tests
{
public class BasicEstateTest
{
//private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public IEstateDataStore db;
public IRegionDataStore regionDb;
@ -57,14 +56,7 @@ namespace OpenSim.Data.Tests
public void SuperInit()
{
try
{
XmlConfigurator.Configure();
}
catch (Exception)
{
// I don't care, just leave log4net off
}
OpenSim.Tests.Common.TestLogging.LogToConsole();
}
#region 0Tests
@ -168,7 +160,7 @@ namespace OpenSim.Data.Tests
// Letting estate store generate rows to database for us
EstateSettings originalSettings = db.LoadEstateSettings(REGION_ID);
ScrambleForTesting.Scramble(originalSettings);
new PropertyScrambler<EstateSettings>().Scramble(originalSettings);
// Saving settings.
db.StoreEstateSettings(originalSettings);

View File

@ -28,22 +28,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Config;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using OpenMetaverse;
using log4net;
using System.Reflection;
namespace OpenSim.Data.Tests
{
public class BasicGridTest
{
//private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public GridDataBase db;
public IGridDataPlugin db;
public UUID region1, region2, region3;
public UUID zero = UUID.Zero;
public static Random random;
public static Random random = new Random();
[TearDown]
public void removeAllRegions()
@ -61,24 +57,16 @@ namespace OpenSim.Data.Tests
public void SuperInit()
{
try
{
XmlConfigurator.Configure();
}
catch (Exception)
{
// I don't care, just leave log4net off
}
OpenSim.Tests.Common.TestLogging.LogToConsole();
region1 = UUID.Random();
region2 = UUID.Random();
region3 = UUID.Random();
random = new Random();
}
protected RegionProfileData createRegion(UUID regionUUID, string regionName)
{
RegionProfileData reg = new RegionProfileData();
ScrambleForTesting.Scramble(reg);
new PropertyScrambler<RegionProfileData>().Scramble(reg);
reg.Uuid = regionUUID;
reg.RegionName = regionName;

View File

@ -66,14 +66,7 @@ namespace OpenSim.Data.Tests
public void SuperInit()
{
try
{
XmlConfigurator.Configure();
}
catch (Exception)
{
// I don't care, just leave log4net off
}
OpenSim.Tests.Common.TestLogging.LogToConsole();
folder1 = UUID.Random();
folder2 = UUID.Random();
@ -258,37 +251,38 @@ namespace OpenSim.Data.Tests
[Test]
public void T104_RandomUpdateItem()
{
PropertyScrambler<InventoryFolderBase> folderScrambler =
new PropertyScrambler<InventoryFolderBase>()
.DontScramble(x => x.Owner)
.DontScramble(x => x.ParentID)
.DontScramble(x => x.ID);
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;
folderScrambler.Scramble(f1);
// 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;
folderScrambler.Scramble(f1a);
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.
PropertyScrambler<InventoryItemBase> inventoryScrambler =
new PropertyScrambler<InventoryItemBase>()
.DontScramble(x => x.ID)
.DontScramble(x => x.AssetID)
.DontScramble(x => x.Owner)
.DontScramble(x => x.Folder);
InventoryItemBase root = NewItem(rootId, folder, owner, iname1, rootAsset);
ScrambleForTesting.Scramble(root);
root.ID = rootId;
root.AssetID = rootAsset;
root.Owner = owner;
root.Folder = folder;
inventoryScrambler.Scramble(root);
db.addInventoryItem(root);
InventoryItemBase expected = db.getInventoryItem(rootId);
@ -298,11 +292,7 @@ namespace OpenSim.Data.Tests
.IgnoreProperty(x => x.Description)
.IgnoreProperty(x => x.CreatorId));
ScrambleForTesting.Scramble(expected);
expected.ID = rootId;
expected.AssetID = rootAsset;
expected.Owner = owner;
expected.Folder = folder;
inventoryScrambler.Scramble(expected);
db.updateInventoryItem(expected);
InventoryItemBase actual = db.getInventoryItem(rootId);

View File

@ -71,14 +71,7 @@ namespace OpenSim.Data.Tests
public void SuperInit()
{
try
{
XmlConfigurator.Configure();
}
catch (Exception)
{
// I don't care, just leave log4net off
}
OpenSim.Tests.Common.TestLogging.LogToConsole();
region1 = UUID.Random();
region3 = UUID.Random();
@ -536,6 +529,9 @@ namespace OpenSim.Data.Tests
[Test]
public void T016_RandomSogWithSceneParts()
{
PropertyScrambler<SceneObjectPart> scrambler =
new PropertyScrambler<SceneObjectPart>()
.DontScramble(x => x.UUID);
UUID tmpSog = UUID.Random();
UUID tmp1 = UUID.Random();
UUID tmp2 = UUID.Random();
@ -547,14 +543,18 @@ namespace OpenSim.Data.Tests
p1.Shape = PrimitiveBaseShape.Default;
p2.Shape = PrimitiveBaseShape.Default;
p3.Shape = PrimitiveBaseShape.Default;
ScrambleForTesting.Scramble(p1);
ScrambleForTesting.Scramble(p2);
ScrambleForTesting.Scramble(p3);
p1.UUID = tmp1;
p2.UUID = tmp2;
p3.UUID = tmp3;
scrambler.Scramble(p1);
scrambler.Scramble(p2);
scrambler.Scramble(p3);
SceneObjectGroup sog = NewSOG("Sop 0", tmpSog, newregion);
ScrambleForTesting.Scramble(sog);
PropertyScrambler<SceneObjectGroup> sogScrambler =
new PropertyScrambler<SceneObjectGroup>()
.DontScramble(x => x.UUID);
sogScrambler.Scramble(sog);
sog.UUID = tmpSog;
sog.AddPart(p1);
sog.AddPart(p2);

View File

@ -71,14 +71,7 @@ namespace OpenSim.Data.Tests
public void SuperInit()
{
try
{
XmlConfigurator.Configure();
}
catch (Exception)
{
// I don't care, just leave log4net off
}
OpenSim.Tests.Common.TestLogging.LogToConsole();
random = new Random();
user1 = UUID.Random();
user2 = UUID.Random();
@ -395,8 +388,7 @@ namespace OpenSim.Data.Tests
{
UUID id = user5;
UserProfileData u = db.GetUserByUUID(id);
ScrambleForTesting.Scramble(u);
u.ID = id;
new PropertyScrambler<UserProfileData>().DontScramble(x=>x.ID).Scramble(u);
db.UpdateUserProfile(u);
UserProfileData u1a = db.GetUserByUUID(id);

View File

@ -0,0 +1,159 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using OpenMetaverse;
using OpenSim.Framework;
namespace OpenSim.Data.Tests
{
//This is generic so that the lambda expressions will work right in IDEs.
public class PropertyScrambler<T>
{
readonly System.Collections.Generic.List<string> membersToNotScramble = new List<string>();
private void AddExpressionToNotScrableList(Expression expression)
{
UnaryExpression unaryExpression = expression as UnaryExpression;
if(unaryExpression != null)
{
AddExpressionToNotScrableList(unaryExpression.Operand);
return;
}
MemberExpression memberExpression = expression as MemberExpression;
if (memberExpression != null)
{
if (!(memberExpression.Member is PropertyInfo))
{
throw new NotImplementedException("I don't know how deal with a MemberExpression that is a " + expression.Type);
}
membersToNotScramble.Add(memberExpression.Member.Name);
return;
}
throw new NotImplementedException("I don't know how to parse a " + expression.Type);
}
public PropertyScrambler<T> DontScramble(Expression<Func<T, object>> expression)
{
AddExpressionToNotScrableList(expression.Body);
return this;
}
public void Scramble(T obj)
{
internalScramble(obj);
}
private void internalScramble(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
//Skip indexers of classes. We will assume that everything that has an indexer
// is also IEnumberable. May not always be true, but should be true normally.
if(property.GetIndexParameters().Length > 0)
continue;
RandomizeProperty(obj, property, null);
}
//Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
// everything inside of it.
IEnumerable enumerable = obj as IEnumerable;
if(enumerable != null)
{
foreach (object value in enumerable)
{
internalScramble(value);
}
}
}
private readonly Random random = new Random();
private void RandomizeProperty(object obj, PropertyInfo property, object[] index)
{//I'd like a better way to compare, but I had lots of problems with InventoryFolderBase because the ID is inherited.
if(membersToNotScramble.Contains(property.Name))
return;
Type t = property.PropertyType;
if (!property.CanWrite)
return;
object value = property.GetValue(obj, index);
if (value == null)
return;
if (t == typeof(string))
property.SetValue(obj, RandomName(), index);
else if (t == typeof(UUID))
property.SetValue(obj, UUID.Random(), index);
else if (t == typeof(sbyte))
property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
else if (t == typeof(short))
property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
else if (t == typeof(int))
property.SetValue(obj, random.Next(), index);
else if (t == typeof(long))
property.SetValue(obj, random.Next() * int.MaxValue, index);
else if (t == typeof(byte))
property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
else if (t == typeof(ushort))
property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
else if (t == typeof(uint))
property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
else if (t == typeof(ulong))
property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
else if (t == typeof(bool))
property.SetValue(obj, true, index);
else if (t == typeof(byte[]))
{
byte[] bytes = new byte[30];
random.NextBytes(bytes);
property.SetValue(obj, bytes, index);
}
else
internalScramble(value);
}
private string RandomName()
{
StringBuilder name = new StringBuilder();
int size = random.Next(5, 12);
for (int i = 0; i < size; i++)
{
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
name.Append(ch);
}
return name.ToString();
}
}
[TestFixture]
public class PropertyScramblerTests
{
[Test]
public void TestScramble()
{
AssetBase actual = new AssetBase(UUID.Random(), "asset one");
new PropertyScrambler<AssetBase>().Scramble(actual);
}
[Test]
public void DontScramble()
{
UUID uuid = UUID.Random();
AssetBase asset = new AssetBase();
asset.FullID = uuid;
new PropertyScrambler<AssetBase>()
.DontScramble(x => x.Metadata)
.DontScramble(x => x.FullID)
.DontScramble(x => x.ID)
.Scramble(asset);
Assert.That(asset.FullID, Is.EqualTo(uuid));
}
}
}

View File

@ -1,129 +0,0 @@
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using OpenMetaverse;
using OpenSim.Framework;
namespace OpenSim.Data.Tests
{
public static class ScrambleForTesting
{
private static readonly Random random = new Random();
public static void Scramble(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
//Skip indexers of classes. We will assume that everything that has an indexer
// is also IEnumberable. May not always be true, but should be true normally.
if (property.GetIndexParameters().Length > 0)
continue;
RandomizeProperty(obj, property, null);
}
//Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
// everything inside of it.
IEnumerable enumerable = obj as IEnumerable;
if (enumerable != null)
{
foreach (object value in enumerable)
{
Scramble(value);
}
}
}
private static void RandomizeProperty(object obj, PropertyInfo property, object[] index)
{
Type t = property.PropertyType;
if (!property.CanWrite)
return;
object value = property.GetValue(obj, index);
if (value == null)
return;
if (t == typeof (string))
property.SetValue(obj, RandomName(), index);
else if (t == typeof (UUID))
property.SetValue(obj, UUID.Random(), index);
else if (t == typeof (sbyte))
property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
else if (t == typeof (short))
property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
else if (t == typeof (int))
property.SetValue(obj, random.Next(), index);
else if (t == typeof (long))
property.SetValue(obj, random.Next() * int.MaxValue, index);
else if (t == typeof (byte))
property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
else if (t == typeof (ushort))
property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
else if (t == typeof (uint))
property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
else if (t == typeof (ulong))
property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
else if (t == typeof (bool))
property.SetValue(obj, true, index);
else if (t == typeof (byte[]))
{
byte[] bytes = new byte[30];
random.NextBytes(bytes);
property.SetValue(obj, bytes, index);
}
else
Scramble(value);
}
private static string RandomName()
{
StringBuilder name = new StringBuilder();
int size = random.Next(5, 12);
for (int i = 0; i < size; i++)
{
char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
name.Append(ch);
}
return name.ToString();
}
}
[TestFixture]
public class ScrableForTestingTest
{
[Test]
public void TestScramble()
{
AssetBase actual = new AssetBase(UUID.Random(), "asset one");
ScrambleForTesting.Scramble(actual);
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using log4net.Appender;
using log4net.Layout;
namespace OpenSim.Tests.Common
{
public static class TestLogging
{
public static void LogToConsole()
{
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.Layout =
new PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline");
log4net.Config.BasicConfigurator.Configure(consoleAppender);
}
}
}