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
;
19 using System
.Collections
.Generic
;
20 using System
.Collections
.Specialized
;
21 using System
.Security
.Principal
;
23 using Castle
.Components
.Common
.EmailSender
;
27 /// Represents a mock implementation of <see cref="IEngineContext"/> for unit test purposes.
29 public class MockEngineContext
: AbstractServiceContainer
, IEngineContext
31 private readonly IMockRequest request
;
32 private readonly IMockResponse response
;
33 private readonly IDictionary contextItems
= new HybridDictionary(true);
34 private readonly List
<RenderedEmailTemplate
> renderedEmailTemplates
= new List
<RenderedEmailTemplate
>();
35 private readonly List
<Message
> messagesSent
= new List
<Message
>();
36 private IServerUtility serverUtility
= new MockServerUtility();
37 private IPrincipal currentUser
;
38 private IDictionary session
= new HybridDictionary(true);
39 private IController currentController
;
40 private IControllerContext currentControllerContext
;
41 private IMonoRailServices services
;
43 private UrlInfo urlInfo
;
44 private Flash flash
= new Flash();
45 private Exception lastException
;
48 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
50 /// <param name="request">The request.</param>
51 /// <param name="response">The response.</param>
52 /// <param name="services">The services.</param>
53 /// <param name="urlInfo">The URL info.</param>
54 public MockEngineContext(IMockRequest request
, IMockResponse response
, IMonoRailServices services
, UrlInfo urlInfo
)
56 this.request
= request
;
57 this.response
= response
;
58 this.services
= services
;
59 this.urlInfo
= urlInfo
;
63 response
.UrlInfo
= urlInfo
;
64 response
.UrlBuilder
= services
.UrlBuilder
;
69 /// Gets the underlying context of the API being used.
72 public virtual HttpContext UnderlyingContext
76 // new HttpContext(new HttpRequest(), new HttpResponse(writer));
77 // TODO: Consider whether it's worthwhile to implement this one (definitely it's not easy)
83 /// Access the session objects.
86 public virtual IDictionary Session
88 get { return session; }
89 set { session = value; }
93 /// Gets the request object.
96 public virtual IRequest Request
98 get { return request; }
102 /// Gets the response object.
105 public virtual IResponse Response
107 get { return response; }
111 /// Gets the trace object.
114 public virtual ITrace Trace
116 get { return trace; }
117 set { trace = value; }
121 /// Access a dictionary of volative items.
124 public virtual Flash Flash
126 get { return flash; }
127 set { flash = value; }
131 /// Gets or sets the current user.
134 public IPrincipal CurrentUser
136 get { return currentUser; }
137 set { currentUser = value; }
141 /// Gets the last exception raised during
142 /// the execution of an action.
145 public Exception LastException
147 get { return lastException; }
148 set { lastException = value; }
152 /// Returns the application path.
155 public virtual string ApplicationPath
157 get { return urlInfo.AppVirtualDir ?? "/"; }
161 /// Returns the <see cref="UrlInfo"/> of the the current request.
164 public virtual UrlInfo UrlInfo
166 get { return urlInfo; }
167 set { urlInfo = value; }
171 /// Returns an <see cref="IServerUtility"/>.
174 public virtual IServerUtility Server
176 get { return serverUtility; }
177 set { serverUtility = value; }
181 /// Returns the Items collection from the current HttpContext.
184 public virtual IDictionary Items
186 get { return contextItems; }
190 /// Gets or sets the current controller.
192 /// <value>The current controller.</value>
193 public virtual IController CurrentController
195 get { return currentController; }
196 set { currentController = value; }
200 /// Gets or sets the current controller context.
202 /// <value>The current controller context.</value>
203 public IControllerContext CurrentControllerContext
205 get { return currentControllerContext; }
206 set { currentControllerContext = value; }
210 /// Gets a reference to the MonoRail services.
212 /// <value>The services.</value>
213 public IMonoRailServices Services
215 get { return services; }
216 set { services = value; }
220 /// Gets the rendered email templates.
222 /// <value>The rendered email templates.</value>
223 public virtual List
<RenderedEmailTemplate
> RenderedEmailTemplates
225 get { return renderedEmailTemplates; }
229 /// Gets the messages sent.
231 /// <value>The messages sent.</value>
232 public virtual List
<Message
> MessagesSent
234 get { return messagesSent; }
237 internal void AddMailTemplateRendered(string templateName
, IDictionary parameters
)
239 renderedEmailTemplates
.Add(new RenderedEmailTemplate(templateName
, parameters
));
242 internal void AddEmailMessageSent(Message message
)
244 messagesSent
.Add(message
);
248 /// Represents an email template for unit test purposes
250 public class RenderedEmailTemplate
252 private readonly string name
;
253 private readonly IDictionary parameters
;
256 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
258 /// <param name="name">The name.</param>
259 /// <param name="parameters">The parameters.</param>
260 public RenderedEmailTemplate(string name
, IDictionary parameters
)
263 this.parameters
= parameters
;
269 /// <value>The name.</value>
276 /// Gets the parameters.
278 /// <value>The parameters.</value>
279 public IDictionary Parameters
281 get { return parameters; }