/// <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)); }
/// <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); }
/// <summary> /// TestStepBase.Execute() implementation /// </summary> /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param> public override void Execute(Context context) { var endTime = DateTime.Now.AddMilliseconds(Timeout); string[] filelist; SearchPattern = GetFormattedSearchQuery(context); context.LogInfo("Searching directory: {0}, search pattern: {1}", DirectoryPath, SearchPattern); do { Thread.Sleep(100); if (CreatedAfter.HasValue) { var di = new DirectoryInfo(DirectoryPath); var fis = di.GetFiles(SearchPattern); var fileList = (from fi in fis where _createdAfter != null where fi.LastAccessTimeUtc > CreatedAfter.Value select fi.FullName).ToList(); filelist = fileList.ToArray(); } else { // Get the list of files in the directory filelist = Directory.GetFiles(DirectoryPath, SearchPattern); } if (filelist.Length == ExpectedNumberOfFiles) { break; } } while (endTime > DateTime.Now); context.LogInfo("Number of files found: {0}", filelist.Length); if (filelist.Length == 0) { // Expecting more than one file throw new ApplicationException(String.Format("Directory contains no files matching the pattern!")); } if (0 < ExpectedNumberOfFiles && filelist.Length != ExpectedNumberOfFiles) { // Expecting a specified number of files throw new ApplicationException(String.Format("Directory contained: {0} files, but the step expected: {1} files", filelist.Length, ExpectedNumberOfFiles)); } // For each file in the file list foreach (var filePath in filelist) { context.LogInfo("FileReadMultipleStep validating file: {0}", filePath); // add the current filepath to the context for validation based on file properties context.Add("validatingFilePath", filePath); Stream fileData = StreamHelper.LoadFileToStream(filePath, Timeout); context.LogData("File: " + filePath, fileData, NumberOfCharsToLog); fileData.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 fileData = subStep.Execute(fileData, context); } catch (Exception ex) { context.LogException(ex); throw; } } if (DeleteFiles) { System.IO.File.Delete(filePath); } // remove the current filepath from the context context.Remove("validatingFilePath"); } }
/// <summary> /// Helper method to load a disc FILE into a string /// </summary> /// <param name="filePath">The path to the FILE containing the data</param> /// <returns>string containing the data in the FILE</returns> public static string LoadFileToString(string filePath) { var ms = StreamHelper1.LoadFileToStream(filePath); return(StreamHelper.WriteStreamToString(ms)); }