Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / Facilities / Prevalence / Castle.Facilities.Prevalence.Tests / FacilityTestCase.cs
blob1ffb576c757dae1afd66c6e96bccd9e3ac83c041
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.Prevalence.Tests
17 using System;
18 using System.IO;
20 using Bamboo.Prevalence;
21 using Bamboo.Prevalence.Util;
23 using Castle.MicroKernel;
24 using Castle.Core.Configuration;
26 using NUnit.Framework;
28 /// <summary>
29 /// Summary description for FacilityTestCase.
30 /// </summary>
31 [TestFixture]
32 public class FacilityTestCase
34 String _storageDir;
36 [SetUp]
37 public void Init()
39 _storageDir = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "storage" );
42 [TearDown]
43 public void Clean()
45 Directory.Delete(_storageDir, true);
48 [Test]
49 public void TestUsage()
51 IKernel kernel = CreateConfiguredKernel();
52 kernel.AddFacility( "prevalence", new PrevalenceFacility() );
54 // Lookup for the engine
55 object engineService = kernel["engineid"];
56 Assert.IsNotNull( engineService );
57 Assert.IsTrue( engineService is PrevalenceEngine );
59 // Lookup for the system
60 object system = kernel["systemid"];
61 Assert.IsNotNull( system );
62 Assert.IsTrue( system is UserDatabase );
65 private IKernel CreateConfiguredKernel()
67 IKernel kernel = new DefaultKernel();
69 MutableConfiguration confignode = new MutableConfiguration("facility");
71 IConfiguration engines =
72 confignode.Children.Add( new MutableConfiguration("engines") );
74 IConfiguration engine =
75 engines.Children.Add( new MutableConfiguration("engine") );
77 engine.Attributes["id"] = "engineid";
78 engine.Attributes["systemId"] = "systemid";
79 engine.Attributes["systemType"] = typeof(UserDatabase).AssemblyQualifiedName;
80 engine.Attributes["storageDir"] = _storageDir;
82 kernel.ConfigurationStore.AddFacilityConfiguration( "prevalence", confignode );
84 return kernel;
87 [Test]
88 #if MONO
89 [Ignore("Test depending on KERNEL32.DLL")]
90 #endif
91 public void TestWithSnapshot()
93 IKernel kernel = CreateConfiguredSnapshotKernel();
94 kernel.AddFacility( "prevalence", new PrevalenceFacility() );
96 // Lookup for the engine
97 object engineService = kernel["engineid"];
98 Assert.IsNotNull( engineService );
99 Assert.IsTrue( engineService is PrevalenceEngine );
101 // Lookup for the system
102 object system = kernel["systemid"];
103 Assert.IsNotNull( system );
104 Assert.IsTrue( system is UserDatabase );
106 // Lookup for SnapshotTaker
107 object snapshotTaker = kernel[PrevalenceFacility.SnapShotTakerComponentPropertyKey];
108 Assert.IsNotNull( snapshotTaker );
109 Assert.IsTrue( snapshotTaker is SnapshotTaker );
111 //Cleanup Policy
112 object policy = kernel[PrevalenceFacility.CleanupPolicyComponentPropertyKey];
113 Assert.IsNotNull( policy );
114 Assert.IsTrue( policy is ICleanUpPolicy );
116 ((UserDatabase)system).Init();;
118 kernel.Dispose();
120 bool snapshoted = false;
122 foreach(string file in Directory.GetFiles(_storageDir))
124 if (Path.GetExtension(file).Equals(".snapshot"))
126 snapshoted = true;
130 Assert.IsTrue(snapshoted);
133 private IKernel CreateConfiguredSnapshotKernel()
135 IKernel kernel = new DefaultKernel();
137 MutableConfiguration confignode = new MutableConfiguration("facility");
139 IConfiguration engines =
140 confignode.Children.Add( new MutableConfiguration("engines") );
142 IConfiguration engine =
143 engines.Children.Add( new MutableConfiguration("engine") );
145 engine.Attributes["id"] = "engineid";
146 engine.Attributes["systemId"] = "systemid";
147 engine.Attributes["systemType"] = typeof(UserDatabase).AssemblyQualifiedName;
148 engine.Attributes["storageDir"] = _storageDir;
150 engine.Attributes["snapshotIntervalInHours"] = "1";
151 engine.Attributes["cleanupPolicyComponent"] = null;
153 kernel.ConfigurationStore.AddFacilityConfiguration( "prevalence", confignode );
155 return kernel;