コード例 #1
0
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock <IModelBinder>().Object;

            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback.");
        }
コード例 #2
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(
                typeof(MyFirstConvertibleType),
                false /* fallbackToDefault */
                );
            IModelBinder binder2 = binders.GetBinder(
                typeof(MySecondConvertibleType),
                false /* fallbackToDefault */
                );
            IModelBinder binder3 = binders.GetBinder(
                typeof(object),
                false /* fallbackToDefault */
                );

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType <MySecondBinder>(binder2);
            Assert.Null(binder3);
        }
コード例 #3
0
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock <IModelBinder>().Object;

            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType <MySecondBinder>(binder2);
            Assert.Same(defaultBinder, binder3);
        }
コード例 #4
0
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { binders.GetBinder(null); }, "modelType");
        }
コード例 #5
0
        public void ShouldResolveBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet));

            Assert.AreSame(FakeBinder, binder);
        }
コード例 #6
0
        public void GetBinderThrowsIfModelTypeContainsMultipleAttributes()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                delegate { binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */); },
                "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute.");
        }
コード例 #7
0
        public void ShouldResolveBindersViaInheritance()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(IListSource), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet)); // DataSet implements IListSource

            Assert.AreSame(FakeBinder, binder);
        }
コード例 #8
0
        public void ShouldFallBackToDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(int));

            Assert.IsInstanceOf <DefaultModelBinder>(binder);
        }
コード例 #9
0
        public void ShouldRespectExplicitDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());

            dictionary.DefaultModelBinder = FakeBinder;

            var binder = dictionary.GetBinder(typeof(int));

            Assert.AreSame(FakeBinder, binder);
        }
コード例 #10
0
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                binders.GetBinder(null);
            }, "modelType");
        }
コード例 #11
0
        public T GetModel <T>()
        {
            var binder = _binders.GetBinder(typeof(T));
            var res    = binder.BindModel(_context, new ModelBindingContext
            {
                FallbackToEmptyPrefix = true,
                ModelMetadata         = new ModelMetadata(new EmptyModelMetadataProvider(), null, () => default(T), typeof(T), ""),
                ValueProvider         = _values
            });

            return((res is T) ? (T)res : default(T));
        }
コード例 #12
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo() {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary() {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default.");
        }
コード例 #13
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder          registeredFirstBinder = new Mock <IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.IsNull(binder3, "Third binder should have returned null since asked not to use default.");
        }
コード例 #14
0
        public void GetBinderConsultsProviders() {

            //Arrange
            Type modelType = typeof(string);
            IModelBinder expectedBinderFromProvider = new Mock<IModelBinder>().Object;

            Mock<IModelBinderProvider> locatedProvider = new Mock<IModelBinderProvider>();
            locatedProvider.Setup(p => p.GetBinder(modelType))
                .Returns(expectedBinderFromProvider);

            Mock<IModelBinderProvider> secondProvider = new Mock<IModelBinderProvider>();

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object });
            ModelBinderDictionary binders = new ModelBinderDictionary(providers);

            //Act
            IModelBinder returnedBinder = binders.GetBinder(modelType);

            //Assert
            Assert.AreSame(expectedBinderFromProvider, returnedBinder);
        }
コード例 #15
0
        public void GetBinderConsultsProviders()
        {
            //Arrange
            Type         modelType = typeof(string);
            IModelBinder expectedBinderFromProvider = new Mock <IModelBinder>().Object;

            Mock <IModelBinderProvider> locatedProvider = new Mock <IModelBinderProvider>();

            locatedProvider.Setup(p => p.GetBinder(modelType))
            .Returns(expectedBinderFromProvider);

            Mock <IModelBinderProvider> secondProvider = new Mock <IModelBinderProvider>();

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection(new IModelBinderProvider[] { locatedProvider.Object, secondProvider.Object });
            ModelBinderDictionary         binders   = new ModelBinderDictionary(providers);

            //Act
            IModelBinder returnedBinder = binders.GetBinder(modelType);

            //Assert
            Assert.AreSame(expectedBinderFromProvider, returnedBinder);
        }
コード例 #16
0
        public void RegisterBinderPopulatesModelBinderDictionary()
        {
            // Arrange
            ModelBinderDictionary binderDict = new ModelBinderDictionary();

            // Act
            FileCollectionModelBinder.RegisterBinder(binderDict);

            // Assert
            Assert.AreEqual(6, binderDict.Count);
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(HttpPostedFileBase[])), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IEnumerable <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(ICollection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(IList <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(Collection <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
            Assert.IsInstanceOfType(binderDict.GetBinder(typeof(List <HttpPostedFileBase>)), typeof(FileCollectionModelBinder));
        }
コード例 #17
0
        public void GetBinderThrowsIfModelTypeContainsMultipleAttributes() {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                    binders.GetBinder(typeof(ConvertibleTypeWithSeveralBinders), true /* fallbackToDefault */);
                },
                "The type 'System.Web.Mvc.Test.ModelBinderDictionaryTest+ConvertibleTypeWithSeveralBinders' contains multiple attributes that inherit from CustomModelBinderAttribute.");
        }
コード例 #18
0
 public DefaultEncoding()
 {
     this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true);
 }
コード例 #19
0
        public void GetBinderDoesNotReturnDefaultBinderIfAskedNotTo()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. <null>

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType), false /* fallbackToDefault */);
            IModelBinder binder3 = binders.GetBinder(typeof(object), false /* fallbackToDefault */);

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType<MySecondBinder>(binder2);
            Assert.Null(binder3);
        }
コード例 #20
0
 private MvcEncoderDecoder()
 {
     this.binder = binderDictionary.GetBinder(typeof(T), fallbackToDefault: true);
 }
コード例 #21
0
        public void GetBinderThrowsIfModelTypeIsNull() {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    binders.GetBinder(null);
                }, "modelType");
        }
コード例 #22
0
        public void GetBinderThrowsIfModelTypeIsNull()
        {
            // Arrange
            ModelBinderDictionary binders = new ModelBinderDictionary();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { binders.GetBinder(null); }, "modelType");
        }
コード例 #23
0
        public void GetBinderResolvesBindersWithCorrectPrecedence() {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary() {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.AreSame(registeredFirstBinder, binder1, "First binder should have been matched in the registered binders table.");
            Assert.IsInstanceOfType(binder2, typeof(MySecondBinder), "Second binder should have been matched on the type.");
            Assert.AreSame(defaultBinder, binder3, "Third binder should have been the fallback.");
        }
コード例 #24
0
        public void GetBinderResolvesBindersWithCorrectPrecedence()
        {
            // Proper order of precedence:
            // 1. Binder registered in the global table
            // 2. Binder attribute defined on the type
            // 3. Default binder

            // Arrange
            IModelBinder registeredFirstBinder = new Mock<IModelBinder>().Object;
            ModelBinderDictionary binders = new ModelBinderDictionary()
            {
                { typeof(MyFirstConvertibleType), registeredFirstBinder }
            };

            IModelBinder defaultBinder = new Mock<IModelBinder>().Object;
            binders.DefaultBinder = defaultBinder;

            // Act
            IModelBinder binder1 = binders.GetBinder(typeof(MyFirstConvertibleType));
            IModelBinder binder2 = binders.GetBinder(typeof(MySecondConvertibleType));
            IModelBinder binder3 = binders.GetBinder(typeof(object));

            // Assert
            Assert.Same(registeredFirstBinder, binder1);
            Assert.IsType<MySecondBinder>(binder2);
            Assert.Same(defaultBinder, binder3);
        }