/// <summary>
 /// Initializes a new instance of the <see cref="NinjectDefaultModelValidatorProvider"/> class.
 /// </summary>
 /// <param name="kernel">The kernel.</param>
 /// <param name="defaultModelValidatorProviders">The default model validator providers.</param>
 public NinjectDefaultModelValidatorProvider(IKernel kernel, IEnumerable<ModelValidatorProvider> defaultModelValidatorProviders)
 {
     this.kernel = kernel;
     this.defaultModelValidatorProviders = defaultModelValidatorProviders.ToList();
     var dataAnnotationsModelValidatorProvider = new DataAnnotationsModelValidatorProvider();
     dataAnnotationsModelValidatorProvider.RegisterDefaultAdapterFactory(
         ((modelValidatorProviders, attribute) =>
         {
             this.kernel.Inject(attribute);
             return (ModelValidator)new DataAnnotationsModelValidator(modelValidatorProviders, attribute);
         }));
 }
        public void RegisterAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            provider.AttributeFactories = new Dictionary<Type, DataAnnotationsModelValidationFactory>();

            // Act
            provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter));

            // Assert
            var type = provider.AttributeFactories.Keys.Single();
            Assert.Equal(typeof(MyValidationAttribute), type);

            var factory = provider.AttributeFactories.Values.Single();
            var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(object));
            var attribute = new MyValidationAttribute();
            var validator = factory(_noValidatorProviders, attribute);
            Assert.IsType<MyValidationAttributeAdapter>(validator);
        }
        public void IValidatableObjectGetsAValidator()
        {
            // Arrange
            var provider        = new DataAnnotationsModelValidatorProvider();
            var mockValidatable = new Mock <IValidatableObject>();
            var metadata        = _metadataProvider.GetMetadataForType(
                () => null,
                mockValidatable.Object.GetType()
                );

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(
                metadata,
                _noValidatorProviders
                );

            // Assert
            Assert.Single(validators);
        }
        public void RegisterAdapterGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();

            // Attribute type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapter(null, typeof(MyValidationAttributeAdapter)),
                "attributeType"
                );

            // Adapter type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapter(typeof(MyValidationAttribute), null),
                "adapterType"
                );

            // Validation attribute must derive from ValidationAttribute
            Assert.ThrowsArgument(
                () =>
                provider.RegisterAdapter(typeof(object), typeof(MyValidationAttributeAdapter)),
                "attributeType",
                "The type Object must derive from ValidationAttribute"
                );

            // Adapter must derive from ModelValidator
            Assert.ThrowsArgument(
                () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(object)),
                "adapterType",
                "The type Object must derive from ModelValidator"
                );

            // Adapter must have the expected constructor
            Assert.ThrowsArgument(
                () =>
                provider.RegisterAdapter(
                    typeof(MyValidationAttribute),
                    typeof(MyValidationAttributeAdapterBadCtor)
                    ),
                "adapterType",
                "The type MyValidationAttributeAdapterBadCtor must have a public constructor which accepts three parameters of types ModelMetadata, IEnumerable<ModelValidatorProvider>, and MyValidationAttribute"
                );
        }
        public void UnknownValidationAttributeGetsDefaultAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(
                () => null,
                typeof(DummyClassWithDummyValidationAttribute)
                );

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(
                metadata,
                _noValidatorProviders
                );

            // Assert
            var validator = validators.Single();

            Assert.IsType <DataAnnotationsModelValidator>(validator);
        }
        public void RegisterValidatableObjectAdapterFactoryGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();
            DataAnnotationsValidatableObjectAdapterFactory factory = (metadata, context) => null;

            // Attribute type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterValidatableObjectAdapterFactory(null, factory),
                "modelType");

            // Factory cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterValidatableObjectAdapterFactory(typeof(MyValidatableClass), null),
                "factory");

            // Validation attribute must derive from ValidationAttribute
            Assert.ThrowsArgument(
                () => provider.RegisterValidatableObjectAdapterFactory(typeof(object), factory),
                "modelType",
                "The type Object must derive from IValidatableObject");
        }
        public void RegisterAdapterFactoryGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();
            DataAnnotationsModelValidationFactory factory = (validatorProviders, attribute) => null;

            // Attribute type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapterFactory(null, factory),
                "attributeType");

            // Factory cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapterFactory(typeof(MyValidationAttribute), null),
                "factory");

            // Validation attribute must derive from ValidationAttribute
            Assert.ThrowsArgument(
                () => provider.RegisterAdapterFactory(typeof(object), factory),
                "attributeType",
                "The type Object must derive from ValidationAttribute");
        }
        public void RegisterDefaultValidatableObjectAdapterGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();

            // Adapter type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterDefaultValidatableObjectAdapter(null),
                "adapterType");

            // Adapter must derive from ModelValidator
            Assert.ThrowsArgument(
                () => provider.RegisterDefaultValidatableObjectAdapter(typeof(object)),
                "adapterType",
                "The type Object must derive from ModelValidator");

            // Adapter must have the expected constructor
            Assert.ThrowsArgument(
                () => provider.RegisterDefaultValidatableObjectAdapter(typeof(MyValidatableAdapterBadCtor)),
                "adapterType",
                "The type MyValidatableAdapterBadCtor must have a public constructor which accepts two parameters of types ModelMetadata and IEnumerable<ModelValidatorProvider>");
        }
        public void RegisterValidatableObjectAdapterFactory()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();

            provider.ValidatableFactories =
                new Dictionary <Type, DataAnnotationsValidatableObjectAdapterFactory>();
            DataAnnotationsValidatableObjectAdapterFactory factory = delegate
            {
                return(null);
            };

            // Act
            provider.RegisterValidatableObjectAdapterFactory(typeof(MyValidatableClass), factory);

            // Assert
            var type = provider.ValidatableFactories.Keys.Single();

            Assert.Equal(typeof(MyValidatableClass), type);
            Assert.Same(factory, provider.ValidatableFactories.Values.Single());
        }
        public void RegisterDefaultValidatableObjectAdapterFactory()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(
                () => null,
                typeof(MyValidatableClass)
                );
            ModelValidator validator = new Mock <ModelValidator>(_noValidatorProviders).Object;
            DataAnnotationsValidatableObjectAdapterFactory factory = delegate
            {
                return(validator);
            };

            provider.RegisterDefaultValidatableObjectAdapterFactory(factory);

            // Act
            var result = provider.GetValidators(metadata, _noValidatorProviders).Single();

            // Assert
            Assert.Same(validator, result);
        }
        public void RegisterAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();

            provider.AttributeFactories = new Dictionary <Type, DataAnnotationsModelValidationFactory>();

            // Act
            provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter));

            // Assert
            var type = provider.AttributeFactories.Keys.Single();

            Assert.Equal(typeof(MyValidationAttribute), type);

            var factory   = provider.AttributeFactories.Values.Single();
            var metadata  = _metadataProvider.GetMetadataForType(() => null, typeof(object));
            var attribute = new MyValidationAttribute();
            var validator = factory(metadata, _noValidatorProviders, attribute);

            Assert.IsType <MyValidationAttributeAdapter>(validator);
        }
        public void RegisterValidatableObjectAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();

            provider.ValidatableFactories = new Dictionary <Type, DataAnnotationsValidatableObjectAdapterFactory>();
            IValidatableObject validatable = new Mock <IValidatableObject>().Object;

            // Act
            provider.RegisterValidatableObjectAdapter(validatable.GetType(), typeof(MyValidatableAdapter));

            // Assert
            var type = provider.ValidatableFactories.Keys.Single();

            Assert.Equal(validatable.GetType(), type);

            var factory   = provider.ValidatableFactories.Values.Single();
            var metadata  = _metadataProvider.GetMetadataForType(() => null, typeof(object));
            var validator = factory(metadata, _noValidatorProviders);

            Assert.IsType <MyValidatableAdapter>(validator);
        }
        public void DoesNotReadPropertyValue()
        {
            // Arrange
            var           provider = new DataAnnotationsModelValidatorProvider();
            var           model    = new ObservableModel();
            ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(
                () => model.TheProperty,
                typeof(ObservableModel),
                "TheProperty"
                );

            // Act
            ModelValidator[] validators = provider
                                          .GetValidators(metadata, _noValidatorProviders)
                                          .ToArray();
            ModelValidationResult[] results = validators
                                              .SelectMany(o => o.Validate(metadata, model))
                                              .ToArray();

            // Assert
            Assert.Empty(validators);
            Assert.False(model.PropertyWasRead());
        }
        public void RegisterDefaultValidatableObjectAdapterGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();

            // Adapter type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterDefaultValidatableObjectAdapter(null),
                "adapterType");

            // Adapter must derive from ModelValidator
            Assert.ThrowsArgument(
                () => provider.RegisterDefaultValidatableObjectAdapter(typeof(object)),
                "adapterType",
                "The type Object must derive from ModelValidator");

            // Adapter must have the expected constructor
            Assert.ThrowsArgument(
                () => provider.RegisterDefaultValidatableObjectAdapter(typeof(MyValidatableAdapterBadCtor)),
                "adapterType",
                "The type MyValidatableAdapterBadCtor must have a public constructor which accepts two parameters of types ModelMetadata and IEnumerable<ModelValidatorProvider>");
        }
        public void RegisterValidatableObjectAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            provider.ValidatableFactories = new Dictionary<Type, DataAnnotationsValidatableObjectAdapterFactory>();
            IValidatableObject validatable = new Mock<IValidatableObject>().Object;

            // Act
            provider.RegisterValidatableObjectAdapter(validatable.GetType(), typeof(MyValidatableAdapter));

            // Assert
            var type = provider.ValidatableFactories.Keys.Single();
            Assert.Equal(validatable.GetType(), type);

            var factory = provider.ValidatableFactories.Values.Single();
            var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(object));
            var validator = factory(_noValidatorProviders);
            Assert.IsType<MyValidatableAdapter>(validator);
        }
        public void RegisterValidatableObjectAdapterFactory()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            provider.ValidatableFactories = new Dictionary<Type, DataAnnotationsValidatableObjectAdapterFactory>();
            DataAnnotationsValidatableObjectAdapterFactory factory = delegate { return null; };

            // Act
            provider.RegisterValidatableObjectAdapterFactory(typeof(MyValidatableClass), factory);

            // Assert
            var type = provider.ValidatableFactories.Keys.Single();
            Assert.Equal(typeof(MyValidatableClass), type);
            Assert.Same(factory, provider.ValidatableFactories.Values.Single());
        }
        public void RegisterAdapterGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();

            // Attribute type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapter(null, typeof(MyValidationAttributeAdapter)),
                "attributeType");

            // Adapter type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterAdapter(typeof(MyValidationAttribute), null),
                "adapterType");

            // Validation attribute must derive from ValidationAttribute
            Assert.ThrowsArgument(
                () => provider.RegisterAdapter(typeof(object), typeof(MyValidationAttributeAdapter)),
                "attributeType",
                "The type Object must derive from ValidationAttribute");

            // Adapter must derive from ModelValidator
            Assert.ThrowsArgument(
                () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(object)),
                "adapterType",
                "The type Object must derive from ModelValidator");

            // Adapter must have the expected constructor
            Assert.ThrowsArgument(
                () => provider.RegisterAdapter(typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapterBadCtor)),
                "adapterType",
                "The type MyValidationAttributeAdapterBadCtor must have a public constructor which accepts three parameters of types ModelMetadata, IEnumerable<ModelValidatorProvider>, and MyValidationAttribute");
        }
        public void IValidatableObjectGetsAValidator()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var mockValidatable = new Mock<IValidatableObject>();
            var metadata = _metadataProvider.GetMetadataForType(() => null, mockValidatable.Object.GetType());

            // Act
            IEnumerable<ModelValidator> validators = provider.GetValidators(metadata, _noValidatorProviders);

            // Assert
            Assert.Single(validators);
        }
        public void DoesNotReadPropertyValue()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var model = new ObservableModel();
            ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => model.TheProperty, typeof(ObservableModel), "TheProperty");

            // Act
            ModelValidator[] validators = provider.GetValidators(metadata, _noValidatorProviders).ToArray();
            ModelValidationResult[] results = validators.SelectMany(o => o.Validate(metadata, model)).ToArray();

            // Assert
            Assert.Empty(validators);
            Assert.False(model.PropertyWasRead());
        }
        public void UnknownValidationAttributeGetsDefaultAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(DummyClassWithDummyValidationAttribute));

            // Act
            IEnumerable<ModelValidator> validators = provider.GetValidators(metadata, _noValidatorProviders);

            // Assert
            var validator = validators.Single();
            Assert.IsType<DataAnnotationsModelValidator>(validator);
        }
        public void RegisterDefaultValidatableObjectAdapterFactoryGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();

            // Factory cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterDefaultValidatableObjectAdapterFactory(null),
                "factory");
        }
        public void RegisterDefaultValidatableObjectAdapterFactory()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(MyValidatableClass));
            ModelValidator validator = new Mock<ModelValidator>(_noValidatorProviders).Object;
            DataAnnotationsValidatableObjectAdapterFactory factory = delegate { return validator; };
            provider.RegisterDefaultValidatableObjectAdapterFactory(factory);

            // Act
            var result = provider.GetValidators(metadata, _noValidatorProviders).Single();

            // Assert
            Assert.Same(validator, result);
        }
        public void RegisterValidatableObjectAdapterFactoryGuardClauses()
        {
            var provider = new DataAnnotationsModelValidatorProvider();
            DataAnnotationsValidatableObjectAdapterFactory factory = (context) => null;

            // Attribute type cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterValidatableObjectAdapterFactory(null, factory),
                "modelType");

            // Factory cannot be null
            Assert.ThrowsArgumentNull(
                () => provider.RegisterValidatableObjectAdapterFactory(typeof(MyValidatableClass), null),
                "factory");

            // Validation attribute must derive from ValidationAttribute
            Assert.ThrowsArgument(
                () => provider.RegisterValidatableObjectAdapterFactory(typeof(object), factory),
                "modelType",
                "The type Object must derive from IValidatableObject");
        }
        public void RegisterDefaultValidatableObjectAdapter()
        {
            // Arrange
            var provider = new DataAnnotationsModelValidatorProvider();
            var metadata = _metadataProvider.GetMetadataForType(() => null, typeof(MyValidatableClass));
            provider.RegisterDefaultValidatableObjectAdapter(typeof(MyValidatableAdapter));

            // Act
            var result = provider.GetValidators(metadata, _noValidatorProviders).Single();

            // Assert
            Assert.IsType<MyValidatableAdapter>(result);
        }