Example #1
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_WithAddMethod(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            });
            Assert.Equal(
                new[] { 1, 2, 3 },
                value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>);
        }
Example #2
0
        public void SetCollectionProperty_NonSettableProperty_ArrayValue_FixedSize_Throws(string propertyName)
        {
            object value        = new SampleClassWithNonSettableCollectionProperties();
            Type   propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;

            Assert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The value of the property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' is an array. " +
                              "Consider adding a setter for the property.", propertyName));
        }
Example #3
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_NoAdd_Throws(string propertyName)
        {
            object value        = new SampleClassWithNonSettableCollectionProperties();
            Type   propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;

            Assert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The type '{0}' of the property '{1}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' does not have an Add method. " +
                              "Consider using a collection type that does have an Add method - for example IList<T> or ICollection<T>.", propertyType.FullName, propertyName));
        }
Example #4
0
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_WithAddMethod(string propertyName)
        {
            object       value       = new SampleClassWithNonSettableCollectionProperties();
            IEdmProperty edmProperty = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            });

            Assert.Equal(
                new[] { 1, 2, 3 },
                value.GetType().GetProperty(propertyName).GetValue(value, index: null) as IEnumerable <int>);
        }
Example #5
0
        public void SetCollectionProperty_NonSettableProperty_NullValue_Throws(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            value.GetType().GetProperty(propertyName).SetValue(value, null, null);

            Assert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, propertyName, isDelta: false, value: new List <int> {
                1, 2, 3
            }),
                String.Format("The property '{0}' on type 'System.Web.Http.OData.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' returned a null value. " +
                              "The input stream contains collection items which cannot be added if the instance is null.", propertyName));
        }
        public void SetCollectionProperty_NonSettableProperty_NonNullValue_NoAdd_Throws(string propertyName)
        {
            object       value        = new SampleClassWithNonSettableCollectionProperties();
            Type         propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;
            IEdmProperty edmProperty  = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The type '{0}' of the property '{1}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' does not have an Add method. " +
                              "Consider using a collection type that does have an Add method - for example IList<T> or ICollection<T>.", propertyType.FullName, propertyName));
        }
        public void SetCollectionProperty_NonSettableProperty_ArrayValue_FixedSize_Throws(string propertyName)
        {
            object       value        = new SampleClassWithNonSettableCollectionProperties();
            Type         propertyType = typeof(SampleClassWithNonSettableCollectionProperties).GetProperty(propertyName).PropertyType;
            IEdmProperty edmProperty  = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The value of the property '{0}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' is an array. " +
                              "Consider adding a setter for the property.", propertyName));
        }
        public void SetCollectionProperty_NonSettableProperty_NullValue_Throws(string propertyName)
        {
            object value = new SampleClassWithNonSettableCollectionProperties();

            value.GetType().GetProperty(propertyName).SetValue(value, null, null);
            IEdmProperty edmProperty = GetMockEdmProperty(propertyName, EdmPrimitiveTypeKind.Int32);

            ExceptionAssert.Throws <SerializationException>(
                () => DeserializationHelpers.SetCollectionProperty(value, edmProperty, value: new List <int> {
                1, 2, 3
            }, propertyName: edmProperty.Name),
                String.Format("The property '{0}' on type 'Microsoft.AspNet.OData.Test.Formatter.Deserialization.DeserializationHelpersTest+SampleClassWithNonSettableCollectionProperties' returned a null value. " +
                              "The input stream contains collection items which cannot be added if the instance is null.", propertyName));
        }
        public void SetCollectionProperty_ClearsCollection_IfClearCollectionIsTrue(string propertyName)
        {
            // Arrange
            IEnumerable <int> value    = new int[] { 1, 2, 3 };
            object            resource = new SampleClassWithNonSettableCollectionProperties
            {
                ICollection = { 42 },
                IList       = { 42 },
                Collection  = { 42 },
                List        = { 42 },
                CustomCollectionWithNoEmptyCtor = { 42 },
                CustomCollection = { 42 }
            };

            // Act
            DeserializationHelpers.SetCollectionProperty(resource, propertyName, null, value, clearCollection: true);

            // Assert
            Assert.Equal(
                value,
                resource.GetType().GetProperty(propertyName).GetValue(resource, index: null) as IEnumerable <int>);
        }