1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Facilities
.Cache
.Tests
20 using Castle
.Facilities
.Cache
.Manager
;
23 using NUnit
.Framework
;
26 /// Summary description for Class1.
29 public class CacheTest
31 private StringWriter _outWriter
= new StringWriter();
32 private IWindsorContainer _container
= null;
37 _container
= new WindsorContainer(ConfigHelper
.ResolvePath("../Castle.Facilities.Cache.Tests.config"));
38 _container
.AddComponent("ServiceA",typeof(IServiceA
), typeof(ServiceA
));
39 _container
.AddComponent("ServiceC",typeof(IServiceC
), typeof(ServiceC
));
40 _container
.AddComponent("ServiceD",typeof(IServiceD
), typeof(ServiceD
));
46 public void TearDown()
51 public void ResetConsoleOut()
53 _outWriter
.GetStringBuilder().Length
= 0;
54 Console
.SetOut(_outWriter
);
58 public void TestCacheViaCode()
60 IServiceA serviceA
= _container
[typeof(IServiceA
)] as IServiceA
;
62 serviceA
.MyMethod(2, 5.5M
);
63 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
65 serviceA
.MyMethodNotcached("Gilles");
67 serviceA
.MyMethod(2, 5.5M
);
68 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
72 public void TestMultipleCacheViaCode()
74 IServiceD serviceD
= _container
[typeof(IServiceD
)] as IServiceD
;
77 FifoCacheManager fifoCacheManager
= _container
["Another.Cache"] as FifoCacheManager
;
78 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==0);
80 serviceD
.MyMethodA(2, 5);
81 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
82 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==1);
84 serviceD
.MyMethodA(2, 5);
85 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
87 serviceD
.MyMethodA(3, 5);
88 Assert
.IsFalse(consoleContents
== _outWriter
.GetStringBuilder().ToString() );
93 serviceD
.MyMethodB( "Castle" );
94 consoleContents
= _outWriter
.GetStringBuilder().ToString();
96 serviceD
.MyMethodB( "Castle" );
97 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
99 serviceD
.MyMethodB( "iBATIS" );
100 Assert
.IsFalse(consoleContents
== _outWriter
.GetStringBuilder().ToString() );
104 public void TestCacheViaConfig()
106 IServiceB serviceB
= _container
[typeof(IServiceB
)] as IServiceB
;
109 FifoCacheManager fifoCacheManager
= _container
["Another.Cache"] as FifoCacheManager
;
110 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==0);
112 serviceB
.MyMethodA("cache", "serviceB", "MyMethodA");
113 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
114 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==1);
116 serviceB
.MyMethodA("cache", "serviceB", "MyMethodA");
117 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
122 serviceB
.MyMethodB();
123 consoleContents
= _outWriter
.GetStringBuilder().ToString();
125 serviceB
.MyMethodB();
126 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
130 public void TestFicoCache()
132 IServiceA serviceA
= _container
[typeof(IServiceA
)] as IServiceA
;
133 IServiceC serviceC
= _container
[typeof(IServiceC
)] as IServiceC
;
135 serviceA
.MyMethod(2, 5.5M
);
136 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
138 serviceC
.MyMethod(2, 5.5M
);
142 WaitOneMillisecond();
144 serviceA
.MyMethod(2, 5.5M
);
145 Assert
.IsFalse( consoleContents
== _outWriter
.GetStringBuilder().ToString() );
148 private void WaitOneMillisecond()
151 DateTime startTime
= DateTime
.Now
;
152 while(startTime
.Millisecond
==DateTime
.Now
.Millisecond
)