public void FormatWritesTheTransformedReport() { string resourcePath = Path.Combine(Path.GetDirectoryName(AssemblyUtils.GetAssemblyLocalPath(GetType().Assembly)), @"..\Reports"); IReportWriter reportWriter = Mocks.StrictMock <IReportWriter>(); IReportContainer reportContainer = Mocks.StrictMock <IReportContainer>(); IProgressMonitor progressMonitor = NullProgressMonitor.CreateInstance(); string reportPath = SpecialPathPolicy.For <XsltReportFormatter>().CreateTempFileWithUniqueName().FullName; using (Stream tempFileStream = File.OpenWrite(reportPath)) { using (Mocks.Record()) { SetupResult.For(reportWriter.ReportContainer).Return(reportContainer); reportWriter.SerializeReport(null, AttachmentContentDisposition.Link); LastCall.Constraints(Is.NotNull(), Is.Equal(AttachmentContentDisposition.Link)) .Do((SerializeReportDelegate) delegate(XmlWriter writer, AttachmentContentDisposition contentDisposition) { XmlDocument doc = new XmlDocument(); doc.InnerXml = "<report>The report.</report>"; doc.Save(writer); }); SetupResult.For(reportContainer.ReportName).Return("Foo"); Expect.Call(reportContainer.OpenWrite("Foo.ext", MimeTypes.PlainText, new UTF8Encoding(false))) .Return(tempFileStream); reportWriter.AddReportDocumentPath("Foo.ext"); Expect.Call(reportContainer.OpenWrite(@"Foo\MbUnitLogo.png", MimeTypes.Png, null)).Return(new MemoryStream()); reportWriter.SaveReportAttachments(null); LastCall.Constraints(Is.NotNull()); } using (Mocks.Playback()) { XsltReportFormatter formatter = new XsltReportFormatter("ext", MimeTypes.PlainText, new DirectoryInfo(resourcePath), "Diagnostic.xslt", new string[] { "MbUnitLogo.png" }); var reportFormatterOptions = new ReportFormatterOptions(); reportFormatterOptions.AddProperty(XsltReportFormatter.AttachmentContentDispositionOption, AttachmentContentDisposition.Link.ToString()); formatter.Format(reportWriter, reportFormatterOptions, progressMonitor); string reportContents = File.ReadAllText(reportPath); TestLog.EmbedXml("Diagnostic report contents", reportContents); Assert.Contains(reportContents, "<resourceRoot>Foo</resourceRoot>"); Assert.Contains(reportContents, "The report."); File.Delete(reportPath); } } }
/// <inheritdoc /> public void SaveReport(AttachmentContentDisposition attachmentContentDisposition, IProgressMonitor progressMonitor) { if (progressMonitor == null) { throw new ArgumentNullException(@"progressMonitor"); } if (reportSaved) { return; } int attachmentCount = CountAttachments(report); using (progressMonitor.BeginTask("Saving report.", attachmentCount + 1)) { var encoding = new UTF8Encoding(false); var settings = new XmlWriterSettings(); settings.CheckCharacters = false; settings.Indent = true; settings.Encoding = encoding; settings.CloseOutput = true; string reportPath = reportContainer.ReportName + @".xml"; progressMonitor.ThrowIfCanceled(); progressMonitor.SetStatus(reportPath); using (XmlWriter writer = XmlWriter.Create(reportContainer.OpenWrite(reportPath, MimeTypes.Xml, settings.Encoding), settings)) { progressMonitor.ThrowIfCanceled(); SerializeReport(writer, attachmentContentDisposition); } progressMonitor.Worked(1); progressMonitor.SetStatus(@""); if (attachmentContentDisposition == AttachmentContentDisposition.Link && attachmentCount != 0) { progressMonitor.ThrowIfCanceled(); using (IProgressMonitor subProgressMonitor = progressMonitor.CreateSubProgressMonitor(attachmentCount)) SaveReportAttachments(subProgressMonitor); } AddReportDocumentPath(reportPath); reportSaved = true; } }
/// <summary> /// Recursively copies files and folders from the source path in the native /// file system to the destination path within the report container. /// </summary> /// <param name="container">The container.</param> /// <param name="sourcePathInFileSystem">The source file or directory path in the native file system.</param> /// <param name="destPathInContainer">The destination file or directory path in the report container.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="container" />, /// <paramref name="sourcePathInFileSystem"/> or <paramref name="destPathInContainer"/> is null.</exception> public static void CopyToReport(IReportContainer container, string sourcePathInFileSystem, string destPathInContainer) { FileUtils.CopyAllIndirect(sourcePathInFileSystem, destPathInContainer, null, delegate(string sourceFilePath, string destFilePath) { using (Stream sourceStream = File.OpenRead(sourceFilePath)) { using (Stream destStream = container.OpenWrite(destFilePath, MimeTypes.GetMimeTypeByExtension(Path.GetExtension(sourceFilePath)), null)) { FileUtils.CopyStreamContents(sourceStream, destStream); } } }); }
public void FormatWritesTheArchivedReport() { IReportWriter reportWriter = Mocks.StrictMock <IReportWriter>(); IReportContainer reportContainer = Mocks.StrictMock <IReportContainer>(); IReportFormatter htmlReportFormatter = Mocks.StrictMock <IReportFormatter>(); IProgressMonitor progressMonitor = NullProgressMonitor.CreateInstance(); var reportFormatterOptions = new ReportFormatterOptions(); string reportPath = SpecialPathPolicy.For <MHtmlReportFormatterTest>().CreateTempFileWithUniqueName().FullName; using (Stream tempFileStream = File.OpenWrite(reportPath)) { using (Mocks.Record()) { SetupResult.For(reportWriter.ReportContainer).Return(reportContainer); SetupResult.For(reportWriter.Report).Return(new Report()); Expect.Call(reportContainer.EncodeFileName(null)) .Repeat.Any() .IgnoreArguments() .Do((Gallio.Common.GallioFunc <string, string>) delegate(string value) { return(value); }); SetupResult.For(reportContainer.ReportName).Return("Foo"); Expect.Call(reportContainer.OpenWrite("Foo.mht", MimeTypes.MHtml, new UTF8Encoding(false))) .Return(tempFileStream); reportWriter.AddReportDocumentPath("Foo.mht"); Expect.Call(delegate { htmlReportFormatter.Format(null, null, null); }) .Constraints(Is.NotNull(), Is.Same(reportFormatterOptions), Is.NotNull()) .Do((FormatDelegate) delegate(IReportWriter innerReportWriter, ReportFormatterOptions innerFormatterOptions, IProgressMonitor innerProgressMonitor) { using (StreamWriter contentWriter = new StreamWriter(innerReportWriter.ReportContainer.OpenWrite("Foo.html", MimeTypes.Html, Encoding.UTF8))) contentWriter.Write("<html><body>Some HTML</body></html>"); using (StreamWriter contentWriter = new StreamWriter(innerReportWriter.ReportContainer.OpenWrite( innerReportWriter.ReportContainer.EncodeFileName("Foo\\Attachment 1%.txt"), MimeTypes.PlainText, Encoding.UTF8))) contentWriter.Write("An attachment."); using (StreamWriter contentWriter = new StreamWriter(innerReportWriter.ReportContainer.OpenWrite("Foo.css", null, null))) contentWriter.Write("#Some CSS."); }); } using (Mocks.Playback()) { MHtmlReportFormatter formatter = new MHtmlReportFormatter(htmlReportFormatter); formatter.Format(reportWriter, reportFormatterOptions, progressMonitor); string reportContents = File.ReadAllText(reportPath); TestLog.AttachPlainText("MHTML Report", reportContents); Assert.Contains(reportContents, "MIME-Version: 1.0"); Assert.Contains(reportContents, "Content-Type: multipart/related; type=\"text/html\"; boundary="); Assert.Contains(reportContents, "This is a multi-part message in MIME format."); Assert.Contains(reportContents, "text/html"); Assert.Contains(reportContents, "Content-Location: file:///Foo.html"); Assert.Contains(reportContents, Convert.ToBase64String(Encoding.UTF8.GetBytes("<html><body>Some HTML</body></html>"), Base64FormattingOptions.InsertLineBreaks)); Assert.Contains(reportContents, "text/plain"); Assert.Contains(reportContents, "Content-Location: file:///Foo/Attachment_1%25.txt"); Assert.Contains(reportContents, Convert.ToBase64String(Encoding.UTF8.GetBytes("An attachment."), Base64FormattingOptions.InsertLineBreaks)); Assert.Contains(reportContents, "text/css"); Assert.Contains(reportContents, "Content-Location: file:///Foo.css"); Assert.Contains(reportContents, Convert.ToBase64String(Encoding.UTF8.GetBytes("#Some CSS."), Base64FormattingOptions.InsertLineBreaks)); File.Delete(reportPath); } } }