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
.Prevalence
.Tests
20 using Bamboo
.Prevalence
;
21 using Bamboo
.Prevalence
.Util
;
23 using Castle
.MicroKernel
;
24 using Castle
.Core
.Configuration
;
26 using NUnit
.Framework
;
29 /// Summary description for FacilityTestCase.
32 public class FacilityTestCase
39 _storageDir
= Path
.Combine( AppDomain
.CurrentDomain
.BaseDirectory
, "storage" );
45 Directory
.Delete(_storageDir
, true);
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
);
89 [Ignore("Test depending on KERNEL32.DLL")]
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
);
112 object policy
= kernel
[PrevalenceFacility
.CleanupPolicyComponentPropertyKey
];
113 Assert
.IsNotNull( policy
);
114 Assert
.IsTrue( policy
is ICleanUpPolicy
);
116 ((UserDatabase
)system
).Init();;
120 bool snapshoted
= false;
122 foreach(string file
in Directory
.GetFiles(_storageDir
))
124 if (Path
.GetExtension(file
).Equals(".snapshot"))
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
);