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
.MonoRail
.Framework
.Test
18 using System
.Collections
.Generic
;
19 using System
.ComponentModel
.Design
;
20 using Castle
.Components
.Common
.EmailSender
;
21 using Castle
.Components
.Validator
;
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
;
56 /// Initializes a new instance of the <see cref="MockServices"/> class with default mock services.
58 public MockServices() : this(new DefaultUrlBuilder(new MockServerUtility(), new MockRoutingEngine()),
59 new DefaultFilterFactory(),
60 new ViewEngineManagerStub(),
61 new DefaultActionSelector())
66 /// Initializes a new instance of the <see cref="MockServices"/> class.
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
109 /// Adds the specified service to the service container.
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
;
119 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
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();
130 /// Adds the specified service to the service container.
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();
140 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
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();
151 /// Removes the specified service type from the service container.
153 /// <param name="serviceType">The type of service to remove.</param>
154 public void RemoveService(Type serviceType
)
156 throw new NotImplementedException();
160 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
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();
172 /// Gets or sets the URL tokenizer.
174 /// <value>The URL tokenizer.</value>
175 public IUrlTokenizer UrlTokenizer
177 get { return urlTokenizer; }
178 set { urlTokenizer = value; }
182 /// Gets or sets the URL builder.
184 /// <value>The URL builder.</value>
185 public IUrlBuilder UrlBuilder
187 get { return urlBuilder; }
188 set { urlBuilder = value; }
192 /// Gets or sets the cache provider.
194 /// <value>The cache provider.</value>
195 public ICacheProvider CacheProvider
197 get { return cacheProvider; }
198 set { cacheProvider = value; }
202 /// Gets or sets the engine context factory.
204 /// <value>The engine context factory.</value>
205 public IEngineContextFactory EngineContextFactory
207 get { return engineContextFactory; }
208 set { engineContextFactory = value; }
212 /// Gets or sets the controller factory.
214 /// <value>The controller factory.</value>
215 public IControllerFactory ControllerFactory
217 get { return controllerFactory; }
218 set { controllerFactory = value; }
222 /// Gets or sets the controller context factory.
224 /// <value>The controller context factory.</value>
225 public IControllerContextFactory ControllerContextFactory
227 get { return controllerContextFactory; }
228 set { controllerContextFactory = value; }
232 /// Gets or sets the controller tree.
234 /// <value>The controller tree.</value>
235 public IControllerTree ControllerTree
237 get { return controllerTree; }
238 set { controllerTree = value; }
242 /// Gets or sets the view source loader.
244 /// <value>The view source loader.</value>
245 public IViewSourceLoader ViewSourceLoader
247 get { return viewSourceLoader; }
248 set { viewSourceLoader = value; }
252 /// Gets or sets the filter factory.
254 /// <value>The filter factory.</value>
255 public IFilterFactory FilterFactory
257 get { return filterFactory; }
258 set { filterFactory = value; }
262 /// Gets or sets the controller descriptor provider.
264 /// <value>The controller descriptor provider.</value>
265 public IControllerDescriptorProvider ControllerDescriptorProvider
267 get { return controllerDescriptorProvider; }
268 set { controllerDescriptorProvider = value; }
272 /// Gets or sets the view engine manager.
274 /// <value>The view engine manager.</value>
275 public IViewEngineManager ViewEngineManager
277 get { return viewEngineManager; }
278 set { viewEngineManager = value; }
282 /// Gets or sets the validator registry.
284 /// <value>The validator registry.</value>
285 public IValidatorRegistry ValidatorRegistry
287 get { return validatorRegistry; }
288 set { validatorRegistry = value; }
292 /// Gets or sets the action selector.
294 /// <value>The action selector.</value>
295 public IActionSelector ActionSelector
297 get { return actionSelector; }
298 set { actionSelector = value; }
302 /// Gets or sets the scaffold support.
304 /// <value>The scaffold support.</value>
305 public IScaffoldingSupport ScaffoldSupport
307 get { return scaffoldSupport; }
308 set { scaffoldSupport = value; }
312 /// Gets or sets the JSON serializer.
314 /// <value>The JSON serializer.</value>
315 public IJSONSerializer JSONSerializer
317 get { return jsonSerializer; }
318 set { jsonSerializer = value; }
322 /// Gets or sets the static resource registry service.
324 /// <value>The static resource registry.</value>
325 public IStaticResourceRegistry StaticResourceRegistry
327 get { return staticResourceRegistry; }
328 set { staticResourceRegistry = value; }
332 /// Gets or sets the email template service.
334 /// <value>The email template service.</value>
335 public IEmailTemplateService EmailTemplateService
337 get { return emailTemplateService; }
338 set { emailTemplateService = value; }
342 /// Gets or sets the email sender.
344 /// <value>The email sender.</value>
345 public IEmailSender EmailSender
347 get { return emailSender; }
348 set { emailSender = value; }
352 /// Gets or sets the resource factory.
354 /// <value>The resource factory.</value>
355 public IResourceFactory ResourceFactory
357 get { return resourceFactory; }
358 set { resourceFactory = value; }
362 /// Gets or sets the service initializer.
364 /// <value>The service initializer.</value>
365 public IServiceInitializer ServiceInitializer
367 get { return serviceInitializer; }
368 set { serviceInitializer = value; }
372 /// Gets or sets the helper factory.
374 /// <value>The helper factory.</value>
375 public IHelperFactory HelperFactory
377 get { return helperFactory; }
378 set { helperFactory = value; }
382 /// Gets or sets the extension manager.
384 /// <value>The extension manager.</value>
385 public ExtensionManager ExtensionManager
387 get { return extensionManager; }
388 set { extensionManager = value; }
392 /// Gets the service.
394 /// <typeparam name="T"></typeparam>
395 /// <returns></returns>
396 public T GetService
<T
>() where T
: class
398 return (T
) service2Impl
[typeof(T
)];
402 /// Gets the service object of the specified type.
404 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
406 /// A service object of type <paramref name="serviceType"/>.-or- null if there is no service object of type <paramref name="serviceType"/>.
408 public object GetService(Type serviceType
)
411 service2Impl
.TryGetValue(serviceType
, out value);