diff --git a/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java index 2cfe8926517f79a620230d1031a50ba7933e2792..d9c58f8e3d4e6d2391dfcc4a996b7cb164ccb0a4 100644 --- a/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/feed/FeedManagerImpl.java @@ -398,7 +398,7 @@ class FeedManagerImpl implements FeedManager, Client, EventListener, } } - private long postFeedEntries(Feed feed, List<SyndEntry> entries) + long postFeedEntries(Feed feed, List<SyndEntry> entries) throws DbException { long lastEntryTime = feed.getLastEntryTime(); @@ -501,25 +501,20 @@ class FeedManagerImpl implements FeedManager, Client, EventListener, return StringUtils.truncateUtf8(text, MAX_BLOG_POST_BODY_LENGTH); } - /** - * This Comparator assumes that SyndEntry returns a valid Date either for - * getPublishedDate() or getUpdatedDate(). - */ private Comparator<SyndEntry> getEntryComparator() { return new Comparator<SyndEntry>() { @Override public int compare(SyndEntry e1, SyndEntry e2) { - if (e1.getPublishedDate() == null && - e1.getUpdatedDate() == null) { - // we will be ignoring such entries anyway - return 0; - } Date d1 = e1.getPublishedDate() != null ? e1.getPublishedDate() : e1.getUpdatedDate(); Date d2 = e2.getPublishedDate() != null ? e2.getPublishedDate() : e2.getUpdatedDate(); + if (d1 == null && d2 == null) return 0; + if (d1 == null) return -1; + if (d2 == null) return 1; + if (d1.after(d2)) return 1; if (d1.before(d2)) return -1; return 0; diff --git a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerImplTest.java b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerImplTest.java index d8618425113e640b40c8ebf5197d33e4d9c14ba4..7dafcd48e52b9bc32b0dfb0fbc90a4e4cd7a5dba 100644 --- a/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerImplTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/feed/FeedManagerImplTest.java @@ -1,5 +1,8 @@ package org.briarproject.briar.feed; +import com.rometools.rome.feed.synd.SyndEntry; +import com.rometools.rome.feed.synd.SyndEntryImpl; + import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.client.ContactGroupFactory; import org.briarproject.bramble.api.data.BdfDictionary; @@ -11,17 +14,23 @@ import org.briarproject.bramble.api.identity.AuthorId; import org.briarproject.bramble.api.identity.LocalAuthor; import org.briarproject.bramble.api.sync.Group; import org.briarproject.bramble.api.sync.GroupId; +import org.briarproject.bramble.api.sync.Message; +import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.briar.api.blog.Blog; import org.briarproject.briar.api.blog.BlogManager; +import org.briarproject.briar.api.blog.BlogPost; import org.briarproject.briar.api.blog.BlogPostFactory; import org.briarproject.briar.api.feed.Feed; import org.jmock.Expectations; import org.junit.Test; import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; @@ -93,6 +102,36 @@ public class FeedManagerImplTest extends BrambleMockTestCase { feedManager.fetchFeeds(); } + @Test + public void testPostFeedEntriesEmptyDate() throws Exception { + final Transaction txn = new Transaction(null, false); + List<SyndEntry> entries = new ArrayList<SyndEntry>(); + entries.add(new SyndEntryImpl()); + final SyndEntry entry = new SyndEntryImpl(); + entry.setUpdatedDate(new Date()); + entries.add(entry); + final String body = + "<p> (" + entry.getUpdatedDate().toString() + ")</p>"; + Message msg = new Message(new MessageId(getRandomId()), blogGroupId, 0, + getRandomBytes(42)); + final BlogPost post = new BlogPost(msg, null, localAuthor); + + context.checking(new Expectations() {{ + oneOf(db).startTransaction(false); + will(returnValue(txn)); + oneOf(clock).currentTimeMillis(); + will(returnValue(42L)); + oneOf(blogPostFactory) + .createBlogPost(feed.getBlogId(), 42L, null, localAuthor, + body); + will(returnValue(post)); + oneOf(blogManager).addLocalPost(txn, post); + oneOf(db).commitTransaction(txn); + oneOf(db).endTransaction(txn); + }}); + feedManager.postFeedEntries(feed, entries); + } + private void expectGetLocalGroup() { context.checking(new Expectations() {{ oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);