aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Tests/PropertyScrambler.cs
diff options
context:
space:
mode:
authorKunnis2009-08-16 03:35:31 -0500
committerTeravus Ovares (Dan Olivares)2009-08-16 14:34:16 -0400
commitdd78c250aed0924d06e28a826c2ad565ca232045 (patch)
tree331487699d7d2fba8d00cd2a8e57b85e8f77e794 /OpenSim/Data/Tests/PropertyScrambler.cs
parent* More improvements to BasicAssetTest.cs (diff)
downloadopensim-SC-dd78c250aed0924d06e28a826c2ad565ca232045.zip
opensim-SC-dd78c250aed0924d06e28a826c2ad565ca232045.tar.gz
opensim-SC-dd78c250aed0924d06e28a826c2ad565ca232045.tar.bz2
opensim-SC-dd78c250aed0924d06e28a826c2ad565ca232045.tar.xz
* Added Expression based ignores to the PropertyScrambler, which makes a lot of the tests clearer because I'm not constantly resetting properties.
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Tests/PropertyScrambler.cs159
1 files changed, 159 insertions, 0 deletions
diff --git a/OpenSim/Data/Tests/PropertyScrambler.cs b/OpenSim/Data/Tests/PropertyScrambler.cs
new file mode 100644
index 0000000..c56c10f
--- /dev/null
+++ b/OpenSim/Data/Tests/PropertyScrambler.cs
@@ -0,0 +1,159 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq.Expressions;
5using System.Reflection;
6using System.Text;
7using NUnit.Framework;
8using NUnit.Framework.SyntaxHelpers;
9using OpenMetaverse;
10using OpenSim.Framework;
11
12namespace OpenSim.Data.Tests
13{
14
15 //This is generic so that the lambda expressions will work right in IDEs.
16 public class PropertyScrambler<T>
17 {
18 readonly System.Collections.Generic.List<string> membersToNotScramble = new List<string>();
19
20 private void AddExpressionToNotScrableList(Expression expression)
21 {
22 UnaryExpression unaryExpression = expression as UnaryExpression;
23 if(unaryExpression != null)
24 {
25 AddExpressionToNotScrableList(unaryExpression.Operand);
26 return;
27 }
28
29 MemberExpression memberExpression = expression as MemberExpression;
30 if (memberExpression != null)
31 {
32 if (!(memberExpression.Member is PropertyInfo))
33 {
34 throw new NotImplementedException("I don't know how deal with a MemberExpression that is a " + expression.Type);
35 }
36 membersToNotScramble.Add(memberExpression.Member.Name);
37 return;
38 }
39
40 throw new NotImplementedException("I don't know how to parse a " + expression.Type);
41 }
42
43 public PropertyScrambler<T> DontScramble(Expression<Func<T, object>> expression)
44 {
45 AddExpressionToNotScrableList(expression.Body);
46 return this;
47 }
48
49 public void Scramble(T obj)
50 {
51 internalScramble(obj);
52 }
53
54 private void internalScramble(object obj)
55 {
56 PropertyInfo[] properties = obj.GetType().GetProperties();
57 foreach (var property in properties)
58 {
59 //Skip indexers of classes. We will assume that everything that has an indexer
60 // is also IEnumberable. May not always be true, but should be true normally.
61 if(property.GetIndexParameters().Length > 0)
62 continue;
63
64 RandomizeProperty(obj, property, null);
65 }
66 //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
67 // everything inside of it.
68 IEnumerable enumerable = obj as IEnumerable;
69 if(enumerable != null)
70 {
71 foreach (object value in enumerable)
72 {
73 internalScramble(value);
74 }
75 }
76 }
77
78 private readonly Random random = new Random();
79 private void RandomizeProperty(object obj, PropertyInfo property, object[] index)
80 {//I'd like a better way to compare, but I had lots of problems with InventoryFolderBase because the ID is inherited.
81 if(membersToNotScramble.Contains(property.Name))
82 return;
83 Type t = property.PropertyType;
84 if (!property.CanWrite)
85 return;
86 object value = property.GetValue(obj, index);
87 if (value == null)
88 return;
89
90 if (t == typeof(string))
91 property.SetValue(obj, RandomName(), index);
92 else if (t == typeof(UUID))
93 property.SetValue(obj, UUID.Random(), index);
94 else if (t == typeof(sbyte))
95 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
96 else if (t == typeof(short))
97 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
98 else if (t == typeof(int))
99 property.SetValue(obj, random.Next(), index);
100 else if (t == typeof(long))
101 property.SetValue(obj, random.Next() * int.MaxValue, index);
102 else if (t == typeof(byte))
103 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
104 else if (t == typeof(ushort))
105 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
106 else if (t == typeof(uint))
107 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
108 else if (t == typeof(ulong))
109 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
110 else if (t == typeof(bool))
111 property.SetValue(obj, true, index);
112 else if (t == typeof(byte[]))
113 {
114 byte[] bytes = new byte[30];
115 random.NextBytes(bytes);
116 property.SetValue(obj, bytes, index);
117 }
118 else
119 internalScramble(value);
120 }
121
122 private string RandomName()
123 {
124 StringBuilder name = new StringBuilder();
125 int size = random.Next(5, 12);
126 for (int i = 0; i < size; i++)
127 {
128 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
129 name.Append(ch);
130 }
131 return name.ToString();
132 }
133 }
134
135 [TestFixture]
136 public class PropertyScramblerTests
137 {
138 [Test]
139 public void TestScramble()
140 {
141 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
142 new PropertyScrambler<AssetBase>().Scramble(actual);
143 }
144
145 [Test]
146 public void DontScramble()
147 {
148 UUID uuid = UUID.Random();
149 AssetBase asset = new AssetBase();
150 asset.FullID = uuid;
151 new PropertyScrambler<AssetBase>()
152 .DontScramble(x => x.Metadata)
153 .DontScramble(x => x.FullID)
154 .DontScramble(x => x.ID)
155 .Scramble(asset);
156 Assert.That(asset.FullID, Is.EqualTo(uuid));
157 }
158 }
159} \ No newline at end of file