/// <summary>
        ///     Adds everything possible to target from another source.
        ///     This may include new comments, full comments, larger text etc.
        /// </summary>
        /// <param name="target">Object to add data to.</param>
        /// <param name="otherSource">Object to use data from.</param>
        public bool AddData(EntryPage target, EntryPage otherSource)
        {
            bool updated = false;

            // Check arguments.
            if (target == null || otherSource == null)
            {
                throw new ArgumentNullException();
            }

            // Entry.
            log.DebugFormat("Updating content for entry page '{0}' from other source.", target);
            updated |= _entryHelper.UpdateWith(target.Entry, otherSource.Entry);

            // Comments.
            log.DebugFormat("Updating comments for entry page '{0}' from other source.", target);
            updated |= _repliesHelper.MergeFrom(target, otherSource);

            // CommentPages.
            if (target.CommentPages != null && !CommentPages.IsEmpty(target.CommentPages))
            {
                log.DebugFormat("Cleaning comment pages information for entry page '{0}'.", target);
                target.CommentPages = CommentPages.Empty;
                updated             = true;
            }

            return(updated);
        }
        public void DownloadsCommentPagesCorrectly()
        {
            int source = 4;
            int total = 10;

            ILJClient clientMock = MockRepository.GenerateMock<ILJClient>();
            ILayerParser parserMock = MockRepository.GenerateMock<ILayerParser>();

            // Creates a comment pages object by page index.
            Func<int, CommentPages> createCPByPage = cpi =>
            {
                CommentPages c = new CommentPages();
                c.Current = cpi;
                c.Total = total;
                if(c.Current != 1)
                {
                    c.FirstUrl = new LiveJournalTarget("galkovsky", 1, page: 1).ToString();
                    c.PrevUrl = new LiveJournalTarget("galkovsky", 1, page: c.Current - 1).ToString();
                }
                if(c.Current != total)
                {
                    c.LastUrl = new LiveJournalTarget("galkovsky", 1, page: total).ToString();
                    c.NextUrl = new LiveJournalTarget("galkovsky", 1, page: c.Current + 1).ToString();
                }
                return c;
            };

            clientMock.Expect(z => z.GetContent(Arg<LiveJournalTarget>.Is.NotNull, Arg<ILJClientData>.Is.Null))
                 .Return(null)
                 .WhenCalled(_ =>
                 {
                     LiveJournalTarget t = (LiveJournalTarget)_.Arguments[0];
                     int page = t.Page.Value;
                     _.ReturnValue = page.ToString();
                 });

            parserMock.Expect(z => z.ParseAsAnEntryPage(Arg<string>.Is.Anything))
                .Return(null)
                .WhenCalled(_ =>
                {
                    string req = (string)_.Arguments[0];

                    EntryPage ep = new EntryPage();
                    ep.CommentPages = createCPByPage(int.Parse(req));
                    _.ReturnValue = ep;
                });

            OtherPagesLoader opl = new OtherPagesLoader(parserMock, clientMock);

            // This is the source object we get from an entry page.
            CommentPages cp = createCPByPage(source);
            EntryPage[] others = opl.LoadOtherCommentPages(cp, null);

            Assert.AreEqual(total - 1, others.Length);

            IEnumerable<int> numbersWeExpect = Enumerable.Range(1, total).Where(z => z != source);
            IEnumerable<int> numbersWeHave = others.Select(z => z.CommentPages.Current);
            CollectionAssert.AreEqual(numbersWeExpect, numbersWeHave);
        }
        public void EnsuresTargetCommentsPagesDataIsNull(bool startFromEmpty)
        {
            EntryPage target = startFromEmpty ? new EntryPage() : TestingShared.GenerateEntryPage();
            EntryPage source = TestingShared.GenerateEntryPage(true);

            _eph.AddData(target, source);

            Assert.IsTrue(CommentPages.IsEmpty(target.CommentPages));
        }
        public void SaysItsEmptyOnlyWhenItIsReallyEmpty()
        {
            CommentPages cp = new CommentPages();
            Assert.IsTrue(CommentPages.IsEmpty(cp));

            Action<CommentPages>[] changers = new Action<CommentPages>[]
            {
                z => z.Current = 2,
                z => z.Total = 2,
                z => z.FirstUrl = "http://1",
                z => z.LastUrl = "http://1",
                z => z.PrevUrl = "http://1",
                z => z.NextUrl = "http://1",
            };

            foreach(Action<CommentPages> changer in changers)
            {
                cp = new CommentPages();
                changer(cp);

                Assert.IsFalse(CommentPages.IsEmpty(cp));
            }
        }
示例#5
0
        public void DownloadsCommentPagesCorrectly()
        {
            int source = 4;
            int total  = 10;

            ILJClient    clientMock = MockRepository.GenerateMock <ILJClient>();
            ILayerParser parserMock = MockRepository.GenerateMock <ILayerParser>();

            // Creates a comment pages object by page index.
            Func <int, CommentPages> createCpByPage = cpi =>
            {
                CommentPages c = new CommentPages();
                c.Current = cpi;
                c.Total   = total;
                if (c.Current != 1)
                {
                    c.FirstUrl = new LiveJournalTarget("galkovsky", 1, page: 1).ToString();
                    c.PrevUrl  = new LiveJournalTarget("galkovsky", 1, page: c.Current - 1).ToString();
                }

                if (c.Current != total)
                {
                    c.LastUrl = new LiveJournalTarget("galkovsky", 1, page: total).ToString();
                    c.NextUrl = new LiveJournalTarget("galkovsky", 1, page: c.Current + 1).ToString();
                }

                return(c);
            };

            clientMock.Expect(z => z.GetContent(Arg <LiveJournalTarget> .Is.NotNull, Arg <ILJClientData> .Is.Null))
            .Return(null)
            .WhenCalled(
                _ =>
            {
                LiveJournalTarget t = (LiveJournalTarget)_.Arguments[0];
                int page            = t.Page.Value;
                _.ReturnValue       = page.ToString();
            }
                );

            parserMock.Expect(z => z.ParseAsAnEntryPage(Arg <string> .Is.Anything))
            .Return(null)
            .WhenCalled(
                _ =>
            {
                string req = (string)_.Arguments[0];

                EntryPage ep    = new EntryPage();
                ep.CommentPages = createCpByPage(int.Parse(req));
                _.ReturnValue   = ep;
            }
                );

            OtherPagesLoader opl = new OtherPagesLoader(parserMock, clientMock);

            // This is the source object we get from an entry page.
            CommentPages cp = createCpByPage(source);

            EntryPage[] others = opl.LoadOtherCommentPages(cp, null);

            Assert.AreEqual(total - 1, others.Length);

            IEnumerable <int> numbersWeExpect = Enumerable.Range(1, total)
                                                .Where(z => z != source);
            IEnumerable <int> numbersWeHave = others.Select(z => z.CommentPages.Current);

            CollectionAssert.AreEqual(numbersWeExpect, numbersWeHave);
        }
示例#6
0
        public EntryPage[] LoadOtherCommentPages(CommentPages commentPages, ILJClientData clientData)
        {
            int initialIndex = commentPages.Current;
            int total        = commentPages.Total;

            log.Info(
                string.Format(
                    "Loading other comment pages given page №{0} out of {1}."
                    , commentPages.Current
                    , commentPages.Total
                    )
                );

            // We need to download these.
            int[] need = Enumerable.Range(1, total)
                         .Where(i => i != initialIndex)
                         .ToArray();
            IDictionary <int, LiveJournalTarget> targets = new SortedDictionary <int, LiveJournalTarget>();
            IDictionary <int, EntryPage>         pages   = new SortedDictionary <int, EntryPage>();
            EntryPage p;

            CommentPages latest = commentPages;

            while (pages.Count < need.Length)
            {
                int cur = latest.Current;

                if (cur != 1 && !string.IsNullOrWhiteSpace(latest.FirstUrl))
                {
                    targets[1] = LiveJournalTarget.FromString(latest.FirstUrl);
                }

                if (cur != total && !string.IsNullOrWhiteSpace(latest.LastUrl))
                {
                    targets[total] = LiveJournalTarget.FromString(latest.LastUrl);
                }

                if (!string.IsNullOrWhiteSpace(latest.PrevUrl))
                {
                    targets[cur - 1] = LiveJournalTarget.FromString(latest.PrevUrl);
                }

                if (!string.IsNullOrWhiteSpace(latest.NextUrl))
                {
                    targets[cur + 1] = LiveJournalTarget.FromString(latest.NextUrl);
                }

                // First target without a page.
                int keyToDownload = targets.Keys.First(z => z != initialIndex && !pages.ContainsKey(z));
                log.Info(string.Format("Will download page №{0}.", keyToDownload));
                LiveJournalTarget targetToDownload = targets[keyToDownload];

                // Download the page.
                string content = _client.GetContent(targetToDownload, clientData);
                p      = _parser.ParseAsAnEntryPage(content);
                latest = p.CommentPages;
                pages[keyToDownload] = p;
                log.Info(string.Format("Parsed page №{0}.", keyToDownload));
            }

            EntryPage[] ret = pages.Values.ToArray();
            return(ret);
        }