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();
105 jsonSerializer
= new NewtonsoftJSONSerializer();
108 #region IServiceContainer
111 /// Adds the specified service to the service container.
113 /// <param name="serviceType">The type of service to add.</param>
114 /// <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>
115 public void AddService(Type serviceType
, object serviceInstance
)
117 service2Impl
[serviceType
] = serviceInstance
;
121 /// Adds the specified service to the service container, and optionally promotes the service to any parent service containers.
123 /// <param name="serviceType">The type of service to add.</param>
124 /// <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>
125 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
126 public void AddService(Type serviceType
, object serviceInstance
, bool promote
)
128 throw new NotImplementedException();
132 /// Adds the specified service to the service container.
134 /// <param name="serviceType">The type of service to add.</param>
135 /// <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>
136 public void AddService(Type serviceType
, ServiceCreatorCallback callback
)
138 throw new NotImplementedException();
142 /// Adds the specified service to the service container, and optionally promotes the service to parent service containers.
144 /// <param name="serviceType">The type of service to add.</param>
145 /// <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>
146 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
147 public void AddService(Type serviceType
, ServiceCreatorCallback callback
, bool promote
)
149 throw new NotImplementedException();
153 /// Removes the specified service type from the service container.
155 /// <param name="serviceType">The type of service to remove.</param>
156 public void RemoveService(Type serviceType
)
158 throw new NotImplementedException();
162 /// Removes the specified service type from the service container, and optionally promotes the service to parent service containers.
164 /// <param name="serviceType">The type of service to remove.</param>
165 /// <param name="promote">true to promote this request to any parent service containers; otherwise, false.</param>
166 public void RemoveService(Type serviceType
, bool promote
)
168 throw new NotImplementedException();
174 /// Gets or sets the URL tokenizer.
176 /// <value>The URL tokenizer.</value>
177 public IUrlTokenizer UrlTokenizer
179 get { return urlTokenizer; }
180 set { urlTokenizer = value; }
184 /// Gets or sets the URL builder.
186 /// <value>The URL builder.</value>
187 public IUrlBuilder UrlBuilder
189 get { return urlBuilder; }
190 set { urlBuilder = value; }
194 /// Gets or sets the cache provider.
196 /// <value>The cache provider.</value>
197 public ICacheProvider CacheProvider
199 get { return cacheProvider; }
200 set { cacheProvider = value; }
204 /// Gets or sets the engine context factory.
206 /// <value>The engine context factory.</value>
207 public IEngineContextFactory EngineContextFactory
209 get { return engineContextFactory; }
210 set { engineContextFactory = value; }
214 /// Gets or sets the controller factory.
216 /// <value>The controller factory.</value>
217 public IControllerFactory ControllerFactory
219 get { return controllerFactory; }
220 set { controllerFactory = value; }
224 /// Gets or sets the controller context factory.
226 /// <value>The controller context factory.</value>
227 public IControllerContextFactory ControllerContextFactory
229 get { return controllerContextFactory; }
230 set { controllerContextFactory = value; }
234 /// Gets or sets the controller tree.
236 /// <value>The controller tree.</value>
237 public IControllerTree ControllerTree
239 get { return controllerTree; }
240 set { controllerTree = value; }
244 /// Gets or sets the view source loader.
246 /// <value>The view source loader.</value>
247 public IViewSourceLoader ViewSourceLoader
249 get { return viewSourceLoader; }
250 set { viewSourceLoader = value; }
254 /// Gets or sets the filter factory.
256 /// <value>The filter factory.</value>
257 public IFilterFactory FilterFactory
259 get { return filterFactory; }
260 set { filterFactory = value; }
264 /// Gets or sets the controller descriptor provider.
266 /// <value>The controller descriptor provider.</value>
267 public IControllerDescriptorProvider ControllerDescriptorProvider
269 get { return controllerDescriptorProvider; }
270 set { controllerDescriptorProvider = value; }
274 /// Gets or sets the view engine manager.
276 /// <value>The view engine manager.</value>
277 public IViewEngineManager ViewEngineManager
279 get { return viewEngineManager; }
280 set { viewEngineManager = value; }
284 /// Gets or sets the validator registry.
286 /// <value>The validator registry.</value>
287 public IValidatorRegistry ValidatorRegistry
289 get { return validatorRegistry; }
290 set { validatorRegistry = value; }
294 /// Gets or sets the action selector.
296 /// <value>The action selector.</value>
297 public IActionSelector ActionSelector
299 get { return actionSelector; }
300 set { actionSelector = value; }
304 /// Gets or sets the scaffold support.
306 /// <value>The scaffold support.</value>
307 public IScaffoldingSupport ScaffoldSupport
309 get { return scaffoldSupport; }
310 set { scaffoldSupport = value; }
314 /// Gets or sets the JSON serializer.
316 /// <value>The JSON serializer.</value>
317 public IJSONSerializer JSONSerializer
319 get { return jsonSerializer; }
320 set { jsonSerializer = value; }
324 /// Gets or sets the static resource registry service.
326 /// <value>The static resource registry.</value>
327 public IStaticResourceRegistry StaticResourceRegistry
329 get { return staticResourceRegistry; }
330 set { staticResourceRegistry = value; }
334 /// Gets or sets the email template service.
336 /// <value>The email template service.</value>
337 public IEmailTemplateService EmailTemplateService
339 get { return emailTemplateService; }
340 set { emailTemplateService = value; }
344 /// Gets or sets the email sender.
346 /// <value>The email sender.</value>
347 public IEmailSender EmailSender
349 get { return emailSender; }
350 set { emailSender = value; }
354 /// Gets or sets the resource factory.
356 /// <value>The resource factory.</value>
357 public IResourceFactory ResourceFactory
359 get { return resourceFactory; }
360 set { resourceFactory = value; }
364 /// Gets or sets the service initializer.
366 /// <value>The service initializer.</value>
367 public IServiceInitializer ServiceInitializer
369 get { return serviceInitializer; }
370 set { serviceInitializer = value; }
374 /// Gets or sets the helper factory.
376 /// <value>The helper factory.</value>
377 public IHelperFactory HelperFactory
379 get { return helperFactory; }
380 set { helperFactory = value; }
384 /// Gets or sets the extension manager.
386 /// <value>The extension manager.</value>
387 public ExtensionManager ExtensionManager
389 get { return extensionManager; }
390 set { extensionManager = value; }
394 /// Gets the service.
396 /// <typeparam name="T"></typeparam>
397 /// <returns></returns>
398 public T GetService
<T
>() where T
: class
400 return (T
) service2Impl
[typeof(T
)];
404 /// Gets the service object of the specified type.
406 /// <param name="serviceType">An object that specifies the type of service object to get.</param>
408 /// A service object of type <paramref name="serviceType"/>.-or- null if there is no service object of type <paramref name="serviceType"/>.
410 public object GetService(Type serviceType
)
413 service2Impl
.TryGetValue(serviceType
, out value);