PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/jeanlyn/ASP.NET-Mvc-3
C# | 225 lines | 172 code | 31 blank | 22 comment | 0 complexity | 960619917f22d9c7e9db178a1562a091 MD5 | raw file
  1. namespace Microsoft.Web.Mvc.ModelBinding.Test {
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Web.Mvc;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Microsoft.Web.UnitTestUtil;
  9. using Moq;
  10. using ModelBinderProviderCollection = Microsoft.Web.Mvc.ModelBinding.ModelBinderProviderCollection;
  11. [TestClass]
  12. public class CollectionModelBinderTest {
  13. [TestMethod]
  14. public void BindComplexCollectionFromIndexes_FiniteIndexes() {
  15. // Arrange
  16. ControllerContext controllerContext = new ControllerContext();
  17. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  18. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  19. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  20. ModelName = "someName",
  21. ModelBinderProviders = new ModelBinderProviderCollection(),
  22. ValueProvider = new SimpleValueProvider() {
  23. { "someName[foo]", "42" },
  24. { "someName[baz]", "200" }
  25. }
  26. };
  27. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  28. mockIntBinder
  29. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  30. .Returns(
  31. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  32. mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
  33. return true;
  34. });
  35. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
  36. // Act
  37. List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, new string[] { "foo", "bar", "baz" });
  38. // Assert
  39. CollectionAssert.AreEqual(new int[] { 42, 0, 200 }, boundCollection, "Missing element should have been converted to default(Int32).");
  40. CollectionAssert.AreEquivalent(new string[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
  41. }
  42. [TestMethod]
  43. public void BindComplexCollectionFromIndexes_InfiniteIndexes() {
  44. // Arrange
  45. ControllerContext controllerContext = new ControllerContext();
  46. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  47. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  48. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  49. ModelName = "someName",
  50. ModelBinderProviders = new ModelBinderProviderCollection(),
  51. ValueProvider = new SimpleValueProvider() {
  52. { "someName[0]", "42" },
  53. { "someName[1]", "100" },
  54. { "someName[3]", "400" }
  55. }
  56. };
  57. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  58. mockIntBinder
  59. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  60. .Returns(
  61. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  62. mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
  63. return true;
  64. });
  65. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
  66. // Act
  67. List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, null /* indexNames */);
  68. // Assert
  69. CollectionAssert.AreEqual(new int[] { 42, 100 }, boundCollection, "Binding should have halted at missing element.");
  70. CollectionAssert.AreEquivalent(new string[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
  71. }
  72. [TestMethod]
  73. public void BindModel_ComplexCollection() {
  74. // Arrange
  75. ControllerContext controllerContext = new ControllerContext();
  76. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  77. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  78. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  79. ModelName = "someName",
  80. ModelBinderProviders = new ModelBinderProviderCollection(),
  81. ValueProvider = new SimpleValueProvider() {
  82. { "someName.index", new string[] { "foo", "bar", "baz" } },
  83. { "someName[foo]", "42" },
  84. { "someName[bar]", "100" },
  85. { "someName[baz]", "200" }
  86. }
  87. };
  88. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  89. mockIntBinder
  90. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  91. .Returns(
  92. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  93. mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
  94. return true;
  95. });
  96. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
  97. CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
  98. // Act
  99. bool retVal = modelBinder.BindModel(controllerContext, bindingContext);
  100. // Assert
  101. CollectionAssert.AreEqual(new int[] { 42, 100, 200 }, bindingContext.Model as ICollection);
  102. }
  103. [TestMethod]
  104. public void BindModel_SimpleCollection() {
  105. // Arrange
  106. ControllerContext controllerContext = new ControllerContext();
  107. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  108. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  109. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  110. ModelName = "someName",
  111. ModelBinderProviders = new ModelBinderProviderCollection(),
  112. ValueProvider = new SimpleValueProvider() {
  113. { "someName", new string[] { "42", "100", "200" } }
  114. }
  115. };
  116. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  117. mockIntBinder
  118. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  119. .Returns(
  120. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  121. mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
  122. return true;
  123. });
  124. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
  125. CollectionModelBinder<int> modelBinder = new CollectionModelBinder<int>();
  126. // Act
  127. bool retVal = modelBinder.BindModel(controllerContext, bindingContext);
  128. // Assert
  129. Assert.IsTrue(retVal);
  130. CollectionAssert.AreEqual(new int[] { 42, 100, 200 }, bindingContext.Model as ICollection);
  131. }
  132. [TestMethod]
  133. public void BindSimpleCollection_RawValueIsEmptyCollection_ReturnsEmptyList() {
  134. // Act
  135. List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, new object[0], null);
  136. // Assert
  137. Assert.IsNotNull(boundCollection);
  138. Assert.AreEqual(0, boundCollection.Count);
  139. }
  140. [TestMethod]
  141. public void BindSimpleCollection_RawValueIsNull_ReturnsNull() {
  142. // Act
  143. List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(null, null, null, null);
  144. // Assert
  145. Assert.IsNull(boundCollection);
  146. }
  147. [TestMethod]
  148. public void BindSimpleCollection_SubBinderDoesNotExist() {
  149. // Arrange
  150. ControllerContext controllerContext = new ControllerContext();
  151. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  152. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  153. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  154. ModelName = "someName",
  155. ModelBinderProviders = new ModelBinderProviderCollection(),
  156. ValueProvider = new SimpleValueProvider()
  157. };
  158. // Act
  159. List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(controllerContext, bindingContext, new int[1], culture);
  160. // Assert
  161. CollectionAssert.AreEqual(new int[] { 0 }, boundCollection, "default(int) was not correctly added to collection.");
  162. Assert.AreEqual(0, bindingContext.ValidationNode.ChildNodes.Count, "No child validation node should have been added.");
  163. }
  164. [TestMethod]
  165. public void BindSimpleCollection_SubBindingSucceeds() {
  166. // Arrange
  167. ControllerContext controllerContext = new ControllerContext();
  168. CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
  169. ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
  170. ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
  171. ModelName = "someName",
  172. ModelBinderProviders = new ModelBinderProviderCollection(),
  173. ValueProvider = new SimpleValueProvider()
  174. };
  175. ModelValidationNode childValidationNode = null;
  176. Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
  177. mockIntBinder
  178. .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
  179. .Returns(
  180. delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
  181. Assert.AreEqual("someName", mbc.ModelName);
  182. childValidationNode = mbc.ValidationNode;
  183. mbc.Model = 42;
  184. return true;
  185. });
  186. bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
  187. // Act
  188. List<int> boundCollection = CollectionModelBinder<int>.BindSimpleCollection(controllerContext, bindingContext, new int[1], culture);
  189. // Assert
  190. CollectionAssert.AreEqual(new int[] { 42 }, boundCollection);
  191. CollectionAssert.AreEqual(new object[] { childValidationNode }, bindingContext.ValidationNode.ChildNodes.ToArray(), "Child validation node was not added.");
  192. }
  193. }
  194. }