PageRenderTime 3158ms CodeModel.GetById 27ms RepoModel.GetById 4ms app.codeStats 0ms

/MVC/test/MvcFuturesTest/Mvc/Test/AsyncActionMethodSelectorTest.cs

#
C# | 368 lines | 243 code | 81 blank | 44 comment | 0 complexity | 0eb6a955a566aa849898412144bf5dc2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. namespace Microsoft.Web.Mvc.Test {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Web.Mvc;
  7. using System.Web.TestUtil;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Microsoft.Web.Mvc;
  10. using Moq;
  11. [TestClass]
  12. public class AsyncActionMethodSelectorTest {
  13. [TestMethod]
  14. public void AliasedMethodsProperty() {
  15. // Arrange
  16. Type controllerType = typeof(MethodLocatorController);
  17. // Act
  18. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  19. // Assert
  20. Assert.AreEqual(3, selector.AliasedMethods.Length);
  21. List<MethodInfo> sortedAliasedMethods = selector.AliasedMethods.OrderBy(methodInfo => methodInfo.Name).ToList();
  22. Assert.AreEqual("Bar", sortedAliasedMethods[0].Name);
  23. Assert.AreEqual("BeginAsyncResultPatternWithRename2", sortedAliasedMethods[1].Name);
  24. Assert.AreEqual("FooRenamed", sortedAliasedMethods[2].Name);
  25. }
  26. [TestMethod]
  27. public void ControllerTypeProperty() {
  28. // Arrange
  29. Type controllerType = typeof(MethodLocatorController);
  30. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  31. // Act & Assert
  32. Assert.AreSame(controllerType, selector.ControllerType);
  33. }
  34. [TestMethod]
  35. public void FindActionMethodDoesNotMatchBeginMethod() {
  36. // Arrange
  37. Type controllerType = typeof(MethodLocatorController);
  38. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  39. // Act
  40. ActionDescriptorCreator creator = selector.FindActionMethod(null, "BeginAsyncResultPattern");
  41. // Assert
  42. Assert.IsNull(creator, "No method should have matched.");
  43. }
  44. [TestMethod]
  45. public void FindActionMethodDoesNotMatchCompletionMethod() {
  46. // Arrange
  47. Type controllerType = typeof(MethodLocatorController);
  48. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  49. // Act
  50. ActionDescriptorCreator creator = selector.FindActionMethod(null, "EventPatternCompleted");
  51. // Assert
  52. Assert.IsNull(creator, "No method should have matched.");
  53. }
  54. [TestMethod]
  55. public void FindActionMethodDoesNotMatchEndMethod() {
  56. // Arrange
  57. Type controllerType = typeof(MethodLocatorController);
  58. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  59. // Act
  60. ActionDescriptorCreator creator = selector.FindActionMethod(null, "EndAsyncResultPattern");
  61. // Assert
  62. Assert.IsNull(creator, "No method should have matched.");
  63. }
  64. [TestMethod]
  65. public void FindActionMethodReturnsMatchingMethodIfOneMethodMatches() {
  66. // Arrange
  67. Type controllerType = typeof(SelectionAttributeController);
  68. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  69. // Act
  70. ActionDescriptorCreator creator = selector.FindActionMethod(null, "OneMatch");
  71. ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);
  72. // Assert
  73. Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedActionDescriptor));
  74. ReflectedActionDescriptor castActionDescriptor = (ReflectedActionDescriptor)actionDescriptor;
  75. Assert.AreEqual("OneMatch", castActionDescriptor.MethodInfo.Name, "Method named OneMatch() should have matched.");
  76. Assert.AreEqual(typeof(string), castActionDescriptor.MethodInfo.GetParameters()[0].ParameterType, "Method overload OneMatch(string) should have matched.");
  77. }
  78. [TestMethod]
  79. public void FindActionMethodReturnsMethodWithActionSelectionAttributeIfMultipleMethodsMatchRequest() {
  80. // DevDiv Bugs 212062: If multiple action methods match a request, we should match only the methods with an
  81. // [ActionMethod] attribute since we assume those methods are more specific.
  82. // Arrange
  83. Type controllerType = typeof(SelectionAttributeController);
  84. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  85. // Act
  86. ActionDescriptorCreator creator = selector.FindActionMethod(null, "ShouldMatchMethodWithSelectionAttribute");
  87. ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);
  88. // Assert
  89. Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedActionDescriptor));
  90. ReflectedActionDescriptor castActionDescriptor = (ReflectedActionDescriptor)actionDescriptor;
  91. Assert.AreEqual("MethodHasSelectionAttribute1", castActionDescriptor.MethodInfo.Name);
  92. }
  93. [TestMethod]
  94. public void FindActionMethodReturnsNullIfNoMethodMatches() {
  95. // Arrange
  96. Type controllerType = typeof(SelectionAttributeController);
  97. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  98. // Act
  99. ActionDescriptorCreator creator = selector.FindActionMethod(null, "ZeroMatch");
  100. // Assert
  101. Assert.IsNull(creator, "No method should have matched.");
  102. }
  103. [TestMethod]
  104. public void FindActionMethodThrowsIfMultipleMethodsMatch() {
  105. // Arrange
  106. Type controllerType = typeof(SelectionAttributeController);
  107. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  108. // Act & veriy
  109. ExceptionHelper.ExpectException<AmbiguousMatchException>(
  110. delegate {
  111. selector.FindActionMethod(null, "TwoMatch");
  112. },
  113. @"The current request for action 'TwoMatch' on controller type 'SelectionAttributeController' is ambiguous between the following action methods:
  114. Void TwoMatch2() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+SelectionAttributeController
  115. Void TwoMatch() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+SelectionAttributeController");
  116. }
  117. [TestMethod]
  118. public void FindActionMethodWithAsyncPatternType() {
  119. // Arrange
  120. Type controllerType = typeof(MethodLocatorController);
  121. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  122. // Act
  123. ActionDescriptorCreator creator = selector.FindActionMethod(null, "AsyncResultPattern");
  124. ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);
  125. // Assert
  126. Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedAsyncPatternActionDescriptor));
  127. ReflectedAsyncPatternActionDescriptor castActionDescriptor = (ReflectedAsyncPatternActionDescriptor)actionDescriptor;
  128. Assert.AreEqual("BeginAsyncResultPattern", castActionDescriptor.BeginMethod.Name);
  129. Assert.AreEqual("EndAsyncResultPattern", castActionDescriptor.EndMethod.Name);
  130. }
  131. [TestMethod]
  132. public void FindActionMethodWithAsyncPatternTypeThrowsIfEndMethodIsAmbiguous() {
  133. // Arrange
  134. Type controllerType = typeof(MethodLocatorController);
  135. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  136. // Act & assert
  137. ExceptionHelper.ExpectException<AmbiguousMatchException>(
  138. delegate {
  139. selector.FindActionMethod(null, "AsyncResultPatternWithConflict");
  140. },
  141. @"Lookup for method 'EndAsyncResultPatternWithRename2' on controller type 'MethodLocatorController' failed because of an ambiguity between the following methods:
  142. Void EndAsyncResultPatternWithRename2(System.IAsyncResult) on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController
  143. Void EndAsyncResultPatternWithRename2() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController");
  144. }
  145. [TestMethod]
  146. public void FindActionMethodWithAsyncPatternTypeThrowsIfEndMethodNotFound() {
  147. // Arrange
  148. Type controllerType = typeof(MethodLocatorController);
  149. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  150. // Act & assert
  151. ExceptionHelper.ExpectInvalidOperationException(
  152. delegate {
  153. selector.FindActionMethod(null, "AsyncResultPatternEndNotFound");
  154. },
  155. @"Could not locate a method named 'EndAsyncResultPatternEndNotFound' on controller type 'Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController'.");
  156. }
  157. [TestMethod]
  158. public void FindActionMethodWithDelegatePatternType() {
  159. // Arrange
  160. Type controllerType = typeof(MethodLocatorController);
  161. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  162. // Act
  163. ActionDescriptorCreator creator = selector.FindActionMethod(null, "DelegatePattern");
  164. ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);
  165. // Assert
  166. Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedDelegatePatternActionDescriptor));
  167. ReflectedDelegatePatternActionDescriptor castActionDescriptor = (ReflectedDelegatePatternActionDescriptor)actionDescriptor;
  168. Assert.AreEqual("DelegatePattern", castActionDescriptor.ActionMethod.Name);
  169. }
  170. [TestMethod]
  171. public void FindActionMethodWithEventPatternType() {
  172. // Arrange
  173. Type controllerType = typeof(MethodLocatorController);
  174. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  175. // Act
  176. ActionDescriptorCreator creator = selector.FindActionMethod(null, "EventPattern");
  177. ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);
  178. // Assert
  179. Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedEventPatternActionDescriptor));
  180. ReflectedEventPatternActionDescriptor castActionDescriptor = (ReflectedEventPatternActionDescriptor)actionDescriptor;
  181. Assert.AreEqual("EventPattern", castActionDescriptor.SetupMethod.Name);
  182. Assert.AreEqual("EventPatternCompleted", castActionDescriptor.CompletionMethod.Name);
  183. }
  184. [TestMethod]
  185. public void NonAliasedMethodsProperty() {
  186. // Arrange
  187. Type controllerType = typeof(MethodLocatorController);
  188. // Act
  189. AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);
  190. // Assert
  191. Assert.AreEqual(5, selector.NonAliasedMethods.Count);
  192. List<MethodInfo> sortedMethods = selector.NonAliasedMethods["foo"].OrderBy(methodInfo => methodInfo.GetParameters().Length).ToList();
  193. Assert.AreEqual("Foo", sortedMethods[0].Name);
  194. Assert.AreEqual(0, sortedMethods[0].GetParameters().Length);
  195. Assert.AreEqual("Foo", sortedMethods[1].Name);
  196. Assert.AreEqual(typeof(string), sortedMethods[1].GetParameters()[0].ParameterType);
  197. Assert.AreEqual(1, selector.NonAliasedMethods["AsyncResultPattern"].Count());
  198. Assert.AreEqual("BeginAsyncResultPattern", selector.NonAliasedMethods["AsyncResultPattern"].First().Name);
  199. Assert.AreEqual(1, selector.NonAliasedMethods["AsyncResultPatternEndNotFound"].Count());
  200. Assert.AreEqual("BeginAsyncResultPatternEndNotFound", selector.NonAliasedMethods["AsyncResultPatternEndNotFound"].First().Name);
  201. Assert.AreEqual(1, selector.NonAliasedMethods["DelegatePattern"].Count());
  202. Assert.AreEqual(1, selector.NonAliasedMethods["EventPattern"].Count());
  203. Assert.AreEqual("EventPattern", selector.NonAliasedMethods["EventPattern"].First().Name);
  204. }
  205. private class MethodLocatorController : Controller {
  206. public void Foo() {
  207. }
  208. public void Foo(string s) {
  209. }
  210. [ActionName("Foo")]
  211. public void FooRenamed() {
  212. }
  213. [ActionName("Bar")]
  214. public void Bar() {
  215. }
  216. [ActionName("PrivateVoid")]
  217. private void PrivateVoid() {
  218. }
  219. protected void ProtectedVoidAction() {
  220. }
  221. public static void StaticMethod() {
  222. }
  223. public IAsyncResult BeginAsyncResultPattern(AsyncCallback callback, object state) {
  224. throw new NotImplementedException();
  225. }
  226. public void EndAsyncResultPattern(IAsyncResult asyncResult) {
  227. }
  228. public IAsyncResult BeginAsyncResultPatternEndNotFound(AsyncCallback callback, object state) {
  229. throw new NotImplementedException();
  230. }
  231. [ActionName("AsyncResultPatternWithConflict")]
  232. public IAsyncResult BeginAsyncResultPatternWithRename2(AsyncCallback callback, object state) {
  233. throw new NotImplementedException();
  234. }
  235. public void EndAsyncResultPatternWithRename2(IAsyncResult asyncResult) {
  236. }
  237. public void EndAsyncResultPatternWithRename2() {
  238. }
  239. public void EventPattern() {
  240. }
  241. public void EventPatternCompleted() {
  242. }
  243. public Action DelegatePattern() {
  244. throw new NotImplementedException();
  245. }
  246. // ensure that methods inheriting from Controller or a base class are not matched
  247. [ActionName("Blah")]
  248. protected override void ExecuteCore() {
  249. throw new NotImplementedException();
  250. }
  251. public string StringProperty {
  252. get;
  253. set;
  254. }
  255. #pragma warning disable 0067
  256. public event EventHandler<EventArgs> SomeEvent;
  257. #pragma warning restore 0067
  258. }
  259. private class SelectionAttributeController : Controller {
  260. [Match(false)]
  261. public void OneMatch() {
  262. }
  263. public void OneMatch(string s) {
  264. }
  265. public void TwoMatch() {
  266. }
  267. [ActionName("TwoMatch")]
  268. public void TwoMatch2() {
  269. }
  270. [Match(true), ActionName("ShouldMatchMethodWithSelectionAttribute")]
  271. public void MethodHasSelectionAttribute1() {
  272. }
  273. [ActionName("ShouldMatchMethodWithSelectionAttribute")]
  274. public void MethodDoesNotHaveSelectionAttribute1() {
  275. }
  276. private class MatchAttribute : ActionMethodSelectorAttribute {
  277. private bool _match;
  278. public MatchAttribute(bool match) {
  279. _match = match;
  280. }
  281. public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
  282. return _match;
  283. }
  284. }
  285. }
  286. }
  287. }