/// <summary> /// Helper method to load a disc FILE into a MemoryStream (as unicode string) /// </summary> /// <param name="filePath">The path to the FILE containing the data</param> /// <returns>MemoryStream containing the data in the FILE</returns> public static MemoryStream LoadFileToStream(string filePath) { var ms = StreamHelper1.LoadFileToStream(filePath); var strm = StreamHelper1.EncodeStream(ms, Encoding.Unicode); return(StreamHelper1.LoadMemoryStream(strm)); }
public override void Execute(Context context) { MemoryStream msgData = null; var queuePath = QueuePath; var timeout = TimeOut; try { var queue = new MessageQueue(MSMQHelper.DeNormalizeQueueName(queuePath)); // Receive msg from queue... if (BodyType != VarEnum.VT_EMPTY) { queue.Formatter = new ActiveXMessageFormatter(); } var msg = queue.Receive(TimeSpan.FromMilliseconds(timeout), MessageQueueTransactionType.Single); if (msg == null) { throw new Exception("No message read!"); } // Dump msg content to console... msgData = StreamHelper.LoadMemoryStream(msg.BodyStream); StreamHelper.WriteStreamToConsole("MSMQ message data", msgData, context); // Validate data... msgData.Seek(0, SeekOrigin.Begin); // Check it against the validate steps to see if it matches one of them foreach (var subStep in SubSteps) { try { // Try the validation and catch the exception var strm = subStep.Execute(msgData, context); } catch (Exception ex) { context.LogException(ex); throw; } } ProcessContextProperties(context, ContextProperties, msg); } finally { if (null != msgData) { msgData.Close(); } } }
public override void Execute(Context context) { var queuePath = QueuePath; var timeout = TimeOut; var queue = new MessageQueue(MSMQHelper.DeNormalizeQueueName(queuePath)); var c = queue.CreateCursor(); // Count msgs in queue... var numberOfMessages = 0; var msg = PeekWithTimeout(queue, timeout, c, PeekAction.Current); if (null != msg) { numberOfMessages = 1; while ((msg = PeekWithoutTimeout(queue, c, PeekAction.Next)) != null) { numberOfMessages++; // Dump msg content to console... var msgData = StreamHelper.LoadMemoryStream(msg.BodyStream); StreamHelper.WriteStreamToConsole("MSMQ message data", msgData, context); msgData.Close(); } } context.LogInfo("Number of messages found: {0}, in queue '{1}'", numberOfMessages, QueuePath); switch (ExpectedNumberOfMessages) { case -1: break; default: if (ExpectedNumberOfMessages != numberOfMessages) { throw new Exception(String.Format("Queue contained: {0} messages, but the step expected: {1} messages", numberOfMessages, ExpectedNumberOfMessages)); } break; } }
/// <summary> /// IValidationStep.ExecuteValidation() implementation /// </summary> /// <param name='data'>The stream cintaining the data to be validated.</param> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override Stream Execute(Stream data, Context context) { MemoryStream dataToValidateAgainst = null; try { try { if (ReadAsString) { dataToValidateAgainst = StreamHelper.LoadMemoryStream(Common.StreamHelper.LoadFileToString(_comparisonDataPath)); } else { dataToValidateAgainst = !ReadAsUnicode?StreamHelper.LoadFileToStream(_comparisonDataPath) : Common.StreamHelper.LoadFileToStream(_comparisonDataPath); } } catch (Exception e) { context.LogError("BinaryValidationStep failed, exception caugh trying to open comparison file: {0}", _comparisonDataPath); context.LogException(e); throw; } try { data.Seek(0, SeekOrigin.Begin); dataToValidateAgainst.Seek(0, SeekOrigin.Begin); if (_compareAsUtf8) { // Compare the streams, make sure we are comparing like for like StreamHelper.CompareStreams(StreamHelper.EncodeStream(data, System.Text.Encoding.UTF8), StreamHelper.EncodeStream(dataToValidateAgainst, System.Text.Encoding.UTF8)); } else { StreamHelper.CompareStreams(data, dataToValidateAgainst); } } catch (Exception e) { context.LogError("Binary validation failed while comparing the two data streams with the following exception: {0}", e.ToString()); // Dump out streams for validation... data.Seek(0, SeekOrigin.Begin); dataToValidateAgainst.Seek(0, SeekOrigin.Begin); context.LogData("Stream 1:", data); context.LogData("Stream 2:", dataToValidateAgainst); throw; } } finally { if (null != dataToValidateAgainst) { dataToValidateAgainst.Close(); } } return(data); }