コード例 #1
0
        public void Coposition()
        {
            DerivatedClass dFullBroken = new DerivatedClass();
            dFullBroken.A = "1234";
            dFullBroken.B = "1234";

            DerivatedClass dPartialBroken = new DerivatedClass();
            dPartialBroken.A = "1234";

            BaseClass bOk = new BaseClass();
            bOk.A = "123";

            BaseClass bBroken = new BaseClass();
            bBroken.A = "1234";

            ClassValidator vtor = new ClassValidator(typeof(Composition));
            Composition c = new Composition();

            c.Any = bBroken;
            vtor.GetInvalidValues(c).Should().Have.Count.EqualTo(1);

            c.Any = dFullBroken;
            vtor.GetInvalidValues(c).Should().Have.Count.EqualTo(2);

            c.Any = bOk;
            vtor.GetInvalidValues(c).Should().Be.Empty();

            c.Any = dPartialBroken;
            var ivalue = vtor.GetInvalidValues(c).ToArray();
            ivalue.Should().Not.Be.Empty();
            ivalue.Single().PropertyName.Should().Be.EqualTo("A");
        }
コード例 #2
0
 public void AllowMoreThanOne()
 {
     var e = new WithRegEx { Value = "aaa" };
     var ca = new ClassValidator(typeof(WithRegEx));
     var iv = ca.GetInvalidValues(e).ToArray();
     ActionAssert.NotThrow(() => iv = ca.GetInvalidValues(e).ToArray());
 }
コード例 #3
0
 public void ShouldUseTheValidatorForTheProperty()
 {
     var cv = new ClassValidator(typeof(ObjWithPropertyValidator));
     var iv = cv.GetInvalidValues(new ObjWithPropertyValidator()).ToArray();
     iv.Should().Not.Be.Empty();
     iv.Single().Message.Should().Be.EqualTo("something for prop");
 }
コード例 #4
0
        public void MessageNull()
        {
            IClassValidator vtor = new ClassValidator(typeof(Foo2));

            Foo2 f = new Foo2();

            vtor.GetInvalidValues(f);
        }
コード例 #5
0
        public void ExceptionMustBeThrown()
        {
            IClassValidator vtor = new ClassValidator(typeof (Foo));

            Foo f = new Foo();

            vtor.GetInvalidValues(f);
        }
コード例 #6
0
 public void WhenNoTagIsSpecified_AllTagsMatch()
 {
     // all propeties are wrong
     IClassValidator cv = new ClassValidator(typeof(aEntity));
     var invalidValues = cv.GetInvalidValues(new aEntity {ValueMinMax = 101 }).ToArray();
     invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error", "warning");
     invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning", "information");
     invalidValues.First(iv => iv.PropertyName == "ValueWithoutTags").MatchTags.Should().Have.Count.EqualTo(0);
 }
コード例 #7
0
 public void ValidatableElementTest()
 {
     ClassValidator cv = new ClassValidator(typeof(Address));
     ValidatableElement ve = new ValidatableElement(typeof(Address), cv);
     Assert.AreEqual(ve.EntityType, typeof(Address));
     Assert.IsTrue(ReferenceEquals(cv, ve.Validator));
     Assert.IsNull(ve.Getter);
     Assert.IsFalse(ve.SubElements.GetEnumerator().MoveNext());
     Assert.IsTrue(ve.Equals(new ValidatableElement(typeof(Address), new ClassValidator(typeof(Address)))));
     Assert.IsFalse(ve.Equals(5)); // any other obj
     Assert.AreEqual(ve.GetHashCode(),
                                     (new ValidatableElement(typeof(Address), new ClassValidator(typeof(Address)))).GetHashCode());
 }
コード例 #8
0
 /// <summary>
 /// Get the validator for a type. If doesn't exists, will create one, add to the engine and return it.
 /// </summary>
 /// <param name="type">Type for the validator</param>
 /// <returns>Validator encountered or created-and-added.</returns>
 public override IClassValidator GetRootValidator(System.Type type)
 {
     lock (syncRoot)
     {
         IClassValidator result;
         if (!validators.TryGetValue(type, out result))
         {
             result = new ClassValidator(type, ConstraintValidatorFactory, new Dictionary <System.Type, IClassValidator>(), this);
             validators.Add(type, result);
         }
         return(result);
     }
 }
コード例 #9
0
 /// <summary>
 /// Get the validator for a type. If doesn't exists, will create one, add to the engine and return it.
 /// </summary>
 /// <param name="type">Type for the validator</param>
 /// <returns>Validator encountered or created-and-added.</returns>
 public override IClassValidator GetRootValidator(System.Type type)
 {
     lock (syncRoot)
     {
         IClassValidator result;
         if (!validators.TryGetValue(type, out result))
         {
             result = new ClassValidator(type, ConstraintValidatorFactory,  new Dictionary<System.Type, IClassValidator>(), this);
             validators.Add(type, result);
         }
         return result;
     }
 }
コード例 #10
0
        public void MissingKey()
        {
            Building b = new Building();
            b.Address = "2323 Younge St";
            ClassValidator validator = new ClassValidator(typeof (Building));
                validator.GetInvalidValues(b); // message should be interpolated lazily in DefaultMessageInterpolator

            b = new Building();
            b.Address = string.Empty;
            InvalidValue[] invalidValues = validator.GetInvalidValues(b);
            Assert.Greater(invalidValues.Length, 0);
            Assert.AreEqual("{notpresent.Key} and #{key.notPresent} and {key.notPresent2} 1", invalidValues[0].Message,
                            "Missing key should be left unchanged");
        }
コード例 #11
0
        public void MissingKey()
        {
            Building b = new Building();
            b.Address = "2323 Younge St";
            ClassValidator validator = new ClassValidator(typeof (Building));
                validator.GetInvalidValues(b); // message should be interpolated lazily in DefaultMessageInterpolator

            b = new Building();
            b.Address = string.Empty;
            var invalidValues = validator.GetInvalidValues(b);
            invalidValues.Should().Not.Be.Empty();
            invalidValues.Select(iv => iv.Message).Should("Missing key should be left unchanged").Contain(
                "{notpresent.Key} and {key.notPresent} and {key.notPresent2} 1");
        }
コード例 #12
0
        public void WorkAfterDeserialization()
        {
            PrefixMessageInterpolator pmi = new PrefixMessageInterpolator();

            ClassValidator cv = new ClassValidator(typeof(Address),
                                                    new ResourceManager("NHibernate.Validator.Tests.Resource.Messages",
                                                    Assembly.GetExecutingAssembly()), pmi, new CultureInfo("en"),
                                                     ValidatorMode.UseAttribute);

            RunSerializationTest(cv);

            cv = new ClassValidator(typeof(Address), new ResourceManager("NHibernate.Validator.Tests.Resource.Messages",
                                                    Assembly.GetExecutingAssembly()), pmi, new CultureInfo("en"),
                                                    ValidatorMode.UseExternal);
            RunSerializationTest(cv);
        }
コード例 #13
0
 public void SubElements()
 {
     IGetter getter = new BasicPropertyAccessor().GetGetter(typeof(AClass), "Address");
     ClassValidator cvadd = new ClassValidator(typeof(Address));
     ClassValidator cv = new ClassValidator(typeof(AClass));
     ValidatableElement ve = new ValidatableElement(typeof(AClass), cv);
     try
     {
         ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd));
         Assert.Fail("No exception adding a subelement without getter");
     }
     catch (ArgumentException)
     {
         //ok
     }
     Assert.IsFalse(ve.HasSubElements);
     ve.AddSubElement(new ValidatableElement(typeof(Address), cvadd, getter));
     Assert.IsTrue(ve.HasSubElements);
 }
コード例 #14
0
ファイル: EventValidation.cs プロジェクト: martinherr3/medusa
        public void ValidatingHandler(object sender, CancelEventArgs e)
        {
            System.Type entityType = vvtor.GetEntityType((Control) sender);

            ClassValidator vtor = new ClassValidator(entityType);

            IControlValuable controlValuable = vvtor.Resolver.GetControlValuable(sender);

            IEnumerable<InvalidValue> errors = vtor.GetPotentialInvalidValues(GetPropertyName((Control) sender),controlValuable.GetValue((Control)sender));
            int i = 0;

            foreach (InvalidValue error in errors)
            {
                errorProvider.SetError((TextBox)sender, error.Message);
                i++;
            }

            if (i == 0)
                errorProvider.SetError((TextBox) sender, string.Empty);
        }
コード例 #15
0
        public void WhenTagIsSpecified_MatchTagsContainOnlyMatchs()
        {
            // only property with 'typeof(Error)' as tag
            IClassValidator cv = new ClassValidator(typeof(aEntity));
            var invalidValues = cv.GetInvalidValues(new aEntity(), new[] { "information" }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("information");

            invalidValues = cv.GetInvalidValues(new aEntity { ValueMinMax = 101 }, new[] { "information", "warning" }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("warning");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning",
                                                                                                          "information");

            invalidValues = cv.GetInvalidValues(new aEntity(), new[] {"error", "warning"}).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning");

            invalidValues = cv.GetInvalidValues(new aEntity { ValueMinMax = 120 }, new[] {"error", "warning"}).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error", "warning");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning");

            invalidValues = cv.GetInvalidValues(new aEntity(), new[]{ "error", null}).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error");
            invalidValues.First(iv => iv.PropertyName == "ValueWithoutTags").MatchTags.Should().Have.Count.EqualTo(0);
        }
コード例 #16
0
        public void Coposition()
        {
            DerivatedClass dFullBroken = new DerivatedClass();
            dFullBroken.A = "1234";
            dFullBroken.B = "1234";

            DerivatedClass dPartialBroken = new DerivatedClass();
            dPartialBroken.A = "1234";

            BaseClass bOk = new BaseClass();
            bOk.A = "123";

            BaseClass bBroken = new BaseClass();
            bBroken.A = "1234";

            ClassValidator vtor = new ClassValidator(typeof(Composition));
            InvalidValue[] ivalues;
            Composition c = new Composition();

            c.Any = bBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(1, ivalues.Length);

            c.Any = dFullBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(2, ivalues.Length);

            c.Any = bOk;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(0, ivalues.Length);

            c.Any = dPartialBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(1, ivalues.Length);
            Assert.AreEqual("A", ivalues[0].PropertyName);
        }
コード例 #17
0
 private void GivingRulesFor(string propertyName, out ITagableRule minAttribute, out ITagableRule maxAttribute)
 {
     IClassValidator cv = new ClassValidator(typeof (Entity));
     IEnumerable<Attribute> ma = cv.GetMemberConstraints(propertyName);
     minAttribute = (ITagableRule) ma.First(a => a.TypeId == minTypeId);
     maxAttribute = (ITagableRule) ma.First(a => a.TypeId == maxTypeId);
 }
コード例 #18
0
 public void WhenTagsIncludeOnlyNull_ValidateOnlyWithNoTags()
 {
     // only property 'ValueWithoutTags'
     IClassValidator cv = new ClassValidator(typeof(Entity));
     cv.GetInvalidValues(new Entity(), new object[] {null}).Should().Have.Count.EqualTo(1);
 }
コード例 #19
0
 public void WhenTagsIncludeNull_ValidateOnlyWithTagAndWithNoTags()
 {
     // only properties with 'typeof(Error)' as tag and with no tag (prop 'ValueWithoutTags')
     IClassValidator cv = new ClassValidator(typeof(Entity));
     cv.GetInvalidValues(new Entity(), typeof(Error), null).Should().Have.Count.EqualTo(2);
 }
コード例 #20
0
 public void WhenTagIsSpecified_ValidateRelationForGivenTags()
 {
     // specifying [Valid] the relation is always analized
     IClassValidator cv = new ClassValidator(typeof(EntityWithReletion));
     cv.GetInvalidValues(new EntityWithReletion { Reference = new Entity() }, typeof(Error)).Should().Have.Count.EqualTo(1);
     cv.GetInvalidValues(new EntityWithReletion { Reference = new Entity() }, typeof(Error), null).Should().Have.Count.EqualTo(2);
 }
コード例 #21
0
 public void WhenTagIsSpecified_ValidatePotentialValueForGivenTags()
 {
     IClassValidator cv = new ClassValidator(typeof(EntityForPotential));
     cv.GetPotentialInvalidValues("Value", "123", typeof(Warning)).Should().Have.Count.EqualTo(1);
     cv.GetPotentialInvalidValues("Value", "", typeof(Warning)).Should().Have.Count.EqualTo(2);
     cv.GetPotentialInvalidValues("Value", "", typeof(Error), typeof(Warning)).Should().Have.Count.EqualTo(2);
     cv.GetPotentialInvalidValues("Value", "123", typeof(Error)).Should().Be.Empty();
     cv.GetPotentialInvalidValues("Value", "", typeof(Error)).Should().Have.Count.EqualTo(1);
 }
コード例 #22
0
 public void WhenTagIsSpecified_ValidateOnlyWithTag()
 {
     // only property with 'typeof(Error)' as tag
     IClassValidator cv = new ClassValidator(typeof(Entity));
     cv.GetInvalidValues(new Entity(), typeof(Error)).Should().Have.Count.EqualTo(1);
 }
コード例 #23
0
 public void CanBeSerialized()
 {
     ClassValidator cv = new ClassValidator(typeof(Address));
     Assert.That(cv, Is.BinarySerializable);
 }
コード例 #24
0
 public void WhenNoTagIsSpecified_ValidateAnything()
 {
     // all propeties are wrong
     IClassValidator cv = new ClassValidator(typeof(Entity));
     cv.GetInvalidValues(new Entity()).Should().Have.Count.EqualTo(4);
 }
コード例 #25
0
        public void UseOnlySpecificTaggedValidators()
        {
            IClassValidator cv = new ClassValidator(typeof (Entity));
            cv.GetInvalidValues(new Entity {ValueUsingTypes = 5}, "ValueUsingTypes", typeof (Warning)).Should().Have.Count.
                EqualTo(0);
            cv.GetInvalidValues(new Entity { ValueUsingTypes = 101}, "ValueUsingTypes", typeof(Warning)).Should().Have.Count.
                EqualTo(1);
            cv.GetInvalidValues(new Entity { ValueUsingTypes = 5 }, "ValueUsingTypes", typeof(Error)).Should().Have.Count.
                EqualTo(1);
            cv.GetInvalidValues(new Entity { ValueUsingTypes = 101 }, "ValueUsingTypes", typeof(Error)).Should().Have.Count.
                EqualTo(1);

            // Mixing
            cv.GetInvalidValues(new Entity(), typeof (Error), "error").Should().Have.Count.EqualTo(2);
        }
コード例 #26
0
 public void ProofFor_NHV48()
 {
     IClassValidator cv = new ClassValidator(typeof(Book));
     cv.GetInvalidValues(new Book(), BTags.Draft).Should().Have.Count.EqualTo(1);
     cv.GetInvalidValues(new Book(), BTags.Publish).Should().Have.Count.EqualTo(2);
 }
コード例 #27
0
        public void DoPolimorphismWithClasses()
        {
            DerivatedClass d = new DerivatedClass();
            d.A = "hola";
            d.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(DerivatedClass));
            vtor.GetInvalidValues(d).Should().Have.Count.EqualTo(2);

            ClassValidator vtor2 = new ClassValidator(typeof(BaseClass));
            vtor2.GetInvalidValues(d).Should("Polimorphic support is no working").Have.Count.EqualTo(1);

            ValidatorEngine ve = new ValidatorEngine();
            ve.Validate(d).Should().Have.Count.EqualTo(2);
        }
コード例 #28
0
        public void DoPolimorphismWithClasses()
        {
            DerivatedClass d = new DerivatedClass();
            d.A = "hola";
            d.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(DerivatedClass));
            InvalidValue[] values = vtor.GetInvalidValues(d);
            Assert.AreEqual(2,values.Length);

            ClassValidator vtor2 = new ClassValidator(typeof(BaseClass));
            InvalidValue[] values2 = vtor2.GetInvalidValues(d);
            Assert.AreEqual(1, values2.Length, "Polimorphic support is no working");

            ValidatorEngine ve = new ValidatorEngine();
            values = ve.Validate(d);
            Assert.AreEqual(2, values.Length);
        }
コード例 #29
0
        public void DoPolimorphismWithInterfaces()
        {
            Impl obj = new Impl();
            obj.A = "hola";
            obj.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(Impl));
            vtor.GetInvalidValues(obj).Should().Have.Count.EqualTo(2);

            ClassValidator vtor2 = new ClassValidator(typeof(IContract));
            vtor2.GetInvalidValues(obj).Should("Polimorphic support is no working").Have.Count.EqualTo(1);

            ValidatorEngine ve = new ValidatorEngine();
            ve.Validate(obj).Should().Have.Count.EqualTo(2);
        }
コード例 #30
0
 public void WhenTagIsSpecified_ValidateCollectionForGivenTags()
 {
     // specifying [Valid] the collection is always analized
     IClassValidator cv = new ClassValidator(typeof(EntityWithCollection));
     cv.GetInvalidValues(new EntityWithCollection { Entities = new List<Entity> { new Entity(), new Entity() } }, typeof(Error)).Should().Have.Count.EqualTo(2);
     cv.GetInvalidValues(new EntityWithCollection { Entities = new List<Entity> { new Entity(), new Entity() } }, typeof(Error), null).Should().Have.Count.EqualTo(4);
 }
コード例 #31
0
        public void DoPolimorphismWithInterfaces()
        {
            Impl obj = new Impl();
            obj.A = "hola";
            obj.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(Impl));
            InvalidValue[] values = vtor.GetInvalidValues(obj);
            Assert.AreEqual(2,values.Length);

            ClassValidator vtor2 = new ClassValidator(typeof(IContract));
            InvalidValue[] values2 = vtor2.GetInvalidValues(obj);
            Assert.AreEqual(1, values2.Length, "Polimorphic support is no working");

            ValidatorEngine ve = new ValidatorEngine();
            values = ve.Validate(obj);
            Assert.AreEqual(2, values.Length);
        }