protected static string CreateAmbiguousMatchList(IEnumerable <DirectRouteCandidate> candidates) { StringBuilder exceptionMessageBuilder = new StringBuilder(); foreach (DirectRouteCandidate candidate in candidates) { MethodInfo method = null; ReflectedActionDescriptor reflectedActionDescriptor = candidate.ActionDescriptor as ReflectedActionDescriptor; if (reflectedActionDescriptor == null) { ReflectedAsyncActionDescriptor reflectedAsyncActionDescriptor = candidate.ActionDescriptor as ReflectedAsyncActionDescriptor; if (reflectedAsyncActionDescriptor != null) { method = reflectedAsyncActionDescriptor.AsyncMethodInfo; } } else { method = reflectedActionDescriptor.MethodInfo; } string controllerAction = method == null ? candidate.ActionDescriptor.ActionName : Convert.ToString(method, CultureInfo.CurrentCulture); string controllerType = method.DeclaringType.FullName; exceptionMessageBuilder.AppendLine(); exceptionMessageBuilder.AppendFormat(CultureInfo.CurrentCulture, MvcResources.ActionMethodSelector_AmbiguousMatchType, controllerAction, controllerType); } return(exceptionMessageBuilder.ToString()); }
public void GetSelectors() { // Arrange ControllerContext controllerContext = new Mock <ControllerContext>().Object; Mock <MethodInfo> mockMethod = new Mock <MethodInfo>(); Mock <ActionMethodSelectorAttribute> mockAttr = new Mock <ActionMethodSelectorAttribute>(); mockAttr .Setup(attr => attr.IsValidForRequest(controllerContext, mockMethod.Object)) .Returns(true) .Verifiable(); mockMethod .Setup(m => m.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true)) .Returns(new ActionMethodSelectorAttribute[] { mockAttr.Object }); ReflectedAsyncActionDescriptor ad = GetActionDescriptor( mockMethod.Object, _completedMethod ); // Act ICollection <ActionSelector> selectors = ad.GetSelectors(); bool executedSuccessfully = selectors.All(s => s(controllerContext)); // Assert Assert.Single(selectors); Assert.True(executedSuccessfully); mockAttr.Verify(); }
public void Execute() { // Arrange Mock <ControllerContext> mockControllerContext = new Mock <ControllerContext>(); mockControllerContext.Expect(c => c.Controller).Returns(new ExecuteController()); ControllerContext controllerContext = mockControllerContext.Object; Dictionary <string, object> parameters = new Dictionary <string, object>() { { "id1", 42 } }; ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); SignalContainer <object> resultContainer = new SignalContainer <object>(); AsyncCallback callback = ar => { object o = ad.EndExecute(ar); resultContainer.Signal(o); }; // Act ad.BeginExecute(controllerContext, parameters, callback, null); object retVal = resultContainer.Wait(); // Assert Assert.AreEqual("Hello world: 42", retVal); }
public void Execute_ThrowsIfParametersIsNull() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act & assert Assert.ThrowsArgumentNull( delegate { ad.BeginExecute(new ControllerContext(), null, null, null); }, "parameters"); }
public void Execute_ThrowsIfControllerContextIsNull() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act & assert Assert.ThrowsArgumentNull( delegate { ad.BeginExecute(null, new Dictionary <string, object>(), null, null); }, "controllerContext"); }
public void IsDefined() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act bool isDefined = ad.IsDefined(typeof(AuthorizeAttribute), true /* inherit */); // Assert Assert.IsTrue(isDefined); }
public void GetFilters_ConsidersOnlyAsyncMethod() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act FilterInfo filterInfo = ad.GetFilters(); // Assert Assert.AreEqual(0, filterInfo.ActionFilters.Count, "Filters on Completed() method should be excluded from consideration."); }
public void GetCustomAttributes() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act object[] attributes = ad.GetCustomAttributes(true /* inherit */); // Assert Assert.AreEqual(1, attributes.Length); Assert.AreEqual(typeof(AuthorizeAttribute), attributes[0].GetType()); }
public void GetCustomAttributes_FilterByType() { // Shouldn't match attributes on the Completed() method, only the Async() method // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act object[] attributes = ad.GetCustomAttributes(typeof(OutputCacheAttribute), true /* inherit */); // Assert Assert.AreEqual(0, attributes.Length); }
public void GetCustomAttributes() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act object[] attributes = ad.GetCustomAttributes(true /* inherit */); // Assert object attribute = Assert.Single(attributes); Assert.IsType <AuthorizeAttribute>(attribute); }
public void Execute_ThrowsIfControllerIsNotAsyncManagerContainer() { // Arrange ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); ControllerContext controllerContext = new ControllerContext() { Controller = new RegularSyncController() }; // Act & assert Assert.Throws <InvalidOperationException>( delegate { ad.BeginExecute(controllerContext, new Dictionary <string, object>(), null, null); }, @"The controller of type 'System.Web.Mvc.Async.Test.ReflectedAsyncActionDescriptorTest+RegularSyncController' must subclass AsyncController or implement the IAsyncManagerContainer interface."); }
public void Constructor_SetsProperties() { // Arrange string actionName = "SomeAction"; ControllerDescriptor cd = new Mock <ControllerDescriptor>().Object; // Act ReflectedAsyncActionDescriptor ad = new ReflectedAsyncActionDescriptor(_asyncMethod, _completedMethod, actionName, cd); // Assert Assert.AreEqual(_asyncMethod, ad.AsyncMethodInfo); Assert.AreEqual(_completedMethod, ad.CompletedMethodInfo); Assert.AreEqual(actionName, ad.ActionName); Assert.AreEqual(cd, ad.ControllerDescriptor); }
public void Constructor_SetsProperties() { // Arrange string actionName = "SomeAction"; ControllerDescriptor cd = new Mock<ControllerDescriptor>().Object; // Act ReflectedAsyncActionDescriptor ad = new ReflectedAsyncActionDescriptor(_asyncMethod, _completedMethod, actionName, cd); // Assert Assert.Equal(_asyncMethod, ad.AsyncMethodInfo); Assert.Equal(_completedMethod, ad.CompletedMethodInfo); Assert.Equal(actionName, ad.ActionName); Assert.Equal(cd, ad.ControllerDescriptor); }
public void FindActionMethod_Asynchronous() { // Arrange Type controllerType = typeof(MethodLocatorController); AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType); // Act ActionDescriptorCreator creator = selector.FindAction(null, "EventPattern"); ActionDescriptor actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object); // Assert Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedAsyncActionDescriptor)); ReflectedAsyncActionDescriptor castActionDescriptor = (ReflectedAsyncActionDescriptor)actionDescriptor; Assert.AreEqual("EventPatternAsync", castActionDescriptor.AsyncMethodInfo.Name); Assert.AreEqual("EventPatternCompleted", castActionDescriptor.CompletedMethodInfo.Name); }
public void FixtureSetUp() { var controllerContext = new Mock <ControllerContext>(); controllerContext.Setup(mock => mock.Controller).Returns(new TestController()); _controllerContext = controllerContext.Object; _controllerDescriptor = new Mock <ControllerDescriptor>().Object; _baseMethodInfo = TestController.GetAction1MethodInfo <TestController>(); _derivedMethodInfo = TestController.GetAction1MethodInfo <TestControllerA>(); _mostDerivedMethodInfo = TestController.GetAction1MethodInfo <TestControllerB>(); _actionName = _baseMethodInfo.Name; _reflectedActionDescriptor = new ReflectedActionDescriptor(_baseMethodInfo, _actionName, _controllerDescriptor); _reflectedAsyncActionDescriptor = new ReflectedAsyncActionDescriptor(_baseMethodInfo, _baseMethodInfo, _actionName, _controllerDescriptor); _taskAsyncActionDescriptor = new TaskAsyncActionDescriptor(_baseMethodInfo, _actionName, _controllerDescriptor); _derivedActionDescriptor = new ReflectedActionDescriptor(_derivedMethodInfo, _actionName, _controllerDescriptor); _mostDerivedActionDescriptor = new ReflectedActionDescriptor(_mostDerivedMethodInfo, _actionName, _controllerDescriptor); }
public void FindActionReturnsActionDescriptorIfFound() { // Arrange Type controllerType = typeof(MyController); MethodInfo asyncMethodInfo = controllerType.GetMethod("FooAsync"); MethodInfo completedMethodInfo = controllerType.GetMethod("FooCompleted"); ReflectedAsyncControllerDescriptor cd = new ReflectedAsyncControllerDescriptor(controllerType); // Act ActionDescriptor ad = cd.FindAction(new Mock <ControllerContext>().Object, "NewName"); // Assert Assert.AreEqual("NewName", ad.ActionName); Assert.IsInstanceOfType(ad, typeof(ReflectedAsyncActionDescriptor)); ReflectedAsyncActionDescriptor castAd = (ReflectedAsyncActionDescriptor)ad; Assert.AreSame(asyncMethodInfo, castAd.AsyncMethodInfo, "AsyncMethodInfo pointed to wrong method."); Assert.AreSame(completedMethodInfo, castAd.CompletedMethodInfo, "CompletedMethodInfo pointed to wrong method."); Assert.AreSame(cd, ad.ControllerDescriptor, "ControllerDescriptor did not point back to correct descriptor."); }
public void GetParameters() { // Arrange ParameterInfo pInfo = _asyncMethod.GetParameters()[0]; ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act ParameterDescriptor[] pDescsFirstCall = ad.GetParameters(); ParameterDescriptor[] pDescsSecondCall = ad.GetParameters(); // Assert Assert.NotSame(pDescsFirstCall, pDescsSecondCall); Assert.Equal(pDescsFirstCall, pDescsSecondCall); Assert.Single(pDescsFirstCall); ReflectedParameterDescriptor pDesc = pDescsFirstCall[0] as ReflectedParameterDescriptor; Assert.NotNull(pDesc); Assert.Same(ad, pDesc.ActionDescriptor); Assert.Same(pInfo, pDesc.ParameterInfo); }
public void GetParameters() { // Arrange ParameterInfo pInfo = _asyncMethod.GetParameters()[0]; ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod); // Act ParameterDescriptor[] pDescsFirstCall = ad.GetParameters(); ParameterDescriptor[] pDescsSecondCall = ad.GetParameters(); // Assert Assert.AreNotSame(pDescsFirstCall, pDescsSecondCall, "GetParameters() should return a new array on each invocation."); CollectionAssert.AreEqual(pDescsFirstCall, pDescsSecondCall, "Array elements were not equal."); Assert.AreEqual(1, pDescsFirstCall.Length); ReflectedParameterDescriptor pDesc = pDescsFirstCall[0] as ReflectedParameterDescriptor; Assert.IsNotNull(pDesc, "Parameter 0 should have been of type ReflectedParameterDescriptor."); Assert.AreSame(ad, pDesc.ActionDescriptor, "Parameter 0 Action did not match."); Assert.AreSame(pInfo, pDesc.ParameterInfo, "Parameter 0 ParameterInfo did not match."); }
public void FixtureSetUp() { _baseControllerContext = new ControllerContext { Controller = new TestController() }; _derivedControllerContext = new ControllerContext { Controller = new TestControllerA() }; _mostDerivedControllerContext = new ControllerContext { Controller = new TestControllerB() }; _baseMethodInfo = TestController.GetAction1MethodInfo <TestController>(); _derivedMethodInfo = TestController.GetAction1MethodInfo <TestControllerA>(); _mostDerivedMethodInfo = TestController.GetAction1MethodInfo <TestControllerB>(); _actionName = _baseMethodInfo.Name; _controllerDescriptor = new Mock <ControllerDescriptor>().Object; _reflectedActionDescriptor = new ReflectedActionDescriptor(_baseMethodInfo, _actionName, _controllerDescriptor); _reflectedAsyncActionDescriptor = new ReflectedAsyncActionDescriptor(_baseMethodInfo, _baseMethodInfo, _actionName, _controllerDescriptor); _taskAsyncActionDescriptor = new TaskAsyncActionDescriptor(_baseMethodInfo, _actionName, _controllerDescriptor); _derivedActionDescriptor = new ReflectedActionDescriptor(_derivedMethodInfo, _actionName, _controllerDescriptor); _mostDerivedActionDescriptor = new ReflectedActionDescriptor(_mostDerivedMethodInfo, _actionName, _controllerDescriptor); }
public DescribedMvcAsyncActionInfo(ReflectedAsyncActionDescriptor actionDescr, ControllerInfo controller) : base(actionDescr, controller) { this.actionDescr = actionDescr; }