public ReflectedAsyncControllerDescriptor(Type controllerType) {
            if (controllerType == null) {
                throw new ArgumentNullException("controllerType");
            }

            _controllerType = controllerType;
            _selector = new AsyncActionMethodSelector(_controllerType);
        }
        public void ControllerTypeProperty() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & Assert
            Assert.AreSame(controllerType, selector.ControllerType);
        }
Exemplo n.º 3
0
        public ReflectedAsyncControllerDescriptor(Type controllerType)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }

            _controllerType = controllerType;
            _selector       = new AsyncActionMethodSelector(_controllerType);
        }
        public void FindActionMethodDoesNotMatchEndMethod() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "EndAsyncResultPattern");

            // Assert
            Assert.IsNull(creator, "No method should have matched.");
        }
        public void AliasedMethodsProperty() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Assert
            Assert.AreEqual(3, selector.AliasedMethods.Length);

            List<MethodInfo> sortedAliasedMethods = selector.AliasedMethods.OrderBy(methodInfo => methodInfo.Name).ToList();
            Assert.AreEqual("Bar", sortedAliasedMethods[0].Name);
            Assert.AreEqual("BeginAsyncResultPatternWithRename2", sortedAliasedMethods[1].Name);
            Assert.AreEqual("FooRenamed", sortedAliasedMethods[2].Name);
        }
        public void FindActionMethodReturnsMatchingMethodIfOneMethodMatches() {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "OneMatch");
            ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedActionDescriptor));
            ReflectedActionDescriptor castActionDescriptor = (ReflectedActionDescriptor)actionDescriptor;

            Assert.AreEqual("OneMatch", castActionDescriptor.MethodInfo.Name, "Method named OneMatch() should have matched.");
            Assert.AreEqual(typeof(string), castActionDescriptor.MethodInfo.GetParameters()[0].ParameterType, "Method overload OneMatch(string) should have matched.");
        }
        public void FindActionMethodReturnsMethodWithActionSelectionAttributeIfMultipleMethodsMatchRequest() {
            // DevDiv Bugs 212062: If multiple action methods match a request, we should match only the methods with an
            // [ActionMethod] attribute since we assume those methods are more specific.

            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "ShouldMatchMethodWithSelectionAttribute");
            ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedActionDescriptor));
            ReflectedActionDescriptor castActionDescriptor = (ReflectedActionDescriptor)actionDescriptor;

            Assert.AreEqual("MethodHasSelectionAttribute1", castActionDescriptor.MethodInfo.Name);
        }
        public void NonAliasedMethodsProperty() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);

            // Act
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Assert
            Assert.AreEqual(5, selector.NonAliasedMethods.Count);

            List<MethodInfo> sortedMethods = selector.NonAliasedMethods["foo"].OrderBy(methodInfo => methodInfo.GetParameters().Length).ToList();
            Assert.AreEqual("Foo", sortedMethods[0].Name);
            Assert.AreEqual(0, sortedMethods[0].GetParameters().Length);
            Assert.AreEqual("Foo", sortedMethods[1].Name);
            Assert.AreEqual(typeof(string), sortedMethods[1].GetParameters()[0].ParameterType);

            Assert.AreEqual(1, selector.NonAliasedMethods["AsyncResultPattern"].Count());
            Assert.AreEqual("BeginAsyncResultPattern", selector.NonAliasedMethods["AsyncResultPattern"].First().Name);
            Assert.AreEqual(1, selector.NonAliasedMethods["AsyncResultPatternEndNotFound"].Count());
            Assert.AreEqual("BeginAsyncResultPatternEndNotFound", selector.NonAliasedMethods["AsyncResultPatternEndNotFound"].First().Name);
            Assert.AreEqual(1, selector.NonAliasedMethods["DelegatePattern"].Count());
            Assert.AreEqual(1, selector.NonAliasedMethods["EventPattern"].Count());
            Assert.AreEqual("EventPattern", selector.NonAliasedMethods["EventPattern"].First().Name);
        }
        public void FindActionMethodWithEventPatternType() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "EventPattern");
            ActionDescriptor actionDescriptor = creator("someName", new Mock<ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedEventPatternActionDescriptor));
            ReflectedEventPatternActionDescriptor castActionDescriptor = (ReflectedEventPatternActionDescriptor)actionDescriptor;

            Assert.AreEqual("EventPattern", castActionDescriptor.SetupMethod.Name);
            Assert.AreEqual("EventPatternCompleted", castActionDescriptor.CompletionMethod.Name);
        }
        public void FindActionMethodWithAsyncPatternTypeThrowsIfEndMethodNotFound() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                    selector.FindActionMethod(null, "AsyncResultPatternEndNotFound");
                },
                @"Could not locate a method named 'EndAsyncResultPatternEndNotFound' on controller type 'Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController'.");
        }
        public void FindActionMethodWithAsyncPatternTypeThrowsIfEndMethodIsAmbiguous() {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & assert
            ExceptionHelper.ExpectException<AmbiguousMatchException>(
                delegate {
                    selector.FindActionMethod(null, "AsyncResultPatternWithConflict");
                },
                @"Lookup for method 'EndAsyncResultPatternWithRename2' on controller type 'MethodLocatorController' failed because of an ambiguity between the following methods:
Void EndAsyncResultPatternWithRename2(System.IAsyncResult) on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController
Void EndAsyncResultPatternWithRename2() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+MethodLocatorController");
        }
        public void FindActionMethodThrowsIfMultipleMethodsMatch() {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & veriy
            ExceptionHelper.ExpectException<AmbiguousMatchException>(
                delegate {
                    selector.FindActionMethod(null, "TwoMatch");
                },
                @"The current request for action 'TwoMatch' on controller type 'SelectionAttributeController' is ambiguous between the following action methods:
Void TwoMatch2() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+SelectionAttributeController
Void TwoMatch() on type Microsoft.Web.Mvc.Test.AsyncActionMethodSelectorTest+SelectionAttributeController");
        }
        public void FindActionMethodReturnsNullIfNoMethodMatches() {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "ZeroMatch");

            // Assert
            Assert.IsNull(creator, "No method should have matched.");
        }
 public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName)
 {
     var selector = new AsyncActionMethodSelector(controllerContext.Controller.GetType());
     var creator = selector.FindActionMethod(controllerContext, actionName);
     return creator == null ? null : creator(actionName, this);
 }