public void Validation_Method_Invalid_Bad_Params_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "LongerThan5Chars", 2.0 }; // legal params

            ExceptionHelper.ExpectValidationException(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "The field FirstParameterDisplayName must be a string with a maximum length of 5.", typeof(StringLengthAttribute), "LongerThan5Chars");
        }
        public void Validation_Method_Invalid_Good_Params_Throws()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "hello", 2.0 }; // legal params

            vtc._failMethod = true;                 // tells validation method below to fail the IsValid

            ExceptionHelper.ExpectValidationException(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "-MethodDisplayName", typeof(CustomValidationAttribute), vtc);
        }
        public void Validation_Method_Invalid_Null_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = null; // actually requires 2 params

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");
        }
        public void Validation_Fail_Method_Null_ValueType()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "xxx", null }; // 2nd param should be double

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', null)' could not be found. Parameter count: 2.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', null)' could not be found. Parameter count: 2.");
        }
        public void Validation_Fail_Method_Too_Many_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "xxx", 2.0, 5 }; // actually requires 2 params

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', 'System.Double', 'System.Int32')' could not be found. Parameter count: 3.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', 'System.Double', 'System.Int32')' could not be found. Parameter count: 3.");
        }
        public void Validation_Method_Errors_Must_Match_Expected_Method_Description()
        {
            ValTestClass instance = new ValTestClass()
            {
                _failMethod = true
            };
            ValidationContext       context = new ValidationContext(instance, null, null);
            MethodInfo              method  = typeof(ValTestClass).GetMethod("MethodWithNoParameters");
            List <ValidationResult> results = new List <ValidationResult>();

            ExceptionHelper.ExpectException <ValidationException>(delegate
            {
                ValidationUtilities.ValidateMethodCall("MethodWithNoParameters", context, null);
            }, "-MethodWithNoParameters");

            bool result = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithNoParameters", context, null, results);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("-MethodWithNoParameters", results[0].ErrorMessage);
        }
 /// <summary>
 /// Determines whether it is valid to call the specified method.
 /// </summary>
 /// <remarks>This method evaluates all the <see cref="ValidationAttribute"/>s associated
 /// with the method and its parameters. It does not do deep validation of parameter
 /// instances. Any failure will cause the exception to be thrown.
 /// </remarks>
 /// <param name="methodName">The name of the method to be called</param>
 /// <param name="validationContext">Describes the method being called.</param>
 /// <param name="parameters">The parameter values to be passed to the method.  They will be
 /// validated.</param>
 /// <exception cref="ArgumentNullException">When <paramref name="methodName"/> or
 /// <paramref name="validationContext"/> is null.</exception>
 /// <exception cref="ValidationException"> When it is not valid to call the specified
 /// method.</exception>
 internal static void ValidateMethodCall(string methodName, ValidationContext validationContext, object[] parameters)
 {
     ValidationUtilities.ValidateMethodCall(methodName, validationContext, parameters, null, false);
 }
 /// <summary>
 /// Determines whether it is valid to call the specified method.
 /// </summary>
 /// <remarks>
 /// This method evaluates all <see cref="ValidationAttribute"/>s associated with the method and its parameters.  Any failure returns <c>false</c>.
 /// </remarks>
 /// <param name="methodName">The name of the method to be called</param>
 /// <param name="validationContext">Describes the method being tested</param>
 /// <param name="parameters">The parameter values to be passed to the method.  They will be validated.</param>
 /// <param name="validationResults">Optional collection to receive validation results for failed validations.</param>
 /// <returns><c>true</c> if the method is valid.</returns>
 internal static bool TryValidateCustomUpdateMethodCall(string methodName, ValidationContext validationContext, object[] parameters, List <ValidationResult> validationResults)
 {
     return(ValidationUtilities.ValidateMethodCall(methodName, validationContext, parameters, validationResults, true));
 }