Correct use of EdmTypeCannotBeNull message (fixes #1558)

- https://aspnetwebstack.codeplex.com/workitem/1558
- "[OData] Incorrect use of EdmTypeCannotBeNull message"
- Make three uses of this resource string consistent
- PLOC issue primarily in EntityInstanceContext.GetPropertyValue() because message
mentioned "an System.Web.Http.OData.IEdmObject"
- Problem more than a PLOC issue since ODataSerializerContext.GetEdmType() would
throw NRE not intended Exception and ODataMediaTypeFormatter.GetSerializer()
would reference incorrect Type in Exception message
- Also add tests covering these sad paths (2 were missed)
This commit is contained in:
dougbu 2013-12-17 21:52:09 -08:00
Родитель bcd40d2f6b
Коммит 3c305a0baa
12 изменённых файлов: 82 добавлений и 10 удалений

Просмотреть файл

@ -186,7 +186,9 @@ namespace System.Web.Http.OData
IEdmTypeReference edmType = EdmObject.GetEdmType();
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, EdmObject.GetType(), typeof(IEdmObject));
// Provide general guidance in the message. typeof(IEdmTypeReference).Name would be too specific.
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, EdmObject.GetType().FullName,
typeof(IEdmObject).Name);
}
throw Error.InvalidOperation(SRResources.PropertyNotFound, edmType.ToTraceString(), propertyName);

Просмотреть файл

@ -556,8 +556,8 @@ namespace System.Web.Http.OData.Formatter
IEdmTypeReference edmType = edmObject.GetEdmType();
if (edmType == null)
{
throw new SerializationException(
Error.Format(SRResources.EdmTypeCannotBeNull, type.FullName, typeof(IEdmObject).Name));
throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
edmObject.GetType().FullName, typeof(IEdmObject).Name));
}
serializer = serializerProvider.GetEdmTypeSerializer(edmType);

Просмотреть файл

@ -150,7 +150,8 @@ namespace System.Web.Http.OData.Formatter.Serialization
edmType = edmObject.GetEdmType();
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull);
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
typeof(IEdmObject).Name);
}
}
else

Просмотреть файл

@ -186,7 +186,9 @@ namespace System.Web.Http.OData
IEdmTypeReference edmType = EdmObject.GetEdmType();
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, EdmObject.GetType(), typeof(IEdmObject));
// Provide general guidance in the message. typeof(IEdmTypeReference).Name would be too specific.
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, EdmObject.GetType().FullName,
typeof(IEdmObject).Name);
}
throw Error.InvalidOperation(SRResources.PropertyNotFound, edmType.ToTraceString(), propertyName);

Просмотреть файл

@ -560,8 +560,8 @@ namespace System.Web.Http.OData.Formatter
IEdmTypeReference edmType = edmObject.GetEdmType();
if (edmType == null)
{
throw new SerializationException(
Error.Format(SRResources.EdmTypeCannotBeNull, type.FullName, typeof(IEdmObject).Name));
throw new SerializationException(Error.Format(SRResources.EdmTypeCannotBeNull,
edmObject.GetType().FullName, typeof(IEdmObject).Name));
}
serializer = serializerProvider.GetEdmTypeSerializer(edmType);

Просмотреть файл

@ -150,7 +150,8 @@ namespace System.Web.Http.OData.Formatter.Serialization
edmType = edmObject.GetEdmType();
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull);
throw Error.InvalidOperation(SRResources.EdmTypeCannotBeNull, edmObject.GetType().FullName,
typeof(IEdmObject).Name);
}
}
else

Просмотреть файл

@ -96,6 +96,24 @@ namespace System.Web.Http.OData
"The property 'EdmObject' of EntityInstanceContext cannot be null.");
}
[Fact]
public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
{
// Arrange
object outObject = null;
Mock<IEdmEntityObject> mock = new Mock<IEdmEntityObject>();
mock.Setup(o => o.TryGetPropertyValue(It.IsAny<string>(), out outObject)).Returns(false).Verifiable();
mock.Setup(o => o.GetEdmType()).Returns<IEdmRowTypeReference>(null).Verifiable();
EntityInstanceContext context = new EntityInstanceContext();
context.EdmObject = mock.Object;
// Act & Assert
Assert.Throws<InvalidOperationException>(() => context.GetPropertyValue("SomeProperty"),
exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmEntityObjectProxy' is null. " +
"The EDM type of an IEdmObject cannot be null.");
mock.Verify();
}
[Fact]
public void Property_EntityInstance_CanBeBuiltFromIEdmObject()
{

Просмотреть файл

@ -609,7 +609,7 @@ namespace System.Web.Http.OData.Formatter
() => formatter
.WriteToStreamAsync(typeof(int), edmObject.Object, new MemoryStream(), new Mock<HttpContent>().Object, transportContext: null)
.Wait(),
"The EDM type of the object of type 'System.Int32' is null. The EDM type of an IEdmObject cannot be null.");
"The EDM type of the object of type 'Castle.Proxies.IEdmObjectProxy' is null. The EDM type of an IEdmObject cannot be null.");
}
[Fact]

Просмотреть файл

@ -90,5 +90,20 @@ namespace System.Web.Http.OData.Formatter.Serialization
ODataSerializerContext context = new ODataSerializerContext();
Assert.NotNull(context.Items);
}
[Fact]
public void GetEdmType_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
{
// Arrange (this code path does not use ODataSerializerContext fields or properties)
var context = new ODataSerializerContext();
Mock<IEdmObject> mock = new Mock<IEdmObject>(MockBehavior.Strict);
mock.Setup(edmObject => edmObject.GetEdmType()).Returns<IEdmTypeReference>(null).Verifiable();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => context.GetEdmType(mock.Object, null),
exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmObjectProxy' is null. " +
"The EDM type of an IEdmObject cannot be null.");
mock.Verify();
}
}
}

Просмотреть файл

@ -95,6 +95,24 @@ namespace System.Web.Http.OData
"The property 'EdmObject' of EntityInstanceContext cannot be null.");
}
[Fact]
public void GetPropertyValue_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
{
// Arrange
object outObject = null;
Mock<IEdmEntityObject> mock = new Mock<IEdmEntityObject>();
mock.Setup(o => o.TryGetPropertyValue(It.IsAny<string>(), out outObject)).Returns(false).Verifiable();
mock.Setup(o => o.GetEdmType()).Returns<IEdmRowTypeReference>(null).Verifiable();
EntityInstanceContext context = new EntityInstanceContext();
context.EdmObject = mock.Object;
// Act & Assert
Assert.Throws<InvalidOperationException>(() => context.GetPropertyValue("SomeProperty"),
exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmEntityObjectProxy' is null. " +
"The EDM type of an IEdmObject cannot be null.");
mock.Verify();
}
[Fact]
public void Property_EntityInstance_CanBeBuiltFromIEdmObject()
{

Просмотреть файл

@ -610,7 +610,7 @@ namespace System.Web.Http.OData.Formatter
() => formatter
.WriteToStreamAsync(typeof(int), edmObject.Object, new MemoryStream(), new Mock<HttpContent>().Object, transportContext: null)
.Wait(),
"The EDM type of the object of type 'System.Int32' is null. The EDM type of an IEdmObject cannot be null.");
"The EDM type of the object of type 'Castle.Proxies.IEdmObjectProxy' is null. The EDM type of an IEdmObject cannot be null.");
}
[Fact]

Просмотреть файл

@ -90,5 +90,20 @@ namespace System.Web.Http.OData.Formatter.Serialization
ODataSerializerContext context = new ODataSerializerContext();
Assert.NotNull(context.Items);
}
[Fact]
public void GetEdmType_ThrowsInvalidOperation_IfEdmObjectGetEdmTypeReturnsNull()
{
// Arrange (this code path does not use ODataSerializerContext fields or properties)
var context = new ODataSerializerContext();
Mock<IEdmObject> mock = new Mock<IEdmObject>(MockBehavior.Strict);
mock.Setup(edmObject => edmObject.GetEdmType()).Returns<IEdmTypeReference>(null).Verifiable();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => context.GetEdmType(mock.Object, null),
exceptionMessage: "The EDM type of the object of type 'Castle.Proxies.IEdmObjectProxy' is null. " +
"The EDM type of an IEdmObject cannot be null.");
mock.Verify();
}
}
}