Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Facilities / Cache / Castle.Facilities.Cache.Tests / CacheTest.cs
blob6e0870c057ac91087d2738b281ba1a75efeaf3f3
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 using System.Threading;
17 namespace Castle.Facilities.Cache.Tests
19 using System;
20 using System.IO;
22 using Castle.Facilities.Cache.Manager;
23 using Castle.Windsor;
25 using NUnit.Framework;
27 /// <summary>
28 /// Summary description for Class1.
29 /// </summary>
30 [TestFixture]
31 public class CacheTest
33 private StringWriter _outWriter = new StringWriter();
34 private IWindsorContainer _container = null;
36 [SetUp]
37 public void SetUp()
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));
44 ResetConsoleOut();
47 [TestFixtureTearDown]
48 public void TearDown()
50 _container.Dispose();
53 public void ResetConsoleOut()
55 _outWriter = new StringWriter();
56 Console.SetOut(_outWriter);
59 [Test]
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() );
73 [Test]
74 public void TestMultipleCacheViaCode()
76 IServiceD serviceD = _container[typeof(IServiceD)] as IServiceD;
78 // MethodeA
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() );
92 // MethodeB
93 ResetConsoleOut();
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() );
105 [Test]
106 public void TestCacheViaConfig()
108 IServiceB serviceB= _container[typeof(IServiceB)] as IServiceB;
110 // MethodeA
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() );
121 // MethodeB
122 ResetConsoleOut();
124 serviceB.MyMethodB();
125 consoleContents = _outWriter.GetStringBuilder().ToString();
127 serviceB.MyMethodB();
128 Assert.AreEqual(consoleContents, _outWriter.GetStringBuilder().ToString() );
131 [Test]
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);
142 ResetConsoleOut();
144 Thread.Sleep(10);
146 serviceA.MyMethod(2, 5.5M);
147 Assert.AreNotEqual( consoleContents , _outWriter.GetStringBuilder().ToString() );