29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Linq.Expressions;
32 using System.Reflection;
34 using NUnit.Framework;
36 using OpenSim.Framework;
37 using OpenSim.Tests.Common;
39 namespace OpenSim.Data.Tests
42 public class PropertyScrambler<T>
44 readonly System.Collections.Generic.List<
string> membersToNotScramble =
new List<string>();
46 private void AddExpressionToNotScrableList(Expression expression)
48 UnaryExpression unaryExpression = expression as UnaryExpression;
49 if (unaryExpression != null)
51 AddExpressionToNotScrableList(unaryExpression.Operand);
55 MemberExpression memberExpression = expression as MemberExpression;
56 if (memberExpression != null)
58 if (!(memberExpression.Member is PropertyInfo))
60 throw new NotImplementedException(
"I don't know how deal with a MemberExpression that is a " + expression.Type);
62 membersToNotScramble.Add(memberExpression.Member.Name);
66 throw new NotImplementedException(
"I don't know how to parse a " + expression.Type);
71 AddExpressionToNotScrableList(expression.Body);
77 internalScramble(obj);
80 private void internalScramble(
object obj)
82 PropertyInfo[] properties = obj.GetType().GetProperties();
83 foreach (var property
in properties)
87 if (property.GetIndexParameters().Length > 0)
90 RandomizeProperty(obj, property, null);
95 if (enumerable != null)
97 foreach (
object value
in enumerable)
99 internalScramble(value);
104 private readonly Random random =
new Random();
105 private void RandomizeProperty(
object obj, PropertyInfo property,
object[] index)
107 if (membersToNotScramble.Contains(property.Name))
109 Type t = property.PropertyType;
110 if (!property.CanWrite)
112 object value = property.GetValue(obj, index);
116 if (t == typeof(
string))
117 property.SetValue(obj, RandomName(), index);
118 else if (t == typeof(UUID))
119 property.SetValue(obj, UUID.Random(), index);
120 else if (t == typeof(sbyte))
121 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
122 else if (t == typeof(
short))
123 property.SetValue(obj, (
short)random.Next(
short.MinValue,
short.MaxValue), index);
124 else if (t == typeof(
int))
125 property.SetValue(obj, random.Next(), index);
126 else if (t == typeof(
long))
127 property.SetValue(obj, random.Next() * int.MaxValue, index);
128 else if (t == typeof(byte))
129 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
130 else if (t == typeof(ushort))
131 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
132 else if (t == typeof(uint))
133 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
134 else if (t == typeof(ulong))
135 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
136 else if (t == typeof(
bool))
137 property.SetValue(obj,
true, index);
138 else if (t == typeof(byte[]))
140 byte[] bytes =
new byte[30];
141 random.NextBytes(bytes);
142 property.SetValue(obj, bytes, index);
145 internalScramble(value);
148 private string RandomName()
150 StringBuilder name =
new StringBuilder();
151 int size = random.Next(5, 12);
152 for (
int i = 0; i < size; i++)
154 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
157 return name.ToString();
167 AssetBase actual =
new AssetBase(UUID.Random(),
"asset one", (sbyte)AssetType.Texture, UUID.Zero.ToString());
168 new PropertyScrambler<AssetBase>().Scramble(actual);
174 UUID uuid = UUID.Random();
175 AssetBase asset =
new AssetBase(uuid,
"asset", (sbyte)AssetType.Texture, UUID.Zero.ToString());
176 new PropertyScrambler<AssetBase>()
177 .DontScramble(x => x.Metadata)
178 .DontScramble(x => x.FullID)
179 .DontScramble(x => x.ID)
181 Assert.That(asset.FullID, Is.EqualTo(uuid));
PropertyScrambler< T > DontScramble(Expression< Func< T, object >> expression)
Asset class. All Assets are reference by this class or a class derived from this class ...
System.Collections.IEnumerable IEnumerable