Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockEngineContext.cs
blob719afbdae8833f756863928ea2e20a39795ae8f3
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 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;
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 public MockEngineContext()
51 : this(new MockRequest(), new MockResponse(), new MockServices(), new UrlInfo("area", "home", "index"))
55 /// <summary>
56 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
57 /// </summary>
58 /// <param name="urlInfo">The URL info.</param>
59 public MockEngineContext(UrlInfo urlInfo)
60 : this(new MockRequest(), new MockResponse(), new MockServices(), urlInfo)
64 /// <summary>
65 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
66 /// </summary>
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)
75 /// <summary>
76 /// Initializes a new instance of the <see cref="MockEngineContext"/> class.
77 /// </summary>
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;
89 if (response != null)
91 response.UrlInfo = urlInfo;
92 response.UrlBuilder = services.UrlBuilder;
96 /// <summary>
97 /// Gets or sets the mock request.
98 /// </summary>
99 /// <value>The mock request.</value>
100 public IMockRequest MockRequest
102 get { return request; }
103 set { request = value; }
106 /// <summary>
107 /// Gets or sets the mock response.
108 /// </summary>
109 /// <value>The mock response.</value>
110 public IMockResponse MockResponse
112 get { return response; }
113 set { response = value; }
116 /// <summary>
117 /// Gets the underlying context of the API being used.
118 /// </summary>
119 /// <value></value>
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)
126 return null;
130 /// <summary>
131 /// Access the session objects.
132 /// </summary>
133 /// <value></value>
134 public virtual IDictionary Session
136 get { return session; }
137 set { session = value; }
140 /// <summary>
141 /// Gets the request object.
142 /// </summary>
143 /// <value></value>
144 public virtual IRequest Request
146 get { return request; }
149 /// <summary>
150 /// Gets the response object.
151 /// </summary>
152 /// <value></value>
153 public virtual IResponse Response
155 get { return response; }
158 /// <summary>
159 /// Gets the trace object.
160 /// </summary>
161 /// <value></value>
162 public virtual ITrace Trace
164 get { return trace; }
165 set { trace = value; }
168 /// <summary>
169 /// Access a dictionary of volative items.
170 /// </summary>
171 /// <value></value>
172 public virtual Flash Flash
174 get { return flash; }
175 set { flash = value; }
178 /// <summary>
179 /// Gets or sets the current user.
180 /// </summary>
181 /// <value></value>
182 public IPrincipal CurrentUser
184 get { return currentUser; }
185 set { currentUser = value; }
188 /// <summary>
189 /// Gets the last exception raised during
190 /// the execution of an action.
191 /// </summary>
192 /// <value></value>
193 public Exception LastException
195 get { return lastException; }
196 set { lastException = value; }
199 /// <summary>
200 /// Returns the application path.
201 /// </summary>
202 /// <value></value>
203 public virtual string ApplicationPath
205 get { return urlInfo.AppVirtualDir ?? "/"; }
208 /// <summary>
209 /// Returns the physical application path.
210 /// </summary>
211 /// <value></value>
212 public string ApplicationPhysicalPath
214 get { return AppDomain.CurrentDomain.BaseDirectory; }
217 /// <summary>
218 /// Returns the <see cref="UrlInfo"/> of the the current request.
219 /// </summary>
220 /// <value></value>
221 public virtual UrlInfo UrlInfo
223 get { return urlInfo; }
224 set { urlInfo = value; }
227 /// <summary>
228 /// Returns an <see cref="IServerUtility"/>.
229 /// </summary>
230 /// <value></value>
231 public virtual IServerUtility Server
233 get { return serverUtility; }
234 set { serverUtility = value; }
237 /// <summary>
238 /// Returns the Items collection from the current HttpContext.
239 /// </summary>
240 /// <value></value>
241 public virtual IDictionary Items
243 get { return contextItems; }
246 /// <summary>
247 /// Gets or sets the current controller.
248 /// </summary>
249 /// <value>The current controller.</value>
250 public virtual IController CurrentController
252 get { return currentController; }
253 set { currentController = value; }
256 /// <summary>
257 /// Gets or sets the current controller context.
258 /// </summary>
259 /// <value>The current controller context.</value>
260 public IControllerContext CurrentControllerContext
262 get { return currentControllerContext; }
263 set { currentControllerContext = value; }
266 /// <summary>
267 /// Gets a reference to the MonoRail services.
268 /// </summary>
269 /// <value>The services.</value>
270 public IMonoRailServices Services
272 get { return services; }
273 set { services = value; }
276 /// <summary>
277 /// Gets the rendered email templates.
278 /// </summary>
279 /// <value>The rendered email templates.</value>
280 public virtual List<RenderedEmailTemplate> RenderedEmailTemplates
282 get { return renderedEmailTemplates; }
285 /// <summary>
286 /// Gets the messages sent.
287 /// </summary>
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);
304 /// <summary>
305 /// Represents an email template for unit test purposes
306 /// </summary>
307 public class RenderedEmailTemplate
309 private readonly string name;
310 private readonly IDictionary parameters;
312 /// <summary>
313 /// Initializes a new instance of the <see cref="RenderedEmailTemplate"/> class.
314 /// </summary>
315 /// <param name="name">The name.</param>
316 /// <param name="parameters">The parameters.</param>
317 public RenderedEmailTemplate(string name, IDictionary parameters)
319 this.name = name;
320 this.parameters = new Hashtable(parameters);
323 /// <summary>
324 /// Gets the name.
325 /// </summary>
326 /// <value>The name.</value>
327 public string Name
329 get { return name; }
332 /// <summary>
333 /// Gets the parameters.
334 /// </summary>
335 /// <value>The parameters.</value>
336 public IDictionary Parameters
338 get { return parameters; }