PageRenderTime 68ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/mvc3/test/MvcFuturesTest/Mvc/ModelBinding/Test/GenericModelBinderProviderTest.cs

https://github.com/jeanlyn/ASP.NET-Mvc-3
C# | 244 lines | 172 code | 41 blank | 31 comment | 0 complexity | 627cca74f4f40e5534407025b26928ab MD5 | raw file
  1. namespace Microsoft.Web.Mvc.ModelBinding.Test {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web.Mvc;
  5. using System.Web.TestUtil;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Microsoft.Web.UnitTestUtil;
  8. using Moq;
  9. [TestClass]
  10. public class GenericModelBinderProviderTest {
  11. [TestMethod]
  12. public void Constructor_WithFactory_ThrowsIfModelBinderFactoryIsNull() {
  13. // Act & assert
  14. ExceptionHelper.ExpectArgumentNullException(
  15. delegate {
  16. new GenericModelBinderProvider(typeof(List<>), (Func<Type[], IExtensibleModelBinder>)null);
  17. }, "modelBinderFactory");
  18. }
  19. [TestMethod]
  20. public void Constructor_WithFactory_ThrowsIfModelTypeIsNotOpenGeneric() {
  21. // Act & assert
  22. ExceptionHelper.ExpectArgumentException(
  23. delegate {
  24. new GenericModelBinderProvider(typeof(List<int>), _ => null);
  25. },
  26. @"The type 'System.Collections.Generic.List`1[System.Int32]' is not an open generic type.
  27. Parameter name: modelType");
  28. }
  29. [TestMethod]
  30. public void Constructor_WithFactory_ThrowsIfModelTypeIsNull() {
  31. // Act & assert
  32. ExceptionHelper.ExpectArgumentNullException(
  33. delegate {
  34. new GenericModelBinderProvider(null, _ => null);
  35. }, "modelType");
  36. }
  37. [TestMethod]
  38. public void Constructor_WithInstance_ThrowsIfModelBinderIsNull() {
  39. // Act & assert
  40. ExceptionHelper.ExpectArgumentNullException(
  41. delegate {
  42. new GenericModelBinderProvider(typeof(List<>), (IExtensibleModelBinder)null);
  43. }, "modelBinder");
  44. }
  45. [TestMethod]
  46. public void Constructor_WithInstance_ThrowsIfModelTypeIsNotOpenGeneric() {
  47. // Act & assert
  48. ExceptionHelper.ExpectArgumentException(
  49. delegate {
  50. new GenericModelBinderProvider(typeof(List<int>), new MutableObjectModelBinder());
  51. },
  52. @"The type 'System.Collections.Generic.List`1[System.Int32]' is not an open generic type.
  53. Parameter name: modelType");
  54. }
  55. [TestMethod]
  56. public void Constructor_WithInstance_ThrowsIfModelTypeIsNull() {
  57. // Act & assert
  58. ExceptionHelper.ExpectArgumentNullException(
  59. delegate {
  60. new GenericModelBinderProvider(null, new MutableObjectModelBinder());
  61. }, "modelType");
  62. }
  63. [TestMethod]
  64. public void Constructor_WithType_ThrowsIfModelBinderTypeIsNotModelBinder() {
  65. // Act & assert
  66. ExceptionHelper.ExpectArgumentException(
  67. delegate {
  68. new GenericModelBinderProvider(typeof(List<>), typeof(string));
  69. },
  70. @"The type 'System.String' does not implement the interface 'Microsoft.Web.Mvc.ModelBinding.IExtensibleModelBinder'.
  71. Parameter name: modelBinderType");
  72. }
  73. [TestMethod]
  74. public void Constructor_WithType_ThrowsIfModelBinderTypeIsNull() {
  75. // Act & assert
  76. ExceptionHelper.ExpectArgumentNullException(
  77. delegate {
  78. new GenericModelBinderProvider(typeof(List<>), (Type)null);
  79. }, "modelBinderType");
  80. }
  81. [TestMethod]
  82. public void Constructor_WithType_ThrowsIfModelBinderTypeTypeArgumentMismatch() {
  83. // Act & assert
  84. ExceptionHelper.ExpectArgumentException(
  85. delegate {
  86. new GenericModelBinderProvider(typeof(List<>), typeof(DictionaryModelBinder<,>));
  87. },
  88. @"The open model type 'System.Collections.Generic.List`1[T]' has 1 generic type argument(s), but the open binder type 'Microsoft.Web.Mvc.ModelBinding.DictionaryModelBinder`2[TKey,TValue]' has 2 generic type argument(s). The binder type must not be an open generic type or must have the same number of generic arguments as the open model type.
  89. Parameter name: modelBinderType");
  90. }
  91. [TestMethod]
  92. public void Constructor_WithType_ThrowsIfModelTypeIsNotOpenGeneric() {
  93. // Act & assert
  94. ExceptionHelper.ExpectArgumentException(
  95. delegate {
  96. new GenericModelBinderProvider(typeof(List<int>), typeof(MutableObjectModelBinder));
  97. },
  98. @"The type 'System.Collections.Generic.List`1[System.Int32]' is not an open generic type.
  99. Parameter name: modelType");
  100. }
  101. [TestMethod]
  102. public void Constructor_WithType_ThrowsIfModelTypeIsNull() {
  103. // Act & assert
  104. ExceptionHelper.ExpectArgumentNullException(
  105. delegate {
  106. new GenericModelBinderProvider(null, typeof(MutableObjectModelBinder));
  107. }, "modelType");
  108. }
  109. [TestMethod]
  110. public void GetBinder_TypeDoesNotMatch_ModelTypeIsInterface_ReturnsNull() {
  111. // Arrange
  112. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable<>), typeof(CollectionModelBinder<>)) {
  113. SuppressPrefixCheck = true
  114. };
  115. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(object));
  116. // Act
  117. IExtensibleModelBinder binder = provider.GetBinder(null, bindingContext);
  118. // Assert
  119. Assert.IsNull(binder);
  120. }
  121. [TestMethod]
  122. public void GetBinder_TypeDoesNotMatch_ModelTypeIsNotInterface_ReturnsNull() {
  123. // Arrange
  124. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>)) {
  125. SuppressPrefixCheck = true
  126. };
  127. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(object));
  128. // Act
  129. IExtensibleModelBinder binder = provider.GetBinder(null, bindingContext);
  130. // Assert
  131. Assert.IsNull(binder);
  132. }
  133. [TestMethod]
  134. public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull() {
  135. // Arrange
  136. IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object;
  137. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance);
  138. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));
  139. bindingContext.ValueProvider = new SimpleValueProvider();
  140. // Act
  141. IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext);
  142. // Assert
  143. Assert.IsNull(returnedBinder);
  144. }
  145. [TestMethod]
  146. public void GetBinder_TypeMatches_Success_Factory_ReturnsBinder() {
  147. // Arrange
  148. IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object;
  149. Func<Type[], IExtensibleModelBinder> binderFactory = typeArguments => {
  150. CollectionAssert.AreEqual(new Type[] { typeof(int) }, typeArguments);
  151. return binderInstance;
  152. };
  153. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IList<>), binderFactory) {
  154. SuppressPrefixCheck = true
  155. };
  156. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));
  157. // Act
  158. IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext);
  159. // Assert
  160. Assert.AreSame(binderInstance, returnedBinder);
  161. }
  162. [TestMethod]
  163. public void GetBinder_TypeMatches_Success_Instance_ReturnsBinder() {
  164. // Arrange
  165. IExtensibleModelBinder binderInstance = new Mock<IExtensibleModelBinder>().Object;
  166. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance) {
  167. SuppressPrefixCheck = true
  168. };
  169. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));
  170. // Act
  171. IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext);
  172. // Assert
  173. Assert.AreSame(binderInstance, returnedBinder);
  174. }
  175. [TestMethod]
  176. public void GetBinder_TypeMatches_Success_TypeActivation_ReturnsBinder() {
  177. // Arrange
  178. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>)) {
  179. SuppressPrefixCheck = true
  180. };
  181. ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));
  182. // Act
  183. IExtensibleModelBinder returnedBinder = provider.GetBinder(null, bindingContext);
  184. // Assert
  185. Assert.IsInstanceOfType(returnedBinder, typeof(CollectionModelBinder<int>));
  186. }
  187. [TestMethod]
  188. public void GetBinderThrowsIfBindingContextIsNull() {
  189. // Arrange
  190. GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable<>), typeof(CollectionModelBinder<>));
  191. // Act & assert
  192. ExceptionHelper.ExpectArgumentNullException(
  193. delegate {
  194. provider.GetBinder(null, null);
  195. }, "bindingContext");
  196. }
  197. private static ExtensibleModelBindingContext GetBindingContext(Type modelType) {
  198. return new ExtensibleModelBindingContext() {
  199. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => null, modelType)
  200. };
  201. }
  202. }
  203. }