Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Facilities / Cache / Castle.Facilities.Cache.Tests / CacheTest.cs
bloba31a545e89134b807757de2131d6c341f344fbb1
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.IO;
20 using Castle.Facilities.Cache.Manager;
21 using Castle.Windsor;
23 using NUnit.Framework;
25 /// <summary>
26 /// Summary description for Class1.
27 /// </summary>
28 [TestFixture]
29 public class CacheTest
31 private StringWriter _outWriter = new StringWriter();
32 private IWindsorContainer _container = null;
34 [SetUp]
35 public void SetUp()
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));
42 ResetConsoleOut();
45 [TestFixtureTearDown]
46 public void TearDown()
48 _container.Dispose();
51 public void ResetConsoleOut()
53 _outWriter.GetStringBuilder().Length = 0;
54 Console.SetOut(_outWriter);
57 [Test]
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() );
71 [Test]
72 public void TestMultipleCacheViaCode()
74 IServiceD serviceD = _container[typeof(IServiceD)] as IServiceD;
76 // MethodeA
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() );
90 // MethodeB
91 ResetConsoleOut();
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() );
103 [Test]
104 public void TestCacheViaConfig()
106 IServiceB serviceB= _container[typeof(IServiceB)] as IServiceB;
108 // MethodeA
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() );
119 // MethodeB
120 ResetConsoleOut();
122 serviceB.MyMethodB();
123 consoleContents = _outWriter.GetStringBuilder().ToString();
125 serviceB.MyMethodB();
126 Assert.AreEqual(consoleContents, _outWriter.GetStringBuilder().ToString() );
129 [Test]
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);
140 ResetConsoleOut();
142 WaitOneMillisecond();
144 serviceA.MyMethod(2, 5.5M);
145 Assert.IsFalse( consoleContents == _outWriter.GetStringBuilder().ToString() );
148 private void WaitOneMillisecond()
150 // Wait a moment
151 DateTime startTime = DateTime.Now;
152 while(startTime.Millisecond==DateTime.Now.Millisecond)