BlogMLWriterTests.cs :  » Bloggers » SubText » UnitTests » Subtext » BlogMl » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » BlogMl » BlogMLWriterTests.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using BlogML;
using BlogML.Xml;
using MbUnit.Framework;
using Moq;
using Subtext.ImportExport;

namespace UnitTests.Subtext.BlogMl{
    [TestFixture]
    public class BlogMLWriterTests
    {
        [Test]
        public void Write_WithSourceReturningBlog_WritesBlogInfoToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter);
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            source.Setup(s => s.GetBlog()).Returns(new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime});
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<title type=""text""><![CDATA[Subtext Blog]]></title>");
            Assert.Contains(output, @"<sub-title type=""text""><![CDATA[A test blog]]></sub-title>");
            Assert.Contains(output, @"root-url=""http://subtextproject.com/""");
            Assert.Contains(output, @"date-created=""2009-01-23T08:00:00""");
        }

        [Test]
        public void Write_WithSourceReturningBlogWithNullSubtitle_RendersEmptyStringForUrl()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter);
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            source.Setup(s => s.GetBlog()).Returns(new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://example.com/", SubTitle = null, DateCreated = dateTime });
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<sub-title type=""text""><![CDATA[]]></sub-title>");
        }

        [Test]
        public void Write_WithSourceReturningAuthors_WritesAuthorsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog {Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime};
            blog.Authors.Add(new BlogMLAuthor{ ID = "112", Title = "Phineas", Email = "phineas@example.com", Approved = true});
            source.Setup(s => s.GetBlog()).Returns(blog);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<author id=""112""");
            Assert.Contains(output, @"email=""phineas@example.com""");
            Assert.Contains(output, @"approved=""true""");
            Assert.Contains(output, @"<title type=""text""><![CDATA[Phineas]]></title>");
        }

        [Test]
        public void Write_WithBlogContainingExtendedProperties_WritesPropertiesToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            blog.ExtendedProperties.Add(new Pair<string, string>("Color", "Blue"));
            source.Setup(s => s.GetBlog()).Returns(blog);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<property name=""Color"" value=""Blue"" />");
        }

        [Test]
        public void Write_WithBlogContainingCategories_WritesCategoriesToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            blog.Categories.Add(new BlogMLCategory { ID = "221", Title = "Test Category"});
            source.Setup(s => s.GetBlog()).Returns(blog);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<category id=""221""");
            Assert.Contains(output, @"<title type=""text""><![CDATA[Test Category]]></title>");
        }

        [Test]
        public void Write_WithBlogContainingPosts_WritesPostsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var posts = new List<BlogMLPost> {new BlogMLPost {Title = "This is a blog post"}};
            posts[0].Content.Text = "<p>Test</p>";
            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, "<posts>");
            Assert.Contains(output, "</posts>");
            Assert.Contains(output, @"<title type=""text""><![CDATA[This is a blog post]]></title>");
            Assert.Contains(output, @"<content type=""text""><![CDATA[<p>Test</p>]]></content>");
        }

        [Test]
        public void Write_WithBlogContainingBase64EncodedPosts_WritesPostsToWriterAsBase64Encoded()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Content = BlogMLContent.Create("<p>This is a Test</p>", ContentTypes.Base64) };
            var posts = new List<BlogMLPost> { post };
            blog.Posts.Add(post);
            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes("<p>This is a Test</p>")));
            Assert.Contains(output, @"<content type=""base64""><![CDATA[PHA+VGhpcyBpcyBhIFRlc3Q8L3A+]]></content>");
        }

        [Test]
        public void Write_WithBlogContainingPostsWithCategories_WritesPostCategoriesToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            blog.Categories.Add(new BlogMLCategory { ID = "221", Title = "Test Category" });
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost {Title = "This is a blog post"};
            var posts = new List<BlogMLPost> { post };
            post.Categories.Add("221");
            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<categories>");
            Assert.Contains(output, @"<category ref=""221"" />");
            Assert.Contains(output, @"</categories>");
        }

        [Test]
        public void Write_WithBlogContainingPostsWithComments_WritesPostCommentsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Title = "This is a blog post" };
            var posts = new List<BlogMLPost> { post };
            var comment = new BlogMLComment {Title = "Test Comment Title", Content = {Text = "<p>Comment Body</p>"}};
            post.Comments.Add(comment);
            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<title type=""text""><![CDATA[Test Comment Title]]></title>");
            Assert.Contains(output, @"<content type=""text""><![CDATA[<p>Comment Body</p>]]></content>");
        }

        [Test]
        public void Write_WithBlogContainingTrackbacksWithComments_WritesPostTrackbacksToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Title = "This is a blog post" };
            post.Trackbacks.Add(new BlogMLTrackback { Title = "Post Test Trackback", Url = "http://example.com/trackback-source"});
            var posts = new List<BlogMLPost> { post };

            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<title type=""text""><![CDATA[Post Test Trackback]]></title>");
            Assert.Contains(output, @"url=""http://example.com/trackback-source""");
        }

        [Test]
        public void Write_WithBlogContainingEmbeddedAttachmentsWithComments_WritesPostAttachmentsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Title = "This is a blog post" };
            var attachment = new BlogMLAttachment
            {
                Data = new byte[] {1, 2, 3, 4, 5},
                Path = @"c:\\path-to-attachment.jpg",
                Url = "/foo/path-to-attachment.jpg",
                Embedded = true,
                MimeType = "img/jpeg"
            };
            post.Attachments.Add(attachment);
            var posts = new List<BlogMLPost> { post };

            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"external-uri=""c:\\path-to-attachment.jpg""");
            Assert.Contains(output, @"url=""/foo/path-to-attachment.jpg""");
            Assert.Contains(output, @"mime-type=""img/jpeg""");
            Assert.Contains(output, @"embedded=""true""");
            Assert.Contains(output, @"AQIDBAU=</attachment>");
        }

        [Test]
        public void Write_WithBlogContainingNonEmbeddedAttachmentsWithComments_WritesPostAttachmentsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Title = "This is a blog post" };
            var attachment = new BlogMLAttachment
            {
                Path = @"c:\\path-to-attachment.jpg",
                Url = "/foo/path-to-attachment.jpg",
                Embedded = false,
                MimeType = "img/jpeg"
            };
            post.Attachments.Add(attachment);
            var posts = new List<BlogMLPost> { post };

            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<attachment url=""/foo/path-to-attachment.jpg"" mime-type=""img/jpeg"" external-uri=""c:\\path-to-attachment.jpg"" embedded=""false"" />");
        }

        [Test]
        public void Write_WithBlogContainingMultipleAuthors_WritesPostAuthorsToWriter()
        {
            // arrange
            var stringWriter = new StringWriter();
            var xmlWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
            var source = new Mock<IBlogMLSource>();
            var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture);
            var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime };
            blog.Authors.Add(new BlogMLAuthor { ID = "10"});
            blog.Authors.Add(new BlogMLAuthor { ID = "20" });
            source.Setup(s => s.GetBlog()).Returns(blog);
            var post = new BlogMLPost { Title = "This is a blog post" };
            post.Authors.Add(new BlogMLAuthorReference { Ref = "20" });
            var posts = new List<BlogMLPost> { post };

            source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts);
            var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/);

            // act
            ((IBlogMLWriter)writer).Write(xmlWriter);

            // assert
            string output = stringWriter.ToString();
            Assert.Contains(output, @"<author ref=""20"" />");
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.