Added general purpose Pair class
[castle.git] / MonoRail / Castle.MonoRail.TestSupport / BaseControllerTest.cs
blob7ca9300fee85fb047b1c1b947e26b7c6f7c4d363
1 // Copyright 2004-2007 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.TestSupport
17 using System;
18 using System.IO;
19 using Castle.Components.Common.EmailSender;
20 using Castle.MonoRail.Framework.Test;
21 using Framework;
23 public delegate void ContextInitializer(MockRailsEngineContext context);
25 /// <summary>
26 /// Pendent
27 /// </summary>
28 public abstract class BaseControllerTest
30 private readonly string domain;
31 private readonly string domainPrefix;
32 private readonly int port;
33 protected string virtualDir = "";
34 private MockRailsEngineContext context;
35 private IRequest request;
36 private IMockResponse response;
37 private ITrace trace;
39 protected BaseControllerTest() : this("app.com", "www", 80)
43 protected BaseControllerTest(string domain, string domainPrefix, int port)
45 this.domain = domain;
46 this.domainPrefix = domainPrefix;
47 this.port = port;
50 protected IRailsEngineContext Context
52 get { return context; }
55 public IRequest Request
57 get { return request; }
60 public IMockResponse Response
62 get { return response; }
65 public ITrace Trace
67 get { return trace; }
70 protected void PrepareController(Controller controller)
72 PrepareController(controller, null);
75 protected void PrepareController(Controller controller, ContextInitializer contextInitializer)
77 PrepareController(controller, "", "Controller", "Action");
80 protected void PrepareController(Controller controller, string controllerName, string actionName)
82 PrepareController(controller, "", controllerName, actionName);
85 protected void PrepareController(Controller controller, string areaName, string controllerName, string actionName)
87 if (controller == null)
89 throw new ArgumentNullException("controller", "'controller' cannot be null");
91 if (areaName == null)
93 throw new ArgumentNullException("areaName");
95 if (controllerName == null)
97 throw new ArgumentNullException("controllerName");
99 if (actionName == null)
101 throw new ArgumentNullException("actionName");
104 BuildRailsContext(areaName, controllerName, actionName);
105 controller.InitializeFieldsFromServiceProvider(context);
106 controller.InitializeControllerState(areaName, controllerName, actionName);
107 ControllerLifecycleExecutor executor = new ControllerLifecycleExecutor(controller, context);
108 executor.Service(context);
109 executor.InitializeController(controller.AreaName, controller.Name, controller.Action);
112 protected void BuildRailsContext(string areaName, string controllerName, string actionName)
114 UrlInfo info = BuildUrlInfo(areaName, controllerName, actionName);
115 request = BuildRequest();
116 response = BuildResponse();
117 trace = BuildTrace();
118 context = BuildRailsEngineContext(request, response, trace, info);
121 protected virtual IRequest BuildRequest()
123 return new MockRequest();
126 protected virtual IMockResponse BuildResponse()
128 return new MockResponse();
131 protected virtual ITrace BuildTrace()
133 return new MockTrace();
136 protected virtual MockRailsEngineContext BuildRailsEngineContext(IRequest request, IResponse response, ITrace trace,
137 UrlInfo urlInfo)
139 return new MockRailsEngineContext(request, response, trace, urlInfo);
142 protected virtual UrlInfo BuildUrlInfo(string areaName, string controllerName, string actionName)
144 return new UrlInfo(domain, domainPrefix, virtualDir, "http", port,
145 Path.Combine(Path.Combine(areaName, controllerName), actionName),
146 areaName, controllerName, actionName, "rails");
149 protected bool HasRenderedEmailTemplateNamed(string templateName)
151 MockRailsEngineContext.RenderedEmailTemplate template =
152 context.RenderedEmailTemplates.Find(
153 delegate(MockRailsEngineContext.RenderedEmailTemplate emailTemplate)
155 return templateName.Equals(emailTemplate.Name, StringComparison.OrdinalIgnoreCase);
158 return template != null;
161 protected Message[] MessagesSent
163 get { return context.MessagesSent.ToArray(); }
166 protected MockRailsEngineContext.RenderedEmailTemplate[] RenderedEmailTemplates
168 get { return context.RenderedEmailTemplates.ToArray(); }