Remove empty constructors that where there for unit testing only, but where not critical for unit tests as Exception and CatchBlock are very easy to mock.
Thus removing multiple unnecessary unit tests, and unnecessary tests in the code to verify that the unit tests constructors are not called by users.
This commit is contained in:
Yishai Galatzer 2013-12-11 13:02:47 -08:00
Родитель 9aa310ccff
Коммит 0af7cf7446
21 изменённых файлов: 156 добавлений и 539 удалений

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

@ -31,13 +31,6 @@ namespace System.Web.Http.Owin.ExceptionHandling
ExceptionContext exceptionContext = context.ExceptionContext;
Contract.Assert(exceptionContext != null);
Exception exception = exceptionContext.Exception;
if (exception == null)
{
throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Exception"), "context");
}
HttpRequestMessage request = exceptionContext.Request;
@ -48,7 +41,7 @@ namespace System.Web.Http.Owin.ExceptionHandling
}
context.Result = new ResponseMessageResult(request.CreateErrorResponse(HttpStatusCode.InternalServerError,
exception));
exceptionContext.Exception));
}
}
}

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

@ -38,12 +38,6 @@ namespace System.Web.Http.ExceptionHandling
Contract.Assert(exceptionContext != null);
Exception exception = exceptionContext.Exception;
if (exception == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Exception"), "context");
}
HttpRequestMessage request = exceptionContext.Request;
if (request == null)

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

@ -11,19 +11,10 @@ namespace System.Web.Http.ExceptionHandling
public class ExceptionContext
{
/// <summary>Initializes a new instance of the <see cref="ExceptionContext"/> class.</summary>
/// <remarks>This constructor is for unit testing purposes only.</remarks>
public ExceptionContext()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="actionContext">The action context in which the exception occurred.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock,
HttpActionContext actionContext)
/// <remarks>This constructor is for unit testing purposes only.</remarks>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock)
{
if (exception == null)
{
@ -38,7 +29,18 @@ namespace System.Web.Http.ExceptionHandling
}
CatchBlock = catchBlock;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="actionContext">The action context in which the exception occurred.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock,
HttpActionContext actionContext)
: this(exception, catchBlock)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionContext");
@ -78,21 +80,8 @@ namespace System.Web.Http.ExceptionHandling
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="request">The request being processed when the exception was caught.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request)
: this(exception, catchBlock)
{
if (exception == null)
{
throw new ArgumentNullException("exception");
}
Exception = exception;
if (catchBlock == null)
{
throw new ArgumentNullException("catchBlock");
}
CatchBlock = catchBlock;
if (request == null)
{
throw new ArgumentNullException("request");
@ -110,22 +99,8 @@ namespace System.Web.Http.ExceptionHandling
/// <param name="request">The request being processed when the exception was caught.</param>
/// <param name="response">The repsonse being returned when the exception was caught.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request,
HttpResponseMessage response)
HttpResponseMessage response) : this(exception, catchBlock)
{
if (exception == null)
{
throw new ArgumentNullException("exception");
}
Exception = exception;
if (catchBlock == null)
{
throw new ArgumentNullException("catchBlock");
}
CatchBlock = catchBlock;
if (request == null)
{
throw new ArgumentNullException("request");
@ -143,12 +118,10 @@ namespace System.Web.Http.ExceptionHandling
}
/// <summary>Gets the exception caught.</summary>
/// <remarks>The setter is for unit testing purposes only.</remarks>
public Exception Exception { get; set; }
public Exception Exception { get; private set; }
/// <summary>Gets the catch block in which the exception was caught.</summary>
/// <remarks>The setter is for unit testing purposes only.</remarks>
public ExceptionContextCatchBlock CatchBlock { get; set; }
public ExceptionContextCatchBlock CatchBlock { get; private set; }
/// <summary>Gets the request being processed when the exception was caught.</summary>
/// <remarks>The setter is for unit testing purposes only.</remarks>

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

@ -21,12 +21,6 @@ namespace System.Web.Http.ExceptionHandling
ExceptionContext exceptionContext = context.ExceptionContext;
Contract.Assert(exceptionContext != null);
if (exceptionContext.Exception == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Exception"), "context");
}
if (!ShouldHandle(context))
{
return TaskHelpers.Completed();
@ -69,12 +63,6 @@ namespace System.Web.Http.ExceptionHandling
ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock;
if (catchBlock == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext), "CatchBlock"), "context");
}
return catchBlock.IsTopLevel;
}
}

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

@ -25,12 +25,6 @@ namespace System.Web.Http.ExceptionHandling
ExceptionContext exceptionContext = context.ExceptionContext;
Contract.Assert(exceptionContext != null);
if (exceptionContext.Exception == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Exception"), "context");
}
if (!ShouldLog(context))
{
return TaskHelpers.Completed();
@ -72,15 +66,8 @@ namespace System.Web.Http.ExceptionHandling
ExceptionContext exceptionContext = context.ExceptionContext;
Contract.Assert(exceptionContext != null);
Exception exception = exceptionContext.Exception;
if (exception == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "Exception"), "context");
}
IDictionary data = exception.Data;
IDictionary data = exceptionContext.Exception.Data;
if (data == null || data.IsReadOnly)
{

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

@ -80,12 +80,6 @@ namespace System.Web.Http.ExceptionHandling
Contract.Assert(_exceptionContext != null);
ExceptionContextCatchBlock catchBlock = _exceptionContext.CatchBlock;
if (catchBlock == null)
{
throw new InvalidOperationException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(ExceptionContext).Name, "CatchBlock"));
}
return catchBlock.CallsHandler;
}
}

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

@ -29,29 +29,13 @@ namespace System.Web.Http.Owin.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context");
}
[Fact]
public void HandleAsync_IfExceptionIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = CreateContext(new ExceptionContext());
Assert.Null(context.ExceptionContext.Exception); // Guard
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context",
"ExceptionContext.Exception must not be null.");
}
[Fact]
public void HandleAsync_IfRequestIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext
{
Exception = CreateException()
});
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer));
Assert.Null(context.ExceptionContext.Request); // Guard
CancellationToken cancellationToken = CancellationToken.None;
@ -135,11 +119,9 @@ namespace System.Web.Http.Owin.ExceptionHandling
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request)
{
return CreateContext(new ExceptionContext
{
Exception = CreateException(),
Request = request
});
return CreateContext(new ExceptionContext(CreateException(),
ExceptionCatchBlocks.HttpServer,
request));
}
}
}

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

@ -28,7 +28,7 @@ namespace System.Web.Http.Owin.ExceptionHandling
private static ExceptionLoggerContext CreateContext()
{
return new ExceptionLoggerContext(new ExceptionContext());
return new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
}
private static EmptyExceptionLogger CreateProductUnderTest()

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

@ -261,10 +261,7 @@ namespace System.Web.Http.Owin
{
Assert.NotNull(actual);
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext()
{
Exception = new Exception()
});
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
CancellationToken cancellationToken = CancellationToken.None;
expected
@ -284,10 +281,7 @@ namespace System.Web.Http.Owin
{
Assert.NotNull(actual);
ExceptionLoggerContext context = new ExceptionLoggerContext(new ExceptionContext()
{
Exception = new Exception()
});
ExceptionLoggerContext context = new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
CancellationToken cancellationToken = CancellationToken.None;
expected

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

@ -94,7 +94,7 @@ namespace System.Web.Http.ExceptionHandling
};
IExceptionLogger product = CreateProductUnderTest(loggers);
ExceptionLoggerContext expectedContext = CreateContext();
ExceptionLoggerContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = CreateCancellationToken();
// Act
@ -115,7 +115,7 @@ namespace System.Web.Http.ExceptionHandling
IEnumerable<IExceptionLogger> loggers = new IExceptionLogger[] { null };
IExceptionLogger product = CreateProductUnderTest(loggers);
ExceptionLoggerContext context = CreateContext();
ExceptionLoggerContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CreateCancellationToken();
// Act & Assert
@ -130,9 +130,9 @@ namespace System.Web.Http.ExceptionHandling
return source.Token;
}
private static ExceptionLoggerContext CreateContext()
private static ExceptionLoggerContext CreateMinimalValidContext()
{
return new ExceptionLoggerContext(new ExceptionContext());
return new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
}
private static IExceptionLogger CreateDummyLogger()

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

@ -28,29 +28,12 @@ namespace System.Web.Http.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context");
}
[Fact]
public void HandleAsync_IfExceptionIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = CreateContext(new ExceptionContext());
Assert.Null(context.ExceptionContext.Exception); // Guard
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context",
"ExceptionContext.Exception must not be null.");
}
[Fact]
public void HandleAsync_IfRequestIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext
{
Exception = CreateException()
});
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer));
Assert.Null(context.ExceptionContext.Request); // Guard
CancellationToken cancellationToken = CancellationToken.None;
@ -155,22 +138,13 @@ namespace System.Web.Http.ExceptionHandling
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request)
{
return CreateContext(new ExceptionContext
{
Exception = CreateException(),
Request = request
});
return CreateContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer, request));
}
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request,
ExceptionContextCatchBlock catchBlock)
{
return CreateContext(new ExceptionContext
{
Exception = CreateException(),
Request = request,
CatchBlock = catchBlock
});
return CreateContext(new ExceptionContext(CreateException(), catchBlock, request));
}
}
}

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

@ -27,7 +27,7 @@ namespace System.Web.Http.ExceptionHandling
private static ExceptionHandlerContext CreateContext()
{
return new ExceptionHandlerContext(new ExceptionContext());
return new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
}
private static EmptyExceptionHandler CreateProductUnderTest()

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

@ -8,85 +8,6 @@ namespace System.Web.Http.ExceptionHandling
{
public class ExceptionContextTests
{
[Fact]
public void RequestSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
using (HttpRequestMessage expectedRequest = CreateRequest())
{
// Act
product.Request = expectedRequest;
// Assert
HttpRequestMessage request = product.Request;
Assert.Same(expectedRequest, request);
}
}
[Fact]
public void RequestContextSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
HttpRequestContext expectedRequestContext = CreateRequestContext();
// Act
product.RequestContext = expectedRequestContext;
// Assert
HttpRequestContext requestContext = product.RequestContext;
Assert.Same(expectedRequestContext, requestContext);
}
[Fact]
public void ControllerContextSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
HttpControllerContext expectedControllerContext = CreateControllerContext();
// Act
product.ControllerContext = expectedControllerContext;
// Assert
HttpControllerContext controllerContext = product.ControllerContext;
Assert.Same(expectedControllerContext, controllerContext);
}
[Fact]
public void ActionContextSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
HttpActionContext expectedActionContext = CreateActionContext();
// Act
product.ActionContext = expectedActionContext;
// Assert
HttpActionContext actionContext = product.ActionContext;
Assert.Same(expectedActionContext, actionContext);
}
[Fact]
public void ResponseSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
using (HttpResponseMessage expectedResponse = CreateResponse())
{
// Act
product.Response = expectedResponse;
// Assert
HttpResponseMessage response = product.Response;
Assert.Same(expectedResponse, response);
}
}
[Fact]
public void ConstructorWithoutArguments_SetsPropertiesToSpecifiedValues()
{
@ -94,12 +15,13 @@ namespace System.Web.Http.ExceptionHandling
ExceptionContext product = CreateProductUnderTest();
// Assert
Assert.Null(product.Exception);
Assert.NotNull(product.Exception);
Assert.NotNull(product.CatchBlock);
Assert.Null(product.ActionContext);
Assert.Null(product.ControllerContext);
Assert.Null(product.RequestContext);
Assert.Null(product.Request);
Assert.Null(product.CatchBlock);
Assert.Null(product.Response);
}
@ -427,36 +349,6 @@ namespace System.Web.Http.ExceptionHandling
}
}
[Fact]
public void ExceptionSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
Exception expectedException = CreateException();
// Act
product.Exception = expectedException;
// Assert
Exception exception = product.Exception;
Assert.Same(expectedException, exception);
}
[Fact]
public void CatchBlockSet_UpdatesValue()
{
// Arrange
ExceptionContext product = CreateProductUnderTest();
ExceptionContextCatchBlock expectedCatchBlock = CreateCatchBlock();
// Act
product.CatchBlock = expectedCatchBlock;
// Assert
ExceptionContextCatchBlock catchBlock = product.CatchBlock;
Assert.Same(expectedCatchBlock, catchBlock);
}
private static HttpActionContext CreateActionContext()
{
return new HttpActionContext();
@ -497,7 +389,7 @@ namespace System.Web.Http.ExceptionHandling
private static ExceptionContext CreateProductUnderTest()
{
return new ExceptionContext();
return new ExceptionContext(CreateException(), CreateCatchBlock());
}
private static ExceptionContext CreateProductUnderTest(Exception exception,

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

@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling
public void ExceptionContextGet_ReturnsSpecifiedInstance()
{
// Arrange
ExceptionContext expectedContext = CreateContext();
ExceptionContext expectedContext = CreateMinimalContext();
ExceptionHandlerContext product = CreateProductUnderTest(expectedContext);
// Act
@ -37,7 +37,7 @@ namespace System.Web.Http.ExceptionHandling
public void ResultSet_UpdatesValue()
{
// Arrange
ExceptionHandlerContext product = CreateProductUnderTest();
ExceptionHandlerContext product = CreateProductUnderTest(CreateMinimalContext());
IHttpActionResult expectedResult = CreateDummyResult();
// Act
@ -53,10 +53,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
Exception expectedException = new InvalidOperationException();
ExceptionContext context = new ExceptionContext
{
Exception = expectedException
};
ExceptionContext context = new ExceptionContext(expectedException, ExceptionCatchBlocks.HttpServer);
ExceptionHandlerContext product = CreateProductUnderTest(context);
// Act
@ -71,10 +68,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
ExceptionContextCatchBlock expectedCatchBlock = new ExceptionContextCatchBlock("IgnoreName", false, false);
ExceptionContext context = new ExceptionContext
{
CatchBlock = expectedCatchBlock
};
ExceptionContext context = new ExceptionContext(new Exception(), expectedCatchBlock);
ExceptionHandlerContext product = CreateProductUnderTest(context);
// Act
@ -90,10 +84,7 @@ namespace System.Web.Http.ExceptionHandling
// Arrange
using (HttpRequestMessage expectedRequest = new HttpRequestMessage())
{
ExceptionContext context = new ExceptionContext
{
Request = expectedRequest
};
ExceptionContext context = CreateMinimalContext(expectedRequest);
ExceptionHandlerContext product = CreateProductUnderTest(context);
// Act
@ -109,10 +100,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
HttpRequestContext expectedRequestContext = new HttpRequestContext();
ExceptionContext context = new ExceptionContext
{
RequestContext = expectedRequestContext
};
ExceptionContext context = CreateMinimalContext(expectedRequestContext);
ExceptionHandlerContext product = CreateProductUnderTest(context);
// Act
@ -122,9 +110,17 @@ namespace System.Web.Http.ExceptionHandling
Assert.Same(expectedRequestContext, requestContext);
}
private static ExceptionContext CreateContext()
private static ExceptionContext CreateMinimalContext(HttpRequestContext context = null)
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)
{
RequestContext = context,
};
}
private static ExceptionContext CreateMinimalContext(HttpRequestMessage request)
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer, request);
}
private static IHttpActionResult CreateDummyResult()
@ -132,11 +128,6 @@ namespace System.Web.Http.ExceptionHandling
return new Mock<IHttpActionResult>(MockBehavior.Strict).Object;
}
private static ExceptionHandlerContext CreateProductUnderTest()
{
return CreateProductUnderTest(CreateContext());
}
private static ExceptionHandlerContext CreateProductUnderTest(ExceptionContext exceptionContext)
{
return new ExceptionHandlerContext(exceptionContext);

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

@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext expectedContext = CreateContext();
ExceptionContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
@ -45,7 +45,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
IExceptionHandler handler = CreateStubHandler();
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
@ -72,7 +72,7 @@ namespace System.Web.Http.ExceptionHandling
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
@ -100,7 +100,7 @@ namespace System.Web.Http.ExceptionHandling
IHttpActionResult result = mock.Object;
IExceptionHandler handler = CreateResultHandler(result);
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
@ -119,7 +119,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
IExceptionHandler handler = null;
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
@ -150,7 +150,7 @@ namespace System.Web.Http.ExceptionHandling
.Returns(Task.FromResult<HttpResponseMessage>(null));
IHttpActionResult result = mock.Object;
IExceptionHandler handler = CreateResultHandler(result);
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
@ -172,9 +172,9 @@ namespace System.Web.Http.ExceptionHandling
return new CancellationTokenSource();
}
private static ExceptionContext CreateContext()
private static ExceptionContext CreateMinimalValidContext()
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static IExceptionHandler CreateDummyHandler()

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

@ -23,22 +23,6 @@ namespace System.Web.Http.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context");
}
[Fact]
public void HandleAsync_IfExceptionIsNull_Throws()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>(MockBehavior.Strict);
IExceptionHandler product = mock.Object;
ExceptionHandlerContext context = CreateContext(CreateExceptionContext());
Assert.Null(context.ExceptionContext.Exception); // Guard
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context",
"ExceptionContext.Exception must not be null.");
}
[Fact]
public void HandleAsync_IfShouldHandleReturnsTrue_DelegatesToHandleAsyncCore()
{
@ -52,7 +36,7 @@ namespace System.Web.Http.ExceptionHandling
IExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateValidContext();
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
using (CancellationTokenSource tokenSource = CreateTokenSource())
{
@ -78,7 +62,7 @@ namespace System.Web.Http.ExceptionHandling
IExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateValidContext();
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
CancellationToken expectedCancellationToken = CancellationToken.None;
// Act
@ -101,7 +85,7 @@ namespace System.Web.Http.ExceptionHandling
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateContext();
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
@ -123,7 +107,7 @@ namespace System.Web.Http.ExceptionHandling
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = CreateContext();
ExceptionHandlerContext context = CreateMinimalValidHandlerContext();
// Act & Assert
Assert.DoesNotThrow(() => product.Handle(context));
@ -143,22 +127,6 @@ namespace System.Web.Http.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.ShouldHandle(context), "context");
}
[Fact]
public void ShouldHandle_IfCallStackIsNull_Throws()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>();
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext());
Assert.Null(context.ExceptionContext.CatchBlock); // Guard
// Act & Assert
Assert.ThrowsArgument(() => product.ShouldHandle(context), "context",
"ExceptionContext.CatchBlock must not be null.");
}
[Theory]
[InlineData(true)]
[InlineData(false)]
@ -169,10 +137,8 @@ namespace System.Web.Http.ExceptionHandling
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = CreateContext(new ExceptionContext
{
CatchBlock = new ExceptionContextCatchBlock("IgnoreCaughtAt", isTopLevelCatchBlock, callsHandler: false)
});
ExceptionHandlerContext context = CreateContext(new ExceptionContext(new Exception(),
new ExceptionContextCatchBlock("IgnoreCaughtAt", isTopLevelCatchBlock, callsHandler: false)));
// Act
bool shouldHandle = product.ShouldHandle(context);
@ -187,19 +153,14 @@ namespace System.Web.Http.ExceptionHandling
return source.Task;
}
private static ExceptionHandlerContext CreateContext()
{
return CreateContext(CreateExceptionContext());
}
private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext)
{
return new ExceptionHandlerContext(exceptionContext);
}
private static ExceptionContext CreateExceptionContext()
private static ExceptionContext CreateMinimalValidExceptionContext()
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static CancellationTokenSource CreateTokenSource()
@ -207,12 +168,9 @@ namespace System.Web.Http.ExceptionHandling
return new CancellationTokenSource();
}
private static ExceptionHandlerContext CreateValidContext()
private static ExceptionHandlerContext CreateMinimalValidHandlerContext()
{
return new ExceptionHandlerContext(new ExceptionContext
{
Exception = new Exception()
});
return new ExceptionHandlerContext(CreateMinimalValidExceptionContext());
}
}
}

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

@ -22,7 +22,7 @@ namespace System.Web.Http.ExceptionHandling
public void ExceptionContextGet_ReturnsSpecifiedInstance()
{
// Arrange
ExceptionContext expectedContext = CreateContext();
ExceptionContext expectedContext = CreateMinimalValidContext();
ExceptionLoggerContext product = CreateProductUnderTest(expectedContext);
// Act
@ -37,10 +37,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
Exception expectedException = new InvalidOperationException();
ExceptionContext context = new ExceptionContext
{
Exception = expectedException
};
ExceptionContext context = new ExceptionContext(expectedException, ExceptionCatchBlocks.HttpServer);
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act
@ -55,10 +52,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
ExceptionContextCatchBlock expectedCatchBlock = new ExceptionContextCatchBlock("IgnoreName", false, false);
ExceptionContext context = new ExceptionContext
{
CatchBlock = expectedCatchBlock
};
ExceptionContext context = new ExceptionContext(new Exception(), expectedCatchBlock);
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act
@ -74,10 +68,8 @@ namespace System.Web.Http.ExceptionHandling
// Arrange
using (HttpRequestMessage expectedRequest = new HttpRequestMessage())
{
ExceptionContext context = new ExceptionContext
{
Request = expectedRequest
};
ExceptionContext context = new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer, expectedRequest);
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act
@ -93,10 +85,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
HttpRequestContext expectedRequestContext = new HttpRequestContext();
ExceptionContext context = new ExceptionContext
{
RequestContext = expectedRequestContext
};
ExceptionContext context = CreateMinimalValidContext(expectedRequestContext);
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act
@ -112,11 +101,10 @@ namespace System.Web.Http.ExceptionHandling
public void CallsHandlerGet_ReturnsCatchBlockCallsHandler(bool expectedCallsHandler)
{
// Arrange
ExceptionContext context = new ExceptionContext
{
CatchBlock = new ExceptionContextCatchBlock("IgnoreName", isTopLevel: false,
callsHandler: expectedCallsHandler)
};
ExceptionContext context = new ExceptionContext(new Exception(),
new ExceptionContextCatchBlock("IgnoreName", isTopLevel: false,
callsHandler: expectedCallsHandler));
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act
@ -126,22 +114,12 @@ namespace System.Web.Http.ExceptionHandling
Assert.Equal(expectedCallsHandler, callsHandler);
}
[Fact]
public void CallsHandlerGet_IfCatchBlockIsNull_Throws()
private static ExceptionContext CreateMinimalValidContext(HttpRequestContext context = null)
{
// Arrange
ExceptionContext context = new ExceptionContext();
Assert.Null(context.CatchBlock); // Guard
ExceptionLoggerContext product = CreateProductUnderTest(context);
// Act & Assert
Assert.Throws<InvalidOperationException>(() => product.CallsHandler,
"ExceptionContext.CatchBlock must not be null.");
}
private static ExceptionContext CreateContext()
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)
{
RequestContext = context,
};
}
private static ExceptionLoggerContext CreateProductUnderTest(ExceptionContext exceptionContext)

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

@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext expectedContext = CreateContext();
ExceptionContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
@ -43,7 +43,7 @@ namespace System.Web.Http.ExceptionHandling
{
// Arrange
IExceptionLogger logger = null;
ExceptionContext context = CreateContext();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
@ -76,9 +76,9 @@ namespace System.Web.Http.ExceptionHandling
return source.Task;
}
private static ExceptionContext CreateContext()
private static ExceptionContext CreateMinimalValidContext()
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static IExceptionLogger CreateDummyLogger()

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

@ -25,22 +25,6 @@ namespace System.Web.Http.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.LogAsync(context, cancellationToken), "context");
}
[Fact]
public void LogAsync_IfExceptionIsNull_Throws()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>(MockBehavior.Strict);
IExceptionLogger product = mock.Object;
ExceptionLoggerContext context = CreateContext(CreateExceptionContext());
Assert.Null(context.ExceptionContext.Exception); // Guard
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgument(() => product.LogAsync(context, cancellationToken), "context",
"ExceptionContext.Exception must not be null.");
}
[Fact]
public void LogAsync_IfShouldLogReturnsTrue_DelegatesToLogAsyncCore()
{
@ -54,7 +38,7 @@ namespace System.Web.Http.ExceptionHandling
IExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateValidContext();
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
using (CancellationTokenSource tokenSource = CreateTokenSource())
{
@ -80,7 +64,7 @@ namespace System.Web.Http.ExceptionHandling
IExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateValidContext();
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
CancellationToken expectedCancellationToken = CancellationToken.None;
// Act
@ -103,7 +87,7 @@ namespace System.Web.Http.ExceptionHandling
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateContext();
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
@ -125,7 +109,7 @@ namespace System.Web.Http.ExceptionHandling
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext context = CreateContext();
ExceptionLoggerContext context = CreateMinimalValidLoggerContext();
// Act & Assert
Assert.DoesNotThrow(() => product.Log(context));
@ -145,22 +129,6 @@ namespace System.Web.Http.ExceptionHandling
Assert.ThrowsArgumentNull(() => product.ShouldLog(context), "context");
}
[Fact]
public void ShouldLog_IfExceptionIsNull_Throws()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext context = CreateContext(CreateExceptionContext());
Assert.Null(context.ExceptionContext.Exception); // Guard
// Act & Assert
Assert.ThrowsArgument(() => product.ShouldLog(context), "context",
"ExceptionContext.Exception must not be null.");
}
[Fact]
public void ShouldLog_IfExceptionDataIsNull_ReturnsTrue()
{
@ -170,7 +138,7 @@ namespace System.Web.Http.ExceptionHandling
ExceptionLogger product = mock.Object;
Exception exception = CreateException(data: null);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -198,7 +166,7 @@ namespace System.Web.Http.ExceptionHandling
dataMock.Setup(d => d.IsReadOnly).Returns(true);
IDictionary data = dataMock.Object;
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -217,7 +185,7 @@ namespace System.Web.Http.ExceptionHandling
IDictionary data = new Dictionary();
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -242,7 +210,7 @@ namespace System.Web.Http.ExceptionHandling
IDictionary data = new Dictionary();
data.Add(ExceptionLogger.LoggedByKey, loggedBy);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -267,7 +235,7 @@ namespace System.Web.Http.ExceptionHandling
ICollection<object> loggedBy = new List<object>() { product };
data.Add(ExceptionLogger.LoggedByKey, loggedBy);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -291,7 +259,7 @@ namespace System.Web.Http.ExceptionHandling
IDictionary data = new Dictionary();
data.Add(ExceptionLogger.LoggedByKey, null);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateValidContext(exception);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
@ -309,11 +277,6 @@ namespace System.Web.Http.ExceptionHandling
return source.Task;
}
private static ExceptionLoggerContext CreateContext()
{
return CreateContext(CreateExceptionContext());
}
private static ExceptionLoggerContext CreateContext(ExceptionContext exceptionContext)
{
return new ExceptionLoggerContext(exceptionContext);
@ -326,9 +289,9 @@ namespace System.Web.Http.ExceptionHandling
return mock.Object;
}
private static ExceptionContext CreateExceptionContext()
private static ExceptionContext CreateMinimalValidExceptionContext()
{
return new ExceptionContext();
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static CancellationTokenSource CreateTokenSource()
@ -336,17 +299,14 @@ namespace System.Web.Http.ExceptionHandling
return new CancellationTokenSource();
}
private static ExceptionLoggerContext CreateValidContext()
private static ExceptionLoggerContext CreateMinimalValidLoggerContext()
{
return CreateValidContext(new Exception());
return CreateMinimalValidLoggerContext(new Exception());
}
private static ExceptionLoggerContext CreateValidContext(Exception exception)
private static ExceptionLoggerContext CreateMinimalValidLoggerContext(Exception exception)
{
return CreateContext(new ExceptionContext
{
Exception = exception
});
return CreateContext(new ExceptionContext(exception, ExceptionCatchBlocks.HttpServer));
}
private class Dictionary : DictionaryBase

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

@ -95,16 +95,16 @@ namespace System.Web.Http.ExceptionHandling
MediaTypeFormatter expectedFormatter = CreateDummyFormatter();
configuration.Formatters.Add(expectedFormatter);
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(
exception: expectedException,
catchBlock: CreateTopLevelCatchBlock(),
request: expectedRequest)
{
Exception = expectedException,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
{
Configuration = configuration,
IncludeErrorDetail = includeDetail
},
Request = expectedRequest
{
Configuration = configuration,
IncludeErrorDetail = includeDetail
},
});
CancellationToken cancellationToken = CancellationToken.None;
@ -138,16 +138,16 @@ namespace System.Web.Http.ExceptionHandling
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
ExceptionContext context = new ExceptionContext
{
Exception = exception,
CatchBlock = CreateNonTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
ExceptionContext context = new ExceptionContext(
exception,
CreateNonTopLevelCatchBlock(),
request)
{
Configuration = configuration
},
Request = request
};
RequestContext = new HttpRequestContext
{
Configuration = configuration
},
};
// More Arrange; then Act & Assert
TestHandleAsyncLeavesResultNull(context);
@ -164,56 +164,6 @@ namespace System.Web.Http.ExceptionHandling
TestHandleAsyncLeavesResultNull(context);
}
[Fact]
public void HandleAsync_IfExceptionIsNull_LeavesResultNull()
{
// Arrange
Exception exception = null;
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
ExceptionContext context = new ExceptionContext
{
Exception = exception,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
{
Configuration = configuration
},
Request = request
};
// More Arrange; then Act & Assert
TestHandleAsyncLeavesResultNull(context);
}
}
[Fact]
public void HandleAsync_IfCatchBlockIsNull_LeavesResultNull()
{
// Arrange
Exception exception = CreateDummyException();
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
ExceptionContext context = new ExceptionContext
{
Exception = exception,
CatchBlock = null,
RequestContext = new HttpRequestContext
{
Configuration = configuration
},
Request = request
};
// More Arrange; then Act & Assert
TestHandleAsyncLeavesResultNull(context);
}
}
[Fact]
public void HandleAsync_IfRequestIsNull_LeavesResultNull()
{
@ -222,10 +172,10 @@ namespace System.Web.Http.ExceptionHandling
using (HttpConfiguration configuration = CreateConfiguration())
{
ExceptionContext context = new ExceptionContext
ExceptionContext context = new ExceptionContext(
exception,
CreateTopLevelCatchBlock())
{
Exception = exception,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
{
Configuration = configuration
@ -246,10 +196,10 @@ namespace System.Web.Http.ExceptionHandling
using (HttpRequestMessage request = CreateRequest())
{
ExceptionContext context = new ExceptionContext
ExceptionContext context = new ExceptionContext(
exception,
CreateTopLevelCatchBlock())
{
Exception = exception,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = null,
Request = request
};
@ -267,10 +217,10 @@ namespace System.Web.Http.ExceptionHandling
using (HttpRequestMessage request = CreateRequest())
{
ExceptionContext context = new ExceptionContext
ExceptionContext context = new ExceptionContext(
exception,
CreateTopLevelCatchBlock())
{
Exception = exception,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
{
Configuration = null
@ -294,10 +244,10 @@ namespace System.Web.Http.ExceptionHandling
{
configuration.Services.Clear(typeof(IContentNegotiator));
ExceptionContext context = new ExceptionContext
ExceptionContext context = new ExceptionContext(
exception,
CreateTopLevelCatchBlock())
{
Exception = exception,
CatchBlock = CreateTopLevelCatchBlock(),
RequestContext = new HttpRequestContext
{
Configuration = configuration
@ -362,7 +312,12 @@ namespace System.Web.Http.ExceptionHandling
private static ExceptionHandlerContext CreateContext()
{
return new ExceptionHandlerContext(new ExceptionContext());
return new ExceptionHandlerContext(CreateMinimalValidExceptionContext());
}
private static ExceptionContext CreateMinimalValidExceptionContext()
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext)

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

@ -246,8 +246,9 @@ namespace System.Web.Http.WebHost
// Arrange
using (HttpResponseMessage response = CreateResponse())
{
ExceptionHandlerContext context = CreateValidContext(null,
WebHostExceptionCatchBlocks.HttpControllerHandlerBufferContent);
ExceptionHandlerContext context = CreateContext(
CreateMinimalValidExceptionContext(WebHostExceptionCatchBlocks.HttpControllerHandlerBufferContent));
Assert.Null(context.ExceptionContext.Request); // Guard
CancellationToken cancellationToken = CancellationToken.None;
@ -364,7 +365,7 @@ namespace System.Web.Http.WebHost
private static ExceptionHandlerContext CreateContext()
{
return CreateContext(new ExceptionContext());
return CreateContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
}
private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext)
@ -406,12 +407,15 @@ namespace System.Web.Http.WebHost
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request,
ExceptionContextCatchBlock catchBlock)
{
return CreateContext(new ExceptionContext
{
Exception = new InvalidOperationException(),
CatchBlock = catchBlock,
Request = request
});
return CreateContext(CreateMinimalValidExceptionContext(catchBlock, request));
}
private static ExceptionContext CreateMinimalValidExceptionContext(ExceptionContextCatchBlock catchBlock, HttpRequestMessage request = null)
{
return new ExceptionContext(new InvalidOperationException(), catchBlock)
{
Request = request,
};
}
}
}