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 IDictionary contextItems
= new HybridDictionary(true);
32 private readonly List
<RenderedEmailTemplate
> renderedEmailTemplates
= new List
<RenderedEmailTemplate
>();
33 private readonly List
<Message
> messagesSent
= new List
<Message
>();
34 private IMockRequest request
;
35 private IMockResponse response
;
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 public MockEngineContext()
51 : this(new MockRequest(), new MockResponse(), new MockServices(), new UrlInfo("area", "home", "index"))
56 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
58 /// <param name="urlInfo">The URL info.</param>
59 public MockEngineContext(UrlInfo urlInfo
)
60 : this(new MockRequest(), new MockResponse(), new MockServices(), urlInfo
)
65 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
67 /// <param name="request">The request.</param>
68 /// <param name="response">The response.</param>
69 /// <param name="urlInfo">The URL info.</param>
70 public MockEngineContext(IMockRequest request
, IMockResponse response
, UrlInfo urlInfo
)
71 : this(request
, response
, new MockServices(), urlInfo
)
76 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
78 /// <param name="request">The request.</param>
79 /// <param name="response">The response.</param>
80 /// <param name="services">The services.</param>
81 /// <param name="urlInfo">The URL info.</param>
82 public MockEngineContext(IMockRequest request
, IMockResponse response
, IMonoRailServices services
, UrlInfo urlInfo
)
84 this.request
= request
;
85 this.response
= response
;
86 this.services
= services
;
87 this.urlInfo
= urlInfo
;
91 response
.UrlInfo
= urlInfo
;
92 response
.UrlBuilder
= services
.UrlBuilder
;
97 /// Gets or sets the mock request.
99 /// <value>The mock request.</value>
100 public IMockRequest MockRequest
102 get { return request; }
103 set { request = value; }
107 /// Gets or sets the mock response.
109 /// <value>The mock response.</value>
110 public IMockResponse MockResponse
112 get { return response; }
113 set { response = value; }
117 /// Gets the underlying context of the API being used.
120 public virtual HttpContext UnderlyingContext
124 // new HttpContext(new HttpRequest(), new HttpResponse(writer));
125 // TODO: Consider whether it's worthwhile to implement this one (definitely it's not easy)
131 /// Access the session objects.
134 public virtual IDictionary Session
136 get { return session; }
137 set { session = value; }
141 /// Gets the request object.
144 public virtual IRequest Request
146 get { return request; }
150 /// Gets the response object.
153 public virtual IResponse Response
155 get { return response; }
159 /// Gets the trace object.
162 public virtual ITrace Trace
164 get { return trace; }
165 set { trace = value; }
169 /// Access a dictionary of volative items.
172 public virtual Flash Flash
174 get { return flash; }
175 set { flash = value; }
179 /// Gets or sets the current user.
182 public IPrincipal CurrentUser
184 get { return currentUser; }
185 set { currentUser = value; }
189 /// Gets the last exception raised during
190 /// the execution of an action.
193 public Exception LastException
195 get { return lastException; }
196 set { lastException = value; }
200 /// Returns the application path.
203 public virtual string ApplicationPath
205 get { return urlInfo.AppVirtualDir ?? "/"; }
209 /// Returns the physical application path.
212 public string ApplicationPhysicalPath
214 get { return AppDomain.CurrentDomain.BaseDirectory; }
218 /// Returns the <see cref="UrlInfo"/> of the the current request.
221 public virtual UrlInfo UrlInfo
223 get { return urlInfo; }
224 set { urlInfo = value; }
228 /// Returns an <see cref="IServerUtility"/>.
231 public virtual IServerUtility Server
233 get { return serverUtility; }
234 set { serverUtility = value; }
238 /// Returns the Items collection from the current HttpContext.
241 public virtual IDictionary Items
243 get { return contextItems; }
247 /// Gets or sets the current controller.
249 /// <value>The current controller.</value>
250 public virtual IController CurrentController
252 get { return currentController; }
253 set { currentController = value; }
257 /// Gets or sets the current controller context.
259 /// <value>The current controller context.</value>
260 public IControllerContext CurrentControllerContext
262 get { return currentControllerContext; }
263 set { currentControllerContext = value; }
267 /// Gets a reference to the MonoRail services.
269 /// <value>The services.</value>
270 public IMonoRailServices Services
272 get { return services; }
273 set { services = value; }
277 /// Gets the rendered email templates.
279 /// <value>The rendered email templates.</value>
280 public virtual List
<RenderedEmailTemplate
> RenderedEmailTemplates
282 get { return renderedEmailTemplates; }
286 /// Gets the messages sent.
288 /// <value>The messages sent.</value>
289 public virtual List
<Message
> MessagesSent
291 get { return messagesSent; }
294 internal void AddMailTemplateRendered(string templateName
, IDictionary parameters
)
296 renderedEmailTemplates
.Add(new RenderedEmailTemplate(templateName
, parameters
));
299 internal void AddEmailMessageSent(Message message
)
301 messagesSent
.Add(message
);
305 /// Represents an email template for unit test purposes
307 public class RenderedEmailTemplate
309 private readonly string name
;
310 private readonly IDictionary parameters
;
313 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
315 /// <param name="name">The name.</param>
316 /// <param name="parameters">The parameters.</param>
317 public RenderedEmailTemplate(string name
, IDictionary parameters
)
320 this.parameters
= new Hashtable(parameters
);
326 /// <value>The name.</value>
333 /// Gets the parameters.
335 /// <value>The parameters.</value>
336 public IDictionary Parameters
338 get { return parameters; }