1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Reflection
;
19 using Castle
.MonoRail
.Framework
.Providers
;
21 using NUnit
.Framework
;
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
;
37 public void CollectsSkipRescueForAction()
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
);
52 public void CollectsDefaultActionForController()
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
);
65 public void CollectsDynamicActionsForController()
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]);
78 public void CollectsScaffoldingForController()
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
);
91 public void CollectsSkipFilterForAction()
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
);
106 public void CollectsAccessibleThroughForAction()
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
);
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())
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]);
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
);
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
186 public void Action1()
191 public class SkipFilterController
: Controller
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
228 private void BuildDescriptor()
230 provider
= new DefaultControllerDescriptorProvider(new DefaultHelperDescriptorProvider(),
231 new DefaultFilterDescriptorProvider(), new DefaultLayoutDescriptorProvider(),
232 new DefaultRescueDescriptorProvider(), new DefaultResourceDescriptorProvider(),
233 new DefaultTransformFilterDescriptorProvider());