Example #1
0
        public void CanDeserializeSerializedSectionWithTypeWithValidatorsForNamedRule()
        {
            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);

            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);
            }
        }
        public void CanDeserializeSerializedInstanceWithNameAndTypeAndResourceMessageTemplateInformation()
        {
            MockValidationSettings rwSettings      = new MockValidationSettings();
            ValidatorData          rwValidatorData = new MockValidatorData("validator1", false);

            rwSettings.Validators.Add(rwValidatorData);
            rwValidatorData.MessageTemplateResourceName     = "message template name";
            rwValidatorData.MessageTemplateResourceTypeName = "my type";

            IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>();

            sections[ValidationSettings.SectionName] = rwSettings;

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

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

                Assert.IsNotNull(roSettings);
                Assert.AreEqual(1, roSettings.Validators.Count);
                Assert.AreEqual("validator1", roSettings.Validators.Get(0).Name);
                Assert.AreSame(typeof(MockValidator), roSettings.Validators.Get(0).Type);
                Assert.AreEqual(string.Empty, roSettings.Validators.Get(0).MessageTemplate);
                Assert.AreEqual("message template name", roSettings.Validators.Get(0).MessageTemplateResourceName);
                Assert.AreEqual("my type", roSettings.Validators.Get(0).MessageTemplateResourceTypeName);
                Assert.AreEqual(string.Empty, roSettings.Validators.Get(0).Tag);
            }
        }
        public void Given()
        {
            container = new UnityContainer();
            var configurationSource     = new DictionaryConfigurationSource();
            ValidationSettings settings = new ValidationSettings();

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

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

            typeReference.Rulesets.Add(ruleData);
            ValidatedPropertyReference propertyReference1 = new ValidatedPropertyReference("Property1");

            ruleData.Properties.Add(propertyReference1);
            MockValidatorData validator11 = new MockValidatorData("validator1", true);

            propertyReference1.Validators.Add(validator11);
            validator11.MessageTemplate = "message-from-config1-RuleA";
            MockValidatorData validator12 = new MockValidatorData("validator2", true);

            propertyReference1.Validators.Add(validator12);
            validator12.MessageTemplate = "message-from-config2-RuleA";
            MockValidatorData validator13 = new MockValidatorData("validator3", false);

            propertyReference1.Validators.Add(validator13);
            validator13.MessageTemplate = "message-from-config3-RuleA";

            var typeRegistrationProvider = new ValidationTypeRegistrationProvider();
            var configurator             = new UnityContainerConfigurator(container);

            configurator.RegisterAll(configurationSource, typeRegistrationProvider);
        }
        public void GetsLiteralTemplateForMessageTemplateIfLiteralTemplateIsSet()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            rwValidatorData.MessageTemplate = "message template";

            Assert.AreEqual("message template", rwValidatorData.GetMessageTemplate());
        }
        public void GetsResourceBasedTemplateForMessageTemplateIfResourceBasedTemplateTypeAndNameAreSet()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            rwValidatorData.MessageTemplateResourceName     = "TestMessageTemplate";
            rwValidatorData.MessageTemplateResourceTypeName = typeof(Resources).AssemblyQualifiedName;

            Assert.AreEqual(Resources.TestMessageTemplate, rwValidatorData.GetMessageTemplate());
        }
        public void CreatesValidatorWithNullTagIfTagNotSet()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            Validator validator = ((IValidatorDescriptor)rwValidatorData).CreateValidator(null, null, null, null);

            Assert.AreEqual(string.Empty, rwValidatorData.Tag);
            Assert.IsNotNull(validator);
            Assert.IsNull(validator.Tag);
        }
        public void GetsLiteralTemplateForMessageTemplateIfBothLiteralOrResourceTemplateAreSet()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            rwValidatorData.MessageTemplate                 = "message template";
            rwValidatorData.MessageTemplateResourceName     = "TestMessageTemplate";
            rwValidatorData.MessageTemplateResourceTypeName = typeof(Resources).AssemblyQualifiedName;

            Assert.AreEqual("message template", rwValidatorData.GetMessageTemplate());
        }
        public void CreatesValidatorWithSpecifiedTemplate()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            rwValidatorData.MessageTemplateResourceName     = "TestMessageTemplate";
            rwValidatorData.MessageTemplateResourceTypeName = typeof(Resources).AssemblyQualifiedName;

            Validator validator = ((IValidatorDescriptor)rwValidatorData).CreateValidator(null, null, null, null);

            Assert.IsNotNull(validator);
            Assert.AreEqual(Resources.TestMessageTemplate, validator.MessageTemplate);
        }
        public void CreatesValidatorWithTagIfTagSet()
        {
            string        tag             = new string(new char[] { 'a', 'b', 'c' });
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            rwValidatorData.Tag = tag;

            Validator validator = ((IValidatorDescriptor)rwValidatorData).CreateValidator(null, null, null, null);

            Assert.IsNotNull(validator);
            Assert.AreSame(tag, validator.Tag);
        }
Example #10
0
        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);
            }
        }
        public void GetsNullForMessageTemplateIfNeitherLiteralOrResourceTemplateIsSet()
        {
            ValidatorData rwValidatorData = new MockValidatorData("validator1", false);

            Assert.AreEqual(null, rwValidatorData.GetMessageTemplate());
        }