- Changed ReflectionBasedDictionaryAdapter to ignore calls to Remove instead of throw...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockRailsEngineContext.cs
blob17c777d9345c91473d7ab898b1fa5fd2e56c3cb2
1 // Copyright 2004-2007 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;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Security.Principal;
22 using System.Web;
23 using Castle.Components.Common.EmailSender;
24 using Castle.Components.Validator;
25 using Castle.MonoRail.Framework.Internal;
26 using Castle.MonoRail.Framework.Services;
28 /// <summary>
29 /// Represents a mock implementation of <see cref="IRailsEngineContext"/> for unit test purposes.
30 /// </summary>
31 public class MockRailsEngineContext : AbstractServiceContainer, IRailsEngineContext
33 private readonly string physicalPath = AppDomain.CurrentDomain.BaseDirectory;
34 private readonly IRequest request;
35 private readonly IResponse response;
36 private readonly ITrace trace;
37 private readonly UrlInfo urlInfo;
38 private readonly Flash flash = new Flash();
39 private readonly ICacheProvider cacheProvider = new MockCacheProvider();
40 private readonly IServerUtility serverUtility = new MockServerUtility();
41 private readonly IDictionary session = new HybridDictionary(true);
42 private readonly IDictionary contextItems = new HybridDictionary(true);
43 private readonly List<RenderedEmailTemplate> renderedEmailTemplates = new List<RenderedEmailTemplate>();
44 private readonly List<Message> messagesSent = new List<Message>();
45 private string urlReferrer;
46 private IServiceProvider container;
47 private IPrincipal currentUser;
48 private Exception lastException;
49 private IController currentController;
51 /// <summary>
52 /// Initializes a new instance of the <see cref="MockRailsEngineContext"/> class.
53 /// </summary>
54 protected MockRailsEngineContext()
56 RegisterServices();
59 /// <summary>
60 /// Initializes a new instance of the <see cref="MockRailsEngineContext"/> class.
61 /// </summary>
62 /// <param name="request">The request.</param>
63 /// <param name="response">The response.</param>
64 /// <param name="trace">The trace.</param>
65 /// <param name="urlInfo">The URL info.</param>
66 public MockRailsEngineContext(IRequest request, IResponse response, ITrace trace, UrlInfo urlInfo) : this()
68 this.request = request;
69 this.response = response;
70 this.trace = trace;
71 this.urlInfo = urlInfo;
74 #region IRailsEngineContext Members
76 /// <summary>
77 /// Gets the request type (GET, POST, etc)
78 /// </summary>
79 /// <value></value>
80 public virtual String RequestType
82 get { return request.HttpMethod; }
85 /// <summary>
86 /// Gets the request URL.
87 /// </summary>
88 /// <value></value>
89 public virtual string Url
91 get { return urlInfo.UrlRaw; }
94 /// <summary>
95 /// Gets the referring URL.
96 /// </summary>
97 /// <value></value>
98 public string UrlReferrer
100 get { return urlReferrer; }
101 set { urlReferrer = value; }
104 /// <summary>
105 /// Gets the underlying context of the API being used.
106 /// </summary>
107 /// <value></value>
108 public virtual HttpContext UnderlyingContext
110 get
112 // new HttpContext(new HttpRequest(), new HttpResponse(writer));
113 // TODO: Consider whether it's worthwhile to implement this one (definitely it's not easy)
114 return null;
118 /// <summary>
119 /// Access the params (Query, Post, headers and Cookies)
120 /// </summary>
121 /// <value></value>
122 public virtual NameValueCollection Params
124 get { return request.Params; }
127 /// <summary>
128 /// Access the session objects.
129 /// </summary>
130 /// <value></value>
131 public virtual IDictionary Session
133 get { return session; }
136 /// <summary>
137 /// Gets the request object.
138 /// </summary>
139 /// <value></value>
140 public virtual IRequest Request
142 get { return request; }
145 /// <summary>
146 /// Gets the response object.
147 /// </summary>
148 /// <value></value>
149 public virtual IResponse Response
151 get { return response; }
154 /// <summary>
155 /// Gets the trace object.
156 /// </summary>
157 /// <value></value>
158 public virtual ITrace Trace
160 get { return trace; }
163 /// <summary>
164 /// Access the Cache associated with this
165 /// web execution context.
166 /// </summary>
167 /// <value></value>
168 public virtual ICacheProvider Cache
170 get { return cacheProvider; }
173 /// <summary>
174 /// Access a dictionary of volative items.
175 /// </summary>
176 /// <value></value>
177 public virtual Flash Flash
179 get { return flash; }
182 /// <summary>
183 /// Transfer the execution to another resource.
184 /// </summary>
185 /// <param name="path"></param>
186 /// <param name="preserveForm"></param>
187 public virtual void Transfer(string path, bool preserveForm)
189 throw new NotImplementedException();
192 /// <summary>
193 /// Gets or sets the current user.
194 /// </summary>
195 /// <value></value>
196 public IPrincipal CurrentUser
198 get { return currentUser; }
199 set { currentUser = value; }
202 /// <summary>
203 /// Gets the last exception raised during
204 /// the execution of an action.
205 /// </summary>
206 /// <value></value>
207 public Exception LastException
209 get { return lastException; }
210 set { lastException = value; }
213 /// <summary>
214 /// Returns the application path.
215 /// </summary>
216 /// <value></value>
217 public virtual string ApplicationPath
219 get { return urlInfo.AppVirtualDir; }
222 /// <summary>
223 /// Returns the physical application path.
224 /// </summary>
225 /// <value></value>
226 public virtual string ApplicationPhysicalPath
228 get { return physicalPath; }
231 /// <summary>
232 /// Returns the <see cref="UrlInfo"/> of the the current request.
233 /// </summary>
234 /// <value></value>
235 public virtual UrlInfo UrlInfo
237 get { return urlInfo; }
240 /// <summary>
241 /// Returns an <see cref="IServerUtility"/>.
242 /// </summary>
243 /// <value></value>
244 public virtual IServerUtility Server
246 get { return serverUtility; }
249 /// <summary>
250 /// Returns the Items collection from the current HttpContext.
251 /// </summary>
252 /// <value></value>
253 public virtual IDictionary Items
255 get { return contextItems; }
258 /// <summary>
259 /// Gets or sets the current controller.
260 /// </summary>
261 /// <value>The current controller.</value>
262 public virtual IController CurrentController
264 get { return currentController; }
265 set { currentController = value; }
268 /// <summary>
269 /// If a container is available for the app, this
270 /// property exposes its instance.
271 /// </summary>
272 /// <value></value>
273 public IServiceProvider Container
275 get { return container; }
278 #endregion
280 /// <summary>
281 /// Sets the container.
282 /// </summary>
283 /// <param name="serviceProvider">The service provider.</param>
284 public void SetContainer(IServiceProvider serviceProvider)
286 container = serviceProvider;
289 /// <summary>
290 /// Gets the rendered email templates.
291 /// </summary>
292 /// <value>The rendered email templates.</value>
293 public virtual List<RenderedEmailTemplate> RenderedEmailTemplates
295 get { return renderedEmailTemplates; }
298 /// <summary>
299 /// Gets the messages sent.
300 /// </summary>
301 /// <value>The messages sent.</value>
302 public virtual List<Message> MessagesSent
304 get { return messagesSent; }
307 /// <summary>
308 /// Registers the services.
309 /// </summary>
310 private void RegisterServices()
312 DefaultUrlBuilder urlBuilder = new DefaultUrlBuilder();
313 urlBuilder.ServerUtil = serverUtility;
314 AddService(typeof(IUrlBuilder), urlBuilder);
316 AddService(typeof(IValidatorRegistry), new CachedValidationRegistry());
318 AddService(typeof(IEmailTemplateService), new MockEmailTemplateService(this));
319 AddService(typeof(IEmailSender), new MockSmtpSender(this));
321 AddService(typeof(IHelperDescriptorProvider), new DefaultHelperDescriptorProvider());
322 AddService(typeof(IFilterDescriptorProvider), new DefaultFilterDescriptorProvider());
323 AddService(typeof(ILayoutDescriptorProvider), new DefaultLayoutDescriptorProvider());
324 AddService(typeof(IRescueDescriptorProvider), new DefaultRescueDescriptorProvider());
325 AddService(typeof(IResourceDescriptorProvider), new DefaultResourceDescriptorProvider());
326 AddService(typeof(ITransformFilterDescriptorProvider), new DefaultTransformFilterDescriptorProvider());
328 DefaultControllerDescriptorProvider controllerDescProvider = new DefaultControllerDescriptorProvider();
329 controllerDescProvider.Service(this);
330 AddService(typeof(IControllerDescriptorProvider), controllerDescProvider);
332 AddService(typeof(IViewEngineManager), new DefaultViewEngineManager());
333 AddService(typeof(IScaffoldingSupport), new MockScaffoldingSupport());
336 internal void AddMailTemplateRendered(string templateName, IDictionary parameters)
338 renderedEmailTemplates.Add(new RenderedEmailTemplate(templateName, parameters));
341 internal void AddEmailMessageSent(Message message)
343 messagesSent.Add(message);
346 /// <summary>
347 /// Represents an email template for unit test purposes
348 /// </summary>
349 public class RenderedEmailTemplate
351 private readonly string name;
352 private readonly IDictionary parameters;
354 /// <summary>
355 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
356 /// </summary>
357 /// <param name="name">The name.</param>
358 /// <param name="parameters">The parameters.</param>
359 public RenderedEmailTemplate(string name, IDictionary parameters)
361 this.name = name;
362 this.parameters = parameters;
365 /// <summary>
366 /// Gets the name.
367 /// </summary>
368 /// <value>The name.</value>
369 public string Name
371 get { return name; }
374 /// <summary>
375 /// Gets the parameters.
376 /// </summary>
377 /// <value>The parameters.</value>
378 public IDictionary Parameters
380 get { return parameters; }