mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Fix NPE when some RSS items don't have dates and add test
This commit is contained in:
@@ -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 {
|
throws DbException {
|
||||||
|
|
||||||
long lastEntryTime = feed.getLastEntryTime();
|
long lastEntryTime = feed.getLastEntryTime();
|
||||||
@@ -501,25 +501,20 @@ class FeedManagerImpl implements FeedManager, Client, EventListener,
|
|||||||
return StringUtils.truncateUtf8(text, MAX_BLOG_POST_BODY_LENGTH);
|
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() {
|
private Comparator<SyndEntry> getEntryComparator() {
|
||||||
return new Comparator<SyndEntry>() {
|
return new Comparator<SyndEntry>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(SyndEntry e1, SyndEntry e2) {
|
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 =
|
Date d1 =
|
||||||
e1.getPublishedDate() != null ? e1.getPublishedDate() :
|
e1.getPublishedDate() != null ? e1.getPublishedDate() :
|
||||||
e1.getUpdatedDate();
|
e1.getUpdatedDate();
|
||||||
Date d2 =
|
Date d2 =
|
||||||
e2.getPublishedDate() != null ? e2.getPublishedDate() :
|
e2.getPublishedDate() != null ? e2.getPublishedDate() :
|
||||||
e2.getUpdatedDate();
|
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.after(d2)) return 1;
|
||||||
if (d1.before(d2)) return -1;
|
if (d1.before(d2)) return -1;
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.briarproject.briar.feed;
|
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.ClientHelper;
|
||||||
import org.briarproject.bramble.api.client.ContactGroupFactory;
|
import org.briarproject.bramble.api.client.ContactGroupFactory;
|
||||||
import org.briarproject.bramble.api.data.BdfDictionary;
|
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.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.sync.Group;
|
import org.briarproject.bramble.api.sync.Group;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
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.api.system.Clock;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||||
import org.briarproject.briar.api.blog.Blog;
|
import org.briarproject.briar.api.blog.Blog;
|
||||||
import org.briarproject.briar.api.blog.BlogManager;
|
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.blog.BlogPostFactory;
|
||||||
import org.briarproject.briar.api.feed.Feed;
|
import org.briarproject.briar.api.feed.Feed;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
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.Executor;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
@@ -93,6 +102,36 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
|||||||
feedManager.fetchFeeds();
|
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() {
|
private void expectGetLocalGroup() {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
|
oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
|
||||||
|
|||||||
Reference in New Issue
Block a user