public void FlyweightUpdatedWithValidatedFieldReferenceReturnsCorrectValues()
        {
            ValidatedFieldReference fieldReference = new ValidatedFieldReference("Field");
            fieldReference.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference.Validators.Get("validator1").MessageTemplate = "field validator 1 message";
            fieldReference.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference.Validators.Get("validator2").MessageTemplate = "field validator 2 message";
            fieldReference.Validators.Add(new MockValidatorData("validator3", false));
            fieldReference.Validators.Get("validator3").MessageTemplate = "field validator 3 message";

            ConfigurationValidatedElement validatedElement = new ConfigurationValidatedElement();
            FieldInfo fieldInfo = typeof(ConfigurationValidatedElementFixtureTestClass).GetField("Field");

            validatedElement.UpdateFlyweight(fieldReference, fieldInfo);

            Assert.AreSame(typeof(int), ((IValidatedElement)validatedElement).TargetType);
            Assert.AreSame(fieldInfo, ((IValidatedElement)validatedElement).MemberInfo);
            Assert.AreEqual(CompositionType.And, ((IValidatedElement)validatedElement).CompositionType);
            Assert.AreEqual(false, ((IValidatedElement)validatedElement).IgnoreNulls);

            IEnumerator<IValidatorDescriptor> validatorDescriptorsEnumerator
                = ((IValidatedElement)validatedElement).GetValidatorDescriptors().GetEnumerator();
            Assert.IsNotNull(validatorDescriptorsEnumerator);
            Assert.IsTrue(validatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 1 message",
                            ((MockValidatorData)validatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsTrue(validatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 2 message",
                            ((MockValidatorData)validatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsTrue(validatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 3 message",
                            ((MockValidatorData)validatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsFalse(validatorDescriptorsEnumerator.MoveNext());
        }
        public void CanDeserializeSerializedSectionWithNamedRuleSpecifyingMultipleValidatorsForField()
        {
            ValidationSettings rwSettings = new ValidationSettings();
            ValidatedTypeReference rwStringType = new ValidatedTypeReference(typeof(string));
            rwSettings.Types.Add(rwStringType);
            ValidationRulesetData rwValidationRule = new ValidationRulesetData("ruleset");
            rwStringType.Rulesets.Add(rwValidationRule);
            ValidatorData rwValidatorData = new MockValidatorData("validator1", true);
            rwValidationRule.Validators.Add(rwValidatorData);
            ValidatedFieldReference rwValidationFieldReference = new ValidatedFieldReference("System.Object.GetHashCode");
            rwValidationRule.Fields.Add(rwValidationFieldReference);
            ValidatorData rwFieldValidatorData1 = new MockValidatorData("ruleset-validator1", false);
            rwValidationFieldReference.Validators.Add(rwFieldValidatorData1);
            ValidatorData rwFieldValidatorData2 = new MockValidatorData("ruleset-validator2", false);
            rwValidationFieldReference.Validators.Add(rwFieldValidatorData2);

            IDictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();
            sections[ValidationSettings.SectionName] = rwSettings;

            using (ConfigurationFileHelper configurationFileHelper = new ConfigurationFileHelper(sections))
            {
                IConfigurationSource configurationSource = configurationFileHelper.ConfigurationSource;

                ValidationSettings roSettings = configurationSource.GetSection(ValidationSettings.SectionName) as ValidationSettings;

                Assert.IsNotNull(roSettings);
                Assert.AreEqual(1, roSettings.Types.Count);
                Assert.AreEqual(typeof(string).FullName, roSettings.Types.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Count);
                Assert.AreEqual("ruleset", roSettings.Types.Get(0).Rulesets.Get(0).Name);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Get(0).Validators.Count);
                Assert.AreEqual("validator1", roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0).GetType());
                Assert.AreEqual(true, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Validators.Get(0)).ReturnFailure);
                Assert.AreEqual(1, roSettings.Types.Get(0).Rulesets.Get(0).Fields.Count);
                Assert.AreEqual("System.Object.GetHashCode", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Name);
                Assert.AreEqual(2, roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Count);
                Assert.AreEqual("ruleset-validator1", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0).GetType());
                Assert.AreEqual(false, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(0)).ReturnFailure);
                Assert.AreEqual("ruleset-validator2", roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1).Name);
                Assert.AreSame(typeof(MockValidatorData), roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1).GetType());
                Assert.AreEqual(false, ((MockValidatorData)roSettings.Types.Get(0).Rulesets.Get(0).Fields.Get(0).Validators.Get(1)).ReturnFailure);
            }
        }
        /// <summary>
        /// This member supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// </summary>
        public Validator CreateValidatorForField(Type type, ValidatedFieldReference fieldReference)
        {
            if (fieldReference == null) throw new ArgumentNullException("fieldReference");

            if (fieldReference.Validators.Count == 0)
                return null;

            FieldInfo fieldInfo = ValidationReflectionHelper.GetField(type, fieldReference.Name, false);
            if (fieldInfo == null)
                return null;

            ConfigurationValidatedElement validatedElement = new ConfigurationValidatedElement(fieldReference, fieldInfo);

            return CreateValidatorForValidatedElement(validatedElement, this.GetCompositeValidatorBuilderForField);
        }
        private static IConfigurationSource GetNotNullValidationConfig()
        {
            var validatorData = new NotNullValidatorData()
            {
                Name = "Not Null",
                MessageTemplate = "City cannot be null"
            };

            var fieldRef = new ValidatedFieldReference()
            {
                Name = "City"
            };

            fieldRef.Validators.Add(validatorData);

            var rulesetData = new ValidationRulesetData("default");
            rulesetData.Fields.Add(fieldRef);

            var typeData = new ValidatedTypeReference(typeof (TargetAddress));
            typeData.Rulesets.Add(rulesetData);
            typeData.DefaultRuleset = rulesetData.Name;

            var section = new ValidationSettings();
            section.Types.Add(typeData);

            var configSource = new DictionaryConfigurationSource();
            configSource.Add(BlockSectionNames.Validation, section);
            return configSource;
        }
        /// <summary>
        /// Updates the flyweight for a field.
        /// </summary>
        /// <param name="validatedFieldReference">The field reference configuration object.</param>
        /// <param name="fieldInfo">The field.</param>
        public void UpdateFlyweight(ValidatedFieldReference validatedFieldReference, FieldInfo fieldInfo)
        {
            if (fieldInfo == null) throw new ArgumentNullException("fieldInfo");

            this.UpdateFlyweight(validatedFieldReference, fieldInfo, fieldInfo.FieldType);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigurationValidatedElement"/> class for a field.
 /// </summary>
 /// <param name="validatedFieldReference">The field reference configuration object.</param>
 /// <param name="fieldInfo">The field.</param>
 public ConfigurationValidatedElement(ValidatedFieldReference validatedFieldReference, FieldInfo fieldInfo)
 {
     UpdateFlyweight(validatedFieldReference, fieldInfo);
 }
        public void ValidatedFieldsEnumerableIncludesPublicFieldWithValidatorsOnly()
        {
            ValidationRulesetData rulesetData = new ValidationRulesetData();

            ValidatedFieldReference nonExistingFieldReference
                = new ValidatedFieldReference("NonExistingField");
            rulesetData.Fields.Add(nonExistingFieldReference);
            nonExistingFieldReference.Validators.Add(new MockValidatorData("validator1", false));

            ValidatedFieldReference publicFieldWithoutValidatorsReference
                = new ValidatedFieldReference("PublicFieldWithoutValidators");
            rulesetData.Fields.Add(publicFieldWithoutValidatorsReference);

            ValidatedFieldReference nonPublicFieldReference
                = new ValidatedFieldReference("NonPublicField");
            rulesetData.Fields.Add(nonPublicFieldReference);
            nonPublicFieldReference.Validators.Add(new MockValidatorData("validator1", false));

            ValidatedFieldReference publicFieldReference
                = new ValidatedFieldReference("PublicField");
            rulesetData.Fields.Add(publicFieldReference);
            publicFieldReference.Validators.Add(new MockValidatorData("validator1", false));

            ValidatedFieldReference secondPublicFieldReference
                = new ValidatedFieldReference("SecondPublicField");
            rulesetData.Fields.Add(secondPublicFieldReference);
            secondPublicFieldReference.Validators.Add(new MockValidatorData("validator1", false));

            ConfigurationValidatedType validatedType
                = new ConfigurationValidatedType(rulesetData, typeof(ConfigurationValidatedTypeFixtureTestClass));
            IEnumerator<IValidatedElement> validatedFieldsEnumerator
                = ((IValidatedType)validatedType).GetValidatedFields().GetEnumerator();

            Assert.IsTrue(validatedFieldsEnumerator.MoveNext());
            Assert.AreSame(typeof(ConfigurationValidatedTypeFixtureTestClass).GetField("PublicField"),
                           validatedFieldsEnumerator.Current.MemberInfo);
            Assert.IsTrue(validatedFieldsEnumerator.MoveNext());
            Assert.AreSame(typeof(ConfigurationValidatedTypeFixtureTestClass).GetField("SecondPublicField"),
                           validatedFieldsEnumerator.Current.MemberInfo);
            Assert.AreSame(typeof(string), validatedFieldsEnumerator.Current.TargetType);

            Assert.IsFalse(validatedFieldsEnumerator.MoveNext());
        }
        public void ValidatedFieldsEnumerableSkipsNonPublicFieldWithValidators()
        {
            ValidationRulesetData rulesetData = new ValidationRulesetData();
            ValidatedFieldReference nonPublicFieldReference
                = new ValidatedFieldReference("NonPublicField");
            rulesetData.Fields.Add(nonPublicFieldReference);
            nonPublicFieldReference.Validators.Add(new MockValidatorData("validator1", false));

            ConfigurationValidatedType validatedType
                = new ConfigurationValidatedType(rulesetData, typeof(ConfigurationValidatedTypeFixtureTestClass));
            IEnumerator<IValidatedElement> validatedFieldsEnumerator
                = ((IValidatedType)validatedType).GetValidatedFields().GetEnumerator();

            Assert.IsFalse(validatedFieldsEnumerator.MoveNext());
        }
        public void ValidatedFieldsEnumerableSkipsPublicFieldWithoutValidators()
        {
            ValidationRulesetData rulesetData = new ValidationRulesetData();
            ValidatedFieldReference publicFieldWithoutValidatorsReference
                = new ValidatedFieldReference("PublicFieldWithoutValidators");
            rulesetData.Fields.Add(publicFieldWithoutValidatorsReference);

            ConfigurationValidatedType validatedType
                = new ConfigurationValidatedType(rulesetData, typeof(ConfigurationValidatedTypeFixtureTestClass));
            IEnumerator<IValidatedElement> validatedFieldsEnumerator
                = ((IValidatedType)validatedType).GetValidatedFields().GetEnumerator();

            Assert.IsFalse(validatedFieldsEnumerator.MoveNext());
        }
        public void CreateValidatorForRuleWithInvalidFieldReferenceIgnoresInvalidField()
        {
            ValidationRulesetData ruleData = new ValidationRulesetData();
            ValidatedFieldReference fieldReference1 = new ValidatedFieldReference("PublicField");
            ruleData.Fields.Add(fieldReference1);
            ValidatedFieldReference fieldReference2 = new ValidatedFieldReference("NonPublicField");
            ruleData.Fields.Add(fieldReference2);
            fieldReference1.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            fieldReference2.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidatorForRule(typeof(TestClass), ruleData);

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(ValueAccessValidator), validator.GetType());
            ValueAccessValidatorBuilder valueAccessValidatorBuilder1 = mockFactory.requestedMembers["TestClass.PublicField"];
            Assert.AreSame(valueAccessValidatorBuilder1.BuiltValidator, validator);
            Assert.AreEqual("validator 1 message", ((MockValidator<object>)valueAccessValidatorBuilder1.ValueValidators[0]).MessageTemplate);
        }
        public void IterationContinuesAfterFlyweightUpdate()
        {
            ValidatedPropertyReference propertyReference = new ValidatedPropertyReference("Property");
            propertyReference.Validators.Add(new MockValidatorData("validator1", false));
            propertyReference.Validators.Get("validator1").MessageTemplate = "property validator 1 message";
            propertyReference.Validators.Add(new MockValidatorData("validator2", false));
            propertyReference.Validators.Get("validator2").MessageTemplate = "property validator 2 message";
            ValidatedFieldReference fieldReference = new ValidatedFieldReference("Field");
            fieldReference.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference.Validators.Get("validator1").MessageTemplate = "field validator 1 message";
            fieldReference.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference.Validators.Get("validator2").MessageTemplate = "field validator 2 message";
            fieldReference.Validators.Add(new MockValidatorData("validator3", false));
            fieldReference.Validators.Get("validator3").MessageTemplate = "field validator 3 message";

            ConfigurationValidatedElement validatedElement = new ConfigurationValidatedElement();
            PropertyInfo propertyInfo = typeof(ConfigurationValidatedElementFixtureTestClass).GetProperty("Property");
            FieldInfo fieldInfo = typeof(ConfigurationValidatedElementFixtureTestClass).GetField("Field");

            validatedElement.UpdateFlyweight(propertyReference, propertyInfo);
            Assert.AreSame(typeof(string), ((IValidatedElement)validatedElement).TargetType);
            IEnumerator<IValidatorDescriptor> propertyValidatorDescriptorsEnumerator
                = ((IValidatedElement)validatedElement).GetValidatorDescriptors().GetEnumerator();
            Assert.IsTrue(propertyValidatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("property validator 1 message",
                            ((MockValidatorData)propertyValidatorDescriptorsEnumerator.Current).MessageTemplate);

            validatedElement.UpdateFlyweight(fieldReference, fieldInfo);
            Assert.AreSame(typeof(int), ((IValidatedElement)validatedElement).TargetType);
            IEnumerator<IValidatorDescriptor> fieldValidatorDescriptorsEnumerator
                = ((IValidatedElement)validatedElement).GetValidatorDescriptors().GetEnumerator();
            Assert.IsTrue(fieldValidatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 1 message",
                            ((MockValidatorData)fieldValidatorDescriptorsEnumerator.Current).MessageTemplate);

            Assert.IsTrue(propertyValidatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("property validator 2 message",
                            ((MockValidatorData)propertyValidatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsTrue(fieldValidatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 2 message",
                            ((MockValidatorData)fieldValidatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsFalse(propertyValidatorDescriptorsEnumerator.MoveNext());
            Assert.IsTrue(fieldValidatorDescriptorsEnumerator.MoveNext());
            Assert.AreEqual("field validator 3 message",
                            ((MockValidatorData)fieldValidatorDescriptorsEnumerator.Current).MessageTemplate);
            Assert.IsFalse(fieldValidatorDescriptorsEnumerator.MoveNext());
        }
        public void CreateValidatorForRuleWithFieldReferenceReturnsCompositeValidatorWithFieldValueAccess()
        {
            ValidationRulesetData ruleData = new ValidationRulesetData();
            ValidatedFieldReference fieldReference1 = new ValidatedFieldReference("PublicField");
            ruleData.Fields.Add(fieldReference1);
            ValidatedFieldReference fieldReference2 = new ValidatedFieldReference("SecondPublicField");
            ruleData.Fields.Add(fieldReference2);
            fieldReference1.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference1.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            fieldReference2.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference2.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidatorForRule(typeof(TestClass), ruleData);

            Assert.IsNotNull(validator);
            Assert.AreSame(typeof(AndCompositeValidator), validator.GetType());
            IList<Validator> validators = ValidationTestHelper.CreateListFromEnumerable(((AndCompositeValidator)validator).Validators);
            Assert.AreEqual(2, validators.Count);
            Assert.AreEqual(2, mockFactory.requestedMembers.Count);
            ValueAccessValidatorBuilder valueAccessValidatorBuilder1 = mockFactory.requestedMembers["TestClass.PublicField"];
            Assert.AreSame(valueAccessValidatorBuilder1.BuiltValidator, validators[0]);
            Assert.AreEqual(1, valueAccessValidatorBuilder1.ValueValidators.Count);
            Assert.AreEqual("validator 1 message", ((MockValidator<object>)valueAccessValidatorBuilder1.ValueValidators[0]).MessageTemplate);
            ValueAccessValidatorBuilder valueAccessValidatorBuilder2 = mockFactory.requestedMembers["TestClass.SecondPublicField"];
            Assert.AreSame(valueAccessValidatorBuilder2.BuiltValidator, validators[1]);
            Assert.AreEqual(1, valueAccessValidatorBuilder2.ValueValidators.Count);
            Assert.AreEqual("validator 2 message", ((MockValidator<object>)valueAccessValidatorBuilder2.ValueValidators[0]).MessageTemplate);
        }
        public void CreateValidatorForFieldReferenceWithValidatorsReturnsValueAccessValidator()
        {
            ValidatedFieldReference fieldReference = new ValidatedFieldReference("PublicField");
            fieldReference.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            fieldReference.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidatorForField(typeof(TestClass), fieldReference);

            Assert.IsNotNull(validator);
            Assert.AreEqual(1, mockFactory.requestedMembers.Count);
            ValueAccessValidatorBuilder valueAccessValidatorBuilder = mockFactory.requestedMembers["TestClass.PublicField"];
            Assert.AreSame(valueAccessValidatorBuilder.BuiltValidator, validator);
            Assert.AreEqual(2, valueAccessValidatorBuilder.ValueValidators.Count);
            Assert.AreEqual("validator 1 message", ((MockValidator<object>)valueAccessValidatorBuilder.ValueValidators[0]).MessageTemplate);
            Assert.AreEqual("validator 2 message", ((MockValidator<object>)valueAccessValidatorBuilder.ValueValidators[1]).MessageTemplate);
        }
        public void CreateValidatorForNonPublicFieldReferenceWithValidatorsReturnsNull()
        {
            ValidatedFieldReference fieldReference = new ValidatedFieldReference("NonPublicField");
            fieldReference.Validators.Add(new MockValidatorData("validator1", false));
            fieldReference.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            fieldReference.Validators.Add(new MockValidatorData("validator2", false));
            fieldReference.Validators.Get("validator2").MessageTemplate = "validator 2 message";

            Validator validator = builder.CreateValidatorForField(typeof(TestClass), fieldReference);

            Assert.IsNull(validator);
            Assert.AreEqual(0, mockFactory.requestedMembers.Count);
        }
        public void CreateValidatorForFieldReferenceWithoutValidatorsReturnsNull()
        {
            ValidatedFieldReference fieldReference = new ValidatedFieldReference("PublicField");

            Validator validator = builder.CreateValidatorForField(typeof(TestClass), fieldReference);

            Assert.IsNull(validator);
        }
 public void UpdateFlyweight(ValidatedFieldReference validatedFieldReference, FieldInfo fieldInfo)
 {
     this.UpdateFlyweight(validatedFieldReference, fieldInfo, fieldInfo.FieldType);
 }