Exemple #1
0
        public void ValidateObject_Object_ValidationContext_Bool_02()
        {
            var dummy = new Dummy();
            var ctx   = new ValidationContext(dummy, null, null);

            try {
                Validator.ValidateObject(dummy, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A1 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            try {
                Validator.ValidateObject(dummy, ctx, true);
            } catch (Exception ex) {
                Assert.Fail("#A2 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            dummy = new Dummy {
                NameField = null
            };
            AssertExtensions.Throws <ArgumentException> (() => {
                // The instance provided must match the ObjectInstance on the ValidationContext supplied.
                Validator.ValidateObject(dummy, ctx, false);
            }, "#A3-1");

            AssertExtensions.Throws <ArgumentException> (() => {
                // The instance provided must match the ObjectInstance on the ValidationContext supplied.
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A3-2");

            // Fields are ignored
            ctx = new ValidationContext(dummy, null, null);
            try {
                Validator.ValidateObject(dummy, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A4-1 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            try {
                Validator.ValidateObject(dummy, ctx, true);
            } catch (Exception ex) {
                Assert.Fail("#A4-2 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            dummy = new Dummy {
                RequiredDummyField = null
            };
            ctx = new ValidationContext(dummy, null, null);
            try {
                Validator.ValidateObject(dummy, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A5-1 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            try {
                Validator.ValidateObject(dummy, ctx, true);
            } catch (Exception ex) {
                Assert.Fail("#A5-2 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            // Required properties existence is validated
            dummy = new Dummy {
                RequiredDummyProperty = null
            };
            ctx = new ValidationContext(dummy, null, null);
            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, false);
            }, "#A6-1");

            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A6-2");

            dummy = new Dummy {
                NameProperty = null
            };
            ctx = new ValidationContext(dummy, null, null);
            try {
                Validator.ValidateObject(dummy, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A7 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            // NameProperty is null, that causes the StringLength validator to skip its tests
            try {
                Validator.ValidateObject(dummy, ctx, true);
            } catch (Exception ex) {
                Assert.Fail("#A8 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            dummy.NameProperty = "0";
            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A9");

            dummy.NameProperty = "name too long (invalid value)";
            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A10");

            dummy = new Dummy {
                MinMaxProperty = 0
            };
            ctx = new ValidationContext(dummy, null, null);
            try {
                Validator.ValidateObject(dummy, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A11 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A12");

            dummy = new Dummy {
                FailValidation = true
            };
            ctx = new ValidationContext(dummy, null, null);
            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, false);
            }, "#A13-1");

            AssertExtensions.Throws <ValidationException> (() => {
                Validator.ValidateObject(dummy, ctx, true);
            }, "#A13-2");

            var dummy2 = new DummyWithException();

            ctx = new ValidationContext(dummy2, null, null);
            AssertExtensions.Throws <ApplicationException> (() => {
                Validator.ValidateObject(dummy2, ctx, true);
            }, "#A14");

            var dummy3 = new DummyMultipleCustomValidators();

            ctx = new ValidationContext(dummy3, null, null);
            try {
                Validator.ValidateObject(dummy3, ctx, false);
            } catch (Exception ex) {
                Assert.Fail("#A9 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }

            try {
                Validator.ValidateObject(dummy3, ctx, true);
            } catch (ValidationException ex) {
                Assert.AreEqual("FirstPropertyValidationMethod", ex.Message, "#A10");
            } catch (Exception ex) {
                Assert.Fail("#A10 (exception {0} thrown: {1})", ex.GetType(), ex.Message);
            }
        }
Exemple #2
0
		public void ValidateObject_Object_ValidationContext_Bool_02 ()
		{
			var dummy = new Dummy ();
			var ctx = new ValidationContext (dummy, null, null);

			try {
				Validator.ValidateObject (dummy, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A1 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			try {
				Validator.ValidateObject (dummy, ctx, true);
			} catch (Exception ex) {
				Assert.Fail ("#A2 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			dummy = new Dummy {
				NameField = null
			};
			AssertExtensions.Throws<ArgumentException> (() => {
				// The instance provided must match the ObjectInstance on the ValidationContext supplied.
				Validator.ValidateObject (dummy, ctx, false);
			}, "#A3-1");

			AssertExtensions.Throws<ArgumentException> (() => {
				// The instance provided must match the ObjectInstance on the ValidationContext supplied.
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A3-2");

			// Fields are ignored
			ctx = new ValidationContext (dummy, null, null);
			try {
				Validator.ValidateObject (dummy, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A4-1 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			try {
				Validator.ValidateObject (dummy, ctx, true);
			} catch (Exception ex) {
				Assert.Fail ("#A4-2 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			dummy = new Dummy {
				RequiredDummyField = null
			};
			ctx = new ValidationContext (dummy, null, null);
			try {
				Validator.ValidateObject (dummy, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A5-1 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			try {
				Validator.ValidateObject (dummy, ctx, true);
			} catch (Exception ex) {
				Assert.Fail ("#A5-2 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			// Required properties existence is validated
			dummy = new Dummy {
				RequiredDummyProperty = null
			};
			ctx = new ValidationContext (dummy, null, null);
			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, false);
			}, "#A6-1");

			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A6-2");

			dummy = new Dummy {
				NameProperty = null
			};
			ctx = new ValidationContext (dummy, null, null);
			try {
				Validator.ValidateObject (dummy, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A7 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			// NameProperty is null, that causes the StringLength validator to skip its tests
			try {
				Validator.ValidateObject (dummy, ctx, true);
			} catch (Exception ex) {
				Assert.Fail ("#A8 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			dummy.NameProperty = "0";
			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A9");

			dummy.NameProperty = "name too long (invalid value)";
			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A10");

			dummy = new Dummy {
				MinMaxProperty = 0
			};
			ctx = new ValidationContext (dummy, null, null);
			try {
				Validator.ValidateObject (dummy, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A11 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A12");

			dummy = new Dummy {
				FailValidation = true
			};
			ctx = new ValidationContext (dummy, null, null);
			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, false);
			}, "#A13-1");

			AssertExtensions.Throws<ValidationException> (() => {
				Validator.ValidateObject (dummy, ctx, true);
			}, "#A13-2");

			var dummy2 = new DummyWithException ();
			ctx = new ValidationContext (dummy2, null, null);
			AssertExtensions.Throws<ApplicationException> (() => {
				Validator.ValidateObject (dummy2, ctx, true);
			}, "#A14");

			var dummy3 = new DummyMultipleCustomValidators ();
			ctx = new ValidationContext (dummy3, null, null);
			try {
				Validator.ValidateObject (dummy3, ctx, false);
			} catch (Exception ex) {
				Assert.Fail ("#A9 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}

			try {
				Validator.ValidateObject (dummy3, ctx, true);
			} catch (ValidationException ex) {
				Assert.AreEqual ("FirstPropertyValidationMethod", ex.Message, "#A10");
			} catch (Exception ex) {
				Assert.Fail ("#A10 (exception {0} thrown: {1})", ex.GetType (), ex.Message);
			}
		}
Exemple #3
0
        public void TryValidateObject_Object_ValidationContext_ICollection_Bool_02()
        {
            var dummy   = new Dummy();
            var ctx     = new ValidationContext(dummy, null, null);
            var results = new List <ValidationResult> ();

            bool valid = Validator.TryValidateObject(dummy, ctx, results, false);

            Assert.IsTrue(valid, "#A1-1");
            Assert.AreEqual(0, results.Count, "#A1-2");

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsTrue(valid, "#A1-3");
            Assert.AreEqual(0, results.Count, "#A1-4");

            dummy = new Dummy {
                NameField = null
            };
            AssertExtensions.Throws <ArgumentException> (() => {
                // The instance provided must match the ObjectInstance on the ValidationContext supplied.
                valid = Validator.TryValidateObject(dummy, ctx, results, false);
            }, "#A2-1");

            AssertExtensions.Throws <ArgumentException> (() => {
                // The instance provided must match the ObjectInstance on the ValidationContext supplied.
                valid = Validator.TryValidateObject(dummy, ctx, results, true);
            }, "#A2-2");

            // Fields are ignored
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsTrue(valid, "#A3-1");
            Assert.AreEqual(0, results.Count, "#A3-2");

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsTrue(valid, "#A3-3");
            Assert.AreEqual(0, results.Count, "#A3-4");

            dummy = new Dummy {
                RequiredDummyField = null
            };
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsTrue(valid, "#A4-1");
            Assert.AreEqual(0, results.Count, "#A4-2");

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsTrue(valid, "#A4-3");
            Assert.AreEqual(0, results.Count, "#A4-4");

            // Required properties existence is validated
            dummy = new Dummy {
                RequiredDummyProperty = null
            };
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsFalse(valid, "#A5-1");
            Assert.AreEqual(1, results.Count, "#A5-2");
            results.Clear();

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsFalse(valid, "#A5-3");
            Assert.AreEqual(1, results.Count, "#A5-4");
            results.Clear();

            dummy = new Dummy {
                NameProperty = null
            };
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsTrue(valid, "#A6-1");
            Assert.AreEqual(0, results.Count, "#A6-2");

            // NameProperty is null, that causes the StringLength validator to skip its tests
            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsTrue(valid, "#A6-3");
            Assert.AreEqual(0, results.Count, "#A6-4");

            dummy.NameProperty = "0";
            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsFalse(valid, "#A6-5");
            Assert.AreEqual(1, results.Count, "#A6-6");
            results.Clear();

            dummy.NameProperty = "name too long (invalid value)";
            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsFalse(valid, "#A6-7");
            Assert.AreEqual(1, results.Count, "#A6-8");
            results.Clear();

            dummy = new Dummy {
                MinMaxProperty = 0
            };
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsTrue(valid, "#A7-1");
            Assert.AreEqual(0, results.Count, "#A7-2");

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsFalse(valid, "#A7-3");
            Assert.AreEqual(1, results.Count, "#A7-4");
            results.Clear();

            dummy = new Dummy {
                FailValidation = true
            };
            ctx   = new ValidationContext(dummy, null, null);
            valid = Validator.TryValidateObject(dummy, ctx, results, false);
            Assert.IsFalse(valid, "#A8-1");
            Assert.AreEqual(1, results.Count, "#A8-2");
            results.Clear();

            valid = Validator.TryValidateObject(dummy, ctx, results, true);
            Assert.IsFalse(valid, "#A8-3");
            Assert.AreEqual(1, results.Count, "#A8-4");
            results.Clear();

            var dummy2 = new DummyWithException();

            ctx = new ValidationContext(dummy2, null, null);
            AssertExtensions.Throws <ApplicationException> (() => {
                Validator.TryValidateObject(dummy2, ctx, results, true);
            }, "#A9");
        }
Exemple #4
0
		public void TryValidateObject_Object_ValidationContext_ICollection_Bool_02 ()
		{
			var dummy = new Dummy ();
			var ctx = new ValidationContext (dummy, null, null);
			var results = new List<ValidationResult> ();

			bool valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsTrue (valid, "#A1-1");
			Assert.AreEqual (0, results.Count, "#A1-2");

			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsTrue (valid, "#A1-3");
			Assert.AreEqual (0, results.Count, "#A1-4");

			dummy = new Dummy {
				NameField = null
			};
			AssertExtensions.Throws<ArgumentException> (() => {
				// The instance provided must match the ObjectInstance on the ValidationContext supplied.
				valid = Validator.TryValidateObject (dummy, ctx, results, false);
			}, "#A2-1");

			AssertExtensions.Throws<ArgumentException> (() => {
				// The instance provided must match the ObjectInstance on the ValidationContext supplied.
				valid = Validator.TryValidateObject (dummy, ctx, results, true);
			}, "#A2-2");

			// Fields are ignored
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsTrue (valid, "#A3-1");
			Assert.AreEqual (0, results.Count, "#A3-2");

			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsTrue (valid, "#A3-3");
			Assert.AreEqual (0, results.Count, "#A3-4");

			dummy = new Dummy {
				RequiredDummyField = null
			};
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsTrue (valid, "#A4-1");
			Assert.AreEqual (0, results.Count, "#A4-2");

			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsTrue (valid, "#A4-3");
			Assert.AreEqual (0, results.Count, "#A4-4");

			// Required properties existence is validated
			dummy = new Dummy {
				RequiredDummyProperty = null
			};
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsFalse (valid, "#A5-1");
			Assert.AreEqual (1, results.Count, "#A5-2");
			results.Clear ();
			
			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsFalse (valid, "#A5-3");
			Assert.AreEqual (1, results.Count, "#A5-4");
			results.Clear ();

			dummy = new Dummy {
				NameProperty = null
			};
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsTrue (valid, "#A6-1");
			Assert.AreEqual (0, results.Count, "#A6-2");

			// NameProperty is null, that causes the StringLength validator to skip its tests
			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsTrue (valid, "#A6-3");
			Assert.AreEqual (0, results.Count, "#A6-4");

			dummy.NameProperty = "0";
			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsFalse (valid, "#A6-5");
			Assert.AreEqual (1, results.Count, "#A6-6");
			results.Clear ();

			dummy.NameProperty = "name too long (invalid value)";
			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsFalse (valid, "#A6-7");
			Assert.AreEqual (1, results.Count, "#A6-8");
			results.Clear ();

			dummy = new Dummy {
				MinMaxProperty = 0
			};
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsTrue (valid, "#A7-1");
			Assert.AreEqual (0, results.Count, "#A7-2");

			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsFalse (valid, "#A7-3");
			Assert.AreEqual (1, results.Count, "#A7-4");
			results.Clear ();

			dummy = new Dummy {
				FailValidation = true
			};
			ctx = new ValidationContext (dummy, null, null);
			valid = Validator.TryValidateObject (dummy, ctx, results, false);
			Assert.IsFalse (valid, "#A8-1");
			Assert.AreEqual (1, results.Count, "#A8-2");
			results.Clear ();

			valid = Validator.TryValidateObject (dummy, ctx, results, true);
			Assert.IsFalse (valid, "#A8-3");
			Assert.AreEqual (1, results.Count, "#A8-4");
			results.Clear ();

			var dummy2 = new DummyWithException ();
			ctx = new ValidationContext (dummy2, null, null);
			AssertExtensions.Throws<ApplicationException> (() => {
				Validator.TryValidateObject (dummy2, ctx, results, true);
			}, "#A9");
		}