Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Providers / DefaultControllerDescriptorProviderTestCase.cs
blob5d4774b7c1d7d24651f9946a00f990a7b3624d17
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.Providers
17 using System;
18 using System.Reflection;
19 using Castle.MonoRail.Framework.Providers;
20 using Descriptors;
21 using NUnit.Framework;
22 using Rhino.Mocks;
24 [TestFixture]
25 public class DefaultControllerDescriptorProviderTestCase
27 private MockRepository mockRepository = new MockRepository();
28 private DefaultControllerDescriptorProvider provider;
29 private IHelperDescriptorProvider helperDescProviderMock;
30 private IFilterDescriptorProvider filterDescProviderMock;
31 private ILayoutDescriptorProvider layoutDescProviderMock;
32 private IRescueDescriptorProvider rescueDescProviderMock;
33 private IResourceDescriptorProvider resourceProviderMock;
34 private ITransformFilterDescriptorProvider transformDescProviderMock;
36 [Test]
37 public void CollectsSkipRescueForAction()
39 BuildDescriptor();
41 Type controllerType = typeof(SkipRescueController);
43 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
44 Assert.IsNotNull(metaDesc);
45 MethodInfo actionMethod = controllerType.GetMethod("Action1");
46 ActionMetaDescriptor actionMetaDesc = metaDesc.GetAction(actionMethod);
47 Assert.IsNotNull(actionMetaDesc);
48 Assert.IsNotNull(actionMetaDesc.SkipRescue);
51 [Test]
52 public void CollectsDefaultActionForController()
54 BuildDescriptor();
56 Type controllerType = typeof(DefActionController);
58 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
59 Assert.IsNotNull(metaDesc);
60 Assert.IsNotNull(metaDesc.DefaultAction);
61 Assert.AreEqual("action", metaDesc.DefaultAction.DefaultAction);
64 [Test]
65 public void CollectsDynamicActionsForController()
67 BuildDescriptor();
69 Type controllerType = typeof(DynActionController);
71 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
72 Assert.IsNotNull(metaDesc);
73 Assert.AreEqual(1, metaDesc.ActionProviders.Count);
74 Assert.AreEqual(typeof(DummyDynActionProvider), metaDesc.ActionProviders[0]);
77 [Test]
78 public void CollectsScaffoldingForController()
80 BuildDescriptor();
82 Type controllerType = typeof(ScaffoldController);
84 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
85 Assert.IsNotNull(metaDesc);
86 Assert.AreEqual(1, metaDesc.Scaffoldings.Count);
87 Assert.AreEqual(typeof(DummyScaffoldEntity), metaDesc.Scaffoldings[0].Model);
90 [Test]
91 public void CollectsSkipFilterForAction()
93 BuildDescriptor();
95 Type controllerType = typeof(SkipFilterController);
97 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
98 Assert.IsNotNull(metaDesc);
99 MethodInfo actionMethod = controllerType.GetMethod("Action1");
100 ActionMetaDescriptor actionMetaDesc = metaDesc.GetAction(actionMethod);
101 Assert.IsNotNull(actionMetaDesc);
102 Assert.IsNotNull(actionMetaDesc.SkipFilters);
105 [Test]
106 public void CollectsAccessibleThroughForAction()
108 BuildDescriptor();
110 Type controllerType = typeof(AccThrController);
112 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
113 Assert.IsNotNull(metaDesc);
114 MethodInfo actionMethod = controllerType.GetMethod("Action1");
115 ActionMetaDescriptor actionMetaDesc = metaDesc.GetAction(actionMethod);
116 Assert.IsNotNull(actionMetaDesc);
117 Assert.IsNotNull(actionMetaDesc.AccessibleThrough);
120 [Test]
121 public void DescriptorIsCreatedForControllerAndAction()
123 helperDescProviderMock = mockRepository.CreateMock<IHelperDescriptorProvider>();
124 filterDescProviderMock = mockRepository.CreateMock<IFilterDescriptorProvider>();
125 layoutDescProviderMock = mockRepository.CreateMock<ILayoutDescriptorProvider>();
126 rescueDescProviderMock = mockRepository.CreateMock<IRescueDescriptorProvider>();
127 resourceProviderMock = mockRepository.CreateMock<IResourceDescriptorProvider>();
128 transformDescProviderMock = mockRepository.CreateMock<ITransformFilterDescriptorProvider>();
130 provider = new DefaultControllerDescriptorProvider(helperDescProviderMock,
131 filterDescProviderMock,
132 layoutDescProviderMock,
133 rescueDescProviderMock,
134 resourceProviderMock,
135 transformDescProviderMock);
137 Type controllerType = typeof(SingleActionController);
138 MethodInfo actionMethod = controllerType.GetMethod("Action1");
140 using(mockRepository.Record())
142 // Controller level
143 Expect.Call(helperDescProviderMock.CollectHelpers(controllerType)).Return(new HelperDescriptor[0]);
144 Expect.Call(resourceProviderMock.CollectResources(controllerType)).Return(new ResourceDescriptor[0]);
145 Expect.Call(filterDescProviderMock.CollectFilters(controllerType)).Return(new FilterDescriptor[0]);
146 Expect.Call(layoutDescProviderMock.CollectLayout(controllerType)).Return(null);
147 Expect.Call(rescueDescProviderMock.CollectRescues(controllerType)).Return(new RescueDescriptor[0]);
149 // Action level
150 Expect.Call(resourceProviderMock.CollectResources(actionMethod)).Return(new ResourceDescriptor[0]);
151 Expect.Call(rescueDescProviderMock.CollectRescues(actionMethod)).Return(new RescueDescriptor[0]);
152 Expect.Call(layoutDescProviderMock.CollectLayout(actionMethod)).Return(null);
153 Expect.Call(transformDescProviderMock.CollectFilters(actionMethod)).Return(new TransformFilterDescriptor[0]);
156 using(mockRepository.Playback())
158 ControllerMetaDescriptor metaDesc = provider.BuildDescriptor(controllerType);
159 Assert.IsNotNull(metaDesc);
160 ActionMetaDescriptor actionMetaDesc = metaDesc.GetAction(actionMethod);
161 Assert.IsNotNull(actionMetaDesc);
162 Assert.IsNull(actionMetaDesc.AccessibleThrough);
166 #region Controllers
168 public class SingleActionController : Controller
170 public void Action1()
175 public class AccThrController : Controller
177 [AccessibleThrough(Verb.Post)]
178 public void Action1()
183 public class SkipRescueController : Controller
185 [SkipRescue]
186 public void Action1()
191 public class SkipFilterController : Controller
193 [SkipFilter]
194 public void Action1()
199 [DynamicActionProvider(typeof(DummyDynActionProvider))]
200 public class DynActionController : Controller
204 public class DummyDynActionProvider : IDynamicActionProvider
206 public void IncludeActions(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
211 [Scaffolding(typeof(DummyScaffoldEntity))]
212 public class ScaffoldController : Controller
217 public class DummyScaffoldEntity
221 [DefaultAction("action")]
222 public class DefActionController : Controller
226 #endregion
228 private void BuildDescriptor()
230 provider = new DefaultControllerDescriptorProvider(new DefaultHelperDescriptorProvider(),
231 new DefaultFilterDescriptorProvider(), new DefaultLayoutDescriptorProvider(),
232 new DefaultRescueDescriptorProvider(), new DefaultResourceDescriptorProvider(),
233 new DefaultTransformFilterDescriptorProvider());