public void Entity_Validation_Property_Fail_ReadOnly_Throws()
 {
     MockEntity entity = new MockEntity();
     ExceptionHelper.ExpectInvalidOperationException(delegate()
     {
         entity.ReadOnlyProperty = "x";
     }, "The 'ReadOnlyProperty' property is read only.");
 }
 public void Entity_Validation_Property_Fail_Bogus_Throws()
 {
     MockEntity entity = new MockEntity();
     ExceptionHelper.ExpectArgumentException(delegate()
     {
         entity.BogusProperty = "x";
     }, "Type 'MockEntity' does not contain a public property named 'XXX'.\r\nParameter name: propertyName");
 }
        public void EntityConflict_ArgumentNullExceptions()
        {
            MockEntity entity = new MockEntity();
            IEnumerable<string> propertyNames = new[] { "Property1" };

            ExceptionHelper.ExpectArgumentNullException(
                () => new EntityConflict(entity, null, propertyNames, false),
                "storeEntity");

            ExceptionHelper.ExpectArgumentNullException(
                () => new EntityConflict(null, entity, propertyNames, false),
                "currentEntity");

            ExceptionHelper.ExpectArgumentNullException(
                () => new EntityConflict(entity, entity, null, false),
                "propertyNames");
        }
            public void CustomMethod(MockEntity entity)
            {
                var parameters = new Dictionary<string, object>();
                parameters.Add("entity", entity);
                this.OnOperationInvoked("CustomMethod", parameters);

                if (entity.Name == "ThrowError")
                {
                    throw new ValidationException(entity.Name);
                }
            }
        public void DisposalTracking()
        {
            var proxy = DomainServiceProxy.Create<IMockDomainServiceContract, MockDomainService>(new MockDomainServiceContext(DomainOperationType.Submit));
            var entity = new MockEntity();

            var args = new List<OperationInvokedEventArgs>();
            EventHandler<OperationInvokedEventArgs> handler = (s, a) => args.Add(a);

            try
            {
                MockDomainService.Disposed += handler;

                // Invoke a operation multiple times
                proxy.CustomMethod(entity);
                proxy.CustomMethod(entity);
                proxy.CustomMethod(entity);

                proxy.Dispose();

                Assert.AreEqual(3, args.Count);
                Assert.AreEqual("Dispose", args[0].Name);
                Assert.AreEqual("Dispose", args[1].Name);
                Assert.AreEqual("Dispose", args[2].Name);
            }
            finally
            {
                MockDomainService.Disposed -= handler;
            }
        }
            public void UpdateEntity(MockEntity entity)
            {
                var parameters = new Dictionary<string, object>();
                parameters.Add("entity", entity);
                this.OnOperationInvoked("UpdateEntity", parameters);

                if (entity.Name == "ThrowError")
                {
                    throw new ValidationException(entity.Name);
                }
                else if (entity.Name == "CurrentEntity")
                {
                    var original = this.ChangeSet.GetOriginal(entity);
                    Assert.IsNotNull(original);

                    entity.ID = original.ID;
                    entity.Name = original.Name;
                }
            }
 public void Overload2(MockEntity e)
 {
 }
        public void AssociateOriginal_ChangeSet_GetOriginal()
        {
            var proxy = DomainServiceProxy.Create<IMockDomainServiceContract, MockDomainService>(new MockDomainServiceContext(DomainOperationType.Submit));
            Assert.IsNull(this.OperationInvokedArgs);

            var entity = proxy.GetSingletonEntity();
            entity.Name = "CurrentEntity";
            Assert.AreEqual("CurrentEntity", entity.Name);

            var original = new MockEntity() { ID = entity.ID, Name = "OriginalEntity" };
            DomainServiceProxy.AssociateOriginal(proxy, entity, original);

            proxy.UpdateEntity(entity);
            Assert.IsNotNull(this.OperationInvokedArgs);
            Assert.AreEqual("UpdateEntity", this.OperationInvokedArgs.Name);
            Assert.AreEqual(1, this.OperationInvokedArgs.Parameters.Count);
            Assert.AreSame(entity, this.OperationInvokedArgs.Parameters.Values.Single());
            Assert.AreEqual("OriginalEntity", entity.Name);
        }
        public void Entity_Validation_Property_Fail_Null_Required()
        {
            MockEntity entity = new MockEntity();

#if SILVERLIGHT
            INotifyDataErrorInfo notifier = (INotifyDataErrorInfo)entity;

            List<string> actualErrors = new List<string>();

            notifier.ErrorsChanged += (s, e) =>
                {
                    actualErrors.Add(e.PropertyName);
                };

            entity.ReadWriteProperty = null;

            // With INotifyDataErrorInfo validation, the property will get set, even though it's invalid
            Assert.AreEqual<string>(null, entity.ReadWriteProperty, "The value should have been set");
            Assert.IsTrue(actualErrors.SequenceEqual(new string[] { "ReadWriteProperty" }), "The list of errors received");

            // Verify that we have the expected validation error for the property
            IEnumerable<ValidationResult> results = entity.ValidationErrors.Where(e => e.MemberNames.Contains("ReadWriteProperty"));
            Assert.AreEqual<int>(1, results.Count(), "Expected a single error for the property");
            Assert.AreEqual<string>("The ReadWriteProperty field is required.", results.Single().ErrorMessage, "ErrorMessage from the result");
#else
            ExceptionHelper.ExpectValidationException(delegate()
            {
                entity.ReadWriteProperty = null;
            }, "The ReadWriteProperty field is required.", typeof(RequiredAttribute), null);
#endif
        }
 public void ValidateProperty_Null_ValidationContext_Throws()
 {
     MockEntity entity = new MockEntity();
     ExceptionHelper.ExpectArgumentNullException(() => entity.CallValidateProperty(null, "value"), "validationContext");
 }
            public void CustomMethodWithParams(MockEntity entity, long param1, IEnumerable<string> param2, Dictionary<byte, string> param3, Uri param4)
            {
                var parameters = new Dictionary<string, object>();
                parameters.Add("entity", entity);
                parameters.Add("param1", param1);
                parameters.Add("param2", param2);
                parameters.Add("param3", param3);
                parameters.Add("param4", param4);
                this.OnOperationInvoked("CustomMethodWithParams", parameters);

                if (entity.Name == "ThrowError")
                {
                    throw new ValidationException(entity.Name);
                }
            }
 public void Entity_Validation_Object_Fail_Null_Required_Throws()
 {
     MockEntity entity = new MockEntity();
     ValidationContext context = new ValidationContext(entity, null, null);
     ExceptionHelper.ExpectValidationException(delegate()
     {
         System.ComponentModel.DataAnnotations.Validator.ValidateObject(entity, context);
     }, "The ReadWriteProperty field is required.", typeof(RequiredAttribute), null);
 }
        public void ValidationErrors_PropertyErrorsAreReplacedFromValidateProperty()
        {
            MockEntity entity = new MockEntity();
            INotifyDataErrorInfo notifier = (INotifyDataErrorInfo)entity;

            string[] affectedMemberNames = new string[] { "ReadWriteProperty" };
            string[] unaffectedMemberNames = new string[] { "Foo", "Bar" };

            // Add a fake error that we'll ensure gets cleared out as well as other errors that will remain
            entity.ValidationErrors.Add(new ValidationResult("Fake error for affected members", affectedMemberNames));
            entity.ValidationErrors.Add(new ValidationResult("Fake error for unaffected members", unaffectedMemberNames));

            List<string> actualChanges = new List<string>();
            IEnumerable<ValidationResult> errors;
            notifier.ErrorsChanged += (s, e) => actualChanges.Add(e.PropertyName);

            // Set the property to an invalid value
            entity.ReadWriteProperty = null;
            errors = notifier.GetErrors(affectedMemberNames.Single()).Cast<ValidationResult>();

            Assert.IsTrue(actualChanges.SequenceEqual(affectedMemberNames), "Events when setting to an invalid value");
            Assert.AreEqual<int>(1, errors.Count(), "Expect a single error after setting to an invalid value");

            errors = notifier.GetErrors("Foo").Cast<ValidationResult>();
            Assert.AreEqual<int>(1, errors.Count(), "Expect a single Foo error after setting to an invalid value");

            errors = notifier.GetErrors("Bar").Cast<ValidationResult>();
            Assert.AreEqual<int>(1, errors.Count(), "Expect a single Bar error after setting to an invalid value");
            actualChanges.Clear();

            // Set the property to a valid value
            entity.ReadWriteProperty = "Valid";
            errors = notifier.GetErrors(affectedMemberNames.Single()).Cast<ValidationResult>();

            Assert.IsTrue(actualChanges.SequenceEqual(affectedMemberNames), "Events when setting to a valid value");
            Assert.AreEqual<int>(0, errors.Count(), "Expect no errors after setting to a valid value");
            actualChanges.Clear();
        }
 public void Entity_Validation_Property_ReadOnly_Missing()
 {
     MockEntity entity = new MockEntity();
     entity.ReadWriteProperty = "x";
     Assert.AreEqual("x", entity.ReadWriteProperty);
 }
        public void ValidationErrors_ErrorsChangedEvents()
        {
            MockEntity entity = new MockEntity();
            INotifyDataErrorInfo notifier = (INotifyDataErrorInfo)entity;

            List<string> actualChanges = new List<string>();
            notifier.ErrorsChanged += (s, e) => actualChanges.Add(e.PropertyName);

            // Adding an error for Foo should raise a single event only for Foo
            string[] oneMemberName = new string[] { "Foo" };
            ValidationResult singlePropertyResult = new ValidationResult("Error Message", oneMemberName);
            entity.ValidationErrors.Add(singlePropertyResult);
            Assert.IsTrue(actualChanges.SequenceEqual(oneMemberName), "Events when adding single property error");
            actualChanges.Clear();

            // Adding an error for Foo and Bar will raise events for both properties even though it's a single error
            // Additionally, having a member name of null (entity-level error) will cause an event with a null property name
            string[] twoMemberNamesPlusNull = new string[] { "Foo", "Bar", null };
            ValidationResult multiPropertyResult = new ValidationResult("Error Message", twoMemberNamesPlusNull);
            entity.ValidationErrors.Add(multiPropertyResult);
            Assert.IsTrue(actualChanges.OrderBy(s => s).SequenceEqual(twoMemberNamesPlusNull.OrderBy(s => s)), "Events when adding multi-property error");
            actualChanges.Clear();

            // Removing the single-property result will raise an event for that property
            entity.ValidationErrors.Remove(singlePropertyResult);
            Assert.IsTrue(actualChanges.SequenceEqual(oneMemberName), "HasChanges events when removing single property error");
            actualChanges.Clear();

            // Removing the multi-property result will raise events for both properties
            entity.ValidationErrors.Remove(multiPropertyResult);
            Assert.IsTrue(actualChanges.OrderBy(s => s).SequenceEqual(twoMemberNamesPlusNull.OrderBy(s => s)), "HasChanges events when removing multi-property error");
        }
        public void ValidationErrors_PropertyChangedEventsEntityErrors()
        {
            MockEntity entity = new MockEntity();
            INotifyDataErrorInfo notifier = (INotifyDataErrorInfo)entity;

            // Expect property change notifications for: HasValidationErrors, ValidationErrors
            string[] expectedChanges = new string[] { "HasValidationErrors", "ValidationErrors" };

            List<string> actualChanges = new List<string>();
            entity.PropertyChanged += (s, e) => actualChanges.Add(e.PropertyName);

            Assert.IsFalse(entity.HasValidationErrors, "HasValidationErrors before adding");
            Assert.IsFalse(notifier.HasErrors, "HasErrors before adding");

            entity.ValidationErrors.Add(new ValidationResult("Error Message", null));
            Assert.IsTrue(entity.HasValidationErrors, "HasValidationErrors after adding");
            Assert.IsTrue(notifier.HasErrors, "HasErrors after adding");
            Assert.IsTrue(actualChanges.SequenceEqual(expectedChanges), "Property changes after adding");
            actualChanges.Clear();

            entity.ValidationErrors.Clear();
            Assert.IsFalse(entity.HasValidationErrors, "HasValidationErrors after clearing");
            Assert.IsFalse(notifier.HasErrors, "HasErrors after clearing");
            Assert.IsTrue(actualChanges.SequenceEqual(expectedChanges), "Property changes after clearing");
        }
 public void Entity_Validation_Property_ReadOnly_False()
 {
     MockEntity entity = new MockEntity();
     entity.ReadOnlyFalseProperty = "x";
     Assert.AreEqual("x", entity.ReadOnlyFalseProperty);
 }
 public void Entity_Validation_Property_Internal_Setter()
 {
     MockEntity entity = new MockEntity();
     entity.PublicGetterInternalSetter = "x";
     Assert.AreEqual("x", entity.PublicGetterInternalSetter);
 }
        public void Entity_Validation_Property_Fail_StringLength_Exceeded_Throws()
        {
            MockEntity entity = new MockEntity();
#if SILVERLIGHT
            INotifyDataErrorInfo notifier = (INotifyDataErrorInfo)entity;

            List<string> actualErrors = new List<string>();

            notifier.ErrorsChanged += (s, e) =>
            {
                actualErrors.Add(e.PropertyName);
            };

            string newValue = "LongerThan10Characters";
            entity.ReadWriteProperty = newValue;

            // With INotifyDataErrorInfo validation, the property will get set, even though it's invalid
            Assert.AreEqual<string>(newValue, entity.ReadWriteProperty, "The value should have been set");
            Assert.IsTrue(actualErrors.SequenceEqual(new string[] { "ReadWriteProperty" }), "The list of errors received");

            // Verify that we have the expected validation error for the property
            IEnumerable<ValidationResult> results = entity.ValidationErrors.Where(e => e.MemberNames.Contains("ReadWriteProperty"));
            Assert.AreEqual<int>(1, results.Count(), "Expected a single error for the property");
            Assert.AreEqual<string>("The field ReadWriteProperty must be a string with a maximum length of 10.", results.Single().ErrorMessage, "ErrorMessage from the result");
#else
            ExceptionHelper.ExpectValidationException(delegate()
            {
                entity.ReadWriteProperty = "LongerThan10Characters";
            }, "The field ReadWriteProperty must be a string with a maximum length of 10.", typeof(StringLengthAttribute), "LongerThan10Characters");
#endif
        }