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
.Actions
17 using System
.Reflection
;
18 using NUnit
.Framework
;
19 using Castle
.MonoRail
.Framework
.Descriptors
;
20 using Castle
.MonoRail
.Framework
.Test
;
23 public class ActionMethodExecutorTestCase
26 public void ShouldReflectAbsenceOfConfigurationOnMetaDescriptor()
28 BaseController controller
= new BaseController();
29 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
31 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
33 Assert
.IsFalse(executor
.ShouldSkipAllFilters
);
34 Assert
.IsFalse(executor
.ShouldSkipRescues
);
35 Assert
.IsFalse(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
36 Assert
.IsNull(executor
.LayoutOverride
);
37 Assert
.AreEqual(0, executor
.Resources
.Length
);
41 public void ShouldReturnTrueToSkipRescueReflectingMeta()
43 BaseController controller
= new BaseController();
44 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
45 actionMeta
.SkipRescue
= new SkipRescueAttribute();
47 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
49 Assert
.IsTrue(executor
.ShouldSkipRescues
);
50 Assert
.IsFalse(executor
.ShouldSkipAllFilters
);
51 Assert
.IsFalse(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
52 Assert
.IsNull(executor
.LayoutOverride
);
53 Assert
.AreEqual(0, executor
.Resources
.Length
);
57 public void ShouldReturnTrueToSkipAllFiltersReflectingMeta()
59 BaseController controller
= new BaseController();
60 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
61 actionMeta
.SkipFilters
.Add(new SkipFilterAttribute());
63 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
65 Assert
.IsTrue(executor
.ShouldSkipAllFilters
);
66 Assert
.IsFalse(executor
.ShouldSkipRescues
);
67 Assert
.IsFalse(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
68 Assert
.IsNull(executor
.LayoutOverride
);
69 Assert
.AreEqual(0, executor
.Resources
.Length
);
73 public void ShouldReturnTrueToSkipSpecifiedFiltersReflectingMeta()
75 BaseController controller
= new BaseController();
76 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
77 actionMeta
.SkipFilters
.Add(new SkipFilterAttribute(typeof(DummyFilter
)));
79 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
81 Assert
.IsTrue(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
82 Assert
.IsFalse(executor
.ShouldSkipRescues
);
83 Assert
.IsFalse(executor
.ShouldSkipAllFilters
);
84 Assert
.IsNull(executor
.LayoutOverride
);
85 Assert
.AreEqual(0, executor
.Resources
.Length
);
89 public void ShouldReturnLayoutReflectingMeta()
91 BaseController controller
= new BaseController();
92 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
93 actionMeta
.Layout
= new LayoutDescriptor("layoutname");
95 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
97 Assert
.IsFalse(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
98 Assert
.IsFalse(executor
.ShouldSkipRescues
);
99 Assert
.IsFalse(executor
.ShouldSkipAllFilters
);
100 Assert
.AreEqual("layoutname", executor
.LayoutOverride
[0]);
101 Assert
.AreEqual(0, executor
.Resources
.Length
);
105 public void ShouldReturnResourcesReflectingMeta()
107 BaseController controller
= new BaseController();
108 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
109 actionMeta
.Resources
= new ResourceDescriptor
[] { new ResourceDescriptor(typeof(BaseController), "name", "resname", "cult", "assm") }
;
111 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
113 Assert
.IsFalse(executor
.ShouldSkipFilter(typeof(DummyFilter
)));
114 Assert
.IsFalse(executor
.ShouldSkipRescues
);
115 Assert
.IsFalse(executor
.ShouldSkipAllFilters
);
116 Assert
.IsNull(executor
.LayoutOverride
);
117 Assert
.AreEqual(1, executor
.Resources
.Length
);
121 public void ExecutesActionAndReturnValue()
123 BaseController controller
= new BaseController();
124 ActionMetaDescriptor actionMeta
= new ActionMetaDescriptor();
126 ActionMethodExecutor executor
= new ActionMethodExecutor(GetActionMethod(controller
), actionMeta
);
128 MockRequest req
= new MockRequest();
129 MockResponse res
= new MockResponse();
130 MockServices services
= new MockServices();
131 IEngineContext engineContext
= new MockEngineContext(req
, res
, services
, new UrlInfo("area", "controller", "action"));
132 object retVal
= executor
.Execute(engineContext
, controller
, new ControllerContext());
134 Assert
.IsTrue(controller
.WasExecuted
);
135 Assert
.AreEqual(1, retVal
);
138 private MethodInfo
GetActionMethod(object controller
)
140 return controller
.GetType().GetMethod("Action1");
143 public class BaseController
: Controller
145 private bool wasExecuted
;
147 public object Action1()
153 public bool WasExecuted
155 get { return wasExecuted; }
159 public class DummyFilter
: Filter