using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Xml; using MbUnit.Framework; using Rss; namespace MockSubSonic.Tests { [TestFixture] public class TechnorariTests { [Test] public void GetTagListFromPostText() { RssFeed rss = TestHelper.CreateMockRSS(TestHelper.url); RssItemCollection items = rss.Channels[0].Items; string post = items[0].Description; List tags = Technorati.GetTagListFromPost(post); Assert.AreEqual(3, tags.Count); Assert.AreEqual("MbUnit", tags[0]); Assert.AreEqual("TDD", tags[1]); Assert.AreEqual("Testing", tags[2]); } [Test] public void Spike_RegExMatchesString() { Regex r = new Regex("(.+)"); MatchCollection collection = r.Matches("Test Code"); Assert.AreEqual(1, collection.Count, "Collection"); foreach (Match match in collection) { Assert.IsTrue(match.Success); Assert.AreEqual(3, match.Groups.Count, "Captures"); Assert.AreEqual("TestCode", match.Groups[1].Value, "One"); Assert.AreEqual("Test Code", match.Groups[2].Value, "Two"); } } [Test] public void Spike_RegExMatches2Strings() { Regex r = new Regex("(.+?)", RegexOptions.Multiline); MatchCollection collection = r.Matches("Test CodeTest Code"); Assert.AreEqual(2, collection.Count, "Collection"); foreach (Match match in collection) { Assert.IsTrue(match.Success); Assert.AreEqual(3, match.Groups.Count, "Captures"); Assert.AreEqual("TestCode", match.Groups[1].Value, "One"); Assert.AreEqual("Test Code", match.Groups[2].Value, "Two"); } } [Test] public void GetMatchCollectionForHtml() { string post = "Test Code"; MatchCollection collection = Technorati.GetMatches(post); Assert.AreEqual(1, collection.Count, "Collection"); } [Test] public void GetListFromMatches() { string post = "Test Code"; MatchCollection collection = Technorati.GetMatches(post); List tags = Technorati.GetTagList(collection); Assert.AreEqual(1, tags.Count); Assert.AreEqual("TestCode", tags[0]); } [Test] public void GetTechnoratiUrl() { string technoratiAPI = "REMOVED"; int limit = 10; string technoratiTerm = "MbUnit"; string url = string.Format("http://api.technorati.com/tag?key={0}&tag={1}&limit={2}", technoratiAPI, technoratiTerm, limit); string actualUrl = Technorati.GetUrl(technoratiTerm); Assert.AreEqual(url, actualUrl); } [Test] public void GetXmlFromRss() { int limit = 10; string technoratiTerm = "MbUnit"; Technorati.TechnoratiAPI = new TechnoratiStub(); XmlNodeList xmlNodes = Technorati.GetXmlNodeList(Technorati.GetUrl(technoratiTerm)); Assert.AreEqual(limit, xmlNodes.Count); } [Test] public void GivenXmlNodeCreateBlog() { string technoratiTerm = "MbUnit"; Technorati.TechnoratiAPI = new TechnoratiStub(); XmlNodeList xmlNodes = Technorati.GetXmlNodeList(Technorati.GetUrl(technoratiTerm)); Blog blog = Technorati.CreateBlogPost(xmlNodes[0]); Assert.AreEqual(blog.Title, xmlNodes[0]["title"].InnerText); } [Test] public void GetBlogPostListTerm() { string technoratiTerm = "MbUnit"; Technorati.TechnoratiAPI = new TechnoratiStub(); XmlNodeList xmlNodes = Technorati.GetXmlNodeList(Technorati.GetUrl(technoratiTerm)); List blogs = Technorati.CreateBlogPostList(xmlNodes); Assert.AreEqual(10, blogs.Count); } [Test] public void GetBlogPostListForListOfTerms() { List terms = new List(3); terms.Add("MbUnit"); terms.Add("TDD"); terms.Add("Testing"); Technorati.TechnoratiAPI = new TechnoratiStub(); List blogs = Technorati.CreateBlogPostList(terms); Assert.AreEqual(30, blogs.Count); } [Test] public void CreateTechnoratiResponseTest() { TechnoratiStub stub = new TechnoratiStub(); XmlNodeList xmlResponse = stub.GetXmlNodeList(""); Assert.AreEqual(10, xmlResponse.Count); } } public class TechnoratiStub : ITechnoratiAccessor { public XmlNodeList GetXmlNodeList(string technoratiUrl) { #region Xml string cachedXmlResponse = @"" + Environment.NewLine + @"" + Environment.NewLine + @"" + Environment.NewLine + @"" + Environment.NewLine + @"" + Environment.NewLine + @"" + Environment.NewLine + @" MBunit" + Environment.NewLine + @" 10" + Environment.NewLine + @" 10" + Environment.NewLine + @" 1" + Environment.NewLine + @" 10" + Environment.NewLine + @" 00" + Environment.NewLine + @"" + Environment.NewLine + @"" + Environment.NewLine + @" " + Environment.NewLine + @" [name of blog containing match]" + Environment.NewLine + @" [blog URL]" + Environment.NewLine + @" [blog RSS URL]" + Environment.NewLine + @" [blog Atom URL]" + Environment.NewLine + @" [inbound blogs]" + Environment.NewLine + @" [inbound links]" + Environment.NewLine + @" [date blog last updated]" + Environment.NewLine + @" " + Environment.NewLine + @" [entry title]" + Environment.NewLine + @" [entry blurb with term highlighting]" + Environment.NewLine + @" 01/01/2000" + Environment.NewLine + @" [date post last updated]" + Environment.NewLine + @" [blog entry URL]" + Environment.NewLine + @"" + Environment.NewLine + //MORE CUT OUT @"" + Environment.NewLine + @""; #endregion XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(cachedXmlResponse); return xmlDoc.SelectNodes("/tapi/document/item"); } } }