Esempio n. 1
0
        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();
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        public void Execute_ThrowsIfParametersIsNull()
        {
            // Arrange
            ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod);

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { ad.BeginExecute(new ControllerContext(), null, null, null); }, "parameters");
        }
Esempio n. 5
0
        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");
        }
Esempio n. 6
0
        public void IsDefined()
        {
            // Arrange
            ReflectedAsyncActionDescriptor ad = GetActionDescriptor(_asyncMethod, _completedMethod);

            // Act
            bool isDefined = ad.IsDefined(typeof(AuthorizeAttribute), true /* inherit */);

            // Assert
            Assert.IsTrue(isDefined);
        }
Esempio n. 7
0
        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.");
        }
Esempio n. 8
0
        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());
        }
Esempio n. 9
0
        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);
        }
Esempio n. 11
0
        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.");
        }
Esempio n. 12
0
        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);
        }
Esempio n. 15
0
        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.");
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        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.");
        }
Esempio n. 19
0
        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);
        }
Esempio n. 20
0
 public DescribedMvcAsyncActionInfo(ReflectedAsyncActionDescriptor actionDescr, ControllerInfo controller)
     : base(actionDescr, controller)
 {
     this.actionDescr = actionDescr;
 }