/// <summary> /// Runs the actual test action against the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to execute the test action against.</param> protected override void RunTestAction(BatchReaderStreamWrapper streamWrapper) { bool isEndBoundary, isParentBoundary; bool success = streamWrapper.SkipToBoundary(out isEndBoundary, out isParentBoundary); this.VerifyResult(streamWrapper, success, isEndBoundary, isParentBoundary); }
/// <summary> /// Runs the test case. /// </summary> public override void Run() { BatchReaderStreamBufferWrapper streamBuffer = new BatchReaderStreamBufferWrapper(); MemoryStreamBatchPayloadBuilder payloadBuilder = new MemoryStreamBatchPayloadBuilder(this.Encoding, this.LineFeedChars); // If no explicit payload func was specified, use a default payload MemoryStream memoryStream; if (this.PayloadFunc == null) { memoryStream = payloadBuilder.FillBytes(BatchReaderStreamBufferWrapper.BufferLength).ResetMemoryStream(); } else { memoryStream = this.PayloadFunc(payloadBuilder); } // Create a message reader and then a batch reader for the message using (ODataMessageReader messageReader = this.CreateMessageReader(memoryStream)) { ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); BatchReaderStreamWrapper streamWrapper = new BatchReaderStreamWrapper(batchReader); Exception exception = TestExceptionUtils.RunCatching(() => { this.RunTestAction(streamWrapper); }); this.VerifyException(exception); } }
/// <summary> /// Runs the test action of this test after setting up the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream to test.</param> protected override void RunTestAction(BatchReaderStreamWrapper streamWrapper) { // Set a batch encoding since we assert that it is set before callign ProcessPartHeader streamWrapper.SetBatchEncoding(this.Encoding); bool isChangeSetPart = streamWrapper.ProcessPartHeader(); this.VerifyResult(streamWrapper, isChangeSetPart); }
/// <summary> /// Verifies the result of a boundary header validation test case. /// </summary> /// <param name="streamWrapper">The stream buffer used to read the headers from.</param> /// <param name="isChangeSetPart">true if we detected a changeset part; otherwise false.</param> private void VerifyResult(BatchReaderStreamWrapper streamWrapper, bool isChangeSetPart) { base.VerifyResult(streamWrapper.BatchBuffer); if (this.ExpectChangeSetPart.HasValue) { this.Assert.AreEqual(this.ExpectChangeSetPart.Value, isChangeSetPart, string.Format("\r\n{0}:\r\nExpected changeset part is '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectChangeSetPart.Value, isChangeSetPart)); } this.Assert.AreEqual(this.ExpectedChangeSetBoundary, streamWrapper.ChangeSetBoundary, string.Format("\r\n{0}:\r\nExpected changeset boundary '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectedChangeSetBoundary, streamWrapper.ChangeSetBoundary)); }
/// <summary> /// Runs the actual test action against the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to execute the test action against.</param> protected override void RunTestAction(BatchReaderStreamWrapper streamWrapper) { this.Assert.IsNotNull(this.ReadDescriptors, "Must have non-null read descriptors."); // Set a batch encoding since we assert that it is set before callign ProcessPartHeader streamWrapper.SetBatchEncoding(this.Encoding); foreach (BatchReaderStreamReadDescriptor readDescriptor in this.ReadDescriptors) { byte[] buffer = new byte[readDescriptor.BufferSize]; int actualBytesRead = streamWrapper.ReadWithDelimiter(buffer, readDescriptor.BufferOffset, readDescriptor.NumberOfBytesToRead); base.VerifyResult(readDescriptor, buffer, actualBytesRead); } base.VerifyResult(streamWrapper.BatchBuffer); }
/// <summary> /// Runs the actual test action against the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to execute the test action against.</param> protected override void RunTestAction(BatchReaderStreamWrapper streamWrapper) { this.Assert.IsTrue(this.ReadIterationCount > 0, "Must read at least once."); // Set a batch encoding since we assert that it is set before callign ProcessPartHeader streamWrapper.SetBatchEncoding(this.Encoding); string line = null; for (int i = 0; i < this.ReadIterationCount; ++i) { line = streamWrapper.ReadLine(); } this.VerifyResult(streamWrapper, line); }
/// <summary> /// Verifies the result of executing the test action. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to verify the result against.</param> /// <param name="line">The line read from the stream.</param> private void VerifyResult(BatchReaderStreamWrapper streamWrapper, string line) { this.Assert.IsTrue(this.ReadIterationCount > 0, "Must have read at least once."); base.VerifyResult(streamWrapper.BatchBuffer); this.Assert.IsNotNull(this.payloadStream, "Must have a payload stream."); this.payloadStream.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[this.payloadStream.Length]; Buffer.BlockCopy(this.payloadStream.GetBuffer(), 0, bytes, 0, (int)this.payloadStream.Length); string expectedLine = this.Encoding.GetString(bytes); string lineFeed = new string(this.LineFeedChars); int lineFeedLength = this.LineFeedChars.Length; int startIndex = -lineFeedLength; int endIndex = -lineFeedLength; for (int i = 0; i < this.ReadIterationCount; ++i) { startIndex = endIndex + lineFeedLength; endIndex = expectedLine.IndexOf(lineFeed, startIndex); if (endIndex < 0) { break; } } startIndex = Math.Max(startIndex, 0); if (endIndex >= 0) { expectedLine = expectedLine.Substring(startIndex, endIndex - startIndex); } else { expectedLine = expectedLine.Substring(startIndex); if (String.IsNullOrEmpty(expectedLine)) { expectedLine = null; } } this.Assert.AreEqual(expectedLine, line, string.Format("\r\n{0}:\r\nExpected to read line '{1}' but reported value is '{2}'.", this.DebugDescription, expectedLine, line)); }
/// <summary> /// Verifies that the resulting stream buffer is in the expected state. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to execute the test action against.</param> /// <param name="foundBoundary">true if we found the boundary; otherwise false.</param> /// <param name="isEndBoundary">true if we detected an end boundary; otherwise false.</param> private void VerifyResult(BatchReaderStreamWrapper streamWrapper, bool foundBoundary, bool isEndBoundary, bool isParentBoundary) { if (this.ExpectedBoundaryFound.HasValue) { this.Assert.AreEqual(this.ExpectedBoundaryFound.Value, foundBoundary, string.Format("\r\n{0}:\r\nExpected 'foundBoundary' to be '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectedBoundaryFound.Value, foundBoundary)); } if (this.ExpectedIsEndBoundary.HasValue) { this.Assert.AreEqual(this.ExpectedIsEndBoundary.Value, isEndBoundary, string.Format("\r\n{0}:\r\nExpected 'isEndBoundary' to be '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectedIsEndBoundary.Value, isEndBoundary)); } if (this.ExpectedIsParentBoundary.HasValue) { this.Assert.AreEqual(this.ExpectedIsParentBoundary.Value, isParentBoundary, string.Format("\r\n{0}:\r\nExpected 'isParentBoundary' to be '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectedIsParentBoundary.Value, isParentBoundary)); } base.VerifyResult(streamWrapper.BatchBuffer); }
/// <summary> /// Verifies the result of reading the part headers. /// </summary> /// <param name="streamWrapper">The stream buffer used to read the headers from.</param> /// <param name="headers">The headers read from the part.</param> /// <param name="isChangeSetPart">true if we detected a changeset part; otherwise false.</param> private void VerifyResult(BatchReaderStreamWrapper streamWrapper, BatchOperationHeadersWrapper headers, bool isChangeSetPart) { base.VerifyResult(streamWrapper.BatchBuffer); if (this.ExpectChangeSetPart.HasValue) { this.Assert.AreEqual(this.ExpectChangeSetPart.Value, isChangeSetPart, string.Format("\r\n{0}:\r\nExpected changeset part is '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectChangeSetPart.Value, isChangeSetPart)); } if (this.ExpectedHeaders != null) { this.Assert.IsNotNull(headers, string.Format("Expected {0} headers but none were found.", this.ExpectedHeaders.Count())); foreach (KeyValuePair <string, string> expectedHeader in this.ExpectedHeaders) { // check that the expected header is present string actualHeaderValue; if (headers.TryGetValue(expectedHeader.Key, out actualHeaderValue)) { this.Assert.AreEqual(expectedHeader.Value, actualHeaderValue, string.Format("Expected value '{0}' for header '{1}' but found '{2}'.", expectedHeader.Value, expectedHeader.Key, actualHeaderValue)); } else { this.Assert.Fail(string.Format("Did not find expected header '{0}'.", expectedHeader.Key)); } } if (headers.Any(kvp => !this.ExpectedHeaders.Any(kvp2 => string.Compare(kvp.Key, kvp2.Key, /*ignoreCase*/ true) == 0))) { string expectedHeaders = string.Join(", ", this.ExpectedHeaders.Select(kvp => kvp.Key).ToArray()); string actualHeaders = string.Join(", ", headers.Select(kvp => kvp.Key).ToArray()); this.Assert.Fail("Found unexpected headers in the message headers.\r\n" + "Expected headers: " + expectedHeaders + "\r\n" + "Actual headers: " + actualHeaders); } } }
/// <summary> /// Runs the test action of this test after setting up the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream to test.</param> protected abstract void RunTestAction(BatchReaderStreamWrapper streamWrapper);
/// <summary> /// Runs the actual test action against the batch reader stream. /// </summary> /// <param name="streamWrapper">The batch reader stream wrapper to execute the test action against.</param> protected override void RunTestAction(BatchReaderStreamWrapper streamWrapper) { this.Assert.IsNotNull(this.ReadDescriptors, "Must have non-null read descriptors."); // Set a batch encoding since we assert that it is set before callign ProcessPartHeader streamWrapper.SetBatchEncoding(this.Encoding); foreach (BatchReaderStreamReadDescriptor readDescriptor in this.ReadDescriptors) { byte[] buffer = new byte[readDescriptor.BufferSize]; int actualBytesRead = streamWrapper.ReadWithLength(buffer, readDescriptor.BufferOffset, readDescriptor.NumberOfBytesToRead); this.VerifyResult(readDescriptor, buffer, actualBytesRead); } base.VerifyResult(streamWrapper.BatchBuffer); }
/// <summary> /// Verifies the result of reading the part headers. /// </summary> /// <param name="streamWrapper">The stream buffer used to read the headers from.</param> /// <param name="headers">The headers read from the part.</param> /// <param name="isChangeSetPart">true if we detected a changeset part; otherwise false.</param> private void VerifyResult(BatchReaderStreamWrapper streamWrapper, BatchOperationHeadersWrapper headers, bool isChangeSetPart) { base.VerifyResult(streamWrapper.BatchBuffer); if (this.ExpectChangeSetPart.HasValue) { this.Assert.AreEqual(this.ExpectChangeSetPart.Value, isChangeSetPart, string.Format("\r\n{0}:\r\nExpected changeset part is '{1}' but reported value is '{2}'.", this.DebugDescription, this.ExpectChangeSetPart.Value, isChangeSetPart)); } if (this.ExpectedHeaders != null) { this.Assert.IsNotNull(headers, string.Format("Expected {0} headers but none were found.", this.ExpectedHeaders.Count())); foreach (KeyValuePair<string, string> expectedHeader in this.ExpectedHeaders) { // check that the expected header is present string actualHeaderValue; if (headers.TryGetValue(expectedHeader.Key, out actualHeaderValue)) { this.Assert.AreEqual(expectedHeader.Value, actualHeaderValue, string.Format("Expected value '{0}' for header '{1}' but found '{2}'.", expectedHeader.Value, expectedHeader.Key, actualHeaderValue)); } else { this.Assert.Fail(string.Format("Did not find expected header '{0}'.", expectedHeader.Key)); } } if (headers.Any(kvp => !this.ExpectedHeaders.Any(kvp2 => string.Compare(kvp.Key, kvp2.Key, /*ignoreCase*/true) == 0))) { string expectedHeaders = string.Join(", ", this.ExpectedHeaders.Select(kvp => kvp.Key).ToArray()); string actualHeaders = string.Join(", ", headers.Select(kvp => kvp.Key).ToArray()); this.Assert.Fail("Found unexpected headers in the message headers.\r\n" + "Expected headers: " + expectedHeaders + "\r\n" + "Actual headers: " + actualHeaders); } } }