Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Filters / ControllerLevelFiltersTestCase.cs
blob5e2c92cbbd2b45f8ca22c956be623a64e9923600
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.Tests.Filters
17 using NUnit.Framework;
18 using Rhino.Mocks;
19 using Test;
21 [TestFixture]
22 public class ControllerLevelFiltersTestCase
24 private MockRepository mockRepository = new MockRepository();
25 private MockEngineContext engineContext;
26 private ViewEngineManagerStub viewEngStub;
27 private MockServices services;
28 private IFilterFactory filterFactoryMock;
30 [SetUp]
31 public void Init()
33 MockRequest request = new MockRequest();
34 MockResponse response = new MockResponse();
35 services = new MockServices();
36 viewEngStub = new ViewEngineManagerStub();
37 services.ViewEngineManager = viewEngStub;
38 filterFactoryMock = mockRepository.CreateMock<IFilterFactory>();
39 services.FilterFactory = filterFactoryMock;
40 engineContext = new MockEngineContext(request, response, services, null);
43 [Test]
44 public void Filter_BeforeActionReturningFalsePreventsActionProcessment()
46 ControllerWithSingleBeforeActionFilter controller = new ControllerWithSingleBeforeActionFilter();
48 IControllerContext context = services.ControllerContextFactory.
49 Create("", "home", "index",
50 services.ControllerDescriptorProvider.BuildDescriptor(controller));
52 IFilter filterMock = mockRepository.CreateMock<IFilter>();
54 using(mockRepository.Record())
56 Expect.Call(filterFactoryMock.Create(typeof(DummyFilter))).Return(filterMock);
58 Expect.Call(filterMock.Perform(ExecuteWhen.BeforeAction, engineContext, controller, context)).Return(false);
60 filterFactoryMock.Release(filterMock);
61 LastCall.Repeat.Once();
64 using(mockRepository.Playback())
66 controller.Process(engineContext, context);
67 controller.Dispose();
69 Assert.IsNull(viewEngStub.TemplateRendered);
70 Assert.IsFalse(controller.indexActionExecuted);
74 [Test]
75 public void Filter_BeforeActionReturningTrueAllowsProcessToContinue()
77 ControllerWithSingleBeforeActionFilter controller = new ControllerWithSingleBeforeActionFilter();
79 IControllerContext context = services.ControllerContextFactory.
80 Create("", "home", "index",
81 services.ControllerDescriptorProvider.BuildDescriptor(controller));
83 IFilter filterMock = mockRepository.CreateMock<IFilter>();
85 using(mockRepository.Record())
87 Expect.Call(filterFactoryMock.Create(typeof(DummyFilter))).Return(filterMock);
89 Expect.Call(filterMock.Perform(ExecuteWhen.BeforeAction, engineContext, controller, context)).Return(true);
91 filterFactoryMock.Release(filterMock);
92 LastCall.Repeat.Once();
95 using(mockRepository.Playback())
97 controller.Process(engineContext, context);
98 controller.Dispose();
100 Assert.AreEqual("home\\index", viewEngStub.TemplateRendered);
101 Assert.IsTrue(controller.indexActionExecuted);
105 [Test]
106 public void Filter_AfterActionIsRun()
108 ControllerWithAfterActionFilter controller = new ControllerWithAfterActionFilter();
110 IControllerContext context = services.ControllerContextFactory.
111 Create("", "home", "index",
112 services.ControllerDescriptorProvider.BuildDescriptor(controller));
114 IFilter filterMock = mockRepository.CreateMock<IFilter>();
116 using(mockRepository.Record())
118 Expect.Call(filterFactoryMock.Create(typeof(DummyFilter))).Return(filterMock);
120 Expect.Call(filterMock.Perform(ExecuteWhen.AfterAction, engineContext, controller, context)).Return(true);
122 filterFactoryMock.Release(filterMock);
123 LastCall.Repeat.Once();
126 using(mockRepository.Playback())
128 controller.Process(engineContext, context);
129 controller.Dispose();
131 Assert.AreEqual("home\\index", viewEngStub.TemplateRendered);
132 Assert.IsTrue(controller.indexActionExecuted);
136 [Test]
137 public void Filter_AfterActionReturningFalsePreventsRendering()
139 ControllerWithAfterActionFilter controller = new ControllerWithAfterActionFilter();
141 IControllerContext context = services.ControllerContextFactory.
142 Create("", "home", "index",
143 services.ControllerDescriptorProvider.BuildDescriptor(controller));
145 IFilter filterMock = mockRepository.CreateMock<IFilter>();
147 using(mockRepository.Record())
149 Expect.Call(filterFactoryMock.Create(typeof(DummyFilter))).Return(filterMock);
151 Expect.Call(filterMock.Perform(ExecuteWhen.AfterAction, engineContext, controller, context)).Return(false);
153 filterFactoryMock.Release(filterMock);
154 LastCall.Repeat.Once();
157 using(mockRepository.Playback())
159 controller.Process(engineContext, context);
160 controller.Dispose();
162 Assert.IsNull(viewEngStub.TemplateRendered);
163 Assert.IsTrue(controller.indexActionExecuted);
167 [Test]
168 public void Filter_AfterRenderingIsRun()
170 ControllerWithAfterRenderingFilter controller = new ControllerWithAfterRenderingFilter();
172 IControllerContext context = services.ControllerContextFactory.
173 Create("", "home", "index",
174 services.ControllerDescriptorProvider.BuildDescriptor(controller));
176 IFilter filterMock = mockRepository.CreateMock<IFilter>();
178 using(mockRepository.Record())
180 Expect.Call(filterFactoryMock.Create(typeof(DummyFilter))).Return(filterMock);
182 Expect.Call(filterMock.Perform(ExecuteWhen.AfterRendering, engineContext, controller, context)).Return(true);
184 filterFactoryMock.Release(filterMock);
185 LastCall.Repeat.Once();
188 using(mockRepository.Playback())
190 controller.Process(engineContext, context);
191 controller.Dispose();
193 Assert.AreEqual("home\\index", viewEngStub.TemplateRendered);
194 Assert.IsTrue(controller.indexActionExecuted);
198 [Test]
199 public void Filter_SkipFilterAttributeSkipsTheFilter()
201 ControllerWithSkipFilter controller = new ControllerWithSkipFilter();
203 IControllerContext context = services.ControllerContextFactory.
204 Create("", "home", "index",
205 services.ControllerDescriptorProvider.BuildDescriptor(controller));
207 IFilter filterMock = mockRepository.CreateMock<IFilter>();
209 using(mockRepository.Record())
213 using(mockRepository.Playback())
215 controller.Process(engineContext, context);
216 controller.Dispose();
218 Assert.AreEqual("home\\index", viewEngStub.TemplateRendered);
219 Assert.IsTrue(controller.indexActionExecuted);
223 #region Controllers
225 [Filter(ExecuteWhen.BeforeAction, typeof(DummyFilter))]
226 private class ControllerWithSingleBeforeActionFilter : Controller
228 public bool indexActionExecuted;
230 public void Index()
232 indexActionExecuted = true;
236 [Filter(ExecuteWhen.BeforeAction, typeof(DummyFilter))]
237 private class ControllerWithSkipFilter : Controller
239 public bool indexActionExecuted;
241 [SkipFilter]
242 public void Index()
244 indexActionExecuted = true;
248 [Filter(ExecuteWhen.AfterAction, typeof(DummyFilter))]
249 private class ControllerWithAfterActionFilter : Controller
251 public bool indexActionExecuted;
253 public void Index()
255 indexActionExecuted = true;
259 [Filter(ExecuteWhen.AfterRendering, typeof(DummyFilter))]
260 private class ControllerWithAfterRenderingFilter : Controller
262 public bool indexActionExecuted;
264 public void Index()
266 indexActionExecuted = true;
270 #endregion
272 private class DummyFilter : IFilter
274 public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller,
275 IControllerContext controllerContext)
277 return false;