diff options
author | Kunnis | 2009-08-09 02:05:21 -0500 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2009-08-14 18:15:25 +0100 |
commit | c18f7560d9ab13c0c7a73c679383ceee13d1b1e3 (patch) | |
tree | d64cc86e616ce6d6a9de67cae6bae11513cb70bc /OpenSim/Data/Tests/ScrambleForTesting.cs | |
parent | Remove one more sponsor reference (diff) | |
download | opensim-SC-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.zip opensim-SC-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.gz opensim-SC-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.bz2 opensim-SC-c18f7560d9ab13c0c7a73c679383ceee13d1b1e3.tar.xz |
Adding in Reflection-based testing, to ensure that all properties are covered.
Diffstat (limited to 'OpenSim/Data/Tests/ScrambleForTesting.cs')
-rw-r--r-- | OpenSim/Data/Tests/ScrambleForTesting.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/OpenSim/Data/Tests/ScrambleForTesting.cs b/OpenSim/Data/Tests/ScrambleForTesting.cs new file mode 100644 index 0000000..c6e467f --- /dev/null +++ b/OpenSim/Data/Tests/ScrambleForTesting.cs | |||
@@ -0,0 +1,102 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Reflection; | ||
4 | using System.Text; | ||
5 | using NUnit.Framework; | ||
6 | using OpenMetaverse; | ||
7 | using OpenSim.Framework; | ||
8 | |||
9 | namespace OpenSim.Data.Tests | ||
10 | { | ||
11 | public static class ScrambleForTesting | ||
12 | { | ||
13 | private static readonly Random random = new Random(); | ||
14 | public static void Scramble(object obj) | ||
15 | { | ||
16 | PropertyInfo[] properties = obj.GetType().GetProperties(); | ||
17 | foreach (var property in properties) | ||
18 | { | ||
19 | //Skip indexers of classes. We will assume that everything that has an indexer | ||
20 | // is also IEnumberable. May not always be true, but should be true normally. | ||
21 | if(property.GetIndexParameters().Length > 0) | ||
22 | continue; | ||
23 | |||
24 | RandomizeProperty(obj, property, null); | ||
25 | } | ||
26 | //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize | ||
27 | // everything inside of it. | ||
28 | IEnumerable enumerable = obj as IEnumerable; | ||
29 | if(enumerable != null) | ||
30 | { | ||
31 | foreach (object value in enumerable) | ||
32 | { | ||
33 | Scramble(value); | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | private static void RandomizeProperty(object obj, PropertyInfo property, object[] index) | ||
39 | { | ||
40 | Type t = property.PropertyType; | ||
41 | if (!property.CanWrite) | ||
42 | return; | ||
43 | object value = property.GetValue(obj, index); | ||
44 | if (value == null) | ||
45 | return; | ||
46 | |||
47 | if (t == typeof (string)) | ||
48 | property.SetValue(obj, RandomName(), index); | ||
49 | else if (t == typeof (UUID)) | ||
50 | property.SetValue(obj, UUID.Random(), index); | ||
51 | else if (t == typeof (sbyte)) | ||
52 | property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index); | ||
53 | else if (t == typeof (short)) | ||
54 | property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index); | ||
55 | else if (t == typeof (int)) | ||
56 | property.SetValue(obj, random.Next(), index); | ||
57 | else if (t == typeof (long)) | ||
58 | property.SetValue(obj, random.Next() * int.MaxValue, index); | ||
59 | else if (t == typeof (byte)) | ||
60 | property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index); | ||
61 | else if (t == typeof (ushort)) | ||
62 | property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index); | ||
63 | else if (t == typeof (uint)) | ||
64 | property.SetValue(obj, Convert.ToUInt32(random.Next()), index); | ||
65 | else if (t == typeof (ulong)) | ||
66 | property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index); | ||
67 | else if (t == typeof (bool)) | ||
68 | property.SetValue(obj, true, index); | ||
69 | else if (t == typeof (byte[])) | ||
70 | { | ||
71 | byte[] bytes = new byte[30]; | ||
72 | random.NextBytes(bytes); | ||
73 | property.SetValue(obj, bytes, index); | ||
74 | } | ||
75 | else | ||
76 | Scramble(value); | ||
77 | } | ||
78 | |||
79 | private static string RandomName() | ||
80 | { | ||
81 | StringBuilder name = new StringBuilder(); | ||
82 | int size = random.Next(5, 12); | ||
83 | for (int i = 0; i < size; i++) | ||
84 | { | ||
85 | char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); | ||
86 | name.Append(ch); | ||
87 | } | ||
88 | return name.ToString(); | ||
89 | } | ||
90 | } | ||
91 | |||
92 | [TestFixture] | ||
93 | public class ScrableForTestingTest | ||
94 | { | ||
95 | [Test] | ||
96 | public void TestScramble() | ||
97 | { | ||
98 | AssetBase actual = new AssetBase(UUID.Random(), "asset one"); | ||
99 | ScrambleForTesting.Scramble(actual); | ||
100 | } | ||
101 | } | ||
102 | } \ No newline at end of file | ||