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 using System
.Threading
;
17 namespace Castle
.Facilities
.Cache
.Tests
22 using Castle
.Facilities
.Cache
.Manager
;
25 using NUnit
.Framework
;
28 /// Summary description for Class1.
31 public class CacheTest
33 private StringWriter _outWriter
= new StringWriter();
34 private IWindsorContainer _container
= null;
39 _container
= new WindsorContainer(ConfigHelper
.ResolvePath("../Castle.Facilities.Cache.Tests.config"));
40 _container
.AddComponent("ServiceA",typeof(IServiceA
), typeof(ServiceA
));
41 _container
.AddComponent("ServiceC",typeof(IServiceC
), typeof(ServiceC
));
42 _container
.AddComponent("ServiceD",typeof(IServiceD
), typeof(ServiceD
));
48 public void TearDown()
53 public void ResetConsoleOut()
55 _outWriter
= new StringWriter();
56 Console
.SetOut(_outWriter
);
60 public void TestCacheViaCode()
62 IServiceA serviceA
= _container
[typeof(IServiceA
)] as IServiceA
;
64 serviceA
.MyMethod(2, 5.5M
);
65 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
67 serviceA
.MyMethodNotcached("Gilles");
69 serviceA
.MyMethod(2, 5.5M
);
70 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
74 public void TestMultipleCacheViaCode()
76 IServiceD serviceD
= _container
[typeof(IServiceD
)] as IServiceD
;
79 FifoCacheManager fifoCacheManager
= _container
["Another.Cache"] as FifoCacheManager
;
80 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==0);
82 serviceD
.MyMethodA(2, 5);
83 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
84 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==1);
86 serviceD
.MyMethodA(2, 5);
87 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
89 serviceD
.MyMethodA(3, 5);
90 Assert
.IsFalse(consoleContents
== _outWriter
.GetStringBuilder().ToString() );
95 serviceD
.MyMethodB( "Castle" );
96 consoleContents
= _outWriter
.GetStringBuilder().ToString();
98 serviceD
.MyMethodB( "Castle" );
99 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
101 serviceD
.MyMethodB( "iBATIS" );
102 Assert
.IsFalse(consoleContents
== _outWriter
.GetStringBuilder().ToString() );
106 public void TestCacheViaConfig()
108 IServiceB serviceB
= _container
[typeof(IServiceB
)] as IServiceB
;
111 FifoCacheManager fifoCacheManager
= _container
["Another.Cache"] as FifoCacheManager
;
112 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==0);
114 serviceB
.MyMethodA("cache", "serviceB", "MyMethodA");
115 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
116 Assert
.IsTrue(fifoCacheManager
.KeyList
.Count
==1);
118 serviceB
.MyMethodA("cache", "serviceB", "MyMethodA");
119 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
124 serviceB
.MyMethodB();
125 consoleContents
= _outWriter
.GetStringBuilder().ToString();
127 serviceB
.MyMethodB();
128 Assert
.AreEqual(consoleContents
, _outWriter
.GetStringBuilder().ToString() );
132 public void TestFicoCache()
134 IServiceA serviceA
= _container
[typeof(IServiceA
)] as IServiceA
;
135 IServiceC serviceC
= _container
[typeof(IServiceC
)] as IServiceC
;
137 serviceA
.MyMethod(2, 5.5M
);
138 string consoleContents
= _outWriter
.GetStringBuilder().ToString();
140 serviceC
.MyMethod(2, 5.5M
);
146 serviceA
.MyMethod(2, 5.5M
);
147 Assert
.AreNotEqual( consoleContents
, _outWriter
.GetStringBuilder().ToString() );