1 // Copyright 2004-2007 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
;
19 using System
.Collections
.Generic
;
20 using System
.Collections
.Specialized
;
21 using System
.Security
.Principal
;
23 using Castle
.Components
.Common
.EmailSender
;
24 using Castle
.Components
.Validator
;
25 using Castle
.MonoRail
.Framework
.Internal
;
26 using Castle
.MonoRail
.Framework
.Services
;
29 /// Represents a mock implementation of <see cref="IRailsEngineContext"/> for unit test purposes.
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
;
52 /// Initializes a new instance of the <see cref="MockRailsEngineContext"/> class.
54 protected MockRailsEngineContext()
60 /// Initializes a new instance of the <see cref="MockRailsEngineContext"/> class.
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
;
71 this.urlInfo
= urlInfo
;
74 #region IRailsEngineContext Members
77 /// Gets the request type (GET, POST, etc)
80 public virtual String RequestType
82 get { return request.HttpMethod; }
86 /// Gets the request URL.
89 public virtual string Url
91 get { return urlInfo.UrlRaw; }
95 /// Gets the referring URL.
98 public string UrlReferrer
100 get { return urlReferrer; }
101 set { urlReferrer = value; }
105 /// Gets the underlying context of the API being used.
108 public virtual HttpContext UnderlyingContext
112 // new HttpContext(new HttpRequest(), new HttpResponse(writer));
113 // TODO: Consider whether it's worthwhile to implement this one (definitely it's not easy)
119 /// Access the params (Query, Post, headers and Cookies)
122 public virtual NameValueCollection Params
124 get { return request.Params; }
128 /// Access the session objects.
131 public virtual IDictionary Session
133 get { return session; }
137 /// Gets the request object.
140 public virtual IRequest Request
142 get { return request; }
146 /// Gets the response object.
149 public virtual IResponse Response
151 get { return response; }
155 /// Gets the trace object.
158 public virtual ITrace Trace
160 get { return trace; }
164 /// Access the Cache associated with this
165 /// web execution context.
168 public virtual ICacheProvider Cache
170 get { return cacheProvider; }
174 /// Access a dictionary of volative items.
177 public virtual Flash Flash
179 get { return flash; }
183 /// Transfer the execution to another resource.
185 /// <param name="path"></param>
186 /// <param name="preserveForm"></param>
187 public virtual void Transfer(string path
, bool preserveForm
)
189 throw new NotImplementedException();
193 /// Gets or sets the current user.
196 public IPrincipal CurrentUser
198 get { return currentUser; }
199 set { currentUser = value; }
203 /// Gets the last exception raised during
204 /// the execution of an action.
207 public Exception LastException
209 get { return lastException; }
210 set { lastException = value; }
214 /// Returns the application path.
217 public virtual string ApplicationPath
219 get { return urlInfo.AppVirtualDir; }
223 /// Returns the physical application path.
226 public virtual string ApplicationPhysicalPath
228 get { return physicalPath; }
232 /// Returns the <see cref="UrlInfo"/> of the the current request.
235 public virtual UrlInfo UrlInfo
237 get { return urlInfo; }
241 /// Returns an <see cref="IServerUtility"/>.
244 public virtual IServerUtility Server
246 get { return serverUtility; }
250 /// Returns the Items collection from the current HttpContext.
253 public virtual IDictionary Items
255 get { return contextItems; }
259 /// Gets or sets the current controller.
261 /// <value>The current controller.</value>
262 public virtual IController CurrentController
264 get { return currentController; }
265 set { currentController = value; }
269 /// If a container is available for the app, this
270 /// property exposes its instance.
273 public IServiceProvider Container
275 get { return container; }
281 /// Sets the container.
283 /// <param name="serviceProvider">The service provider.</param>
284 public void SetContainer(IServiceProvider serviceProvider
)
286 container
= serviceProvider
;
290 /// Gets the rendered email templates.
292 /// <value>The rendered email templates.</value>
293 public virtual List
<RenderedEmailTemplate
> RenderedEmailTemplates
295 get { return renderedEmailTemplates; }
299 /// Gets the messages sent.
301 /// <value>The messages sent.</value>
302 public virtual List
<Message
> MessagesSent
304 get { return messagesSent; }
308 /// Registers the services.
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
);
347 /// Represents an email template for unit test purposes
349 public class RenderedEmailTemplate
351 private readonly string name
;
352 private readonly IDictionary parameters
;
355 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
357 /// <param name="name">The name.</param>
358 /// <param name="parameters">The parameters.</param>
359 public RenderedEmailTemplate(string name
, IDictionary parameters
)
362 this.parameters
= parameters
;
368 /// <value>The name.</value>
375 /// Gets the parameters.
377 /// <value>The parameters.</value>
378 public IDictionary Parameters
380 get { return parameters; }