public void GetAttributeMessage() { var mia = new DefaultMessageInterpolatorAggregator(); var va = new RangeValidator(); try { mia.GetAttributeMessage(va); } catch (AssertionFailureException) { // Ok } var defrm = new ResourceManager(Environment.BaseNameOfMessageResource, typeof (DefaultMessageInterpolatorAggregator).Assembly); var custrm = new ResourceManager("NHibernate.Validator.Tests.Resource.Messages", Assembly.GetExecutingAssembly()); var culture = new CultureInfo("en"); mia.Initialize(custrm, defrm, culture); var a = new RangeAttribute(2, 10); va.Initialize(a); mia.AddInterpolator(a, va); Assert.IsFalse(string.IsNullOrEmpty(mia.GetAttributeMessage(va))); }
public void Extreme() { var v = new RangeValidator(); v.Initialize(new Constraints.RangeAttribute(long.MinValue, 10000)); Assert.IsTrue(v.IsValid(10000, null)); Assert.IsTrue(v.IsValid(10000L, null)); Assert.IsTrue(v.IsValid(123UL, null)); Assert.IsTrue(v.IsValid(123U, null)); Assert.IsTrue(v.IsValid((ushort)5, null)); Assert.IsTrue(v.IsValid((short)5, null)); Assert.IsTrue(v.IsValid(true, null)); Assert.IsTrue(v.IsValid((byte)100, null)); Assert.IsTrue(v.IsValid((sbyte)100, null)); Assert.IsTrue(v.IsValid(AEnum.A, null)); Assert.IsTrue(v.IsValid(CarOptions.Spoiler | CarOptions.FogLights, null)); Assert.IsTrue(v.IsValid('A', null)); Assert.IsTrue(v.IsValid(9999.99999f, null)); Assert.IsTrue(v.IsValid(9999.9999999999999999999999999d, null)); Assert.IsFalse(v.IsValid(decimal.MaxValue, null)); Assert.IsFalse(v.IsValid(decimal.MaxValue.ToString(), null)); Assert.IsFalse(v.IsValid(double.MaxValue, null)); Assert.IsFalse(v.IsValid("1" + double.MaxValue, null)); v.Initialize(new Constraints.RangeAttribute(-10000, long.MaxValue)); Assert.IsTrue(v.IsValid(-10000, null)); Assert.IsTrue(v.IsValid(-10000L, null)); Assert.IsTrue(v.IsValid(123UL, null)); Assert.IsTrue(v.IsValid(123U, null)); Assert.IsTrue(v.IsValid((ushort)5, null)); Assert.IsTrue(v.IsValid((short)5, null)); Assert.IsTrue(v.IsValid(true, null)); Assert.IsTrue(v.IsValid((byte)100, null)); Assert.IsTrue(v.IsValid((sbyte)100, null)); Assert.IsTrue(v.IsValid(AEnum.A, null)); Assert.IsTrue(v.IsValid(CarOptions.Spoiler | CarOptions.FogLights, null)); Assert.IsTrue(v.IsValid('A', null)); Assert.IsTrue(v.IsValid(-9999.99999f, null)); Assert.IsTrue(v.IsValid(-9999.9999999999999999999999999d, null)); Assert.IsFalse(v.IsValid(decimal.MinValue, null)); Assert.IsFalse(v.IsValid(decimal.MinValue.ToString(), null)); Assert.IsFalse(v.IsValid(double.MinValue, null)); Assert.IsFalse(v.IsValid(double.MinValue + "9", null)); }
public void Init() { Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us"); // Integer validation validatorIntLow = new RangeValidator(0, int.MaxValue); validatorIntLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorIntHigh = new RangeValidator(int.MinValue, 0); validatorIntHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorIntLowOrHigh = new RangeValidator(RangeValidationType.Integer, "-1", "1"); validatorIntLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); intTarget = new TestTargetInt(); // decimal validation validatorDecimalLow = new RangeValidator(0, decimal.MaxValue); validatorDecimalLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorDecimalHigh = new RangeValidator(decimal.MinValue, 0); validatorDecimalHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorDecimalLowOrHigh = new RangeValidator(RangeValidationType.Decimal, "-1", "1"); validatorDecimalLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); decimalTarget = new TestTargetDecimal(); // DateTime validation validatorDateTimeLow = new RangeValidator(DateTime.Now, DateTime.MaxValue); validatorDateTimeLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); validatorDateTimeHigh = new RangeValidator(DateTime.MinValue, DateTime.Now); validatorDateTimeHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); validatorDateTimeLowOrHigh = new RangeValidator(RangeValidationType.DateTime, "2000-01-01", "2099-12-31"); validatorDateTimeLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); dateTimeTarget = new TestTargetDateTime(); // String validation validatorStringLow = new RangeValidator("bbb", String.Empty); validatorStringLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); validatorStringHigh = new RangeValidator(String.Empty, "yyy"); validatorStringHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); validatorStringLowOrHigh = new RangeValidator(RangeValidationType.String, 'b'.ToString(), 'y'.ToString()); validatorStringLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); stringTarget = new TestTargetString(); }
public void Serialization() { var defrm = new ResourceManager(Environment.BaseNameOfMessageResource, typeof (DefaultMessageInterpolatorAggregator).Assembly); var custrm = new ResourceManager("NHibernate.Validator.Tests.Resource.Messages", Assembly.GetExecutingAssembly()); var culture = new CultureInfo("en"); var mia = new DefaultMessageInterpolatorAggregator(); mia.Initialize(custrm, defrm, culture); var a = new RangeAttribute(2, 10); var va = new RangeValidator(); va.Initialize(a); mia.AddInterpolator(a, va); string originalMessage = mia.GetAttributeMessage(va); Assert.IsFalse(string.IsNullOrEmpty(originalMessage)); using (var memory = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(memory, mia); memory.Position = 0; var dmia = (DefaultMessageInterpolatorAggregator) formatter.Deserialize(memory); // follow instruction is what the owing a reference of interpolator must do dmia.Initialize(custrm, defrm, culture); Assert.AreEqual(originalMessage, dmia.GetAttributeMessage(va)); /* TODO : To make the serialization of agregator really work we need a sort of * "Validator ID" to make work the interpolators dictionary inside DefaultMessageInterpolatorAggregator. * So far it work using the validator instance but after a deserialization the instance change * so is imposible to find it again (change the reference). * Consideration to create "Validator ID" are: * - Each Validator is linked to a entity member * - Each entity member have an instance of an attribute initialized with values. * - A entity can have the same validator more than one time but with different attribute instance * (different values of Message for example). * * Note: if each Validator overrides Equals this test would pass, but it's too much invasive */ } }
public void IsValid() { var v = new RangeValidator(); v.Initialize(new Constraints.RangeAttribute()); Assert.IsTrue(v.IsValid(long.MinValue, null)); v.Initialize(new Constraints.RangeAttribute(100, 1000)); Assert.IsTrue(v.IsValid(100, null)); Assert.IsTrue(v.IsValid(1000, null)); Assert.IsTrue(v.IsValid(null, null)); Assert.IsTrue(v.IsValid("105", null)); Assert.IsTrue(v.IsValid(200, null)); Assert.IsTrue(v.IsValid(200.0m, null)); Assert.IsFalse(v.IsValid(99, null)); Assert.IsFalse(v.IsValid(1001, null)); Assert.IsFalse(v.IsValid("aaa", null)); Assert.IsFalse(v.IsValid(new object(), null)); Assert.IsFalse(v.IsValid(long.MaxValue, null)); }
public void Init() { Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us"); // Integer validation validatorIntLow = new RangeValidator(0, int.MaxValue); validatorIntLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorIntHigh = new RangeValidator(int.MinValue, 0); validatorIntHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorIntLowOrHigh = new RangeValidator(RangeValidationType.Integer, "-1", "1"); validatorIntLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); intTarget = new TestTargetInt(); // Long validation validatorLongLow = new RangeValidator(0, long.MaxValue); validatorLongLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetLong).GetProperty("TargetField")); validatorLongHigh = new RangeValidator(long.MinValue, 0); validatorLongHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetLong).GetProperty("TargetField")); validatorLongLowOrHigh = new RangeValidator(RangeValidationType.Long, "-1", "1"); validatorLongLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetLong).GetProperty("TargetField")); longTarget = new TestTargetLong(); // decimal validation validatorDecimalLow = new RangeValidator(0, decimal.MaxValue); validatorDecimalLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorDecimalHigh = new RangeValidator(decimal.MinValue, 0); validatorDecimalHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); validatorDecimalLowOrHigh = new RangeValidator(RangeValidationType.Decimal, "-1", "1"); validatorDecimalLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); decimalTarget = new TestTargetDecimal(); // DateTime validation validatorDateTimeLow = new RangeValidator(DateTime.Now, DateTime.MaxValue); validatorDateTimeLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); validatorDateTimeHigh = new RangeValidator(DateTime.MinValue, DateTime.Now); validatorDateTimeHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); validatorDateTimeLowOrHigh = new RangeValidator(RangeValidationType.DateTime, "2000-01-01", "2099-12-31"); validatorDateTimeLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime).GetProperty("TargetField")); dateTimeTarget = new TestTargetDateTime(); // String validation validatorStringLow = new RangeValidator("bbb", String.Empty); validatorStringLow.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); validatorStringHigh = new RangeValidator(String.Empty, "yyy"); validatorStringHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); validatorStringLowOrHigh = new RangeValidator(RangeValidationType.String, 'b'.ToString(), 'y'.ToString()); validatorStringLowOrHigh.Initialize(new CachedValidationRegistry(), typeof(TestTargetString).GetProperty("TargetField")); stringTarget = new TestTargetString(); // Message validation validatorIntMessage = new RangeValidator(0, 1) { ErrorMessage = CustomErrorMessage }; validatorIntMessage.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt).GetProperty("TargetField")); }