コード例 #1
0
 public void GetAttachmentReturnsNamedAttachment()
 {
     var log = new StructuredDocument();
     var data = new AttachmentData("foo", MimeTypes.Binary, AttachmentType.Binary, null, new byte[0]);
     log.Attachments.Add(data);
     log.Attachments.Add(new AttachmentData("bar", MimeTypes.Binary, AttachmentType.Binary, null, new byte[0]));
     Assert.AreSame(data, log.GetAttachment("foo"));
 }
コード例 #2
0
 public void FromAttachmentData_Text()
 {
     var attachmentData = new AttachmentData("name", MimeTypes.PlainText, AttachmentType.Text, "content", null);
     var attachment = (TextAttachment)Attachment.FromAttachmentData(attachmentData);
     Assert.AreEqual("name", attachment.Name);
     Assert.AreEqual(MimeTypes.PlainText, attachment.ContentType);
     Assert.AreEqual("content", attachment.Text);
 }
コード例 #3
0
 public void FromAttachmentData_Binary()
 {
     byte[] bytes = new byte[] { 1, 2, 3 };
     var attachmentData = new AttachmentData("name", MimeTypes.Binary, AttachmentType.Binary, null, bytes);
     var attachment = (BinaryAttachment)Attachment.FromAttachmentData(attachmentData);
     Assert.AreEqual("name", attachment.Name);
     Assert.AreEqual(MimeTypes.Binary, attachment.ContentType);
     Assert.AreEqual(bytes, attachment.Bytes);
 }
コード例 #4
0
ファイル: Attachment.cs プロジェクト: citizenmatt/gallio
        /// <summary>
        /// Recovers the attachment information from serializable attachment data.
        /// </summary>
        /// <param name="data">The attachment data.</param>
        /// <returns>The attachment.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="data"/> is null.</exception>
        public static Attachment FromAttachmentData(AttachmentData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            return((data.Type == AttachmentType.Text)
                ? (Attachment) new TextAttachment(data.Name, data.ContentType, data.GetText())
                : (Attachment) new BinaryAttachment(data.Name, data.ContentType, data.GetBytes()));
        }
コード例 #5
0
 private void SaveAttachmentContents(AttachmentData attachmentData, string attachmentPath)
 {
     var encoding = new UTF8Encoding(false);
     using (Stream attachmentStream = reportContainer.OpenWrite(attachmentPath, attachmentData.ContentType, encoding))
         attachmentData.SaveContents(attachmentStream, encoding);
 }
コード例 #6
0
        public static void AreEqual(AttachmentData expected, AttachmentData actual)
        {
            if (expected == null)
            {
                Assert.IsNull(actual);
                return;
            }

            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.ContentType, actual.ContentType);
            Assert.AreEqual(expected.Type, actual.Type);
            Assert.AreEqual(expected.ContentPath, actual.ContentPath);
            Assert.AreEqual(expected.SerializedContents, actual.SerializedContents);
        }
コード例 #7
0
        private static string GetAttachmentPath(string stepId, AttachmentData attachment)
        {
            string fileName = FileUtils.EncodeFileName(attachment.Name);
            string extension = MimeTypes.GetExtensionByMimeType(attachment.ContentType);
            if (extension != null)
                fileName += extension;

            return Path.Combine(ReportName, Path.Combine(FileUtils.EncodeFileName(stepId), fileName));
        }
コード例 #8
0
 private FileInfo GetAttachmentFileInfo(string stepId, AttachmentData attachment)
 {
     return cacheGroup.GetFileInfo(GetAttachmentPath(stepId, attachment));
 }
コード例 #9
0
        private void SaveAttachment(string stepId, AttachmentData attachmentData)
        {
            string attachmentPath = GetAttachmentPath(stepId, attachmentData);
            if (attachmentPaths.Contains(attachmentPath))
                return;

            attachmentPaths.Add(attachmentPath);

            using (Stream fs = cacheGroup.OpenFile(attachmentPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
                attachmentData.SaveContents(fs, Encoding.Default);
        }
コード例 #10
0
 private void LoadAttachmentContents(AttachmentData attachmentData, string attachmentPath)
 {
     using (Stream attachmentStream = reportContainer.OpenRead(attachmentPath))
     {
         // TODO: How should we handle missing attachments?  Currently we just throw an exception.
         try
         {
             attachmentData.LoadContents(attachmentStream);
         }
         catch (Exception ex)
         {
             throw new IOException(String.Format(CultureInfo.CurrentCulture,
                 "Unable to load report attachment from file: '{0}'.", attachmentPath), ex);
         }
     }
 }