Mercurial > pub > bltoolkit
comparison UnitTests/CS/Aspects/CacheAspectTest.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Collections; | |
3 using BLToolkit.Aspects; | |
4 using BLToolkit.Reflection; | |
5 | |
6 using NUnit.Framework; | |
7 | |
8 namespace Aspects | |
9 { | |
10 [TestFixture] | |
11 public class CacheAspectTest | |
12 { | |
13 [Log] | |
14 public abstract class TestClass | |
15 { | |
16 public static int Value; | |
17 | |
18 [Cache(500, false)] | |
19 public virtual int Test(int i1, int i2) | |
20 { | |
21 return Value; | |
22 } | |
23 | |
24 [InstanceCache] | |
25 public virtual int InstanceTest(int i1, int i2) | |
26 { | |
27 return Value; | |
28 } | |
29 } | |
30 | |
31 [Test] | |
32 public void Test() | |
33 { | |
34 TestClass t = TypeAccessor.CreateInstance<TestClass>(); | |
35 | |
36 DateTime begin = DateTime.Now; | |
37 | |
38 for (TestClass.Value = 777; t.Test(2, 2) == 777; TestClass.Value++) | |
39 continue; | |
40 | |
41 Assert.IsTrue((DateTime.Now - begin).TotalMilliseconds >= 500); | |
42 | |
43 TestClass.Value = 1; Assert.AreEqual(1, t.Test(1, 1)); | |
44 TestClass.Value = 2; Assert.AreEqual(1, t.Test(1, 1)); | |
45 TestClass.Value = 3; Assert.AreEqual(3, t.Test(2, 1)); | |
46 | |
47 CacheAspect.ClearCache(typeof(TestClass), "Test", typeof(int), typeof(int)); | |
48 TestClass.Value = 4; Assert.AreEqual(4, t.Test(2, 1)); | |
49 | |
50 CacheAspect.ClearCache(t.GetType(), "Test", typeof(int), typeof(int)); | |
51 TestClass.Value = 5; Assert.AreEqual(5, t.Test(2, 1)); | |
52 | |
53 CacheAspect.ClearCache(); | |
54 TestClass.Value = 6; Assert.AreEqual(6, t.Test(2, 1)); | |
55 } | |
56 | |
57 [Test] | |
58 public void InstanceTest() | |
59 { | |
60 TestClass t = TypeAccessor.CreateInstance<TestClass>(); | |
61 | |
62 TestClass.Value = 1; Assert.AreEqual(1, t.InstanceTest(1, 1)); | |
63 TestClass.Value = 2; Assert.AreEqual(1, t.InstanceTest(1, 1)); | |
64 TestClass.Value = 3; Assert.AreEqual(3, t.InstanceTest(2, 1)); | |
65 TestClass.Value = 4; | |
66 | |
67 t = TypeAccessor.CreateInstance<TestClass>(); | |
68 | |
69 Assert.AreNotEqual(1, t.InstanceTest(1, 1)); | |
70 Assert.AreNotEqual(3, t.InstanceTest(2, 1)); | |
71 } | |
72 | |
73 public class CustomCacheAspect : CacheAspect | |
74 { | |
75 private static IDictionary _methodcache = new Hashtable(); | |
76 public static IDictionary MethodCache | |
77 { | |
78 get { return _methodcache; } | |
79 } | |
80 | |
81 protected override IDictionary CreateCache() | |
82 { | |
83 return MethodCache; | |
84 } | |
85 } | |
86 | |
87 [Log] | |
88 public abstract class CustomTestClass | |
89 { | |
90 public static int Value; | |
91 | |
92 [Cache(typeof(CustomCacheAspect), MaxCacheTime = 500)] | |
93 public virtual int Test(int i1, int i2) | |
94 { | |
95 return Value; | |
96 } | |
97 } | |
98 | |
99 [Test] | |
100 public void CustomCacheAspectTest() | |
101 { | |
102 CustomTestClass t = (CustomTestClass)TypeAccessor.CreateInstance(typeof(CustomTestClass)); | |
103 | |
104 CustomTestClass.Value = 1; Assert.AreEqual(1, t.Test(1, 1)); | |
105 CustomTestClass.Value = 2; Assert.AreEqual(1, t.Test(1, 1)); | |
106 CustomTestClass.Value = 3; Assert.AreEqual(3, t.Test(2, 1)); | |
107 | |
108 CustomCacheAspect.MethodCache.Clear(); | |
109 CustomTestClass.Value = 4; Assert.AreEqual(4, t.Test(2, 1)); | |
110 } | |
111 | |
112 public abstract class GenericClass<T> | |
113 where T : new() | |
114 { | |
115 [Cache] | |
116 public virtual T GetT(int i) | |
117 { | |
118 return new T(); | |
119 } | |
120 } | |
121 | |
122 [Test] | |
123 public void GenericClassTest() | |
124 { | |
125 TypeAccessor<GenericClass<int>>. CreateInstance().GetT(0); | |
126 TypeAccessor<GenericClass<DateTime>>.CreateInstance().GetT(0); | |
127 } | |
128 | |
129 public abstract class TestClass1 | |
130 { | |
131 [Cache] | |
132 public virtual T Get<T>() | |
133 where T : new() | |
134 { | |
135 return new T(); | |
136 } | |
137 | |
138 [Cache] | |
139 public virtual T Get<T>(int i) | |
140 { | |
141 if (typeof(T) == typeof(int)) return (T)(object)10; | |
142 | |
143 return (T)(object)new DateTime(); | |
144 } | |
145 } | |
146 | |
147 //[Test] | |
148 public void GenericMethodTest() | |
149 { | |
150 var i = TypeAccessor<TestClass1>.CreateInstance().Get<int> (); | |
151 var d = TypeAccessor<TestClass1>.CreateInstance().Get<DateTime>(); | |
152 | |
153 i = TypeAccessor<TestClass1>.CreateInstance().Get<int> (0); | |
154 d = TypeAccessor<TestClass1>.CreateInstance().Get<DateTime>(0); | |
155 } | |
156 } | |
157 } |