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
;
35 private IReturnBinderDescriptorProvider returnTypeDescProviderMock
;
38 public void CollectsSkipRescueForAction()
42 Type controllerType
= typeof(SkipRescueController
);
44 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
45 Assert
.IsNotNull(metaDesc
);
46 MethodInfo actionMethod
= controllerType
.GetMethod("Action1");
47 ActionMetaDescriptor actionMetaDesc
= metaDesc
.GetAction(actionMethod
);
48 Assert
.IsNotNull(actionMetaDesc
);
49 Assert
.IsNotNull(actionMetaDesc
.SkipRescue
);
53 public void CollectsDefaultActionForController()
57 Type controllerType
= typeof(DefActionController
);
59 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
60 Assert
.IsNotNull(metaDesc
);
61 Assert
.IsNotNull(metaDesc
.DefaultAction
);
62 Assert
.AreEqual("action", metaDesc
.DefaultAction
.DefaultAction
);
66 public void CollectsDynamicActionsForController()
70 Type controllerType
= typeof(DynActionController
);
72 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
73 Assert
.IsNotNull(metaDesc
);
74 Assert
.AreEqual(1, metaDesc
.ActionProviders
.Count
);
75 Assert
.AreEqual(typeof(DummyDynActionProvider
), metaDesc
.ActionProviders
[0]);
79 public void CollectsScaffoldingForController()
83 Type controllerType
= typeof(ScaffoldController
);
85 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
86 Assert
.IsNotNull(metaDesc
);
87 Assert
.AreEqual(1, metaDesc
.Scaffoldings
.Count
);
88 Assert
.AreEqual(typeof(DummyScaffoldEntity
), metaDesc
.Scaffoldings
[0].Model
);
92 public void CollectsSkipFilterForAction()
96 Type controllerType
= typeof(SkipFilterController
);
98 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
99 Assert
.IsNotNull(metaDesc
);
100 MethodInfo actionMethod
= controllerType
.GetMethod("Action1");
101 ActionMetaDescriptor actionMetaDesc
= metaDesc
.GetAction(actionMethod
);
102 Assert
.IsNotNull(actionMetaDesc
);
103 Assert
.IsNotNull(actionMetaDesc
.SkipFilters
);
107 public void CollectsAccessibleThroughForAction()
111 Type controllerType
= typeof(AccThrController
);
113 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
114 Assert
.IsNotNull(metaDesc
);
115 MethodInfo actionMethod
= controllerType
.GetMethod("Action1");
116 ActionMetaDescriptor actionMetaDesc
= metaDesc
.GetAction(actionMethod
);
117 Assert
.IsNotNull(actionMetaDesc
);
118 Assert
.IsNotNull(actionMetaDesc
.AccessibleThrough
);
122 public void DescriptorIsCreatedForControllerAndAction()
124 helperDescProviderMock
= mockRepository
.CreateMock
<IHelperDescriptorProvider
>();
125 filterDescProviderMock
= mockRepository
.CreateMock
<IFilterDescriptorProvider
>();
126 layoutDescProviderMock
= mockRepository
.CreateMock
<ILayoutDescriptorProvider
>();
127 rescueDescProviderMock
= mockRepository
.CreateMock
<IRescueDescriptorProvider
>();
128 resourceProviderMock
= mockRepository
.CreateMock
<IResourceDescriptorProvider
>();
129 transformDescProviderMock
= mockRepository
.CreateMock
<ITransformFilterDescriptorProvider
>();
130 returnTypeDescProviderMock
= mockRepository
.CreateMock
<IReturnBinderDescriptorProvider
>();
132 provider
= new DefaultControllerDescriptorProvider(helperDescProviderMock
,
133 filterDescProviderMock
,
134 layoutDescProviderMock
,
135 rescueDescProviderMock
,
136 resourceProviderMock
,
137 transformDescProviderMock
, returnTypeDescProviderMock
);
139 Type controllerType
= typeof(SingleActionController
);
140 MethodInfo actionMethod
= controllerType
.GetMethod("Action1");
142 using(mockRepository
.Record())
145 Expect
.Call(helperDescProviderMock
.CollectHelpers(controllerType
)).Return(new HelperDescriptor
[0]);
146 Expect
.Call(resourceProviderMock
.CollectResources(controllerType
)).Return(new ResourceDescriptor
[0]);
147 Expect
.Call(filterDescProviderMock
.CollectFilters(controllerType
)).Return(new FilterDescriptor
[0]);
148 Expect
.Call(layoutDescProviderMock
.CollectLayout(controllerType
)).Return(null);
149 Expect
.Call(rescueDescProviderMock
.CollectRescues(controllerType
)).Return(new RescueDescriptor
[0]);
152 Expect
.Call(resourceProviderMock
.CollectResources(actionMethod
)).Return(new ResourceDescriptor
[0]);
153 Expect
.Call(rescueDescProviderMock
.CollectRescues(actionMethod
)).Return(new RescueDescriptor
[0]);
154 Expect
.Call(layoutDescProviderMock
.CollectLayout(actionMethod
)).Return(null);
155 Expect
.Call(transformDescProviderMock
.CollectFilters(actionMethod
)).Return(new TransformFilterDescriptor
[0]);
156 Expect
.Call(returnTypeDescProviderMock
.Collect(actionMethod
)).Return(null);
159 using(mockRepository
.Playback())
161 ControllerMetaDescriptor metaDesc
= provider
.BuildDescriptor(controllerType
);
162 Assert
.IsNotNull(metaDesc
);
163 ActionMetaDescriptor actionMetaDesc
= metaDesc
.GetAction(actionMethod
);
164 Assert
.IsNotNull(actionMetaDesc
);
165 Assert
.IsNull(actionMetaDesc
.AccessibleThrough
);
171 public class SingleActionController
: Controller
173 public void Action1()
178 public class AccThrController
: Controller
180 [AccessibleThrough(Verb
.Post
)]
181 public void Action1()
186 public class SkipRescueController
: Controller
189 public void Action1()
194 public class SkipFilterController
: Controller
197 public void Action1()
202 [DynamicActionProvider(typeof(DummyDynActionProvider
))]
203 public class DynActionController
: Controller
207 public class DummyDynActionProvider
: IDynamicActionProvider
209 public void IncludeActions(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
214 [Scaffolding(typeof(DummyScaffoldEntity
))]
215 public class ScaffoldController
: Controller
220 public class DummyScaffoldEntity
224 [DefaultAction("action")]
225 public class DefActionController
: Controller
231 private void BuildDescriptor()
233 provider
= new DefaultControllerDescriptorProvider(new DefaultHelperDescriptorProvider(),
234 new DefaultFilterDescriptorProvider(), new DefaultLayoutDescriptorProvider(),
235 new DefaultRescueDescriptorProvider(), new DefaultResourceDescriptorProvider(),
236 new DefaultTransformFilterDescriptorProvider(), new DefaultReturnBinderDescriptorProvider());