Exemple #1
0
        public void CanCreateValidatorForTypeWithMultipleValidatorConfigurations()
        {
            ValidationRulesetData ruleData = new ValidationRulesetData("ruleset1");

            ruleData.Validators.Add(new MockValidatorData("validator1", false));
            ruleData.Validators.Get("validator1").MessageTemplate = "validator 1 message";
            ruleData.Validators.Add(new MockValidatorData("validator2", false));
            ruleData.Validators.Get("validator2").MessageTemplate = "validator 2 message";

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

            Assert.IsNotNull(validator);

            CompositeValidatorBuilder compositeValidatorBuilder = mockFactory.requestedTypes["TestClass"];

            Assert.IsNotNull(compositeValidatorBuilder);
            Assert.AreSame(compositeValidatorBuilder.BuiltValidator, validator);
            Assert.AreEqual(false, compositeValidatorBuilder.IgnoreNulls);
            Assert.AreEqual(CompositionType.And, compositeValidatorBuilder.CompositionType);
            IDictionary <string, MockValidator <object> > valueValidators = ValidationTestHelper.CreateMockValidatorsMapping(compositeValidatorBuilder.ValueValidators);

            Assert.AreEqual(2, valueValidators.Count);
            Assert.IsTrue(valueValidators.ContainsKey("validator 1 message"));
            Assert.IsTrue(valueValidators.ContainsKey("validator 2 message"));
        }
Exemple #2
0
        public void CanCreateValidatorForTypeWithMultipleValidationConfigurationWithProvidedRulesetThroughTopLevelFactoryMethod()
        {
            ValidationSettings settings = new ValidationSettings();

            configurationSource.Add(ValidationSettings.SectionName, settings);
            ValidatedTypeReference typeReference = new ValidatedTypeReference(typeof(TestClass));

            settings.Types.Add(typeReference);
            typeReference.DefaultRuleset = "RuleB";
            ValidationRulesetData ruleDataA = new ValidationRulesetData("RuleA");

            typeReference.Rulesets.Add(ruleDataA);
            ruleDataA.Validators.Add(new MockValidatorData("validator1", false));
            ruleDataA.Validators.Get("validator1").MessageTemplate = "Message1-RuleA";
            ruleDataA.Validators.Add(new MockValidatorData("validator2", false));
            ruleDataA.Validators.Get("validator2").MessageTemplate = "Message2-RuleA";
            ruleDataA.Validators.Add(new MockValidatorData("validator3", false));
            ruleDataA.Validators.Get("validator3").MessageTemplate = "Message3-RuleA";
            ValidationRulesetData ruleDataB = new ValidationRulesetData("RuleB");

            typeReference.Rulesets.Add(ruleDataB);
            ruleDataB.Validators.Add(new MockValidatorData("validator1", false));
            ruleDataB.Validators.Get("validator1").MessageTemplate = "Message1-RuleB";
            ruleDataB.Validators.Add(new MockValidatorData("validator2", false));
            ruleDataB.Validators.Get("validator2").MessageTemplate = "Message1-RuleB";

            // Note: Use a local builder here since the assumption was made about how and when the
            //       the builder uses the configuration source....
            var localBuilder =
                ConfigurationValidatorBuilder.FromConfiguration(
                    configurationSource,
                    mockFactory,
                    ValidationFactory.DefaultCompositeValidatorFactory);
            Validator validator = localBuilder.CreateValidator(typeof(TestClass), "RuleA");

            Assert.IsNotNull(validator);
            CompositeValidatorBuilder compositeValidatorBuilder = mockFactory.requestedTypes["TestClass"];

            Assert.IsNotNull(compositeValidatorBuilder);
            Assert.AreEqual(false, compositeValidatorBuilder.IgnoreNulls);
            Assert.AreEqual(CompositionType.And, compositeValidatorBuilder.CompositionType);
            IDictionary <string, MockValidator <object> > valueValidators = ValidationTestHelper.CreateMockValidatorsMapping(compositeValidatorBuilder.ValueValidators);

            Assert.AreEqual(3, valueValidators.Count);
            Assert.IsTrue(valueValidators.ContainsKey("Message1-RuleA"));
            Assert.IsTrue(valueValidators.ContainsKey("Message2-RuleA"));
            Assert.IsTrue(valueValidators.ContainsKey("Message3-RuleA"));
        }