Files
data
debian
docs
eglib
external
Lucene.Net
Newtonsoft.Json
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
Ajax
Async
ExpressionUtil
Html
Properties
Razor
Test
AcceptVerbsAttributeTest.cs
ActionDescriptorTest.cs
ActionExecutedContextTest.cs
ActionExecutingContextTest.cs
ActionFilterAttributeTest.cs
ActionMethodDispatcherCacheTest.cs
ActionMethodDispatcherTest.cs
ActionMethodSelectorTest.cs
ActionNameAttributeTest.cs
AdditionalMetadataAttributeTest.cs
AjaxHelperTest.cs
AjaxHelper`1Test.cs
AjaxRequestExtensionsTest.cs
AllowHtmlAttributeTest.cs
AreaHelpersTest.cs
AreaRegistrationContextTest.cs
AreaRegistrationTest.cs
AssociatedMetadataProviderTest.cs
AssociatedValidatorProviderTest.cs
AsyncControllerTest.cs
AsyncTimeoutAttributeTest.cs
AuthorizationContextTest.cs
AuthorizeAttributeTest.cs
BindAttributeTest.cs
BuildManagerCompiledViewTest.cs
BuildManagerViewEngineTest.cs
ByteArrayModelBinderTest.cs
CachedAssociatedMetadataProviderTest.cs
CachedDataAnnotationsModelMetadataProviderTest.cs
CancellationTokenModelBinderTest.cs
ChildActionOnlyAttributeTest.cs
ChildActionValueProviderFactoryTest.cs
ClientDataTypeModelValidatorProviderTest.cs
CompareAttributeTest.cs
ContentResultTest.cs
ControllerActionInvokerTest.cs.REMOVED.git-id
ControllerBaseTest.cs
ControllerBuilderTest.cs
ControllerContextTest.cs
ControllerDescriptorCacheTest.cs
ControllerDescriptorTest.cs
ControllerInstanceFilterProviderTest.cs
ControllerTest.cs
DataAnnotationsModelMetadataProviderTest.cs
DataAnnotationsModelMetadataProviderTestBase.cs
DataAnnotationsModelValidatorProviderTest.cs
DataAnnotationsModelValidatorTest.cs
DataErrorInfoModelValidatorProviderTest.cs
DataTypeUtilTest.cs
DefaultControllerFactoryTest.cs
DefaultModelBinderTest.cs.REMOVED.git-id
DefaultViewLocationCacheTest.cs
DependencyResolverTest.cs
DescriptorUtilTest.cs
DictionaryHelpersTest.cs
DictionaryValueProviderTest.cs
DynamicViewDataDictionaryTest.cs
EmptyModelValidatorProviderTest.cs
ExceptionContextTest.cs
ExpressionHelperTest.cs
FieldValidationMetadataTest.cs
FileContentResultTest.cs
FilePathResultTest.cs
FileResultTest.cs
FileStreamResultTest.cs
FilterAttributeFilterProviderTest.cs
FilterInfoTest.cs
FilterProviderCollectionTest.cs
FilterProvidersTest.cs
FilterTest.cs
FormCollectionTest.cs
FormContextTest.cs
FormValueProviderFactoryTest.cs
GlobalFilterCollectionTest.cs
HandleErrorAttributeTest.cs
HandleErrorInfoTest.cs
HtmlHelperTest.cs
HtmlHelper`1Test.cs
HttpDeleteAttributeTest.cs
HttpFileCollectionValueProviderFactoryTest.cs
HttpFileCollectionValueProviderTest.cs
HttpGetAttributeTest.cs
HttpHandlerUtilTest.cs
HttpHeadAttributeTest.cs
HttpNotFoundResultTest.cs
HttpOptionsAttributeTest.cs
HttpPatchAttributeTest.cs
HttpPostAttributeTest.cs
HttpPostedFileBaseModelBinderTest.cs
HttpPutAttributeTest.cs
HttpRequestExtensionsTest.cs
HttpStatusCodeResultTest.cs
HttpUnauthorizedResultTest.cs
HttpVerbAttributeHelper.cs
JavaScriptResultTest.cs
JsonResultTest.cs
JsonValueProviderFactoryTest.cs
LinqBinaryModelBinderTest.cs
MockBuildManager.cs
MockHelpers.cs
MockableUnvalidatedRequestValues.cs
ModelBinderAttributeTest.cs
ModelBinderDictionaryTest.cs
ModelBinderProviderCollectionTest.cs
ModelBinderProvidersTest.cs
ModelBindersTest.cs
ModelBindingContextTest.cs
ModelClientValidationRuleTest.cs
ModelErrorCollectionTest.cs
ModelErrorTest.cs
ModelMetadataProvidersTest.cs
ModelMetadataTest.cs
ModelStateDictionaryTest.cs
ModelStateTest.cs
ModelValidationResultTest.cs
ModelValidatorProviderCollectionTest.cs
ModelValidatorProvidersTest.cs
ModelValidatorTest.cs
MultiSelectListTest.cs
MultiServiceResolverTest.cs
MvcHandlerTest.cs
MvcHtmlStringTest.cs
MvcHttpHandlerTest.cs
MvcRouteHandlerTest.cs
MvcTestHelper.cs
MvcWebRazorHostFactoryTest.cs
NameValueCollectionExtensionsTest.cs
NameValueCollectionValueProviderTest.cs
NoAsyncTimeoutAttributeTest.cs
NonActionAttributeTest.cs
OutputCacheAttributeTest.cs
ParameterBindingInfoTest.cs
ParameterDescriptorTest.cs
ParameterInfoUtilTest.cs
PartialViewResultTest.cs
PathHelpersTest.cs
PreApplicationStartCodeTest.cs
QueryStringValueProviderFactoryTest.cs
RangeAttributeAdapterTest.cs
RazorViewEngineTest.cs
RazorViewTest.cs
ReaderWriterCacheTest.cs
RedirectResultTest.cs
RedirectToRouteResultTest.cs
ReflectedActionDescriptorTest.cs
ReflectedControllerDescriptorTest.cs
ReflectedParameterBindingInfoTest.cs
ReflectedParameterDescriptorTest.cs
RegularExpressionAttributeAdapterTest.cs
RemoteAttributeTest.cs
RequireHttpsAttributeTest.cs
RequiredAttributeAdapterTest.cs
ResultExecutedContextTest.cs
ResultExecutingContextTest.cs
RouteCollectionExtensionsTest.cs
RouteDataValueProviderFactoryTest.cs
SelectListTest.cs
SessionStateTempDataProviderTest.cs
SingleServiceResolverTest.cs
StringLengthAttributeAdapterTest.cs
TempDataDictionaryTest.cs
TypeCacheSerializerTest.cs
TypeCacheUtilTest.cs
TypeHelpersTest.cs
UrlHelperTest.cs
UrlParameterTest.cs
UrlRewriterHelperTest.cs
ValidatableObjectAdapterTest.cs
ValidateAntiForgeryTokenAttributeTest.cs
ValidateInputAttributeTest.cs
ValueProviderCollectionTest.cs
ValueProviderDictionaryTest.cs
ValueProviderFactoriesTest.cs
ValueProviderFactoryCollectionTest.cs
ValueProviderResultTest.cs
ValueProviderUtilTest.cs
ViewContextTest.cs
ViewDataDictionaryTest.cs
ViewDataInfoTest.cs
ViewEngineCollectionTest.cs
ViewEngineResultTest.cs
ViewEnginesTest.cs
ViewMasterPageControlBuilderTest.cs
ViewMasterPageTest.cs
ViewPageControlBuilderTest.cs
ViewPageTest.cs
ViewResultBaseTest.cs
ViewResultTest.cs
ViewStartPageTest.cs
ViewTypeParserFilterTest.cs
ViewUserControlControlBuilderTest.cs
ViewUserControlTest.cs
VirtualPathProviderViewEngineTest.cs
WebFormViewEngineTest.cs
WebFormViewTest.cs
Util
System.Web.Mvc.Test.csproj
packages.config
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
cecil
debian-snapshot
ikdasm
ikvm
referencesource
rx
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
AUTHORS
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
build-mingw32.sh
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-core.spec
mono-core.spec.in
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Mvc.Test/Test/AssociatedMetadataProviderTest.cs

349 lines
14 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class AssociatedMetadataProviderTest
{
// FilterAttributes
[Fact]
public void ReadOnlyAttributeIsFilteredOffWhenContainerTypeIsViewPage()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ViewPage<PropertyModel>), "Model");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.False(parms.Attributes.Any(a => a is ReadOnlyAttribute));
}
[Fact]
public void ReadOnlyAttributeIsFilteredOffWhenContainerTypeIsViewUserControl()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ViewUserControl<PropertyModel>), "Model");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.False(parms.Attributes.Any(a => a is ReadOnlyAttribute));
}
[Fact]
public void ReadOnlyAttributeIsPreservedForReadOnlyModelProperties()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperty(() => null, typeof(ModelWithReadOnlyProperty), "ReadOnlyProperty");
// Assert
CreateMetadataParams parms = provider.CreateMetadataLog.Single();
Assert.True(parms.Attributes.Any(a => a is ReadOnlyAttribute));
}
// GetMetadataForProperties
[Fact]
public void GetMetadataForPropertiesNullContainerTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForProperties(new Object(), null),
"containerType");
}
[Fact]
public void GetMetadataForPropertiesCreatesMetadataForAllPropertiesOnModelWithPropertyValues()
{
// Arrange
PropertyModel model = new PropertyModel { LocalAttributes = 42, MetadataAttributes = "hello", MixedAttributes = 21.12 };
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperties(model, typeof(PropertyModel)).ToList(); // Call ToList() to force the lazy evaluation to evaluate
// Assert
CreateMetadataParams local =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "LocalAttributes");
Assert.Equal(typeof(int), local.ModelType);
Assert.Equal(42, local.Model);
Assert.True(local.Attributes.Any(a => a is RequiredAttribute));
CreateMetadataParams metadata =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "MetadataAttributes");
Assert.Equal(typeof(string), metadata.ModelType);
Assert.Equal("hello", metadata.Model);
Assert.True(metadata.Attributes.Any(a => a is RangeAttribute));
CreateMetadataParams mixed =
provider.CreateMetadataLog.Single(m => m.ContainerType == typeof(PropertyModel) &&
m.PropertyName == "MixedAttributes");
Assert.Equal(typeof(double), mixed.ModelType);
Assert.Equal(21.12, mixed.Model);
Assert.True(mixed.Attributes.Any(a => a is RequiredAttribute));
Assert.True(mixed.Attributes.Any(a => a is RangeAttribute));
}
[Fact]
public void GetMetadataForPropertyWithNullContainerReturnsMetadataWithNullValuesForProperties()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act
provider.GetMetadataForProperties(null, typeof(PropertyModel)).ToList(); // Call ToList() to force the lazy evaluation to evaluate
// Assert
Assert.True(provider.CreateMetadataLog.Any());
foreach (var parms in provider.CreateMetadataLog)
{
Assert.Null(parms.Model);
}
}
// GetMetadataForProperty
[Fact]
public void GetMetadataForPropertyNullContainerTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForProperty(null /* model */, null /* containerType */, "propertyName"),
"containerType");
}
[Fact]
public void GetMetadataForPropertyNullOrEmptyPropertyNameThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNullOrEmpty(
() => provider.GetMetadataForProperty(null /* model */, typeof(object), null /* propertyName */),
"propertyName");
Assert.ThrowsArgumentNullOrEmpty(
() => provider.GetMetadataForProperty(null, typeof(object), String.Empty),
"propertyName");
}
[Fact]
public void GetMetadataForPropertyInvalidPropertyNameThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.Throws<ArgumentException>(
() => provider.GetMetadataForProperty(null, typeof(object), "BadPropertyName"),
"The property System.Object.BadPropertyName could not be found.");
}
[Fact]
public void GetMetadataForPropertyWithLocalAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(int), "LocalAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "LocalAttributes");
// Assert
Assert.Same(metadata, result);
Assert.True(provider.CreateMetadataLog.Single().Attributes.Any(a => a is RequiredAttribute));
}
[Fact]
public void GetMetadataForPropertyWithMetadataAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(string), "MetadataAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "MetadataAttributes");
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.PropertyName == "MetadataAttributes");
Assert.True(parms.Attributes.Any(a => a is RangeAttribute));
}
[Fact]
public void GetMetadataForPropertyWithMixedAttributes()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, typeof(PropertyModel), null, typeof(double), "MixedAttributes");
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForProperty(null, typeof(PropertyModel), "MixedAttributes");
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.PropertyName == "MixedAttributes");
Assert.True(parms.Attributes.Any(a => a is RequiredAttribute));
Assert.True(parms.Attributes.Any(a => a is RangeAttribute));
}
// GetMetadataForType
[Fact]
public void GetMetadataForTypeNullModelTypeThrows()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// Act & Assert
Assert.ThrowsArgumentNull(
() => provider.GetMetadataForType(() => new Object(), null),
"modelType");
}
[Fact]
public void GetMetadataForTypeIncludesAttributesOnType()
{
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
ModelMetadata metadata = new ModelMetadata(provider, null, null, typeof(TypeModel), null);
provider.CreateMetadataReturnValue = metadata;
// Act
ModelMetadata result = provider.GetMetadataForType(null, typeof(TypeModel));
// Assert
Assert.Same(metadata, result);
CreateMetadataParams parms = provider.CreateMetadataLog.Single(p => p.ModelType == typeof(TypeModel));
Assert.True(parms.Attributes.Any(a => a is ReadOnlyAttribute));
}
[AdditionalMetadata("ClassName", "ClassValue")]
class ClassWithAdditionalMetadata
{
[AdditionalMetadata("PropertyName", "PropertyValue")]
public int MyProperty { get; set; }
}
[Fact]
public void MetadataAwareAttributeCanModifyTypeMetadata()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
provider.CreateMetadataReturnValue = new ModelMetadata(provider, null, null, typeof(ClassWithAdditionalMetadata), null);
// Act
ModelMetadata metadata = provider.GetMetadataForType(null, typeof(ClassWithAdditionalMetadata));
// Assert
var kvp = metadata.AdditionalValues.Single();
Assert.Equal("ClassName", kvp.Key);
Assert.Equal("ClassValue", kvp.Value);
}
[Fact]
public void MetadataAwareAttributeCanModifyPropertyMetadata()
{
// Arrange
TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
provider.CreateMetadataReturnValue = new ModelMetadata(provider, typeof(ClassWithAdditionalMetadata), null, typeof(int), "MyProperty");
// Act
ModelMetadata metadata = provider.GetMetadataForProperty(null, typeof(ClassWithAdditionalMetadata), "MyProperty");
// Assert
var kvp = metadata.AdditionalValues.Single();
Assert.Equal("PropertyName", kvp.Key);
Assert.Equal("PropertyValue", kvp.Value);
}
// Helpers
[MetadataType(typeof(Metadata))]
private class PropertyModel
{
[Required]
public int LocalAttributes { get; set; }
public string MetadataAttributes { get; set; }
[Required]
public double MixedAttributes { get; set; }
private class Metadata
{
[Range(10, 100)]
public object MetadataAttributes { get; set; }
[Range(10, 100)]
public object MixedAttributes { get; set; }
}
}
private class ModelWithReadOnlyProperty
{
public int ReadOnlyProperty { get; private set; }
}
[ReadOnly(true)]
private class TypeModel
{
}
class TestableAssociatedMetadataProvider : AssociatedMetadataProvider
{
public List<CreateMetadataParams> CreateMetadataLog = new List<CreateMetadataParams>();
public ModelMetadata CreateMetadataReturnValue = null;
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType,
Func<object> modelAccessor, Type modelType,
string propertyName)
{
CreateMetadataLog.Add(new CreateMetadataParams
{
Attributes = attributes,
ContainerType = containerType,
Model = modelAccessor == null ? null : modelAccessor(),
ModelType = modelType,
PropertyName = propertyName
});
return CreateMetadataReturnValue;
}
}
class CreateMetadataParams
{
public IEnumerable<Attribute> Attributes { get; set; }
public Type ContainerType { get; set; }
public object Model { get; set; }
public Type ModelType { get; set; }
public string PropertyName { get; set; }
}
}
}