Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockServices.cs
blobc72f80d739ffc3df3b59d4bedfe7db84557e2f17
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.MonoRail.Framework.Test
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel.Design;
20 using Castle.Components.Common.EmailSender;
21 using Castle.Components.Validator;
22 using Providers;
23 using Resources;
24 using Services;
26 /// <summary>
27 /// Pendent
28 /// </summary>
29 public class MockServices : IMonoRailServices, IServiceContainer
31 private IUrlTokenizer urlTokenizer;
32 private IUrlBuilder urlBuilder;
33 private ICacheProvider cacheProvider;
34 private IEngineContextFactory engineContextFactory;
35 private IControllerFactory controllerFactory;
36 private IControllerContextFactory controllerContextFactory;
37 private IControllerTree controllerTree;
38 private IViewSourceLoader viewSourceLoader;
39 private IFilterFactory filterFactory;
40 private IControllerDescriptorProvider controllerDescriptorProvider;
41 private IViewEngineManager viewEngineManager;
42 private IValidatorRegistry validatorRegistry;
43 private IActionSelector actionSelector;
44 private IScaffoldingSupport scaffoldSupport;
45 private IJSONSerializer jsonSerializer;
46 private IStaticResourceRegistry staticResourceRegistry;
47 private IEmailTemplateService emailTemplateService;
48 private IEmailSender emailSender;
49 private IResourceFactory resourceFactory;
50 private ExtensionManager extensionManager;
51 private readonly Dictionary<Type, object> service2Impl = new Dictionary<Type, object>();
52 private IServiceInitializer serviceInitializer;
53 private IHelperFactory helperFactory;
55 /// <summary>
56 /// Initializes a new instance of the <see cref="MockServices"/> class with default mock services.
57 /// </summary>
58 public MockServices() : this(new DefaultUrlBuilder(new MockServerUtility(), new MockRoutingEngine()),
59 new DefaultFilterFactory(),
60 new ViewEngineManagerStub(),
61 new DefaultActionSelector())
65 /// <summary>
66 /// Initializes a new instance of the <see cref="MockServices"/> class.
67 /// </summary>
68 /// <param name="urlBuilder">The URL builder.</param>
69 /// <param name="filterFactory">The filter factory.</param>
70 /// <param name="viewEngineManager">The view engine manager.</param>
71 /// <param name="actionSelector">The action selector.</param>
72 public MockServices(IUrlBuilder urlBuilder, IFilterFactory filterFactory, IViewEngineManager viewEngineManager,
73 IActionSelector actionSelector)
75 this.urlBuilder = urlBuilder;
76 this.filterFactory = filterFactory;
77 this.viewEngineManager = viewEngineManager;
78 this.actionSelector = actionSelector;
80 controllerTree = new DefaultControllerTree();
81 controllerFactory = new DefaultControllerFactory(controllerTree);
82 staticResourceRegistry = new DefaultStaticResourceRegistry();
84 controllerContextFactory = new DefaultControllerContextFactory();
86 controllerDescriptorProvider = new DefaultControllerDescriptorProvider(
87 new DefaultHelperDescriptorProvider(),
88 new DefaultFilterDescriptorProvider(),
89 new DefaultLayoutDescriptorProvider(),
90 new DefaultRescueDescriptorProvider(),
91 new DefaultResourceDescriptorProvider(),
92 new DefaultTransformFilterDescriptorProvider(),
93 new DefaultReturnBinderDescriptorProvider());
95 resourceFactory = new DefaultResourceFactory();
96 scaffoldSupport = new MockScaffoldingSupport();
97 cacheProvider = new MockCacheProvider();
98 helperFactory = new DefaultHelperFactory();
99 serviceInitializer = new DefaultServiceInitializer();
101 extensionManager = new ExtensionManager(this);
103 validatorRegistry = new CachedValidationRegistry();
106 #region IServiceContainer
108 /// <summary>
109 /// Adds the specified service to the service container.
110 /// </summary>
111 /// <param name="serviceType">The type of service to add.</param>
112 /// <param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the <paramref name="serviceType"/> parameter.</param>
113 public void AddService(Type serviceType, object serviceInstance)
115 service2Impl[serviceType] = serviceInstance;
118 /// <summary>
119 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
120 /// </summary>
121 /// <param name="serviceType">The type of service to add.</param>
122 /// <param name="serviceInstance">An instance of the service type to add. This object must implement or inherit from the type indicated by the <paramref name="serviceType"/> parameter.</param>
123 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
124 public void AddService(Type serviceType, object serviceInstance, bool promote)
126 throw new NotImplementedException();
129 /// <summary>
130 /// Adds the specified service to the service container.
131 /// </summary>
132 /// <param name="serviceType">The type of service to add.</param>
133 /// <param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.</param>
134 public void AddService(Type serviceType, ServiceCreatorCallback callback)
136 throw new NotImplementedException();
139 /// <summary>
140 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
141 /// </summary>
142 /// <param name="serviceType">The type of service to add.</param>
143 /// <param name="callback">A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested.</param>
144 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
145 public void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
147 throw new NotImplementedException();
150 /// <summary>
151 /// Removes the specified service type from the service container.
152 /// </summary>
153 /// <param name="serviceType">The type of service to remove.</param>
154 public void RemoveService(Type serviceType)
156 throw new NotImplementedException();
159 /// <summary>
160 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
161 /// </summary>
162 /// <param name="serviceType">The type of service to remove.</param>
163 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
164 public void RemoveService(Type serviceType, bool promote)
166 throw new NotImplementedException();
169 #endregion
171 /// <summary>
172 /// Gets or sets the URL tokenizer.
173 /// </summary>
174 /// <value>The URL tokenizer.</value>
175 public IUrlTokenizer UrlTokenizer
177 get { return urlTokenizer; }
178 set { urlTokenizer = value; }
181 /// <summary>
182 /// Gets or sets the URL builder.
183 /// </summary>
184 /// <value>The URL builder.</value>
185 public IUrlBuilder UrlBuilder
187 get { return urlBuilder; }
188 set { urlBuilder = value; }
191 /// <summary>
192 /// Gets or sets the cache provider.
193 /// </summary>
194 /// <value>The cache provider.</value>
195 public ICacheProvider CacheProvider
197 get { return cacheProvider; }
198 set { cacheProvider = value; }
201 /// <summary>
202 /// Gets or sets the engine context factory.
203 /// </summary>
204 /// <value>The engine context factory.</value>
205 public IEngineContextFactory EngineContextFactory
207 get { return engineContextFactory; }
208 set { engineContextFactory = value; }
211 /// <summary>
212 /// Gets or sets the controller factory.
213 /// </summary>
214 /// <value>The controller factory.</value>
215 public IControllerFactory ControllerFactory
217 get { return controllerFactory; }
218 set { controllerFactory = value; }
221 /// <summary>
222 /// Gets or sets the controller context factory.
223 /// </summary>
224 /// <value>The controller context factory.</value>
225 public IControllerContextFactory ControllerContextFactory
227 get { return controllerContextFactory; }
228 set { controllerContextFactory = value; }
231 /// <summary>
232 /// Gets or sets the controller tree.
233 /// </summary>
234 /// <value>The controller tree.</value>
235 public IControllerTree ControllerTree
237 get { return controllerTree; }
238 set { controllerTree = value; }
241 /// <summary>
242 /// Gets or sets the view source loader.
243 /// </summary>
244 /// <value>The view source loader.</value>
245 public IViewSourceLoader ViewSourceLoader
247 get { return viewSourceLoader; }
248 set { viewSourceLoader = value; }
251 /// <summary>
252 /// Gets or sets the filter factory.
253 /// </summary>
254 /// <value>The filter factory.</value>
255 public IFilterFactory FilterFactory
257 get { return filterFactory; }
258 set { filterFactory = value; }
261 /// <summary>
262 /// Gets or sets the controller descriptor provider.
263 /// </summary>
264 /// <value>The controller descriptor provider.</value>
265 public IControllerDescriptorProvider ControllerDescriptorProvider
267 get { return controllerDescriptorProvider; }
268 set { controllerDescriptorProvider = value; }
271 /// <summary>
272 /// Gets or sets the view engine manager.
273 /// </summary>
274 /// <value>The view engine manager.</value>
275 public IViewEngineManager ViewEngineManager
277 get { return viewEngineManager; }
278 set { viewEngineManager = value; }
281 /// <summary>
282 /// Gets or sets the validator registry.
283 /// </summary>
284 /// <value>The validator registry.</value>
285 public IValidatorRegistry ValidatorRegistry
287 get { return validatorRegistry; }
288 set { validatorRegistry = value; }
291 /// <summary>
292 /// Gets or sets the action selector.
293 /// </summary>
294 /// <value>The action selector.</value>
295 public IActionSelector ActionSelector
297 get { return actionSelector; }
298 set { actionSelector = value; }
301 /// <summary>
302 /// Gets or sets the scaffold support.
303 /// </summary>
304 /// <value>The scaffold support.</value>
305 public IScaffoldingSupport ScaffoldSupport
307 get { return scaffoldSupport; }
308 set { scaffoldSupport = value; }
311 /// <summary>
312 /// Gets or sets the JSON serializer.
313 /// </summary>
314 /// <value>The JSON serializer.</value>
315 public IJSONSerializer JSONSerializer
317 get { return jsonSerializer; }
318 set { jsonSerializer = value; }
321 /// <summary>
322 /// Gets or sets the static resource registry service.
323 /// </summary>
324 /// <value>The static resource registry.</value>
325 public IStaticResourceRegistry StaticResourceRegistry
327 get { return staticResourceRegistry; }
328 set { staticResourceRegistry = value; }
331 /// <summary>
332 /// Gets or sets the email template service.
333 /// </summary>
334 /// <value>The email template service.</value>
335 public IEmailTemplateService EmailTemplateService
337 get { return emailTemplateService; }
338 set { emailTemplateService = value; }
341 /// <summary>
342 /// Gets or sets the email sender.
343 /// </summary>
344 /// <value>The email sender.</value>
345 public IEmailSender EmailSender
347 get { return emailSender; }
348 set { emailSender = value; }
351 /// <summary>
352 /// Gets or sets the resource factory.
353 /// </summary>
354 /// <value>The resource factory.</value>
355 public IResourceFactory ResourceFactory
357 get { return resourceFactory; }
358 set { resourceFactory = value; }
361 /// <summary>
362 /// Gets or sets the service initializer.
363 /// </summary>
364 /// <value>The service initializer.</value>
365 public IServiceInitializer ServiceInitializer
367 get { return serviceInitializer; }
368 set { serviceInitializer = value; }
371 /// <summary>
372 /// Gets or sets the helper factory.
373 /// </summary>
374 /// <value>The helper factory.</value>
375 public IHelperFactory HelperFactory
377 get { return helperFactory; }
378 set { helperFactory = value; }
381 /// <summary>
382 /// Gets or sets the extension manager.
383 /// </summary>
384 /// <value>The extension manager.</value>
385 public ExtensionManager ExtensionManager
387 get { return extensionManager; }
388 set { extensionManager = value; }
391 /// <summary>
392 /// Gets the service.
393 /// </summary>
394 /// <typeparam name="T"></typeparam>
395 /// <returns></returns>
396 public T GetService<T>() where T : class
398 return (T) service2Impl[typeof(T)];
401 /// <summary>
402 /// Gets the service object of the specified type.
403 /// </summary>
404 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
405 /// <returns>
406 /// A service object of type <paramref name="serviceType"/>.-or- null if there is no service object of type <paramref name="serviceType"/>.
407 /// </returns>
408 public object GetService(Type serviceType)
410 object value;
411 service2Impl.TryGetValue(serviceType, out value);
412 return value;