aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Tests/PropertyScrambler.cs
diff options
context:
space:
mode:
authorAdam Frisby2009-08-18 00:23:27 +1000
committerAdam Frisby2009-08-18 00:23:27 +1000
commit58d9d6026eb36d530c7cec753279b7aeaa6b43eb (patch)
tree86a09687ef01b308ac2ae8e1023ec9ddb943c54f /OpenSim/Data/Tests/PropertyScrambler.cs
parent* Implementing a bunch of Unimplemented MRM stubs. (diff)
parentAdd System.Xml reference to the console project (diff)
downloadopensim-SC_OLD-58d9d6026eb36d530c7cec753279b7aeaa6b43eb.zip
opensim-SC_OLD-58d9d6026eb36d530c7cec753279b7aeaa6b43eb.tar.gz
opensim-SC_OLD-58d9d6026eb36d530c7cec753279b7aeaa6b43eb.tar.bz2
opensim-SC_OLD-58d9d6026eb36d530c7cec753279b7aeaa6b43eb.tar.xz
Merge branch 'master' of ssh://opensimulator.org/var/git/opensim
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Tests/PropertyScrambler.cs (renamed from OpenSim/Data/Tests/ScrambleForTesting.cs)101
1 files changed, 79 insertions, 22 deletions
diff --git a/OpenSim/Data/Tests/ScrambleForTesting.cs b/OpenSim/Data/Tests/PropertyScrambler.cs
index 3a22347..72aaff1 100644
--- a/OpenSim/Data/Tests/ScrambleForTesting.cs
+++ b/OpenSim/Data/Tests/PropertyScrambler.cs
@@ -27,18 +27,58 @@
27 27
28using System; 28using System;
29using System.Collections; 29using System.Collections;
30using System.Collections.Generic;
31using System.Linq.Expressions;
30using System.Reflection; 32using System.Reflection;
31using System.Text; 33using System.Text;
32using NUnit.Framework; 34using NUnit.Framework;
35using NUnit.Framework.SyntaxHelpers;
33using OpenMetaverse; 36using OpenMetaverse;
34using OpenSim.Framework; 37using OpenSim.Framework;
35 38
36namespace OpenSim.Data.Tests 39namespace OpenSim.Data.Tests
37{ 40{
38 public static class ScrambleForTesting 41
42 //This is generic so that the lambda expressions will work right in IDEs.
43 public class PropertyScrambler<T>
39 { 44 {
40 private static readonly Random random = new Random(); 45 readonly System.Collections.Generic.List<string> membersToNotScramble = new List<string>();
41 public static void Scramble(object obj) 46
47 private void AddExpressionToNotScrableList(Expression expression)
48 {
49 UnaryExpression unaryExpression = expression as UnaryExpression;
50 if (unaryExpression != null)
51 {
52 AddExpressionToNotScrableList(unaryExpression.Operand);
53 return;
54 }
55
56 MemberExpression memberExpression = expression as MemberExpression;
57 if (memberExpression != null)
58 {
59 if (!(memberExpression.Member is PropertyInfo))
60 {
61 throw new NotImplementedException("I don't know how deal with a MemberExpression that is a " + expression.Type);
62 }
63 membersToNotScramble.Add(memberExpression.Member.Name);
64 return;
65 }
66
67 throw new NotImplementedException("I don't know how to parse a " + expression.Type);
68 }
69
70 public PropertyScrambler<T> DontScramble(Expression<Func<T, object>> expression)
71 {
72 AddExpressionToNotScrableList(expression.Body);
73 return this;
74 }
75
76 public void Scramble(T obj)
77 {
78 internalScramble(obj);
79 }
80
81 private void internalScramble(object obj)
42 { 82 {
43 PropertyInfo[] properties = obj.GetType().GetProperties(); 83 PropertyInfo[] properties = obj.GetType().GetProperties();
44 foreach (var property in properties) 84 foreach (var property in properties)
@@ -57,13 +97,16 @@ namespace OpenSim.Data.Tests
57 { 97 {
58 foreach (object value in enumerable) 98 foreach (object value in enumerable)
59 { 99 {
60 Scramble(value); 100 internalScramble(value);
61 } 101 }
62 } 102 }
63 } 103 }
64 104
65 private static void RandomizeProperty(object obj, PropertyInfo property, object[] index) 105 private readonly Random random = new Random();
66 { 106 private void RandomizeProperty(object obj, PropertyInfo property, object[] index)
107 {//I'd like a better way to compare, but I had lots of problems with InventoryFolderBase because the ID is inherited.
108 if (membersToNotScramble.Contains(property.Name))
109 return;
67 Type t = property.PropertyType; 110 Type t = property.PropertyType;
68 if (!property.CanWrite) 111 if (!property.CanWrite)
69 return; 112 return;
@@ -71,39 +114,39 @@ namespace OpenSim.Data.Tests
71 if (value == null) 114 if (value == null)
72 return; 115 return;
73 116
74 if (t == typeof (string)) 117 if (t == typeof(string))
75 property.SetValue(obj, RandomName(), index); 118 property.SetValue(obj, RandomName(), index);
76 else if (t == typeof (UUID)) 119 else if (t == typeof(UUID))
77 property.SetValue(obj, UUID.Random(), index); 120 property.SetValue(obj, UUID.Random(), index);
78 else if (t == typeof (sbyte)) 121 else if (t == typeof(sbyte))
79 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index); 122 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
80 else if (t == typeof (short)) 123 else if (t == typeof(short))
81 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index); 124 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
82 else if (t == typeof (int)) 125 else if (t == typeof(int))
83 property.SetValue(obj, random.Next(), index); 126 property.SetValue(obj, random.Next(), index);
84 else if (t == typeof (long)) 127 else if (t == typeof(long))
85 property.SetValue(obj, random.Next() * int.MaxValue, index); 128 property.SetValue(obj, random.Next() * int.MaxValue, index);
86 else if (t == typeof (byte)) 129 else if (t == typeof(byte))
87 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index); 130 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
88 else if (t == typeof (ushort)) 131 else if (t == typeof(ushort))
89 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index); 132 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
90 else if (t == typeof (uint)) 133 else if (t == typeof(uint))
91 property.SetValue(obj, Convert.ToUInt32(random.Next()), index); 134 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
92 else if (t == typeof (ulong)) 135 else if (t == typeof(ulong))
93 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index); 136 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
94 else if (t == typeof (bool)) 137 else if (t == typeof(bool))
95 property.SetValue(obj, true, index); 138 property.SetValue(obj, true, index);
96 else if (t == typeof (byte[])) 139 else if (t == typeof(byte[]))
97 { 140 {
98 byte[] bytes = new byte[30]; 141 byte[] bytes = new byte[30];
99 random.NextBytes(bytes); 142 random.NextBytes(bytes);
100 property.SetValue(obj, bytes, index); 143 property.SetValue(obj, bytes, index);
101 } 144 }
102 else 145 else
103 Scramble(value); 146 internalScramble(value);
104 } 147 }
105 148
106 private static string RandomName() 149 private string RandomName()
107 { 150 {
108 StringBuilder name = new StringBuilder(); 151 StringBuilder name = new StringBuilder();
109 int size = random.Next(5, 12); 152 int size = random.Next(5, 12);
@@ -117,13 +160,27 @@ namespace OpenSim.Data.Tests
117 } 160 }
118 161
119 [TestFixture] 162 [TestFixture]
120 public class ScrableForTestingTest 163 public class PropertyScramblerTests
121 { 164 {
122 [Test] 165 [Test]
123 public void TestScramble() 166 public void TestScramble()
124 { 167 {
125 AssetBase actual = new AssetBase(UUID.Random(), "asset one"); 168 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
126 ScrambleForTesting.Scramble(actual); 169 new PropertyScrambler<AssetBase>().Scramble(actual);
170 }
171
172 [Test]
173 public void DontScramble()
174 {
175 UUID uuid = UUID.Random();
176 AssetBase asset = new AssetBase();
177 asset.FullID = uuid;
178 new PropertyScrambler<AssetBase>()
179 .DontScramble(x => x.Metadata)
180 .DontScramble(x => x.FullID)
181 .DontScramble(x => x.ID)
182 .Scramble(asset);
183 Assert.That(asset.FullID, Is.EqualTo(uuid));
127 } 184 }
128 } 185 }
129} \ No newline at end of file 186} \ No newline at end of file