Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockEngineContext.cs
blob9bdcd92aeb8f0aec7d27afd2a2662ee4c7544dac
1 // Copyright 2004-2008 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 Container;
26 /// <summary>
27 /// Represents a mock implementation of <see cref="IEngineContext"/> for unit test purposes.
28 /// </summary>
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;
42 private ITrace trace;
43 private UrlInfo urlInfo;
44 private Flash flash = new Flash();
45 private Exception lastException;
47 /// <summary>
48 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
49 /// </summary>
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;
61 if (response != null)
63 response.UrlInfo = urlInfo;
64 response.UrlBuilder = services.UrlBuilder;
68 /// <summary>
69 /// Gets the underlying context of the API being used.
70 /// </summary>
71 /// <value></value>
72 public virtual HttpContext UnderlyingContext
74 get
76 // new HttpContext(new HttpRequest(), new HttpResponse(writer));
77 // TODO: Consider whether it's worthwhile to implement this one (definitely it's not easy)
78 return null;
82 /// <summary>
83 /// Access the session objects.
84 /// </summary>
85 /// <value></value>
86 public virtual IDictionary Session
88 get { return session; }
89 set { session = value; }
92 /// <summary>
93 /// Gets the request object.
94 /// </summary>
95 /// <value></value>
96 public virtual IRequest Request
98 get { return request; }
101 /// <summary>
102 /// Gets the response object.
103 /// </summary>
104 /// <value></value>
105 public virtual IResponse Response
107 get { return response; }
110 /// <summary>
111 /// Gets the trace object.
112 /// </summary>
113 /// <value></value>
114 public virtual ITrace Trace
116 get { return trace; }
117 set { trace = value; }
120 /// <summary>
121 /// Access a dictionary of volative items.
122 /// </summary>
123 /// <value></value>
124 public virtual Flash Flash
126 get { return flash; }
127 set { flash = value; }
130 /// <summary>
131 /// Gets or sets the current user.
132 /// </summary>
133 /// <value></value>
134 public IPrincipal CurrentUser
136 get { return currentUser; }
137 set { currentUser = value; }
140 /// <summary>
141 /// Gets the last exception raised during
142 /// the execution of an action.
143 /// </summary>
144 /// <value></value>
145 public Exception LastException
147 get { return lastException; }
148 set { lastException = value; }
151 /// <summary>
152 /// Returns the application path.
153 /// </summary>
154 /// <value></value>
155 public virtual string ApplicationPath
157 get { return urlInfo.AppVirtualDir ?? "/"; }
160 /// <summary>
161 /// Returns the <see cref="UrlInfo"/> of the the current request.
162 /// </summary>
163 /// <value></value>
164 public virtual UrlInfo UrlInfo
166 get { return urlInfo; }
167 set { urlInfo = value; }
170 /// <summary>
171 /// Returns an <see cref="IServerUtility"/>.
172 /// </summary>
173 /// <value></value>
174 public virtual IServerUtility Server
176 get { return serverUtility; }
177 set { serverUtility = value; }
180 /// <summary>
181 /// Returns the Items collection from the current HttpContext.
182 /// </summary>
183 /// <value></value>
184 public virtual IDictionary Items
186 get { return contextItems; }
189 /// <summary>
190 /// Gets or sets the current controller.
191 /// </summary>
192 /// <value>The current controller.</value>
193 public virtual IController CurrentController
195 get { return currentController; }
196 set { currentController = value; }
199 /// <summary>
200 /// Gets or sets the current controller context.
201 /// </summary>
202 /// <value>The current controller context.</value>
203 public IControllerContext CurrentControllerContext
205 get { return currentControllerContext; }
206 set { currentControllerContext = value; }
209 /// <summary>
210 /// Gets a reference to the MonoRail services.
211 /// </summary>
212 /// <value>The services.</value>
213 public IMonoRailServices Services
215 get { return services; }
216 set { services = value; }
219 /// <summary>
220 /// Gets the rendered email templates.
221 /// </summary>
222 /// <value>The rendered email templates.</value>
223 public virtual List<RenderedEmailTemplate> RenderedEmailTemplates
225 get { return renderedEmailTemplates; }
228 /// <summary>
229 /// Gets the messages sent.
230 /// </summary>
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);
247 /// <summary>
248 /// Represents an email template for unit test purposes
249 /// </summary>
250 public class RenderedEmailTemplate
252 private readonly string name;
253 private readonly IDictionary parameters;
255 /// <summary>
256 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
257 /// </summary>
258 /// <param name="name">The name.</param>
259 /// <param name="parameters">The parameters.</param>
260 public RenderedEmailTemplate(string name, IDictionary parameters)
262 this.name = name;
263 this.parameters = parameters;
266 /// <summary>
267 /// Gets the name.
268 /// </summary>
269 /// <value>The name.</value>
270 public string Name
272 get { return name; }
275 /// <summary>
276 /// Gets the parameters.
277 /// </summary>
278 /// <value>The parameters.</value>
279 public IDictionary Parameters
281 get { return parameters; }